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); |
| 354 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 355 | EXPORT_SYMBOL_GPL(mddev_resume); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 356 | |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 357 | int mddev_congested(mddev_t *mddev, int bits) |
| 358 | { |
| 359 | return mddev->suspended; |
| 360 | } |
| 361 | EXPORT_SYMBOL(mddev_congested); |
| 362 | |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 363 | /* |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 364 | * Generic flush handling for md |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 365 | */ |
| 366 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 367 | static void md_end_flush(struct bio *bio, int err) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 368 | { |
| 369 | mdk_rdev_t *rdev = bio->bi_private; |
| 370 | mddev_t *mddev = rdev->mddev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 371 | |
| 372 | rdev_dec_pending(rdev, mddev); |
| 373 | |
| 374 | if (atomic_dec_and_test(&mddev->flush_pending)) { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 375 | /* The pre-request flush has finished */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 376 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 377 | } |
| 378 | bio_put(bio); |
| 379 | } |
| 380 | |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 381 | static void md_submit_flush_data(struct work_struct *ws); |
| 382 | |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 383 | static void submit_flushes(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 384 | { |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 385 | mddev_t *mddev = container_of(ws, mddev_t, flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 386 | mdk_rdev_t *rdev; |
| 387 | |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 388 | INIT_WORK(&mddev->flush_work, md_submit_flush_data); |
| 389 | atomic_set(&mddev->flush_pending, 1); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 390 | rcu_read_lock(); |
| 391 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
| 392 | if (rdev->raid_disk >= 0 && |
| 393 | !test_bit(Faulty, &rdev->flags)) { |
| 394 | /* Take two references, one is dropped |
| 395 | * when request finishes, one after |
| 396 | * we reclaim rcu_read_lock |
| 397 | */ |
| 398 | struct bio *bi; |
| 399 | atomic_inc(&rdev->nr_pending); |
| 400 | atomic_inc(&rdev->nr_pending); |
| 401 | rcu_read_unlock(); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 402 | bi = bio_alloc_mddev(GFP_KERNEL, 0, mddev); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 403 | bi->bi_end_io = md_end_flush; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 404 | bi->bi_private = rdev; |
| 405 | bi->bi_bdev = rdev->bdev; |
| 406 | atomic_inc(&mddev->flush_pending); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 407 | submit_bio(WRITE_FLUSH, bi); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 408 | rcu_read_lock(); |
| 409 | rdev_dec_pending(rdev, mddev); |
| 410 | } |
| 411 | rcu_read_unlock(); |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 412 | if (atomic_dec_and_test(&mddev->flush_pending)) |
| 413 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 414 | } |
| 415 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 416 | static void md_submit_flush_data(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 417 | { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 418 | mddev_t *mddev = container_of(ws, mddev_t, flush_work); |
| 419 | struct bio *bio = mddev->flush_bio; |
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 | if (bio->bi_size == 0) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 422 | /* an empty barrier - all done */ |
| 423 | bio_endio(bio, 0); |
| 424 | else { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 425 | bio->bi_rw &= ~REQ_FLUSH; |
NeilBrown | 21a52c6 | 2010-04-01 15:02:13 +1100 | [diff] [blame] | 426 | if (mddev->pers->make_request(mddev, bio)) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 427 | generic_make_request(bio); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 428 | } |
NeilBrown | 2b74e12 | 2010-12-09 15:59:01 +1100 | [diff] [blame] | 429 | |
| 430 | mddev->flush_bio = NULL; |
| 431 | wake_up(&mddev->sb_wait); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 432 | } |
| 433 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 434 | void md_flush_request(mddev_t *mddev, struct bio *bio) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 435 | { |
| 436 | spin_lock_irq(&mddev->write_lock); |
| 437 | wait_event_lock_irq(mddev->sb_wait, |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 438 | !mddev->flush_bio, |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 439 | mddev->write_lock, /*nothing*/); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 440 | mddev->flush_bio = bio; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 441 | spin_unlock_irq(&mddev->write_lock); |
| 442 | |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 443 | INIT_WORK(&mddev->flush_work, submit_flushes); |
| 444 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 445 | } |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 446 | EXPORT_SYMBOL(md_flush_request); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 447 | |
NeilBrown | 2ac8740 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 448 | /* Support for plugging. |
| 449 | * This mirrors the plugging support in request_queue, but does not |
| 450 | * require having a whole queue |
| 451 | */ |
| 452 | static void plugger_work(struct work_struct *work) |
| 453 | { |
| 454 | struct plug_handle *plug = |
| 455 | container_of(work, struct plug_handle, unplug_work); |
| 456 | plug->unplug_fn(plug); |
| 457 | } |
| 458 | static void plugger_timeout(unsigned long data) |
| 459 | { |
| 460 | struct plug_handle *plug = (void *)data; |
| 461 | kblockd_schedule_work(NULL, &plug->unplug_work); |
| 462 | } |
| 463 | void plugger_init(struct plug_handle *plug, |
| 464 | void (*unplug_fn)(struct plug_handle *)) |
| 465 | { |
| 466 | plug->unplug_flag = 0; |
| 467 | plug->unplug_fn = unplug_fn; |
| 468 | init_timer(&plug->unplug_timer); |
| 469 | plug->unplug_timer.function = plugger_timeout; |
| 470 | plug->unplug_timer.data = (unsigned long)plug; |
| 471 | INIT_WORK(&plug->unplug_work, plugger_work); |
| 472 | } |
| 473 | EXPORT_SYMBOL_GPL(plugger_init); |
| 474 | |
| 475 | void plugger_set_plug(struct plug_handle *plug) |
| 476 | { |
| 477 | if (!test_and_set_bit(PLUGGED_FLAG, &plug->unplug_flag)) |
| 478 | mod_timer(&plug->unplug_timer, jiffies + msecs_to_jiffies(3)+1); |
| 479 | } |
| 480 | EXPORT_SYMBOL_GPL(plugger_set_plug); |
| 481 | |
| 482 | int plugger_remove_plug(struct plug_handle *plug) |
| 483 | { |
| 484 | if (test_and_clear_bit(PLUGGED_FLAG, &plug->unplug_flag)) { |
| 485 | del_timer(&plug->unplug_timer); |
| 486 | return 1; |
| 487 | } else |
| 488 | return 0; |
| 489 | } |
| 490 | EXPORT_SYMBOL_GPL(plugger_remove_plug); |
| 491 | |
| 492 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | static inline mddev_t *mddev_get(mddev_t *mddev) |
| 494 | { |
| 495 | atomic_inc(&mddev->active); |
| 496 | return mddev; |
| 497 | } |
| 498 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 499 | static void mddev_delayed_delete(struct work_struct *ws); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 500 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | static void mddev_put(mddev_t *mddev) |
| 502 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 503 | struct bio_set *bs = NULL; |
| 504 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
| 506 | return; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 507 | if (!mddev->raid_disks && list_empty(&mddev->disks) && |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 508 | mddev->ctime == 0 && !mddev->hold_active) { |
| 509 | /* Array is not configured at all, and not held active, |
| 510 | * so destroy it */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | list_del(&mddev->all_mddevs); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 512 | bs = mddev->bio_set; |
| 513 | mddev->bio_set = NULL; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 514 | if (mddev->gendisk) { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 515 | /* We did a probe so need to clean up. Call |
| 516 | * queue_work inside the spinlock so that |
| 517 | * flush_workqueue() after mddev_find will |
| 518 | * succeed in waiting for the work to be done. |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 519 | */ |
| 520 | INIT_WORK(&mddev->del_work, mddev_delayed_delete); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 521 | queue_work(md_misc_wq, &mddev->del_work); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 522 | } else |
| 523 | kfree(mddev); |
| 524 | } |
| 525 | spin_unlock(&all_mddevs_lock); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 526 | if (bs) |
| 527 | bioset_free(bs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | } |
| 529 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 530 | void mddev_init(mddev_t *mddev) |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 531 | { |
| 532 | mutex_init(&mddev->open_mutex); |
| 533 | mutex_init(&mddev->reconfig_mutex); |
| 534 | mutex_init(&mddev->bitmap_info.mutex); |
| 535 | INIT_LIST_HEAD(&mddev->disks); |
| 536 | INIT_LIST_HEAD(&mddev->all_mddevs); |
| 537 | init_timer(&mddev->safemode_timer); |
| 538 | atomic_set(&mddev->active, 1); |
| 539 | atomic_set(&mddev->openers, 0); |
| 540 | atomic_set(&mddev->active_io, 0); |
| 541 | spin_lock_init(&mddev->write_lock); |
| 542 | atomic_set(&mddev->flush_pending, 0); |
| 543 | init_waitqueue_head(&mddev->sb_wait); |
| 544 | init_waitqueue_head(&mddev->recovery_wait); |
| 545 | mddev->reshape_position = MaxSector; |
| 546 | mddev->resync_min = 0; |
| 547 | mddev->resync_max = MaxSector; |
| 548 | mddev->level = LEVEL_NONE; |
| 549 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 550 | EXPORT_SYMBOL_GPL(mddev_init); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 551 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | static mddev_t * mddev_find(dev_t unit) |
| 553 | { |
| 554 | mddev_t *mddev, *new = NULL; |
| 555 | |
NeilBrown | 8f5f02c | 2011-02-16 13:58:51 +1100 | [diff] [blame] | 556 | if (unit && MAJOR(unit) != MD_MAJOR) |
| 557 | unit &= ~((1<<MdpMinorShift)-1); |
| 558 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | retry: |
| 560 | spin_lock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 562 | if (unit) { |
| 563 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 564 | if (mddev->unit == unit) { |
| 565 | mddev_get(mddev); |
| 566 | spin_unlock(&all_mddevs_lock); |
| 567 | kfree(new); |
| 568 | return mddev; |
| 569 | } |
| 570 | |
| 571 | if (new) { |
| 572 | list_add(&new->all_mddevs, &all_mddevs); |
| 573 | spin_unlock(&all_mddevs_lock); |
| 574 | new->hold_active = UNTIL_IOCTL; |
| 575 | return new; |
| 576 | } |
| 577 | } else if (new) { |
| 578 | /* find an unused unit number */ |
| 579 | static int next_minor = 512; |
| 580 | int start = next_minor; |
| 581 | int is_free = 0; |
| 582 | int dev = 0; |
| 583 | while (!is_free) { |
| 584 | dev = MKDEV(MD_MAJOR, next_minor); |
| 585 | next_minor++; |
| 586 | if (next_minor > MINORMASK) |
| 587 | next_minor = 0; |
| 588 | if (next_minor == start) { |
| 589 | /* Oh dear, all in use. */ |
| 590 | spin_unlock(&all_mddevs_lock); |
| 591 | kfree(new); |
| 592 | return NULL; |
| 593 | } |
| 594 | |
| 595 | is_free = 1; |
| 596 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 597 | if (mddev->unit == dev) { |
| 598 | is_free = 0; |
| 599 | break; |
| 600 | } |
| 601 | } |
| 602 | new->unit = dev; |
| 603 | new->md_minor = MINOR(dev); |
| 604 | new->hold_active = UNTIL_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | list_add(&new->all_mddevs, &all_mddevs); |
| 606 | spin_unlock(&all_mddevs_lock); |
| 607 | return new; |
| 608 | } |
| 609 | spin_unlock(&all_mddevs_lock); |
| 610 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 611 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | if (!new) |
| 613 | return NULL; |
| 614 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | new->unit = unit; |
| 616 | if (MAJOR(unit) == MD_MAJOR) |
| 617 | new->md_minor = MINOR(unit); |
| 618 | else |
| 619 | new->md_minor = MINOR(unit) >> MdpMinorShift; |
| 620 | |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 621 | mddev_init(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | goto retry; |
| 624 | } |
| 625 | |
| 626 | static inline int mddev_lock(mddev_t * mddev) |
| 627 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 628 | return mutex_lock_interruptible(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | } |
| 630 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 631 | static inline int mddev_is_locked(mddev_t *mddev) |
| 632 | { |
| 633 | return mutex_is_locked(&mddev->reconfig_mutex); |
| 634 | } |
| 635 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | static inline int mddev_trylock(mddev_t * mddev) |
| 637 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 638 | return mutex_trylock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | } |
| 640 | |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 641 | static struct attribute_group md_redundancy_group; |
| 642 | |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 643 | static void mddev_unlock(mddev_t * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | { |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 645 | if (mddev->to_remove) { |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 646 | /* These cannot be removed under reconfig_mutex as |
| 647 | * an access to the files will try to take reconfig_mutex |
| 648 | * while holding the file unremovable, which leads to |
| 649 | * a deadlock. |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 650 | * So hold set sysfs_active while the remove in happeing, |
| 651 | * and anything else which might set ->to_remove or my |
| 652 | * otherwise change the sysfs namespace will fail with |
| 653 | * -EBUSY if sysfs_active is still set. |
| 654 | * We set sysfs_active under reconfig_mutex and elsewhere |
| 655 | * test it under the same mutex to ensure its correct value |
| 656 | * is seen. |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 657 | */ |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 658 | struct attribute_group *to_remove = mddev->to_remove; |
| 659 | mddev->to_remove = NULL; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 660 | mddev->sysfs_active = 1; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 661 | mutex_unlock(&mddev->reconfig_mutex); |
| 662 | |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 663 | if (mddev->kobj.sd) { |
| 664 | if (to_remove != &md_redundancy_group) |
| 665 | sysfs_remove_group(&mddev->kobj, to_remove); |
| 666 | if (mddev->pers == NULL || |
| 667 | mddev->pers->sync_request == NULL) { |
| 668 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 669 | if (mddev->sysfs_action) |
| 670 | sysfs_put(mddev->sysfs_action); |
| 671 | mddev->sysfs_action = NULL; |
| 672 | } |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 673 | } |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 674 | mddev->sysfs_active = 0; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 675 | } else |
| 676 | mutex_unlock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 678 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | } |
| 680 | |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 681 | static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 683 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 685 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | if (rdev->desc_nr == nr) |
| 687 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 688 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | return NULL; |
| 690 | } |
| 691 | |
| 692 | static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev) |
| 693 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | mdk_rdev_t *rdev; |
| 695 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 696 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | if (rdev->bdev->bd_dev == dev) |
| 698 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 699 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | return NULL; |
| 701 | } |
| 702 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 703 | static struct mdk_personality *find_pers(int level, char *clevel) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 704 | { |
| 705 | struct mdk_personality *pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 706 | list_for_each_entry(pers, &pers_list, list) { |
| 707 | if (level != LEVEL_NONE && pers->level == level) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 708 | return pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 709 | if (strcmp(pers->name, clevel)==0) |
| 710 | return pers; |
| 711 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 712 | return NULL; |
| 713 | } |
| 714 | |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 715 | /* return the offset of the super block in 512byte sectors */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 716 | static inline sector_t calc_dev_sboffset(mdk_rdev_t *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | { |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 718 | sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512; |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 719 | return MD_NEW_SIZE_SECTORS(num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | } |
| 721 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | static int alloc_disk_sb(mdk_rdev_t * rdev) |
| 723 | { |
| 724 | if (rdev->sb_page) |
| 725 | MD_BUG(); |
| 726 | |
| 727 | rdev->sb_page = alloc_page(GFP_KERNEL); |
| 728 | if (!rdev->sb_page) { |
| 729 | printk(KERN_ALERT "md: out of memory.\n"); |
Andre Noll | ebc2433 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 730 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | return 0; |
| 734 | } |
| 735 | |
| 736 | static void free_disk_sb(mdk_rdev_t * rdev) |
| 737 | { |
| 738 | if (rdev->sb_page) { |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 739 | put_page(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | rdev->sb_loaded = 0; |
| 741 | rdev->sb_page = NULL; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 742 | rdev->sb_start = 0; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 743 | rdev->sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | } |
| 745 | } |
| 746 | |
| 747 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 748 | static void super_written(struct bio *bio, int error) |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 749 | { |
| 750 | mdk_rdev_t *rdev = bio->bi_private; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 751 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 752 | |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 753 | if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) { |
| 754 | printk("md: super_written gets error=%d, uptodate=%d\n", |
| 755 | error, test_bit(BIO_UPTODATE, &bio->bi_flags)); |
| 756 | WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags)); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 757 | md_error(mddev, rdev); |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 758 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 759 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 760 | if (atomic_dec_and_test(&mddev->pending_writes)) |
| 761 | wake_up(&mddev->sb_wait); |
Neil Brown | f8b58ed | 2005-06-27 22:29:34 -0700 | [diff] [blame] | 762 | bio_put(bio); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev, |
| 766 | sector_t sector, int size, struct page *page) |
| 767 | { |
| 768 | /* write first size bytes of page to sector of rdev |
| 769 | * Increment mddev->pending_writes before returning |
| 770 | * and decrement it on completion, waking up sb_wait |
| 771 | * if zero is reached. |
| 772 | * If an error occurred, call md_error |
| 773 | */ |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 774 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 775 | |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 776 | bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 777 | bio->bi_sector = sector; |
| 778 | bio_add_page(bio, page, size, 0); |
| 779 | bio->bi_private = rdev; |
| 780 | bio->bi_end_io = super_written; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 781 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 782 | atomic_inc(&mddev->pending_writes); |
Jens Axboe | 721a960 | 2011-03-09 11:56:30 +0100 | [diff] [blame] | 783 | submit_bio(REQ_WRITE | REQ_SYNC | REQ_FLUSH | REQ_FUA, bio); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | void md_super_wait(mddev_t *mddev) |
| 787 | { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 788 | /* wait for all superblock writes that were scheduled to complete */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 789 | DEFINE_WAIT(wq); |
| 790 | for(;;) { |
| 791 | prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE); |
| 792 | if (atomic_read(&mddev->pending_writes)==0) |
| 793 | break; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 794 | schedule(); |
| 795 | } |
| 796 | finish_wait(&mddev->sb_wait, &wq); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 797 | } |
| 798 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 799 | static void bi_complete(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | complete((struct completion*)bio->bi_private); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | } |
| 803 | |
NeilBrown | 2b19336 | 2010-10-27 15:16:40 +1100 | [diff] [blame] | 804 | 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] | 805 | struct page *page, int rw, bool metadata_op) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 807 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | struct completion event; |
| 809 | int ret; |
| 810 | |
Jens Axboe | 721a960 | 2011-03-09 11:56:30 +0100 | [diff] [blame] | 811 | rw |= REQ_SYNC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 813 | bio->bi_bdev = (metadata_op && rdev->meta_bdev) ? |
| 814 | rdev->meta_bdev : rdev->bdev; |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 815 | if (metadata_op) |
| 816 | bio->bi_sector = sector + rdev->sb_start; |
| 817 | else |
| 818 | bio->bi_sector = sector + rdev->data_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | bio_add_page(bio, page, size, 0); |
| 820 | init_completion(&event); |
| 821 | bio->bi_private = &event; |
| 822 | bio->bi_end_io = bi_complete; |
| 823 | submit_bio(rw, bio); |
| 824 | wait_for_completion(&event); |
| 825 | |
| 826 | ret = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 827 | bio_put(bio); |
| 828 | return ret; |
| 829 | } |
NeilBrown | a8745db | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 830 | EXPORT_SYMBOL_GPL(sync_page_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 832 | static int read_disk_sb(mdk_rdev_t * rdev, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | { |
| 834 | char b[BDEVNAME_SIZE]; |
| 835 | if (!rdev->sb_page) { |
| 836 | MD_BUG(); |
| 837 | return -EINVAL; |
| 838 | } |
| 839 | if (rdev->sb_loaded) |
| 840 | return 0; |
| 841 | |
| 842 | |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 843 | if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | goto fail; |
| 845 | rdev->sb_loaded = 1; |
| 846 | return 0; |
| 847 | |
| 848 | fail: |
| 849 | printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n", |
| 850 | bdevname(rdev->bdev,b)); |
| 851 | return -EINVAL; |
| 852 | } |
| 853 | |
| 854 | static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 855 | { |
Andre Noll | 0571046 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 856 | return sb1->set_uuid0 == sb2->set_uuid0 && |
| 857 | sb1->set_uuid1 == sb2->set_uuid1 && |
| 858 | sb1->set_uuid2 == sb2->set_uuid2 && |
| 859 | sb1->set_uuid3 == sb2->set_uuid3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | } |
| 861 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 863 | { |
| 864 | int ret; |
| 865 | mdp_super_t *tmp1, *tmp2; |
| 866 | |
| 867 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); |
| 868 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); |
| 869 | |
| 870 | if (!tmp1 || !tmp2) { |
| 871 | ret = 0; |
Andre Noll | 35020f1 | 2008-03-23 15:10:33 +0100 | [diff] [blame] | 872 | printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | goto abort; |
| 874 | } |
| 875 | |
| 876 | *tmp1 = *sb1; |
| 877 | *tmp2 = *sb2; |
| 878 | |
| 879 | /* |
| 880 | * nr_disks is not constant |
| 881 | */ |
| 882 | tmp1->nr_disks = 0; |
| 883 | tmp2->nr_disks = 0; |
| 884 | |
Andre Noll | ce0c8e0 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 885 | ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | abort: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 887 | kfree(tmp1); |
| 888 | kfree(tmp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | return ret; |
| 890 | } |
| 891 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 892 | |
| 893 | static u32 md_csum_fold(u32 csum) |
| 894 | { |
| 895 | csum = (csum & 0xffff) + (csum >> 16); |
| 896 | return (csum & 0xffff) + (csum >> 16); |
| 897 | } |
| 898 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | static unsigned int calc_sb_csum(mdp_super_t * sb) |
| 900 | { |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 901 | u64 newcsum = 0; |
| 902 | u32 *sb32 = (u32*)sb; |
| 903 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 904 | unsigned int disk_csum, csum; |
| 905 | |
| 906 | disk_csum = sb->sb_csum; |
| 907 | sb->sb_csum = 0; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 908 | |
| 909 | for (i = 0; i < MD_SB_BYTES/4 ; i++) |
| 910 | newcsum += sb32[i]; |
| 911 | csum = (newcsum & 0xffffffff) + (newcsum>>32); |
| 912 | |
| 913 | |
| 914 | #ifdef CONFIG_ALPHA |
| 915 | /* This used to use csum_partial, which was wrong for several |
| 916 | * reasons including that different results are returned on |
| 917 | * different architectures. It isn't critical that we get exactly |
| 918 | * the same return value as before (we always csum_fold before |
| 919 | * testing, and that removes any differences). However as we |
| 920 | * know that csum_partial always returned a 16bit value on |
| 921 | * alphas, do a fold to maximise conformity to previous behaviour. |
| 922 | */ |
| 923 | sb->sb_csum = md_csum_fold(disk_csum); |
| 924 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | sb->sb_csum = disk_csum; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 926 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | return csum; |
| 928 | } |
| 929 | |
| 930 | |
| 931 | /* |
| 932 | * Handle superblock details. |
| 933 | * We want to be able to handle multiple superblock formats |
| 934 | * so we have a common interface to them all, and an array of |
| 935 | * different handlers. |
| 936 | * We rely on user-space to write the initial superblock, and support |
| 937 | * reading and updating of superblocks. |
| 938 | * Interface methods are: |
| 939 | * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version) |
| 940 | * loads and validates a superblock on dev. |
| 941 | * if refdev != NULL, compare superblocks on both devices |
| 942 | * Return: |
| 943 | * 0 - dev has a superblock that is compatible with refdev |
| 944 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 945 | * so dev should be used as the refdev in future |
| 946 | * -EINVAL superblock incompatible or invalid |
| 947 | * -othererror e.g. -EIO |
| 948 | * |
| 949 | * int validate_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 950 | * Verify that dev is acceptable into mddev. |
| 951 | * The first time, mddev->raid_disks will be 0, and data from |
| 952 | * dev should be merged in. Subsequent calls check that dev |
| 953 | * is new enough. Return 0 or -EINVAL |
| 954 | * |
| 955 | * void sync_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 956 | * Update the superblock for rdev with data in mddev |
| 957 | * This does not write to disc. |
| 958 | * |
| 959 | */ |
| 960 | |
| 961 | struct super_type { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 962 | char *name; |
| 963 | struct module *owner; |
| 964 | int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, |
| 965 | int minor_version); |
| 966 | int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 967 | void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 968 | unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev, |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 969 | sector_t num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | }; |
| 971 | |
| 972 | /* |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 973 | * Check that the given mddev has no bitmap. |
| 974 | * |
| 975 | * This function is called from the run method of all personalities that do not |
| 976 | * support bitmaps. It prints an error message and returns non-zero if mddev |
| 977 | * has a bitmap. Otherwise, it returns 0. |
| 978 | * |
| 979 | */ |
| 980 | int md_check_no_bitmap(mddev_t *mddev) |
| 981 | { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 982 | if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 983 | return 0; |
| 984 | printk(KERN_ERR "%s: bitmaps are not supported for %s\n", |
| 985 | mdname(mddev), mddev->pers->name); |
| 986 | return 1; |
| 987 | } |
| 988 | EXPORT_SYMBOL(md_check_no_bitmap); |
| 989 | |
| 990 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | * load_super for 0.90.0 |
| 992 | */ |
| 993 | static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 994 | { |
| 995 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 996 | mdp_super_t *sb; |
| 997 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | |
| 999 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1000 | * Calculate the position of the superblock (512byte sectors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | * it's at the end of the disk. |
| 1002 | * |
| 1003 | * It also happens to be a multiple of 4Kb. |
| 1004 | */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1005 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1007 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | if (ret) return ret; |
| 1009 | |
| 1010 | ret = -EINVAL; |
| 1011 | |
| 1012 | bdevname(rdev->bdev, b); |
| 1013 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 1014 | |
| 1015 | if (sb->md_magic != MD_SB_MAGIC) { |
| 1016 | printk(KERN_ERR "md: invalid raid superblock magic on %s\n", |
| 1017 | b); |
| 1018 | goto abort; |
| 1019 | } |
| 1020 | |
| 1021 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1022 | sb->minor_version < 90 || |
| 1023 | sb->minor_version > 91) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | printk(KERN_WARNING "Bad version number %d.%d on %s\n", |
| 1025 | sb->major_version, sb->minor_version, |
| 1026 | b); |
| 1027 | goto abort; |
| 1028 | } |
| 1029 | |
| 1030 | if (sb->raid_disks <= 0) |
| 1031 | goto abort; |
| 1032 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 1033 | 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] | 1034 | printk(KERN_WARNING "md: invalid superblock checksum on %s\n", |
| 1035 | b); |
| 1036 | goto abort; |
| 1037 | } |
| 1038 | |
| 1039 | rdev->preferred_minor = sb->md_minor; |
| 1040 | rdev->data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1041 | rdev->sb_size = MD_SB_BYTES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | |
| 1043 | if (sb->level == LEVEL_MULTIPATH) |
| 1044 | rdev->desc_nr = -1; |
| 1045 | else |
| 1046 | rdev->desc_nr = sb->this_disk.number; |
| 1047 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1048 | if (!refdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1050 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | __u64 ev1, ev2; |
| 1052 | mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page); |
| 1053 | if (!uuid_equal(refsb, sb)) { |
| 1054 | printk(KERN_WARNING "md: %s has different UUID to %s\n", |
| 1055 | b, bdevname(refdev->bdev,b2)); |
| 1056 | goto abort; |
| 1057 | } |
| 1058 | if (!sb_equal(refsb, sb)) { |
| 1059 | printk(KERN_WARNING "md: %s has same UUID" |
| 1060 | " but different superblock to %s\n", |
| 1061 | b, bdevname(refdev->bdev, b2)); |
| 1062 | goto abort; |
| 1063 | } |
| 1064 | ev1 = md_event(sb); |
| 1065 | ev2 = md_event(refsb); |
| 1066 | if (ev1 > ev2) |
| 1067 | ret = 1; |
| 1068 | else |
| 1069 | ret = 0; |
| 1070 | } |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 1071 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1073 | if (rdev->sectors < sb->size * 2 && sb->level > 1) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1074 | /* "this cannot possibly happen" ... */ |
| 1075 | ret = -EINVAL; |
| 1076 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | abort: |
| 1078 | return ret; |
| 1079 | } |
| 1080 | |
| 1081 | /* |
| 1082 | * validate_super for 0.90.0 |
| 1083 | */ |
| 1084 | static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1085 | { |
| 1086 | mdp_disk_t *desc; |
| 1087 | mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1088 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1090 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1091 | clear_bit(Faulty, &rdev->flags); |
| 1092 | clear_bit(In_sync, &rdev->flags); |
| 1093 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1094 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | if (mddev->raid_disks == 0) { |
| 1096 | mddev->major_version = 0; |
| 1097 | mddev->minor_version = sb->minor_version; |
| 1098 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1099 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1100 | mddev->chunk_sectors = sb->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1101 | mddev->ctime = sb->ctime; |
| 1102 | mddev->utime = sb->utime; |
| 1103 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1104 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1105 | mddev->layout = sb->layout; |
| 1106 | mddev->raid_disks = sb->raid_disks; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1107 | mddev->dev_sectors = sb->size * 2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1108 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1109 | mddev->bitmap_info.offset = 0; |
| 1110 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1112 | if (mddev->minor_version >= 91) { |
| 1113 | mddev->reshape_position = sb->reshape_position; |
| 1114 | mddev->delta_disks = sb->delta_disks; |
| 1115 | mddev->new_level = sb->new_level; |
| 1116 | mddev->new_layout = sb->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1117 | mddev->new_chunk_sectors = sb->new_chunk >> 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1118 | } else { |
| 1119 | mddev->reshape_position = MaxSector; |
| 1120 | mddev->delta_disks = 0; |
| 1121 | mddev->new_level = mddev->level; |
| 1122 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1123 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1124 | } |
| 1125 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1126 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 1127 | mddev->recovery_cp = MaxSector; |
| 1128 | else { |
| 1129 | if (sb->events_hi == sb->cp_events_hi && |
| 1130 | sb->events_lo == sb->cp_events_lo) { |
| 1131 | mddev->recovery_cp = sb->recovery_cp; |
| 1132 | } else |
| 1133 | mddev->recovery_cp = 0; |
| 1134 | } |
| 1135 | |
| 1136 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 1137 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 1138 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 1139 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 1140 | |
| 1141 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1142 | |
| 1143 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1144 | mddev->bitmap_info.file == NULL) |
| 1145 | mddev->bitmap_info.offset = |
| 1146 | mddev->bitmap_info.default_offset; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1147 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1148 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1149 | /* Insist on good event counter while assembling, except |
| 1150 | * for spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1151 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1152 | if (sb->disks[rdev->desc_nr].state & ( |
| 1153 | (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE))) |
| 1154 | if (ev1 < mddev->events) |
| 1155 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1156 | } else if (mddev->bitmap) { |
| 1157 | /* if adding to array with a bitmap, then we can accept an |
| 1158 | * older device ... but not too old. |
| 1159 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1160 | if (ev1 < mddev->bitmap->events_cleared) |
| 1161 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1162 | } else { |
| 1163 | if (ev1 < mddev->events) |
| 1164 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1165 | return 0; |
| 1166 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1167 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1168 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | desc = sb->disks + rdev->desc_nr; |
| 1170 | |
| 1171 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1172 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 1173 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 1174 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1175 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1176 | rdev->raid_disk = desc->raid_disk; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1177 | } else if (desc->state & (1<<MD_DISK_ACTIVE)) { |
| 1178 | /* active but not in sync implies recovery up to |
| 1179 | * reshape position. We don't know exactly where |
| 1180 | * that is, so set to zero for now */ |
| 1181 | if (mddev->minor_version >= 91) { |
| 1182 | rdev->recovery_offset = 0; |
| 1183 | rdev->raid_disk = desc->raid_disk; |
| 1184 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1186 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 1187 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1188 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1189 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | return 0; |
| 1191 | } |
| 1192 | |
| 1193 | /* |
| 1194 | * sync_super for 0.90.0 |
| 1195 | */ |
| 1196 | static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1197 | { |
| 1198 | mdp_super_t *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1199 | mdk_rdev_t *rdev2; |
| 1200 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1201 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | |
| 1203 | /* make rdev->sb match mddev data.. |
| 1204 | * |
| 1205 | * 1/ zero out disks |
| 1206 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 1207 | * 3/ any empty disks < next_spare become removed |
| 1208 | * |
| 1209 | * disks[0] gets initialised to REMOVED because |
| 1210 | * we cannot be sure from other fields if it has |
| 1211 | * been initialised or not. |
| 1212 | */ |
| 1213 | int i; |
| 1214 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 1215 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 1216 | rdev->sb_size = MD_SB_BYTES; |
| 1217 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1218 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 1219 | |
| 1220 | memset(sb, 0, sizeof(*sb)); |
| 1221 | |
| 1222 | sb->md_magic = MD_SB_MAGIC; |
| 1223 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1224 | sb->patch_version = mddev->patch_version; |
| 1225 | sb->gvalid_words = 0; /* ignored */ |
| 1226 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 1227 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 1228 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 1229 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 1230 | |
| 1231 | sb->ctime = mddev->ctime; |
| 1232 | sb->level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1233 | sb->size = mddev->dev_sectors / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1234 | sb->raid_disks = mddev->raid_disks; |
| 1235 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1236 | sb->not_persistent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1237 | sb->utime = mddev->utime; |
| 1238 | sb->state = 0; |
| 1239 | sb->events_hi = (mddev->events>>32); |
| 1240 | sb->events_lo = (u32)mddev->events; |
| 1241 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1242 | if (mddev->reshape_position == MaxSector) |
| 1243 | sb->minor_version = 90; |
| 1244 | else { |
| 1245 | sb->minor_version = 91; |
| 1246 | sb->reshape_position = mddev->reshape_position; |
| 1247 | sb->new_level = mddev->new_level; |
| 1248 | sb->delta_disks = mddev->delta_disks; |
| 1249 | sb->new_layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1250 | sb->new_chunk = mddev->new_chunk_sectors << 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1251 | } |
| 1252 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1253 | if (mddev->in_sync) |
| 1254 | { |
| 1255 | sb->recovery_cp = mddev->recovery_cp; |
| 1256 | sb->cp_events_hi = (mddev->events>>32); |
| 1257 | sb->cp_events_lo = (u32)mddev->events; |
| 1258 | if (mddev->recovery_cp == MaxSector) |
| 1259 | sb->state = (1<< MD_SB_CLEAN); |
| 1260 | } else |
| 1261 | sb->recovery_cp = 0; |
| 1262 | |
| 1263 | sb->layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1264 | sb->chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1266 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1267 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 1268 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1269 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1270 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1271 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1272 | int desc_nr; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1273 | int is_active = test_bit(In_sync, &rdev2->flags); |
| 1274 | |
| 1275 | if (rdev2->raid_disk >= 0 && |
| 1276 | sb->minor_version >= 91) |
| 1277 | /* we have nowhere to store the recovery_offset, |
| 1278 | * but if it is not below the reshape_position, |
| 1279 | * we can piggy-back on that. |
| 1280 | */ |
| 1281 | is_active = 1; |
| 1282 | if (rdev2->raid_disk < 0 || |
| 1283 | test_bit(Faulty, &rdev2->flags)) |
| 1284 | is_active = 0; |
| 1285 | if (is_active) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1286 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1288 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1289 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1290 | d = &sb->disks[rdev2->desc_nr]; |
| 1291 | nr_disks++; |
| 1292 | d->number = rdev2->desc_nr; |
| 1293 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 1294 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1295 | if (is_active) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | d->raid_disk = rdev2->raid_disk; |
| 1297 | else |
| 1298 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 1299 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1301 | else if (is_active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | d->state = (1<<MD_DISK_ACTIVE); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1303 | if (test_bit(In_sync, &rdev2->flags)) |
| 1304 | d->state |= (1<<MD_DISK_SYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1305 | active++; |
| 1306 | working++; |
| 1307 | } else { |
| 1308 | d->state = 0; |
| 1309 | spare++; |
| 1310 | working++; |
| 1311 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1312 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 1313 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1315 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 1316 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 1317 | mdp_disk_t *d = &sb->disks[i]; |
| 1318 | if (d->state == 0 && d->number == 0) { |
| 1319 | d->number = i; |
| 1320 | d->raid_disk = i; |
| 1321 | d->state = (1<<MD_DISK_REMOVED); |
| 1322 | d->state |= (1<<MD_DISK_FAULTY); |
| 1323 | failed++; |
| 1324 | } |
| 1325 | } |
| 1326 | sb->nr_disks = nr_disks; |
| 1327 | sb->active_disks = active; |
| 1328 | sb->working_disks = working; |
| 1329 | sb->failed_disks = failed; |
| 1330 | sb->spare_disks = spare; |
| 1331 | |
| 1332 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 1333 | sb->sb_csum = calc_sb_csum(sb); |
| 1334 | } |
| 1335 | |
| 1336 | /* |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1337 | * rdev_size_change for 0.90.0 |
| 1338 | */ |
| 1339 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1340 | 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] | 1341 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1342 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1343 | return 0; /* component must fit device */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1344 | if (rdev->mddev->bitmap_info.offset) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1345 | return 0; /* can't move bitmap */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1346 | rdev->sb_start = calc_dev_sboffset(rdev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1347 | if (!num_sectors || num_sectors > rdev->sb_start) |
| 1348 | num_sectors = rdev->sb_start; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1349 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1350 | rdev->sb_page); |
| 1351 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1352 | return num_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1353 | } |
| 1354 | |
| 1355 | |
| 1356 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1357 | * version 1 superblock |
| 1358 | */ |
| 1359 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1360 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1361 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1362 | __le32 disk_csum; |
| 1363 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1364 | unsigned long long newcsum; |
| 1365 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1366 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1367 | int i; |
| 1368 | |
| 1369 | disk_csum = sb->sb_csum; |
| 1370 | sb->sb_csum = 0; |
| 1371 | newcsum = 0; |
| 1372 | for (i=0; size>=4; size -= 4 ) |
| 1373 | newcsum += le32_to_cpu(*isuper++); |
| 1374 | |
| 1375 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1376 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1377 | |
| 1378 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1379 | sb->sb_csum = disk_csum; |
| 1380 | return cpu_to_le32(csum); |
| 1381 | } |
| 1382 | |
| 1383 | static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 1384 | { |
| 1385 | struct mdp_superblock_1 *sb; |
| 1386 | int ret; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1387 | sector_t sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1388 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1389 | int bmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1390 | |
| 1391 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1392 | * Calculate the position of the superblock in 512byte sectors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1393 | * It is always aligned to a 4K boundary and |
| 1394 | * depeding on minor_version, it can be: |
| 1395 | * 0: At least 8K, but less than 12K, from end of device |
| 1396 | * 1: At start of device |
| 1397 | * 2: 4K from start of device. |
| 1398 | */ |
| 1399 | switch(minor_version) { |
| 1400 | case 0: |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1401 | sb_start = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1402 | sb_start -= 8*2; |
| 1403 | sb_start &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1404 | break; |
| 1405 | case 1: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1406 | sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1407 | break; |
| 1408 | case 2: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1409 | sb_start = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1410 | break; |
| 1411 | default: |
| 1412 | return -EINVAL; |
| 1413 | } |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1414 | rdev->sb_start = sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1415 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1416 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1417 | * and it is safe to read 4k, so we do that |
| 1418 | */ |
| 1419 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1420 | if (ret) return ret; |
| 1421 | |
| 1422 | |
| 1423 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1424 | |
| 1425 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1426 | sb->major_version != cpu_to_le32(1) || |
| 1427 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1428 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1429 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1430 | return -EINVAL; |
| 1431 | |
| 1432 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
| 1433 | printk("md: invalid superblock checksum on %s\n", |
| 1434 | bdevname(rdev->bdev,b)); |
| 1435 | return -EINVAL; |
| 1436 | } |
| 1437 | if (le64_to_cpu(sb->data_size) < 10) { |
| 1438 | printk("md: data_size too small on %s\n", |
| 1439 | bdevname(rdev->bdev,b)); |
| 1440 | return -EINVAL; |
| 1441 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1442 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1443 | rdev->preferred_minor = 0xffff; |
| 1444 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1445 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1446 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1447 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1448 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1449 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1450 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1451 | |
| 1452 | if (minor_version |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1453 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1454 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1455 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1456 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1457 | rdev->desc_nr = -1; |
| 1458 | else |
| 1459 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1460 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1461 | if (!refdev) { |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1462 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1463 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1464 | __u64 ev1, ev2; |
| 1465 | struct mdp_superblock_1 *refsb = |
| 1466 | (struct mdp_superblock_1*)page_address(refdev->sb_page); |
| 1467 | |
| 1468 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1469 | sb->level != refsb->level || |
| 1470 | sb->layout != refsb->layout || |
| 1471 | sb->chunksize != refsb->chunksize) { |
| 1472 | printk(KERN_WARNING "md: %s has strangely different" |
| 1473 | " superblock to %s\n", |
| 1474 | bdevname(rdev->bdev,b), |
| 1475 | bdevname(refdev->bdev,b2)); |
| 1476 | return -EINVAL; |
| 1477 | } |
| 1478 | ev1 = le64_to_cpu(sb->events); |
| 1479 | ev2 = le64_to_cpu(refsb->events); |
| 1480 | |
| 1481 | if (ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1482 | ret = 1; |
| 1483 | else |
| 1484 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 | } |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1486 | if (minor_version) |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1487 | rdev->sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) - |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1488 | le64_to_cpu(sb->data_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1489 | else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1490 | rdev->sectors = rdev->sb_start; |
| 1491 | if (rdev->sectors < le64_to_cpu(sb->data_size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1492 | return -EINVAL; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1493 | rdev->sectors = le64_to_cpu(sb->data_size); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1494 | if (le64_to_cpu(sb->size) > rdev->sectors) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1495 | return -EINVAL; |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1496 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 | } |
| 1498 | |
| 1499 | static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1500 | { |
| 1501 | struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1502 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1503 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1504 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1505 | clear_bit(Faulty, &rdev->flags); |
| 1506 | clear_bit(In_sync, &rdev->flags); |
| 1507 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1508 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1509 | if (mddev->raid_disks == 0) { |
| 1510 | mddev->major_version = 1; |
| 1511 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1512 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1513 | mddev->chunk_sectors = le32_to_cpu(sb->chunksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1514 | mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1); |
| 1515 | mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1); |
| 1516 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1517 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1518 | mddev->layout = le32_to_cpu(sb->layout); |
| 1519 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1520 | mddev->dev_sectors = le64_to_cpu(sb->size); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1521 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1522 | mddev->bitmap_info.offset = 0; |
| 1523 | mddev->bitmap_info.default_offset = 1024 >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1524 | |
| 1525 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1526 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1527 | |
| 1528 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1529 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1530 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1531 | mddev->bitmap_info.file == NULL ) |
| 1532 | mddev->bitmap_info.offset = |
| 1533 | (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1534 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1535 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1536 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1537 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1538 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1539 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1540 | mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1541 | } else { |
| 1542 | mddev->reshape_position = MaxSector; |
| 1543 | mddev->delta_disks = 0; |
| 1544 | mddev->new_level = mddev->level; |
| 1545 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1546 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1547 | } |
| 1548 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1549 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1550 | /* Insist of good event counter while assembling, except for |
| 1551 | * spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1552 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1553 | if (rdev->desc_nr >= 0 && |
| 1554 | rdev->desc_nr < le32_to_cpu(sb->max_dev) && |
| 1555 | le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe) |
| 1556 | if (ev1 < mddev->events) |
| 1557 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1558 | } else if (mddev->bitmap) { |
| 1559 | /* If adding to array with a bitmap, then we can accept an |
| 1560 | * older device, but not too old. |
| 1561 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1562 | if (ev1 < mddev->bitmap->events_cleared) |
| 1563 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1564 | } else { |
| 1565 | if (ev1 < mddev->events) |
| 1566 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1567 | return 0; |
| 1568 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1569 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1570 | int role; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1571 | if (rdev->desc_nr < 0 || |
| 1572 | rdev->desc_nr >= le32_to_cpu(sb->max_dev)) { |
| 1573 | role = 0xffff; |
| 1574 | rdev->desc_nr = -1; |
| 1575 | } else |
| 1576 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1577 | switch(role) { |
| 1578 | case 0xffff: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1579 | break; |
| 1580 | case 0xfffe: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1581 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1582 | break; |
| 1583 | default: |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1584 | if ((le32_to_cpu(sb->feature_map) & |
| 1585 | MD_FEATURE_RECOVERY_OFFSET)) |
| 1586 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 1587 | else |
| 1588 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1589 | rdev->raid_disk = role; |
| 1590 | break; |
| 1591 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1592 | if (sb->devflags & WriteMostly1) |
| 1593 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1594 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1595 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1596 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1597 | return 0; |
| 1598 | } |
| 1599 | |
| 1600 | static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1601 | { |
| 1602 | struct mdp_superblock_1 *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1603 | mdk_rdev_t *rdev2; |
| 1604 | int max_dev, i; |
| 1605 | /* make rdev->sb match mddev and rdev data. */ |
| 1606 | |
| 1607 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1608 | |
| 1609 | sb->feature_map = 0; |
| 1610 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1611 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1612 | memset(sb->pad1, 0, sizeof(sb->pad1)); |
| 1613 | memset(sb->pad2, 0, sizeof(sb->pad2)); |
| 1614 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1615 | |
| 1616 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1617 | sb->events = cpu_to_le64(mddev->events); |
| 1618 | if (mddev->in_sync) |
| 1619 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
| 1620 | else |
| 1621 | sb->resync_offset = cpu_to_le64(0); |
| 1622 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1623 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1624 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1625 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1626 | sb->size = cpu_to_le64(mddev->dev_sectors); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1627 | sb->chunksize = cpu_to_le32(mddev->chunk_sectors); |
NeilBrown | 62e1e38 | 2009-05-26 09:40:59 +1000 | [diff] [blame] | 1628 | sb->level = cpu_to_le32(mddev->level); |
| 1629 | sb->layout = cpu_to_le32(mddev->layout); |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1630 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1631 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) { |
| 1632 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1633 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1634 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1635 | |
| 1636 | if (rdev->raid_disk >= 0 && |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1637 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1638 | sb->feature_map |= |
| 1639 | cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 1640 | sb->recovery_offset = |
| 1641 | cpu_to_le64(rdev->recovery_offset); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1642 | } |
| 1643 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1644 | if (mddev->reshape_position != MaxSector) { |
| 1645 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 1646 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1647 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1648 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1649 | sb->new_level = cpu_to_le32(mddev->new_level); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1650 | sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1651 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1652 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1653 | max_dev = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1654 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1655 | if (rdev2->desc_nr+1 > max_dev) |
| 1656 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1657 | |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1658 | if (max_dev > le32_to_cpu(sb->max_dev)) { |
| 1659 | int bmask; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1660 | sb->max_dev = cpu_to_le32(max_dev); |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1661 | rdev->sb_size = max_dev * 2 + 256; |
| 1662 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
| 1663 | if (rdev->sb_size & bmask) |
| 1664 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
NeilBrown | ddcf352 | 2010-09-08 16:48:17 +1000 | [diff] [blame] | 1665 | } else |
| 1666 | max_dev = le32_to_cpu(sb->max_dev); |
| 1667 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1668 | for (i=0; i<max_dev;i++) |
| 1669 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
| 1670 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1671 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1672 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1673 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1674 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1675 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1676 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1677 | else if (rdev2->raid_disk >= 0) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1678 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1679 | else |
| 1680 | sb->dev_roles[i] = cpu_to_le16(0xffff); |
| 1681 | } |
| 1682 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1683 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1684 | } |
| 1685 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1686 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1687 | 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] | 1688 | { |
| 1689 | struct mdp_superblock_1 *sb; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1690 | sector_t max_sectors; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1691 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1692 | return 0; /* component must fit device */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1693 | if (rdev->sb_start < rdev->data_offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1694 | /* minor versions 1 and 2; superblock before data */ |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1695 | max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1696 | max_sectors -= rdev->data_offset; |
| 1697 | if (!num_sectors || num_sectors > max_sectors) |
| 1698 | num_sectors = max_sectors; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1699 | } else if (rdev->mddev->bitmap_info.offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1700 | /* minor version 0 with bitmap we can't move */ |
| 1701 | return 0; |
| 1702 | } else { |
| 1703 | /* minor version 0; superblock after data */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1704 | sector_t sb_start; |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1705 | sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1706 | sb_start &= ~(sector_t)(4*2 - 1); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1707 | max_sectors = rdev->sectors + sb_start - rdev->sb_start; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1708 | if (!num_sectors || num_sectors > max_sectors) |
| 1709 | num_sectors = max_sectors; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1710 | rdev->sb_start = sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1711 | } |
| 1712 | sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1713 | sb->data_size = cpu_to_le64(num_sectors); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1714 | sb->super_offset = rdev->sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1715 | sb->sb_csum = calc_sb_1_csum(sb); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1716 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1717 | rdev->sb_page); |
| 1718 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1719 | return num_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1720 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1721 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 1722 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1723 | [0] = { |
| 1724 | .name = "0.90.0", |
| 1725 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1726 | .load_super = super_90_load, |
| 1727 | .validate_super = super_90_validate, |
| 1728 | .sync_super = super_90_sync, |
| 1729 | .rdev_size_change = super_90_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1730 | }, |
| 1731 | [1] = { |
| 1732 | .name = "md-1", |
| 1733 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1734 | .load_super = super_1_load, |
| 1735 | .validate_super = super_1_validate, |
| 1736 | .sync_super = super_1_sync, |
| 1737 | .rdev_size_change = super_1_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1738 | }, |
| 1739 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1740 | |
| 1741 | static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2) |
| 1742 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1743 | mdk_rdev_t *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1744 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1745 | rcu_read_lock(); |
| 1746 | rdev_for_each_rcu(rdev, mddev1) |
| 1747 | rdev_for_each_rcu(rdev2, mddev2) |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1748 | if (rdev->bdev->bd_contains == |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1749 | rdev2->bdev->bd_contains) { |
| 1750 | rcu_read_unlock(); |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1751 | return 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1752 | } |
| 1753 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1754 | return 0; |
| 1755 | } |
| 1756 | |
| 1757 | static LIST_HEAD(pending_raid_disks); |
| 1758 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1759 | /* |
| 1760 | * Try to register data integrity profile for an mddev |
| 1761 | * |
| 1762 | * This is called when an array is started and after a disk has been kicked |
| 1763 | * from the array. It only succeeds if all working and active component devices |
| 1764 | * are integrity capable with matching profiles. |
| 1765 | */ |
| 1766 | int md_integrity_register(mddev_t *mddev) |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1767 | { |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1768 | mdk_rdev_t *rdev, *reference = NULL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1769 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1770 | if (list_empty(&mddev->disks)) |
| 1771 | return 0; /* nothing to do */ |
| 1772 | if (blk_get_integrity(mddev->gendisk)) |
| 1773 | return 0; /* already registered */ |
| 1774 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 1775 | /* skip spares and non-functional disks */ |
| 1776 | if (test_bit(Faulty, &rdev->flags)) |
| 1777 | continue; |
| 1778 | if (rdev->raid_disk < 0) |
| 1779 | continue; |
| 1780 | /* |
| 1781 | * If at least one rdev is not integrity capable, we can not |
| 1782 | * enable data integrity for the md device. |
| 1783 | */ |
| 1784 | if (!bdev_get_integrity(rdev->bdev)) |
| 1785 | return -EINVAL; |
| 1786 | if (!reference) { |
| 1787 | /* Use the first rdev as the reference */ |
| 1788 | reference = rdev; |
| 1789 | continue; |
| 1790 | } |
| 1791 | /* does this rdev's profile match the reference profile? */ |
| 1792 | if (blk_integrity_compare(reference->bdev->bd_disk, |
| 1793 | rdev->bdev->bd_disk) < 0) |
| 1794 | return -EINVAL; |
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 | /* |
| 1797 | * All component devices are integrity capable and have matching |
| 1798 | * profiles, register the common profile for the md device. |
| 1799 | */ |
| 1800 | if (blk_integrity_register(mddev->gendisk, |
| 1801 | bdev_get_integrity(reference->bdev)) != 0) { |
| 1802 | printk(KERN_ERR "md: failed to register integrity for %s\n", |
| 1803 | mdname(mddev)); |
| 1804 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1805 | } |
Martin K. Petersen | a91a278 | 2011-03-17 11:11:05 +0100 | [diff] [blame^] | 1806 | printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev)); |
| 1807 | if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) { |
| 1808 | printk(KERN_ERR "md: failed to create integrity pool for %s\n", |
| 1809 | mdname(mddev)); |
| 1810 | return -EINVAL; |
| 1811 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1812 | return 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1813 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1814 | EXPORT_SYMBOL(md_integrity_register); |
| 1815 | |
| 1816 | /* Disable data integrity if non-capable/non-matching disk is being added */ |
| 1817 | void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 1818 | { |
| 1819 | struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev); |
| 1820 | struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk); |
| 1821 | |
| 1822 | if (!bi_mddev) /* nothing to do */ |
| 1823 | return; |
| 1824 | if (rdev->raid_disk < 0) /* skip spares */ |
| 1825 | return; |
| 1826 | if (bi_rdev && blk_integrity_compare(mddev->gendisk, |
| 1827 | rdev->bdev->bd_disk) >= 0) |
| 1828 | return; |
| 1829 | printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev)); |
| 1830 | blk_integrity_unregister(mddev->gendisk); |
| 1831 | } |
| 1832 | EXPORT_SYMBOL(md_integrity_add_rdev); |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1833 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1834 | static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev) |
| 1835 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1836 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 1837 | struct kobject *ko; |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1838 | char *s; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1839 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1840 | |
| 1841 | if (rdev->mddev) { |
| 1842 | MD_BUG(); |
| 1843 | return -EINVAL; |
| 1844 | } |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1845 | |
| 1846 | /* prevent duplicates */ |
| 1847 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 1848 | return -EEXIST; |
| 1849 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1850 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
| 1851 | if (rdev->sectors && (mddev->dev_sectors == 0 || |
| 1852 | rdev->sectors < mddev->dev_sectors)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1853 | if (mddev->pers) { |
| 1854 | /* Cannot change size, so fail |
| 1855 | * If mddev->level <= 0, then we don't care |
| 1856 | * about aligning sizes (e.g. linear) |
| 1857 | */ |
| 1858 | if (mddev->level > 0) |
| 1859 | return -ENOSPC; |
| 1860 | } else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1861 | mddev->dev_sectors = rdev->sectors; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1862 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1863 | |
| 1864 | /* Verify rdev->desc_nr is unique. |
| 1865 | * If it is -1, assign a free number, else |
| 1866 | * check number is not in use |
| 1867 | */ |
| 1868 | if (rdev->desc_nr < 0) { |
| 1869 | int choice = 0; |
| 1870 | if (mddev->pers) choice = mddev->raid_disks; |
| 1871 | while (find_rdev_nr(mddev, choice)) |
| 1872 | choice++; |
| 1873 | rdev->desc_nr = choice; |
| 1874 | } else { |
| 1875 | if (find_rdev_nr(mddev, rdev->desc_nr)) |
| 1876 | return -EBUSY; |
| 1877 | } |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 1878 | if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
| 1879 | printk(KERN_WARNING "md: %s: array is limited to %d devices\n", |
| 1880 | mdname(mddev), mddev->max_disks); |
| 1881 | return -EBUSY; |
| 1882 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1883 | bdevname(rdev->bdev,b); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1884 | while ( (s=strchr(b, '/')) != NULL) |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1885 | *s = '!'; |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1886 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1887 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1888 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1889 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1890 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1891 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1892 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1893 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 1894 | if (sysfs_create_link(&rdev->kobj, ko, "block")) |
| 1895 | /* failure here is OK */; |
| 1896 | rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 1897 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1898 | list_add_rcu(&rdev->same_set, &mddev->disks); |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1899 | bd_link_disk_holder(rdev->bdev, mddev->gendisk); |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 1900 | |
| 1901 | /* May as well allow recovery to be retried once */ |
| 1902 | mddev->recovery_disabled = 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1903 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1904 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1905 | |
| 1906 | fail: |
| 1907 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", |
| 1908 | b, mdname(mddev)); |
| 1909 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1910 | } |
| 1911 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1912 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1913 | { |
| 1914 | mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work); |
| 1915 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1916 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1917 | } |
| 1918 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1919 | static void unbind_rdev_from_array(mdk_rdev_t * rdev) |
| 1920 | { |
| 1921 | char b[BDEVNAME_SIZE]; |
| 1922 | if (!rdev->mddev) { |
| 1923 | MD_BUG(); |
| 1924 | return; |
| 1925 | } |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 1926 | bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1927 | list_del_rcu(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1928 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 1929 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1930 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 1931 | sysfs_put(rdev->sysfs_state); |
| 1932 | rdev->sysfs_state = NULL; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1933 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1934 | * writing to 'remove' to "dev/state". We also need |
| 1935 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1936 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1937 | synchronize_rcu(); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1938 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 1939 | kobject_get(&rdev->kobj); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 1940 | queue_work(md_misc_wq, &rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1941 | } |
| 1942 | |
| 1943 | /* |
| 1944 | * prevent the device from being mounted, repartitioned or |
| 1945 | * otherwise reused by a RAID array (or any other kernel |
| 1946 | * subsystem), by bd_claiming the device. |
| 1947 | */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1948 | 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] | 1949 | { |
| 1950 | int err = 0; |
| 1951 | struct block_device *bdev; |
| 1952 | char b[BDEVNAME_SIZE]; |
| 1953 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1954 | bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, |
| 1955 | shared ? (mdk_rdev_t *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1956 | if (IS_ERR(bdev)) { |
| 1957 | printk(KERN_ERR "md: could not open %s.\n", |
| 1958 | __bdevname(dev, b)); |
| 1959 | return PTR_ERR(bdev); |
| 1960 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1961 | rdev->bdev = bdev; |
| 1962 | return err; |
| 1963 | } |
| 1964 | |
| 1965 | static void unlock_rdev(mdk_rdev_t *rdev) |
| 1966 | { |
| 1967 | struct block_device *bdev = rdev->bdev; |
| 1968 | rdev->bdev = NULL; |
| 1969 | if (!bdev) |
| 1970 | MD_BUG(); |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1971 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1972 | } |
| 1973 | |
| 1974 | void md_autodetect_dev(dev_t dev); |
| 1975 | |
| 1976 | static void export_rdev(mdk_rdev_t * rdev) |
| 1977 | { |
| 1978 | char b[BDEVNAME_SIZE]; |
| 1979 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 1980 | bdevname(rdev->bdev,b)); |
| 1981 | if (rdev->mddev) |
| 1982 | MD_BUG(); |
| 1983 | free_disk_sb(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1984 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1985 | if (test_bit(AutoDetected, &rdev->flags)) |
| 1986 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1987 | #endif |
| 1988 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1989 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1990 | } |
| 1991 | |
| 1992 | static void kick_rdev_from_array(mdk_rdev_t * rdev) |
| 1993 | { |
| 1994 | unbind_rdev_from_array(rdev); |
| 1995 | export_rdev(rdev); |
| 1996 | } |
| 1997 | |
| 1998 | static void export_array(mddev_t *mddev) |
| 1999 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2000 | mdk_rdev_t *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2001 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2002 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2003 | if (!rdev->mddev) { |
| 2004 | MD_BUG(); |
| 2005 | continue; |
| 2006 | } |
| 2007 | kick_rdev_from_array(rdev); |
| 2008 | } |
| 2009 | if (!list_empty(&mddev->disks)) |
| 2010 | MD_BUG(); |
| 2011 | mddev->raid_disks = 0; |
| 2012 | mddev->major_version = 0; |
| 2013 | } |
| 2014 | |
| 2015 | static void print_desc(mdp_disk_t *desc) |
| 2016 | { |
| 2017 | printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number, |
| 2018 | desc->major,desc->minor,desc->raid_disk,desc->state); |
| 2019 | } |
| 2020 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2021 | static void print_sb_90(mdp_super_t *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2022 | { |
| 2023 | int i; |
| 2024 | |
| 2025 | printk(KERN_INFO |
| 2026 | "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n", |
| 2027 | sb->major_version, sb->minor_version, sb->patch_version, |
| 2028 | sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3, |
| 2029 | sb->ctime); |
| 2030 | printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n", |
| 2031 | sb->level, sb->size, sb->nr_disks, sb->raid_disks, |
| 2032 | sb->md_minor, sb->layout, sb->chunk_size); |
| 2033 | printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d" |
| 2034 | " FD:%d SD:%d CSUM:%08x E:%08lx\n", |
| 2035 | sb->utime, sb->state, sb->active_disks, sb->working_disks, |
| 2036 | sb->failed_disks, sb->spare_disks, |
| 2037 | sb->sb_csum, (unsigned long)sb->events_lo); |
| 2038 | |
| 2039 | printk(KERN_INFO); |
| 2040 | for (i = 0; i < MD_SB_DISKS; i++) { |
| 2041 | mdp_disk_t *desc; |
| 2042 | |
| 2043 | desc = sb->disks + i; |
| 2044 | if (desc->number || desc->major || desc->minor || |
| 2045 | desc->raid_disk || (desc->state && (desc->state != 4))) { |
| 2046 | printk(" D %2d: ", i); |
| 2047 | print_desc(desc); |
| 2048 | } |
| 2049 | } |
| 2050 | printk(KERN_INFO "md: THIS: "); |
| 2051 | print_desc(&sb->this_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2052 | } |
| 2053 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2054 | static void print_sb_1(struct mdp_superblock_1 *sb) |
| 2055 | { |
| 2056 | __u8 *uuid; |
| 2057 | |
| 2058 | uuid = sb->set_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2059 | printk(KERN_INFO |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2060 | "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2061 | "md: Name: \"%s\" CT:%llu\n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2062 | le32_to_cpu(sb->major_version), |
| 2063 | le32_to_cpu(sb->feature_map), |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2064 | uuid, |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2065 | sb->set_name, |
| 2066 | (unsigned long long)le64_to_cpu(sb->ctime) |
| 2067 | & MD_SUPERBLOCK_1_TIME_SEC_MASK); |
| 2068 | |
| 2069 | uuid = sb->device_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2070 | printk(KERN_INFO |
| 2071 | "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] | 2072 | " RO:%llu\n" |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2073 | "md: Dev:%08x UUID: %pU\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2074 | "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n" |
| 2075 | "md: (MaxDev:%u) \n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2076 | le32_to_cpu(sb->level), |
| 2077 | (unsigned long long)le64_to_cpu(sb->size), |
| 2078 | le32_to_cpu(sb->raid_disks), |
| 2079 | le32_to_cpu(sb->layout), |
| 2080 | le32_to_cpu(sb->chunksize), |
| 2081 | (unsigned long long)le64_to_cpu(sb->data_offset), |
| 2082 | (unsigned long long)le64_to_cpu(sb->data_size), |
| 2083 | (unsigned long long)le64_to_cpu(sb->super_offset), |
| 2084 | (unsigned long long)le64_to_cpu(sb->recovery_offset), |
| 2085 | le32_to_cpu(sb->dev_number), |
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->devflags, |
| 2088 | (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK, |
| 2089 | (unsigned long long)le64_to_cpu(sb->events), |
| 2090 | (unsigned long long)le64_to_cpu(sb->resync_offset), |
| 2091 | le32_to_cpu(sb->sb_csum), |
| 2092 | le32_to_cpu(sb->max_dev) |
| 2093 | ); |
| 2094 | } |
| 2095 | |
| 2096 | static void print_rdev(mdk_rdev_t *rdev, int major_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2097 | { |
| 2098 | char b[BDEVNAME_SIZE]; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2099 | printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n", |
| 2100 | bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors, |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2101 | test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags), |
| 2102 | rdev->desc_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2103 | if (rdev->sb_loaded) { |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2104 | printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version); |
| 2105 | switch (major_version) { |
| 2106 | case 0: |
| 2107 | print_sb_90((mdp_super_t*)page_address(rdev->sb_page)); |
| 2108 | break; |
| 2109 | case 1: |
| 2110 | print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page)); |
| 2111 | break; |
| 2112 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2113 | } else |
| 2114 | printk(KERN_INFO "md: no rdev superblock!\n"); |
| 2115 | } |
| 2116 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 2117 | static void md_print_devices(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2118 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2119 | struct list_head *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2120 | mdk_rdev_t *rdev; |
| 2121 | mddev_t *mddev; |
| 2122 | char b[BDEVNAME_SIZE]; |
| 2123 | |
| 2124 | printk("\n"); |
| 2125 | printk("md: **********************************\n"); |
| 2126 | printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n"); |
| 2127 | printk("md: **********************************\n"); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2128 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2129 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 2130 | if (mddev->bitmap) |
| 2131 | bitmap_print_sb(mddev->bitmap); |
| 2132 | else |
| 2133 | printk("%s: ", mdname(mddev)); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2134 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2135 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 2136 | printk("\n"); |
| 2137 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2138 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2139 | print_rdev(rdev, mddev->major_version); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2140 | } |
| 2141 | printk("md: **********************************\n"); |
| 2142 | printk("\n"); |
| 2143 | } |
| 2144 | |
| 2145 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2146 | static void sync_sbs(mddev_t * mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2147 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2148 | /* Update each superblock (in-memory image), but |
| 2149 | * if we are allowed to, skip spares which already |
| 2150 | * have the right event counter, or have one earlier |
| 2151 | * (which would mean they aren't being marked as dirty |
| 2152 | * with the rest of the array) |
| 2153 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2154 | mdk_rdev_t *rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2155 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2156 | if (rdev->sb_events == mddev->events || |
| 2157 | (nospares && |
| 2158 | rdev->raid_disk < 0 && |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2159 | rdev->sb_events+1 == mddev->events)) { |
| 2160 | /* Don't update this superblock */ |
| 2161 | rdev->sb_loaded = 2; |
| 2162 | } else { |
| 2163 | super_types[mddev->major_version]. |
| 2164 | sync_super(mddev, rdev); |
| 2165 | rdev->sb_loaded = 1; |
| 2166 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2167 | } |
| 2168 | } |
| 2169 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2170 | static void md_update_sb(mddev_t * mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2171 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2172 | mdk_rdev_t *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2173 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2174 | int nospares = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2175 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2176 | repeat: |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2177 | /* First make sure individual recovery_offsets are correct */ |
| 2178 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 2179 | if (rdev->raid_disk >= 0 && |
| 2180 | mddev->delta_disks >= 0 && |
| 2181 | !test_bit(In_sync, &rdev->flags) && |
| 2182 | mddev->curr_resync_completed > rdev->recovery_offset) |
| 2183 | rdev->recovery_offset = mddev->curr_resync_completed; |
| 2184 | |
| 2185 | } |
Dan Williams | bd52b74 | 2010-08-30 17:33:33 +1000 | [diff] [blame] | 2186 | if (!mddev->persistent) { |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 2187 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2188 | clear_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | d97a41d | 2010-10-28 17:30:20 +1100 | [diff] [blame] | 2189 | if (!mddev->external) |
| 2190 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2191 | wake_up(&mddev->sb_wait); |
| 2192 | return; |
| 2193 | } |
| 2194 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2195 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2196 | |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2197 | mddev->utime = get_seconds(); |
| 2198 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2199 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 2200 | force_change = 1; |
| 2201 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 2202 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 2203 | * though if events isn't the right even/odd, we will have to do |
| 2204 | * spares after all |
| 2205 | */ |
| 2206 | nospares = 1; |
| 2207 | if (force_change) |
| 2208 | nospares = 0; |
| 2209 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2210 | /* If the array is degraded, then skipping spares is both |
| 2211 | * dangerous and fairly pointless. |
| 2212 | * Dangerous because a device that was removed from the array |
| 2213 | * might have a event_count that still looks up-to-date, |
| 2214 | * so it can be re-added without a resync. |
| 2215 | * Pointless because if there are any spares to skip, |
| 2216 | * then a recovery will happen and soon that array won't |
| 2217 | * be degraded any more and the spare can go back to sleep then. |
| 2218 | */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2219 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2220 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2221 | sync_req = mddev->in_sync; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2222 | |
| 2223 | /* If this is just a dirty<->clean transition, and the array is clean |
| 2224 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2225 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2226 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2227 | && mddev->can_decrease_events |
| 2228 | && mddev->events != 1) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2229 | mddev->events--; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2230 | mddev->can_decrease_events = 0; |
| 2231 | } else { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2232 | /* otherwise we have to go forward and ... */ |
| 2233 | mddev->events ++; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2234 | mddev->can_decrease_events = nospares; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2235 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2236 | |
| 2237 | if (!mddev->events) { |
| 2238 | /* |
| 2239 | * oops, this 64-bit counter should never wrap. |
| 2240 | * Either we are in around ~1 trillion A.C., assuming |
| 2241 | * 1 reboot per second, or we have a bug: |
| 2242 | */ |
| 2243 | MD_BUG(); |
| 2244 | mddev->events --; |
| 2245 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2246 | sync_sbs(mddev, nospares); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2247 | spin_unlock_irq(&mddev->write_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2248 | |
| 2249 | dprintk(KERN_INFO |
| 2250 | "md: updating %s RAID superblock on device (in sync %d)\n", |
| 2251 | mdname(mddev),mddev->in_sync); |
| 2252 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 2253 | bitmap_update_sb(mddev->bitmap); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2254 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2255 | char b[BDEVNAME_SIZE]; |
| 2256 | dprintk(KERN_INFO "md: "); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2257 | if (rdev->sb_loaded != 1) |
| 2258 | continue; /* no noise on spare devices */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2259 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2260 | dprintk("(skipping faulty "); |
| 2261 | |
| 2262 | dprintk("%s ", bdevname(rdev->bdev,b)); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2263 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2264 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2265 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2266 | rdev->sb_page); |
| 2267 | dprintk(KERN_INFO "(write) %s's sb offset: %llu\n", |
| 2268 | bdevname(rdev->bdev,b), |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2269 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2270 | rdev->sb_events = mddev->events; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2271 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2272 | } else |
| 2273 | dprintk(")\n"); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2274 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2275 | /* only need to write one superblock... */ |
| 2276 | break; |
| 2277 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2278 | md_super_wait(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2279 | /* 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] | 2280 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2281 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2282 | if (mddev->in_sync != sync_req || |
| 2283 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2284 | /* have to write it out again */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2285 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2286 | goto repeat; |
| 2287 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2288 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2289 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2290 | wake_up(&mddev->sb_wait); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 2291 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 2292 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2293 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2294 | } |
| 2295 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 2296 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2297 | * We want to accept with case. For this we use cmd_match. |
| 2298 | */ |
| 2299 | static int cmd_match(const char *cmd, const char *str) |
| 2300 | { |
| 2301 | /* See if cmd, written into a sysfs file, matches |
| 2302 | * str. They must either be the same, or cmd can |
| 2303 | * have a trailing newline |
| 2304 | */ |
| 2305 | while (*cmd && *str && *cmd == *str) { |
| 2306 | cmd++; |
| 2307 | str++; |
| 2308 | } |
| 2309 | if (*cmd == '\n') |
| 2310 | cmd++; |
| 2311 | if (*str || *cmd) |
| 2312 | return 0; |
| 2313 | return 1; |
| 2314 | } |
| 2315 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2316 | struct rdev_sysfs_entry { |
| 2317 | struct attribute attr; |
| 2318 | ssize_t (*show)(mdk_rdev_t *, char *); |
| 2319 | ssize_t (*store)(mdk_rdev_t *, const char *, size_t); |
| 2320 | }; |
| 2321 | |
| 2322 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2323 | state_show(mdk_rdev_t *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2324 | { |
| 2325 | char *sep = ""; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2326 | size_t len = 0; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2327 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2328 | if (test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2329 | len+= sprintf(page+len, "%sfaulty",sep); |
| 2330 | sep = ","; |
| 2331 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2332 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2333 | len += sprintf(page+len, "%sin_sync",sep); |
| 2334 | sep = ","; |
| 2335 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2336 | if (test_bit(WriteMostly, &rdev->flags)) { |
| 2337 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 2338 | sep = ","; |
| 2339 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2340 | if (test_bit(Blocked, &rdev->flags)) { |
| 2341 | len += sprintf(page+len, "%sblocked", sep); |
| 2342 | sep = ","; |
| 2343 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2344 | if (!test_bit(Faulty, &rdev->flags) && |
| 2345 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2346 | len += sprintf(page+len, "%sspare", sep); |
| 2347 | sep = ","; |
| 2348 | } |
| 2349 | return len+sprintf(page+len, "\n"); |
| 2350 | } |
| 2351 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2352 | static ssize_t |
| 2353 | state_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2354 | { |
| 2355 | /* can write |
| 2356 | * faulty - simulates and error |
| 2357 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2358 | * writemostly - sets write_mostly |
| 2359 | * -writemostly - clears write_mostly |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2360 | * blocked - sets the Blocked flag |
| 2361 | * -blocked - clears the Blocked flag |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2362 | * insync - sets Insync providing device isn't active |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2363 | */ |
| 2364 | int err = -EINVAL; |
| 2365 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 2366 | md_error(rdev->mddev, rdev); |
| 2367 | err = 0; |
| 2368 | } else if (cmd_match(buf, "remove")) { |
| 2369 | if (rdev->raid_disk >= 0) |
| 2370 | err = -EBUSY; |
| 2371 | else { |
| 2372 | mddev_t *mddev = rdev->mddev; |
| 2373 | kick_rdev_from_array(rdev); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2374 | if (mddev->pers) |
| 2375 | md_update_sb(mddev, 1); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2376 | md_new_event(mddev); |
| 2377 | err = 0; |
| 2378 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2379 | } else if (cmd_match(buf, "writemostly")) { |
| 2380 | set_bit(WriteMostly, &rdev->flags); |
| 2381 | err = 0; |
| 2382 | } else if (cmd_match(buf, "-writemostly")) { |
| 2383 | clear_bit(WriteMostly, &rdev->flags); |
| 2384 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2385 | } else if (cmd_match(buf, "blocked")) { |
| 2386 | set_bit(Blocked, &rdev->flags); |
| 2387 | err = 0; |
| 2388 | } else if (cmd_match(buf, "-blocked")) { |
| 2389 | clear_bit(Blocked, &rdev->flags); |
| 2390 | wake_up(&rdev->blocked_wait); |
| 2391 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2392 | md_wakeup_thread(rdev->mddev->thread); |
| 2393 | |
| 2394 | err = 0; |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2395 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { |
| 2396 | set_bit(In_sync, &rdev->flags); |
| 2397 | err = 0; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2398 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2399 | if (!err) |
| 2400 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2401 | return err ? err : len; |
| 2402 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2403 | static struct rdev_sysfs_entry rdev_state = |
| 2404 | __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2405 | |
| 2406 | static ssize_t |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2407 | errors_show(mdk_rdev_t *rdev, char *page) |
| 2408 | { |
| 2409 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 2410 | } |
| 2411 | |
| 2412 | static ssize_t |
| 2413 | errors_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2414 | { |
| 2415 | char *e; |
| 2416 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2417 | if (*buf && (*e == 0 || *e == '\n')) { |
| 2418 | atomic_set(&rdev->corrected_errors, n); |
| 2419 | return len; |
| 2420 | } |
| 2421 | return -EINVAL; |
| 2422 | } |
| 2423 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2424 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2425 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2426 | static ssize_t |
| 2427 | slot_show(mdk_rdev_t *rdev, char *page) |
| 2428 | { |
| 2429 | if (rdev->raid_disk < 0) |
| 2430 | return sprintf(page, "none\n"); |
| 2431 | else |
| 2432 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 2433 | } |
| 2434 | |
| 2435 | static ssize_t |
| 2436 | slot_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2437 | { |
| 2438 | char *e; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2439 | int err; |
| 2440 | char nm[20]; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2441 | int slot = simple_strtoul(buf, &e, 10); |
| 2442 | if (strncmp(buf, "none", 4)==0) |
| 2443 | slot = -1; |
| 2444 | else if (e==buf || (*e && *e!= '\n')) |
| 2445 | return -EINVAL; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2446 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2447 | /* Setting 'slot' on an active array requires also |
| 2448 | * updating the 'rd%d' link, and communicating |
| 2449 | * with the personality with ->hot_*_disk. |
| 2450 | * For now we only support removing |
| 2451 | * failed/spare devices. This normally happens automatically, |
| 2452 | * but not when the metadata is externally managed. |
| 2453 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2454 | if (rdev->raid_disk == -1) |
| 2455 | return -EEXIST; |
| 2456 | /* personality does all needed checks */ |
| 2457 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2458 | return -EINVAL; |
| 2459 | err = rdev->mddev->pers-> |
| 2460 | hot_remove_disk(rdev->mddev, rdev->raid_disk); |
| 2461 | if (err) |
| 2462 | return err; |
| 2463 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2464 | sysfs_remove_link(&rdev->mddev->kobj, nm); |
Maciej Trela | b710310 | 2010-04-14 16:58:16 +1000 | [diff] [blame] | 2465 | rdev->raid_disk = -1; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2466 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2467 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2468 | } else if (rdev->mddev->pers) { |
| 2469 | mdk_rdev_t *rdev2; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2470 | /* Activating a spare .. or possibly reactivating |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2471 | * if we ever get bitmaps working here. |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2472 | */ |
| 2473 | |
| 2474 | if (rdev->raid_disk != -1) |
| 2475 | return -EBUSY; |
| 2476 | |
NeilBrown | c6751b2 | 2011-02-02 11:57:13 +1100 | [diff] [blame] | 2477 | if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery)) |
| 2478 | return -EBUSY; |
| 2479 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2480 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2481 | return -EINVAL; |
| 2482 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2483 | list_for_each_entry(rdev2, &rdev->mddev->disks, same_set) |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2484 | if (rdev2->raid_disk == slot) |
| 2485 | return -EEXIST; |
| 2486 | |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2487 | if (slot >= rdev->mddev->raid_disks && |
| 2488 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
| 2489 | return -ENOSPC; |
| 2490 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2491 | rdev->raid_disk = slot; |
| 2492 | if (test_bit(In_sync, &rdev->flags)) |
| 2493 | rdev->saved_raid_disk = slot; |
| 2494 | else |
| 2495 | rdev->saved_raid_disk = -1; |
| 2496 | err = rdev->mddev->pers-> |
| 2497 | hot_add_disk(rdev->mddev, rdev); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 2498 | if (err) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2499 | rdev->raid_disk = -1; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2500 | return err; |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 2501 | } else |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2502 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2503 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2504 | if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2505 | /* failure here is OK */; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2506 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2507 | } else { |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2508 | if (slot >= rdev->mddev->raid_disks && |
| 2509 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2510 | return -ENOSPC; |
| 2511 | rdev->raid_disk = slot; |
| 2512 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2513 | clear_bit(Faulty, &rdev->flags); |
| 2514 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2515 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2516 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2517 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2518 | return len; |
| 2519 | } |
| 2520 | |
| 2521 | |
| 2522 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2523 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2524 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2525 | static ssize_t |
| 2526 | offset_show(mdk_rdev_t *rdev, char *page) |
| 2527 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2528 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2529 | } |
| 2530 | |
| 2531 | static ssize_t |
| 2532 | offset_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2533 | { |
| 2534 | char *e; |
| 2535 | unsigned long long offset = simple_strtoull(buf, &e, 10); |
| 2536 | if (e==buf || (*e && *e != '\n')) |
| 2537 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2538 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2539 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2540 | if (rdev->sectors && rdev->mddev->external) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2541 | /* Must set offset before size, so overlap checks |
| 2542 | * can be sane */ |
| 2543 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2544 | rdev->data_offset = offset; |
| 2545 | return len; |
| 2546 | } |
| 2547 | |
| 2548 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2549 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2550 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2551 | static ssize_t |
| 2552 | rdev_size_show(mdk_rdev_t *rdev, char *page) |
| 2553 | { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2554 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2555 | } |
| 2556 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2557 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 2558 | { |
| 2559 | /* check if two start/length pairs overlap */ |
| 2560 | if (s1+l1 <= s2) |
| 2561 | return 0; |
| 2562 | if (s2+l2 <= s1) |
| 2563 | return 0; |
| 2564 | return 1; |
| 2565 | } |
| 2566 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2567 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
| 2568 | { |
| 2569 | unsigned long long blocks; |
| 2570 | sector_t new; |
| 2571 | |
| 2572 | if (strict_strtoull(buf, 10, &blocks) < 0) |
| 2573 | return -EINVAL; |
| 2574 | |
| 2575 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) |
| 2576 | return -EINVAL; /* sector conversion overflow */ |
| 2577 | |
| 2578 | new = blocks * 2; |
| 2579 | if (new != blocks * 2) |
| 2580 | return -EINVAL; /* unsigned long long to sector_t overflow */ |
| 2581 | |
| 2582 | *sectors = new; |
| 2583 | return 0; |
| 2584 | } |
| 2585 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2586 | static ssize_t |
| 2587 | rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2588 | { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2589 | mddev_t *my_mddev = rdev->mddev; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2590 | sector_t oldsectors = rdev->sectors; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2591 | sector_t sectors; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2592 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2593 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2594 | return -EINVAL; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2595 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2596 | if (my_mddev->persistent) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2597 | sectors = super_types[my_mddev->major_version]. |
| 2598 | rdev_size_change(rdev, sectors); |
| 2599 | if (!sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2600 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2601 | } else if (!sectors) |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 2602 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) - |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2603 | rdev->data_offset; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2604 | } |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2605 | if (sectors < my_mddev->dev_sectors) |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 2606 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2607 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2608 | rdev->sectors = sectors; |
| 2609 | if (sectors > oldsectors && my_mddev->external) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2610 | /* need to check that all other rdevs with the same ->bdev |
| 2611 | * do not overlap. We need to unlock the mddev to avoid |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2612 | * a deadlock. We have already changed rdev->sectors, and if |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2613 | * we have to change it back, we will have the lock again. |
| 2614 | */ |
| 2615 | mddev_t *mddev; |
| 2616 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2617 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2618 | |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2619 | mddev_unlock(my_mddev); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2620 | for_each_mddev(mddev, tmp) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2621 | mdk_rdev_t *rdev2; |
| 2622 | |
| 2623 | mddev_lock(mddev); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2624 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
NeilBrown | f21e9ff | 2011-01-31 12:10:09 +1100 | [diff] [blame] | 2625 | if (rdev->bdev == rdev2->bdev && |
| 2626 | rdev != rdev2 && |
| 2627 | overlaps(rdev->data_offset, rdev->sectors, |
| 2628 | rdev2->data_offset, |
| 2629 | rdev2->sectors)) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2630 | overlap = 1; |
| 2631 | break; |
| 2632 | } |
| 2633 | mddev_unlock(mddev); |
| 2634 | if (overlap) { |
| 2635 | mddev_put(mddev); |
| 2636 | break; |
| 2637 | } |
| 2638 | } |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2639 | mddev_lock(my_mddev); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2640 | if (overlap) { |
| 2641 | /* Someone else could have slipped in a size |
| 2642 | * change here, but doing so is just silly. |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2643 | * We put oldsectors back because we *know* it is |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2644 | * safe, and trust userspace not to race with |
| 2645 | * itself |
| 2646 | */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2647 | rdev->sectors = oldsectors; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2648 | return -EBUSY; |
| 2649 | } |
| 2650 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2651 | return len; |
| 2652 | } |
| 2653 | |
| 2654 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2655 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2656 | |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2657 | |
| 2658 | static ssize_t recovery_start_show(mdk_rdev_t *rdev, char *page) |
| 2659 | { |
| 2660 | unsigned long long recovery_start = rdev->recovery_offset; |
| 2661 | |
| 2662 | if (test_bit(In_sync, &rdev->flags) || |
| 2663 | recovery_start == MaxSector) |
| 2664 | return sprintf(page, "none\n"); |
| 2665 | |
| 2666 | return sprintf(page, "%llu\n", recovery_start); |
| 2667 | } |
| 2668 | |
| 2669 | static ssize_t recovery_start_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2670 | { |
| 2671 | unsigned long long recovery_start; |
| 2672 | |
| 2673 | if (cmd_match(buf, "none")) |
| 2674 | recovery_start = MaxSector; |
| 2675 | else if (strict_strtoull(buf, 10, &recovery_start)) |
| 2676 | return -EINVAL; |
| 2677 | |
| 2678 | if (rdev->mddev->pers && |
| 2679 | rdev->raid_disk >= 0) |
| 2680 | return -EBUSY; |
| 2681 | |
| 2682 | rdev->recovery_offset = recovery_start; |
| 2683 | if (recovery_start == MaxSector) |
| 2684 | set_bit(In_sync, &rdev->flags); |
| 2685 | else |
| 2686 | clear_bit(In_sync, &rdev->flags); |
| 2687 | return len; |
| 2688 | } |
| 2689 | |
| 2690 | static struct rdev_sysfs_entry rdev_recovery_start = |
| 2691 | __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store); |
| 2692 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2693 | static struct attribute *rdev_default_attrs[] = { |
| 2694 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2695 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2696 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2697 | &rdev_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2698 | &rdev_size.attr, |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2699 | &rdev_recovery_start.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2700 | NULL, |
| 2701 | }; |
| 2702 | static ssize_t |
| 2703 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 2704 | { |
| 2705 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2706 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2707 | mddev_t *mddev = rdev->mddev; |
| 2708 | ssize_t rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2709 | |
| 2710 | if (!entry->show) |
| 2711 | return -EIO; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2712 | |
| 2713 | rv = mddev ? mddev_lock(mddev) : -EBUSY; |
| 2714 | if (!rv) { |
| 2715 | if (rdev->mddev == NULL) |
| 2716 | rv = -EBUSY; |
| 2717 | else |
| 2718 | rv = entry->show(rdev, page); |
| 2719 | mddev_unlock(mddev); |
| 2720 | } |
| 2721 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2722 | } |
| 2723 | |
| 2724 | static ssize_t |
| 2725 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 2726 | const char *page, size_t length) |
| 2727 | { |
| 2728 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2729 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2730 | ssize_t rv; |
| 2731 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2732 | |
| 2733 | if (!entry->store) |
| 2734 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 2735 | if (!capable(CAP_SYS_ADMIN)) |
| 2736 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2737 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2738 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2739 | if (rdev->mddev == NULL) |
| 2740 | rv = -EBUSY; |
| 2741 | else |
| 2742 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 2743 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2744 | } |
| 2745 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2746 | } |
| 2747 | |
| 2748 | static void rdev_free(struct kobject *ko) |
| 2749 | { |
| 2750 | mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj); |
| 2751 | kfree(rdev); |
| 2752 | } |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 2753 | static const struct sysfs_ops rdev_sysfs_ops = { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2754 | .show = rdev_attr_show, |
| 2755 | .store = rdev_attr_store, |
| 2756 | }; |
| 2757 | static struct kobj_type rdev_ktype = { |
| 2758 | .release = rdev_free, |
| 2759 | .sysfs_ops = &rdev_sysfs_ops, |
| 2760 | .default_attrs = rdev_default_attrs, |
| 2761 | }; |
| 2762 | |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 2763 | void md_rdev_init(mdk_rdev_t *rdev) |
| 2764 | { |
| 2765 | rdev->desc_nr = -1; |
| 2766 | rdev->saved_raid_disk = -1; |
| 2767 | rdev->raid_disk = -1; |
| 2768 | rdev->flags = 0; |
| 2769 | rdev->data_offset = 0; |
| 2770 | rdev->sb_events = 0; |
| 2771 | rdev->last_read_error.tv_sec = 0; |
| 2772 | rdev->last_read_error.tv_nsec = 0; |
| 2773 | atomic_set(&rdev->nr_pending, 0); |
| 2774 | atomic_set(&rdev->read_errors, 0); |
| 2775 | atomic_set(&rdev->corrected_errors, 0); |
| 2776 | |
| 2777 | INIT_LIST_HEAD(&rdev->same_set); |
| 2778 | init_waitqueue_head(&rdev->blocked_wait); |
| 2779 | } |
| 2780 | EXPORT_SYMBOL_GPL(md_rdev_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2781 | /* |
| 2782 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 2783 | * |
| 2784 | * mark the device faulty if: |
| 2785 | * |
| 2786 | * - the device is nonexistent (zero size) |
| 2787 | * - the device has no valid superblock |
| 2788 | * |
| 2789 | * a faulty rdev _never_ has rdev->sb set. |
| 2790 | */ |
| 2791 | static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor) |
| 2792 | { |
| 2793 | char b[BDEVNAME_SIZE]; |
| 2794 | int err; |
| 2795 | mdk_rdev_t *rdev; |
| 2796 | sector_t size; |
| 2797 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 2798 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2799 | if (!rdev) { |
| 2800 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 2801 | return ERR_PTR(-ENOMEM); |
| 2802 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2803 | |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 2804 | md_rdev_init(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2805 | if ((err = alloc_disk_sb(rdev))) |
| 2806 | goto abort_free; |
| 2807 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2808 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2809 | if (err) |
| 2810 | goto abort_free; |
| 2811 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2812 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2813 | |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 2814 | size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2815 | if (!size) { |
| 2816 | printk(KERN_WARNING |
| 2817 | "md: %s has zero or unknown size, marking faulty!\n", |
| 2818 | bdevname(rdev->bdev,b)); |
| 2819 | err = -EINVAL; |
| 2820 | goto abort_free; |
| 2821 | } |
| 2822 | |
| 2823 | if (super_format >= 0) { |
| 2824 | err = super_types[super_format]. |
| 2825 | load_super(rdev, NULL, super_minor); |
| 2826 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 2827 | printk(KERN_WARNING |
| 2828 | "md: %s does not have a valid v%d.%d " |
| 2829 | "superblock, not importing!\n", |
| 2830 | bdevname(rdev->bdev,b), |
| 2831 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2832 | goto abort_free; |
| 2833 | } |
| 2834 | if (err < 0) { |
| 2835 | printk(KERN_WARNING |
| 2836 | "md: could not read %s's sb, not importing!\n", |
| 2837 | bdevname(rdev->bdev,b)); |
| 2838 | goto abort_free; |
| 2839 | } |
| 2840 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2841 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2842 | return rdev; |
| 2843 | |
| 2844 | abort_free: |
| 2845 | if (rdev->sb_page) { |
| 2846 | if (rdev->bdev) |
| 2847 | unlock_rdev(rdev); |
| 2848 | free_disk_sb(rdev); |
| 2849 | } |
| 2850 | kfree(rdev); |
| 2851 | return ERR_PTR(err); |
| 2852 | } |
| 2853 | |
| 2854 | /* |
| 2855 | * Check a full RAID array for plausibility |
| 2856 | */ |
| 2857 | |
| 2858 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 2859 | static void analyze_sbs(mddev_t * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2860 | { |
| 2861 | int i; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2862 | mdk_rdev_t *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2863 | char b[BDEVNAME_SIZE]; |
| 2864 | |
| 2865 | freshest = NULL; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2866 | rdev_for_each(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2867 | switch (super_types[mddev->major_version]. |
| 2868 | load_super(rdev, freshest, mddev->minor_version)) { |
| 2869 | case 1: |
| 2870 | freshest = rdev; |
| 2871 | break; |
| 2872 | case 0: |
| 2873 | break; |
| 2874 | default: |
| 2875 | printk( KERN_ERR \ |
| 2876 | "md: fatal superblock inconsistency in %s" |
| 2877 | " -- removing from array\n", |
| 2878 | bdevname(rdev->bdev,b)); |
| 2879 | kick_rdev_from_array(rdev); |
| 2880 | } |
| 2881 | |
| 2882 | |
| 2883 | super_types[mddev->major_version]. |
| 2884 | validate_super(mddev, freshest); |
| 2885 | |
| 2886 | i = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2887 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 2888 | if (mddev->max_disks && |
| 2889 | (rdev->desc_nr >= mddev->max_disks || |
| 2890 | i > mddev->max_disks)) { |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 2891 | printk(KERN_WARNING |
| 2892 | "md: %s: %s: only %d devices permitted\n", |
| 2893 | mdname(mddev), bdevname(rdev->bdev, b), |
| 2894 | mddev->max_disks); |
| 2895 | kick_rdev_from_array(rdev); |
| 2896 | continue; |
| 2897 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2898 | if (rdev != freshest) |
| 2899 | if (super_types[mddev->major_version]. |
| 2900 | validate_super(mddev, rdev)) { |
| 2901 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 2902 | " from array!\n", |
| 2903 | bdevname(rdev->bdev,b)); |
| 2904 | kick_rdev_from_array(rdev); |
| 2905 | continue; |
| 2906 | } |
| 2907 | if (mddev->level == LEVEL_MULTIPATH) { |
| 2908 | rdev->desc_nr = i++; |
| 2909 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2910 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 2911 | } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2912 | rdev->raid_disk = -1; |
| 2913 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2914 | } |
| 2915 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2916 | } |
| 2917 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2918 | /* Read a fixed-point number. |
| 2919 | * Numbers in sysfs attributes should be in "standard" units where |
| 2920 | * possible, so time should be in seconds. |
| 2921 | * However we internally use a a much smaller unit such as |
| 2922 | * milliseconds or jiffies. |
| 2923 | * This function takes a decimal number with a possible fractional |
| 2924 | * component, and produces an integer which is the result of |
| 2925 | * multiplying that number by 10^'scale'. |
| 2926 | * all without any floating-point arithmetic. |
| 2927 | */ |
| 2928 | int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale) |
| 2929 | { |
| 2930 | unsigned long result = 0; |
| 2931 | long decimals = -1; |
| 2932 | while (isdigit(*cp) || (*cp == '.' && decimals < 0)) { |
| 2933 | if (*cp == '.') |
| 2934 | decimals = 0; |
| 2935 | else if (decimals < scale) { |
| 2936 | unsigned int value; |
| 2937 | value = *cp - '0'; |
| 2938 | result = result * 10 + value; |
| 2939 | if (decimals >= 0) |
| 2940 | decimals++; |
| 2941 | } |
| 2942 | cp++; |
| 2943 | } |
| 2944 | if (*cp == '\n') |
| 2945 | cp++; |
| 2946 | if (*cp) |
| 2947 | return -EINVAL; |
| 2948 | if (decimals < 0) |
| 2949 | decimals = 0; |
| 2950 | while (decimals < scale) { |
| 2951 | result *= 10; |
| 2952 | decimals ++; |
| 2953 | } |
| 2954 | *res = result; |
| 2955 | return 0; |
| 2956 | } |
| 2957 | |
| 2958 | |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2959 | static void md_safemode_timeout(unsigned long data); |
| 2960 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2961 | static ssize_t |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2962 | safe_delay_show(mddev_t *mddev, char *page) |
| 2963 | { |
| 2964 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 2965 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 2966 | } |
| 2967 | static ssize_t |
| 2968 | safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len) |
| 2969 | { |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2970 | unsigned long msec; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 2971 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2972 | if (strict_strtoul_scaled(cbuf, &msec, 3) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2973 | return -EINVAL; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2974 | if (msec == 0) |
| 2975 | mddev->safemode_delay = 0; |
| 2976 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2977 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2978 | mddev->safemode_delay = (msec*HZ)/1000; |
| 2979 | if (mddev->safemode_delay == 0) |
| 2980 | mddev->safemode_delay = 1; |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2981 | if (mddev->safemode_delay < old_delay) |
| 2982 | md_safemode_timeout((unsigned long)mddev); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2983 | } |
| 2984 | return len; |
| 2985 | } |
| 2986 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2987 | __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] | 2988 | |
| 2989 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2990 | level_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2991 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 2992 | struct mdk_personality *p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2993 | if (p) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2994 | return sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2995 | else if (mddev->clevel[0]) |
| 2996 | return sprintf(page, "%s\n", mddev->clevel); |
| 2997 | else if (mddev->level != LEVEL_NONE) |
| 2998 | return sprintf(page, "%d\n", mddev->level); |
| 2999 | else |
| 3000 | return 0; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3001 | } |
| 3002 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3003 | static ssize_t |
| 3004 | level_store(mddev_t *mddev, const char *buf, size_t len) |
| 3005 | { |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3006 | char clevel[16]; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 3007 | ssize_t rv = len; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3008 | struct mdk_personality *pers; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3009 | long level; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3010 | void *priv; |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3011 | mdk_rdev_t *rdev; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3012 | |
| 3013 | if (mddev->pers == NULL) { |
| 3014 | if (len == 0) |
| 3015 | return 0; |
| 3016 | if (len >= sizeof(mddev->clevel)) |
| 3017 | return -ENOSPC; |
| 3018 | strncpy(mddev->clevel, buf, len); |
| 3019 | if (mddev->clevel[len-1] == '\n') |
| 3020 | len--; |
| 3021 | mddev->clevel[len] = 0; |
| 3022 | mddev->level = LEVEL_NONE; |
| 3023 | return rv; |
| 3024 | } |
| 3025 | |
| 3026 | /* request to change the personality. Need to ensure: |
| 3027 | * - array is not engaged in resync/recovery/reshape |
| 3028 | * - old personality can be suspended |
| 3029 | * - new personality will access other array. |
| 3030 | */ |
| 3031 | |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 3032 | if (mddev->sync_thread || |
| 3033 | mddev->reshape_position != MaxSector || |
| 3034 | mddev->sysfs_active) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3035 | return -EBUSY; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3036 | |
| 3037 | if (!mddev->pers->quiesce) { |
| 3038 | printk(KERN_WARNING "md: %s: %s does not support online personality change\n", |
| 3039 | mdname(mddev), mddev->pers->name); |
| 3040 | return -EINVAL; |
| 3041 | } |
| 3042 | |
| 3043 | /* Now find the new personality */ |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3044 | if (len == 0 || len >= sizeof(clevel)) |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3045 | return -EINVAL; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3046 | strncpy(clevel, buf, len); |
| 3047 | if (clevel[len-1] == '\n') |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3048 | len--; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3049 | clevel[len] = 0; |
| 3050 | if (strict_strtol(clevel, 10, &level)) |
| 3051 | level = LEVEL_NONE; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3052 | |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3053 | if (request_module("md-%s", clevel) != 0) |
| 3054 | request_module("md-level-%s", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3055 | spin_lock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3056 | pers = find_pers(level, clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3057 | if (!pers || !try_module_get(pers->owner)) { |
| 3058 | spin_unlock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3059 | printk(KERN_WARNING "md: personality %s not loaded\n", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3060 | return -EINVAL; |
| 3061 | } |
| 3062 | spin_unlock(&pers_lock); |
| 3063 | |
| 3064 | if (pers == mddev->pers) { |
| 3065 | /* Nothing to do! */ |
| 3066 | module_put(pers->owner); |
| 3067 | return rv; |
| 3068 | } |
| 3069 | if (!pers->takeover) { |
| 3070 | module_put(pers->owner); |
| 3071 | printk(KERN_WARNING "md: %s: %s does not support personality takeover\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3072 | mdname(mddev), clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3073 | return -EINVAL; |
| 3074 | } |
| 3075 | |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3076 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 3077 | rdev->new_raid_disk = rdev->raid_disk; |
| 3078 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3079 | /* ->takeover must set new_* and/or delta_disks |
| 3080 | * if it succeeds, and may set them when it fails. |
| 3081 | */ |
| 3082 | priv = pers->takeover(mddev); |
| 3083 | if (IS_ERR(priv)) { |
| 3084 | mddev->new_level = mddev->level; |
| 3085 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3086 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3087 | mddev->raid_disks -= mddev->delta_disks; |
| 3088 | mddev->delta_disks = 0; |
| 3089 | module_put(pers->owner); |
| 3090 | printk(KERN_WARNING "md: %s: %s would not accept array\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3091 | mdname(mddev), clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3092 | return PTR_ERR(priv); |
| 3093 | } |
| 3094 | |
| 3095 | /* Looks like we have a winner */ |
| 3096 | mddev_suspend(mddev); |
| 3097 | mddev->pers->stop(mddev); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 3098 | |
| 3099 | if (mddev->pers->sync_request == NULL && |
| 3100 | pers->sync_request != NULL) { |
| 3101 | /* need to add the md_redundancy_group */ |
| 3102 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 3103 | printk(KERN_WARNING |
| 3104 | "md: cannot register extra attributes for %s\n", |
| 3105 | mdname(mddev)); |
NeilBrown | 19fdb9e | 2010-05-22 08:31:36 +1000 | [diff] [blame] | 3106 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action"); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 3107 | } |
| 3108 | if (mddev->pers->sync_request != NULL && |
| 3109 | pers->sync_request == NULL) { |
| 3110 | /* need to remove the md_redundancy_group */ |
| 3111 | if (mddev->to_remove == NULL) |
| 3112 | mddev->to_remove = &md_redundancy_group; |
| 3113 | } |
| 3114 | |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 3115 | if (mddev->pers->sync_request == NULL && |
| 3116 | mddev->external) { |
| 3117 | /* We are converting from a no-redundancy array |
| 3118 | * to a redundancy array and metadata is managed |
| 3119 | * externally so we need to be sure that writes |
| 3120 | * won't block due to a need to transition |
| 3121 | * clean->dirty |
| 3122 | * until external management is started. |
| 3123 | */ |
| 3124 | mddev->in_sync = 0; |
| 3125 | mddev->safemode_delay = 0; |
| 3126 | mddev->safemode = 0; |
| 3127 | } |
| 3128 | |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3129 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 3130 | char nm[20]; |
| 3131 | if (rdev->raid_disk < 0) |
| 3132 | continue; |
NeilBrown | bf2cb0d | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 3133 | if (rdev->new_raid_disk >= mddev->raid_disks) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3134 | rdev->new_raid_disk = -1; |
| 3135 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3136 | continue; |
| 3137 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 3138 | sysfs_remove_link(&mddev->kobj, nm); |
| 3139 | } |
| 3140 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 3141 | if (rdev->raid_disk < 0) |
| 3142 | continue; |
| 3143 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3144 | continue; |
| 3145 | rdev->raid_disk = rdev->new_raid_disk; |
| 3146 | if (rdev->raid_disk < 0) |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3147 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3148 | else { |
| 3149 | char nm[20]; |
| 3150 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 3151 | if(sysfs_create_link(&mddev->kobj, &rdev->kobj, nm)) |
| 3152 | printk("md: cannot register %s for %s after level change\n", |
| 3153 | nm, mdname(mddev)); |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3154 | } |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3155 | } |
| 3156 | |
| 3157 | module_put(mddev->pers->owner); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3158 | mddev->pers = pers; |
| 3159 | mddev->private = priv; |
| 3160 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
| 3161 | mddev->level = mddev->new_level; |
| 3162 | mddev->layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3163 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3164 | mddev->delta_disks = 0; |
Trela, Maciej | 9af204c | 2010-03-08 16:02:44 +1100 | [diff] [blame] | 3165 | if (mddev->pers->sync_request == NULL) { |
| 3166 | /* this is now an array without redundancy, so |
| 3167 | * it must always be in_sync |
| 3168 | */ |
| 3169 | mddev->in_sync = 1; |
| 3170 | del_timer_sync(&mddev->safemode_timer); |
| 3171 | } |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3172 | pers->run(mddev); |
| 3173 | mddev_resume(mddev); |
| 3174 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 3175 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3176 | md_wakeup_thread(mddev->thread); |
Maciej Trela | 5cac7861 | 2010-04-14 17:17:39 +1000 | [diff] [blame] | 3177 | sysfs_notify(&mddev->kobj, NULL, "level"); |
Dan Williams | bb7f8d2 | 2010-05-01 18:14:57 -0700 | [diff] [blame] | 3178 | md_new_event(mddev); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3179 | return rv; |
| 3180 | } |
| 3181 | |
| 3182 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3183 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3184 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3185 | |
| 3186 | static ssize_t |
| 3187 | layout_show(mddev_t *mddev, char *page) |
| 3188 | { |
| 3189 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3190 | if (mddev->reshape_position != MaxSector && |
| 3191 | mddev->layout != mddev->new_layout) |
| 3192 | return sprintf(page, "%d (%d)\n", |
| 3193 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3194 | return sprintf(page, "%d\n", mddev->layout); |
| 3195 | } |
| 3196 | |
| 3197 | static ssize_t |
| 3198 | layout_store(mddev_t *mddev, const char *buf, size_t len) |
| 3199 | { |
| 3200 | char *e; |
| 3201 | unsigned long n = simple_strtoul(buf, &e, 10); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3202 | |
| 3203 | if (!*buf || (*e && *e != '\n')) |
| 3204 | return -EINVAL; |
| 3205 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3206 | if (mddev->pers) { |
| 3207 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3208 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3209 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3210 | mddev->new_layout = n; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3211 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3212 | if (err) { |
| 3213 | mddev->new_layout = mddev->layout; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3214 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3215 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3216 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3217 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3218 | if (mddev->reshape_position == MaxSector) |
| 3219 | mddev->layout = n; |
| 3220 | } |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3221 | return len; |
| 3222 | } |
| 3223 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3224 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3225 | |
| 3226 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3227 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3228 | raid_disks_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3229 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 3230 | if (mddev->raid_disks == 0) |
| 3231 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3232 | if (mddev->reshape_position != MaxSector && |
| 3233 | mddev->delta_disks != 0) |
| 3234 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 3235 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3236 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 3237 | } |
| 3238 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3239 | static int update_raid_disks(mddev_t *mddev, int raid_disks); |
| 3240 | |
| 3241 | static ssize_t |
| 3242 | raid_disks_store(mddev_t *mddev, const char *buf, size_t len) |
| 3243 | { |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3244 | char *e; |
| 3245 | int rv = 0; |
| 3246 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3247 | |
| 3248 | if (!*buf || (*e && *e != '\n')) |
| 3249 | return -EINVAL; |
| 3250 | |
| 3251 | if (mddev->pers) |
| 3252 | rv = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3253 | else if (mddev->reshape_position != MaxSector) { |
| 3254 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
| 3255 | mddev->delta_disks = n - olddisks; |
| 3256 | mddev->raid_disks = n; |
| 3257 | } else |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3258 | mddev->raid_disks = n; |
| 3259 | return rv ? rv : len; |
| 3260 | } |
| 3261 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3262 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3263 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3264 | static ssize_t |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3265 | chunk_size_show(mddev_t *mddev, char *page) |
| 3266 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3267 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3268 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 3269 | return sprintf(page, "%d (%d)\n", |
| 3270 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3271 | mddev->chunk_sectors << 9); |
| 3272 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3273 | } |
| 3274 | |
| 3275 | static ssize_t |
| 3276 | chunk_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3277 | { |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3278 | char *e; |
| 3279 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3280 | |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3281 | if (!*buf || (*e && *e != '\n')) |
| 3282 | return -EINVAL; |
| 3283 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3284 | if (mddev->pers) { |
| 3285 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3286 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3287 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3288 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3289 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3290 | if (err) { |
| 3291 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3292 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3293 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3294 | } else { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3295 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3296 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3297 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3298 | } |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3299 | return len; |
| 3300 | } |
| 3301 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3302 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3303 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3304 | static ssize_t |
| 3305 | resync_start_show(mddev_t *mddev, char *page) |
| 3306 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3307 | if (mddev->recovery_cp == MaxSector) |
| 3308 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3309 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 3310 | } |
| 3311 | |
| 3312 | static ssize_t |
| 3313 | resync_start_store(mddev_t *mddev, const char *buf, size_t len) |
| 3314 | { |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3315 | char *e; |
| 3316 | unsigned long long n = simple_strtoull(buf, &e, 10); |
| 3317 | |
| 3318 | if (mddev->pers) |
| 3319 | return -EBUSY; |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3320 | if (cmd_match(buf, "none")) |
| 3321 | n = MaxSector; |
| 3322 | else if (!*buf || (*e && *e != '\n')) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3323 | return -EINVAL; |
| 3324 | |
| 3325 | mddev->recovery_cp = n; |
| 3326 | return len; |
| 3327 | } |
| 3328 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3329 | __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3330 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3331 | /* |
| 3332 | * The array state can be: |
| 3333 | * |
| 3334 | * clear |
| 3335 | * No devices, no size, no level |
| 3336 | * Equivalent to STOP_ARRAY ioctl |
| 3337 | * inactive |
| 3338 | * May have some settings, but array is not active |
| 3339 | * all IO results in error |
| 3340 | * When written, doesn't tear down array, but just stops it |
| 3341 | * suspended (not supported yet) |
| 3342 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 3343 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3344 | * readonly |
| 3345 | * no resync can happen. no superblocks get written. |
| 3346 | * write requests fail |
| 3347 | * read-auto |
| 3348 | * like readonly, but behaves like 'clean' on a write request. |
| 3349 | * |
| 3350 | * clean - no pending writes, but otherwise active. |
| 3351 | * When written to inactive array, starts without resync |
| 3352 | * If a write request arrives then |
| 3353 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 3354 | * if not known, block and switch to write-pending |
| 3355 | * If written to an active array that has pending writes, then fails. |
| 3356 | * active |
| 3357 | * fully active: IO and resync can be happening. |
| 3358 | * When written to inactive array, starts with resync |
| 3359 | * |
| 3360 | * write-pending |
| 3361 | * clean, but writes are blocked waiting for 'active' to be written. |
| 3362 | * |
| 3363 | * active-idle |
| 3364 | * like active, but no writes have been seen for a while (100msec). |
| 3365 | * |
| 3366 | */ |
| 3367 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 3368 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3369 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3370 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 3371 | "write-pending", "active-idle", NULL }; |
| 3372 | |
| 3373 | static int match_word(const char *word, char **list) |
| 3374 | { |
| 3375 | int n; |
| 3376 | for (n=0; list[n]; n++) |
| 3377 | if (cmd_match(word, list[n])) |
| 3378 | break; |
| 3379 | return n; |
| 3380 | } |
| 3381 | |
| 3382 | static ssize_t |
| 3383 | array_state_show(mddev_t *mddev, char *page) |
| 3384 | { |
| 3385 | enum array_state st = inactive; |
| 3386 | |
| 3387 | if (mddev->pers) |
| 3388 | switch(mddev->ro) { |
| 3389 | case 1: |
| 3390 | st = readonly; |
| 3391 | break; |
| 3392 | case 2: |
| 3393 | st = read_auto; |
| 3394 | break; |
| 3395 | case 0: |
| 3396 | if (mddev->in_sync) |
| 3397 | st = clean; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3398 | else if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3399 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3400 | else if (mddev->safemode) |
| 3401 | st = active_idle; |
| 3402 | else |
| 3403 | st = active; |
| 3404 | } |
| 3405 | else { |
| 3406 | if (list_empty(&mddev->disks) && |
| 3407 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3408 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3409 | st = clear; |
| 3410 | else |
| 3411 | st = inactive; |
| 3412 | } |
| 3413 | return sprintf(page, "%s\n", array_states[st]); |
| 3414 | } |
| 3415 | |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3416 | static int do_md_stop(mddev_t * mddev, int ro, int is_open); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3417 | static int md_set_readonly(mddev_t * mddev, int is_open); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3418 | static int do_md_run(mddev_t * mddev); |
| 3419 | static int restart_array(mddev_t *mddev); |
| 3420 | |
| 3421 | static ssize_t |
| 3422 | array_state_store(mddev_t *mddev, const char *buf, size_t len) |
| 3423 | { |
| 3424 | int err = -EINVAL; |
| 3425 | enum array_state st = match_word(buf, array_states); |
| 3426 | switch(st) { |
| 3427 | case bad_word: |
| 3428 | break; |
| 3429 | case clear: |
| 3430 | /* stopping an active array */ |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3431 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3432 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3433 | err = do_md_stop(mddev, 0, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3434 | break; |
| 3435 | case inactive: |
| 3436 | /* stopping an active array */ |
| 3437 | if (mddev->pers) { |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3438 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3439 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3440 | err = do_md_stop(mddev, 2, 0); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3441 | } else |
| 3442 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3443 | break; |
| 3444 | case suspended: |
| 3445 | break; /* not supported yet */ |
| 3446 | case readonly: |
| 3447 | if (mddev->pers) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3448 | err = md_set_readonly(mddev, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3449 | else { |
| 3450 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3451 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3452 | err = do_md_run(mddev); |
| 3453 | } |
| 3454 | break; |
| 3455 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3456 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3457 | if (mddev->ro == 0) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3458 | err = md_set_readonly(mddev, 0); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3459 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3460 | err = restart_array(mddev); |
| 3461 | if (err == 0) { |
| 3462 | mddev->ro = 2; |
| 3463 | set_disk_ro(mddev->gendisk, 0); |
| 3464 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3465 | } else { |
| 3466 | mddev->ro = 2; |
| 3467 | err = do_md_run(mddev); |
| 3468 | } |
| 3469 | break; |
| 3470 | case clean: |
| 3471 | if (mddev->pers) { |
| 3472 | restart_array(mddev); |
| 3473 | spin_lock_irq(&mddev->write_lock); |
| 3474 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3475 | if (mddev->in_sync == 0) { |
| 3476 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3477 | if (mddev->safemode == 1) |
| 3478 | mddev->safemode = 0; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3479 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3480 | } |
| 3481 | err = 0; |
| 3482 | } else |
| 3483 | err = -EBUSY; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3484 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 3485 | } else |
| 3486 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3487 | break; |
| 3488 | case active: |
| 3489 | if (mddev->pers) { |
| 3490 | restart_array(mddev); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3491 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3492 | wake_up(&mddev->sb_wait); |
| 3493 | err = 0; |
| 3494 | } else { |
| 3495 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3496 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3497 | err = do_md_run(mddev); |
| 3498 | } |
| 3499 | break; |
| 3500 | case write_pending: |
| 3501 | case active_idle: |
| 3502 | /* these cannot be set */ |
| 3503 | break; |
| 3504 | } |
| 3505 | if (err) |
| 3506 | return err; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3507 | else { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 3508 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3509 | return len; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3510 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3511 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3512 | static struct md_sysfs_entry md_array_state = |
| 3513 | __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3514 | |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3515 | static ssize_t |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 3516 | max_corrected_read_errors_show(mddev_t *mddev, char *page) { |
| 3517 | return sprintf(page, "%d\n", |
| 3518 | atomic_read(&mddev->max_corr_read_errors)); |
| 3519 | } |
| 3520 | |
| 3521 | static ssize_t |
| 3522 | max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len) |
| 3523 | { |
| 3524 | char *e; |
| 3525 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3526 | |
| 3527 | if (*buf && (*e == 0 || *e == '\n')) { |
| 3528 | atomic_set(&mddev->max_corr_read_errors, n); |
| 3529 | return len; |
| 3530 | } |
| 3531 | return -EINVAL; |
| 3532 | } |
| 3533 | |
| 3534 | static struct md_sysfs_entry max_corr_read_errors = |
| 3535 | __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show, |
| 3536 | max_corrected_read_errors_store); |
| 3537 | |
| 3538 | static ssize_t |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3539 | null_show(mddev_t *mddev, char *page) |
| 3540 | { |
| 3541 | return -EINVAL; |
| 3542 | } |
| 3543 | |
| 3544 | static ssize_t |
| 3545 | new_dev_store(mddev_t *mddev, const char *buf, size_t len) |
| 3546 | { |
| 3547 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 3548 | /* The new device is added to the array. |
| 3549 | * If the array has a persistent superblock, we read the |
| 3550 | * superblock to initialise info and check validity. |
| 3551 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 3552 | * which mainly checks size. |
| 3553 | */ |
| 3554 | char *e; |
| 3555 | int major = simple_strtoul(buf, &e, 10); |
| 3556 | int minor; |
| 3557 | dev_t dev; |
| 3558 | mdk_rdev_t *rdev; |
| 3559 | int err; |
| 3560 | |
| 3561 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 3562 | return -EINVAL; |
| 3563 | minor = simple_strtoul(e+1, &e, 10); |
| 3564 | if (*e && *e != '\n') |
| 3565 | return -EINVAL; |
| 3566 | dev = MKDEV(major, minor); |
| 3567 | if (major != MAJOR(dev) || |
| 3568 | minor != MINOR(dev)) |
| 3569 | return -EOVERFLOW; |
| 3570 | |
| 3571 | |
| 3572 | if (mddev->persistent) { |
| 3573 | rdev = md_import_device(dev, mddev->major_version, |
| 3574 | mddev->minor_version); |
| 3575 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
| 3576 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 3577 | mdk_rdev_t, same_set); |
| 3578 | err = super_types[mddev->major_version] |
| 3579 | .load_super(rdev, rdev0, mddev->minor_version); |
| 3580 | if (err < 0) |
| 3581 | goto out; |
| 3582 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3583 | } else if (mddev->external) |
| 3584 | rdev = md_import_device(dev, -2, -1); |
| 3585 | else |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3586 | rdev = md_import_device(dev, -1, -1); |
| 3587 | |
| 3588 | if (IS_ERR(rdev)) |
| 3589 | return PTR_ERR(rdev); |
| 3590 | err = bind_rdev_to_array(rdev, mddev); |
| 3591 | out: |
| 3592 | if (err) |
| 3593 | export_rdev(rdev); |
| 3594 | return err ? err : len; |
| 3595 | } |
| 3596 | |
| 3597 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3598 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3599 | |
| 3600 | static ssize_t |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3601 | bitmap_store(mddev_t *mddev, const char *buf, size_t len) |
| 3602 | { |
| 3603 | char *end; |
| 3604 | unsigned long chunk, end_chunk; |
| 3605 | |
| 3606 | if (!mddev->bitmap) |
| 3607 | goto out; |
| 3608 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 3609 | while (*buf) { |
| 3610 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 3611 | if (buf == end) break; |
| 3612 | if (*end == '-') { /* range */ |
| 3613 | buf = end + 1; |
| 3614 | end_chunk = simple_strtoul(buf, &end, 0); |
| 3615 | if (buf == end) break; |
| 3616 | } |
| 3617 | if (*end && !isspace(*end)) break; |
| 3618 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 3619 | buf = skip_spaces(end); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3620 | } |
| 3621 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 3622 | out: |
| 3623 | return len; |
| 3624 | } |
| 3625 | |
| 3626 | static struct md_sysfs_entry md_bitmap = |
| 3627 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 3628 | |
| 3629 | static ssize_t |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3630 | size_show(mddev_t *mddev, char *page) |
| 3631 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3632 | return sprintf(page, "%llu\n", |
| 3633 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3634 | } |
| 3635 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3636 | static int update_size(mddev_t *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3637 | |
| 3638 | static ssize_t |
| 3639 | size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3640 | { |
| 3641 | /* If array is inactive, we can reduce the component size, but |
| 3642 | * not increase it (except from 0). |
| 3643 | * If array is active, we can try an on-line resize |
| 3644 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3645 | sector_t sectors; |
| 3646 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3647 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3648 | if (err < 0) |
| 3649 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3650 | if (mddev->pers) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3651 | err = update_size(mddev, sectors); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3652 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3653 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3654 | if (mddev->dev_sectors == 0 || |
| 3655 | mddev->dev_sectors > sectors) |
| 3656 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3657 | else |
| 3658 | err = -ENOSPC; |
| 3659 | } |
| 3660 | return err ? err : len; |
| 3661 | } |
| 3662 | |
| 3663 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3664 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3665 | |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3666 | |
| 3667 | /* Metdata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3668 | * This is one of |
| 3669 | * 'none' for arrays with no metadata (good luck...) |
| 3670 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3671 | * or N.M for internally known formats |
| 3672 | */ |
| 3673 | static ssize_t |
| 3674 | metadata_show(mddev_t *mddev, char *page) |
| 3675 | { |
| 3676 | if (mddev->persistent) |
| 3677 | return sprintf(page, "%d.%d\n", |
| 3678 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3679 | else if (mddev->external) |
| 3680 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3681 | else |
| 3682 | return sprintf(page, "none\n"); |
| 3683 | } |
| 3684 | |
| 3685 | static ssize_t |
| 3686 | metadata_store(mddev_t *mddev, const char *buf, size_t len) |
| 3687 | { |
| 3688 | int major, minor; |
| 3689 | char *e; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 3690 | /* Changing the details of 'external' metadata is |
| 3691 | * always permitted. Otherwise there must be |
| 3692 | * no devices attached to the array. |
| 3693 | */ |
| 3694 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 3695 | ; |
| 3696 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3697 | return -EBUSY; |
| 3698 | |
| 3699 | if (cmd_match(buf, "none")) { |
| 3700 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3701 | mddev->external = 0; |
| 3702 | mddev->major_version = 0; |
| 3703 | mddev->minor_version = 90; |
| 3704 | return len; |
| 3705 | } |
| 3706 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 3707 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3708 | if (namelen >= sizeof(mddev->metadata_type)) |
| 3709 | namelen = sizeof(mddev->metadata_type)-1; |
| 3710 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 3711 | mddev->metadata_type[namelen] = 0; |
| 3712 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 3713 | mddev->metadata_type[--namelen] = 0; |
| 3714 | mddev->persistent = 0; |
| 3715 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3716 | mddev->major_version = 0; |
| 3717 | mddev->minor_version = 90; |
| 3718 | return len; |
| 3719 | } |
| 3720 | major = simple_strtoul(buf, &e, 10); |
| 3721 | if (e==buf || *e != '.') |
| 3722 | return -EINVAL; |
| 3723 | buf = e+1; |
| 3724 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 3725 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3726 | return -EINVAL; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 3727 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3728 | return -ENOENT; |
| 3729 | mddev->major_version = major; |
| 3730 | mddev->minor_version = minor; |
| 3731 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3732 | mddev->external = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3733 | return len; |
| 3734 | } |
| 3735 | |
| 3736 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3737 | __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3738 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3739 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3740 | action_show(mddev_t *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3741 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3742 | char *type = "idle"; |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3743 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 3744 | type = "frozen"; |
| 3745 | else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 2b12ab6 | 2007-10-16 23:30:53 -0700 | [diff] [blame] | 3746 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3747 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 3748 | type = "reshape"; |
| 3749 | else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3750 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 3751 | type = "resync"; |
| 3752 | else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 3753 | type = "check"; |
| 3754 | else |
| 3755 | type = "repair"; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3756 | } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3757 | type = "recover"; |
| 3758 | } |
| 3759 | return sprintf(page, "%s\n", type); |
| 3760 | } |
| 3761 | |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 3762 | static void reap_sync_thread(mddev_t *mddev); |
| 3763 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3764 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3765 | action_store(mddev_t *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3766 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3767 | if (!mddev->pers || !mddev->pers->sync_request) |
| 3768 | return -EINVAL; |
| 3769 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3770 | if (cmd_match(page, "frozen")) |
| 3771 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3772 | else |
| 3773 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3774 | |
| 3775 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3776 | if (mddev->sync_thread) { |
| 3777 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 3778 | reap_sync_thread(mddev); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3779 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3780 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 3781 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3782 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3783 | else if (cmd_match(page, "resync")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3784 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3785 | else if (cmd_match(page, "recover")) { |
| 3786 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 3787 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3788 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3789 | int err; |
| 3790 | if (mddev->pers->start_reshape == NULL) |
| 3791 | return -EINVAL; |
| 3792 | err = mddev->pers->start_reshape(mddev); |
| 3793 | if (err) |
| 3794 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 3795 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3796 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 3797 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3798 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 3799 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3800 | return -EINVAL; |
| 3801 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 3802 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3803 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3804 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3805 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 3806 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3807 | return len; |
| 3808 | } |
| 3809 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3810 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3811 | mismatch_cnt_show(mddev_t *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3812 | { |
| 3813 | return sprintf(page, "%llu\n", |
| 3814 | (unsigned long long) mddev->resync_mismatches); |
| 3815 | } |
| 3816 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3817 | static struct md_sysfs_entry md_scan_mode = |
| 3818 | __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3819 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3820 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3821 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3822 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3823 | static ssize_t |
| 3824 | sync_min_show(mddev_t *mddev, char *page) |
| 3825 | { |
| 3826 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 3827 | mddev->sync_speed_min ? "local": "system"); |
| 3828 | } |
| 3829 | |
| 3830 | static ssize_t |
| 3831 | sync_min_store(mddev_t *mddev, const char *buf, size_t len) |
| 3832 | { |
| 3833 | int min; |
| 3834 | char *e; |
| 3835 | if (strncmp(buf, "system", 6)==0) { |
| 3836 | mddev->sync_speed_min = 0; |
| 3837 | return len; |
| 3838 | } |
| 3839 | min = simple_strtoul(buf, &e, 10); |
| 3840 | if (buf == e || (*e && *e != '\n') || min <= 0) |
| 3841 | return -EINVAL; |
| 3842 | mddev->sync_speed_min = min; |
| 3843 | return len; |
| 3844 | } |
| 3845 | |
| 3846 | static struct md_sysfs_entry md_sync_min = |
| 3847 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 3848 | |
| 3849 | static ssize_t |
| 3850 | sync_max_show(mddev_t *mddev, char *page) |
| 3851 | { |
| 3852 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 3853 | mddev->sync_speed_max ? "local": "system"); |
| 3854 | } |
| 3855 | |
| 3856 | static ssize_t |
| 3857 | sync_max_store(mddev_t *mddev, const char *buf, size_t len) |
| 3858 | { |
| 3859 | int max; |
| 3860 | char *e; |
| 3861 | if (strncmp(buf, "system", 6)==0) { |
| 3862 | mddev->sync_speed_max = 0; |
| 3863 | return len; |
| 3864 | } |
| 3865 | max = simple_strtoul(buf, &e, 10); |
| 3866 | if (buf == e || (*e && *e != '\n') || max <= 0) |
| 3867 | return -EINVAL; |
| 3868 | mddev->sync_speed_max = max; |
| 3869 | return len; |
| 3870 | } |
| 3871 | |
| 3872 | static struct md_sysfs_entry md_sync_max = |
| 3873 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 3874 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 3875 | static ssize_t |
| 3876 | degraded_show(mddev_t *mddev, char *page) |
| 3877 | { |
| 3878 | return sprintf(page, "%d\n", mddev->degraded); |
| 3879 | } |
| 3880 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3881 | |
| 3882 | static ssize_t |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 3883 | sync_force_parallel_show(mddev_t *mddev, char *page) |
| 3884 | { |
| 3885 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 3886 | } |
| 3887 | |
| 3888 | static ssize_t |
| 3889 | sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len) |
| 3890 | { |
| 3891 | long n; |
| 3892 | |
| 3893 | if (strict_strtol(buf, 10, &n)) |
| 3894 | return -EINVAL; |
| 3895 | |
| 3896 | if (n != 0 && n != 1) |
| 3897 | return -EINVAL; |
| 3898 | |
| 3899 | mddev->parallel_resync = n; |
| 3900 | |
| 3901 | if (mddev->sync_thread) |
| 3902 | wake_up(&resync_wait); |
| 3903 | |
| 3904 | return len; |
| 3905 | } |
| 3906 | |
| 3907 | /* force parallel resync, even with shared block devices */ |
| 3908 | static struct md_sysfs_entry md_sync_force_parallel = |
| 3909 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 3910 | sync_force_parallel_show, sync_force_parallel_store); |
| 3911 | |
| 3912 | static ssize_t |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3913 | sync_speed_show(mddev_t *mddev, char *page) |
| 3914 | { |
| 3915 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3916 | if (mddev->curr_resync == 0) |
| 3917 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3918 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 3919 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3920 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3921 | db = resync - mddev->resync_mark_cnt; |
| 3922 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3923 | } |
| 3924 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3925 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3926 | |
| 3927 | static ssize_t |
| 3928 | sync_completed_show(mddev_t *mddev, char *page) |
| 3929 | { |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 3930 | unsigned long long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3931 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 3932 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3933 | return sprintf(page, "none\n"); |
| 3934 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3935 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3936 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3937 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3938 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3939 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 3940 | resync = mddev->curr_resync_completed; |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 3941 | return sprintf(page, "%llu / %llu\n", resync, max_sectors); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3942 | } |
| 3943 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3944 | static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3945 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3946 | static ssize_t |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3947 | min_sync_show(mddev_t *mddev, char *page) |
| 3948 | { |
| 3949 | return sprintf(page, "%llu\n", |
| 3950 | (unsigned long long)mddev->resync_min); |
| 3951 | } |
| 3952 | static ssize_t |
| 3953 | min_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3954 | { |
| 3955 | unsigned long long min; |
| 3956 | if (strict_strtoull(buf, 10, &min)) |
| 3957 | return -EINVAL; |
| 3958 | if (min > mddev->resync_max) |
| 3959 | return -EINVAL; |
| 3960 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3961 | return -EBUSY; |
| 3962 | |
| 3963 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3964 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 3965 | sector_t temp = min; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3966 | if (sector_div(temp, mddev->chunk_sectors)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3967 | return -EINVAL; |
| 3968 | } |
| 3969 | mddev->resync_min = min; |
| 3970 | |
| 3971 | return len; |
| 3972 | } |
| 3973 | |
| 3974 | static struct md_sysfs_entry md_min_sync = |
| 3975 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 3976 | |
| 3977 | static ssize_t |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3978 | max_sync_show(mddev_t *mddev, char *page) |
| 3979 | { |
| 3980 | if (mddev->resync_max == MaxSector) |
| 3981 | return sprintf(page, "max\n"); |
| 3982 | else |
| 3983 | return sprintf(page, "%llu\n", |
| 3984 | (unsigned long long)mddev->resync_max); |
| 3985 | } |
| 3986 | static ssize_t |
| 3987 | max_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3988 | { |
| 3989 | if (strncmp(buf, "max", 3) == 0) |
| 3990 | mddev->resync_max = MaxSector; |
| 3991 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3992 | unsigned long long max; |
| 3993 | if (strict_strtoull(buf, 10, &max)) |
| 3994 | return -EINVAL; |
| 3995 | if (max < mddev->resync_min) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3996 | return -EINVAL; |
| 3997 | if (max < mddev->resync_max && |
NeilBrown | 4d484a4 | 2009-08-13 10:41:50 +1000 | [diff] [blame] | 3998 | mddev->ro == 0 && |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3999 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4000 | return -EBUSY; |
| 4001 | |
| 4002 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4003 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 4004 | sector_t temp = max; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4005 | if (sector_div(temp, mddev->chunk_sectors)) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4006 | return -EINVAL; |
| 4007 | } |
| 4008 | mddev->resync_max = max; |
| 4009 | } |
| 4010 | wake_up(&mddev->recovery_wait); |
| 4011 | return len; |
| 4012 | } |
| 4013 | |
| 4014 | static struct md_sysfs_entry md_max_sync = |
| 4015 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 4016 | |
| 4017 | static ssize_t |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4018 | suspend_lo_show(mddev_t *mddev, char *page) |
| 4019 | { |
| 4020 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 4021 | } |
| 4022 | |
| 4023 | static ssize_t |
| 4024 | suspend_lo_store(mddev_t *mddev, const char *buf, size_t len) |
| 4025 | { |
| 4026 | char *e; |
| 4027 | unsigned long long new = simple_strtoull(buf, &e, 10); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4028 | unsigned long long old = mddev->suspend_lo; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4029 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 4030 | if (mddev->pers == NULL || |
| 4031 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4032 | return -EINVAL; |
| 4033 | if (buf == e || (*e && *e != '\n')) |
| 4034 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4035 | |
| 4036 | mddev->suspend_lo = new; |
| 4037 | if (new >= old) |
| 4038 | /* Shrinking suspended region */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4039 | mddev->pers->quiesce(mddev, 2); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4040 | else { |
| 4041 | /* Expanding suspended region - need to wait */ |
| 4042 | mddev->pers->quiesce(mddev, 1); |
| 4043 | mddev->pers->quiesce(mddev, 0); |
| 4044 | } |
| 4045 | return len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4046 | } |
| 4047 | static struct md_sysfs_entry md_suspend_lo = |
| 4048 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 4049 | |
| 4050 | |
| 4051 | static ssize_t |
| 4052 | suspend_hi_show(mddev_t *mddev, char *page) |
| 4053 | { |
| 4054 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 4055 | } |
| 4056 | |
| 4057 | static ssize_t |
| 4058 | suspend_hi_store(mddev_t *mddev, const char *buf, size_t len) |
| 4059 | { |
| 4060 | char *e; |
| 4061 | unsigned long long new = simple_strtoull(buf, &e, 10); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4062 | unsigned long long old = mddev->suspend_hi; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4063 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 4064 | if (mddev->pers == NULL || |
| 4065 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4066 | return -EINVAL; |
| 4067 | if (buf == e || (*e && *e != '\n')) |
| 4068 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4069 | |
| 4070 | mddev->suspend_hi = new; |
| 4071 | if (new <= old) |
| 4072 | /* Shrinking suspended region */ |
| 4073 | mddev->pers->quiesce(mddev, 2); |
| 4074 | else { |
| 4075 | /* Expanding suspended region - need to wait */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4076 | mddev->pers->quiesce(mddev, 1); |
| 4077 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4078 | } |
| 4079 | return len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4080 | } |
| 4081 | static struct md_sysfs_entry md_suspend_hi = |
| 4082 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 4083 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4084 | static ssize_t |
| 4085 | reshape_position_show(mddev_t *mddev, char *page) |
| 4086 | { |
| 4087 | if (mddev->reshape_position != MaxSector) |
| 4088 | return sprintf(page, "%llu\n", |
| 4089 | (unsigned long long)mddev->reshape_position); |
| 4090 | strcpy(page, "none\n"); |
| 4091 | return 5; |
| 4092 | } |
| 4093 | |
| 4094 | static ssize_t |
| 4095 | reshape_position_store(mddev_t *mddev, const char *buf, size_t len) |
| 4096 | { |
| 4097 | char *e; |
| 4098 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 4099 | if (mddev->pers) |
| 4100 | return -EBUSY; |
| 4101 | if (buf == e || (*e && *e != '\n')) |
| 4102 | return -EINVAL; |
| 4103 | mddev->reshape_position = new; |
| 4104 | mddev->delta_disks = 0; |
| 4105 | mddev->new_level = mddev->level; |
| 4106 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4107 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4108 | return len; |
| 4109 | } |
| 4110 | |
| 4111 | static struct md_sysfs_entry md_reshape_position = |
| 4112 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 4113 | reshape_position_store); |
| 4114 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4115 | static ssize_t |
| 4116 | array_size_show(mddev_t *mddev, char *page) |
| 4117 | { |
| 4118 | if (mddev->external_size) |
| 4119 | return sprintf(page, "%llu\n", |
| 4120 | (unsigned long long)mddev->array_sectors/2); |
| 4121 | else |
| 4122 | return sprintf(page, "default\n"); |
| 4123 | } |
| 4124 | |
| 4125 | static ssize_t |
| 4126 | array_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 4127 | { |
| 4128 | sector_t sectors; |
| 4129 | |
| 4130 | if (strncmp(buf, "default", 7) == 0) { |
| 4131 | if (mddev->pers) |
| 4132 | sectors = mddev->pers->size(mddev, 0, 0); |
| 4133 | else |
| 4134 | sectors = mddev->array_sectors; |
| 4135 | |
| 4136 | mddev->external_size = 0; |
| 4137 | } else { |
| 4138 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
| 4139 | return -EINVAL; |
| 4140 | if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
NeilBrown | 2b69c83 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4141 | return -E2BIG; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4142 | |
| 4143 | mddev->external_size = 1; |
| 4144 | } |
| 4145 | |
| 4146 | mddev->array_sectors = sectors; |
NeilBrown | cbe6ef1 | 2011-02-16 13:58:38 +1100 | [diff] [blame] | 4147 | if (mddev->pers) { |
| 4148 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 4149 | revalidate_disk(mddev->gendisk); |
NeilBrown | cbe6ef1 | 2011-02-16 13:58:38 +1100 | [diff] [blame] | 4150 | } |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4151 | return len; |
| 4152 | } |
| 4153 | |
| 4154 | static struct md_sysfs_entry md_array_size = |
| 4155 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 4156 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4157 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4158 | static struct attribute *md_default_attrs[] = { |
| 4159 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4160 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4161 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4162 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4163 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4164 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4165 | &md_metadata.attr, |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4166 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4167 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4168 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4169 | &md_reshape_position.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4170 | &md_array_size.attr, |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4171 | &max_corr_read_errors.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4172 | NULL, |
| 4173 | }; |
| 4174 | |
| 4175 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4176 | &md_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4177 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4178 | &md_sync_min.attr, |
| 4179 | &md_sync_max.attr, |
| 4180 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4181 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4182 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4183 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4184 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4185 | &md_suspend_lo.attr, |
| 4186 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4187 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4188 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4189 | NULL, |
| 4190 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4191 | static struct attribute_group md_redundancy_group = { |
| 4192 | .name = NULL, |
| 4193 | .attrs = md_redundancy_attrs, |
| 4194 | }; |
| 4195 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4196 | |
| 4197 | static ssize_t |
| 4198 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 4199 | { |
| 4200 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 4201 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4202 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4203 | |
| 4204 | if (!entry->show) |
| 4205 | return -EIO; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4206 | rv = mddev_lock(mddev); |
| 4207 | if (!rv) { |
| 4208 | rv = entry->show(mddev, page); |
| 4209 | mddev_unlock(mddev); |
| 4210 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4211 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4212 | } |
| 4213 | |
| 4214 | static ssize_t |
| 4215 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 4216 | const char *page, size_t length) |
| 4217 | { |
| 4218 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 4219 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4220 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4221 | |
| 4222 | if (!entry->store) |
| 4223 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 4224 | if (!capable(CAP_SYS_ADMIN)) |
| 4225 | return -EACCES; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4226 | rv = mddev_lock(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4227 | if (mddev->hold_active == UNTIL_IOCTL) |
| 4228 | mddev->hold_active = 0; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4229 | if (!rv) { |
| 4230 | rv = entry->store(mddev, page, length); |
| 4231 | mddev_unlock(mddev); |
| 4232 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4233 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4234 | } |
| 4235 | |
| 4236 | static void md_free(struct kobject *ko) |
| 4237 | { |
| 4238 | mddev_t *mddev = container_of(ko, mddev_t, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4239 | |
| 4240 | if (mddev->sysfs_state) |
| 4241 | sysfs_put(mddev->sysfs_state); |
| 4242 | |
| 4243 | if (mddev->gendisk) { |
| 4244 | del_gendisk(mddev->gendisk); |
| 4245 | put_disk(mddev->gendisk); |
| 4246 | } |
| 4247 | if (mddev->queue) |
| 4248 | blk_cleanup_queue(mddev->queue); |
| 4249 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4250 | kfree(mddev); |
| 4251 | } |
| 4252 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 4253 | static const struct sysfs_ops md_sysfs_ops = { |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4254 | .show = md_attr_show, |
| 4255 | .store = md_attr_store, |
| 4256 | }; |
| 4257 | static struct kobj_type md_ktype = { |
| 4258 | .release = md_free, |
| 4259 | .sysfs_ops = &md_sysfs_ops, |
| 4260 | .default_attrs = md_default_attrs, |
| 4261 | }; |
| 4262 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4263 | int mdp_major = 0; |
| 4264 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4265 | static void mddev_delayed_delete(struct work_struct *ws) |
| 4266 | { |
| 4267 | mddev_t *mddev = container_of(ws, mddev_t, del_work); |
| 4268 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4269 | sysfs_remove_group(&mddev->kobj, &md_bitmap_group); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4270 | kobject_del(&mddev->kobj); |
| 4271 | kobject_put(&mddev->kobj); |
| 4272 | } |
| 4273 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4274 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4275 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4276 | static DEFINE_MUTEX(disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4277 | mddev_t *mddev = mddev_find(dev); |
| 4278 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4279 | int partitioned; |
| 4280 | int shift; |
| 4281 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4282 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4283 | |
| 4284 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4285 | return -ENODEV; |
| 4286 | |
| 4287 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 4288 | shift = partitioned ? MdpMinorShift : 0; |
| 4289 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4290 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4291 | /* wait for any previous instance of this device to be |
| 4292 | * completely removed (mddev_delayed_delete). |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4293 | */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4294 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4295 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4296 | mutex_lock(&disks_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4297 | error = -EEXIST; |
| 4298 | if (mddev->gendisk) |
| 4299 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4300 | |
| 4301 | if (name) { |
| 4302 | /* Need to ensure that 'name' is not a duplicate. |
| 4303 | */ |
| 4304 | mddev_t *mddev2; |
| 4305 | spin_lock(&all_mddevs_lock); |
| 4306 | |
| 4307 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 4308 | if (mddev2->gendisk && |
| 4309 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 4310 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4311 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4312 | } |
| 4313 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4314 | } |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4315 | |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4316 | error = -ENOMEM; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4317 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4318 | if (!mddev->queue) |
| 4319 | goto abort; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4320 | mddev->queue->queuedata = mddev; |
| 4321 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4322 | blk_queue_make_request(mddev->queue, md_make_request); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4323 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4324 | disk = alloc_disk(1 << shift); |
| 4325 | if (!disk) { |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4326 | blk_cleanup_queue(mddev->queue); |
| 4327 | mddev->queue = NULL; |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4328 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4329 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4330 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4331 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4332 | if (name) |
| 4333 | strcpy(disk->disk_name, name); |
| 4334 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4335 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f46 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 4336 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4337 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4338 | disk->fops = &md_fops; |
| 4339 | disk->private_data = mddev; |
| 4340 | disk->queue = mddev->queue; |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4341 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4342 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4343 | * remove it now. |
| 4344 | */ |
| 4345 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4346 | add_disk(disk); |
| 4347 | mddev->gendisk = disk; |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 4348 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
| 4349 | &disk_to_dev(disk)->kobj, "%s", "md"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4350 | if (error) { |
| 4351 | /* This isn't possible, but as kobject_init_and_add is marked |
| 4352 | * __must_check, we must do something with the result |
| 4353 | */ |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4354 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 4355 | disk->disk_name); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4356 | error = 0; |
| 4357 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4358 | if (mddev->kobj.sd && |
| 4359 | sysfs_create_group(&mddev->kobj, &md_bitmap_group)) |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4360 | printk(KERN_DEBUG "pointless warning\n"); |
Darrick J. Wong | be20e6c | 2010-11-24 16:40:33 +1100 | [diff] [blame] | 4361 | |
| 4362 | blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4363 | abort: |
| 4364 | mutex_unlock(&disks_mutex); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4365 | if (!error && mddev->kobj.sd) { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4366 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4367 | mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state"); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4368 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4369 | mddev_put(mddev); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4370 | return error; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4371 | } |
| 4372 | |
| 4373 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 4374 | { |
| 4375 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4376 | return NULL; |
| 4377 | } |
| 4378 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4379 | static int add_named_array(const char *val, struct kernel_param *kp) |
| 4380 | { |
| 4381 | /* val must be "md_*" where * is not all digits. |
| 4382 | * We allocate an array with a large free minor number, and |
| 4383 | * set the name to val. val must not already be an active name. |
| 4384 | */ |
| 4385 | int len = strlen(val); |
| 4386 | char buf[DISK_NAME_LEN]; |
| 4387 | |
| 4388 | while (len && val[len-1] == '\n') |
| 4389 | len--; |
| 4390 | if (len >= DISK_NAME_LEN) |
| 4391 | return -E2BIG; |
| 4392 | strlcpy(buf, val, len+1); |
| 4393 | if (strncmp(buf, "md_", 3) != 0) |
| 4394 | return -EINVAL; |
| 4395 | return md_alloc(0, buf); |
| 4396 | } |
| 4397 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4398 | static void md_safemode_timeout(unsigned long data) |
| 4399 | { |
| 4400 | mddev_t *mddev = (mddev_t *) data; |
| 4401 | |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4402 | if (!atomic_read(&mddev->writes_pending)) { |
| 4403 | mddev->safemode = 1; |
| 4404 | if (mddev->external) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4405 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4406 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4407 | md_wakeup_thread(mddev->thread); |
| 4408 | } |
| 4409 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4410 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4411 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4412 | int md_run(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4413 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4414 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4415 | mdk_rdev_t *rdev; |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4416 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4417 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4418 | if (list_empty(&mddev->disks)) |
| 4419 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4420 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4421 | |
| 4422 | if (mddev->pers) |
| 4423 | return -EBUSY; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 4424 | /* Cannot run until previous stop completes properly */ |
| 4425 | if (mddev->sysfs_active) |
| 4426 | return -EBUSY; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 4427 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4428 | /* |
| 4429 | * Analyze all RAID superblock(s) |
| 4430 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4431 | if (!mddev->raid_disks) { |
| 4432 | if (!mddev->persistent) |
| 4433 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4434 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4435 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4436 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4437 | if (mddev->level != LEVEL_NONE) |
| 4438 | request_module("md-level-%d", mddev->level); |
| 4439 | else if (mddev->clevel[0]) |
| 4440 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4441 | |
| 4442 | /* |
| 4443 | * Drop all container device buffers, from now on |
| 4444 | * the only valid external interface is through the md |
| 4445 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4446 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4447 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4448 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4449 | continue; |
| 4450 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 4451 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4452 | |
| 4453 | /* perform some consistency tests on the device. |
| 4454 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4455 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4456 | */ |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4457 | if (rdev->meta_bdev) { |
| 4458 | /* Nothing to check */; |
| 4459 | } else if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4460 | if (mddev->dev_sectors && |
| 4461 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4462 | > rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4463 | printk("md: %s: data overlaps metadata\n", |
| 4464 | mdname(mddev)); |
| 4465 | return -EINVAL; |
| 4466 | } |
| 4467 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4468 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4469 | > rdev->data_offset) { |
| 4470 | printk("md: %s: metadata overlaps data\n", |
| 4471 | mdname(mddev)); |
| 4472 | return -EINVAL; |
| 4473 | } |
| 4474 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4475 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4476 | } |
| 4477 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 4478 | if (mddev->bio_set == NULL) |
| 4479 | mddev->bio_set = bioset_create(BIO_POOL_SIZE, sizeof(mddev)); |
| 4480 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4481 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4482 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4483 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4484 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4485 | if (mddev->level != LEVEL_NONE) |
| 4486 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 4487 | mddev->level); |
| 4488 | else |
| 4489 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 4490 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4491 | return -EINVAL; |
| 4492 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4493 | mddev->pers = pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4494 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4495 | if (mddev->level != pers->level) { |
| 4496 | mddev->level = pers->level; |
| 4497 | mddev->new_level = pers->level; |
| 4498 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4499 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4500 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4501 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4502 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4503 | /* This personality cannot handle reshaping... */ |
| 4504 | mddev->pers = NULL; |
| 4505 | module_put(pers->owner); |
| 4506 | return -EINVAL; |
| 4507 | } |
| 4508 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4509 | if (pers->sync_request) { |
| 4510 | /* Warn if this is a potentially silly |
| 4511 | * configuration. |
| 4512 | */ |
| 4513 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 4514 | mdk_rdev_t *rdev2; |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4515 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4516 | |
| 4517 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4518 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4519 | if (rdev < rdev2 && |
| 4520 | rdev->bdev->bd_contains == |
| 4521 | rdev2->bdev->bd_contains) { |
| 4522 | printk(KERN_WARNING |
| 4523 | "%s: WARNING: %s appears to be" |
| 4524 | " on the same physical disk as" |
| 4525 | " %s.\n", |
| 4526 | mdname(mddev), |
| 4527 | bdevname(rdev->bdev,b), |
| 4528 | bdevname(rdev2->bdev,b2)); |
| 4529 | warned = 1; |
| 4530 | } |
| 4531 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4532 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4533 | if (warned) |
| 4534 | printk(KERN_WARNING |
| 4535 | "True protection against single-disk" |
| 4536 | " failure might be compromised.\n"); |
| 4537 | } |
| 4538 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 4539 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4540 | /* may be over-ridden by personality */ |
| 4541 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 4542 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4543 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4544 | |
NeilBrown | 0f9552b5 | 2009-12-30 12:08:50 +1100 | [diff] [blame] | 4545 | if (start_readonly && mddev->ro == 0) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4546 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 4547 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4548 | err = mddev->pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 4549 | if (err) |
| 4550 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4551 | else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) { |
| 4552 | WARN_ONCE(!mddev->external_size, "%s: default size too small," |
| 4553 | " but 'external_size' not in effect?\n", __func__); |
| 4554 | printk(KERN_ERR |
| 4555 | "md: invalid array_size %llu > default size %llu\n", |
| 4556 | (unsigned long long)mddev->array_sectors / 2, |
| 4557 | (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2); |
| 4558 | err = -EINVAL; |
| 4559 | mddev->pers->stop(mddev); |
| 4560 | } |
| 4561 | if (err == 0 && mddev->pers->sync_request) { |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4562 | err = bitmap_create(mddev); |
| 4563 | if (err) { |
| 4564 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 4565 | mdname(mddev), err); |
| 4566 | mddev->pers->stop(mddev); |
| 4567 | } |
| 4568 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4569 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4570 | module_put(mddev->pers->owner); |
| 4571 | mddev->pers = NULL; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4572 | bitmap_destroy(mddev); |
| 4573 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4574 | } |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4575 | if (mddev->pers->sync_request) { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4576 | if (mddev->kobj.sd && |
| 4577 | sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4578 | printk(KERN_WARNING |
| 4579 | "md: cannot register extra attributes for %s\n", |
| 4580 | mdname(mddev)); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4581 | mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4582 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 4583 | mddev->ro = 0; |
| 4584 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4585 | atomic_set(&mddev->writes_pending,0); |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4586 | atomic_set(&mddev->max_corr_read_errors, |
| 4587 | MD_DEFAULT_MAX_CORRECTED_READ_ERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4588 | mddev->safemode = 0; |
| 4589 | mddev->safemode_timer.function = md_safemode_timeout; |
| 4590 | mddev->safemode_timer.data = (unsigned long) mddev; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4591 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4592 | mddev->in_sync = 1; |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4593 | smp_wmb(); |
| 4594 | mddev->ready = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4595 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4596 | if (rdev->raid_disk >= 0) { |
| 4597 | char nm[20]; |
| 4598 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4599 | if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4600 | /* failure here is OK */; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4601 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4602 | |
| 4603 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4604 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4605 | if (mddev->flags) |
| 4606 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4607 | |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 4608 | md_wakeup_thread(mddev->thread); |
| 4609 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4610 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4611 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4612 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
| 4613 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4614 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4615 | return 0; |
| 4616 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4617 | EXPORT_SYMBOL_GPL(md_run); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4618 | |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 4619 | static int do_md_run(mddev_t *mddev) |
| 4620 | { |
| 4621 | int err; |
| 4622 | |
| 4623 | err = md_run(mddev); |
| 4624 | if (err) |
| 4625 | goto out; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 4626 | err = bitmap_load(mddev); |
| 4627 | if (err) { |
| 4628 | bitmap_destroy(mddev); |
| 4629 | goto out; |
| 4630 | } |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 4631 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 4632 | revalidate_disk(mddev->gendisk); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 4633 | mddev->changed = 1; |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 4634 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
| 4635 | out: |
| 4636 | return err; |
| 4637 | } |
| 4638 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4639 | static int restart_array(mddev_t *mddev) |
| 4640 | { |
| 4641 | struct gendisk *disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4642 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4643 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4644 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4645 | return -ENXIO; |
| 4646 | if (!mddev->pers) |
| 4647 | return -EINVAL; |
| 4648 | if (!mddev->ro) |
| 4649 | return -EBUSY; |
| 4650 | mddev->safemode = 0; |
| 4651 | mddev->ro = 0; |
| 4652 | set_disk_ro(disk, 0); |
| 4653 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 4654 | mdname(mddev)); |
| 4655 | /* Kick recovery or resync if necessary */ |
| 4656 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4657 | md_wakeup_thread(mddev->thread); |
| 4658 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4659 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4660 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4661 | } |
| 4662 | |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4663 | /* similar to deny_write_access, but accounts for our holding a reference |
| 4664 | * to the file ourselves */ |
| 4665 | static int deny_bitmap_write_access(struct file * file) |
| 4666 | { |
| 4667 | struct inode *inode = file->f_mapping->host; |
| 4668 | |
| 4669 | spin_lock(&inode->i_lock); |
| 4670 | if (atomic_read(&inode->i_writecount) > 1) { |
| 4671 | spin_unlock(&inode->i_lock); |
| 4672 | return -ETXTBSY; |
| 4673 | } |
| 4674 | atomic_set(&inode->i_writecount, -1); |
| 4675 | spin_unlock(&inode->i_lock); |
| 4676 | |
| 4677 | return 0; |
| 4678 | } |
| 4679 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4680 | void restore_bitmap_write_access(struct file *file) |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4681 | { |
| 4682 | struct inode *inode = file->f_mapping->host; |
| 4683 | |
| 4684 | spin_lock(&inode->i_lock); |
| 4685 | atomic_set(&inode->i_writecount, 1); |
| 4686 | spin_unlock(&inode->i_lock); |
| 4687 | } |
| 4688 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4689 | static void md_clean(mddev_t *mddev) |
| 4690 | { |
| 4691 | mddev->array_sectors = 0; |
| 4692 | mddev->external_size = 0; |
| 4693 | mddev->dev_sectors = 0; |
| 4694 | mddev->raid_disks = 0; |
| 4695 | mddev->recovery_cp = 0; |
| 4696 | mddev->resync_min = 0; |
| 4697 | mddev->resync_max = MaxSector; |
| 4698 | mddev->reshape_position = MaxSector; |
| 4699 | mddev->external = 0; |
| 4700 | mddev->persistent = 0; |
| 4701 | mddev->level = LEVEL_NONE; |
| 4702 | mddev->clevel[0] = 0; |
| 4703 | mddev->flags = 0; |
| 4704 | mddev->ro = 0; |
| 4705 | mddev->metadata_type[0] = 0; |
| 4706 | mddev->chunk_sectors = 0; |
| 4707 | mddev->ctime = mddev->utime = 0; |
| 4708 | mddev->layout = 0; |
| 4709 | mddev->max_disks = 0; |
| 4710 | mddev->events = 0; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 4711 | mddev->can_decrease_events = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4712 | mddev->delta_disks = 0; |
| 4713 | mddev->new_level = LEVEL_NONE; |
| 4714 | mddev->new_layout = 0; |
| 4715 | mddev->new_chunk_sectors = 0; |
| 4716 | mddev->curr_resync = 0; |
| 4717 | mddev->resync_mismatches = 0; |
| 4718 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 4719 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 4720 | mddev->recovery = 0; |
| 4721 | mddev->in_sync = 0; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 4722 | mddev->changed = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4723 | mddev->degraded = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4724 | mddev->safemode = 0; |
| 4725 | mddev->bitmap_info.offset = 0; |
| 4726 | mddev->bitmap_info.default_offset = 0; |
| 4727 | mddev->bitmap_info.chunksize = 0; |
| 4728 | mddev->bitmap_info.daemon_sleep = 0; |
| 4729 | mddev->bitmap_info.max_write_behind = 0; |
NeilBrown | 252ac52 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 4730 | mddev->plug = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4731 | } |
| 4732 | |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4733 | static void __md_stop_writes(mddev_t *mddev) |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 4734 | { |
| 4735 | if (mddev->sync_thread) { |
| 4736 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4737 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4738 | reap_sync_thread(mddev); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 4739 | } |
| 4740 | |
| 4741 | del_timer_sync(&mddev->safemode_timer); |
| 4742 | |
| 4743 | bitmap_flush(mddev); |
| 4744 | md_super_wait(mddev); |
| 4745 | |
| 4746 | if (!mddev->in_sync || mddev->flags) { |
| 4747 | /* mark array as shutdown cleanly */ |
| 4748 | mddev->in_sync = 1; |
| 4749 | md_update_sb(mddev, 1); |
| 4750 | } |
| 4751 | } |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4752 | |
| 4753 | void md_stop_writes(mddev_t *mddev) |
| 4754 | { |
| 4755 | mddev_lock(mddev); |
| 4756 | __md_stop_writes(mddev); |
| 4757 | mddev_unlock(mddev); |
| 4758 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4759 | EXPORT_SYMBOL_GPL(md_stop_writes); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 4760 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4761 | void md_stop(mddev_t *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4762 | { |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4763 | mddev->ready = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4764 | mddev->pers->stop(mddev); |
| 4765 | if (mddev->pers->sync_request && mddev->to_remove == NULL) |
| 4766 | mddev->to_remove = &md_redundancy_group; |
| 4767 | module_put(mddev->pers->owner); |
| 4768 | mddev->pers = NULL; |
NeilBrown | cca9cf9 | 2010-04-01 12:08:16 +1100 | [diff] [blame] | 4769 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4770 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4771 | EXPORT_SYMBOL_GPL(md_stop); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4772 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4773 | static int md_set_readonly(mddev_t *mddev, int is_open) |
| 4774 | { |
| 4775 | int err = 0; |
| 4776 | mutex_lock(&mddev->open_mutex); |
| 4777 | if (atomic_read(&mddev->openers) > is_open) { |
| 4778 | printk("md: %s still in use.\n",mdname(mddev)); |
| 4779 | err = -EBUSY; |
| 4780 | goto out; |
| 4781 | } |
| 4782 | if (mddev->pers) { |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4783 | __md_stop_writes(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4784 | |
| 4785 | err = -ENXIO; |
| 4786 | if (mddev->ro==1) |
| 4787 | goto out; |
| 4788 | mddev->ro = 1; |
| 4789 | set_disk_ro(mddev->gendisk, 1); |
| 4790 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4791 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4792 | err = 0; |
| 4793 | } |
| 4794 | out: |
| 4795 | mutex_unlock(&mddev->open_mutex); |
| 4796 | return err; |
| 4797 | } |
| 4798 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4799 | /* mode: |
| 4800 | * 0 - completely stop and dis-assemble array |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4801 | * 2 - stop but do not disassemble array |
| 4802 | */ |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4803 | 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] | 4804 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4805 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 4806 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4807 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 4808 | mutex_lock(&mddev->open_mutex); |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 4809 | if (atomic_read(&mddev->openers) > is_open || |
| 4810 | mddev->sysfs_active) { |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4811 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 4812 | mutex_unlock(&mddev->open_mutex); |
| 4813 | return -EBUSY; |
| 4814 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4815 | |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 4816 | if (mddev->pers) { |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4817 | if (mddev->ro) |
| 4818 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4819 | |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4820 | __md_stop_writes(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4821 | md_stop(mddev); |
| 4822 | mddev->queue->merge_bvec_fn = NULL; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4823 | mddev->queue->backing_dev_info.congested_fn = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4824 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4825 | /* tell userspace to handle 'inactive' */ |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4826 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4827 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4828 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4829 | if (rdev->raid_disk >= 0) { |
| 4830 | char nm[20]; |
| 4831 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 4832 | sysfs_remove_link(&mddev->kobj, nm); |
| 4833 | } |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 4834 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4835 | set_capacity(disk, 0); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 4836 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 4837 | mddev->changed = 1; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4838 | revalidate_disk(disk); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4839 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4840 | if (mddev->ro) |
| 4841 | mddev->ro = 0; |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 4842 | } else |
| 4843 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4844 | /* |
| 4845 | * Free resources if final stop |
| 4846 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4847 | if (mode == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4848 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 4849 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4850 | bitmap_destroy(mddev); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4851 | if (mddev->bitmap_info.file) { |
| 4852 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 4853 | fput(mddev->bitmap_info.file); |
| 4854 | mddev->bitmap_info.file = NULL; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4855 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4856 | mddev->bitmap_info.offset = 0; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4857 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4858 | export_array(mddev); |
| 4859 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4860 | md_clean(mddev); |
NeilBrown | 934d9c2 | 2008-10-28 17:01:23 +1100 | [diff] [blame] | 4861 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4862 | if (mddev->hold_active == UNTIL_STOP) |
| 4863 | mddev->hold_active = 0; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4864 | } |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 4865 | blk_integrity_unregister(disk); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4866 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4867 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 4868 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4869 | } |
| 4870 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4871 | #ifndef MODULE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4872 | static void autorun_array(mddev_t *mddev) |
| 4873 | { |
| 4874 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4875 | int err; |
| 4876 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4877 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4878 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4879 | |
| 4880 | printk(KERN_INFO "md: running: "); |
| 4881 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4882 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4883 | char b[BDEVNAME_SIZE]; |
| 4884 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 4885 | } |
| 4886 | printk("\n"); |
| 4887 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4888 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4889 | if (err) { |
| 4890 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4891 | do_md_stop(mddev, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4892 | } |
| 4893 | } |
| 4894 | |
| 4895 | /* |
| 4896 | * lets try to run arrays based on all disks that have arrived |
| 4897 | * until now. (those are in pending_raid_disks) |
| 4898 | * |
| 4899 | * the method: pick the first pending disk, collect all disks with |
| 4900 | * the same UUID, remove all from the pending list and put them into |
| 4901 | * the 'same_array' list. Then order this list based on superblock |
| 4902 | * update time (freshest comes first), kick out 'old' disks and |
| 4903 | * compare superblocks. If everything's fine then run it. |
| 4904 | * |
| 4905 | * If "unit" is allocated, then bump its reference count |
| 4906 | */ |
| 4907 | static void autorun_devices(int part) |
| 4908 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4909 | mdk_rdev_t *rdev0, *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4910 | mddev_t *mddev; |
| 4911 | char b[BDEVNAME_SIZE]; |
| 4912 | |
| 4913 | printk(KERN_INFO "md: autorun ...\n"); |
| 4914 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4915 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4916 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 4917 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4918 | rdev0 = list_entry(pending_raid_disks.next, |
| 4919 | mdk_rdev_t, same_set); |
| 4920 | |
| 4921 | printk(KERN_INFO "md: considering %s ...\n", |
| 4922 | bdevname(rdev0->bdev,b)); |
| 4923 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4924 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4925 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 4926 | printk(KERN_INFO "md: adding %s ...\n", |
| 4927 | bdevname(rdev->bdev,b)); |
| 4928 | list_move(&rdev->same_set, &candidates); |
| 4929 | } |
| 4930 | /* |
| 4931 | * now we have a set of devices, with all of them having |
| 4932 | * mostly sane superblocks. It's time to allocate the |
| 4933 | * mddev. |
| 4934 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4935 | if (part) { |
| 4936 | dev = MKDEV(mdp_major, |
| 4937 | rdev0->preferred_minor << MdpMinorShift); |
| 4938 | unit = MINOR(dev) >> MdpMinorShift; |
| 4939 | } else { |
| 4940 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 4941 | unit = MINOR(dev); |
| 4942 | } |
| 4943 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4944 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 4945 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 4946 | break; |
| 4947 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4948 | |
| 4949 | md_probe(dev, NULL, NULL); |
| 4950 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 4951 | if (!mddev || !mddev->gendisk) { |
| 4952 | if (mddev) |
| 4953 | mddev_put(mddev); |
| 4954 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4955 | "md: cannot allocate memory for md drive.\n"); |
| 4956 | break; |
| 4957 | } |
| 4958 | if (mddev_lock(mddev)) |
| 4959 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 4960 | mdname(mddev)); |
| 4961 | else if (mddev->raid_disks || mddev->major_version |
| 4962 | || !list_empty(&mddev->disks)) { |
| 4963 | printk(KERN_WARNING |
| 4964 | "md: %s already running, cannot run %s\n", |
| 4965 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 4966 | mddev_unlock(mddev); |
| 4967 | } else { |
| 4968 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4969 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4970 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4971 | list_del_init(&rdev->same_set); |
| 4972 | if (bind_rdev_to_array(rdev, mddev)) |
| 4973 | export_rdev(rdev); |
| 4974 | } |
| 4975 | autorun_array(mddev); |
| 4976 | mddev_unlock(mddev); |
| 4977 | } |
| 4978 | /* on success, candidates will be empty, on error |
| 4979 | * it won't... |
| 4980 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4981 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4982 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4983 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4984 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4985 | mddev_put(mddev); |
| 4986 | } |
| 4987 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 4988 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4989 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4990 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4991 | static int get_version(void __user * arg) |
| 4992 | { |
| 4993 | mdu_version_t ver; |
| 4994 | |
| 4995 | ver.major = MD_MAJOR_VERSION; |
| 4996 | ver.minor = MD_MINOR_VERSION; |
| 4997 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 4998 | |
| 4999 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 5000 | return -EFAULT; |
| 5001 | |
| 5002 | return 0; |
| 5003 | } |
| 5004 | |
| 5005 | static int get_array_info(mddev_t * mddev, void __user * arg) |
| 5006 | { |
| 5007 | mdu_array_info_t info; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5008 | int nr,working,insync,failed,spare; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5009 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5010 | |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5011 | nr=working=insync=failed=spare=0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5012 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5013 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5014 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5015 | failed++; |
| 5016 | else { |
| 5017 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5018 | if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5019 | insync++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5020 | else |
| 5021 | spare++; |
| 5022 | } |
| 5023 | } |
| 5024 | |
| 5025 | info.major_version = mddev->major_version; |
| 5026 | info.minor_version = mddev->minor_version; |
| 5027 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 5028 | info.ctime = mddev->ctime; |
| 5029 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5030 | info.size = mddev->dev_sectors / 2; |
| 5031 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 5032 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5033 | info.nr_disks = nr; |
| 5034 | info.raid_disks = mddev->raid_disks; |
| 5035 | info.md_minor = mddev->md_minor; |
| 5036 | info.not_persistent= !mddev->persistent; |
| 5037 | |
| 5038 | info.utime = mddev->utime; |
| 5039 | info.state = 0; |
| 5040 | if (mddev->in_sync) |
| 5041 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5042 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5043 | info.state = (1<<MD_SB_BITMAP_PRESENT); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5044 | info.active_disks = insync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5045 | info.working_disks = working; |
| 5046 | info.failed_disks = failed; |
| 5047 | info.spare_disks = spare; |
| 5048 | |
| 5049 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5050 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5051 | |
| 5052 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5053 | return -EFAULT; |
| 5054 | |
| 5055 | return 0; |
| 5056 | } |
| 5057 | |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 5058 | static int get_bitmap_file(mddev_t * mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5059 | { |
| 5060 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
| 5061 | char *ptr, *buf = NULL; |
| 5062 | int err = -ENOMEM; |
| 5063 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5064 | if (md_allow_write(mddev)) |
| 5065 | file = kmalloc(sizeof(*file), GFP_NOIO); |
| 5066 | else |
| 5067 | file = kmalloc(sizeof(*file), GFP_KERNEL); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5068 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5069 | if (!file) |
| 5070 | goto out; |
| 5071 | |
| 5072 | /* bitmap disabled, zero the first byte and copy out */ |
| 5073 | if (!mddev->bitmap || !mddev->bitmap->file) { |
| 5074 | file->pathname[0] = '\0'; |
| 5075 | goto copy_out; |
| 5076 | } |
| 5077 | |
| 5078 | buf = kmalloc(sizeof(file->pathname), GFP_KERNEL); |
| 5079 | if (!buf) |
| 5080 | goto out; |
| 5081 | |
Christoph Hellwig | 6bcfd60 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 5082 | ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname)); |
| 5083 | if (IS_ERR(ptr)) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5084 | goto out; |
| 5085 | |
| 5086 | strcpy(file->pathname, ptr); |
| 5087 | |
| 5088 | copy_out: |
| 5089 | err = 0; |
| 5090 | if (copy_to_user(arg, file, sizeof(*file))) |
| 5091 | err = -EFAULT; |
| 5092 | out: |
| 5093 | kfree(buf); |
| 5094 | kfree(file); |
| 5095 | return err; |
| 5096 | } |
| 5097 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5098 | static int get_disk_info(mddev_t * mddev, void __user * arg) |
| 5099 | { |
| 5100 | mdu_disk_info_t info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5101 | mdk_rdev_t *rdev; |
| 5102 | |
| 5103 | if (copy_from_user(&info, arg, sizeof(info))) |
| 5104 | return -EFAULT; |
| 5105 | |
Andre Noll | 26ef379 | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5106 | rdev = find_rdev_nr(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5107 | if (rdev) { |
| 5108 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 5109 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 5110 | info.raid_disk = rdev->raid_disk; |
| 5111 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5112 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5113 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5114 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5115 | info.state |= (1<<MD_DISK_ACTIVE); |
| 5116 | info.state |= (1<<MD_DISK_SYNC); |
| 5117 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5118 | if (test_bit(WriteMostly, &rdev->flags)) |
| 5119 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5120 | } else { |
| 5121 | info.major = info.minor = 0; |
| 5122 | info.raid_disk = -1; |
| 5123 | info.state = (1<<MD_DISK_REMOVED); |
| 5124 | } |
| 5125 | |
| 5126 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5127 | return -EFAULT; |
| 5128 | |
| 5129 | return 0; |
| 5130 | } |
| 5131 | |
| 5132 | static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info) |
| 5133 | { |
| 5134 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 5135 | mdk_rdev_t *rdev; |
| 5136 | dev_t dev = MKDEV(info->major,info->minor); |
| 5137 | |
| 5138 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 5139 | return -EOVERFLOW; |
| 5140 | |
| 5141 | if (!mddev->raid_disks) { |
| 5142 | int err; |
| 5143 | /* expecting a device which has a superblock */ |
| 5144 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 5145 | if (IS_ERR(rdev)) { |
| 5146 | printk(KERN_WARNING |
| 5147 | "md: md_import_device returned %ld\n", |
| 5148 | PTR_ERR(rdev)); |
| 5149 | return PTR_ERR(rdev); |
| 5150 | } |
| 5151 | if (!list_empty(&mddev->disks)) { |
| 5152 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 5153 | mdk_rdev_t, same_set); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5154 | err = super_types[mddev->major_version] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5155 | .load_super(rdev, rdev0, mddev->minor_version); |
| 5156 | if (err < 0) { |
| 5157 | printk(KERN_WARNING |
| 5158 | "md: %s has different UUID to %s\n", |
| 5159 | bdevname(rdev->bdev,b), |
| 5160 | bdevname(rdev0->bdev,b2)); |
| 5161 | export_rdev(rdev); |
| 5162 | return -EINVAL; |
| 5163 | } |
| 5164 | } |
| 5165 | err = bind_rdev_to_array(rdev, mddev); |
| 5166 | if (err) |
| 5167 | export_rdev(rdev); |
| 5168 | return err; |
| 5169 | } |
| 5170 | |
| 5171 | /* |
| 5172 | * add_new_disk can be used once the array is assembled |
| 5173 | * to add "hot spares". They must already have a superblock |
| 5174 | * written |
| 5175 | */ |
| 5176 | if (mddev->pers) { |
| 5177 | int err; |
| 5178 | if (!mddev->pers->hot_add_disk) { |
| 5179 | printk(KERN_WARNING |
| 5180 | "%s: personality does not support diskops!\n", |
| 5181 | mdname(mddev)); |
| 5182 | return -EINVAL; |
| 5183 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 5184 | if (mddev->persistent) |
| 5185 | rdev = md_import_device(dev, mddev->major_version, |
| 5186 | mddev->minor_version); |
| 5187 | else |
| 5188 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5189 | if (IS_ERR(rdev)) { |
| 5190 | printk(KERN_WARNING |
| 5191 | "md: md_import_device returned %ld\n", |
| 5192 | PTR_ERR(rdev)); |
| 5193 | return PTR_ERR(rdev); |
| 5194 | } |
NeilBrown | 1a855a0 | 2010-12-09 16:36:28 +1100 | [diff] [blame] | 5195 | /* set saved_raid_disk if appropriate */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5196 | if (!mddev->persistent) { |
| 5197 | if (info->state & (1<<MD_DISK_SYNC) && |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 5198 | info->raid_disk < mddev->raid_disks) { |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5199 | rdev->raid_disk = info->raid_disk; |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 5200 | set_bit(In_sync, &rdev->flags); |
| 5201 | } else |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5202 | rdev->raid_disk = -1; |
| 5203 | } else |
| 5204 | super_types[mddev->major_version]. |
| 5205 | validate_super(mddev, rdev); |
NeilBrown | 1a855a0 | 2010-12-09 16:36:28 +1100 | [diff] [blame] | 5206 | if (test_bit(In_sync, &rdev->flags)) |
| 5207 | rdev->saved_raid_disk = rdev->raid_disk; |
| 5208 | else |
| 5209 | rdev->saved_raid_disk = -1; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5210 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5211 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5212 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5213 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5214 | else |
| 5215 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5216 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5217 | rdev->raid_disk = -1; |
| 5218 | err = bind_rdev_to_array(rdev, mddev); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 5219 | if (!err && !mddev->pers->hot_remove_disk) { |
| 5220 | /* If there is hot_add_disk but no hot_remove_disk |
| 5221 | * then added disks for geometry changes, |
| 5222 | * and should be added immediately. |
| 5223 | */ |
| 5224 | super_types[mddev->major_version]. |
| 5225 | validate_super(mddev, rdev); |
| 5226 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 5227 | if (err) |
| 5228 | unbind_rdev_from_array(rdev); |
| 5229 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5230 | if (err) |
| 5231 | export_rdev(rdev); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 5232 | else |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5233 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5234 | |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 5235 | md_update_sb(mddev, 1); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 5236 | if (mddev->degraded) |
| 5237 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5238 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 5239 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5240 | return err; |
| 5241 | } |
| 5242 | |
| 5243 | /* otherwise, add_new_disk is only allowed |
| 5244 | * for major_version==0 superblocks |
| 5245 | */ |
| 5246 | if (mddev->major_version != 0) { |
| 5247 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 5248 | mdname(mddev)); |
| 5249 | return -EINVAL; |
| 5250 | } |
| 5251 | |
| 5252 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 5253 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5254 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5255 | if (IS_ERR(rdev)) { |
| 5256 | printk(KERN_WARNING |
| 5257 | "md: error, md_import_device() returned %ld\n", |
| 5258 | PTR_ERR(rdev)); |
| 5259 | return PTR_ERR(rdev); |
| 5260 | } |
| 5261 | rdev->desc_nr = info->number; |
| 5262 | if (info->raid_disk < mddev->raid_disks) |
| 5263 | rdev->raid_disk = info->raid_disk; |
| 5264 | else |
| 5265 | rdev->raid_disk = -1; |
| 5266 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5267 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5268 | if (info->state & (1<<MD_DISK_SYNC)) |
| 5269 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5270 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5271 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5272 | set_bit(WriteMostly, &rdev->flags); |
| 5273 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5274 | if (!mddev->persistent) { |
| 5275 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 5276 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
| 5277 | } else |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5278 | rdev->sb_start = calc_dev_sboffset(rdev); |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5279 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5280 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5281 | err = bind_rdev_to_array(rdev, mddev); |
| 5282 | if (err) { |
| 5283 | export_rdev(rdev); |
| 5284 | return err; |
| 5285 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5286 | } |
| 5287 | |
| 5288 | return 0; |
| 5289 | } |
| 5290 | |
| 5291 | static int hot_remove_disk(mddev_t * mddev, dev_t dev) |
| 5292 | { |
| 5293 | char b[BDEVNAME_SIZE]; |
| 5294 | mdk_rdev_t *rdev; |
| 5295 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5296 | rdev = find_rdev(mddev, dev); |
| 5297 | if (!rdev) |
| 5298 | return -ENXIO; |
| 5299 | |
| 5300 | if (rdev->raid_disk >= 0) |
| 5301 | goto busy; |
| 5302 | |
| 5303 | kick_rdev_from_array(rdev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5304 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5305 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5306 | |
| 5307 | return 0; |
| 5308 | busy: |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 5309 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5310 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5311 | return -EBUSY; |
| 5312 | } |
| 5313 | |
| 5314 | static int hot_add_disk(mddev_t * mddev, dev_t dev) |
| 5315 | { |
| 5316 | char b[BDEVNAME_SIZE]; |
| 5317 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5318 | mdk_rdev_t *rdev; |
| 5319 | |
| 5320 | if (!mddev->pers) |
| 5321 | return -ENODEV; |
| 5322 | |
| 5323 | if (mddev->major_version != 0) { |
| 5324 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 5325 | " version-0 superblocks.\n", |
| 5326 | mdname(mddev)); |
| 5327 | return -EINVAL; |
| 5328 | } |
| 5329 | if (!mddev->pers->hot_add_disk) { |
| 5330 | printk(KERN_WARNING |
| 5331 | "%s: personality does not support diskops!\n", |
| 5332 | mdname(mddev)); |
| 5333 | return -EINVAL; |
| 5334 | } |
| 5335 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5336 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5337 | if (IS_ERR(rdev)) { |
| 5338 | printk(KERN_WARNING |
| 5339 | "md: error, md_import_device() returned %ld\n", |
| 5340 | PTR_ERR(rdev)); |
| 5341 | return -EINVAL; |
| 5342 | } |
| 5343 | |
| 5344 | if (mddev->persistent) |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5345 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5346 | else |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 5347 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5348 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5349 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5350 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5351 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5352 | printk(KERN_WARNING |
| 5353 | "md: can not hot-add faulty %s disk to %s!\n", |
| 5354 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5355 | err = -EINVAL; |
| 5356 | goto abort_export; |
| 5357 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5358 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5359 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 5360 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5361 | err = bind_rdev_to_array(rdev, mddev); |
| 5362 | if (err) |
| 5363 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5364 | |
| 5365 | /* |
| 5366 | * The rest should better be atomic, we can have disk failures |
| 5367 | * noticed in interrupt contexts ... |
| 5368 | */ |
| 5369 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5370 | rdev->raid_disk = -1; |
| 5371 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5372 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5373 | |
| 5374 | /* |
| 5375 | * Kick recovery, maybe this spare has to be added to the |
| 5376 | * array immediately. |
| 5377 | */ |
| 5378 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5379 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5380 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5381 | return 0; |
| 5382 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5383 | abort_export: |
| 5384 | export_rdev(rdev); |
| 5385 | return err; |
| 5386 | } |
| 5387 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5388 | static int set_bitmap_file(mddev_t *mddev, int fd) |
| 5389 | { |
| 5390 | int err; |
| 5391 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5392 | if (mddev->pers) { |
| 5393 | if (!mddev->pers->quiesce) |
| 5394 | return -EBUSY; |
| 5395 | if (mddev->recovery || mddev->sync_thread) |
| 5396 | return -EBUSY; |
| 5397 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5398 | } |
| 5399 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5400 | |
| 5401 | if (fd >= 0) { |
| 5402 | if (mddev->bitmap) |
| 5403 | return -EEXIST; /* cannot add when bitmap is present */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5404 | mddev->bitmap_info.file = fget(fd); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5405 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5406 | if (mddev->bitmap_info.file == NULL) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5407 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 5408 | mdname(mddev)); |
| 5409 | return -EBADF; |
| 5410 | } |
| 5411 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5412 | err = deny_bitmap_write_access(mddev->bitmap_info.file); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5413 | if (err) { |
| 5414 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 5415 | mdname(mddev)); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5416 | fput(mddev->bitmap_info.file); |
| 5417 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5418 | return err; |
| 5419 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5420 | mddev->bitmap_info.offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5421 | } else if (mddev->bitmap == NULL) |
| 5422 | return -ENOENT; /* cannot remove what isn't there */ |
| 5423 | err = 0; |
| 5424 | if (mddev->pers) { |
| 5425 | mddev->pers->quiesce(mddev, 1); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5426 | if (fd >= 0) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5427 | err = bitmap_create(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5428 | if (!err) |
| 5429 | err = bitmap_load(mddev); |
| 5430 | } |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5431 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5432 | bitmap_destroy(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5433 | fd = -1; /* make sure to put the file */ |
| 5434 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5435 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5436 | } |
| 5437 | if (fd < 0) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5438 | if (mddev->bitmap_info.file) { |
| 5439 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 5440 | fput(mddev->bitmap_info.file); |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 5441 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5442 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5443 | } |
| 5444 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5445 | return err; |
| 5446 | } |
| 5447 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5448 | /* |
| 5449 | * set_array_info is used two different ways |
| 5450 | * The original usage is when creating a new array. |
| 5451 | * In this usage, raid_disks is > 0 and it together with |
| 5452 | * level, size, not_persistent,layout,chunksize determine the |
| 5453 | * shape of the array. |
| 5454 | * This will always create an array with a type-0.90.0 superblock. |
| 5455 | * The newer usage is when assembling an array. |
| 5456 | * In this case raid_disks will be 0, and the major_version field is |
| 5457 | * use to determine which style super-blocks are to be found on the devices. |
| 5458 | * The minor and patch _version numbers are also kept incase the |
| 5459 | * super_block handler wishes to interpret them. |
| 5460 | */ |
| 5461 | static int set_array_info(mddev_t * mddev, mdu_array_info_t *info) |
| 5462 | { |
| 5463 | |
| 5464 | if (info->raid_disks == 0) { |
| 5465 | /* just setting version number for superblock loading */ |
| 5466 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 5467 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5468 | super_types[info->major_version].name == NULL) { |
| 5469 | /* maybe try to auto-load a module? */ |
| 5470 | printk(KERN_INFO |
| 5471 | "md: superblock version %d not known\n", |
| 5472 | info->major_version); |
| 5473 | return -EINVAL; |
| 5474 | } |
| 5475 | mddev->major_version = info->major_version; |
| 5476 | mddev->minor_version = info->minor_version; |
| 5477 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5478 | mddev->persistent = !info->not_persistent; |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 5479 | /* ensure mddev_put doesn't delete this now that there |
| 5480 | * is some minimal configuration. |
| 5481 | */ |
| 5482 | mddev->ctime = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5483 | return 0; |
| 5484 | } |
| 5485 | mddev->major_version = MD_MAJOR_VERSION; |
| 5486 | mddev->minor_version = MD_MINOR_VERSION; |
| 5487 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 5488 | mddev->ctime = get_seconds(); |
| 5489 | |
| 5490 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 5491 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5492 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5493 | mddev->raid_disks = info->raid_disks; |
| 5494 | /* don't set md_minor, it is determined by which /dev/md* was |
| 5495 | * openned |
| 5496 | */ |
| 5497 | if (info->state & (1<<MD_SB_CLEAN)) |
| 5498 | mddev->recovery_cp = MaxSector; |
| 5499 | else |
| 5500 | mddev->recovery_cp = 0; |
| 5501 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5502 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5503 | |
| 5504 | mddev->layout = info->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5505 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5506 | |
| 5507 | mddev->max_disks = MD_SB_DISKS; |
| 5508 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5509 | if (mddev->persistent) |
| 5510 | mddev->flags = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5511 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5512 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5513 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
| 5514 | mddev->bitmap_info.offset = 0; |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 5515 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5516 | mddev->reshape_position = MaxSector; |
| 5517 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5518 | /* |
| 5519 | * Generate a 128 bit UUID |
| 5520 | */ |
| 5521 | get_random_bytes(mddev->uuid, 16); |
| 5522 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5523 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5524 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5525 | mddev->new_layout = mddev->layout; |
| 5526 | mddev->delta_disks = 0; |
| 5527 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5528 | return 0; |
| 5529 | } |
| 5530 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5531 | void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors) |
| 5532 | { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5533 | WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__); |
| 5534 | |
| 5535 | if (mddev->external_size) |
| 5536 | return; |
| 5537 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5538 | mddev->array_sectors = array_sectors; |
| 5539 | } |
| 5540 | EXPORT_SYMBOL(md_set_array_sectors); |
| 5541 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5542 | static int update_size(mddev_t *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5543 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5544 | mdk_rdev_t *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5545 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5546 | int fit = (num_sectors == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5547 | |
| 5548 | if (mddev->pers->resize == NULL) |
| 5549 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5550 | /* The "num_sectors" is the number of sectors of each device that |
| 5551 | * is used. This can only make sense for arrays with redundancy. |
| 5552 | * linear and raid0 always use whatever space is available. We can only |
| 5553 | * consider changing this number if no resync or reconstruction is |
| 5554 | * 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] | 5555 | * 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] | 5556 | * of each device. If num_sectors is zero, we find the largest size |
| 5557 | * that fits. |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5558 | */ |
| 5559 | if (mddev->sync_thread) |
| 5560 | return -EBUSY; |
NeilBrown | dba034e | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 5561 | if (mddev->bitmap) |
| 5562 | /* Sorry, cannot grow a bitmap yet, just remove it, |
| 5563 | * grow, and re-add. |
| 5564 | */ |
| 5565 | return -EBUSY; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5566 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5567 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 5568 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5569 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 5570 | num_sectors = avail; |
| 5571 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5572 | return -ENOSPC; |
| 5573 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5574 | rv = mddev->pers->resize(mddev, num_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5575 | if (!rv) |
| 5576 | revalidate_disk(mddev->gendisk); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5577 | return rv; |
| 5578 | } |
| 5579 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5580 | static int update_raid_disks(mddev_t *mddev, int raid_disks) |
| 5581 | { |
| 5582 | int rv; |
| 5583 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5584 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5585 | return -EINVAL; |
| 5586 | if (raid_disks <= 0 || |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 5587 | (mddev->max_disks && raid_disks >= mddev->max_disks)) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5588 | return -EINVAL; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5589 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5590 | return -EBUSY; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5591 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
| 5592 | |
| 5593 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | de171cb | 2011-01-31 11:57:42 +1100 | [diff] [blame] | 5594 | if (rv < 0) |
| 5595 | mddev->delta_disks = 0; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5596 | return rv; |
| 5597 | } |
| 5598 | |
| 5599 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5600 | /* |
| 5601 | * update_array_info is used to change the configuration of an |
| 5602 | * on-line array. |
| 5603 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 5604 | * fields in the info are checked against the array. |
| 5605 | * Any differences that cannot be handled will cause an error. |
| 5606 | * Normally, only one change can be managed at a time. |
| 5607 | */ |
| 5608 | static int update_array_info(mddev_t *mddev, mdu_array_info_t *info) |
| 5609 | { |
| 5610 | int rv = 0; |
| 5611 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5612 | int state = 0; |
| 5613 | |
| 5614 | /* calculate expected state,ignoring low bits */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5615 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5616 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5617 | |
| 5618 | if (mddev->major_version != info->major_version || |
| 5619 | mddev->minor_version != info->minor_version || |
| 5620 | /* mddev->patch_version != info->patch_version || */ |
| 5621 | mddev->ctime != info->ctime || |
| 5622 | mddev->level != info->level || |
| 5623 | /* mddev->layout != info->layout || */ |
| 5624 | !mddev->persistent != info->not_persistent|| |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5625 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5626 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 5627 | ((state^info->state) & 0xfffffe00) |
| 5628 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5629 | return -EINVAL; |
| 5630 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5631 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 5632 | cnt++; |
| 5633 | if (mddev->raid_disks != info->raid_disks) |
| 5634 | cnt++; |
| 5635 | if (mddev->layout != info->layout) |
| 5636 | cnt++; |
| 5637 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 5638 | cnt++; |
| 5639 | if (cnt == 0) |
| 5640 | return 0; |
| 5641 | if (cnt > 1) |
| 5642 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5643 | |
| 5644 | if (mddev->layout != info->layout) { |
| 5645 | /* Change layout |
| 5646 | * we don't need to do anything at the md level, the |
| 5647 | * personality will take care of it all. |
| 5648 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5649 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5650 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5651 | else { |
| 5652 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5653 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5654 | if (rv) |
| 5655 | mddev->new_layout = mddev->layout; |
| 5656 | return rv; |
| 5657 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5658 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5659 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5660 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5661 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5662 | if (mddev->raid_disks != info->raid_disks) |
| 5663 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5664 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5665 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5666 | if (mddev->pers->quiesce == NULL) |
| 5667 | return -EINVAL; |
| 5668 | if (mddev->recovery || mddev->sync_thread) |
| 5669 | return -EBUSY; |
| 5670 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5671 | /* add the bitmap */ |
| 5672 | if (mddev->bitmap) |
| 5673 | return -EEXIST; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5674 | if (mddev->bitmap_info.default_offset == 0) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5675 | return -EINVAL; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5676 | mddev->bitmap_info.offset = |
| 5677 | mddev->bitmap_info.default_offset; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5678 | mddev->pers->quiesce(mddev, 1); |
| 5679 | rv = bitmap_create(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5680 | if (!rv) |
| 5681 | rv = bitmap_load(mddev); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5682 | if (rv) |
| 5683 | bitmap_destroy(mddev); |
| 5684 | mddev->pers->quiesce(mddev, 0); |
| 5685 | } else { |
| 5686 | /* remove the bitmap */ |
| 5687 | if (!mddev->bitmap) |
| 5688 | return -ENOENT; |
| 5689 | if (mddev->bitmap->file) |
| 5690 | return -EINVAL; |
| 5691 | mddev->pers->quiesce(mddev, 1); |
| 5692 | bitmap_destroy(mddev); |
| 5693 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5694 | mddev->bitmap_info.offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5695 | } |
| 5696 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5697 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5698 | return rv; |
| 5699 | } |
| 5700 | |
| 5701 | static int set_disk_faulty(mddev_t *mddev, dev_t dev) |
| 5702 | { |
| 5703 | mdk_rdev_t *rdev; |
| 5704 | |
| 5705 | if (mddev->pers == NULL) |
| 5706 | return -ENODEV; |
| 5707 | |
| 5708 | rdev = find_rdev(mddev, dev); |
| 5709 | if (!rdev) |
| 5710 | return -ENODEV; |
| 5711 | |
| 5712 | md_error(mddev, rdev); |
| 5713 | return 0; |
| 5714 | } |
| 5715 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 5716 | /* |
| 5717 | * We have a problem here : there is no easy way to give a CHS |
| 5718 | * virtual geometry. We currently pretend that we have a 2 heads |
| 5719 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 5720 | * dosfs just mad... ;-) |
| 5721 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5722 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 5723 | { |
| 5724 | mddev_t *mddev = bdev->bd_disk->private_data; |
| 5725 | |
| 5726 | geo->heads = 2; |
| 5727 | geo->sectors = 4; |
NeilBrown | 49ce6ce | 2010-03-29 10:51:42 +1100 | [diff] [blame] | 5728 | geo->cylinders = mddev->array_sectors / 8; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5729 | return 0; |
| 5730 | } |
| 5731 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5732 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5733 | unsigned int cmd, unsigned long arg) |
| 5734 | { |
| 5735 | int err = 0; |
| 5736 | void __user *argp = (void __user *)arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5737 | mddev_t *mddev = NULL; |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 5738 | int ro; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5739 | |
| 5740 | if (!capable(CAP_SYS_ADMIN)) |
| 5741 | return -EACCES; |
| 5742 | |
| 5743 | /* |
| 5744 | * Commands dealing with the RAID driver but not any |
| 5745 | * particular array: |
| 5746 | */ |
| 5747 | switch (cmd) |
| 5748 | { |
| 5749 | case RAID_VERSION: |
| 5750 | err = get_version(argp); |
| 5751 | goto done; |
| 5752 | |
| 5753 | case PRINT_RAID_DEBUG: |
| 5754 | err = 0; |
| 5755 | md_print_devices(); |
| 5756 | goto done; |
| 5757 | |
| 5758 | #ifndef MODULE |
| 5759 | case RAID_AUTORUN: |
| 5760 | err = 0; |
| 5761 | autostart_arrays(arg); |
| 5762 | goto done; |
| 5763 | #endif |
| 5764 | default:; |
| 5765 | } |
| 5766 | |
| 5767 | /* |
| 5768 | * Commands creating/starting a new array: |
| 5769 | */ |
| 5770 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5771 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5772 | |
| 5773 | if (!mddev) { |
| 5774 | BUG(); |
| 5775 | goto abort; |
| 5776 | } |
| 5777 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5778 | err = mddev_lock(mddev); |
| 5779 | if (err) { |
| 5780 | printk(KERN_INFO |
| 5781 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 5782 | err, cmd); |
| 5783 | goto abort; |
| 5784 | } |
| 5785 | |
| 5786 | switch (cmd) |
| 5787 | { |
| 5788 | case SET_ARRAY_INFO: |
| 5789 | { |
| 5790 | mdu_array_info_t info; |
| 5791 | if (!arg) |
| 5792 | memset(&info, 0, sizeof(info)); |
| 5793 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 5794 | err = -EFAULT; |
| 5795 | goto abort_unlock; |
| 5796 | } |
| 5797 | if (mddev->pers) { |
| 5798 | err = update_array_info(mddev, &info); |
| 5799 | if (err) { |
| 5800 | printk(KERN_WARNING "md: couldn't update" |
| 5801 | " array info. %d\n", err); |
| 5802 | goto abort_unlock; |
| 5803 | } |
| 5804 | goto done_unlock; |
| 5805 | } |
| 5806 | if (!list_empty(&mddev->disks)) { |
| 5807 | printk(KERN_WARNING |
| 5808 | "md: array %s already has disks!\n", |
| 5809 | mdname(mddev)); |
| 5810 | err = -EBUSY; |
| 5811 | goto abort_unlock; |
| 5812 | } |
| 5813 | if (mddev->raid_disks) { |
| 5814 | printk(KERN_WARNING |
| 5815 | "md: array %s already initialised!\n", |
| 5816 | mdname(mddev)); |
| 5817 | err = -EBUSY; |
| 5818 | goto abort_unlock; |
| 5819 | } |
| 5820 | err = set_array_info(mddev, &info); |
| 5821 | if (err) { |
| 5822 | printk(KERN_WARNING "md: couldn't set" |
| 5823 | " array info. %d\n", err); |
| 5824 | goto abort_unlock; |
| 5825 | } |
| 5826 | } |
| 5827 | goto done_unlock; |
| 5828 | |
| 5829 | default:; |
| 5830 | } |
| 5831 | |
| 5832 | /* |
| 5833 | * Commands querying/configuring an existing array: |
| 5834 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5835 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5836 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 5837 | if ((!mddev->raid_disks && !mddev->external) |
| 5838 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 5839 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 5840 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5841 | err = -ENODEV; |
| 5842 | goto abort_unlock; |
| 5843 | } |
| 5844 | |
| 5845 | /* |
| 5846 | * Commands even a read-only array can execute: |
| 5847 | */ |
| 5848 | switch (cmd) |
| 5849 | { |
| 5850 | case GET_ARRAY_INFO: |
| 5851 | err = get_array_info(mddev, argp); |
| 5852 | goto done_unlock; |
| 5853 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5854 | case GET_BITMAP_FILE: |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 5855 | err = get_bitmap_file(mddev, argp); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5856 | goto done_unlock; |
| 5857 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5858 | case GET_DISK_INFO: |
| 5859 | err = get_disk_info(mddev, argp); |
| 5860 | goto done_unlock; |
| 5861 | |
| 5862 | case RESTART_ARRAY_RW: |
| 5863 | err = restart_array(mddev); |
| 5864 | goto done_unlock; |
| 5865 | |
| 5866 | case STOP_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5867 | err = do_md_stop(mddev, 0, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5868 | goto done_unlock; |
| 5869 | |
| 5870 | case STOP_ARRAY_RO: |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5871 | err = md_set_readonly(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5872 | goto done_unlock; |
| 5873 | |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 5874 | case BLKROSET: |
| 5875 | if (get_user(ro, (int __user *)(arg))) { |
| 5876 | err = -EFAULT; |
| 5877 | goto done_unlock; |
| 5878 | } |
| 5879 | err = -EINVAL; |
| 5880 | |
| 5881 | /* if the bdev is going readonly the value of mddev->ro |
| 5882 | * does not matter, no writes are coming |
| 5883 | */ |
| 5884 | if (ro) |
| 5885 | goto done_unlock; |
| 5886 | |
| 5887 | /* are we are already prepared for writes? */ |
| 5888 | if (mddev->ro != 1) |
| 5889 | goto done_unlock; |
| 5890 | |
| 5891 | /* transitioning to readauto need only happen for |
| 5892 | * arrays that call md_write_start |
| 5893 | */ |
| 5894 | if (mddev->pers) { |
| 5895 | err = restart_array(mddev); |
| 5896 | if (err == 0) { |
| 5897 | mddev->ro = 2; |
| 5898 | set_disk_ro(mddev->gendisk, 0); |
| 5899 | } |
| 5900 | } |
| 5901 | goto done_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5902 | } |
| 5903 | |
| 5904 | /* |
| 5905 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5906 | * superblock, so we do not allow them on read-only arrays. |
| 5907 | * However non-MD ioctls (e.g. get-size) will still come through |
| 5908 | * here and hit the 'default' below, so only disallow |
| 5909 | * 'md' ioctls, and switch to rw mode if started auto-readonly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5910 | */ |
Andre Noll | bb57fc6 | 2008-04-25 19:06:35 +0200 | [diff] [blame] | 5911 | if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5912 | if (mddev->ro == 2) { |
| 5913 | mddev->ro = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5914 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5915 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5916 | md_wakeup_thread(mddev->thread); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5917 | } else { |
| 5918 | err = -EROFS; |
| 5919 | goto abort_unlock; |
| 5920 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5921 | } |
| 5922 | |
| 5923 | switch (cmd) |
| 5924 | { |
| 5925 | case ADD_NEW_DISK: |
| 5926 | { |
| 5927 | mdu_disk_info_t info; |
| 5928 | if (copy_from_user(&info, argp, sizeof(info))) |
| 5929 | err = -EFAULT; |
| 5930 | else |
| 5931 | err = add_new_disk(mddev, &info); |
| 5932 | goto done_unlock; |
| 5933 | } |
| 5934 | |
| 5935 | case HOT_REMOVE_DISK: |
| 5936 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
| 5937 | goto done_unlock; |
| 5938 | |
| 5939 | case HOT_ADD_DISK: |
| 5940 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
| 5941 | goto done_unlock; |
| 5942 | |
| 5943 | case SET_DISK_FAULTY: |
| 5944 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
| 5945 | goto done_unlock; |
| 5946 | |
| 5947 | case RUN_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5948 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5949 | goto done_unlock; |
| 5950 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5951 | case SET_BITMAP_FILE: |
| 5952 | err = set_bitmap_file(mddev, (int)arg); |
| 5953 | goto done_unlock; |
| 5954 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5955 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5956 | err = -EINVAL; |
| 5957 | goto abort_unlock; |
| 5958 | } |
| 5959 | |
| 5960 | done_unlock: |
| 5961 | abort_unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5962 | if (mddev->hold_active == UNTIL_IOCTL && |
| 5963 | err != -EINVAL) |
| 5964 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5965 | mddev_unlock(mddev); |
| 5966 | |
| 5967 | return err; |
| 5968 | done: |
| 5969 | if (err) |
| 5970 | MD_BUG(); |
| 5971 | abort: |
| 5972 | return err; |
| 5973 | } |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 5974 | #ifdef CONFIG_COMPAT |
| 5975 | static int md_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 5976 | unsigned int cmd, unsigned long arg) |
| 5977 | { |
| 5978 | switch (cmd) { |
| 5979 | case HOT_REMOVE_DISK: |
| 5980 | case HOT_ADD_DISK: |
| 5981 | case SET_DISK_FAULTY: |
| 5982 | case SET_BITMAP_FILE: |
| 5983 | /* These take in integer arg, do not convert */ |
| 5984 | break; |
| 5985 | default: |
| 5986 | arg = (unsigned long)compat_ptr(arg); |
| 5987 | break; |
| 5988 | } |
| 5989 | |
| 5990 | return md_ioctl(bdev, mode, cmd, arg); |
| 5991 | } |
| 5992 | #endif /* CONFIG_COMPAT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5993 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5994 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5995 | { |
| 5996 | /* |
| 5997 | * Succeed if we can lock the mddev, which confirms that |
| 5998 | * it isn't being stopped right now. |
| 5999 | */ |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6000 | mddev_t *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6001 | int err; |
| 6002 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6003 | if (mddev->gendisk != bdev->bd_disk) { |
| 6004 | /* we are racing with mddev_put which is discarding this |
| 6005 | * bd_disk. |
| 6006 | */ |
| 6007 | mddev_put(mddev); |
| 6008 | /* Wait until bdev->bd_disk is definitely gone */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 6009 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6010 | /* Then retry the open from the top */ |
| 6011 | return -ERESTARTSYS; |
| 6012 | } |
| 6013 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 6014 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 6015 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6016 | goto out; |
| 6017 | |
| 6018 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6019 | atomic_inc(&mddev->openers); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 6020 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6021 | |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6022 | check_disk_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6023 | out: |
| 6024 | return err; |
| 6025 | } |
| 6026 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6027 | static int md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6028 | { |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6029 | mddev_t *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6030 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 6031 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6032 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6033 | mddev_put(mddev); |
| 6034 | |
| 6035 | return 0; |
| 6036 | } |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6037 | |
| 6038 | static int md_media_changed(struct gendisk *disk) |
| 6039 | { |
| 6040 | mddev_t *mddev = disk->private_data; |
| 6041 | |
| 6042 | return mddev->changed; |
| 6043 | } |
| 6044 | |
| 6045 | static int md_revalidate(struct gendisk *disk) |
| 6046 | { |
| 6047 | mddev_t *mddev = disk->private_data; |
| 6048 | |
| 6049 | mddev->changed = 0; |
| 6050 | return 0; |
| 6051 | } |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 6052 | static const struct block_device_operations md_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6053 | { |
| 6054 | .owner = THIS_MODULE, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6055 | .open = md_open, |
| 6056 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 6057 | .ioctl = md_ioctl, |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 6058 | #ifdef CONFIG_COMPAT |
| 6059 | .compat_ioctl = md_compat_ioctl, |
| 6060 | #endif |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6061 | .getgeo = md_getgeo, |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6062 | .media_changed = md_media_changed, |
| 6063 | .revalidate_disk= md_revalidate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6064 | }; |
| 6065 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6066 | static int md_thread(void * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6067 | { |
| 6068 | mdk_thread_t *thread = arg; |
| 6069 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6070 | /* |
| 6071 | * md_thread is a 'system-thread', it's priority should be very |
| 6072 | * high. We avoid resource deadlocks individually in each |
| 6073 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 6074 | * the very same RT priority as kswapd, thus we will never get |
| 6075 | * into a priority inversion deadlock. |
| 6076 | * |
| 6077 | * we definitely have to have equal or higher priority than |
| 6078 | * bdflush, otherwise bdflush will deadlock if there are too |
| 6079 | * many dirty RAID5 blocks. |
| 6080 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6081 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 6082 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6083 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6084 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 6085 | /* We need to wait INTERRUPTIBLE so that |
| 6086 | * we don't add to the load-average. |
| 6087 | * That means we need to be sure no signals are |
| 6088 | * pending |
| 6089 | */ |
| 6090 | if (signal_pending(current)) |
| 6091 | flush_signals(current); |
| 6092 | |
| 6093 | wait_event_interruptible_timeout |
| 6094 | (thread->wqueue, |
| 6095 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 6096 | || kthread_should_stop(), |
| 6097 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6098 | |
NeilBrown | 6c98791 | 2011-01-14 09:13:53 +1100 | [diff] [blame] | 6099 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 6100 | if (!kthread_should_stop()) |
NeilBrown | 589a594 | 2010-12-09 17:02:14 +1100 | [diff] [blame] | 6101 | thread->run(thread->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6102 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6103 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6104 | return 0; |
| 6105 | } |
| 6106 | |
| 6107 | void md_wakeup_thread(mdk_thread_t *thread) |
| 6108 | { |
| 6109 | if (thread) { |
| 6110 | dprintk("md: waking up MD thread %s.\n", thread->tsk->comm); |
| 6111 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 6112 | wake_up(&thread->wqueue); |
| 6113 | } |
| 6114 | } |
| 6115 | |
| 6116 | mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev, |
| 6117 | const char *name) |
| 6118 | { |
| 6119 | mdk_thread_t *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6120 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 6121 | thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6122 | if (!thread) |
| 6123 | return NULL; |
| 6124 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6125 | init_waitqueue_head(&thread->wqueue); |
| 6126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6127 | thread->run = run; |
| 6128 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6129 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 6130 | thread->tsk = kthread_run(md_thread, thread, |
| 6131 | "%s_%s", |
| 6132 | mdname(thread->mddev), |
| 6133 | name ?: mddev->pers->name); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6134 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6135 | kfree(thread); |
| 6136 | return NULL; |
| 6137 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6138 | return thread; |
| 6139 | } |
| 6140 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6141 | void md_unregister_thread(mdk_thread_t *thread) |
| 6142 | { |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6143 | if (!thread) |
| 6144 | return; |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 6145 | dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6146 | |
| 6147 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6148 | kfree(thread); |
| 6149 | } |
| 6150 | |
| 6151 | void md_error(mddev_t *mddev, mdk_rdev_t *rdev) |
| 6152 | { |
| 6153 | if (!mddev) { |
| 6154 | MD_BUG(); |
| 6155 | return; |
| 6156 | } |
| 6157 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6158 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6159 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6160 | |
| 6161 | if (mddev->external) |
| 6162 | set_bit(Blocked, &rdev->flags); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6163 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6164 | dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n", |
| 6165 | mdname(mddev), |
| 6166 | MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev), |
| 6167 | __builtin_return_address(0),__builtin_return_address(1), |
| 6168 | __builtin_return_address(2),__builtin_return_address(3)); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6169 | */ |
Andrew Morton | d0a0a5e | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 6170 | if (!mddev->pers) |
| 6171 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6172 | if (!mddev->pers->error_handler) |
| 6173 | return; |
| 6174 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6175 | if (mddev->degraded) |
| 6176 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6177 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6178 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6179 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6180 | md_wakeup_thread(mddev->thread); |
NeilBrown | 768a418 | 2010-07-26 11:49:55 +1000 | [diff] [blame] | 6181 | if (mddev->event_work.func) |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 6182 | queue_work(md_misc_wq, &mddev->event_work); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 6183 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6184 | } |
| 6185 | |
| 6186 | /* seq_file implementation /proc/mdstat */ |
| 6187 | |
| 6188 | static void status_unused(struct seq_file *seq) |
| 6189 | { |
| 6190 | int i = 0; |
| 6191 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6192 | |
| 6193 | seq_printf(seq, "unused devices: "); |
| 6194 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6195 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6196 | char b[BDEVNAME_SIZE]; |
| 6197 | i++; |
| 6198 | seq_printf(seq, "%s ", |
| 6199 | bdevname(rdev->bdev,b)); |
| 6200 | } |
| 6201 | if (!i) |
| 6202 | seq_printf(seq, "<none>"); |
| 6203 | |
| 6204 | seq_printf(seq, "\n"); |
| 6205 | } |
| 6206 | |
| 6207 | |
| 6208 | static void status_resync(struct seq_file *seq, mddev_t * mddev) |
| 6209 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6210 | sector_t max_sectors, resync, res; |
| 6211 | unsigned long dt, db; |
| 6212 | sector_t rt; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6213 | int scale; |
| 6214 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6215 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6216 | resync = mddev->curr_resync - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6217 | |
| 6218 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6219 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6220 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6221 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6222 | |
| 6223 | /* |
| 6224 | * Should not happen. |
| 6225 | */ |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6226 | if (!max_sectors) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6227 | MD_BUG(); |
| 6228 | return; |
| 6229 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6230 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6231 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6232 | * u32, as those are the requirements for sector_div. |
| 6233 | * Thus 'scale' must be at least 10 |
| 6234 | */ |
| 6235 | scale = 10; |
| 6236 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6237 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6238 | scale++; |
| 6239 | } |
| 6240 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6241 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6242 | |
| 6243 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6244 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6245 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6246 | seq_printf(seq, "["); |
| 6247 | for (i = 0; i < x; i++) |
| 6248 | seq_printf(seq, "="); |
| 6249 | seq_printf(seq, ">"); |
| 6250 | for (i = 0; i < y; i++) |
| 6251 | seq_printf(seq, "."); |
| 6252 | seq_printf(seq, "] "); |
| 6253 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6254 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6255 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 6256 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6257 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 6258 | "check" : |
| 6259 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 6260 | "resync" : "recovery"))), |
| 6261 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6262 | (unsigned long long) resync/2, |
| 6263 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6264 | |
| 6265 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6266 | * dt: time from mark until now |
| 6267 | * db: blocks written from mark until now |
| 6268 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6269 | * |
| 6270 | * rt is a sector_t, so could be 32bit or 64bit. |
| 6271 | * So we divide before multiply in case it is 32bit and close |
| 6272 | * to the limit. |
| 6273 | * We scale the divisor (db) by 32 to avoid loosing precision |
| 6274 | * near the end of resync when the number of remaining sectors |
| 6275 | * is close to 'db'. |
| 6276 | * We then divide rt by 32 after multiplying by db to compensate. |
| 6277 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6278 | */ |
| 6279 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 6280 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6281 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 6282 | - mddev->resync_mark_cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6283 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6284 | rt = max_sectors - resync; /* number of remaining sectors */ |
| 6285 | sector_div(rt, db/32+1); |
| 6286 | rt *= dt; |
| 6287 | rt >>= 5; |
| 6288 | |
| 6289 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 6290 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6291 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6292 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6293 | } |
| 6294 | |
| 6295 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 6296 | { |
| 6297 | struct list_head *tmp; |
| 6298 | loff_t l = *pos; |
| 6299 | mddev_t *mddev; |
| 6300 | |
| 6301 | if (l >= 0x10000) |
| 6302 | return NULL; |
| 6303 | if (!l--) |
| 6304 | /* header */ |
| 6305 | return (void*)1; |
| 6306 | |
| 6307 | spin_lock(&all_mddevs_lock); |
| 6308 | list_for_each(tmp,&all_mddevs) |
| 6309 | if (!l--) { |
| 6310 | mddev = list_entry(tmp, mddev_t, all_mddevs); |
| 6311 | mddev_get(mddev); |
| 6312 | spin_unlock(&all_mddevs_lock); |
| 6313 | return mddev; |
| 6314 | } |
| 6315 | spin_unlock(&all_mddevs_lock); |
| 6316 | if (!l--) |
| 6317 | return (void*)2;/* tail */ |
| 6318 | return NULL; |
| 6319 | } |
| 6320 | |
| 6321 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 6322 | { |
| 6323 | struct list_head *tmp; |
| 6324 | mddev_t *next_mddev, *mddev = v; |
| 6325 | |
| 6326 | ++*pos; |
| 6327 | if (v == (void*)2) |
| 6328 | return NULL; |
| 6329 | |
| 6330 | spin_lock(&all_mddevs_lock); |
| 6331 | if (v == (void*)1) |
| 6332 | tmp = all_mddevs.next; |
| 6333 | else |
| 6334 | tmp = mddev->all_mddevs.next; |
| 6335 | if (tmp != &all_mddevs) |
| 6336 | next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs)); |
| 6337 | else { |
| 6338 | next_mddev = (void*)2; |
| 6339 | *pos = 0x10000; |
| 6340 | } |
| 6341 | spin_unlock(&all_mddevs_lock); |
| 6342 | |
| 6343 | if (v != (void*)1) |
| 6344 | mddev_put(mddev); |
| 6345 | return next_mddev; |
| 6346 | |
| 6347 | } |
| 6348 | |
| 6349 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 6350 | { |
| 6351 | mddev_t *mddev = v; |
| 6352 | |
| 6353 | if (mddev && v != (void*)1 && v != (void*)2) |
| 6354 | mddev_put(mddev); |
| 6355 | } |
| 6356 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6357 | struct mdstat_info { |
| 6358 | int event; |
| 6359 | }; |
| 6360 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6361 | static int md_seq_show(struct seq_file *seq, void *v) |
| 6362 | { |
| 6363 | mddev_t *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6364 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6365 | mdk_rdev_t *rdev; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6366 | struct mdstat_info *mi = seq->private; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6367 | struct bitmap *bitmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6368 | |
| 6369 | if (v == (void*)1) { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6370 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6371 | seq_printf(seq, "Personalities : "); |
| 6372 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6373 | list_for_each_entry(pers, &pers_list, list) |
| 6374 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6375 | |
| 6376 | spin_unlock(&pers_lock); |
| 6377 | seq_printf(seq, "\n"); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6378 | mi->event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6379 | return 0; |
| 6380 | } |
| 6381 | if (v == (void*)2) { |
| 6382 | status_unused(seq); |
| 6383 | return 0; |
| 6384 | } |
| 6385 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6386 | if (mddev_lock(mddev) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6387 | return -EINTR; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6388 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6389 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 6390 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 6391 | mddev->pers ? "" : "in"); |
| 6392 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6393 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6394 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6395 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 6396 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6397 | seq_printf(seq, " %s", mddev->pers->name); |
| 6398 | } |
| 6399 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6400 | sectors = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6401 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6402 | char b[BDEVNAME_SIZE]; |
| 6403 | seq_printf(seq, " %s[%d]", |
| 6404 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6405 | if (test_bit(WriteMostly, &rdev->flags)) |
| 6406 | seq_printf(seq, "(W)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6407 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6408 | seq_printf(seq, "(F)"); |
| 6409 | continue; |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6410 | } else if (rdev->raid_disk < 0) |
| 6411 | seq_printf(seq, "(S)"); /* spare */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6412 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6413 | } |
| 6414 | |
| 6415 | if (!list_empty(&mddev->disks)) { |
| 6416 | if (mddev->pers) |
| 6417 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 6418 | (unsigned long long) |
| 6419 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6420 | else |
| 6421 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6422 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6423 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6424 | if (mddev->persistent) { |
| 6425 | if (mddev->major_version != 0 || |
| 6426 | mddev->minor_version != 90) { |
| 6427 | seq_printf(seq," super %d.%d", |
| 6428 | mddev->major_version, |
| 6429 | mddev->minor_version); |
| 6430 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6431 | } else if (mddev->external) |
| 6432 | seq_printf(seq, " super external:%s", |
| 6433 | mddev->metadata_type); |
| 6434 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6435 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6436 | |
| 6437 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6438 | mddev->pers->status(seq, mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6439 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6440 | if (mddev->pers->sync_request) { |
| 6441 | if (mddev->curr_resync > 2) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6442 | status_resync(seq, mddev); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6443 | seq_printf(seq, "\n "); |
| 6444 | } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2) |
| 6445 | seq_printf(seq, "\tresync=DELAYED\n "); |
| 6446 | else if (mddev->recovery_cp < MaxSector) |
| 6447 | seq_printf(seq, "\tresync=PENDING\n "); |
| 6448 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6449 | } else |
| 6450 | seq_printf(seq, "\n "); |
| 6451 | |
| 6452 | if ((bitmap = mddev->bitmap)) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6453 | unsigned long chunk_kb; |
| 6454 | unsigned long flags; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6455 | spin_lock_irqsave(&bitmap->lock, flags); |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 6456 | chunk_kb = mddev->bitmap_info.chunksize >> 10; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6457 | seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], " |
| 6458 | "%lu%s chunk", |
| 6459 | bitmap->pages - bitmap->missing_pages, |
| 6460 | bitmap->pages, |
| 6461 | (bitmap->pages - bitmap->missing_pages) |
| 6462 | << (PAGE_SHIFT - 10), |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 6463 | chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize, |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6464 | chunk_kb ? "KB" : "B"); |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 6465 | if (bitmap->file) { |
| 6466 | seq_printf(seq, ", file: "); |
Jan Blunck | c32c2f6 | 2008-02-14 19:38:43 -0800 | [diff] [blame] | 6467 | seq_path(seq, &bitmap->file->f_path, " \t\n"); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6468 | } |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 6469 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6470 | seq_printf(seq, "\n"); |
| 6471 | spin_unlock_irqrestore(&bitmap->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6472 | } |
| 6473 | |
| 6474 | seq_printf(seq, "\n"); |
| 6475 | } |
| 6476 | mddev_unlock(mddev); |
| 6477 | |
| 6478 | return 0; |
| 6479 | } |
| 6480 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 6481 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6482 | .start = md_seq_start, |
| 6483 | .next = md_seq_next, |
| 6484 | .stop = md_seq_stop, |
| 6485 | .show = md_seq_show, |
| 6486 | }; |
| 6487 | |
| 6488 | static int md_seq_open(struct inode *inode, struct file *file) |
| 6489 | { |
| 6490 | int error; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6491 | struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL); |
| 6492 | if (mi == NULL) |
| 6493 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6494 | |
| 6495 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6496 | if (error) |
| 6497 | kfree(mi); |
| 6498 | else { |
| 6499 | struct seq_file *p = file->private_data; |
| 6500 | p->private = mi; |
| 6501 | mi->event = atomic_read(&md_event_count); |
| 6502 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6503 | return error; |
| 6504 | } |
| 6505 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6506 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 6507 | { |
| 6508 | struct seq_file *m = filp->private_data; |
| 6509 | struct mdstat_info *mi = m->private; |
| 6510 | int mask; |
| 6511 | |
| 6512 | poll_wait(filp, &md_event_waiters, wait); |
| 6513 | |
| 6514 | /* always allow read */ |
| 6515 | mask = POLLIN | POLLRDNORM; |
| 6516 | |
| 6517 | if (mi->event != atomic_read(&md_event_count)) |
| 6518 | mask |= POLLERR | POLLPRI; |
| 6519 | return mask; |
| 6520 | } |
| 6521 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 6522 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 6523 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6524 | .open = md_seq_open, |
| 6525 | .read = seq_read, |
| 6526 | .llseek = seq_lseek, |
Martin Peschke | c3f94b40 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 6527 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6528 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6529 | }; |
| 6530 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6531 | int register_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6532 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6533 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6534 | list_add_tail(&p->list, &pers_list); |
| 6535 | 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] | 6536 | spin_unlock(&pers_lock); |
| 6537 | return 0; |
| 6538 | } |
| 6539 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6540 | int unregister_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6541 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6542 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6543 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6544 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6545 | spin_unlock(&pers_lock); |
| 6546 | return 0; |
| 6547 | } |
| 6548 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6549 | static int is_mddev_idle(mddev_t *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6550 | { |
| 6551 | mdk_rdev_t * rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6552 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6553 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6554 | |
| 6555 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6556 | rcu_read_lock(); |
| 6557 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6558 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6559 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
| 6560 | (int)part_stat_read(&disk->part0, sectors[1]) - |
| 6561 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 6562 | /* sync IO will cause sync_io to increase before the disk_stats |
| 6563 | * as sync_io is counted when a request starts, and |
| 6564 | * disk_stats is counted when it completes. |
| 6565 | * So resync activity will cause curr_events to be smaller than |
| 6566 | * when there was no such activity. |
| 6567 | * non-sync IO will cause disk_stat to increase without |
| 6568 | * increasing sync_io so curr_events will (eventually) |
| 6569 | * be larger than it was before. Once it becomes |
| 6570 | * substantially larger, the test below will cause |
| 6571 | * the array to appear non-idle, and resync will slow |
| 6572 | * down. |
| 6573 | * If there is a lot of outstanding resync activity when |
| 6574 | * we set last_event to curr_events, then all that activity |
| 6575 | * completing might cause the array to appear non-idle |
| 6576 | * and resync will be slowed down even though there might |
| 6577 | * not have been non-resync activity. This will only |
| 6578 | * happen once though. 'last_events' will soon reflect |
| 6579 | * the state where there is little or no outstanding |
| 6580 | * resync requests, and further resync activity will |
| 6581 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6582 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6583 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6584 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6585 | rdev->last_events = curr_events; |
| 6586 | idle = 0; |
| 6587 | } |
| 6588 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6589 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6590 | return idle; |
| 6591 | } |
| 6592 | |
| 6593 | void md_done_sync(mddev_t *mddev, int blocks, int ok) |
| 6594 | { |
| 6595 | /* another "blocks" (512byte) blocks have been synced */ |
| 6596 | atomic_sub(blocks, &mddev->recovery_active); |
| 6597 | wake_up(&mddev->recovery_wait); |
| 6598 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6599 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6600 | md_wakeup_thread(mddev->thread); |
| 6601 | // stop recovery, signal do_sync .... |
| 6602 | } |
| 6603 | } |
| 6604 | |
| 6605 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6606 | /* md_write_start(mddev, bi) |
| 6607 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6608 | * in superblock) before writing, schedule a superblock update |
| 6609 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6610 | */ |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6611 | void md_write_start(mddev_t *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6612 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6613 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6614 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6615 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6616 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6617 | BUG_ON(mddev->ro == 1); |
| 6618 | if (mddev->ro == 2) { |
| 6619 | /* need to switch to read/write */ |
| 6620 | mddev->ro = 0; |
| 6621 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6622 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 6623 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6624 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6625 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6626 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6627 | if (mddev->safemode == 1) |
| 6628 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6629 | if (mddev->in_sync) { |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6630 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6631 | if (mddev->in_sync) { |
| 6632 | mddev->in_sync = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6633 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 6634 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6635 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6636 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6637 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6638 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6639 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6640 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6641 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 6642 | wait_event(mddev->sb_wait, |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 6643 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6644 | } |
| 6645 | |
| 6646 | void md_write_end(mddev_t *mddev) |
| 6647 | { |
| 6648 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 6649 | if (mddev->safemode == 2) |
| 6650 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 6651 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6652 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 6653 | } |
| 6654 | } |
| 6655 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6656 | /* md_allow_write(mddev) |
| 6657 | * Calling this ensures that the array is marked 'active' so that writes |
| 6658 | * may proceed without blocking. It is important to call this before |
| 6659 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 6660 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6661 | * |
| 6662 | * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock |
| 6663 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6664 | */ |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6665 | int md_allow_write(mddev_t *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6666 | { |
| 6667 | if (!mddev->pers) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6668 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6669 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6670 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 6671 | if (!mddev->pers->sync_request) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6672 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6673 | |
| 6674 | spin_lock_irq(&mddev->write_lock); |
| 6675 | if (mddev->in_sync) { |
| 6676 | mddev->in_sync = 0; |
| 6677 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 6678 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6679 | if (mddev->safemode_delay && |
| 6680 | mddev->safemode == 0) |
| 6681 | mddev->safemode = 1; |
| 6682 | spin_unlock_irq(&mddev->write_lock); |
| 6683 | md_update_sb(mddev, 0); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6684 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6685 | } else |
| 6686 | spin_unlock_irq(&mddev->write_lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6687 | |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 6688 | if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6689 | return -EAGAIN; |
| 6690 | else |
| 6691 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6692 | } |
| 6693 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 6694 | |
NeilBrown | b63d7c2 | 2010-06-01 19:37:33 +1000 | [diff] [blame] | 6695 | void md_unplug(mddev_t *mddev) |
NeilBrown | 252ac52 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 6696 | { |
NeilBrown | 252ac52 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 6697 | if (mddev->plug) |
| 6698 | mddev->plug->unplug_fn(mddev->plug); |
| 6699 | } |
| 6700 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6701 | #define SYNC_MARKS 10 |
| 6702 | #define SYNC_MARK_STEP (3*HZ) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6703 | void md_do_sync(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6704 | { |
| 6705 | mddev_t *mddev2; |
| 6706 | unsigned int currspeed = 0, |
| 6707 | window; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6708 | sector_t max_sectors,j, io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6709 | unsigned long mark[SYNC_MARKS]; |
| 6710 | sector_t mark_cnt[SYNC_MARKS]; |
| 6711 | int last_mark,m; |
| 6712 | struct list_head *tmp; |
| 6713 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6714 | int skipped = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6715 | mdk_rdev_t *rdev; |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6716 | char *desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6717 | |
| 6718 | /* just incase thread restarts... */ |
| 6719 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 6720 | return; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6721 | if (mddev->ro) /* never try to sync a read-only array */ |
| 6722 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6723 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6724 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6725 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 6726 | desc = "data-check"; |
| 6727 | else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6728 | desc = "requested-resync"; |
| 6729 | else |
| 6730 | desc = "resync"; |
| 6731 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 6732 | desc = "reshape"; |
| 6733 | else |
| 6734 | desc = "recovery"; |
| 6735 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6736 | /* we overload curr_resync somewhat here. |
| 6737 | * 0 == not engaged in resync at all |
| 6738 | * 2 == checking that there is no conflict with another sync |
| 6739 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 6740 | * commense |
| 6741 | * other == active in resync - this many blocks |
| 6742 | * |
| 6743 | * Before starting a resync we must have set curr_resync to |
| 6744 | * 2, and then checked that every "conflicting" array has curr_resync |
| 6745 | * less than ours. When we find one that is the same or higher |
| 6746 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 6747 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 6748 | * This will mean we have to start checking from the beginning again. |
| 6749 | * |
| 6750 | */ |
| 6751 | |
| 6752 | do { |
| 6753 | mddev->curr_resync = 2; |
| 6754 | |
| 6755 | try_again: |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 6756 | if (kthread_should_stop()) |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 6757 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 6758 | |
| 6759 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6760 | goto skip; |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6761 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6762 | if (mddev2 == mddev) |
| 6763 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 6764 | if (!mddev->parallel_resync |
| 6765 | && mddev2->curr_resync |
| 6766 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6767 | DEFINE_WAIT(wq); |
| 6768 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 6769 | /* arbitrarily yield */ |
| 6770 | mddev->curr_resync = 1; |
| 6771 | wake_up(&resync_wait); |
| 6772 | } |
| 6773 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 6774 | /* no need to wait here, we can wait the next |
| 6775 | * time 'round when curr_resync == 2 |
| 6776 | */ |
| 6777 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 6778 | /* We need to wait 'interruptible' so as not to |
| 6779 | * contribute to the load average, and not to |
| 6780 | * be caught by 'softlockup' |
| 6781 | */ |
| 6782 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 6783 | if (!kthread_should_stop() && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 6784 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6785 | printk(KERN_INFO "md: delaying %s of %s" |
| 6786 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6787 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6788 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6789 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 6790 | if (signal_pending(current)) |
| 6791 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6792 | schedule(); |
| 6793 | finish_wait(&resync_wait, &wq); |
| 6794 | goto try_again; |
| 6795 | } |
| 6796 | finish_wait(&resync_wait, &wq); |
| 6797 | } |
| 6798 | } |
| 6799 | } while (mddev->curr_resync < 2); |
| 6800 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6801 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6802 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6803 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6804 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6805 | */ |
| 6806 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6807 | mddev->resync_mismatches = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6808 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6809 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6810 | j = mddev->resync_min; |
| 6811 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6812 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6813 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6814 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6815 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6816 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6817 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6818 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6819 | j = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6820 | rcu_read_lock(); |
| 6821 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6822 | if (rdev->raid_disk >= 0 && |
| 6823 | !test_bit(Faulty, &rdev->flags) && |
| 6824 | !test_bit(In_sync, &rdev->flags) && |
| 6825 | rdev->recovery_offset < j) |
| 6826 | j = rdev->recovery_offset; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6827 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6828 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6829 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6830 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 6831 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 6832 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 6833 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6834 | "(but not more than %d KB/sec) for %s.\n", |
| 6835 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6836 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6837 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6838 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6839 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6840 | for (m = 0; m < SYNC_MARKS; m++) { |
| 6841 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6842 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6843 | } |
| 6844 | last_mark = 0; |
| 6845 | mddev->resync_mark = mark[last_mark]; |
| 6846 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 6847 | |
| 6848 | /* |
| 6849 | * Tune reconstruction: |
| 6850 | */ |
| 6851 | window = 32*(PAGE_SIZE/512); |
| 6852 | printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n", |
| 6853 | window/2,(unsigned long long) max_sectors/2); |
| 6854 | |
| 6855 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6856 | last_check = 0; |
| 6857 | |
| 6858 | if (j>2) { |
| 6859 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6860 | "md: resuming %s of %s from checkpoint.\n", |
| 6861 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6862 | mddev->curr_resync = j; |
| 6863 | } |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 6864 | mddev->curr_resync_completed = j; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6865 | |
| 6866 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6867 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6868 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6869 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6870 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 6871 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 6872 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 6873 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 6874 | > (max_sectors >> 4)) || |
| 6875 | (j - mddev->curr_resync_completed)*2 |
| 6876 | >= mddev->resync_max - mddev->curr_resync_completed |
| 6877 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6878 | /* time to update curr_resync_completed */ |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6879 | wait_event(mddev->recovery_wait, |
| 6880 | atomic_read(&mddev->recovery_active) == 0); |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 6881 | mddev->curr_resync_completed = j; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 6882 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6883 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6884 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6885 | |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 6886 | while (j >= mddev->resync_max && !kthread_should_stop()) { |
| 6887 | /* As this condition is controlled by user-space, |
| 6888 | * we can block indefinitely, so use '_interruptible' |
| 6889 | * to avoid triggering warnings. |
| 6890 | */ |
| 6891 | flush_signals(current); /* just in case */ |
| 6892 | wait_event_interruptible(mddev->recovery_wait, |
| 6893 | mddev->resync_max > j |
| 6894 | || kthread_should_stop()); |
| 6895 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6896 | |
| 6897 | if (kthread_should_stop()) |
| 6898 | goto interrupted; |
| 6899 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6900 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6901 | currspeed < speed_min(mddev)); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6902 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6903 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6904 | goto out; |
| 6905 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6906 | |
| 6907 | if (!skipped) { /* actual IO requested */ |
| 6908 | io_sectors += sectors; |
| 6909 | atomic_add(sectors, &mddev->recovery_active); |
| 6910 | } |
| 6911 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6912 | j += sectors; |
| 6913 | if (j>1) mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6914 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6915 | if (last_check == 0) |
| 6916 | /* this is the earliers that rebuilt will be |
| 6917 | * visible in /proc/mdstat |
| 6918 | */ |
| 6919 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6920 | |
| 6921 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6922 | continue; |
| 6923 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6924 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6925 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6926 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6927 | break; |
| 6928 | |
| 6929 | repeat: |
| 6930 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 6931 | /* step marks */ |
| 6932 | int next = (last_mark+1) % SYNC_MARKS; |
| 6933 | |
| 6934 | mddev->resync_mark = mark[next]; |
| 6935 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 6936 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6937 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6938 | last_mark = next; |
| 6939 | } |
| 6940 | |
| 6941 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6942 | if (kthread_should_stop()) |
| 6943 | goto interrupted; |
| 6944 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6945 | |
| 6946 | /* |
| 6947 | * this loop exits only if either when we are slower than |
| 6948 | * the 'hard' speed limit, or the system was IO-idle for |
| 6949 | * a jiffy. |
| 6950 | * the system might be non-idle CPU-wise, but we only care |
| 6951 | * about not overloading the IO subsystem. (things like an |
| 6952 | * e2fsck being done on the RAID array should execute fast) |
| 6953 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6954 | cond_resched(); |
| 6955 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6956 | currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2 |
| 6957 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6958 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 6959 | if (currspeed > speed_min(mddev)) { |
| 6960 | if ((currspeed > speed_max(mddev)) || |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6961 | !is_mddev_idle(mddev, 0)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6962 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6963 | goto repeat; |
| 6964 | } |
| 6965 | } |
| 6966 | } |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6967 | printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6968 | /* |
| 6969 | * this also signals 'finished resyncing' to md_stop |
| 6970 | */ |
| 6971 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6972 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 6973 | |
| 6974 | /* tell personality that we are finished */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6975 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6976 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6977 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6978 | mddev->curr_resync > 2) { |
| 6979 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6980 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 6981 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 6982 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6983 | "md: checkpointing %s of %s.\n", |
| 6984 | desc, mdname(mddev)); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6985 | mddev->recovery_cp = mddev->curr_resync; |
| 6986 | } |
| 6987 | } else |
| 6988 | mddev->recovery_cp = MaxSector; |
| 6989 | } else { |
| 6990 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 6991 | mddev->curr_resync = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6992 | rcu_read_lock(); |
| 6993 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6994 | if (rdev->raid_disk >= 0 && |
NeilBrown | 70fffd0 | 2010-06-16 17:01:25 +1000 | [diff] [blame] | 6995 | mddev->delta_disks >= 0 && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6996 | !test_bit(Faulty, &rdev->flags) && |
| 6997 | !test_bit(In_sync, &rdev->flags) && |
| 6998 | rdev->recovery_offset < mddev->curr_resync) |
| 6999 | rdev->recovery_offset = mddev->curr_resync; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7000 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7001 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7002 | } |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 7003 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7004 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7005 | skip: |
NeilBrown | c07b70a | 2009-12-14 12:49:48 +1100 | [diff] [blame] | 7006 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 7007 | /* We completed so min/max setting can be forgotten if used. */ |
| 7008 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7009 | mddev->resync_min = 0; |
| 7010 | mddev->resync_max = MaxSector; |
| 7011 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7012 | mddev->resync_min = mddev->curr_resync_completed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7013 | mddev->curr_resync = 0; |
| 7014 | wake_up(&resync_wait); |
| 7015 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
| 7016 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 7017 | return; |
| 7018 | |
| 7019 | interrupted: |
| 7020 | /* |
| 7021 | * got a signal, exit. |
| 7022 | */ |
| 7023 | printk(KERN_INFO |
| 7024 | "md: md_do_sync() got signal ... exiting\n"); |
| 7025 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7026 | goto out; |
| 7027 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7028 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7029 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7030 | |
| 7031 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7032 | static int remove_and_add_spares(mddev_t *mddev) |
| 7033 | { |
| 7034 | mdk_rdev_t *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7035 | int spares = 0; |
| 7036 | |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7037 | mddev->curr_resync_completed = 0; |
| 7038 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 7039 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7040 | if (rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7041 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7042 | (test_bit(Faulty, &rdev->flags) || |
| 7043 | ! test_bit(In_sync, &rdev->flags)) && |
| 7044 | atomic_read(&rdev->nr_pending)==0) { |
| 7045 | if (mddev->pers->hot_remove_disk( |
| 7046 | mddev, rdev->raid_disk)==0) { |
| 7047 | char nm[20]; |
| 7048 | sprintf(nm,"rd%d", rdev->raid_disk); |
| 7049 | sysfs_remove_link(&mddev->kobj, nm); |
| 7050 | rdev->raid_disk = -1; |
| 7051 | } |
| 7052 | } |
| 7053 | |
NeilBrown | a8c42c7 | 2011-01-31 13:47:13 +1100 | [diff] [blame] | 7054 | if (mddev->degraded && !mddev->recovery_disabled) { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 7055 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7056 | if (rdev->raid_disk >= 0 && |
Dan Williams | e542713 | 2008-07-28 23:28:06 -0700 | [diff] [blame] | 7057 | !test_bit(In_sync, &rdev->flags) && |
| 7058 | !test_bit(Blocked, &rdev->flags)) |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7059 | spares++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7060 | if (rdev->raid_disk < 0 |
| 7061 | && !test_bit(Faulty, &rdev->flags)) { |
| 7062 | rdev->recovery_offset = 0; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 7063 | if (mddev->pers-> |
| 7064 | hot_add_disk(mddev, rdev) == 0) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7065 | char nm[20]; |
| 7066 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 7067 | if (sysfs_create_link(&mddev->kobj, |
| 7068 | &rdev->kobj, nm)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7069 | /* failure here is OK */; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7070 | spares++; |
| 7071 | md_new_event(mddev); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 7072 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7073 | } else |
| 7074 | break; |
| 7075 | } |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7076 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7077 | } |
| 7078 | return spares; |
| 7079 | } |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7080 | |
| 7081 | static void reap_sync_thread(mddev_t *mddev) |
| 7082 | { |
| 7083 | mdk_rdev_t *rdev; |
| 7084 | |
| 7085 | /* resync has finished, collect result */ |
| 7086 | md_unregister_thread(mddev->sync_thread); |
| 7087 | mddev->sync_thread = NULL; |
| 7088 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 7089 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
| 7090 | /* success...*/ |
| 7091 | /* activate any spares */ |
| 7092 | if (mddev->pers->spare_active(mddev)) |
| 7093 | sysfs_notify(&mddev->kobj, NULL, |
| 7094 | "degraded"); |
| 7095 | } |
| 7096 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 7097 | mddev->pers->finish_reshape) |
| 7098 | mddev->pers->finish_reshape(mddev); |
| 7099 | md_update_sb(mddev, 1); |
| 7100 | |
| 7101 | /* if array is no-longer degraded, then any saved_raid_disk |
| 7102 | * information must be scrapped |
| 7103 | */ |
| 7104 | if (!mddev->degraded) |
| 7105 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 7106 | rdev->saved_raid_disk = -1; |
| 7107 | |
| 7108 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7109 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7110 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 7111 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 7112 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 7113 | /* flag recovery needed just to double check */ |
| 7114 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7115 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 7116 | md_new_event(mddev); |
| 7117 | } |
| 7118 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7119 | /* |
| 7120 | * This routine is regularly called by all per-raid-array threads to |
| 7121 | * deal with generic issues like resync and super-block update. |
| 7122 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 7123 | * need this as they never do any recovery or update the superblock. |
| 7124 | * |
| 7125 | * It does not do any resync itself, but rather "forks" off other threads |
| 7126 | * to do that as needed. |
| 7127 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 7128 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7129 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7130 | * and wakeups up this thread which will reap the thread and finish up. |
| 7131 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 7132 | * |
| 7133 | * The overall approach is: |
| 7134 | * 1/ if the superblock needs updating, update it. |
| 7135 | * 2/ If a recovery thread is running, don't do anything else. |
| 7136 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 7137 | * 4/ If there are any faulty devices, remove them. |
| 7138 | * 5/ If array is degraded, try to add spares devices |
| 7139 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 7140 | */ |
| 7141 | void md_check_recovery(mddev_t *mddev) |
| 7142 | { |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 7143 | if (mddev->bitmap) |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 7144 | bitmap_daemon_work(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7145 | |
| 7146 | if (mddev->ro) |
| 7147 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7148 | |
| 7149 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7150 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7151 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 7152 | mdname(mddev)); |
| 7153 | mddev->safemode = 2; |
| 7154 | } |
| 7155 | flush_signals(current); |
| 7156 | } |
| 7157 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7158 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 7159 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7160 | if ( ! ( |
NeilBrown | 126925c | 2010-09-07 17:02:47 +1000 | [diff] [blame] | 7161 | (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7162 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7163 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7164 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7165 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 7166 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7167 | )) |
| 7168 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7169 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 7170 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7171 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7172 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7173 | if (mddev->ro) { |
| 7174 | /* Only thing we do on a ro array is remove |
| 7175 | * failed devices. |
| 7176 | */ |
NeilBrown | a8c42c7 | 2011-01-31 13:47:13 +1100 | [diff] [blame] | 7177 | mdk_rdev_t *rdev; |
| 7178 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 7179 | if (rdev->raid_disk >= 0 && |
| 7180 | !test_bit(Blocked, &rdev->flags) && |
| 7181 | test_bit(Faulty, &rdev->flags) && |
| 7182 | atomic_read(&rdev->nr_pending)==0) { |
| 7183 | if (mddev->pers->hot_remove_disk( |
| 7184 | mddev, rdev->raid_disk)==0) { |
| 7185 | char nm[20]; |
| 7186 | sprintf(nm,"rd%d", rdev->raid_disk); |
| 7187 | sysfs_remove_link(&mddev->kobj, nm); |
| 7188 | rdev->raid_disk = -1; |
| 7189 | } |
| 7190 | } |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7191 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7192 | goto unlock; |
| 7193 | } |
| 7194 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7195 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7196 | int did_change = 0; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7197 | spin_lock_irq(&mddev->write_lock); |
| 7198 | if (mddev->safemode && |
| 7199 | !atomic_read(&mddev->writes_pending) && |
| 7200 | !mddev->in_sync && |
| 7201 | mddev->recovery_cp == MaxSector) { |
| 7202 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7203 | did_change = 1; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7204 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7205 | } |
| 7206 | if (mddev->safemode == 1) |
| 7207 | mddev->safemode = 0; |
| 7208 | spin_unlock_irq(&mddev->write_lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7209 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7210 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7211 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7212 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7213 | if (mddev->flags) |
| 7214 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7215 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7216 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 7217 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 7218 | /* resync/recovery still happening */ |
| 7219 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7220 | goto unlock; |
| 7221 | } |
| 7222 | if (mddev->sync_thread) { |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7223 | reap_sync_thread(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7224 | goto unlock; |
| 7225 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7226 | /* Set RUNNING before clearing NEEDED to avoid |
| 7227 | * any transients in the value of "sync_action". |
| 7228 | */ |
| 7229 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7230 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7231 | /* Clear some bits that don't mean anything, but |
| 7232 | * might be left set |
| 7233 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7234 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7235 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7236 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7237 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 7238 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7239 | /* no recovery is running. |
| 7240 | * remove any failed drives, then |
| 7241 | * add spares if possible. |
| 7242 | * Spare are also removed and re-added, to allow |
| 7243 | * the personality to fail the re-add. |
| 7244 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7245 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7246 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 7247 | if (mddev->pers->check_reshape == NULL || |
| 7248 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7249 | /* Cannot proceed */ |
| 7250 | goto unlock; |
| 7251 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7252 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7253 | } else if ((spares = remove_and_add_spares(mddev))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7254 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7255 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 7256 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7257 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7258 | } else if (mddev->recovery_cp < MaxSector) { |
| 7259 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7260 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7261 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 7262 | /* nothing to be done ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7263 | goto unlock; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7264 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7265 | if (mddev->pers->sync_request) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 7266 | if (spares && mddev->bitmap && ! mddev->bitmap->file) { |
| 7267 | /* We are adding a device or devices to an array |
| 7268 | * which has the bitmap stored on all devices. |
| 7269 | * So make sure all bitmap pages get written |
| 7270 | */ |
| 7271 | bitmap_write_all(mddev->bitmap); |
| 7272 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7273 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 7274 | mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 7275 | "resync"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7276 | if (!mddev->sync_thread) { |
| 7277 | printk(KERN_ERR "%s: could not start resync" |
| 7278 | " thread...\n", |
| 7279 | mdname(mddev)); |
| 7280 | /* leave the spares where they are, it shouldn't hurt */ |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7281 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7282 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7283 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 7284 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 7285 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7286 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7287 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7288 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7289 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7290 | } |
| 7291 | unlock: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7292 | if (!mddev->sync_thread) { |
| 7293 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7294 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 7295 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 7296 | if (mddev->sysfs_action) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7297 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7298 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7299 | mddev_unlock(mddev); |
| 7300 | } |
| 7301 | } |
| 7302 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7303 | void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 7304 | { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7305 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7306 | wait_event_timeout(rdev->blocked_wait, |
| 7307 | !test_bit(Blocked, &rdev->flags), |
| 7308 | msecs_to_jiffies(5000)); |
| 7309 | rdev_dec_pending(rdev, mddev); |
| 7310 | } |
| 7311 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 7312 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7313 | static int md_notify_reboot(struct notifier_block *this, |
| 7314 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7315 | { |
| 7316 | struct list_head *tmp; |
| 7317 | mddev_t *mddev; |
| 7318 | |
| 7319 | if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) { |
| 7320 | |
| 7321 | printk(KERN_INFO "md: stopping all md devices.\n"); |
| 7322 | |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7323 | for_each_mddev(mddev, tmp) |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 7324 | if (mddev_trylock(mddev)) { |
NeilBrown | 2b25000 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7325 | /* Force a switch to readonly even array |
| 7326 | * appears to still be in use. Hence |
| 7327 | * the '100'. |
| 7328 | */ |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 7329 | md_set_readonly(mddev, 100); |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 7330 | mddev_unlock(mddev); |
| 7331 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7332 | /* |
| 7333 | * certain more exotic SCSI devices are known to be |
| 7334 | * volatile wrt too early system reboots. While the |
| 7335 | * right place to handle this issue is the given |
| 7336 | * driver, we do want to have a safe RAID driver ... |
| 7337 | */ |
| 7338 | mdelay(1000*1); |
| 7339 | } |
| 7340 | return NOTIFY_DONE; |
| 7341 | } |
| 7342 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7343 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7344 | .notifier_call = md_notify_reboot, |
| 7345 | .next = NULL, |
| 7346 | .priority = INT_MAX, /* before any real devices */ |
| 7347 | }; |
| 7348 | |
| 7349 | static void md_geninit(void) |
| 7350 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7351 | dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t)); |
| 7352 | |
Denis V. Lunev | c7705f344 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 7353 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7354 | } |
| 7355 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7356 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7357 | { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 7358 | int ret = -ENOMEM; |
| 7359 | |
| 7360 | md_wq = alloc_workqueue("md", WQ_RESCUER, 0); |
| 7361 | if (!md_wq) |
| 7362 | goto err_wq; |
| 7363 | |
| 7364 | md_misc_wq = alloc_workqueue("md_misc", 0, 0); |
| 7365 | if (!md_misc_wq) |
| 7366 | goto err_misc_wq; |
| 7367 | |
| 7368 | if ((ret = register_blkdev(MD_MAJOR, "md")) < 0) |
| 7369 | goto err_md; |
| 7370 | |
| 7371 | if ((ret = register_blkdev(0, "mdp")) < 0) |
| 7372 | goto err_mdp; |
| 7373 | mdp_major = ret; |
| 7374 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7375 | blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE, |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 7376 | md_probe, NULL, NULL); |
| 7377 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7378 | md_probe, NULL, NULL); |
| 7379 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7380 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 7381 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7382 | |
| 7383 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 7384 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7385 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 7386 | err_mdp: |
| 7387 | unregister_blkdev(MD_MAJOR, "md"); |
| 7388 | err_md: |
| 7389 | destroy_workqueue(md_misc_wq); |
| 7390 | err_misc_wq: |
| 7391 | destroy_workqueue(md_wq); |
| 7392 | err_wq: |
| 7393 | return ret; |
| 7394 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7395 | |
| 7396 | #ifndef MODULE |
| 7397 | |
| 7398 | /* |
| 7399 | * Searches all registered partitions for autorun RAID arrays |
| 7400 | * at boot time. |
| 7401 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7402 | |
| 7403 | static LIST_HEAD(all_detected_devices); |
| 7404 | struct detected_devices_node { |
| 7405 | struct list_head list; |
| 7406 | dev_t dev; |
| 7407 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7408 | |
| 7409 | void md_autodetect_dev(dev_t dev) |
| 7410 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7411 | struct detected_devices_node *node_detected_dev; |
| 7412 | |
| 7413 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 7414 | if (node_detected_dev) { |
| 7415 | node_detected_dev->dev = dev; |
| 7416 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 7417 | } else { |
| 7418 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 7419 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 7420 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7421 | } |
| 7422 | |
| 7423 | |
| 7424 | static void autostart_arrays(int part) |
| 7425 | { |
| 7426 | mdk_rdev_t *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7427 | struct detected_devices_node *node_detected_dev; |
| 7428 | dev_t dev; |
| 7429 | int i_scanned, i_passed; |
| 7430 | |
| 7431 | i_scanned = 0; |
| 7432 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7433 | |
| 7434 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 7435 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7436 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 7437 | i_scanned++; |
| 7438 | node_detected_dev = list_entry(all_detected_devices.next, |
| 7439 | struct detected_devices_node, list); |
| 7440 | list_del(&node_detected_dev->list); |
| 7441 | dev = node_detected_dev->dev; |
| 7442 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 7443 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7444 | if (IS_ERR(rdev)) |
| 7445 | continue; |
| 7446 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7447 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7448 | MD_BUG(); |
| 7449 | continue; |
| 7450 | } |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 7451 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7452 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7453 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7454 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7455 | |
| 7456 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 7457 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7458 | |
| 7459 | autorun_devices(part); |
| 7460 | } |
| 7461 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 7462 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7463 | |
| 7464 | static __exit void md_exit(void) |
| 7465 | { |
| 7466 | mddev_t *mddev; |
| 7467 | struct list_head *tmp; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 7468 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7469 | blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS); |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 7470 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7471 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7472 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7473 | unregister_blkdev(mdp_major, "mdp"); |
| 7474 | unregister_reboot_notifier(&md_notifier); |
| 7475 | unregister_sysctl_table(raid_table_header); |
| 7476 | remove_proc_entry("mdstat", NULL); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7477 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7478 | export_array(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7479 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7480 | } |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 7481 | destroy_workqueue(md_misc_wq); |
| 7482 | destroy_workqueue(md_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7483 | } |
| 7484 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 7485 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7486 | module_exit(md_exit) |
| 7487 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7488 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 7489 | { |
| 7490 | return sprintf(buffer, "%d", start_readonly); |
| 7491 | } |
| 7492 | static int set_ro(const char *val, struct kernel_param *kp) |
| 7493 | { |
| 7494 | char *e; |
| 7495 | int num = simple_strtoul(val, &e, 10); |
| 7496 | if (*val && (*e == '\0' || *e == '\n')) { |
| 7497 | start_readonly = num; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 7498 | return 0; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7499 | } |
| 7500 | return -EINVAL; |
| 7501 | } |
| 7502 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 7503 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 7504 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 7505 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7506 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7507 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7508 | EXPORT_SYMBOL(register_md_personality); |
| 7509 | EXPORT_SYMBOL(unregister_md_personality); |
| 7510 | EXPORT_SYMBOL(md_error); |
| 7511 | EXPORT_SYMBOL(md_done_sync); |
| 7512 | EXPORT_SYMBOL(md_write_start); |
| 7513 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7514 | EXPORT_SYMBOL(md_register_thread); |
| 7515 | EXPORT_SYMBOL(md_unregister_thread); |
| 7516 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7517 | EXPORT_SYMBOL(md_check_recovery); |
| 7518 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 7519 | MODULE_DESCRIPTION("MD RAID framework"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 7520 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 7521 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |