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; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1780 | if (!reference) { |
| 1781 | /* Use the first rdev as the reference */ |
| 1782 | reference = rdev; |
| 1783 | continue; |
| 1784 | } |
| 1785 | /* does this rdev's profile match the reference profile? */ |
| 1786 | if (blk_integrity_compare(reference->bdev->bd_disk, |
| 1787 | rdev->bdev->bd_disk) < 0) |
| 1788 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1789 | } |
Martin K. Petersen | 89078d5 | 2011-03-28 20:09:12 -0400 | [diff] [blame] | 1790 | if (!reference || !bdev_get_integrity(reference->bdev)) |
| 1791 | return 0; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1792 | /* |
| 1793 | * All component devices are integrity capable and have matching |
| 1794 | * profiles, register the common profile for the md device. |
| 1795 | */ |
| 1796 | if (blk_integrity_register(mddev->gendisk, |
| 1797 | bdev_get_integrity(reference->bdev)) != 0) { |
| 1798 | printk(KERN_ERR "md: failed to register integrity for %s\n", |
| 1799 | mdname(mddev)); |
| 1800 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1801 | } |
Martin K. Petersen | a91a278 | 2011-03-17 11:11:05 +0100 | [diff] [blame] | 1802 | printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev)); |
| 1803 | if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) { |
| 1804 | printk(KERN_ERR "md: failed to create integrity pool for %s\n", |
| 1805 | mdname(mddev)); |
| 1806 | return -EINVAL; |
| 1807 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1808 | return 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1809 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1810 | EXPORT_SYMBOL(md_integrity_register); |
| 1811 | |
| 1812 | /* Disable data integrity if non-capable/non-matching disk is being added */ |
| 1813 | void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 1814 | { |
| 1815 | struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev); |
| 1816 | struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk); |
| 1817 | |
| 1818 | if (!bi_mddev) /* nothing to do */ |
| 1819 | return; |
| 1820 | if (rdev->raid_disk < 0) /* skip spares */ |
| 1821 | return; |
| 1822 | if (bi_rdev && blk_integrity_compare(mddev->gendisk, |
| 1823 | rdev->bdev->bd_disk) >= 0) |
| 1824 | return; |
| 1825 | printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev)); |
| 1826 | blk_integrity_unregister(mddev->gendisk); |
| 1827 | } |
| 1828 | EXPORT_SYMBOL(md_integrity_add_rdev); |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1829 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1830 | static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev) |
| 1831 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1832 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 1833 | struct kobject *ko; |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1834 | char *s; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1835 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1836 | |
| 1837 | if (rdev->mddev) { |
| 1838 | MD_BUG(); |
| 1839 | return -EINVAL; |
| 1840 | } |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1841 | |
| 1842 | /* prevent duplicates */ |
| 1843 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 1844 | return -EEXIST; |
| 1845 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1846 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
| 1847 | if (rdev->sectors && (mddev->dev_sectors == 0 || |
| 1848 | rdev->sectors < mddev->dev_sectors)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1849 | if (mddev->pers) { |
| 1850 | /* Cannot change size, so fail |
| 1851 | * If mddev->level <= 0, then we don't care |
| 1852 | * about aligning sizes (e.g. linear) |
| 1853 | */ |
| 1854 | if (mddev->level > 0) |
| 1855 | return -ENOSPC; |
| 1856 | } else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1857 | mddev->dev_sectors = rdev->sectors; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1858 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1859 | |
| 1860 | /* Verify rdev->desc_nr is unique. |
| 1861 | * If it is -1, assign a free number, else |
| 1862 | * check number is not in use |
| 1863 | */ |
| 1864 | if (rdev->desc_nr < 0) { |
| 1865 | int choice = 0; |
| 1866 | if (mddev->pers) choice = mddev->raid_disks; |
| 1867 | while (find_rdev_nr(mddev, choice)) |
| 1868 | choice++; |
| 1869 | rdev->desc_nr = choice; |
| 1870 | } else { |
| 1871 | if (find_rdev_nr(mddev, rdev->desc_nr)) |
| 1872 | return -EBUSY; |
| 1873 | } |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 1874 | if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
| 1875 | printk(KERN_WARNING "md: %s: array is limited to %d devices\n", |
| 1876 | mdname(mddev), mddev->max_disks); |
| 1877 | return -EBUSY; |
| 1878 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1879 | bdevname(rdev->bdev,b); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1880 | while ( (s=strchr(b, '/')) != NULL) |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1881 | *s = '!'; |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1882 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1883 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1884 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1885 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1886 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1887 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1888 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1889 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 1890 | if (sysfs_create_link(&rdev->kobj, ko, "block")) |
| 1891 | /* failure here is OK */; |
| 1892 | rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 1893 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1894 | list_add_rcu(&rdev->same_set, &mddev->disks); |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1895 | bd_link_disk_holder(rdev->bdev, mddev->gendisk); |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 1896 | |
| 1897 | /* May as well allow recovery to be retried once */ |
| 1898 | mddev->recovery_disabled = 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1899 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1900 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1901 | |
| 1902 | fail: |
| 1903 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", |
| 1904 | b, mdname(mddev)); |
| 1905 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1906 | } |
| 1907 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1908 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1909 | { |
| 1910 | mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work); |
| 1911 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1912 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1913 | } |
| 1914 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1915 | static void unbind_rdev_from_array(mdk_rdev_t * rdev) |
| 1916 | { |
| 1917 | char b[BDEVNAME_SIZE]; |
| 1918 | if (!rdev->mddev) { |
| 1919 | MD_BUG(); |
| 1920 | return; |
| 1921 | } |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 1922 | bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1923 | list_del_rcu(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1924 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 1925 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1926 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 1927 | sysfs_put(rdev->sysfs_state); |
| 1928 | rdev->sysfs_state = NULL; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1929 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1930 | * writing to 'remove' to "dev/state". We also need |
| 1931 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1932 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1933 | synchronize_rcu(); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1934 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 1935 | kobject_get(&rdev->kobj); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 1936 | queue_work(md_misc_wq, &rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1937 | } |
| 1938 | |
| 1939 | /* |
| 1940 | * prevent the device from being mounted, repartitioned or |
| 1941 | * otherwise reused by a RAID array (or any other kernel |
| 1942 | * subsystem), by bd_claiming the device. |
| 1943 | */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1944 | 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] | 1945 | { |
| 1946 | int err = 0; |
| 1947 | struct block_device *bdev; |
| 1948 | char b[BDEVNAME_SIZE]; |
| 1949 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1950 | bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, |
| 1951 | shared ? (mdk_rdev_t *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1952 | if (IS_ERR(bdev)) { |
| 1953 | printk(KERN_ERR "md: could not open %s.\n", |
| 1954 | __bdevname(dev, b)); |
| 1955 | return PTR_ERR(bdev); |
| 1956 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1957 | rdev->bdev = bdev; |
| 1958 | return err; |
| 1959 | } |
| 1960 | |
| 1961 | static void unlock_rdev(mdk_rdev_t *rdev) |
| 1962 | { |
| 1963 | struct block_device *bdev = rdev->bdev; |
| 1964 | rdev->bdev = NULL; |
| 1965 | if (!bdev) |
| 1966 | MD_BUG(); |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1967 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1968 | } |
| 1969 | |
| 1970 | void md_autodetect_dev(dev_t dev); |
| 1971 | |
| 1972 | static void export_rdev(mdk_rdev_t * rdev) |
| 1973 | { |
| 1974 | char b[BDEVNAME_SIZE]; |
| 1975 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 1976 | bdevname(rdev->bdev,b)); |
| 1977 | if (rdev->mddev) |
| 1978 | MD_BUG(); |
| 1979 | free_disk_sb(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1980 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1981 | if (test_bit(AutoDetected, &rdev->flags)) |
| 1982 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1983 | #endif |
| 1984 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1985 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1986 | } |
| 1987 | |
| 1988 | static void kick_rdev_from_array(mdk_rdev_t * rdev) |
| 1989 | { |
| 1990 | unbind_rdev_from_array(rdev); |
| 1991 | export_rdev(rdev); |
| 1992 | } |
| 1993 | |
| 1994 | static void export_array(mddev_t *mddev) |
| 1995 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1996 | mdk_rdev_t *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1997 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1998 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1999 | if (!rdev->mddev) { |
| 2000 | MD_BUG(); |
| 2001 | continue; |
| 2002 | } |
| 2003 | kick_rdev_from_array(rdev); |
| 2004 | } |
| 2005 | if (!list_empty(&mddev->disks)) |
| 2006 | MD_BUG(); |
| 2007 | mddev->raid_disks = 0; |
| 2008 | mddev->major_version = 0; |
| 2009 | } |
| 2010 | |
| 2011 | static void print_desc(mdp_disk_t *desc) |
| 2012 | { |
| 2013 | printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number, |
| 2014 | desc->major,desc->minor,desc->raid_disk,desc->state); |
| 2015 | } |
| 2016 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2017 | static void print_sb_90(mdp_super_t *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2018 | { |
| 2019 | int i; |
| 2020 | |
| 2021 | printk(KERN_INFO |
| 2022 | "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n", |
| 2023 | sb->major_version, sb->minor_version, sb->patch_version, |
| 2024 | sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3, |
| 2025 | sb->ctime); |
| 2026 | printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n", |
| 2027 | sb->level, sb->size, sb->nr_disks, sb->raid_disks, |
| 2028 | sb->md_minor, sb->layout, sb->chunk_size); |
| 2029 | printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d" |
| 2030 | " FD:%d SD:%d CSUM:%08x E:%08lx\n", |
| 2031 | sb->utime, sb->state, sb->active_disks, sb->working_disks, |
| 2032 | sb->failed_disks, sb->spare_disks, |
| 2033 | sb->sb_csum, (unsigned long)sb->events_lo); |
| 2034 | |
| 2035 | printk(KERN_INFO); |
| 2036 | for (i = 0; i < MD_SB_DISKS; i++) { |
| 2037 | mdp_disk_t *desc; |
| 2038 | |
| 2039 | desc = sb->disks + i; |
| 2040 | if (desc->number || desc->major || desc->minor || |
| 2041 | desc->raid_disk || (desc->state && (desc->state != 4))) { |
| 2042 | printk(" D %2d: ", i); |
| 2043 | print_desc(desc); |
| 2044 | } |
| 2045 | } |
| 2046 | printk(KERN_INFO "md: THIS: "); |
| 2047 | print_desc(&sb->this_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2048 | } |
| 2049 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2050 | static void print_sb_1(struct mdp_superblock_1 *sb) |
| 2051 | { |
| 2052 | __u8 *uuid; |
| 2053 | |
| 2054 | uuid = sb->set_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2055 | printk(KERN_INFO |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2056 | "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2057 | "md: Name: \"%s\" CT:%llu\n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2058 | le32_to_cpu(sb->major_version), |
| 2059 | le32_to_cpu(sb->feature_map), |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2060 | uuid, |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2061 | sb->set_name, |
| 2062 | (unsigned long long)le64_to_cpu(sb->ctime) |
| 2063 | & MD_SUPERBLOCK_1_TIME_SEC_MASK); |
| 2064 | |
| 2065 | uuid = sb->device_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2066 | printk(KERN_INFO |
| 2067 | "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] | 2068 | " RO:%llu\n" |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2069 | "md: Dev:%08x UUID: %pU\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2070 | "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n" |
| 2071 | "md: (MaxDev:%u) \n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2072 | le32_to_cpu(sb->level), |
| 2073 | (unsigned long long)le64_to_cpu(sb->size), |
| 2074 | le32_to_cpu(sb->raid_disks), |
| 2075 | le32_to_cpu(sb->layout), |
| 2076 | le32_to_cpu(sb->chunksize), |
| 2077 | (unsigned long long)le64_to_cpu(sb->data_offset), |
| 2078 | (unsigned long long)le64_to_cpu(sb->data_size), |
| 2079 | (unsigned long long)le64_to_cpu(sb->super_offset), |
| 2080 | (unsigned long long)le64_to_cpu(sb->recovery_offset), |
| 2081 | le32_to_cpu(sb->dev_number), |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2082 | uuid, |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2083 | sb->devflags, |
| 2084 | (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK, |
| 2085 | (unsigned long long)le64_to_cpu(sb->events), |
| 2086 | (unsigned long long)le64_to_cpu(sb->resync_offset), |
| 2087 | le32_to_cpu(sb->sb_csum), |
| 2088 | le32_to_cpu(sb->max_dev) |
| 2089 | ); |
| 2090 | } |
| 2091 | |
| 2092 | static void print_rdev(mdk_rdev_t *rdev, int major_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2093 | { |
| 2094 | char b[BDEVNAME_SIZE]; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2095 | printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n", |
| 2096 | bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors, |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2097 | test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags), |
| 2098 | rdev->desc_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2099 | if (rdev->sb_loaded) { |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2100 | printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version); |
| 2101 | switch (major_version) { |
| 2102 | case 0: |
| 2103 | print_sb_90((mdp_super_t*)page_address(rdev->sb_page)); |
| 2104 | break; |
| 2105 | case 1: |
| 2106 | print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page)); |
| 2107 | break; |
| 2108 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2109 | } else |
| 2110 | printk(KERN_INFO "md: no rdev superblock!\n"); |
| 2111 | } |
| 2112 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 2113 | static void md_print_devices(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2114 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2115 | struct list_head *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2116 | mdk_rdev_t *rdev; |
| 2117 | mddev_t *mddev; |
| 2118 | char b[BDEVNAME_SIZE]; |
| 2119 | |
| 2120 | printk("\n"); |
| 2121 | printk("md: **********************************\n"); |
| 2122 | printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n"); |
| 2123 | printk("md: **********************************\n"); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2124 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2125 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 2126 | if (mddev->bitmap) |
| 2127 | bitmap_print_sb(mddev->bitmap); |
| 2128 | else |
| 2129 | printk("%s: ", mdname(mddev)); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2130 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2131 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 2132 | printk("\n"); |
| 2133 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2134 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2135 | print_rdev(rdev, mddev->major_version); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2136 | } |
| 2137 | printk("md: **********************************\n"); |
| 2138 | printk("\n"); |
| 2139 | } |
| 2140 | |
| 2141 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2142 | static void sync_sbs(mddev_t * mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2143 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2144 | /* Update each superblock (in-memory image), but |
| 2145 | * if we are allowed to, skip spares which already |
| 2146 | * have the right event counter, or have one earlier |
| 2147 | * (which would mean they aren't being marked as dirty |
| 2148 | * with the rest of the array) |
| 2149 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2150 | mdk_rdev_t *rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2151 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2152 | if (rdev->sb_events == mddev->events || |
| 2153 | (nospares && |
| 2154 | rdev->raid_disk < 0 && |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2155 | rdev->sb_events+1 == mddev->events)) { |
| 2156 | /* Don't update this superblock */ |
| 2157 | rdev->sb_loaded = 2; |
| 2158 | } else { |
| 2159 | super_types[mddev->major_version]. |
| 2160 | sync_super(mddev, rdev); |
| 2161 | rdev->sb_loaded = 1; |
| 2162 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2163 | } |
| 2164 | } |
| 2165 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2166 | static void md_update_sb(mddev_t * mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2167 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2168 | mdk_rdev_t *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2169 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2170 | int nospares = 0; |
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 | repeat: |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2173 | /* First make sure individual recovery_offsets are correct */ |
| 2174 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 2175 | if (rdev->raid_disk >= 0 && |
| 2176 | mddev->delta_disks >= 0 && |
| 2177 | !test_bit(In_sync, &rdev->flags) && |
| 2178 | mddev->curr_resync_completed > rdev->recovery_offset) |
| 2179 | rdev->recovery_offset = mddev->curr_resync_completed; |
| 2180 | |
| 2181 | } |
Dan Williams | bd52b74 | 2010-08-30 17:33:33 +1000 | [diff] [blame] | 2182 | if (!mddev->persistent) { |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 2183 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2184 | clear_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | d97a41dc | 2010-10-28 17:30:20 +1100 | [diff] [blame] | 2185 | if (!mddev->external) |
| 2186 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2187 | wake_up(&mddev->sb_wait); |
| 2188 | return; |
| 2189 | } |
| 2190 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2191 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2192 | |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2193 | mddev->utime = get_seconds(); |
| 2194 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2195 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 2196 | force_change = 1; |
| 2197 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 2198 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 2199 | * though if events isn't the right even/odd, we will have to do |
| 2200 | * spares after all |
| 2201 | */ |
| 2202 | nospares = 1; |
| 2203 | if (force_change) |
| 2204 | nospares = 0; |
| 2205 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2206 | /* If the array is degraded, then skipping spares is both |
| 2207 | * dangerous and fairly pointless. |
| 2208 | * Dangerous because a device that was removed from the array |
| 2209 | * might have a event_count that still looks up-to-date, |
| 2210 | * so it can be re-added without a resync. |
| 2211 | * Pointless because if there are any spares to skip, |
| 2212 | * then a recovery will happen and soon that array won't |
| 2213 | * be degraded any more and the spare can go back to sleep then. |
| 2214 | */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2215 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2216 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2217 | sync_req = mddev->in_sync; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2218 | |
| 2219 | /* If this is just a dirty<->clean transition, and the array is clean |
| 2220 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2221 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2222 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2223 | && mddev->can_decrease_events |
| 2224 | && mddev->events != 1) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2225 | mddev->events--; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2226 | mddev->can_decrease_events = 0; |
| 2227 | } else { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2228 | /* otherwise we have to go forward and ... */ |
| 2229 | mddev->events ++; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2230 | mddev->can_decrease_events = nospares; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2231 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2232 | |
| 2233 | if (!mddev->events) { |
| 2234 | /* |
| 2235 | * oops, this 64-bit counter should never wrap. |
| 2236 | * Either we are in around ~1 trillion A.C., assuming |
| 2237 | * 1 reboot per second, or we have a bug: |
| 2238 | */ |
| 2239 | MD_BUG(); |
| 2240 | mddev->events --; |
| 2241 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2242 | sync_sbs(mddev, nospares); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2243 | spin_unlock_irq(&mddev->write_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2244 | |
| 2245 | dprintk(KERN_INFO |
| 2246 | "md: updating %s RAID superblock on device (in sync %d)\n", |
| 2247 | mdname(mddev),mddev->in_sync); |
| 2248 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 2249 | bitmap_update_sb(mddev->bitmap); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2250 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2251 | char b[BDEVNAME_SIZE]; |
| 2252 | dprintk(KERN_INFO "md: "); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2253 | if (rdev->sb_loaded != 1) |
| 2254 | continue; /* no noise on spare devices */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2255 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2256 | dprintk("(skipping faulty "); |
| 2257 | |
| 2258 | dprintk("%s ", bdevname(rdev->bdev,b)); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2259 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2260 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2261 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2262 | rdev->sb_page); |
| 2263 | dprintk(KERN_INFO "(write) %s's sb offset: %llu\n", |
| 2264 | bdevname(rdev->bdev,b), |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2265 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2266 | rdev->sb_events = mddev->events; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2267 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2268 | } else |
| 2269 | dprintk(")\n"); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2270 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2271 | /* only need to write one superblock... */ |
| 2272 | break; |
| 2273 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2274 | md_super_wait(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2275 | /* 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] | 2276 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2277 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2278 | if (mddev->in_sync != sync_req || |
| 2279 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2280 | /* have to write it out again */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2281 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2282 | goto repeat; |
| 2283 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2284 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2285 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2286 | wake_up(&mddev->sb_wait); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 2287 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 2288 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2289 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2290 | } |
| 2291 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 2292 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2293 | * We want to accept with case. For this we use cmd_match. |
| 2294 | */ |
| 2295 | static int cmd_match(const char *cmd, const char *str) |
| 2296 | { |
| 2297 | /* See if cmd, written into a sysfs file, matches |
| 2298 | * str. They must either be the same, or cmd can |
| 2299 | * have a trailing newline |
| 2300 | */ |
| 2301 | while (*cmd && *str && *cmd == *str) { |
| 2302 | cmd++; |
| 2303 | str++; |
| 2304 | } |
| 2305 | if (*cmd == '\n') |
| 2306 | cmd++; |
| 2307 | if (*str || *cmd) |
| 2308 | return 0; |
| 2309 | return 1; |
| 2310 | } |
| 2311 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2312 | struct rdev_sysfs_entry { |
| 2313 | struct attribute attr; |
| 2314 | ssize_t (*show)(mdk_rdev_t *, char *); |
| 2315 | ssize_t (*store)(mdk_rdev_t *, const char *, size_t); |
| 2316 | }; |
| 2317 | |
| 2318 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2319 | state_show(mdk_rdev_t *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2320 | { |
| 2321 | char *sep = ""; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2322 | size_t len = 0; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2323 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2324 | if (test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2325 | len+= sprintf(page+len, "%sfaulty",sep); |
| 2326 | sep = ","; |
| 2327 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2328 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2329 | len += sprintf(page+len, "%sin_sync",sep); |
| 2330 | sep = ","; |
| 2331 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2332 | if (test_bit(WriteMostly, &rdev->flags)) { |
| 2333 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 2334 | sep = ","; |
| 2335 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2336 | if (test_bit(Blocked, &rdev->flags)) { |
| 2337 | len += sprintf(page+len, "%sblocked", sep); |
| 2338 | sep = ","; |
| 2339 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2340 | if (!test_bit(Faulty, &rdev->flags) && |
| 2341 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2342 | len += sprintf(page+len, "%sspare", sep); |
| 2343 | sep = ","; |
| 2344 | } |
| 2345 | return len+sprintf(page+len, "\n"); |
| 2346 | } |
| 2347 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2348 | static ssize_t |
| 2349 | state_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2350 | { |
| 2351 | /* can write |
| 2352 | * faulty - simulates and error |
| 2353 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2354 | * writemostly - sets write_mostly |
| 2355 | * -writemostly - clears write_mostly |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2356 | * blocked - sets the Blocked flag |
| 2357 | * -blocked - clears the Blocked flag |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2358 | * insync - sets Insync providing device isn't active |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2359 | */ |
| 2360 | int err = -EINVAL; |
| 2361 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 2362 | md_error(rdev->mddev, rdev); |
| 2363 | err = 0; |
| 2364 | } else if (cmd_match(buf, "remove")) { |
| 2365 | if (rdev->raid_disk >= 0) |
| 2366 | err = -EBUSY; |
| 2367 | else { |
| 2368 | mddev_t *mddev = rdev->mddev; |
| 2369 | kick_rdev_from_array(rdev); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2370 | if (mddev->pers) |
| 2371 | md_update_sb(mddev, 1); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2372 | md_new_event(mddev); |
| 2373 | err = 0; |
| 2374 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2375 | } else if (cmd_match(buf, "writemostly")) { |
| 2376 | set_bit(WriteMostly, &rdev->flags); |
| 2377 | err = 0; |
| 2378 | } else if (cmd_match(buf, "-writemostly")) { |
| 2379 | clear_bit(WriteMostly, &rdev->flags); |
| 2380 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2381 | } else if (cmd_match(buf, "blocked")) { |
| 2382 | set_bit(Blocked, &rdev->flags); |
| 2383 | err = 0; |
| 2384 | } else if (cmd_match(buf, "-blocked")) { |
| 2385 | clear_bit(Blocked, &rdev->flags); |
| 2386 | wake_up(&rdev->blocked_wait); |
| 2387 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2388 | md_wakeup_thread(rdev->mddev->thread); |
| 2389 | |
| 2390 | err = 0; |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2391 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { |
| 2392 | set_bit(In_sync, &rdev->flags); |
| 2393 | err = 0; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2394 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2395 | if (!err) |
| 2396 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2397 | return err ? err : len; |
| 2398 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2399 | static struct rdev_sysfs_entry rdev_state = |
| 2400 | __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2401 | |
| 2402 | static ssize_t |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2403 | errors_show(mdk_rdev_t *rdev, char *page) |
| 2404 | { |
| 2405 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 2406 | } |
| 2407 | |
| 2408 | static ssize_t |
| 2409 | errors_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2410 | { |
| 2411 | char *e; |
| 2412 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2413 | if (*buf && (*e == 0 || *e == '\n')) { |
| 2414 | atomic_set(&rdev->corrected_errors, n); |
| 2415 | return len; |
| 2416 | } |
| 2417 | return -EINVAL; |
| 2418 | } |
| 2419 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2420 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2421 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2422 | static ssize_t |
| 2423 | slot_show(mdk_rdev_t *rdev, char *page) |
| 2424 | { |
| 2425 | if (rdev->raid_disk < 0) |
| 2426 | return sprintf(page, "none\n"); |
| 2427 | else |
| 2428 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 2429 | } |
| 2430 | |
| 2431 | static ssize_t |
| 2432 | slot_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2433 | { |
| 2434 | char *e; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2435 | int err; |
| 2436 | char nm[20]; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2437 | int slot = simple_strtoul(buf, &e, 10); |
| 2438 | if (strncmp(buf, "none", 4)==0) |
| 2439 | slot = -1; |
| 2440 | else if (e==buf || (*e && *e!= '\n')) |
| 2441 | return -EINVAL; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2442 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2443 | /* Setting 'slot' on an active array requires also |
| 2444 | * updating the 'rd%d' link, and communicating |
| 2445 | * with the personality with ->hot_*_disk. |
| 2446 | * For now we only support removing |
| 2447 | * failed/spare devices. This normally happens automatically, |
| 2448 | * but not when the metadata is externally managed. |
| 2449 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2450 | if (rdev->raid_disk == -1) |
| 2451 | return -EEXIST; |
| 2452 | /* personality does all needed checks */ |
| 2453 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2454 | return -EINVAL; |
| 2455 | err = rdev->mddev->pers-> |
| 2456 | hot_remove_disk(rdev->mddev, rdev->raid_disk); |
| 2457 | if (err) |
| 2458 | return err; |
| 2459 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2460 | sysfs_remove_link(&rdev->mddev->kobj, nm); |
Maciej Trela | b710310 | 2010-04-14 16:58:16 +1000 | [diff] [blame] | 2461 | rdev->raid_disk = -1; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2462 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2463 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2464 | } else if (rdev->mddev->pers) { |
| 2465 | mdk_rdev_t *rdev2; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2466 | /* Activating a spare .. or possibly reactivating |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2467 | * if we ever get bitmaps working here. |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2468 | */ |
| 2469 | |
| 2470 | if (rdev->raid_disk != -1) |
| 2471 | return -EBUSY; |
| 2472 | |
NeilBrown | c6751b2 | 2011-02-02 11:57:13 +1100 | [diff] [blame] | 2473 | if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery)) |
| 2474 | return -EBUSY; |
| 2475 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2476 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2477 | return -EINVAL; |
| 2478 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2479 | list_for_each_entry(rdev2, &rdev->mddev->disks, same_set) |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2480 | if (rdev2->raid_disk == slot) |
| 2481 | return -EEXIST; |
| 2482 | |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2483 | if (slot >= rdev->mddev->raid_disks && |
| 2484 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
| 2485 | return -ENOSPC; |
| 2486 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2487 | rdev->raid_disk = slot; |
| 2488 | if (test_bit(In_sync, &rdev->flags)) |
| 2489 | rdev->saved_raid_disk = slot; |
| 2490 | else |
| 2491 | rdev->saved_raid_disk = -1; |
| 2492 | err = rdev->mddev->pers-> |
| 2493 | hot_add_disk(rdev->mddev, rdev); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 2494 | if (err) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2495 | rdev->raid_disk = -1; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2496 | return err; |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 2497 | } else |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2498 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2499 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2500 | if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2501 | /* failure here is OK */; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2502 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2503 | } else { |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2504 | if (slot >= rdev->mddev->raid_disks && |
| 2505 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2506 | return -ENOSPC; |
| 2507 | rdev->raid_disk = slot; |
| 2508 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2509 | clear_bit(Faulty, &rdev->flags); |
| 2510 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2511 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2512 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2513 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2514 | return len; |
| 2515 | } |
| 2516 | |
| 2517 | |
| 2518 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2519 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2520 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2521 | static ssize_t |
| 2522 | offset_show(mdk_rdev_t *rdev, char *page) |
| 2523 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2524 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2525 | } |
| 2526 | |
| 2527 | static ssize_t |
| 2528 | offset_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2529 | { |
| 2530 | char *e; |
| 2531 | unsigned long long offset = simple_strtoull(buf, &e, 10); |
| 2532 | if (e==buf || (*e && *e != '\n')) |
| 2533 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2534 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2535 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2536 | if (rdev->sectors && rdev->mddev->external) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2537 | /* Must set offset before size, so overlap checks |
| 2538 | * can be sane */ |
| 2539 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2540 | rdev->data_offset = offset; |
| 2541 | return len; |
| 2542 | } |
| 2543 | |
| 2544 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2545 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2546 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2547 | static ssize_t |
| 2548 | rdev_size_show(mdk_rdev_t *rdev, char *page) |
| 2549 | { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2550 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2551 | } |
| 2552 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2553 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 2554 | { |
| 2555 | /* check if two start/length pairs overlap */ |
| 2556 | if (s1+l1 <= s2) |
| 2557 | return 0; |
| 2558 | if (s2+l2 <= s1) |
| 2559 | return 0; |
| 2560 | return 1; |
| 2561 | } |
| 2562 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2563 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
| 2564 | { |
| 2565 | unsigned long long blocks; |
| 2566 | sector_t new; |
| 2567 | |
| 2568 | if (strict_strtoull(buf, 10, &blocks) < 0) |
| 2569 | return -EINVAL; |
| 2570 | |
| 2571 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) |
| 2572 | return -EINVAL; /* sector conversion overflow */ |
| 2573 | |
| 2574 | new = blocks * 2; |
| 2575 | if (new != blocks * 2) |
| 2576 | return -EINVAL; /* unsigned long long to sector_t overflow */ |
| 2577 | |
| 2578 | *sectors = new; |
| 2579 | return 0; |
| 2580 | } |
| 2581 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2582 | static ssize_t |
| 2583 | rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2584 | { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2585 | mddev_t *my_mddev = rdev->mddev; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2586 | sector_t oldsectors = rdev->sectors; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2587 | sector_t sectors; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2588 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2589 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2590 | return -EINVAL; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2591 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2592 | if (my_mddev->persistent) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2593 | sectors = super_types[my_mddev->major_version]. |
| 2594 | rdev_size_change(rdev, sectors); |
| 2595 | if (!sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2596 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2597 | } else if (!sectors) |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 2598 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) - |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2599 | rdev->data_offset; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2600 | } |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2601 | if (sectors < my_mddev->dev_sectors) |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 2602 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2603 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2604 | rdev->sectors = sectors; |
| 2605 | if (sectors > oldsectors && my_mddev->external) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2606 | /* need to check that all other rdevs with the same ->bdev |
| 2607 | * do not overlap. We need to unlock the mddev to avoid |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2608 | * a deadlock. We have already changed rdev->sectors, and if |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2609 | * we have to change it back, we will have the lock again. |
| 2610 | */ |
| 2611 | mddev_t *mddev; |
| 2612 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2613 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2614 | |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2615 | mddev_unlock(my_mddev); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2616 | for_each_mddev(mddev, tmp) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2617 | mdk_rdev_t *rdev2; |
| 2618 | |
| 2619 | mddev_lock(mddev); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2620 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
NeilBrown | f21e9ff | 2011-01-31 12:10:09 +1100 | [diff] [blame] | 2621 | if (rdev->bdev == rdev2->bdev && |
| 2622 | rdev != rdev2 && |
| 2623 | overlaps(rdev->data_offset, rdev->sectors, |
| 2624 | rdev2->data_offset, |
| 2625 | rdev2->sectors)) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2626 | overlap = 1; |
| 2627 | break; |
| 2628 | } |
| 2629 | mddev_unlock(mddev); |
| 2630 | if (overlap) { |
| 2631 | mddev_put(mddev); |
| 2632 | break; |
| 2633 | } |
| 2634 | } |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2635 | mddev_lock(my_mddev); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2636 | if (overlap) { |
| 2637 | /* Someone else could have slipped in a size |
| 2638 | * change here, but doing so is just silly. |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2639 | * We put oldsectors back because we *know* it is |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2640 | * safe, and trust userspace not to race with |
| 2641 | * itself |
| 2642 | */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2643 | rdev->sectors = oldsectors; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2644 | return -EBUSY; |
| 2645 | } |
| 2646 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2647 | return len; |
| 2648 | } |
| 2649 | |
| 2650 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2651 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2652 | |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2653 | |
| 2654 | static ssize_t recovery_start_show(mdk_rdev_t *rdev, char *page) |
| 2655 | { |
| 2656 | unsigned long long recovery_start = rdev->recovery_offset; |
| 2657 | |
| 2658 | if (test_bit(In_sync, &rdev->flags) || |
| 2659 | recovery_start == MaxSector) |
| 2660 | return sprintf(page, "none\n"); |
| 2661 | |
| 2662 | return sprintf(page, "%llu\n", recovery_start); |
| 2663 | } |
| 2664 | |
| 2665 | static ssize_t recovery_start_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2666 | { |
| 2667 | unsigned long long recovery_start; |
| 2668 | |
| 2669 | if (cmd_match(buf, "none")) |
| 2670 | recovery_start = MaxSector; |
| 2671 | else if (strict_strtoull(buf, 10, &recovery_start)) |
| 2672 | return -EINVAL; |
| 2673 | |
| 2674 | if (rdev->mddev->pers && |
| 2675 | rdev->raid_disk >= 0) |
| 2676 | return -EBUSY; |
| 2677 | |
| 2678 | rdev->recovery_offset = recovery_start; |
| 2679 | if (recovery_start == MaxSector) |
| 2680 | set_bit(In_sync, &rdev->flags); |
| 2681 | else |
| 2682 | clear_bit(In_sync, &rdev->flags); |
| 2683 | return len; |
| 2684 | } |
| 2685 | |
| 2686 | static struct rdev_sysfs_entry rdev_recovery_start = |
| 2687 | __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store); |
| 2688 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2689 | static struct attribute *rdev_default_attrs[] = { |
| 2690 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2691 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2692 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2693 | &rdev_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2694 | &rdev_size.attr, |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2695 | &rdev_recovery_start.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2696 | NULL, |
| 2697 | }; |
| 2698 | static ssize_t |
| 2699 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 2700 | { |
| 2701 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2702 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2703 | mddev_t *mddev = rdev->mddev; |
| 2704 | ssize_t rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2705 | |
| 2706 | if (!entry->show) |
| 2707 | return -EIO; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2708 | |
| 2709 | rv = mddev ? mddev_lock(mddev) : -EBUSY; |
| 2710 | if (!rv) { |
| 2711 | if (rdev->mddev == NULL) |
| 2712 | rv = -EBUSY; |
| 2713 | else |
| 2714 | rv = entry->show(rdev, page); |
| 2715 | mddev_unlock(mddev); |
| 2716 | } |
| 2717 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2718 | } |
| 2719 | |
| 2720 | static ssize_t |
| 2721 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 2722 | const char *page, size_t length) |
| 2723 | { |
| 2724 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2725 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2726 | ssize_t rv; |
| 2727 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2728 | |
| 2729 | if (!entry->store) |
| 2730 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 2731 | if (!capable(CAP_SYS_ADMIN)) |
| 2732 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2733 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2734 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2735 | if (rdev->mddev == NULL) |
| 2736 | rv = -EBUSY; |
| 2737 | else |
| 2738 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 2739 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2740 | } |
| 2741 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2742 | } |
| 2743 | |
| 2744 | static void rdev_free(struct kobject *ko) |
| 2745 | { |
| 2746 | mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj); |
| 2747 | kfree(rdev); |
| 2748 | } |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 2749 | static const struct sysfs_ops rdev_sysfs_ops = { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2750 | .show = rdev_attr_show, |
| 2751 | .store = rdev_attr_store, |
| 2752 | }; |
| 2753 | static struct kobj_type rdev_ktype = { |
| 2754 | .release = rdev_free, |
| 2755 | .sysfs_ops = &rdev_sysfs_ops, |
| 2756 | .default_attrs = rdev_default_attrs, |
| 2757 | }; |
| 2758 | |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 2759 | void md_rdev_init(mdk_rdev_t *rdev) |
| 2760 | { |
| 2761 | rdev->desc_nr = -1; |
| 2762 | rdev->saved_raid_disk = -1; |
| 2763 | rdev->raid_disk = -1; |
| 2764 | rdev->flags = 0; |
| 2765 | rdev->data_offset = 0; |
| 2766 | rdev->sb_events = 0; |
| 2767 | rdev->last_read_error.tv_sec = 0; |
| 2768 | rdev->last_read_error.tv_nsec = 0; |
| 2769 | atomic_set(&rdev->nr_pending, 0); |
| 2770 | atomic_set(&rdev->read_errors, 0); |
| 2771 | atomic_set(&rdev->corrected_errors, 0); |
| 2772 | |
| 2773 | INIT_LIST_HEAD(&rdev->same_set); |
| 2774 | init_waitqueue_head(&rdev->blocked_wait); |
| 2775 | } |
| 2776 | EXPORT_SYMBOL_GPL(md_rdev_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2777 | /* |
| 2778 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 2779 | * |
| 2780 | * mark the device faulty if: |
| 2781 | * |
| 2782 | * - the device is nonexistent (zero size) |
| 2783 | * - the device has no valid superblock |
| 2784 | * |
| 2785 | * a faulty rdev _never_ has rdev->sb set. |
| 2786 | */ |
| 2787 | static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor) |
| 2788 | { |
| 2789 | char b[BDEVNAME_SIZE]; |
| 2790 | int err; |
| 2791 | mdk_rdev_t *rdev; |
| 2792 | sector_t size; |
| 2793 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 2794 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2795 | if (!rdev) { |
| 2796 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 2797 | return ERR_PTR(-ENOMEM); |
| 2798 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2799 | |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 2800 | md_rdev_init(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2801 | if ((err = alloc_disk_sb(rdev))) |
| 2802 | goto abort_free; |
| 2803 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2804 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2805 | if (err) |
| 2806 | goto abort_free; |
| 2807 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2808 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2809 | |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 2810 | size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2811 | if (!size) { |
| 2812 | printk(KERN_WARNING |
| 2813 | "md: %s has zero or unknown size, marking faulty!\n", |
| 2814 | bdevname(rdev->bdev,b)); |
| 2815 | err = -EINVAL; |
| 2816 | goto abort_free; |
| 2817 | } |
| 2818 | |
| 2819 | if (super_format >= 0) { |
| 2820 | err = super_types[super_format]. |
| 2821 | load_super(rdev, NULL, super_minor); |
| 2822 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 2823 | printk(KERN_WARNING |
| 2824 | "md: %s does not have a valid v%d.%d " |
| 2825 | "superblock, not importing!\n", |
| 2826 | bdevname(rdev->bdev,b), |
| 2827 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2828 | goto abort_free; |
| 2829 | } |
| 2830 | if (err < 0) { |
| 2831 | printk(KERN_WARNING |
| 2832 | "md: could not read %s's sb, not importing!\n", |
| 2833 | bdevname(rdev->bdev,b)); |
| 2834 | goto abort_free; |
| 2835 | } |
| 2836 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2837 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2838 | return rdev; |
| 2839 | |
| 2840 | abort_free: |
| 2841 | if (rdev->sb_page) { |
| 2842 | if (rdev->bdev) |
| 2843 | unlock_rdev(rdev); |
| 2844 | free_disk_sb(rdev); |
| 2845 | } |
| 2846 | kfree(rdev); |
| 2847 | return ERR_PTR(err); |
| 2848 | } |
| 2849 | |
| 2850 | /* |
| 2851 | * Check a full RAID array for plausibility |
| 2852 | */ |
| 2853 | |
| 2854 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 2855 | static void analyze_sbs(mddev_t * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2856 | { |
| 2857 | int i; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2858 | mdk_rdev_t *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2859 | char b[BDEVNAME_SIZE]; |
| 2860 | |
| 2861 | freshest = NULL; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2862 | rdev_for_each(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2863 | switch (super_types[mddev->major_version]. |
| 2864 | load_super(rdev, freshest, mddev->minor_version)) { |
| 2865 | case 1: |
| 2866 | freshest = rdev; |
| 2867 | break; |
| 2868 | case 0: |
| 2869 | break; |
| 2870 | default: |
| 2871 | printk( KERN_ERR \ |
| 2872 | "md: fatal superblock inconsistency in %s" |
| 2873 | " -- removing from array\n", |
| 2874 | bdevname(rdev->bdev,b)); |
| 2875 | kick_rdev_from_array(rdev); |
| 2876 | } |
| 2877 | |
| 2878 | |
| 2879 | super_types[mddev->major_version]. |
| 2880 | validate_super(mddev, freshest); |
| 2881 | |
| 2882 | i = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2883 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 2884 | if (mddev->max_disks && |
| 2885 | (rdev->desc_nr >= mddev->max_disks || |
| 2886 | i > mddev->max_disks)) { |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 2887 | printk(KERN_WARNING |
| 2888 | "md: %s: %s: only %d devices permitted\n", |
| 2889 | mdname(mddev), bdevname(rdev->bdev, b), |
| 2890 | mddev->max_disks); |
| 2891 | kick_rdev_from_array(rdev); |
| 2892 | continue; |
| 2893 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2894 | if (rdev != freshest) |
| 2895 | if (super_types[mddev->major_version]. |
| 2896 | validate_super(mddev, rdev)) { |
| 2897 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 2898 | " from array!\n", |
| 2899 | bdevname(rdev->bdev,b)); |
| 2900 | kick_rdev_from_array(rdev); |
| 2901 | continue; |
| 2902 | } |
| 2903 | if (mddev->level == LEVEL_MULTIPATH) { |
| 2904 | rdev->desc_nr = i++; |
| 2905 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2906 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 2907 | } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2908 | rdev->raid_disk = -1; |
| 2909 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2910 | } |
| 2911 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2912 | } |
| 2913 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2914 | /* Read a fixed-point number. |
| 2915 | * Numbers in sysfs attributes should be in "standard" units where |
| 2916 | * possible, so time should be in seconds. |
| 2917 | * However we internally use a a much smaller unit such as |
| 2918 | * milliseconds or jiffies. |
| 2919 | * This function takes a decimal number with a possible fractional |
| 2920 | * component, and produces an integer which is the result of |
| 2921 | * multiplying that number by 10^'scale'. |
| 2922 | * all without any floating-point arithmetic. |
| 2923 | */ |
| 2924 | int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale) |
| 2925 | { |
| 2926 | unsigned long result = 0; |
| 2927 | long decimals = -1; |
| 2928 | while (isdigit(*cp) || (*cp == '.' && decimals < 0)) { |
| 2929 | if (*cp == '.') |
| 2930 | decimals = 0; |
| 2931 | else if (decimals < scale) { |
| 2932 | unsigned int value; |
| 2933 | value = *cp - '0'; |
| 2934 | result = result * 10 + value; |
| 2935 | if (decimals >= 0) |
| 2936 | decimals++; |
| 2937 | } |
| 2938 | cp++; |
| 2939 | } |
| 2940 | if (*cp == '\n') |
| 2941 | cp++; |
| 2942 | if (*cp) |
| 2943 | return -EINVAL; |
| 2944 | if (decimals < 0) |
| 2945 | decimals = 0; |
| 2946 | while (decimals < scale) { |
| 2947 | result *= 10; |
| 2948 | decimals ++; |
| 2949 | } |
| 2950 | *res = result; |
| 2951 | return 0; |
| 2952 | } |
| 2953 | |
| 2954 | |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2955 | static void md_safemode_timeout(unsigned long data); |
| 2956 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2957 | static ssize_t |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2958 | safe_delay_show(mddev_t *mddev, char *page) |
| 2959 | { |
| 2960 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 2961 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 2962 | } |
| 2963 | static ssize_t |
| 2964 | safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len) |
| 2965 | { |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2966 | unsigned long msec; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 2967 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2968 | if (strict_strtoul_scaled(cbuf, &msec, 3) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2969 | return -EINVAL; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2970 | if (msec == 0) |
| 2971 | mddev->safemode_delay = 0; |
| 2972 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2973 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2974 | mddev->safemode_delay = (msec*HZ)/1000; |
| 2975 | if (mddev->safemode_delay == 0) |
| 2976 | mddev->safemode_delay = 1; |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2977 | if (mddev->safemode_delay < old_delay) |
| 2978 | md_safemode_timeout((unsigned long)mddev); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2979 | } |
| 2980 | return len; |
| 2981 | } |
| 2982 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2983 | __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] | 2984 | |
| 2985 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2986 | level_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2987 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 2988 | struct mdk_personality *p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2989 | if (p) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2990 | return sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2991 | else if (mddev->clevel[0]) |
| 2992 | return sprintf(page, "%s\n", mddev->clevel); |
| 2993 | else if (mddev->level != LEVEL_NONE) |
| 2994 | return sprintf(page, "%d\n", mddev->level); |
| 2995 | else |
| 2996 | return 0; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2997 | } |
| 2998 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2999 | static ssize_t |
| 3000 | level_store(mddev_t *mddev, const char *buf, size_t len) |
| 3001 | { |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3002 | char clevel[16]; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 3003 | ssize_t rv = len; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3004 | struct mdk_personality *pers; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3005 | long level; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3006 | void *priv; |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3007 | mdk_rdev_t *rdev; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3008 | |
| 3009 | if (mddev->pers == NULL) { |
| 3010 | if (len == 0) |
| 3011 | return 0; |
| 3012 | if (len >= sizeof(mddev->clevel)) |
| 3013 | return -ENOSPC; |
| 3014 | strncpy(mddev->clevel, buf, len); |
| 3015 | if (mddev->clevel[len-1] == '\n') |
| 3016 | len--; |
| 3017 | mddev->clevel[len] = 0; |
| 3018 | mddev->level = LEVEL_NONE; |
| 3019 | return rv; |
| 3020 | } |
| 3021 | |
| 3022 | /* request to change the personality. Need to ensure: |
| 3023 | * - array is not engaged in resync/recovery/reshape |
| 3024 | * - old personality can be suspended |
| 3025 | * - new personality will access other array. |
| 3026 | */ |
| 3027 | |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 3028 | if (mddev->sync_thread || |
| 3029 | mddev->reshape_position != MaxSector || |
| 3030 | mddev->sysfs_active) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3031 | return -EBUSY; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3032 | |
| 3033 | if (!mddev->pers->quiesce) { |
| 3034 | printk(KERN_WARNING "md: %s: %s does not support online personality change\n", |
| 3035 | mdname(mddev), mddev->pers->name); |
| 3036 | return -EINVAL; |
| 3037 | } |
| 3038 | |
| 3039 | /* Now find the new personality */ |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3040 | if (len == 0 || len >= sizeof(clevel)) |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3041 | return -EINVAL; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3042 | strncpy(clevel, buf, len); |
| 3043 | if (clevel[len-1] == '\n') |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3044 | len--; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3045 | clevel[len] = 0; |
| 3046 | if (strict_strtol(clevel, 10, &level)) |
| 3047 | level = LEVEL_NONE; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3048 | |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3049 | if (request_module("md-%s", clevel) != 0) |
| 3050 | request_module("md-level-%s", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3051 | spin_lock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3052 | pers = find_pers(level, clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3053 | if (!pers || !try_module_get(pers->owner)) { |
| 3054 | spin_unlock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3055 | printk(KERN_WARNING "md: personality %s not loaded\n", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3056 | return -EINVAL; |
| 3057 | } |
| 3058 | spin_unlock(&pers_lock); |
| 3059 | |
| 3060 | if (pers == mddev->pers) { |
| 3061 | /* Nothing to do! */ |
| 3062 | module_put(pers->owner); |
| 3063 | return rv; |
| 3064 | } |
| 3065 | if (!pers->takeover) { |
| 3066 | module_put(pers->owner); |
| 3067 | printk(KERN_WARNING "md: %s: %s does not support personality takeover\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3068 | mdname(mddev), clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3069 | return -EINVAL; |
| 3070 | } |
| 3071 | |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3072 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 3073 | rdev->new_raid_disk = rdev->raid_disk; |
| 3074 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3075 | /* ->takeover must set new_* and/or delta_disks |
| 3076 | * if it succeeds, and may set them when it fails. |
| 3077 | */ |
| 3078 | priv = pers->takeover(mddev); |
| 3079 | if (IS_ERR(priv)) { |
| 3080 | mddev->new_level = mddev->level; |
| 3081 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3082 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3083 | mddev->raid_disks -= mddev->delta_disks; |
| 3084 | mddev->delta_disks = 0; |
| 3085 | module_put(pers->owner); |
| 3086 | printk(KERN_WARNING "md: %s: %s would not accept array\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3087 | mdname(mddev), clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3088 | return PTR_ERR(priv); |
| 3089 | } |
| 3090 | |
| 3091 | /* Looks like we have a winner */ |
| 3092 | mddev_suspend(mddev); |
| 3093 | mddev->pers->stop(mddev); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 3094 | |
| 3095 | if (mddev->pers->sync_request == NULL && |
| 3096 | pers->sync_request != NULL) { |
| 3097 | /* need to add the md_redundancy_group */ |
| 3098 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 3099 | printk(KERN_WARNING |
| 3100 | "md: cannot register extra attributes for %s\n", |
| 3101 | mdname(mddev)); |
NeilBrown | 19fdb9e | 2010-05-22 08:31:36 +1000 | [diff] [blame] | 3102 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action"); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 3103 | } |
| 3104 | if (mddev->pers->sync_request != NULL && |
| 3105 | pers->sync_request == NULL) { |
| 3106 | /* need to remove the md_redundancy_group */ |
| 3107 | if (mddev->to_remove == NULL) |
| 3108 | mddev->to_remove = &md_redundancy_group; |
| 3109 | } |
| 3110 | |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 3111 | if (mddev->pers->sync_request == NULL && |
| 3112 | mddev->external) { |
| 3113 | /* We are converting from a no-redundancy array |
| 3114 | * to a redundancy array and metadata is managed |
| 3115 | * externally so we need to be sure that writes |
| 3116 | * won't block due to a need to transition |
| 3117 | * clean->dirty |
| 3118 | * until external management is started. |
| 3119 | */ |
| 3120 | mddev->in_sync = 0; |
| 3121 | mddev->safemode_delay = 0; |
| 3122 | mddev->safemode = 0; |
| 3123 | } |
| 3124 | |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3125 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 3126 | char nm[20]; |
| 3127 | if (rdev->raid_disk < 0) |
| 3128 | continue; |
NeilBrown | bf2cb0d | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 3129 | if (rdev->new_raid_disk >= mddev->raid_disks) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3130 | rdev->new_raid_disk = -1; |
| 3131 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3132 | continue; |
| 3133 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 3134 | sysfs_remove_link(&mddev->kobj, nm); |
| 3135 | } |
| 3136 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 3137 | if (rdev->raid_disk < 0) |
| 3138 | continue; |
| 3139 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3140 | continue; |
| 3141 | rdev->raid_disk = rdev->new_raid_disk; |
| 3142 | if (rdev->raid_disk < 0) |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3143 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3144 | else { |
| 3145 | char nm[20]; |
| 3146 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 3147 | if(sysfs_create_link(&mddev->kobj, &rdev->kobj, nm)) |
| 3148 | printk("md: cannot register %s for %s after level change\n", |
| 3149 | nm, mdname(mddev)); |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3150 | } |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3151 | } |
| 3152 | |
| 3153 | module_put(mddev->pers->owner); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3154 | mddev->pers = pers; |
| 3155 | mddev->private = priv; |
| 3156 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
| 3157 | mddev->level = mddev->new_level; |
| 3158 | mddev->layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3159 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3160 | mddev->delta_disks = 0; |
Trela, Maciej | 9af204c | 2010-03-08 16:02:44 +1100 | [diff] [blame] | 3161 | if (mddev->pers->sync_request == NULL) { |
| 3162 | /* this is now an array without redundancy, so |
| 3163 | * it must always be in_sync |
| 3164 | */ |
| 3165 | mddev->in_sync = 1; |
| 3166 | del_timer_sync(&mddev->safemode_timer); |
| 3167 | } |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3168 | pers->run(mddev); |
| 3169 | mddev_resume(mddev); |
| 3170 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 3171 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3172 | md_wakeup_thread(mddev->thread); |
Maciej Trela | 5cac7861 | 2010-04-14 17:17:39 +1000 | [diff] [blame] | 3173 | sysfs_notify(&mddev->kobj, NULL, "level"); |
Dan Williams | bb7f8d2 | 2010-05-01 18:14:57 -0700 | [diff] [blame] | 3174 | md_new_event(mddev); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3175 | return rv; |
| 3176 | } |
| 3177 | |
| 3178 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3179 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3180 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3181 | |
| 3182 | static ssize_t |
| 3183 | layout_show(mddev_t *mddev, char *page) |
| 3184 | { |
| 3185 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3186 | if (mddev->reshape_position != MaxSector && |
| 3187 | mddev->layout != mddev->new_layout) |
| 3188 | return sprintf(page, "%d (%d)\n", |
| 3189 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3190 | return sprintf(page, "%d\n", mddev->layout); |
| 3191 | } |
| 3192 | |
| 3193 | static ssize_t |
| 3194 | layout_store(mddev_t *mddev, const char *buf, size_t len) |
| 3195 | { |
| 3196 | char *e; |
| 3197 | unsigned long n = simple_strtoul(buf, &e, 10); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3198 | |
| 3199 | if (!*buf || (*e && *e != '\n')) |
| 3200 | return -EINVAL; |
| 3201 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3202 | if (mddev->pers) { |
| 3203 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3204 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3205 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3206 | mddev->new_layout = n; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3207 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3208 | if (err) { |
| 3209 | mddev->new_layout = mddev->layout; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3210 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3211 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3212 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3213 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3214 | if (mddev->reshape_position == MaxSector) |
| 3215 | mddev->layout = n; |
| 3216 | } |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3217 | return len; |
| 3218 | } |
| 3219 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3220 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3221 | |
| 3222 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3223 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3224 | raid_disks_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3225 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 3226 | if (mddev->raid_disks == 0) |
| 3227 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3228 | if (mddev->reshape_position != MaxSector && |
| 3229 | mddev->delta_disks != 0) |
| 3230 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 3231 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3232 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 3233 | } |
| 3234 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3235 | static int update_raid_disks(mddev_t *mddev, int raid_disks); |
| 3236 | |
| 3237 | static ssize_t |
| 3238 | raid_disks_store(mddev_t *mddev, const char *buf, size_t len) |
| 3239 | { |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3240 | char *e; |
| 3241 | int rv = 0; |
| 3242 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3243 | |
| 3244 | if (!*buf || (*e && *e != '\n')) |
| 3245 | return -EINVAL; |
| 3246 | |
| 3247 | if (mddev->pers) |
| 3248 | rv = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3249 | else if (mddev->reshape_position != MaxSector) { |
| 3250 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
| 3251 | mddev->delta_disks = n - olddisks; |
| 3252 | mddev->raid_disks = n; |
| 3253 | } else |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3254 | mddev->raid_disks = n; |
| 3255 | return rv ? rv : len; |
| 3256 | } |
| 3257 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3258 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3259 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3260 | static ssize_t |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3261 | chunk_size_show(mddev_t *mddev, char *page) |
| 3262 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3263 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3264 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 3265 | return sprintf(page, "%d (%d)\n", |
| 3266 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3267 | mddev->chunk_sectors << 9); |
| 3268 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3269 | } |
| 3270 | |
| 3271 | static ssize_t |
| 3272 | chunk_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3273 | { |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3274 | char *e; |
| 3275 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3276 | |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3277 | if (!*buf || (*e && *e != '\n')) |
| 3278 | return -EINVAL; |
| 3279 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3280 | if (mddev->pers) { |
| 3281 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3282 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3283 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3284 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3285 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3286 | if (err) { |
| 3287 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3288 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3289 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3290 | } else { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3291 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3292 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3293 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3294 | } |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3295 | return len; |
| 3296 | } |
| 3297 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3298 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3299 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3300 | static ssize_t |
| 3301 | resync_start_show(mddev_t *mddev, char *page) |
| 3302 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3303 | if (mddev->recovery_cp == MaxSector) |
| 3304 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3305 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 3306 | } |
| 3307 | |
| 3308 | static ssize_t |
| 3309 | resync_start_store(mddev_t *mddev, const char *buf, size_t len) |
| 3310 | { |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3311 | char *e; |
| 3312 | unsigned long long n = simple_strtoull(buf, &e, 10); |
| 3313 | |
| 3314 | if (mddev->pers) |
| 3315 | return -EBUSY; |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3316 | if (cmd_match(buf, "none")) |
| 3317 | n = MaxSector; |
| 3318 | else if (!*buf || (*e && *e != '\n')) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3319 | return -EINVAL; |
| 3320 | |
| 3321 | mddev->recovery_cp = n; |
| 3322 | return len; |
| 3323 | } |
| 3324 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3325 | __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3326 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3327 | /* |
| 3328 | * The array state can be: |
| 3329 | * |
| 3330 | * clear |
| 3331 | * No devices, no size, no level |
| 3332 | * Equivalent to STOP_ARRAY ioctl |
| 3333 | * inactive |
| 3334 | * May have some settings, but array is not active |
| 3335 | * all IO results in error |
| 3336 | * When written, doesn't tear down array, but just stops it |
| 3337 | * suspended (not supported yet) |
| 3338 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 3339 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3340 | * readonly |
| 3341 | * no resync can happen. no superblocks get written. |
| 3342 | * write requests fail |
| 3343 | * read-auto |
| 3344 | * like readonly, but behaves like 'clean' on a write request. |
| 3345 | * |
| 3346 | * clean - no pending writes, but otherwise active. |
| 3347 | * When written to inactive array, starts without resync |
| 3348 | * If a write request arrives then |
| 3349 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 3350 | * if not known, block and switch to write-pending |
| 3351 | * If written to an active array that has pending writes, then fails. |
| 3352 | * active |
| 3353 | * fully active: IO and resync can be happening. |
| 3354 | * When written to inactive array, starts with resync |
| 3355 | * |
| 3356 | * write-pending |
| 3357 | * clean, but writes are blocked waiting for 'active' to be written. |
| 3358 | * |
| 3359 | * active-idle |
| 3360 | * like active, but no writes have been seen for a while (100msec). |
| 3361 | * |
| 3362 | */ |
| 3363 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 3364 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3365 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3366 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 3367 | "write-pending", "active-idle", NULL }; |
| 3368 | |
| 3369 | static int match_word(const char *word, char **list) |
| 3370 | { |
| 3371 | int n; |
| 3372 | for (n=0; list[n]; n++) |
| 3373 | if (cmd_match(word, list[n])) |
| 3374 | break; |
| 3375 | return n; |
| 3376 | } |
| 3377 | |
| 3378 | static ssize_t |
| 3379 | array_state_show(mddev_t *mddev, char *page) |
| 3380 | { |
| 3381 | enum array_state st = inactive; |
| 3382 | |
| 3383 | if (mddev->pers) |
| 3384 | switch(mddev->ro) { |
| 3385 | case 1: |
| 3386 | st = readonly; |
| 3387 | break; |
| 3388 | case 2: |
| 3389 | st = read_auto; |
| 3390 | break; |
| 3391 | case 0: |
| 3392 | if (mddev->in_sync) |
| 3393 | st = clean; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3394 | else if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3395 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3396 | else if (mddev->safemode) |
| 3397 | st = active_idle; |
| 3398 | else |
| 3399 | st = active; |
| 3400 | } |
| 3401 | else { |
| 3402 | if (list_empty(&mddev->disks) && |
| 3403 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3404 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3405 | st = clear; |
| 3406 | else |
| 3407 | st = inactive; |
| 3408 | } |
| 3409 | return sprintf(page, "%s\n", array_states[st]); |
| 3410 | } |
| 3411 | |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3412 | static int do_md_stop(mddev_t * mddev, int ro, int is_open); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3413 | static int md_set_readonly(mddev_t * mddev, int is_open); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3414 | static int do_md_run(mddev_t * mddev); |
| 3415 | static int restart_array(mddev_t *mddev); |
| 3416 | |
| 3417 | static ssize_t |
| 3418 | array_state_store(mddev_t *mddev, const char *buf, size_t len) |
| 3419 | { |
| 3420 | int err = -EINVAL; |
| 3421 | enum array_state st = match_word(buf, array_states); |
| 3422 | switch(st) { |
| 3423 | case bad_word: |
| 3424 | break; |
| 3425 | case clear: |
| 3426 | /* stopping an active array */ |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3427 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3428 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3429 | err = do_md_stop(mddev, 0, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3430 | break; |
| 3431 | case inactive: |
| 3432 | /* stopping an active array */ |
| 3433 | if (mddev->pers) { |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3434 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3435 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3436 | err = do_md_stop(mddev, 2, 0); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3437 | } else |
| 3438 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3439 | break; |
| 3440 | case suspended: |
| 3441 | break; /* not supported yet */ |
| 3442 | case readonly: |
| 3443 | if (mddev->pers) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3444 | err = md_set_readonly(mddev, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3445 | else { |
| 3446 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3447 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3448 | err = do_md_run(mddev); |
| 3449 | } |
| 3450 | break; |
| 3451 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3452 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3453 | if (mddev->ro == 0) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3454 | err = md_set_readonly(mddev, 0); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3455 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3456 | err = restart_array(mddev); |
| 3457 | if (err == 0) { |
| 3458 | mddev->ro = 2; |
| 3459 | set_disk_ro(mddev->gendisk, 0); |
| 3460 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3461 | } else { |
| 3462 | mddev->ro = 2; |
| 3463 | err = do_md_run(mddev); |
| 3464 | } |
| 3465 | break; |
| 3466 | case clean: |
| 3467 | if (mddev->pers) { |
| 3468 | restart_array(mddev); |
| 3469 | spin_lock_irq(&mddev->write_lock); |
| 3470 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3471 | if (mddev->in_sync == 0) { |
| 3472 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3473 | if (mddev->safemode == 1) |
| 3474 | mddev->safemode = 0; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3475 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3476 | } |
| 3477 | err = 0; |
| 3478 | } else |
| 3479 | err = -EBUSY; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3480 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 3481 | } else |
| 3482 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3483 | break; |
| 3484 | case active: |
| 3485 | if (mddev->pers) { |
| 3486 | restart_array(mddev); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3487 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3488 | wake_up(&mddev->sb_wait); |
| 3489 | err = 0; |
| 3490 | } else { |
| 3491 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3492 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3493 | err = do_md_run(mddev); |
| 3494 | } |
| 3495 | break; |
| 3496 | case write_pending: |
| 3497 | case active_idle: |
| 3498 | /* these cannot be set */ |
| 3499 | break; |
| 3500 | } |
| 3501 | if (err) |
| 3502 | return err; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3503 | else { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 3504 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3505 | return len; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3506 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3507 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3508 | static struct md_sysfs_entry md_array_state = |
| 3509 | __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3510 | |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3511 | static ssize_t |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 3512 | max_corrected_read_errors_show(mddev_t *mddev, char *page) { |
| 3513 | return sprintf(page, "%d\n", |
| 3514 | atomic_read(&mddev->max_corr_read_errors)); |
| 3515 | } |
| 3516 | |
| 3517 | static ssize_t |
| 3518 | max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len) |
| 3519 | { |
| 3520 | char *e; |
| 3521 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3522 | |
| 3523 | if (*buf && (*e == 0 || *e == '\n')) { |
| 3524 | atomic_set(&mddev->max_corr_read_errors, n); |
| 3525 | return len; |
| 3526 | } |
| 3527 | return -EINVAL; |
| 3528 | } |
| 3529 | |
| 3530 | static struct md_sysfs_entry max_corr_read_errors = |
| 3531 | __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show, |
| 3532 | max_corrected_read_errors_store); |
| 3533 | |
| 3534 | static ssize_t |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3535 | null_show(mddev_t *mddev, char *page) |
| 3536 | { |
| 3537 | return -EINVAL; |
| 3538 | } |
| 3539 | |
| 3540 | static ssize_t |
| 3541 | new_dev_store(mddev_t *mddev, const char *buf, size_t len) |
| 3542 | { |
| 3543 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 3544 | /* The new device is added to the array. |
| 3545 | * If the array has a persistent superblock, we read the |
| 3546 | * superblock to initialise info and check validity. |
| 3547 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 3548 | * which mainly checks size. |
| 3549 | */ |
| 3550 | char *e; |
| 3551 | int major = simple_strtoul(buf, &e, 10); |
| 3552 | int minor; |
| 3553 | dev_t dev; |
| 3554 | mdk_rdev_t *rdev; |
| 3555 | int err; |
| 3556 | |
| 3557 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 3558 | return -EINVAL; |
| 3559 | minor = simple_strtoul(e+1, &e, 10); |
| 3560 | if (*e && *e != '\n') |
| 3561 | return -EINVAL; |
| 3562 | dev = MKDEV(major, minor); |
| 3563 | if (major != MAJOR(dev) || |
| 3564 | minor != MINOR(dev)) |
| 3565 | return -EOVERFLOW; |
| 3566 | |
| 3567 | |
| 3568 | if (mddev->persistent) { |
| 3569 | rdev = md_import_device(dev, mddev->major_version, |
| 3570 | mddev->minor_version); |
| 3571 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
| 3572 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 3573 | mdk_rdev_t, same_set); |
| 3574 | err = super_types[mddev->major_version] |
| 3575 | .load_super(rdev, rdev0, mddev->minor_version); |
| 3576 | if (err < 0) |
| 3577 | goto out; |
| 3578 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3579 | } else if (mddev->external) |
| 3580 | rdev = md_import_device(dev, -2, -1); |
| 3581 | else |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3582 | rdev = md_import_device(dev, -1, -1); |
| 3583 | |
| 3584 | if (IS_ERR(rdev)) |
| 3585 | return PTR_ERR(rdev); |
| 3586 | err = bind_rdev_to_array(rdev, mddev); |
| 3587 | out: |
| 3588 | if (err) |
| 3589 | export_rdev(rdev); |
| 3590 | return err ? err : len; |
| 3591 | } |
| 3592 | |
| 3593 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3594 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3595 | |
| 3596 | static ssize_t |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3597 | bitmap_store(mddev_t *mddev, const char *buf, size_t len) |
| 3598 | { |
| 3599 | char *end; |
| 3600 | unsigned long chunk, end_chunk; |
| 3601 | |
| 3602 | if (!mddev->bitmap) |
| 3603 | goto out; |
| 3604 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 3605 | while (*buf) { |
| 3606 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 3607 | if (buf == end) break; |
| 3608 | if (*end == '-') { /* range */ |
| 3609 | buf = end + 1; |
| 3610 | end_chunk = simple_strtoul(buf, &end, 0); |
| 3611 | if (buf == end) break; |
| 3612 | } |
| 3613 | if (*end && !isspace(*end)) break; |
| 3614 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 3615 | buf = skip_spaces(end); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3616 | } |
| 3617 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 3618 | out: |
| 3619 | return len; |
| 3620 | } |
| 3621 | |
| 3622 | static struct md_sysfs_entry md_bitmap = |
| 3623 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 3624 | |
| 3625 | static ssize_t |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3626 | size_show(mddev_t *mddev, char *page) |
| 3627 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3628 | return sprintf(page, "%llu\n", |
| 3629 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3630 | } |
| 3631 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3632 | static int update_size(mddev_t *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3633 | |
| 3634 | static ssize_t |
| 3635 | size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3636 | { |
| 3637 | /* If array is inactive, we can reduce the component size, but |
| 3638 | * not increase it (except from 0). |
| 3639 | * If array is active, we can try an on-line resize |
| 3640 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3641 | sector_t sectors; |
| 3642 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3643 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3644 | if (err < 0) |
| 3645 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3646 | if (mddev->pers) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3647 | err = update_size(mddev, sectors); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3648 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3649 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3650 | if (mddev->dev_sectors == 0 || |
| 3651 | mddev->dev_sectors > sectors) |
| 3652 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3653 | else |
| 3654 | err = -ENOSPC; |
| 3655 | } |
| 3656 | return err ? err : len; |
| 3657 | } |
| 3658 | |
| 3659 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3660 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3661 | |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3662 | |
| 3663 | /* Metdata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3664 | * This is one of |
| 3665 | * 'none' for arrays with no metadata (good luck...) |
| 3666 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3667 | * or N.M for internally known formats |
| 3668 | */ |
| 3669 | static ssize_t |
| 3670 | metadata_show(mddev_t *mddev, char *page) |
| 3671 | { |
| 3672 | if (mddev->persistent) |
| 3673 | return sprintf(page, "%d.%d\n", |
| 3674 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3675 | else if (mddev->external) |
| 3676 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3677 | else |
| 3678 | return sprintf(page, "none\n"); |
| 3679 | } |
| 3680 | |
| 3681 | static ssize_t |
| 3682 | metadata_store(mddev_t *mddev, const char *buf, size_t len) |
| 3683 | { |
| 3684 | int major, minor; |
| 3685 | char *e; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 3686 | /* Changing the details of 'external' metadata is |
| 3687 | * always permitted. Otherwise there must be |
| 3688 | * no devices attached to the array. |
| 3689 | */ |
| 3690 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 3691 | ; |
| 3692 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3693 | return -EBUSY; |
| 3694 | |
| 3695 | if (cmd_match(buf, "none")) { |
| 3696 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3697 | mddev->external = 0; |
| 3698 | mddev->major_version = 0; |
| 3699 | mddev->minor_version = 90; |
| 3700 | return len; |
| 3701 | } |
| 3702 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 3703 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3704 | if (namelen >= sizeof(mddev->metadata_type)) |
| 3705 | namelen = sizeof(mddev->metadata_type)-1; |
| 3706 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 3707 | mddev->metadata_type[namelen] = 0; |
| 3708 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 3709 | mddev->metadata_type[--namelen] = 0; |
| 3710 | mddev->persistent = 0; |
| 3711 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3712 | mddev->major_version = 0; |
| 3713 | mddev->minor_version = 90; |
| 3714 | return len; |
| 3715 | } |
| 3716 | major = simple_strtoul(buf, &e, 10); |
| 3717 | if (e==buf || *e != '.') |
| 3718 | return -EINVAL; |
| 3719 | buf = e+1; |
| 3720 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 3721 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3722 | return -EINVAL; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 3723 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3724 | return -ENOENT; |
| 3725 | mddev->major_version = major; |
| 3726 | mddev->minor_version = minor; |
| 3727 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3728 | mddev->external = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3729 | return len; |
| 3730 | } |
| 3731 | |
| 3732 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3733 | __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3734 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3735 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3736 | action_show(mddev_t *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3737 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3738 | char *type = "idle"; |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3739 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 3740 | type = "frozen"; |
| 3741 | else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 2b12ab6 | 2007-10-16 23:30:53 -0700 | [diff] [blame] | 3742 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3743 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 3744 | type = "reshape"; |
| 3745 | else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3746 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 3747 | type = "resync"; |
| 3748 | else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 3749 | type = "check"; |
| 3750 | else |
| 3751 | type = "repair"; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3752 | } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3753 | type = "recover"; |
| 3754 | } |
| 3755 | return sprintf(page, "%s\n", type); |
| 3756 | } |
| 3757 | |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 3758 | static void reap_sync_thread(mddev_t *mddev); |
| 3759 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3760 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3761 | action_store(mddev_t *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3762 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3763 | if (!mddev->pers || !mddev->pers->sync_request) |
| 3764 | return -EINVAL; |
| 3765 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3766 | if (cmd_match(page, "frozen")) |
| 3767 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3768 | else |
| 3769 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3770 | |
| 3771 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3772 | if (mddev->sync_thread) { |
| 3773 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 3774 | reap_sync_thread(mddev); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3775 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3776 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 3777 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3778 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3779 | else if (cmd_match(page, "resync")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3780 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3781 | else if (cmd_match(page, "recover")) { |
| 3782 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 3783 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3784 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3785 | int err; |
| 3786 | if (mddev->pers->start_reshape == NULL) |
| 3787 | return -EINVAL; |
| 3788 | err = mddev->pers->start_reshape(mddev); |
| 3789 | if (err) |
| 3790 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 3791 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3792 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 3793 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3794 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 3795 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3796 | return -EINVAL; |
| 3797 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 3798 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3799 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3800 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3801 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 3802 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3803 | return len; |
| 3804 | } |
| 3805 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3806 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3807 | mismatch_cnt_show(mddev_t *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3808 | { |
| 3809 | return sprintf(page, "%llu\n", |
| 3810 | (unsigned long long) mddev->resync_mismatches); |
| 3811 | } |
| 3812 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3813 | static struct md_sysfs_entry md_scan_mode = |
| 3814 | __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3815 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3816 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3817 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3818 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3819 | static ssize_t |
| 3820 | sync_min_show(mddev_t *mddev, char *page) |
| 3821 | { |
| 3822 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 3823 | mddev->sync_speed_min ? "local": "system"); |
| 3824 | } |
| 3825 | |
| 3826 | static ssize_t |
| 3827 | sync_min_store(mddev_t *mddev, const char *buf, size_t len) |
| 3828 | { |
| 3829 | int min; |
| 3830 | char *e; |
| 3831 | if (strncmp(buf, "system", 6)==0) { |
| 3832 | mddev->sync_speed_min = 0; |
| 3833 | return len; |
| 3834 | } |
| 3835 | min = simple_strtoul(buf, &e, 10); |
| 3836 | if (buf == e || (*e && *e != '\n') || min <= 0) |
| 3837 | return -EINVAL; |
| 3838 | mddev->sync_speed_min = min; |
| 3839 | return len; |
| 3840 | } |
| 3841 | |
| 3842 | static struct md_sysfs_entry md_sync_min = |
| 3843 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 3844 | |
| 3845 | static ssize_t |
| 3846 | sync_max_show(mddev_t *mddev, char *page) |
| 3847 | { |
| 3848 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 3849 | mddev->sync_speed_max ? "local": "system"); |
| 3850 | } |
| 3851 | |
| 3852 | static ssize_t |
| 3853 | sync_max_store(mddev_t *mddev, const char *buf, size_t len) |
| 3854 | { |
| 3855 | int max; |
| 3856 | char *e; |
| 3857 | if (strncmp(buf, "system", 6)==0) { |
| 3858 | mddev->sync_speed_max = 0; |
| 3859 | return len; |
| 3860 | } |
| 3861 | max = simple_strtoul(buf, &e, 10); |
| 3862 | if (buf == e || (*e && *e != '\n') || max <= 0) |
| 3863 | return -EINVAL; |
| 3864 | mddev->sync_speed_max = max; |
| 3865 | return len; |
| 3866 | } |
| 3867 | |
| 3868 | static struct md_sysfs_entry md_sync_max = |
| 3869 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 3870 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 3871 | static ssize_t |
| 3872 | degraded_show(mddev_t *mddev, char *page) |
| 3873 | { |
| 3874 | return sprintf(page, "%d\n", mddev->degraded); |
| 3875 | } |
| 3876 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3877 | |
| 3878 | static ssize_t |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 3879 | sync_force_parallel_show(mddev_t *mddev, char *page) |
| 3880 | { |
| 3881 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 3882 | } |
| 3883 | |
| 3884 | static ssize_t |
| 3885 | sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len) |
| 3886 | { |
| 3887 | long n; |
| 3888 | |
| 3889 | if (strict_strtol(buf, 10, &n)) |
| 3890 | return -EINVAL; |
| 3891 | |
| 3892 | if (n != 0 && n != 1) |
| 3893 | return -EINVAL; |
| 3894 | |
| 3895 | mddev->parallel_resync = n; |
| 3896 | |
| 3897 | if (mddev->sync_thread) |
| 3898 | wake_up(&resync_wait); |
| 3899 | |
| 3900 | return len; |
| 3901 | } |
| 3902 | |
| 3903 | /* force parallel resync, even with shared block devices */ |
| 3904 | static struct md_sysfs_entry md_sync_force_parallel = |
| 3905 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 3906 | sync_force_parallel_show, sync_force_parallel_store); |
| 3907 | |
| 3908 | static ssize_t |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3909 | sync_speed_show(mddev_t *mddev, char *page) |
| 3910 | { |
| 3911 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3912 | if (mddev->curr_resync == 0) |
| 3913 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3914 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 3915 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3916 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3917 | db = resync - mddev->resync_mark_cnt; |
| 3918 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3919 | } |
| 3920 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3921 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3922 | |
| 3923 | static ssize_t |
| 3924 | sync_completed_show(mddev_t *mddev, char *page) |
| 3925 | { |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 3926 | unsigned long long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3927 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 3928 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3929 | return sprintf(page, "none\n"); |
| 3930 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3931 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3932 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3933 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3934 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3935 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 3936 | resync = mddev->curr_resync_completed; |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 3937 | return sprintf(page, "%llu / %llu\n", resync, max_sectors); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3938 | } |
| 3939 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3940 | static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3941 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3942 | static ssize_t |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3943 | min_sync_show(mddev_t *mddev, char *page) |
| 3944 | { |
| 3945 | return sprintf(page, "%llu\n", |
| 3946 | (unsigned long long)mddev->resync_min); |
| 3947 | } |
| 3948 | static ssize_t |
| 3949 | min_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3950 | { |
| 3951 | unsigned long long min; |
| 3952 | if (strict_strtoull(buf, 10, &min)) |
| 3953 | return -EINVAL; |
| 3954 | if (min > mddev->resync_max) |
| 3955 | return -EINVAL; |
| 3956 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3957 | return -EBUSY; |
| 3958 | |
| 3959 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3960 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 3961 | sector_t temp = min; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3962 | if (sector_div(temp, mddev->chunk_sectors)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3963 | return -EINVAL; |
| 3964 | } |
| 3965 | mddev->resync_min = min; |
| 3966 | |
| 3967 | return len; |
| 3968 | } |
| 3969 | |
| 3970 | static struct md_sysfs_entry md_min_sync = |
| 3971 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 3972 | |
| 3973 | static ssize_t |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3974 | max_sync_show(mddev_t *mddev, char *page) |
| 3975 | { |
| 3976 | if (mddev->resync_max == MaxSector) |
| 3977 | return sprintf(page, "max\n"); |
| 3978 | else |
| 3979 | return sprintf(page, "%llu\n", |
| 3980 | (unsigned long long)mddev->resync_max); |
| 3981 | } |
| 3982 | static ssize_t |
| 3983 | max_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3984 | { |
| 3985 | if (strncmp(buf, "max", 3) == 0) |
| 3986 | mddev->resync_max = MaxSector; |
| 3987 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3988 | unsigned long long max; |
| 3989 | if (strict_strtoull(buf, 10, &max)) |
| 3990 | return -EINVAL; |
| 3991 | if (max < mddev->resync_min) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3992 | return -EINVAL; |
| 3993 | if (max < mddev->resync_max && |
NeilBrown | 4d484a4 | 2009-08-13 10:41:50 +1000 | [diff] [blame] | 3994 | mddev->ro == 0 && |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3995 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3996 | return -EBUSY; |
| 3997 | |
| 3998 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3999 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 4000 | sector_t temp = max; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4001 | if (sector_div(temp, mddev->chunk_sectors)) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4002 | return -EINVAL; |
| 4003 | } |
| 4004 | mddev->resync_max = max; |
| 4005 | } |
| 4006 | wake_up(&mddev->recovery_wait); |
| 4007 | return len; |
| 4008 | } |
| 4009 | |
| 4010 | static struct md_sysfs_entry md_max_sync = |
| 4011 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 4012 | |
| 4013 | static ssize_t |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4014 | suspend_lo_show(mddev_t *mddev, char *page) |
| 4015 | { |
| 4016 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 4017 | } |
| 4018 | |
| 4019 | static ssize_t |
| 4020 | suspend_lo_store(mddev_t *mddev, const char *buf, size_t len) |
| 4021 | { |
| 4022 | char *e; |
| 4023 | unsigned long long new = simple_strtoull(buf, &e, 10); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4024 | unsigned long long old = mddev->suspend_lo; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4025 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 4026 | if (mddev->pers == NULL || |
| 4027 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4028 | return -EINVAL; |
| 4029 | if (buf == e || (*e && *e != '\n')) |
| 4030 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4031 | |
| 4032 | mddev->suspend_lo = new; |
| 4033 | if (new >= old) |
| 4034 | /* Shrinking suspended region */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4035 | mddev->pers->quiesce(mddev, 2); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4036 | else { |
| 4037 | /* Expanding suspended region - need to wait */ |
| 4038 | mddev->pers->quiesce(mddev, 1); |
| 4039 | mddev->pers->quiesce(mddev, 0); |
| 4040 | } |
| 4041 | return len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4042 | } |
| 4043 | static struct md_sysfs_entry md_suspend_lo = |
| 4044 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 4045 | |
| 4046 | |
| 4047 | static ssize_t |
| 4048 | suspend_hi_show(mddev_t *mddev, char *page) |
| 4049 | { |
| 4050 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 4051 | } |
| 4052 | |
| 4053 | static ssize_t |
| 4054 | suspend_hi_store(mddev_t *mddev, const char *buf, size_t len) |
| 4055 | { |
| 4056 | char *e; |
| 4057 | unsigned long long new = simple_strtoull(buf, &e, 10); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4058 | unsigned long long old = mddev->suspend_hi; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4059 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 4060 | if (mddev->pers == NULL || |
| 4061 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4062 | return -EINVAL; |
| 4063 | if (buf == e || (*e && *e != '\n')) |
| 4064 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4065 | |
| 4066 | mddev->suspend_hi = new; |
| 4067 | if (new <= old) |
| 4068 | /* Shrinking suspended region */ |
| 4069 | mddev->pers->quiesce(mddev, 2); |
| 4070 | else { |
| 4071 | /* Expanding suspended region - need to wait */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4072 | mddev->pers->quiesce(mddev, 1); |
| 4073 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4074 | } |
| 4075 | return len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4076 | } |
| 4077 | static struct md_sysfs_entry md_suspend_hi = |
| 4078 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 4079 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4080 | static ssize_t |
| 4081 | reshape_position_show(mddev_t *mddev, char *page) |
| 4082 | { |
| 4083 | if (mddev->reshape_position != MaxSector) |
| 4084 | return sprintf(page, "%llu\n", |
| 4085 | (unsigned long long)mddev->reshape_position); |
| 4086 | strcpy(page, "none\n"); |
| 4087 | return 5; |
| 4088 | } |
| 4089 | |
| 4090 | static ssize_t |
| 4091 | reshape_position_store(mddev_t *mddev, const char *buf, size_t len) |
| 4092 | { |
| 4093 | char *e; |
| 4094 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 4095 | if (mddev->pers) |
| 4096 | return -EBUSY; |
| 4097 | if (buf == e || (*e && *e != '\n')) |
| 4098 | return -EINVAL; |
| 4099 | mddev->reshape_position = new; |
| 4100 | mddev->delta_disks = 0; |
| 4101 | mddev->new_level = mddev->level; |
| 4102 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4103 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4104 | return len; |
| 4105 | } |
| 4106 | |
| 4107 | static struct md_sysfs_entry md_reshape_position = |
| 4108 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 4109 | reshape_position_store); |
| 4110 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4111 | static ssize_t |
| 4112 | array_size_show(mddev_t *mddev, char *page) |
| 4113 | { |
| 4114 | if (mddev->external_size) |
| 4115 | return sprintf(page, "%llu\n", |
| 4116 | (unsigned long long)mddev->array_sectors/2); |
| 4117 | else |
| 4118 | return sprintf(page, "default\n"); |
| 4119 | } |
| 4120 | |
| 4121 | static ssize_t |
| 4122 | array_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 4123 | { |
| 4124 | sector_t sectors; |
| 4125 | |
| 4126 | if (strncmp(buf, "default", 7) == 0) { |
| 4127 | if (mddev->pers) |
| 4128 | sectors = mddev->pers->size(mddev, 0, 0); |
| 4129 | else |
| 4130 | sectors = mddev->array_sectors; |
| 4131 | |
| 4132 | mddev->external_size = 0; |
| 4133 | } else { |
| 4134 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
| 4135 | return -EINVAL; |
| 4136 | if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
NeilBrown | 2b69c83 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4137 | return -E2BIG; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4138 | |
| 4139 | mddev->external_size = 1; |
| 4140 | } |
| 4141 | |
| 4142 | mddev->array_sectors = sectors; |
NeilBrown | cbe6ef1 | 2011-02-16 13:58:38 +1100 | [diff] [blame] | 4143 | if (mddev->pers) { |
| 4144 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 4145 | revalidate_disk(mddev->gendisk); |
NeilBrown | cbe6ef1 | 2011-02-16 13:58:38 +1100 | [diff] [blame] | 4146 | } |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4147 | return len; |
| 4148 | } |
| 4149 | |
| 4150 | static struct md_sysfs_entry md_array_size = |
| 4151 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 4152 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4153 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4154 | static struct attribute *md_default_attrs[] = { |
| 4155 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4156 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4157 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4158 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4159 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4160 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4161 | &md_metadata.attr, |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4162 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4163 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4164 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4165 | &md_reshape_position.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4166 | &md_array_size.attr, |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4167 | &max_corr_read_errors.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4168 | NULL, |
| 4169 | }; |
| 4170 | |
| 4171 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4172 | &md_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4173 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4174 | &md_sync_min.attr, |
| 4175 | &md_sync_max.attr, |
| 4176 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4177 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4178 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4179 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4180 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4181 | &md_suspend_lo.attr, |
| 4182 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4183 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4184 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4185 | NULL, |
| 4186 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4187 | static struct attribute_group md_redundancy_group = { |
| 4188 | .name = NULL, |
| 4189 | .attrs = md_redundancy_attrs, |
| 4190 | }; |
| 4191 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4192 | |
| 4193 | static ssize_t |
| 4194 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 4195 | { |
| 4196 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 4197 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4198 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4199 | |
| 4200 | if (!entry->show) |
| 4201 | return -EIO; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4202 | rv = mddev_lock(mddev); |
| 4203 | if (!rv) { |
| 4204 | rv = entry->show(mddev, page); |
| 4205 | mddev_unlock(mddev); |
| 4206 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4207 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4208 | } |
| 4209 | |
| 4210 | static ssize_t |
| 4211 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 4212 | const char *page, size_t length) |
| 4213 | { |
| 4214 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 4215 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4216 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4217 | |
| 4218 | if (!entry->store) |
| 4219 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 4220 | if (!capable(CAP_SYS_ADMIN)) |
| 4221 | return -EACCES; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4222 | rv = mddev_lock(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4223 | if (mddev->hold_active == UNTIL_IOCTL) |
| 4224 | mddev->hold_active = 0; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4225 | if (!rv) { |
| 4226 | rv = entry->store(mddev, page, length); |
| 4227 | mddev_unlock(mddev); |
| 4228 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4229 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4230 | } |
| 4231 | |
| 4232 | static void md_free(struct kobject *ko) |
| 4233 | { |
| 4234 | mddev_t *mddev = container_of(ko, mddev_t, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4235 | |
| 4236 | if (mddev->sysfs_state) |
| 4237 | sysfs_put(mddev->sysfs_state); |
| 4238 | |
| 4239 | if (mddev->gendisk) { |
| 4240 | del_gendisk(mddev->gendisk); |
| 4241 | put_disk(mddev->gendisk); |
| 4242 | } |
| 4243 | if (mddev->queue) |
| 4244 | blk_cleanup_queue(mddev->queue); |
| 4245 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4246 | kfree(mddev); |
| 4247 | } |
| 4248 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 4249 | static const struct sysfs_ops md_sysfs_ops = { |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4250 | .show = md_attr_show, |
| 4251 | .store = md_attr_store, |
| 4252 | }; |
| 4253 | static struct kobj_type md_ktype = { |
| 4254 | .release = md_free, |
| 4255 | .sysfs_ops = &md_sysfs_ops, |
| 4256 | .default_attrs = md_default_attrs, |
| 4257 | }; |
| 4258 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4259 | int mdp_major = 0; |
| 4260 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4261 | static void mddev_delayed_delete(struct work_struct *ws) |
| 4262 | { |
| 4263 | mddev_t *mddev = container_of(ws, mddev_t, del_work); |
| 4264 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4265 | sysfs_remove_group(&mddev->kobj, &md_bitmap_group); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4266 | kobject_del(&mddev->kobj); |
| 4267 | kobject_put(&mddev->kobj); |
| 4268 | } |
| 4269 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4270 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4271 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4272 | static DEFINE_MUTEX(disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4273 | mddev_t *mddev = mddev_find(dev); |
| 4274 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4275 | int partitioned; |
| 4276 | int shift; |
| 4277 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4278 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4279 | |
| 4280 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4281 | return -ENODEV; |
| 4282 | |
| 4283 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 4284 | shift = partitioned ? MdpMinorShift : 0; |
| 4285 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4286 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4287 | /* wait for any previous instance of this device to be |
| 4288 | * completely removed (mddev_delayed_delete). |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4289 | */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4290 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4291 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4292 | mutex_lock(&disks_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4293 | error = -EEXIST; |
| 4294 | if (mddev->gendisk) |
| 4295 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4296 | |
| 4297 | if (name) { |
| 4298 | /* Need to ensure that 'name' is not a duplicate. |
| 4299 | */ |
| 4300 | mddev_t *mddev2; |
| 4301 | spin_lock(&all_mddevs_lock); |
| 4302 | |
| 4303 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 4304 | if (mddev2->gendisk && |
| 4305 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 4306 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4307 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4308 | } |
| 4309 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4310 | } |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4311 | |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4312 | error = -ENOMEM; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4313 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4314 | if (!mddev->queue) |
| 4315 | goto abort; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4316 | mddev->queue->queuedata = mddev; |
| 4317 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4318 | blk_queue_make_request(mddev->queue, md_make_request); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4319 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4320 | disk = alloc_disk(1 << shift); |
| 4321 | if (!disk) { |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4322 | blk_cleanup_queue(mddev->queue); |
| 4323 | mddev->queue = NULL; |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4324 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4325 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4326 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4327 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4328 | if (name) |
| 4329 | strcpy(disk->disk_name, name); |
| 4330 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4331 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 4332 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4333 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4334 | disk->fops = &md_fops; |
| 4335 | disk->private_data = mddev; |
| 4336 | disk->queue = mddev->queue; |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4337 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4338 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4339 | * remove it now. |
| 4340 | */ |
| 4341 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4342 | add_disk(disk); |
| 4343 | mddev->gendisk = disk; |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 4344 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
| 4345 | &disk_to_dev(disk)->kobj, "%s", "md"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4346 | if (error) { |
| 4347 | /* This isn't possible, but as kobject_init_and_add is marked |
| 4348 | * __must_check, we must do something with the result |
| 4349 | */ |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4350 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 4351 | disk->disk_name); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4352 | error = 0; |
| 4353 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4354 | if (mddev->kobj.sd && |
| 4355 | sysfs_create_group(&mddev->kobj, &md_bitmap_group)) |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4356 | printk(KERN_DEBUG "pointless warning\n"); |
Darrick J. Wong | be20e6c | 2010-11-24 16:40:33 +1100 | [diff] [blame] | 4357 | |
| 4358 | blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4359 | abort: |
| 4360 | mutex_unlock(&disks_mutex); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4361 | if (!error && mddev->kobj.sd) { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4362 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4363 | mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state"); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4364 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4365 | mddev_put(mddev); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4366 | return error; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4367 | } |
| 4368 | |
| 4369 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 4370 | { |
| 4371 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4372 | return NULL; |
| 4373 | } |
| 4374 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4375 | static int add_named_array(const char *val, struct kernel_param *kp) |
| 4376 | { |
| 4377 | /* val must be "md_*" where * is not all digits. |
| 4378 | * We allocate an array with a large free minor number, and |
| 4379 | * set the name to val. val must not already be an active name. |
| 4380 | */ |
| 4381 | int len = strlen(val); |
| 4382 | char buf[DISK_NAME_LEN]; |
| 4383 | |
| 4384 | while (len && val[len-1] == '\n') |
| 4385 | len--; |
| 4386 | if (len >= DISK_NAME_LEN) |
| 4387 | return -E2BIG; |
| 4388 | strlcpy(buf, val, len+1); |
| 4389 | if (strncmp(buf, "md_", 3) != 0) |
| 4390 | return -EINVAL; |
| 4391 | return md_alloc(0, buf); |
| 4392 | } |
| 4393 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4394 | static void md_safemode_timeout(unsigned long data) |
| 4395 | { |
| 4396 | mddev_t *mddev = (mddev_t *) data; |
| 4397 | |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4398 | if (!atomic_read(&mddev->writes_pending)) { |
| 4399 | mddev->safemode = 1; |
| 4400 | if (mddev->external) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4401 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4402 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4403 | md_wakeup_thread(mddev->thread); |
| 4404 | } |
| 4405 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4406 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4407 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4408 | int md_run(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4409 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4410 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4411 | mdk_rdev_t *rdev; |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4412 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4413 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4414 | if (list_empty(&mddev->disks)) |
| 4415 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4416 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4417 | |
| 4418 | if (mddev->pers) |
| 4419 | return -EBUSY; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 4420 | /* Cannot run until previous stop completes properly */ |
| 4421 | if (mddev->sysfs_active) |
| 4422 | return -EBUSY; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 4423 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4424 | /* |
| 4425 | * Analyze all RAID superblock(s) |
| 4426 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4427 | if (!mddev->raid_disks) { |
| 4428 | if (!mddev->persistent) |
| 4429 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4430 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4431 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4432 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4433 | if (mddev->level != LEVEL_NONE) |
| 4434 | request_module("md-level-%d", mddev->level); |
| 4435 | else if (mddev->clevel[0]) |
| 4436 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4437 | |
| 4438 | /* |
| 4439 | * Drop all container device buffers, from now on |
| 4440 | * the only valid external interface is through the md |
| 4441 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4442 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4443 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4444 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4445 | continue; |
| 4446 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 4447 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4448 | |
| 4449 | /* perform some consistency tests on the device. |
| 4450 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4451 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4452 | */ |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4453 | if (rdev->meta_bdev) { |
| 4454 | /* Nothing to check */; |
| 4455 | } else if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4456 | if (mddev->dev_sectors && |
| 4457 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4458 | > rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4459 | printk("md: %s: data overlaps metadata\n", |
| 4460 | mdname(mddev)); |
| 4461 | return -EINVAL; |
| 4462 | } |
| 4463 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4464 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4465 | > rdev->data_offset) { |
| 4466 | printk("md: %s: metadata overlaps data\n", |
| 4467 | mdname(mddev)); |
| 4468 | return -EINVAL; |
| 4469 | } |
| 4470 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4471 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4472 | } |
| 4473 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 4474 | if (mddev->bio_set == NULL) |
| 4475 | mddev->bio_set = bioset_create(BIO_POOL_SIZE, sizeof(mddev)); |
| 4476 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4477 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4478 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4479 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4480 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4481 | if (mddev->level != LEVEL_NONE) |
| 4482 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 4483 | mddev->level); |
| 4484 | else |
| 4485 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 4486 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4487 | return -EINVAL; |
| 4488 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4489 | mddev->pers = pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4490 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4491 | if (mddev->level != pers->level) { |
| 4492 | mddev->level = pers->level; |
| 4493 | mddev->new_level = pers->level; |
| 4494 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4495 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4496 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4497 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4498 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4499 | /* This personality cannot handle reshaping... */ |
| 4500 | mddev->pers = NULL; |
| 4501 | module_put(pers->owner); |
| 4502 | return -EINVAL; |
| 4503 | } |
| 4504 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4505 | if (pers->sync_request) { |
| 4506 | /* Warn if this is a potentially silly |
| 4507 | * configuration. |
| 4508 | */ |
| 4509 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 4510 | mdk_rdev_t *rdev2; |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4511 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4512 | |
| 4513 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4514 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4515 | if (rdev < rdev2 && |
| 4516 | rdev->bdev->bd_contains == |
| 4517 | rdev2->bdev->bd_contains) { |
| 4518 | printk(KERN_WARNING |
| 4519 | "%s: WARNING: %s appears to be" |
| 4520 | " on the same physical disk as" |
| 4521 | " %s.\n", |
| 4522 | mdname(mddev), |
| 4523 | bdevname(rdev->bdev,b), |
| 4524 | bdevname(rdev2->bdev,b2)); |
| 4525 | warned = 1; |
| 4526 | } |
| 4527 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4528 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4529 | if (warned) |
| 4530 | printk(KERN_WARNING |
| 4531 | "True protection against single-disk" |
| 4532 | " failure might be compromised.\n"); |
| 4533 | } |
| 4534 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 4535 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4536 | /* may be over-ridden by personality */ |
| 4537 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 4538 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4539 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4540 | |
NeilBrown | 0f9552b | 2009-12-30 12:08:50 +1100 | [diff] [blame] | 4541 | if (start_readonly && mddev->ro == 0) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4542 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 4543 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4544 | err = mddev->pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 4545 | if (err) |
| 4546 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4547 | else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) { |
| 4548 | WARN_ONCE(!mddev->external_size, "%s: default size too small," |
| 4549 | " but 'external_size' not in effect?\n", __func__); |
| 4550 | printk(KERN_ERR |
| 4551 | "md: invalid array_size %llu > default size %llu\n", |
| 4552 | (unsigned long long)mddev->array_sectors / 2, |
| 4553 | (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2); |
| 4554 | err = -EINVAL; |
| 4555 | mddev->pers->stop(mddev); |
| 4556 | } |
| 4557 | if (err == 0 && mddev->pers->sync_request) { |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4558 | err = bitmap_create(mddev); |
| 4559 | if (err) { |
| 4560 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 4561 | mdname(mddev), err); |
| 4562 | mddev->pers->stop(mddev); |
| 4563 | } |
| 4564 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4565 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4566 | module_put(mddev->pers->owner); |
| 4567 | mddev->pers = NULL; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4568 | bitmap_destroy(mddev); |
| 4569 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4570 | } |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4571 | if (mddev->pers->sync_request) { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4572 | if (mddev->kobj.sd && |
| 4573 | sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4574 | printk(KERN_WARNING |
| 4575 | "md: cannot register extra attributes for %s\n", |
| 4576 | mdname(mddev)); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4577 | mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4578 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 4579 | mddev->ro = 0; |
| 4580 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4581 | atomic_set(&mddev->writes_pending,0); |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4582 | atomic_set(&mddev->max_corr_read_errors, |
| 4583 | MD_DEFAULT_MAX_CORRECTED_READ_ERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4584 | mddev->safemode = 0; |
| 4585 | mddev->safemode_timer.function = md_safemode_timeout; |
| 4586 | mddev->safemode_timer.data = (unsigned long) mddev; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4587 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4588 | mddev->in_sync = 1; |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4589 | smp_wmb(); |
| 4590 | mddev->ready = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4591 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4592 | if (rdev->raid_disk >= 0) { |
| 4593 | char nm[20]; |
| 4594 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4595 | if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4596 | /* failure here is OK */; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4597 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4598 | |
| 4599 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4600 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4601 | if (mddev->flags) |
| 4602 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4603 | |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 4604 | md_wakeup_thread(mddev->thread); |
| 4605 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4606 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4607 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4608 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
| 4609 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4610 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4611 | return 0; |
| 4612 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4613 | EXPORT_SYMBOL_GPL(md_run); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4614 | |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 4615 | static int do_md_run(mddev_t *mddev) |
| 4616 | { |
| 4617 | int err; |
| 4618 | |
| 4619 | err = md_run(mddev); |
| 4620 | if (err) |
| 4621 | goto out; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 4622 | err = bitmap_load(mddev); |
| 4623 | if (err) { |
| 4624 | bitmap_destroy(mddev); |
| 4625 | goto out; |
| 4626 | } |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 4627 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 4628 | revalidate_disk(mddev->gendisk); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 4629 | mddev->changed = 1; |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 4630 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
| 4631 | out: |
| 4632 | return err; |
| 4633 | } |
| 4634 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4635 | static int restart_array(mddev_t *mddev) |
| 4636 | { |
| 4637 | struct gendisk *disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4638 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4639 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4640 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4641 | return -ENXIO; |
| 4642 | if (!mddev->pers) |
| 4643 | return -EINVAL; |
| 4644 | if (!mddev->ro) |
| 4645 | return -EBUSY; |
| 4646 | mddev->safemode = 0; |
| 4647 | mddev->ro = 0; |
| 4648 | set_disk_ro(disk, 0); |
| 4649 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 4650 | mdname(mddev)); |
| 4651 | /* Kick recovery or resync if necessary */ |
| 4652 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4653 | md_wakeup_thread(mddev->thread); |
| 4654 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4655 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4656 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4657 | } |
| 4658 | |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4659 | /* similar to deny_write_access, but accounts for our holding a reference |
| 4660 | * to the file ourselves */ |
| 4661 | static int deny_bitmap_write_access(struct file * file) |
| 4662 | { |
| 4663 | struct inode *inode = file->f_mapping->host; |
| 4664 | |
| 4665 | spin_lock(&inode->i_lock); |
| 4666 | if (atomic_read(&inode->i_writecount) > 1) { |
| 4667 | spin_unlock(&inode->i_lock); |
| 4668 | return -ETXTBSY; |
| 4669 | } |
| 4670 | atomic_set(&inode->i_writecount, -1); |
| 4671 | spin_unlock(&inode->i_lock); |
| 4672 | |
| 4673 | return 0; |
| 4674 | } |
| 4675 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4676 | void restore_bitmap_write_access(struct file *file) |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4677 | { |
| 4678 | struct inode *inode = file->f_mapping->host; |
| 4679 | |
| 4680 | spin_lock(&inode->i_lock); |
| 4681 | atomic_set(&inode->i_writecount, 1); |
| 4682 | spin_unlock(&inode->i_lock); |
| 4683 | } |
| 4684 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4685 | static void md_clean(mddev_t *mddev) |
| 4686 | { |
| 4687 | mddev->array_sectors = 0; |
| 4688 | mddev->external_size = 0; |
| 4689 | mddev->dev_sectors = 0; |
| 4690 | mddev->raid_disks = 0; |
| 4691 | mddev->recovery_cp = 0; |
| 4692 | mddev->resync_min = 0; |
| 4693 | mddev->resync_max = MaxSector; |
| 4694 | mddev->reshape_position = MaxSector; |
| 4695 | mddev->external = 0; |
| 4696 | mddev->persistent = 0; |
| 4697 | mddev->level = LEVEL_NONE; |
| 4698 | mddev->clevel[0] = 0; |
| 4699 | mddev->flags = 0; |
| 4700 | mddev->ro = 0; |
| 4701 | mddev->metadata_type[0] = 0; |
| 4702 | mddev->chunk_sectors = 0; |
| 4703 | mddev->ctime = mddev->utime = 0; |
| 4704 | mddev->layout = 0; |
| 4705 | mddev->max_disks = 0; |
| 4706 | mddev->events = 0; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 4707 | mddev->can_decrease_events = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4708 | mddev->delta_disks = 0; |
| 4709 | mddev->new_level = LEVEL_NONE; |
| 4710 | mddev->new_layout = 0; |
| 4711 | mddev->new_chunk_sectors = 0; |
| 4712 | mddev->curr_resync = 0; |
| 4713 | mddev->resync_mismatches = 0; |
| 4714 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 4715 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 4716 | mddev->recovery = 0; |
| 4717 | mddev->in_sync = 0; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 4718 | mddev->changed = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4719 | mddev->degraded = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4720 | mddev->safemode = 0; |
| 4721 | mddev->bitmap_info.offset = 0; |
| 4722 | mddev->bitmap_info.default_offset = 0; |
| 4723 | mddev->bitmap_info.chunksize = 0; |
| 4724 | mddev->bitmap_info.daemon_sleep = 0; |
| 4725 | mddev->bitmap_info.max_write_behind = 0; |
NeilBrown | 252ac52 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 4726 | mddev->plug = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4727 | } |
| 4728 | |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4729 | static void __md_stop_writes(mddev_t *mddev) |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 4730 | { |
| 4731 | if (mddev->sync_thread) { |
| 4732 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4733 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4734 | reap_sync_thread(mddev); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 4735 | } |
| 4736 | |
| 4737 | del_timer_sync(&mddev->safemode_timer); |
| 4738 | |
| 4739 | bitmap_flush(mddev); |
| 4740 | md_super_wait(mddev); |
| 4741 | |
| 4742 | if (!mddev->in_sync || mddev->flags) { |
| 4743 | /* mark array as shutdown cleanly */ |
| 4744 | mddev->in_sync = 1; |
| 4745 | md_update_sb(mddev, 1); |
| 4746 | } |
| 4747 | } |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4748 | |
| 4749 | void md_stop_writes(mddev_t *mddev) |
| 4750 | { |
| 4751 | mddev_lock(mddev); |
| 4752 | __md_stop_writes(mddev); |
| 4753 | mddev_unlock(mddev); |
| 4754 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4755 | EXPORT_SYMBOL_GPL(md_stop_writes); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 4756 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4757 | void md_stop(mddev_t *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4758 | { |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4759 | mddev->ready = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4760 | mddev->pers->stop(mddev); |
| 4761 | if (mddev->pers->sync_request && mddev->to_remove == NULL) |
| 4762 | mddev->to_remove = &md_redundancy_group; |
| 4763 | module_put(mddev->pers->owner); |
| 4764 | mddev->pers = NULL; |
NeilBrown | cca9cf9 | 2010-04-01 12:08:16 +1100 | [diff] [blame] | 4765 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4766 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4767 | EXPORT_SYMBOL_GPL(md_stop); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4768 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4769 | static int md_set_readonly(mddev_t *mddev, int is_open) |
| 4770 | { |
| 4771 | int err = 0; |
| 4772 | mutex_lock(&mddev->open_mutex); |
| 4773 | if (atomic_read(&mddev->openers) > is_open) { |
| 4774 | printk("md: %s still in use.\n",mdname(mddev)); |
| 4775 | err = -EBUSY; |
| 4776 | goto out; |
| 4777 | } |
| 4778 | if (mddev->pers) { |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4779 | __md_stop_writes(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4780 | |
| 4781 | err = -ENXIO; |
| 4782 | if (mddev->ro==1) |
| 4783 | goto out; |
| 4784 | mddev->ro = 1; |
| 4785 | set_disk_ro(mddev->gendisk, 1); |
| 4786 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4787 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4788 | err = 0; |
| 4789 | } |
| 4790 | out: |
| 4791 | mutex_unlock(&mddev->open_mutex); |
| 4792 | return err; |
| 4793 | } |
| 4794 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4795 | /* mode: |
| 4796 | * 0 - completely stop and dis-assemble array |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4797 | * 2 - stop but do not disassemble array |
| 4798 | */ |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4799 | 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] | 4800 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4801 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 4802 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4803 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 4804 | mutex_lock(&mddev->open_mutex); |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 4805 | if (atomic_read(&mddev->openers) > is_open || |
| 4806 | mddev->sysfs_active) { |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4807 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 4808 | mutex_unlock(&mddev->open_mutex); |
| 4809 | return -EBUSY; |
| 4810 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4811 | |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 4812 | if (mddev->pers) { |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4813 | if (mddev->ro) |
| 4814 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4815 | |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4816 | __md_stop_writes(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4817 | md_stop(mddev); |
| 4818 | mddev->queue->merge_bvec_fn = NULL; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4819 | mddev->queue->backing_dev_info.congested_fn = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4820 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4821 | /* tell userspace to handle 'inactive' */ |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4822 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4823 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4824 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4825 | if (rdev->raid_disk >= 0) { |
| 4826 | char nm[20]; |
| 4827 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 4828 | sysfs_remove_link(&mddev->kobj, nm); |
| 4829 | } |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 4830 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4831 | set_capacity(disk, 0); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 4832 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 4833 | mddev->changed = 1; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4834 | revalidate_disk(disk); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4835 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4836 | if (mddev->ro) |
| 4837 | mddev->ro = 0; |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 4838 | } else |
| 4839 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4840 | /* |
| 4841 | * Free resources if final stop |
| 4842 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4843 | if (mode == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4844 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 4845 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4846 | bitmap_destroy(mddev); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4847 | if (mddev->bitmap_info.file) { |
| 4848 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 4849 | fput(mddev->bitmap_info.file); |
| 4850 | mddev->bitmap_info.file = NULL; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4851 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4852 | mddev->bitmap_info.offset = 0; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4853 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4854 | export_array(mddev); |
| 4855 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4856 | md_clean(mddev); |
NeilBrown | 934d9c2 | 2008-10-28 17:01:23 +1100 | [diff] [blame] | 4857 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4858 | if (mddev->hold_active == UNTIL_STOP) |
| 4859 | mddev->hold_active = 0; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4860 | } |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 4861 | blk_integrity_unregister(disk); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4862 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4863 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 4864 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4865 | } |
| 4866 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4867 | #ifndef MODULE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4868 | static void autorun_array(mddev_t *mddev) |
| 4869 | { |
| 4870 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4871 | int err; |
| 4872 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4873 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4874 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4875 | |
| 4876 | printk(KERN_INFO "md: running: "); |
| 4877 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4878 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4879 | char b[BDEVNAME_SIZE]; |
| 4880 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 4881 | } |
| 4882 | printk("\n"); |
| 4883 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4884 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4885 | if (err) { |
| 4886 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4887 | do_md_stop(mddev, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4888 | } |
| 4889 | } |
| 4890 | |
| 4891 | /* |
| 4892 | * lets try to run arrays based on all disks that have arrived |
| 4893 | * until now. (those are in pending_raid_disks) |
| 4894 | * |
| 4895 | * the method: pick the first pending disk, collect all disks with |
| 4896 | * the same UUID, remove all from the pending list and put them into |
| 4897 | * the 'same_array' list. Then order this list based on superblock |
| 4898 | * update time (freshest comes first), kick out 'old' disks and |
| 4899 | * compare superblocks. If everything's fine then run it. |
| 4900 | * |
| 4901 | * If "unit" is allocated, then bump its reference count |
| 4902 | */ |
| 4903 | static void autorun_devices(int part) |
| 4904 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4905 | mdk_rdev_t *rdev0, *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4906 | mddev_t *mddev; |
| 4907 | char b[BDEVNAME_SIZE]; |
| 4908 | |
| 4909 | printk(KERN_INFO "md: autorun ...\n"); |
| 4910 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4911 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4912 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 4913 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4914 | rdev0 = list_entry(pending_raid_disks.next, |
| 4915 | mdk_rdev_t, same_set); |
| 4916 | |
| 4917 | printk(KERN_INFO "md: considering %s ...\n", |
| 4918 | bdevname(rdev0->bdev,b)); |
| 4919 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4920 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4921 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 4922 | printk(KERN_INFO "md: adding %s ...\n", |
| 4923 | bdevname(rdev->bdev,b)); |
| 4924 | list_move(&rdev->same_set, &candidates); |
| 4925 | } |
| 4926 | /* |
| 4927 | * now we have a set of devices, with all of them having |
| 4928 | * mostly sane superblocks. It's time to allocate the |
| 4929 | * mddev. |
| 4930 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4931 | if (part) { |
| 4932 | dev = MKDEV(mdp_major, |
| 4933 | rdev0->preferred_minor << MdpMinorShift); |
| 4934 | unit = MINOR(dev) >> MdpMinorShift; |
| 4935 | } else { |
| 4936 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 4937 | unit = MINOR(dev); |
| 4938 | } |
| 4939 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4940 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 4941 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 4942 | break; |
| 4943 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4944 | |
| 4945 | md_probe(dev, NULL, NULL); |
| 4946 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 4947 | if (!mddev || !mddev->gendisk) { |
| 4948 | if (mddev) |
| 4949 | mddev_put(mddev); |
| 4950 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4951 | "md: cannot allocate memory for md drive.\n"); |
| 4952 | break; |
| 4953 | } |
| 4954 | if (mddev_lock(mddev)) |
| 4955 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 4956 | mdname(mddev)); |
| 4957 | else if (mddev->raid_disks || mddev->major_version |
| 4958 | || !list_empty(&mddev->disks)) { |
| 4959 | printk(KERN_WARNING |
| 4960 | "md: %s already running, cannot run %s\n", |
| 4961 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 4962 | mddev_unlock(mddev); |
| 4963 | } else { |
| 4964 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4965 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4966 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4967 | list_del_init(&rdev->same_set); |
| 4968 | if (bind_rdev_to_array(rdev, mddev)) |
| 4969 | export_rdev(rdev); |
| 4970 | } |
| 4971 | autorun_array(mddev); |
| 4972 | mddev_unlock(mddev); |
| 4973 | } |
| 4974 | /* on success, candidates will be empty, on error |
| 4975 | * it won't... |
| 4976 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4977 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4978 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4979 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4980 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4981 | mddev_put(mddev); |
| 4982 | } |
| 4983 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 4984 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4985 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4986 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4987 | static int get_version(void __user * arg) |
| 4988 | { |
| 4989 | mdu_version_t ver; |
| 4990 | |
| 4991 | ver.major = MD_MAJOR_VERSION; |
| 4992 | ver.minor = MD_MINOR_VERSION; |
| 4993 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 4994 | |
| 4995 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 4996 | return -EFAULT; |
| 4997 | |
| 4998 | return 0; |
| 4999 | } |
| 5000 | |
| 5001 | static int get_array_info(mddev_t * mddev, void __user * arg) |
| 5002 | { |
| 5003 | mdu_array_info_t info; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5004 | int nr,working,insync,failed,spare; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5005 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5006 | |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5007 | nr=working=insync=failed=spare=0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5008 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5009 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5010 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5011 | failed++; |
| 5012 | else { |
| 5013 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5014 | if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5015 | insync++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5016 | else |
| 5017 | spare++; |
| 5018 | } |
| 5019 | } |
| 5020 | |
| 5021 | info.major_version = mddev->major_version; |
| 5022 | info.minor_version = mddev->minor_version; |
| 5023 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 5024 | info.ctime = mddev->ctime; |
| 5025 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5026 | info.size = mddev->dev_sectors / 2; |
| 5027 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 5028 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5029 | info.nr_disks = nr; |
| 5030 | info.raid_disks = mddev->raid_disks; |
| 5031 | info.md_minor = mddev->md_minor; |
| 5032 | info.not_persistent= !mddev->persistent; |
| 5033 | |
| 5034 | info.utime = mddev->utime; |
| 5035 | info.state = 0; |
| 5036 | if (mddev->in_sync) |
| 5037 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5038 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5039 | info.state = (1<<MD_SB_BITMAP_PRESENT); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5040 | info.active_disks = insync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5041 | info.working_disks = working; |
| 5042 | info.failed_disks = failed; |
| 5043 | info.spare_disks = spare; |
| 5044 | |
| 5045 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5046 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5047 | |
| 5048 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5049 | return -EFAULT; |
| 5050 | |
| 5051 | return 0; |
| 5052 | } |
| 5053 | |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 5054 | static int get_bitmap_file(mddev_t * mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5055 | { |
| 5056 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
| 5057 | char *ptr, *buf = NULL; |
| 5058 | int err = -ENOMEM; |
| 5059 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5060 | if (md_allow_write(mddev)) |
| 5061 | file = kmalloc(sizeof(*file), GFP_NOIO); |
| 5062 | else |
| 5063 | file = kmalloc(sizeof(*file), GFP_KERNEL); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5064 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5065 | if (!file) |
| 5066 | goto out; |
| 5067 | |
| 5068 | /* bitmap disabled, zero the first byte and copy out */ |
| 5069 | if (!mddev->bitmap || !mddev->bitmap->file) { |
| 5070 | file->pathname[0] = '\0'; |
| 5071 | goto copy_out; |
| 5072 | } |
| 5073 | |
| 5074 | buf = kmalloc(sizeof(file->pathname), GFP_KERNEL); |
| 5075 | if (!buf) |
| 5076 | goto out; |
| 5077 | |
Christoph Hellwig | 6bcfd60 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 5078 | ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname)); |
| 5079 | if (IS_ERR(ptr)) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5080 | goto out; |
| 5081 | |
| 5082 | strcpy(file->pathname, ptr); |
| 5083 | |
| 5084 | copy_out: |
| 5085 | err = 0; |
| 5086 | if (copy_to_user(arg, file, sizeof(*file))) |
| 5087 | err = -EFAULT; |
| 5088 | out: |
| 5089 | kfree(buf); |
| 5090 | kfree(file); |
| 5091 | return err; |
| 5092 | } |
| 5093 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5094 | static int get_disk_info(mddev_t * mddev, void __user * arg) |
| 5095 | { |
| 5096 | mdu_disk_info_t info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5097 | mdk_rdev_t *rdev; |
| 5098 | |
| 5099 | if (copy_from_user(&info, arg, sizeof(info))) |
| 5100 | return -EFAULT; |
| 5101 | |
Andre Noll | 26ef379 | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5102 | rdev = find_rdev_nr(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5103 | if (rdev) { |
| 5104 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 5105 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 5106 | info.raid_disk = rdev->raid_disk; |
| 5107 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5108 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5109 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5110 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5111 | info.state |= (1<<MD_DISK_ACTIVE); |
| 5112 | info.state |= (1<<MD_DISK_SYNC); |
| 5113 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5114 | if (test_bit(WriteMostly, &rdev->flags)) |
| 5115 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5116 | } else { |
| 5117 | info.major = info.minor = 0; |
| 5118 | info.raid_disk = -1; |
| 5119 | info.state = (1<<MD_DISK_REMOVED); |
| 5120 | } |
| 5121 | |
| 5122 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5123 | return -EFAULT; |
| 5124 | |
| 5125 | return 0; |
| 5126 | } |
| 5127 | |
| 5128 | static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info) |
| 5129 | { |
| 5130 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 5131 | mdk_rdev_t *rdev; |
| 5132 | dev_t dev = MKDEV(info->major,info->minor); |
| 5133 | |
| 5134 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 5135 | return -EOVERFLOW; |
| 5136 | |
| 5137 | if (!mddev->raid_disks) { |
| 5138 | int err; |
| 5139 | /* expecting a device which has a superblock */ |
| 5140 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 5141 | if (IS_ERR(rdev)) { |
| 5142 | printk(KERN_WARNING |
| 5143 | "md: md_import_device returned %ld\n", |
| 5144 | PTR_ERR(rdev)); |
| 5145 | return PTR_ERR(rdev); |
| 5146 | } |
| 5147 | if (!list_empty(&mddev->disks)) { |
| 5148 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 5149 | mdk_rdev_t, same_set); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5150 | err = super_types[mddev->major_version] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5151 | .load_super(rdev, rdev0, mddev->minor_version); |
| 5152 | if (err < 0) { |
| 5153 | printk(KERN_WARNING |
| 5154 | "md: %s has different UUID to %s\n", |
| 5155 | bdevname(rdev->bdev,b), |
| 5156 | bdevname(rdev0->bdev,b2)); |
| 5157 | export_rdev(rdev); |
| 5158 | return -EINVAL; |
| 5159 | } |
| 5160 | } |
| 5161 | err = bind_rdev_to_array(rdev, mddev); |
| 5162 | if (err) |
| 5163 | export_rdev(rdev); |
| 5164 | return err; |
| 5165 | } |
| 5166 | |
| 5167 | /* |
| 5168 | * add_new_disk can be used once the array is assembled |
| 5169 | * to add "hot spares". They must already have a superblock |
| 5170 | * written |
| 5171 | */ |
| 5172 | if (mddev->pers) { |
| 5173 | int err; |
| 5174 | if (!mddev->pers->hot_add_disk) { |
| 5175 | printk(KERN_WARNING |
| 5176 | "%s: personality does not support diskops!\n", |
| 5177 | mdname(mddev)); |
| 5178 | return -EINVAL; |
| 5179 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 5180 | if (mddev->persistent) |
| 5181 | rdev = md_import_device(dev, mddev->major_version, |
| 5182 | mddev->minor_version); |
| 5183 | else |
| 5184 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5185 | if (IS_ERR(rdev)) { |
| 5186 | printk(KERN_WARNING |
| 5187 | "md: md_import_device returned %ld\n", |
| 5188 | PTR_ERR(rdev)); |
| 5189 | return PTR_ERR(rdev); |
| 5190 | } |
NeilBrown | 1a855a0 | 2010-12-09 16:36:28 +1100 | [diff] [blame] | 5191 | /* set saved_raid_disk if appropriate */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5192 | if (!mddev->persistent) { |
| 5193 | if (info->state & (1<<MD_DISK_SYNC) && |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 5194 | info->raid_disk < mddev->raid_disks) { |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5195 | rdev->raid_disk = info->raid_disk; |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 5196 | set_bit(In_sync, &rdev->flags); |
| 5197 | } else |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5198 | rdev->raid_disk = -1; |
| 5199 | } else |
| 5200 | super_types[mddev->major_version]. |
| 5201 | validate_super(mddev, rdev); |
NeilBrown | 1a855a0 | 2010-12-09 16:36:28 +1100 | [diff] [blame] | 5202 | if (test_bit(In_sync, &rdev->flags)) |
| 5203 | rdev->saved_raid_disk = rdev->raid_disk; |
| 5204 | else |
| 5205 | rdev->saved_raid_disk = -1; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5206 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5207 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5208 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5209 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5210 | else |
| 5211 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5212 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5213 | rdev->raid_disk = -1; |
| 5214 | err = bind_rdev_to_array(rdev, mddev); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 5215 | if (!err && !mddev->pers->hot_remove_disk) { |
| 5216 | /* If there is hot_add_disk but no hot_remove_disk |
| 5217 | * then added disks for geometry changes, |
| 5218 | * and should be added immediately. |
| 5219 | */ |
| 5220 | super_types[mddev->major_version]. |
| 5221 | validate_super(mddev, rdev); |
| 5222 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 5223 | if (err) |
| 5224 | unbind_rdev_from_array(rdev); |
| 5225 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5226 | if (err) |
| 5227 | export_rdev(rdev); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 5228 | else |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5229 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5230 | |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 5231 | md_update_sb(mddev, 1); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 5232 | if (mddev->degraded) |
| 5233 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5234 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 5235 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5236 | return err; |
| 5237 | } |
| 5238 | |
| 5239 | /* otherwise, add_new_disk is only allowed |
| 5240 | * for major_version==0 superblocks |
| 5241 | */ |
| 5242 | if (mddev->major_version != 0) { |
| 5243 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 5244 | mdname(mddev)); |
| 5245 | return -EINVAL; |
| 5246 | } |
| 5247 | |
| 5248 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 5249 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5250 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5251 | if (IS_ERR(rdev)) { |
| 5252 | printk(KERN_WARNING |
| 5253 | "md: error, md_import_device() returned %ld\n", |
| 5254 | PTR_ERR(rdev)); |
| 5255 | return PTR_ERR(rdev); |
| 5256 | } |
| 5257 | rdev->desc_nr = info->number; |
| 5258 | if (info->raid_disk < mddev->raid_disks) |
| 5259 | rdev->raid_disk = info->raid_disk; |
| 5260 | else |
| 5261 | rdev->raid_disk = -1; |
| 5262 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5263 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5264 | if (info->state & (1<<MD_DISK_SYNC)) |
| 5265 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5266 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5267 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5268 | set_bit(WriteMostly, &rdev->flags); |
| 5269 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5270 | if (!mddev->persistent) { |
| 5271 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 5272 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
| 5273 | } else |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5274 | rdev->sb_start = calc_dev_sboffset(rdev); |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5275 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5276 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5277 | err = bind_rdev_to_array(rdev, mddev); |
| 5278 | if (err) { |
| 5279 | export_rdev(rdev); |
| 5280 | return err; |
| 5281 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5282 | } |
| 5283 | |
| 5284 | return 0; |
| 5285 | } |
| 5286 | |
| 5287 | static int hot_remove_disk(mddev_t * mddev, dev_t dev) |
| 5288 | { |
| 5289 | char b[BDEVNAME_SIZE]; |
| 5290 | mdk_rdev_t *rdev; |
| 5291 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5292 | rdev = find_rdev(mddev, dev); |
| 5293 | if (!rdev) |
| 5294 | return -ENXIO; |
| 5295 | |
| 5296 | if (rdev->raid_disk >= 0) |
| 5297 | goto busy; |
| 5298 | |
| 5299 | kick_rdev_from_array(rdev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5300 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5301 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5302 | |
| 5303 | return 0; |
| 5304 | busy: |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 5305 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5306 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5307 | return -EBUSY; |
| 5308 | } |
| 5309 | |
| 5310 | static int hot_add_disk(mddev_t * mddev, dev_t dev) |
| 5311 | { |
| 5312 | char b[BDEVNAME_SIZE]; |
| 5313 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5314 | mdk_rdev_t *rdev; |
| 5315 | |
| 5316 | if (!mddev->pers) |
| 5317 | return -ENODEV; |
| 5318 | |
| 5319 | if (mddev->major_version != 0) { |
| 5320 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 5321 | " version-0 superblocks.\n", |
| 5322 | mdname(mddev)); |
| 5323 | return -EINVAL; |
| 5324 | } |
| 5325 | if (!mddev->pers->hot_add_disk) { |
| 5326 | printk(KERN_WARNING |
| 5327 | "%s: personality does not support diskops!\n", |
| 5328 | mdname(mddev)); |
| 5329 | return -EINVAL; |
| 5330 | } |
| 5331 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5332 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5333 | if (IS_ERR(rdev)) { |
| 5334 | printk(KERN_WARNING |
| 5335 | "md: error, md_import_device() returned %ld\n", |
| 5336 | PTR_ERR(rdev)); |
| 5337 | return -EINVAL; |
| 5338 | } |
| 5339 | |
| 5340 | if (mddev->persistent) |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5341 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5342 | else |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 5343 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5344 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5345 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5346 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5347 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5348 | printk(KERN_WARNING |
| 5349 | "md: can not hot-add faulty %s disk to %s!\n", |
| 5350 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5351 | err = -EINVAL; |
| 5352 | goto abort_export; |
| 5353 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5354 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5355 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 5356 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5357 | err = bind_rdev_to_array(rdev, mddev); |
| 5358 | if (err) |
| 5359 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5360 | |
| 5361 | /* |
| 5362 | * The rest should better be atomic, we can have disk failures |
| 5363 | * noticed in interrupt contexts ... |
| 5364 | */ |
| 5365 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5366 | rdev->raid_disk = -1; |
| 5367 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5368 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5369 | |
| 5370 | /* |
| 5371 | * Kick recovery, maybe this spare has to be added to the |
| 5372 | * array immediately. |
| 5373 | */ |
| 5374 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5375 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5376 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5377 | return 0; |
| 5378 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5379 | abort_export: |
| 5380 | export_rdev(rdev); |
| 5381 | return err; |
| 5382 | } |
| 5383 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5384 | static int set_bitmap_file(mddev_t *mddev, int fd) |
| 5385 | { |
| 5386 | int err; |
| 5387 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5388 | if (mddev->pers) { |
| 5389 | if (!mddev->pers->quiesce) |
| 5390 | return -EBUSY; |
| 5391 | if (mddev->recovery || mddev->sync_thread) |
| 5392 | return -EBUSY; |
| 5393 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5394 | } |
| 5395 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5396 | |
| 5397 | if (fd >= 0) { |
| 5398 | if (mddev->bitmap) |
| 5399 | return -EEXIST; /* cannot add when bitmap is present */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5400 | mddev->bitmap_info.file = fget(fd); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5401 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5402 | if (mddev->bitmap_info.file == NULL) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5403 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 5404 | mdname(mddev)); |
| 5405 | return -EBADF; |
| 5406 | } |
| 5407 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5408 | err = deny_bitmap_write_access(mddev->bitmap_info.file); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5409 | if (err) { |
| 5410 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 5411 | mdname(mddev)); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5412 | fput(mddev->bitmap_info.file); |
| 5413 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5414 | return err; |
| 5415 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5416 | mddev->bitmap_info.offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5417 | } else if (mddev->bitmap == NULL) |
| 5418 | return -ENOENT; /* cannot remove what isn't there */ |
| 5419 | err = 0; |
| 5420 | if (mddev->pers) { |
| 5421 | mddev->pers->quiesce(mddev, 1); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5422 | if (fd >= 0) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5423 | err = bitmap_create(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5424 | if (!err) |
| 5425 | err = bitmap_load(mddev); |
| 5426 | } |
NeilBrown | d7375ab32 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5427 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5428 | bitmap_destroy(mddev); |
NeilBrown | d7375ab32 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5429 | fd = -1; /* make sure to put the file */ |
| 5430 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5431 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab32 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5432 | } |
| 5433 | if (fd < 0) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5434 | if (mddev->bitmap_info.file) { |
| 5435 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 5436 | fput(mddev->bitmap_info.file); |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 5437 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5438 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5439 | } |
| 5440 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5441 | return err; |
| 5442 | } |
| 5443 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5444 | /* |
| 5445 | * set_array_info is used two different ways |
| 5446 | * The original usage is when creating a new array. |
| 5447 | * In this usage, raid_disks is > 0 and it together with |
| 5448 | * level, size, not_persistent,layout,chunksize determine the |
| 5449 | * shape of the array. |
| 5450 | * This will always create an array with a type-0.90.0 superblock. |
| 5451 | * The newer usage is when assembling an array. |
| 5452 | * In this case raid_disks will be 0, and the major_version field is |
| 5453 | * use to determine which style super-blocks are to be found on the devices. |
| 5454 | * The minor and patch _version numbers are also kept incase the |
| 5455 | * super_block handler wishes to interpret them. |
| 5456 | */ |
| 5457 | static int set_array_info(mddev_t * mddev, mdu_array_info_t *info) |
| 5458 | { |
| 5459 | |
| 5460 | if (info->raid_disks == 0) { |
| 5461 | /* just setting version number for superblock loading */ |
| 5462 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 5463 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5464 | super_types[info->major_version].name == NULL) { |
| 5465 | /* maybe try to auto-load a module? */ |
| 5466 | printk(KERN_INFO |
| 5467 | "md: superblock version %d not known\n", |
| 5468 | info->major_version); |
| 5469 | return -EINVAL; |
| 5470 | } |
| 5471 | mddev->major_version = info->major_version; |
| 5472 | mddev->minor_version = info->minor_version; |
| 5473 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5474 | mddev->persistent = !info->not_persistent; |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 5475 | /* ensure mddev_put doesn't delete this now that there |
| 5476 | * is some minimal configuration. |
| 5477 | */ |
| 5478 | mddev->ctime = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5479 | return 0; |
| 5480 | } |
| 5481 | mddev->major_version = MD_MAJOR_VERSION; |
| 5482 | mddev->minor_version = MD_MINOR_VERSION; |
| 5483 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 5484 | mddev->ctime = get_seconds(); |
| 5485 | |
| 5486 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 5487 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5488 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5489 | mddev->raid_disks = info->raid_disks; |
| 5490 | /* don't set md_minor, it is determined by which /dev/md* was |
| 5491 | * openned |
| 5492 | */ |
| 5493 | if (info->state & (1<<MD_SB_CLEAN)) |
| 5494 | mddev->recovery_cp = MaxSector; |
| 5495 | else |
| 5496 | mddev->recovery_cp = 0; |
| 5497 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5498 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5499 | |
| 5500 | mddev->layout = info->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5501 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5502 | |
| 5503 | mddev->max_disks = MD_SB_DISKS; |
| 5504 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5505 | if (mddev->persistent) |
| 5506 | mddev->flags = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5507 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5508 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5509 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
| 5510 | mddev->bitmap_info.offset = 0; |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 5511 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5512 | mddev->reshape_position = MaxSector; |
| 5513 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5514 | /* |
| 5515 | * Generate a 128 bit UUID |
| 5516 | */ |
| 5517 | get_random_bytes(mddev->uuid, 16); |
| 5518 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5519 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5520 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5521 | mddev->new_layout = mddev->layout; |
| 5522 | mddev->delta_disks = 0; |
| 5523 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5524 | return 0; |
| 5525 | } |
| 5526 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5527 | void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors) |
| 5528 | { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5529 | WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__); |
| 5530 | |
| 5531 | if (mddev->external_size) |
| 5532 | return; |
| 5533 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5534 | mddev->array_sectors = array_sectors; |
| 5535 | } |
| 5536 | EXPORT_SYMBOL(md_set_array_sectors); |
| 5537 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5538 | static int update_size(mddev_t *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5539 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5540 | mdk_rdev_t *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5541 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5542 | int fit = (num_sectors == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5543 | |
| 5544 | if (mddev->pers->resize == NULL) |
| 5545 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5546 | /* The "num_sectors" is the number of sectors of each device that |
| 5547 | * is used. This can only make sense for arrays with redundancy. |
| 5548 | * linear and raid0 always use whatever space is available. We can only |
| 5549 | * consider changing this number if no resync or reconstruction is |
| 5550 | * 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] | 5551 | * 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] | 5552 | * of each device. If num_sectors is zero, we find the largest size |
| 5553 | * that fits. |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5554 | */ |
| 5555 | if (mddev->sync_thread) |
| 5556 | return -EBUSY; |
NeilBrown | dba034e | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 5557 | if (mddev->bitmap) |
| 5558 | /* Sorry, cannot grow a bitmap yet, just remove it, |
| 5559 | * grow, and re-add. |
| 5560 | */ |
| 5561 | return -EBUSY; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5562 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5563 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 5564 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5565 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 5566 | num_sectors = avail; |
| 5567 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5568 | return -ENOSPC; |
| 5569 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5570 | rv = mddev->pers->resize(mddev, num_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5571 | if (!rv) |
| 5572 | revalidate_disk(mddev->gendisk); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5573 | return rv; |
| 5574 | } |
| 5575 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5576 | static int update_raid_disks(mddev_t *mddev, int raid_disks) |
| 5577 | { |
| 5578 | int rv; |
| 5579 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5580 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5581 | return -EINVAL; |
| 5582 | if (raid_disks <= 0 || |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 5583 | (mddev->max_disks && raid_disks >= mddev->max_disks)) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5584 | return -EINVAL; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5585 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5586 | return -EBUSY; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5587 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
| 5588 | |
| 5589 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | de171cb | 2011-01-31 11:57:42 +1100 | [diff] [blame] | 5590 | if (rv < 0) |
| 5591 | mddev->delta_disks = 0; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5592 | return rv; |
| 5593 | } |
| 5594 | |
| 5595 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5596 | /* |
| 5597 | * update_array_info is used to change the configuration of an |
| 5598 | * on-line array. |
| 5599 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 5600 | * fields in the info are checked against the array. |
| 5601 | * Any differences that cannot be handled will cause an error. |
| 5602 | * Normally, only one change can be managed at a time. |
| 5603 | */ |
| 5604 | static int update_array_info(mddev_t *mddev, mdu_array_info_t *info) |
| 5605 | { |
| 5606 | int rv = 0; |
| 5607 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5608 | int state = 0; |
| 5609 | |
| 5610 | /* calculate expected state,ignoring low bits */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5611 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5612 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5613 | |
| 5614 | if (mddev->major_version != info->major_version || |
| 5615 | mddev->minor_version != info->minor_version || |
| 5616 | /* mddev->patch_version != info->patch_version || */ |
| 5617 | mddev->ctime != info->ctime || |
| 5618 | mddev->level != info->level || |
| 5619 | /* mddev->layout != info->layout || */ |
| 5620 | !mddev->persistent != info->not_persistent|| |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5621 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5622 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 5623 | ((state^info->state) & 0xfffffe00) |
| 5624 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5625 | return -EINVAL; |
| 5626 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5627 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 5628 | cnt++; |
| 5629 | if (mddev->raid_disks != info->raid_disks) |
| 5630 | cnt++; |
| 5631 | if (mddev->layout != info->layout) |
| 5632 | cnt++; |
| 5633 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 5634 | cnt++; |
| 5635 | if (cnt == 0) |
| 5636 | return 0; |
| 5637 | if (cnt > 1) |
| 5638 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5639 | |
| 5640 | if (mddev->layout != info->layout) { |
| 5641 | /* Change layout |
| 5642 | * we don't need to do anything at the md level, the |
| 5643 | * personality will take care of it all. |
| 5644 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5645 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5646 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5647 | else { |
| 5648 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5649 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5650 | if (rv) |
| 5651 | mddev->new_layout = mddev->layout; |
| 5652 | return rv; |
| 5653 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5654 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5655 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5656 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5657 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5658 | if (mddev->raid_disks != info->raid_disks) |
| 5659 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5660 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5661 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5662 | if (mddev->pers->quiesce == NULL) |
| 5663 | return -EINVAL; |
| 5664 | if (mddev->recovery || mddev->sync_thread) |
| 5665 | return -EBUSY; |
| 5666 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5667 | /* add the bitmap */ |
| 5668 | if (mddev->bitmap) |
| 5669 | return -EEXIST; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5670 | if (mddev->bitmap_info.default_offset == 0) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5671 | return -EINVAL; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5672 | mddev->bitmap_info.offset = |
| 5673 | mddev->bitmap_info.default_offset; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5674 | mddev->pers->quiesce(mddev, 1); |
| 5675 | rv = bitmap_create(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5676 | if (!rv) |
| 5677 | rv = bitmap_load(mddev); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5678 | if (rv) |
| 5679 | bitmap_destroy(mddev); |
| 5680 | mddev->pers->quiesce(mddev, 0); |
| 5681 | } else { |
| 5682 | /* remove the bitmap */ |
| 5683 | if (!mddev->bitmap) |
| 5684 | return -ENOENT; |
| 5685 | if (mddev->bitmap->file) |
| 5686 | return -EINVAL; |
| 5687 | mddev->pers->quiesce(mddev, 1); |
| 5688 | bitmap_destroy(mddev); |
| 5689 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5690 | mddev->bitmap_info.offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5691 | } |
| 5692 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5693 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5694 | return rv; |
| 5695 | } |
| 5696 | |
| 5697 | static int set_disk_faulty(mddev_t *mddev, dev_t dev) |
| 5698 | { |
| 5699 | mdk_rdev_t *rdev; |
| 5700 | |
| 5701 | if (mddev->pers == NULL) |
| 5702 | return -ENODEV; |
| 5703 | |
| 5704 | rdev = find_rdev(mddev, dev); |
| 5705 | if (!rdev) |
| 5706 | return -ENODEV; |
| 5707 | |
| 5708 | md_error(mddev, rdev); |
| 5709 | return 0; |
| 5710 | } |
| 5711 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 5712 | /* |
| 5713 | * We have a problem here : there is no easy way to give a CHS |
| 5714 | * virtual geometry. We currently pretend that we have a 2 heads |
| 5715 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 5716 | * dosfs just mad... ;-) |
| 5717 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5718 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 5719 | { |
| 5720 | mddev_t *mddev = bdev->bd_disk->private_data; |
| 5721 | |
| 5722 | geo->heads = 2; |
| 5723 | geo->sectors = 4; |
NeilBrown | 49ce6ce | 2010-03-29 10:51:42 +1100 | [diff] [blame] | 5724 | geo->cylinders = mddev->array_sectors / 8; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5725 | return 0; |
| 5726 | } |
| 5727 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5728 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5729 | unsigned int cmd, unsigned long arg) |
| 5730 | { |
| 5731 | int err = 0; |
| 5732 | void __user *argp = (void __user *)arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5733 | mddev_t *mddev = NULL; |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 5734 | int ro; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5735 | |
| 5736 | if (!capable(CAP_SYS_ADMIN)) |
| 5737 | return -EACCES; |
| 5738 | |
| 5739 | /* |
| 5740 | * Commands dealing with the RAID driver but not any |
| 5741 | * particular array: |
| 5742 | */ |
| 5743 | switch (cmd) |
| 5744 | { |
| 5745 | case RAID_VERSION: |
| 5746 | err = get_version(argp); |
| 5747 | goto done; |
| 5748 | |
| 5749 | case PRINT_RAID_DEBUG: |
| 5750 | err = 0; |
| 5751 | md_print_devices(); |
| 5752 | goto done; |
| 5753 | |
| 5754 | #ifndef MODULE |
| 5755 | case RAID_AUTORUN: |
| 5756 | err = 0; |
| 5757 | autostart_arrays(arg); |
| 5758 | goto done; |
| 5759 | #endif |
| 5760 | default:; |
| 5761 | } |
| 5762 | |
| 5763 | /* |
| 5764 | * Commands creating/starting a new array: |
| 5765 | */ |
| 5766 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5767 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5768 | |
| 5769 | if (!mddev) { |
| 5770 | BUG(); |
| 5771 | goto abort; |
| 5772 | } |
| 5773 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5774 | err = mddev_lock(mddev); |
| 5775 | if (err) { |
| 5776 | printk(KERN_INFO |
| 5777 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 5778 | err, cmd); |
| 5779 | goto abort; |
| 5780 | } |
| 5781 | |
| 5782 | switch (cmd) |
| 5783 | { |
| 5784 | case SET_ARRAY_INFO: |
| 5785 | { |
| 5786 | mdu_array_info_t info; |
| 5787 | if (!arg) |
| 5788 | memset(&info, 0, sizeof(info)); |
| 5789 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 5790 | err = -EFAULT; |
| 5791 | goto abort_unlock; |
| 5792 | } |
| 5793 | if (mddev->pers) { |
| 5794 | err = update_array_info(mddev, &info); |
| 5795 | if (err) { |
| 5796 | printk(KERN_WARNING "md: couldn't update" |
| 5797 | " array info. %d\n", err); |
| 5798 | goto abort_unlock; |
| 5799 | } |
| 5800 | goto done_unlock; |
| 5801 | } |
| 5802 | if (!list_empty(&mddev->disks)) { |
| 5803 | printk(KERN_WARNING |
| 5804 | "md: array %s already has disks!\n", |
| 5805 | mdname(mddev)); |
| 5806 | err = -EBUSY; |
| 5807 | goto abort_unlock; |
| 5808 | } |
| 5809 | if (mddev->raid_disks) { |
| 5810 | printk(KERN_WARNING |
| 5811 | "md: array %s already initialised!\n", |
| 5812 | mdname(mddev)); |
| 5813 | err = -EBUSY; |
| 5814 | goto abort_unlock; |
| 5815 | } |
| 5816 | err = set_array_info(mddev, &info); |
| 5817 | if (err) { |
| 5818 | printk(KERN_WARNING "md: couldn't set" |
| 5819 | " array info. %d\n", err); |
| 5820 | goto abort_unlock; |
| 5821 | } |
| 5822 | } |
| 5823 | goto done_unlock; |
| 5824 | |
| 5825 | default:; |
| 5826 | } |
| 5827 | |
| 5828 | /* |
| 5829 | * Commands querying/configuring an existing array: |
| 5830 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5831 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5832 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 5833 | if ((!mddev->raid_disks && !mddev->external) |
| 5834 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 5835 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 5836 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5837 | err = -ENODEV; |
| 5838 | goto abort_unlock; |
| 5839 | } |
| 5840 | |
| 5841 | /* |
| 5842 | * Commands even a read-only array can execute: |
| 5843 | */ |
| 5844 | switch (cmd) |
| 5845 | { |
| 5846 | case GET_ARRAY_INFO: |
| 5847 | err = get_array_info(mddev, argp); |
| 5848 | goto done_unlock; |
| 5849 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5850 | case GET_BITMAP_FILE: |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 5851 | err = get_bitmap_file(mddev, argp); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5852 | goto done_unlock; |
| 5853 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5854 | case GET_DISK_INFO: |
| 5855 | err = get_disk_info(mddev, argp); |
| 5856 | goto done_unlock; |
| 5857 | |
| 5858 | case RESTART_ARRAY_RW: |
| 5859 | err = restart_array(mddev); |
| 5860 | goto done_unlock; |
| 5861 | |
| 5862 | case STOP_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5863 | err = do_md_stop(mddev, 0, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5864 | goto done_unlock; |
| 5865 | |
| 5866 | case STOP_ARRAY_RO: |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5867 | err = md_set_readonly(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5868 | goto done_unlock; |
| 5869 | |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 5870 | case BLKROSET: |
| 5871 | if (get_user(ro, (int __user *)(arg))) { |
| 5872 | err = -EFAULT; |
| 5873 | goto done_unlock; |
| 5874 | } |
| 5875 | err = -EINVAL; |
| 5876 | |
| 5877 | /* if the bdev is going readonly the value of mddev->ro |
| 5878 | * does not matter, no writes are coming |
| 5879 | */ |
| 5880 | if (ro) |
| 5881 | goto done_unlock; |
| 5882 | |
| 5883 | /* are we are already prepared for writes? */ |
| 5884 | if (mddev->ro != 1) |
| 5885 | goto done_unlock; |
| 5886 | |
| 5887 | /* transitioning to readauto need only happen for |
| 5888 | * arrays that call md_write_start |
| 5889 | */ |
| 5890 | if (mddev->pers) { |
| 5891 | err = restart_array(mddev); |
| 5892 | if (err == 0) { |
| 5893 | mddev->ro = 2; |
| 5894 | set_disk_ro(mddev->gendisk, 0); |
| 5895 | } |
| 5896 | } |
| 5897 | goto done_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5898 | } |
| 5899 | |
| 5900 | /* |
| 5901 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5902 | * superblock, so we do not allow them on read-only arrays. |
| 5903 | * However non-MD ioctls (e.g. get-size) will still come through |
| 5904 | * here and hit the 'default' below, so only disallow |
| 5905 | * 'md' ioctls, and switch to rw mode if started auto-readonly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5906 | */ |
Andre Noll | bb57fc6 | 2008-04-25 19:06:35 +0200 | [diff] [blame] | 5907 | if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5908 | if (mddev->ro == 2) { |
| 5909 | mddev->ro = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5910 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5911 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5912 | md_wakeup_thread(mddev->thread); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5913 | } else { |
| 5914 | err = -EROFS; |
| 5915 | goto abort_unlock; |
| 5916 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5917 | } |
| 5918 | |
| 5919 | switch (cmd) |
| 5920 | { |
| 5921 | case ADD_NEW_DISK: |
| 5922 | { |
| 5923 | mdu_disk_info_t info; |
| 5924 | if (copy_from_user(&info, argp, sizeof(info))) |
| 5925 | err = -EFAULT; |
| 5926 | else |
| 5927 | err = add_new_disk(mddev, &info); |
| 5928 | goto done_unlock; |
| 5929 | } |
| 5930 | |
| 5931 | case HOT_REMOVE_DISK: |
| 5932 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
| 5933 | goto done_unlock; |
| 5934 | |
| 5935 | case HOT_ADD_DISK: |
| 5936 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
| 5937 | goto done_unlock; |
| 5938 | |
| 5939 | case SET_DISK_FAULTY: |
| 5940 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
| 5941 | goto done_unlock; |
| 5942 | |
| 5943 | case RUN_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5944 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5945 | goto done_unlock; |
| 5946 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5947 | case SET_BITMAP_FILE: |
| 5948 | err = set_bitmap_file(mddev, (int)arg); |
| 5949 | goto done_unlock; |
| 5950 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5951 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5952 | err = -EINVAL; |
| 5953 | goto abort_unlock; |
| 5954 | } |
| 5955 | |
| 5956 | done_unlock: |
| 5957 | abort_unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5958 | if (mddev->hold_active == UNTIL_IOCTL && |
| 5959 | err != -EINVAL) |
| 5960 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5961 | mddev_unlock(mddev); |
| 5962 | |
| 5963 | return err; |
| 5964 | done: |
| 5965 | if (err) |
| 5966 | MD_BUG(); |
| 5967 | abort: |
| 5968 | return err; |
| 5969 | } |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 5970 | #ifdef CONFIG_COMPAT |
| 5971 | static int md_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 5972 | unsigned int cmd, unsigned long arg) |
| 5973 | { |
| 5974 | switch (cmd) { |
| 5975 | case HOT_REMOVE_DISK: |
| 5976 | case HOT_ADD_DISK: |
| 5977 | case SET_DISK_FAULTY: |
| 5978 | case SET_BITMAP_FILE: |
| 5979 | /* These take in integer arg, do not convert */ |
| 5980 | break; |
| 5981 | default: |
| 5982 | arg = (unsigned long)compat_ptr(arg); |
| 5983 | break; |
| 5984 | } |
| 5985 | |
| 5986 | return md_ioctl(bdev, mode, cmd, arg); |
| 5987 | } |
| 5988 | #endif /* CONFIG_COMPAT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5989 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5990 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5991 | { |
| 5992 | /* |
| 5993 | * Succeed if we can lock the mddev, which confirms that |
| 5994 | * it isn't being stopped right now. |
| 5995 | */ |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5996 | mddev_t *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5997 | int err; |
| 5998 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5999 | if (mddev->gendisk != bdev->bd_disk) { |
| 6000 | /* we are racing with mddev_put which is discarding this |
| 6001 | * bd_disk. |
| 6002 | */ |
| 6003 | mddev_put(mddev); |
| 6004 | /* Wait until bdev->bd_disk is definitely gone */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 6005 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6006 | /* Then retry the open from the top */ |
| 6007 | return -ERESTARTSYS; |
| 6008 | } |
| 6009 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 6010 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 6011 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6012 | goto out; |
| 6013 | |
| 6014 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6015 | atomic_inc(&mddev->openers); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 6016 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6017 | |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6018 | check_disk_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6019 | out: |
| 6020 | return err; |
| 6021 | } |
| 6022 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6023 | static int md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6024 | { |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6025 | mddev_t *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6026 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 6027 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6028 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6029 | mddev_put(mddev); |
| 6030 | |
| 6031 | return 0; |
| 6032 | } |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6033 | |
| 6034 | static int md_media_changed(struct gendisk *disk) |
| 6035 | { |
| 6036 | mddev_t *mddev = disk->private_data; |
| 6037 | |
| 6038 | return mddev->changed; |
| 6039 | } |
| 6040 | |
| 6041 | static int md_revalidate(struct gendisk *disk) |
| 6042 | { |
| 6043 | mddev_t *mddev = disk->private_data; |
| 6044 | |
| 6045 | mddev->changed = 0; |
| 6046 | return 0; |
| 6047 | } |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 6048 | static const struct block_device_operations md_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6049 | { |
| 6050 | .owner = THIS_MODULE, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6051 | .open = md_open, |
| 6052 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 6053 | .ioctl = md_ioctl, |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 6054 | #ifdef CONFIG_COMPAT |
| 6055 | .compat_ioctl = md_compat_ioctl, |
| 6056 | #endif |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6057 | .getgeo = md_getgeo, |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6058 | .media_changed = md_media_changed, |
| 6059 | .revalidate_disk= md_revalidate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6060 | }; |
| 6061 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6062 | static int md_thread(void * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6063 | { |
| 6064 | mdk_thread_t *thread = arg; |
| 6065 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6066 | /* |
| 6067 | * md_thread is a 'system-thread', it's priority should be very |
| 6068 | * high. We avoid resource deadlocks individually in each |
| 6069 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 6070 | * the very same RT priority as kswapd, thus we will never get |
| 6071 | * into a priority inversion deadlock. |
| 6072 | * |
| 6073 | * we definitely have to have equal or higher priority than |
| 6074 | * bdflush, otherwise bdflush will deadlock if there are too |
| 6075 | * many dirty RAID5 blocks. |
| 6076 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6077 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 6078 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6079 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6080 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 6081 | /* We need to wait INTERRUPTIBLE so that |
| 6082 | * we don't add to the load-average. |
| 6083 | * That means we need to be sure no signals are |
| 6084 | * pending |
| 6085 | */ |
| 6086 | if (signal_pending(current)) |
| 6087 | flush_signals(current); |
| 6088 | |
| 6089 | wait_event_interruptible_timeout |
| 6090 | (thread->wqueue, |
| 6091 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 6092 | || kthread_should_stop(), |
| 6093 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6094 | |
NeilBrown | 6c98791 | 2011-01-14 09:13:53 +1100 | [diff] [blame] | 6095 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 6096 | if (!kthread_should_stop()) |
NeilBrown | 589a594 | 2010-12-09 17:02:14 +1100 | [diff] [blame] | 6097 | thread->run(thread->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6098 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6099 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6100 | return 0; |
| 6101 | } |
| 6102 | |
| 6103 | void md_wakeup_thread(mdk_thread_t *thread) |
| 6104 | { |
| 6105 | if (thread) { |
| 6106 | dprintk("md: waking up MD thread %s.\n", thread->tsk->comm); |
| 6107 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 6108 | wake_up(&thread->wqueue); |
| 6109 | } |
| 6110 | } |
| 6111 | |
| 6112 | mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev, |
| 6113 | const char *name) |
| 6114 | { |
| 6115 | mdk_thread_t *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6116 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 6117 | thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6118 | if (!thread) |
| 6119 | return NULL; |
| 6120 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6121 | init_waitqueue_head(&thread->wqueue); |
| 6122 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6123 | thread->run = run; |
| 6124 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6125 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 6126 | thread->tsk = kthread_run(md_thread, thread, |
| 6127 | "%s_%s", |
| 6128 | mdname(thread->mddev), |
| 6129 | name ?: mddev->pers->name); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6130 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6131 | kfree(thread); |
| 6132 | return NULL; |
| 6133 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6134 | return thread; |
| 6135 | } |
| 6136 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6137 | void md_unregister_thread(mdk_thread_t *thread) |
| 6138 | { |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6139 | if (!thread) |
| 6140 | return; |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 6141 | dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6142 | |
| 6143 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6144 | kfree(thread); |
| 6145 | } |
| 6146 | |
| 6147 | void md_error(mddev_t *mddev, mdk_rdev_t *rdev) |
| 6148 | { |
| 6149 | if (!mddev) { |
| 6150 | MD_BUG(); |
| 6151 | return; |
| 6152 | } |
| 6153 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6154 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6155 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6156 | |
| 6157 | if (mddev->external) |
| 6158 | set_bit(Blocked, &rdev->flags); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6159 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6160 | dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n", |
| 6161 | mdname(mddev), |
| 6162 | MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev), |
| 6163 | __builtin_return_address(0),__builtin_return_address(1), |
| 6164 | __builtin_return_address(2),__builtin_return_address(3)); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6165 | */ |
Andrew Morton | d0a0a5e | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 6166 | if (!mddev->pers) |
| 6167 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6168 | if (!mddev->pers->error_handler) |
| 6169 | return; |
| 6170 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6171 | if (mddev->degraded) |
| 6172 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6173 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6174 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6175 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6176 | md_wakeup_thread(mddev->thread); |
NeilBrown | 768a418 | 2010-07-26 11:49:55 +1000 | [diff] [blame] | 6177 | if (mddev->event_work.func) |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 6178 | queue_work(md_misc_wq, &mddev->event_work); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 6179 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6180 | } |
| 6181 | |
| 6182 | /* seq_file implementation /proc/mdstat */ |
| 6183 | |
| 6184 | static void status_unused(struct seq_file *seq) |
| 6185 | { |
| 6186 | int i = 0; |
| 6187 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6188 | |
| 6189 | seq_printf(seq, "unused devices: "); |
| 6190 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6191 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6192 | char b[BDEVNAME_SIZE]; |
| 6193 | i++; |
| 6194 | seq_printf(seq, "%s ", |
| 6195 | bdevname(rdev->bdev,b)); |
| 6196 | } |
| 6197 | if (!i) |
| 6198 | seq_printf(seq, "<none>"); |
| 6199 | |
| 6200 | seq_printf(seq, "\n"); |
| 6201 | } |
| 6202 | |
| 6203 | |
| 6204 | static void status_resync(struct seq_file *seq, mddev_t * mddev) |
| 6205 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6206 | sector_t max_sectors, resync, res; |
| 6207 | unsigned long dt, db; |
| 6208 | sector_t rt; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6209 | int scale; |
| 6210 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6211 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6212 | resync = mddev->curr_resync - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6213 | |
| 6214 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6215 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6216 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6217 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6218 | |
| 6219 | /* |
| 6220 | * Should not happen. |
| 6221 | */ |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6222 | if (!max_sectors) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6223 | MD_BUG(); |
| 6224 | return; |
| 6225 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6226 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6227 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6228 | * u32, as those are the requirements for sector_div. |
| 6229 | * Thus 'scale' must be at least 10 |
| 6230 | */ |
| 6231 | scale = 10; |
| 6232 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6233 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6234 | scale++; |
| 6235 | } |
| 6236 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6237 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6238 | |
| 6239 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6240 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6241 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6242 | seq_printf(seq, "["); |
| 6243 | for (i = 0; i < x; i++) |
| 6244 | seq_printf(seq, "="); |
| 6245 | seq_printf(seq, ">"); |
| 6246 | for (i = 0; i < y; i++) |
| 6247 | seq_printf(seq, "."); |
| 6248 | seq_printf(seq, "] "); |
| 6249 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6250 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6251 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 6252 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6253 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 6254 | "check" : |
| 6255 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 6256 | "resync" : "recovery"))), |
| 6257 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6258 | (unsigned long long) resync/2, |
| 6259 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6260 | |
| 6261 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6262 | * dt: time from mark until now |
| 6263 | * db: blocks written from mark until now |
| 6264 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6265 | * |
| 6266 | * rt is a sector_t, so could be 32bit or 64bit. |
| 6267 | * So we divide before multiply in case it is 32bit and close |
| 6268 | * to the limit. |
| 6269 | * We scale the divisor (db) by 32 to avoid loosing precision |
| 6270 | * near the end of resync when the number of remaining sectors |
| 6271 | * is close to 'db'. |
| 6272 | * We then divide rt by 32 after multiplying by db to compensate. |
| 6273 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6274 | */ |
| 6275 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 6276 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6277 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 6278 | - mddev->resync_mark_cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6279 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6280 | rt = max_sectors - resync; /* number of remaining sectors */ |
| 6281 | sector_div(rt, db/32+1); |
| 6282 | rt *= dt; |
| 6283 | rt >>= 5; |
| 6284 | |
| 6285 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 6286 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6287 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6288 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6289 | } |
| 6290 | |
| 6291 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 6292 | { |
| 6293 | struct list_head *tmp; |
| 6294 | loff_t l = *pos; |
| 6295 | mddev_t *mddev; |
| 6296 | |
| 6297 | if (l >= 0x10000) |
| 6298 | return NULL; |
| 6299 | if (!l--) |
| 6300 | /* header */ |
| 6301 | return (void*)1; |
| 6302 | |
| 6303 | spin_lock(&all_mddevs_lock); |
| 6304 | list_for_each(tmp,&all_mddevs) |
| 6305 | if (!l--) { |
| 6306 | mddev = list_entry(tmp, mddev_t, all_mddevs); |
| 6307 | mddev_get(mddev); |
| 6308 | spin_unlock(&all_mddevs_lock); |
| 6309 | return mddev; |
| 6310 | } |
| 6311 | spin_unlock(&all_mddevs_lock); |
| 6312 | if (!l--) |
| 6313 | return (void*)2;/* tail */ |
| 6314 | return NULL; |
| 6315 | } |
| 6316 | |
| 6317 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 6318 | { |
| 6319 | struct list_head *tmp; |
| 6320 | mddev_t *next_mddev, *mddev = v; |
| 6321 | |
| 6322 | ++*pos; |
| 6323 | if (v == (void*)2) |
| 6324 | return NULL; |
| 6325 | |
| 6326 | spin_lock(&all_mddevs_lock); |
| 6327 | if (v == (void*)1) |
| 6328 | tmp = all_mddevs.next; |
| 6329 | else |
| 6330 | tmp = mddev->all_mddevs.next; |
| 6331 | if (tmp != &all_mddevs) |
| 6332 | next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs)); |
| 6333 | else { |
| 6334 | next_mddev = (void*)2; |
| 6335 | *pos = 0x10000; |
| 6336 | } |
| 6337 | spin_unlock(&all_mddevs_lock); |
| 6338 | |
| 6339 | if (v != (void*)1) |
| 6340 | mddev_put(mddev); |
| 6341 | return next_mddev; |
| 6342 | |
| 6343 | } |
| 6344 | |
| 6345 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 6346 | { |
| 6347 | mddev_t *mddev = v; |
| 6348 | |
| 6349 | if (mddev && v != (void*)1 && v != (void*)2) |
| 6350 | mddev_put(mddev); |
| 6351 | } |
| 6352 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6353 | struct mdstat_info { |
| 6354 | int event; |
| 6355 | }; |
| 6356 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6357 | static int md_seq_show(struct seq_file *seq, void *v) |
| 6358 | { |
| 6359 | mddev_t *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6360 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6361 | mdk_rdev_t *rdev; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6362 | struct mdstat_info *mi = seq->private; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6363 | struct bitmap *bitmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6364 | |
| 6365 | if (v == (void*)1) { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6366 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6367 | seq_printf(seq, "Personalities : "); |
| 6368 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6369 | list_for_each_entry(pers, &pers_list, list) |
| 6370 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6371 | |
| 6372 | spin_unlock(&pers_lock); |
| 6373 | seq_printf(seq, "\n"); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6374 | mi->event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6375 | return 0; |
| 6376 | } |
| 6377 | if (v == (void*)2) { |
| 6378 | status_unused(seq); |
| 6379 | return 0; |
| 6380 | } |
| 6381 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6382 | if (mddev_lock(mddev) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6383 | return -EINTR; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6384 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6385 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 6386 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 6387 | mddev->pers ? "" : "in"); |
| 6388 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6389 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6390 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6391 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 6392 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6393 | seq_printf(seq, " %s", mddev->pers->name); |
| 6394 | } |
| 6395 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6396 | sectors = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6397 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6398 | char b[BDEVNAME_SIZE]; |
| 6399 | seq_printf(seq, " %s[%d]", |
| 6400 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6401 | if (test_bit(WriteMostly, &rdev->flags)) |
| 6402 | seq_printf(seq, "(W)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6403 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6404 | seq_printf(seq, "(F)"); |
| 6405 | continue; |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6406 | } else if (rdev->raid_disk < 0) |
| 6407 | seq_printf(seq, "(S)"); /* spare */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6408 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6409 | } |
| 6410 | |
| 6411 | if (!list_empty(&mddev->disks)) { |
| 6412 | if (mddev->pers) |
| 6413 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 6414 | (unsigned long long) |
| 6415 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6416 | else |
| 6417 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6418 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6419 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6420 | if (mddev->persistent) { |
| 6421 | if (mddev->major_version != 0 || |
| 6422 | mddev->minor_version != 90) { |
| 6423 | seq_printf(seq," super %d.%d", |
| 6424 | mddev->major_version, |
| 6425 | mddev->minor_version); |
| 6426 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6427 | } else if (mddev->external) |
| 6428 | seq_printf(seq, " super external:%s", |
| 6429 | mddev->metadata_type); |
| 6430 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6431 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6432 | |
| 6433 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6434 | mddev->pers->status(seq, mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6435 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6436 | if (mddev->pers->sync_request) { |
| 6437 | if (mddev->curr_resync > 2) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6438 | status_resync(seq, mddev); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6439 | seq_printf(seq, "\n "); |
| 6440 | } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2) |
| 6441 | seq_printf(seq, "\tresync=DELAYED\n "); |
| 6442 | else if (mddev->recovery_cp < MaxSector) |
| 6443 | seq_printf(seq, "\tresync=PENDING\n "); |
| 6444 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6445 | } else |
| 6446 | seq_printf(seq, "\n "); |
| 6447 | |
| 6448 | if ((bitmap = mddev->bitmap)) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6449 | unsigned long chunk_kb; |
| 6450 | unsigned long flags; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6451 | spin_lock_irqsave(&bitmap->lock, flags); |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 6452 | chunk_kb = mddev->bitmap_info.chunksize >> 10; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6453 | seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], " |
| 6454 | "%lu%s chunk", |
| 6455 | bitmap->pages - bitmap->missing_pages, |
| 6456 | bitmap->pages, |
| 6457 | (bitmap->pages - bitmap->missing_pages) |
| 6458 | << (PAGE_SHIFT - 10), |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 6459 | chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize, |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6460 | chunk_kb ? "KB" : "B"); |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 6461 | if (bitmap->file) { |
| 6462 | seq_printf(seq, ", file: "); |
Jan Blunck | c32c2f6 | 2008-02-14 19:38:43 -0800 | [diff] [blame] | 6463 | seq_path(seq, &bitmap->file->f_path, " \t\n"); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6464 | } |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 6465 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6466 | seq_printf(seq, "\n"); |
| 6467 | spin_unlock_irqrestore(&bitmap->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6468 | } |
| 6469 | |
| 6470 | seq_printf(seq, "\n"); |
| 6471 | } |
| 6472 | mddev_unlock(mddev); |
| 6473 | |
| 6474 | return 0; |
| 6475 | } |
| 6476 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 6477 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6478 | .start = md_seq_start, |
| 6479 | .next = md_seq_next, |
| 6480 | .stop = md_seq_stop, |
| 6481 | .show = md_seq_show, |
| 6482 | }; |
| 6483 | |
| 6484 | static int md_seq_open(struct inode *inode, struct file *file) |
| 6485 | { |
| 6486 | int error; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6487 | struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL); |
| 6488 | if (mi == NULL) |
| 6489 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6490 | |
| 6491 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6492 | if (error) |
| 6493 | kfree(mi); |
| 6494 | else { |
| 6495 | struct seq_file *p = file->private_data; |
| 6496 | p->private = mi; |
| 6497 | mi->event = atomic_read(&md_event_count); |
| 6498 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6499 | return error; |
| 6500 | } |
| 6501 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6502 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 6503 | { |
| 6504 | struct seq_file *m = filp->private_data; |
| 6505 | struct mdstat_info *mi = m->private; |
| 6506 | int mask; |
| 6507 | |
| 6508 | poll_wait(filp, &md_event_waiters, wait); |
| 6509 | |
| 6510 | /* always allow read */ |
| 6511 | mask = POLLIN | POLLRDNORM; |
| 6512 | |
| 6513 | if (mi->event != atomic_read(&md_event_count)) |
| 6514 | mask |= POLLERR | POLLPRI; |
| 6515 | return mask; |
| 6516 | } |
| 6517 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 6518 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 6519 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6520 | .open = md_seq_open, |
| 6521 | .read = seq_read, |
| 6522 | .llseek = seq_lseek, |
Martin Peschke | c3f94b40 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 6523 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6524 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6525 | }; |
| 6526 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6527 | int register_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6528 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6529 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6530 | list_add_tail(&p->list, &pers_list); |
| 6531 | 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] | 6532 | spin_unlock(&pers_lock); |
| 6533 | return 0; |
| 6534 | } |
| 6535 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6536 | int unregister_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6537 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6538 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6539 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6540 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6541 | spin_unlock(&pers_lock); |
| 6542 | return 0; |
| 6543 | } |
| 6544 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6545 | static int is_mddev_idle(mddev_t *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6546 | { |
| 6547 | mdk_rdev_t * rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6548 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6549 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6550 | |
| 6551 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6552 | rcu_read_lock(); |
| 6553 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6554 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6555 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
| 6556 | (int)part_stat_read(&disk->part0, sectors[1]) - |
| 6557 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 6558 | /* sync IO will cause sync_io to increase before the disk_stats |
| 6559 | * as sync_io is counted when a request starts, and |
| 6560 | * disk_stats is counted when it completes. |
| 6561 | * So resync activity will cause curr_events to be smaller than |
| 6562 | * when there was no such activity. |
| 6563 | * non-sync IO will cause disk_stat to increase without |
| 6564 | * increasing sync_io so curr_events will (eventually) |
| 6565 | * be larger than it was before. Once it becomes |
| 6566 | * substantially larger, the test below will cause |
| 6567 | * the array to appear non-idle, and resync will slow |
| 6568 | * down. |
| 6569 | * If there is a lot of outstanding resync activity when |
| 6570 | * we set last_event to curr_events, then all that activity |
| 6571 | * completing might cause the array to appear non-idle |
| 6572 | * and resync will be slowed down even though there might |
| 6573 | * not have been non-resync activity. This will only |
| 6574 | * happen once though. 'last_events' will soon reflect |
| 6575 | * the state where there is little or no outstanding |
| 6576 | * resync requests, and further resync activity will |
| 6577 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6578 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6579 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6580 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6581 | rdev->last_events = curr_events; |
| 6582 | idle = 0; |
| 6583 | } |
| 6584 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6585 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6586 | return idle; |
| 6587 | } |
| 6588 | |
| 6589 | void md_done_sync(mddev_t *mddev, int blocks, int ok) |
| 6590 | { |
| 6591 | /* another "blocks" (512byte) blocks have been synced */ |
| 6592 | atomic_sub(blocks, &mddev->recovery_active); |
| 6593 | wake_up(&mddev->recovery_wait); |
| 6594 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6595 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6596 | md_wakeup_thread(mddev->thread); |
| 6597 | // stop recovery, signal do_sync .... |
| 6598 | } |
| 6599 | } |
| 6600 | |
| 6601 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6602 | /* md_write_start(mddev, bi) |
| 6603 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6604 | * in superblock) before writing, schedule a superblock update |
| 6605 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6606 | */ |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6607 | void md_write_start(mddev_t *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6608 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6609 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6610 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6611 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6612 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6613 | BUG_ON(mddev->ro == 1); |
| 6614 | if (mddev->ro == 2) { |
| 6615 | /* need to switch to read/write */ |
| 6616 | mddev->ro = 0; |
| 6617 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6618 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 6619 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6620 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6621 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6622 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6623 | if (mddev->safemode == 1) |
| 6624 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6625 | if (mddev->in_sync) { |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6626 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6627 | if (mddev->in_sync) { |
| 6628 | mddev->in_sync = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6629 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 6630 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6631 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6632 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6633 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6634 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6635 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6636 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6637 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 6638 | wait_event(mddev->sb_wait, |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 6639 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6640 | } |
| 6641 | |
| 6642 | void md_write_end(mddev_t *mddev) |
| 6643 | { |
| 6644 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 6645 | if (mddev->safemode == 2) |
| 6646 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 6647 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6648 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 6649 | } |
| 6650 | } |
| 6651 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6652 | /* md_allow_write(mddev) |
| 6653 | * Calling this ensures that the array is marked 'active' so that writes |
| 6654 | * may proceed without blocking. It is important to call this before |
| 6655 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 6656 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6657 | * |
| 6658 | * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock |
| 6659 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6660 | */ |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6661 | int md_allow_write(mddev_t *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6662 | { |
| 6663 | if (!mddev->pers) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6664 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6665 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6666 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 6667 | if (!mddev->pers->sync_request) |
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 | |
| 6670 | spin_lock_irq(&mddev->write_lock); |
| 6671 | if (mddev->in_sync) { |
| 6672 | mddev->in_sync = 0; |
| 6673 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 6674 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6675 | if (mddev->safemode_delay && |
| 6676 | mddev->safemode == 0) |
| 6677 | mddev->safemode = 1; |
| 6678 | spin_unlock_irq(&mddev->write_lock); |
| 6679 | md_update_sb(mddev, 0); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6680 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6681 | } else |
| 6682 | spin_unlock_irq(&mddev->write_lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6683 | |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 6684 | if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6685 | return -EAGAIN; |
| 6686 | else |
| 6687 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6688 | } |
| 6689 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 6690 | |
NeilBrown | b63d7c2 | 2010-06-01 19:37:33 +1000 | [diff] [blame] | 6691 | void md_unplug(mddev_t *mddev) |
NeilBrown | 252ac52 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 6692 | { |
NeilBrown | 252ac52 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 6693 | if (mddev->plug) |
| 6694 | mddev->plug->unplug_fn(mddev->plug); |
| 6695 | } |
| 6696 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6697 | #define SYNC_MARKS 10 |
| 6698 | #define SYNC_MARK_STEP (3*HZ) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6699 | void md_do_sync(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6700 | { |
| 6701 | mddev_t *mddev2; |
| 6702 | unsigned int currspeed = 0, |
| 6703 | window; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6704 | sector_t max_sectors,j, io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6705 | unsigned long mark[SYNC_MARKS]; |
| 6706 | sector_t mark_cnt[SYNC_MARKS]; |
| 6707 | int last_mark,m; |
| 6708 | struct list_head *tmp; |
| 6709 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6710 | int skipped = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6711 | mdk_rdev_t *rdev; |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6712 | char *desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6713 | |
| 6714 | /* just incase thread restarts... */ |
| 6715 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 6716 | return; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6717 | if (mddev->ro) /* never try to sync a read-only array */ |
| 6718 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6719 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6720 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6721 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 6722 | desc = "data-check"; |
| 6723 | else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6724 | desc = "requested-resync"; |
| 6725 | else |
| 6726 | desc = "resync"; |
| 6727 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 6728 | desc = "reshape"; |
| 6729 | else |
| 6730 | desc = "recovery"; |
| 6731 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6732 | /* we overload curr_resync somewhat here. |
| 6733 | * 0 == not engaged in resync at all |
| 6734 | * 2 == checking that there is no conflict with another sync |
| 6735 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 6736 | * commense |
| 6737 | * other == active in resync - this many blocks |
| 6738 | * |
| 6739 | * Before starting a resync we must have set curr_resync to |
| 6740 | * 2, and then checked that every "conflicting" array has curr_resync |
| 6741 | * less than ours. When we find one that is the same or higher |
| 6742 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 6743 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 6744 | * This will mean we have to start checking from the beginning again. |
| 6745 | * |
| 6746 | */ |
| 6747 | |
| 6748 | do { |
| 6749 | mddev->curr_resync = 2; |
| 6750 | |
| 6751 | try_again: |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 6752 | if (kthread_should_stop()) |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 6753 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 6754 | |
| 6755 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6756 | goto skip; |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6757 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6758 | if (mddev2 == mddev) |
| 6759 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 6760 | if (!mddev->parallel_resync |
| 6761 | && mddev2->curr_resync |
| 6762 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6763 | DEFINE_WAIT(wq); |
| 6764 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 6765 | /* arbitrarily yield */ |
| 6766 | mddev->curr_resync = 1; |
| 6767 | wake_up(&resync_wait); |
| 6768 | } |
| 6769 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 6770 | /* no need to wait here, we can wait the next |
| 6771 | * time 'round when curr_resync == 2 |
| 6772 | */ |
| 6773 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 6774 | /* We need to wait 'interruptible' so as not to |
| 6775 | * contribute to the load average, and not to |
| 6776 | * be caught by 'softlockup' |
| 6777 | */ |
| 6778 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 6779 | if (!kthread_should_stop() && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 6780 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6781 | printk(KERN_INFO "md: delaying %s of %s" |
| 6782 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6783 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6784 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6785 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 6786 | if (signal_pending(current)) |
| 6787 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6788 | schedule(); |
| 6789 | finish_wait(&resync_wait, &wq); |
| 6790 | goto try_again; |
| 6791 | } |
| 6792 | finish_wait(&resync_wait, &wq); |
| 6793 | } |
| 6794 | } |
| 6795 | } while (mddev->curr_resync < 2); |
| 6796 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6797 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6798 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6799 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6800 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6801 | */ |
| 6802 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6803 | mddev->resync_mismatches = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6804 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6805 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6806 | j = mddev->resync_min; |
| 6807 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6808 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6809 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6810 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6811 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6812 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6813 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6814 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6815 | j = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6816 | rcu_read_lock(); |
| 6817 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6818 | if (rdev->raid_disk >= 0 && |
| 6819 | !test_bit(Faulty, &rdev->flags) && |
| 6820 | !test_bit(In_sync, &rdev->flags) && |
| 6821 | rdev->recovery_offset < j) |
| 6822 | j = rdev->recovery_offset; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6823 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6824 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6825 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6826 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 6827 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 6828 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 6829 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6830 | "(but not more than %d KB/sec) for %s.\n", |
| 6831 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6832 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6833 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6834 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6835 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6836 | for (m = 0; m < SYNC_MARKS; m++) { |
| 6837 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6838 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6839 | } |
| 6840 | last_mark = 0; |
| 6841 | mddev->resync_mark = mark[last_mark]; |
| 6842 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 6843 | |
| 6844 | /* |
| 6845 | * Tune reconstruction: |
| 6846 | */ |
| 6847 | window = 32*(PAGE_SIZE/512); |
| 6848 | printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n", |
| 6849 | window/2,(unsigned long long) max_sectors/2); |
| 6850 | |
| 6851 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6852 | last_check = 0; |
| 6853 | |
| 6854 | if (j>2) { |
| 6855 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6856 | "md: resuming %s of %s from checkpoint.\n", |
| 6857 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6858 | mddev->curr_resync = j; |
| 6859 | } |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 6860 | mddev->curr_resync_completed = j; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6861 | |
| 6862 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6863 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6864 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6865 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6866 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 6867 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 6868 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 6869 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 6870 | > (max_sectors >> 4)) || |
| 6871 | (j - mddev->curr_resync_completed)*2 |
| 6872 | >= mddev->resync_max - mddev->curr_resync_completed |
| 6873 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6874 | /* time to update curr_resync_completed */ |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6875 | wait_event(mddev->recovery_wait, |
| 6876 | atomic_read(&mddev->recovery_active) == 0); |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 6877 | mddev->curr_resync_completed = j; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 6878 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6879 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6880 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6881 | |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 6882 | while (j >= mddev->resync_max && !kthread_should_stop()) { |
| 6883 | /* As this condition is controlled by user-space, |
| 6884 | * we can block indefinitely, so use '_interruptible' |
| 6885 | * to avoid triggering warnings. |
| 6886 | */ |
| 6887 | flush_signals(current); /* just in case */ |
| 6888 | wait_event_interruptible(mddev->recovery_wait, |
| 6889 | mddev->resync_max > j |
| 6890 | || kthread_should_stop()); |
| 6891 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6892 | |
| 6893 | if (kthread_should_stop()) |
| 6894 | goto interrupted; |
| 6895 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6896 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6897 | currspeed < speed_min(mddev)); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6898 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6899 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6900 | goto out; |
| 6901 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6902 | |
| 6903 | if (!skipped) { /* actual IO requested */ |
| 6904 | io_sectors += sectors; |
| 6905 | atomic_add(sectors, &mddev->recovery_active); |
| 6906 | } |
| 6907 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6908 | j += sectors; |
| 6909 | if (j>1) mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6910 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6911 | if (last_check == 0) |
| 6912 | /* this is the earliers that rebuilt will be |
| 6913 | * visible in /proc/mdstat |
| 6914 | */ |
| 6915 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6916 | |
| 6917 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6918 | continue; |
| 6919 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6920 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6921 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6922 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6923 | break; |
| 6924 | |
| 6925 | repeat: |
| 6926 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 6927 | /* step marks */ |
| 6928 | int next = (last_mark+1) % SYNC_MARKS; |
| 6929 | |
| 6930 | mddev->resync_mark = mark[next]; |
| 6931 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 6932 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6933 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6934 | last_mark = next; |
| 6935 | } |
| 6936 | |
| 6937 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6938 | if (kthread_should_stop()) |
| 6939 | goto interrupted; |
| 6940 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6941 | |
| 6942 | /* |
| 6943 | * this loop exits only if either when we are slower than |
| 6944 | * the 'hard' speed limit, or the system was IO-idle for |
| 6945 | * a jiffy. |
| 6946 | * the system might be non-idle CPU-wise, but we only care |
| 6947 | * about not overloading the IO subsystem. (things like an |
| 6948 | * e2fsck being done on the RAID array should execute fast) |
| 6949 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6950 | cond_resched(); |
| 6951 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6952 | currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2 |
| 6953 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6954 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 6955 | if (currspeed > speed_min(mddev)) { |
| 6956 | if ((currspeed > speed_max(mddev)) || |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6957 | !is_mddev_idle(mddev, 0)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6958 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6959 | goto repeat; |
| 6960 | } |
| 6961 | } |
| 6962 | } |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6963 | printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6964 | /* |
| 6965 | * this also signals 'finished resyncing' to md_stop |
| 6966 | */ |
| 6967 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6968 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 6969 | |
| 6970 | /* tell personality that we are finished */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6971 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6972 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6973 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6974 | mddev->curr_resync > 2) { |
| 6975 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6976 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 6977 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 6978 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6979 | "md: checkpointing %s of %s.\n", |
| 6980 | desc, mdname(mddev)); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6981 | mddev->recovery_cp = mddev->curr_resync; |
| 6982 | } |
| 6983 | } else |
| 6984 | mddev->recovery_cp = MaxSector; |
| 6985 | } else { |
| 6986 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 6987 | mddev->curr_resync = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6988 | rcu_read_lock(); |
| 6989 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6990 | if (rdev->raid_disk >= 0 && |
NeilBrown | 70fffd0 | 2010-06-16 17:01:25 +1000 | [diff] [blame] | 6991 | mddev->delta_disks >= 0 && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6992 | !test_bit(Faulty, &rdev->flags) && |
| 6993 | !test_bit(In_sync, &rdev->flags) && |
| 6994 | rdev->recovery_offset < mddev->curr_resync) |
| 6995 | rdev->recovery_offset = mddev->curr_resync; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6996 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6997 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6998 | } |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 6999 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7000 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7001 | skip: |
NeilBrown | c07b70a | 2009-12-14 12:49:48 +1100 | [diff] [blame] | 7002 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 7003 | /* We completed so min/max setting can be forgotten if used. */ |
| 7004 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7005 | mddev->resync_min = 0; |
| 7006 | mddev->resync_max = MaxSector; |
| 7007 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7008 | mddev->resync_min = mddev->curr_resync_completed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7009 | mddev->curr_resync = 0; |
| 7010 | wake_up(&resync_wait); |
| 7011 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
| 7012 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 7013 | return; |
| 7014 | |
| 7015 | interrupted: |
| 7016 | /* |
| 7017 | * got a signal, exit. |
| 7018 | */ |
| 7019 | printk(KERN_INFO |
| 7020 | "md: md_do_sync() got signal ... exiting\n"); |
| 7021 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7022 | goto out; |
| 7023 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7024 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7025 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7026 | |
| 7027 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7028 | static int remove_and_add_spares(mddev_t *mddev) |
| 7029 | { |
| 7030 | mdk_rdev_t *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7031 | int spares = 0; |
| 7032 | |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7033 | mddev->curr_resync_completed = 0; |
| 7034 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 7035 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7036 | if (rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7037 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7038 | (test_bit(Faulty, &rdev->flags) || |
| 7039 | ! test_bit(In_sync, &rdev->flags)) && |
| 7040 | atomic_read(&rdev->nr_pending)==0) { |
| 7041 | if (mddev->pers->hot_remove_disk( |
| 7042 | mddev, rdev->raid_disk)==0) { |
| 7043 | char nm[20]; |
| 7044 | sprintf(nm,"rd%d", rdev->raid_disk); |
| 7045 | sysfs_remove_link(&mddev->kobj, nm); |
| 7046 | rdev->raid_disk = -1; |
| 7047 | } |
| 7048 | } |
| 7049 | |
NeilBrown | a8c42c7 | 2011-01-31 13:47:13 +1100 | [diff] [blame] | 7050 | if (mddev->degraded && !mddev->recovery_disabled) { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 7051 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7052 | if (rdev->raid_disk >= 0 && |
Dan Williams | e542713 | 2008-07-28 23:28:06 -0700 | [diff] [blame] | 7053 | !test_bit(In_sync, &rdev->flags) && |
| 7054 | !test_bit(Blocked, &rdev->flags)) |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7055 | spares++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7056 | if (rdev->raid_disk < 0 |
| 7057 | && !test_bit(Faulty, &rdev->flags)) { |
| 7058 | rdev->recovery_offset = 0; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 7059 | if (mddev->pers-> |
| 7060 | hot_add_disk(mddev, rdev) == 0) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7061 | char nm[20]; |
| 7062 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 7063 | if (sysfs_create_link(&mddev->kobj, |
| 7064 | &rdev->kobj, nm)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7065 | /* failure here is OK */; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7066 | spares++; |
| 7067 | md_new_event(mddev); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 7068 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7069 | } else |
| 7070 | break; |
| 7071 | } |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7072 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7073 | } |
| 7074 | return spares; |
| 7075 | } |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7076 | |
| 7077 | static void reap_sync_thread(mddev_t *mddev) |
| 7078 | { |
| 7079 | mdk_rdev_t *rdev; |
| 7080 | |
| 7081 | /* resync has finished, collect result */ |
| 7082 | md_unregister_thread(mddev->sync_thread); |
| 7083 | mddev->sync_thread = NULL; |
| 7084 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 7085 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
| 7086 | /* success...*/ |
| 7087 | /* activate any spares */ |
| 7088 | if (mddev->pers->spare_active(mddev)) |
| 7089 | sysfs_notify(&mddev->kobj, NULL, |
| 7090 | "degraded"); |
| 7091 | } |
| 7092 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 7093 | mddev->pers->finish_reshape) |
| 7094 | mddev->pers->finish_reshape(mddev); |
| 7095 | md_update_sb(mddev, 1); |
| 7096 | |
| 7097 | /* if array is no-longer degraded, then any saved_raid_disk |
| 7098 | * information must be scrapped |
| 7099 | */ |
| 7100 | if (!mddev->degraded) |
| 7101 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 7102 | rdev->saved_raid_disk = -1; |
| 7103 | |
| 7104 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7105 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7106 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 7107 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 7108 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 7109 | /* flag recovery needed just to double check */ |
| 7110 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7111 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 7112 | md_new_event(mddev); |
| 7113 | } |
| 7114 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7115 | /* |
| 7116 | * This routine is regularly called by all per-raid-array threads to |
| 7117 | * deal with generic issues like resync and super-block update. |
| 7118 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 7119 | * need this as they never do any recovery or update the superblock. |
| 7120 | * |
| 7121 | * It does not do any resync itself, but rather "forks" off other threads |
| 7122 | * to do that as needed. |
| 7123 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 7124 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7125 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7126 | * and wakeups up this thread which will reap the thread and finish up. |
| 7127 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 7128 | * |
| 7129 | * The overall approach is: |
| 7130 | * 1/ if the superblock needs updating, update it. |
| 7131 | * 2/ If a recovery thread is running, don't do anything else. |
| 7132 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 7133 | * 4/ If there are any faulty devices, remove them. |
| 7134 | * 5/ If array is degraded, try to add spares devices |
| 7135 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 7136 | */ |
| 7137 | void md_check_recovery(mddev_t *mddev) |
| 7138 | { |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 7139 | if (mddev->bitmap) |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 7140 | bitmap_daemon_work(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7141 | |
| 7142 | if (mddev->ro) |
| 7143 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7144 | |
| 7145 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7146 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7147 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 7148 | mdname(mddev)); |
| 7149 | mddev->safemode = 2; |
| 7150 | } |
| 7151 | flush_signals(current); |
| 7152 | } |
| 7153 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7154 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 7155 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7156 | if ( ! ( |
NeilBrown | 126925c | 2010-09-07 17:02:47 +1000 | [diff] [blame] | 7157 | (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7158 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7159 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7160 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7161 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 7162 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7163 | )) |
| 7164 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7165 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 7166 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7167 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7168 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7169 | if (mddev->ro) { |
| 7170 | /* Only thing we do on a ro array is remove |
| 7171 | * failed devices. |
| 7172 | */ |
NeilBrown | a8c42c7 | 2011-01-31 13:47:13 +1100 | [diff] [blame] | 7173 | mdk_rdev_t *rdev; |
| 7174 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 7175 | if (rdev->raid_disk >= 0 && |
| 7176 | !test_bit(Blocked, &rdev->flags) && |
| 7177 | test_bit(Faulty, &rdev->flags) && |
| 7178 | atomic_read(&rdev->nr_pending)==0) { |
| 7179 | if (mddev->pers->hot_remove_disk( |
| 7180 | mddev, rdev->raid_disk)==0) { |
| 7181 | char nm[20]; |
| 7182 | sprintf(nm,"rd%d", rdev->raid_disk); |
| 7183 | sysfs_remove_link(&mddev->kobj, nm); |
| 7184 | rdev->raid_disk = -1; |
| 7185 | } |
| 7186 | } |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7187 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7188 | goto unlock; |
| 7189 | } |
| 7190 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7191 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7192 | int did_change = 0; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7193 | spin_lock_irq(&mddev->write_lock); |
| 7194 | if (mddev->safemode && |
| 7195 | !atomic_read(&mddev->writes_pending) && |
| 7196 | !mddev->in_sync && |
| 7197 | mddev->recovery_cp == MaxSector) { |
| 7198 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7199 | did_change = 1; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7200 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7201 | } |
| 7202 | if (mddev->safemode == 1) |
| 7203 | mddev->safemode = 0; |
| 7204 | spin_unlock_irq(&mddev->write_lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7205 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7206 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7207 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7208 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7209 | if (mddev->flags) |
| 7210 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7211 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7212 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 7213 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 7214 | /* resync/recovery still happening */ |
| 7215 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7216 | goto unlock; |
| 7217 | } |
| 7218 | if (mddev->sync_thread) { |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7219 | reap_sync_thread(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7220 | goto unlock; |
| 7221 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7222 | /* Set RUNNING before clearing NEEDED to avoid |
| 7223 | * any transients in the value of "sync_action". |
| 7224 | */ |
| 7225 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7226 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7227 | /* Clear some bits that don't mean anything, but |
| 7228 | * might be left set |
| 7229 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7230 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7231 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7232 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7233 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 7234 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7235 | /* no recovery is running. |
| 7236 | * remove any failed drives, then |
| 7237 | * add spares if possible. |
| 7238 | * Spare are also removed and re-added, to allow |
| 7239 | * the personality to fail the re-add. |
| 7240 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7241 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7242 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 7243 | if (mddev->pers->check_reshape == NULL || |
| 7244 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7245 | /* Cannot proceed */ |
| 7246 | goto unlock; |
| 7247 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7248 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7249 | } else if ((spares = remove_and_add_spares(mddev))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7250 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7251 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 7252 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7253 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7254 | } else if (mddev->recovery_cp < MaxSector) { |
| 7255 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7256 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7257 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 7258 | /* nothing to be done ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7259 | goto unlock; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7260 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7261 | if (mddev->pers->sync_request) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 7262 | if (spares && mddev->bitmap && ! mddev->bitmap->file) { |
| 7263 | /* We are adding a device or devices to an array |
| 7264 | * which has the bitmap stored on all devices. |
| 7265 | * So make sure all bitmap pages get written |
| 7266 | */ |
| 7267 | bitmap_write_all(mddev->bitmap); |
| 7268 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7269 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 7270 | mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 7271 | "resync"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7272 | if (!mddev->sync_thread) { |
| 7273 | printk(KERN_ERR "%s: could not start resync" |
| 7274 | " thread...\n", |
| 7275 | mdname(mddev)); |
| 7276 | /* leave the spares where they are, it shouldn't hurt */ |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7277 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7278 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7279 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 7280 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 7281 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7282 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7283 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7284 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7285 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7286 | } |
| 7287 | unlock: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7288 | if (!mddev->sync_thread) { |
| 7289 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7290 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 7291 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 7292 | if (mddev->sysfs_action) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7293 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7294 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7295 | mddev_unlock(mddev); |
| 7296 | } |
| 7297 | } |
| 7298 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7299 | void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 7300 | { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7301 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7302 | wait_event_timeout(rdev->blocked_wait, |
| 7303 | !test_bit(Blocked, &rdev->flags), |
| 7304 | msecs_to_jiffies(5000)); |
| 7305 | rdev_dec_pending(rdev, mddev); |
| 7306 | } |
| 7307 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 7308 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7309 | static int md_notify_reboot(struct notifier_block *this, |
| 7310 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7311 | { |
| 7312 | struct list_head *tmp; |
| 7313 | mddev_t *mddev; |
| 7314 | |
| 7315 | if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) { |
| 7316 | |
| 7317 | printk(KERN_INFO "md: stopping all md devices.\n"); |
| 7318 | |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7319 | for_each_mddev(mddev, tmp) |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 7320 | if (mddev_trylock(mddev)) { |
NeilBrown | 2b25000 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7321 | /* Force a switch to readonly even array |
| 7322 | * appears to still be in use. Hence |
| 7323 | * the '100'. |
| 7324 | */ |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 7325 | md_set_readonly(mddev, 100); |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 7326 | mddev_unlock(mddev); |
| 7327 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7328 | /* |
| 7329 | * certain more exotic SCSI devices are known to be |
| 7330 | * volatile wrt too early system reboots. While the |
| 7331 | * right place to handle this issue is the given |
| 7332 | * driver, we do want to have a safe RAID driver ... |
| 7333 | */ |
| 7334 | mdelay(1000*1); |
| 7335 | } |
| 7336 | return NOTIFY_DONE; |
| 7337 | } |
| 7338 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7339 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7340 | .notifier_call = md_notify_reboot, |
| 7341 | .next = NULL, |
| 7342 | .priority = INT_MAX, /* before any real devices */ |
| 7343 | }; |
| 7344 | |
| 7345 | static void md_geninit(void) |
| 7346 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7347 | dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t)); |
| 7348 | |
Denis V. Lunev | c7705f344 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 7349 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7350 | } |
| 7351 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7352 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7353 | { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 7354 | int ret = -ENOMEM; |
| 7355 | |
Tejun Heo | ada609e | 2011-01-25 14:35:54 +0100 | [diff] [blame] | 7356 | md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 7357 | if (!md_wq) |
| 7358 | goto err_wq; |
| 7359 | |
| 7360 | md_misc_wq = alloc_workqueue("md_misc", 0, 0); |
| 7361 | if (!md_misc_wq) |
| 7362 | goto err_misc_wq; |
| 7363 | |
| 7364 | if ((ret = register_blkdev(MD_MAJOR, "md")) < 0) |
| 7365 | goto err_md; |
| 7366 | |
| 7367 | if ((ret = register_blkdev(0, "mdp")) < 0) |
| 7368 | goto err_mdp; |
| 7369 | mdp_major = ret; |
| 7370 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7371 | blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE, |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 7372 | md_probe, NULL, NULL); |
| 7373 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7374 | md_probe, NULL, NULL); |
| 7375 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7376 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 7377 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7378 | |
| 7379 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 7380 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7381 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 7382 | err_mdp: |
| 7383 | unregister_blkdev(MD_MAJOR, "md"); |
| 7384 | err_md: |
| 7385 | destroy_workqueue(md_misc_wq); |
| 7386 | err_misc_wq: |
| 7387 | destroy_workqueue(md_wq); |
| 7388 | err_wq: |
| 7389 | return ret; |
| 7390 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7391 | |
| 7392 | #ifndef MODULE |
| 7393 | |
| 7394 | /* |
| 7395 | * Searches all registered partitions for autorun RAID arrays |
| 7396 | * at boot time. |
| 7397 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7398 | |
| 7399 | static LIST_HEAD(all_detected_devices); |
| 7400 | struct detected_devices_node { |
| 7401 | struct list_head list; |
| 7402 | dev_t dev; |
| 7403 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7404 | |
| 7405 | void md_autodetect_dev(dev_t dev) |
| 7406 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7407 | struct detected_devices_node *node_detected_dev; |
| 7408 | |
| 7409 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 7410 | if (node_detected_dev) { |
| 7411 | node_detected_dev->dev = dev; |
| 7412 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 7413 | } else { |
| 7414 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 7415 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 7416 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7417 | } |
| 7418 | |
| 7419 | |
| 7420 | static void autostart_arrays(int part) |
| 7421 | { |
| 7422 | mdk_rdev_t *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7423 | struct detected_devices_node *node_detected_dev; |
| 7424 | dev_t dev; |
| 7425 | int i_scanned, i_passed; |
| 7426 | |
| 7427 | i_scanned = 0; |
| 7428 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7429 | |
| 7430 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 7431 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7432 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 7433 | i_scanned++; |
| 7434 | node_detected_dev = list_entry(all_detected_devices.next, |
| 7435 | struct detected_devices_node, list); |
| 7436 | list_del(&node_detected_dev->list); |
| 7437 | dev = node_detected_dev->dev; |
| 7438 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 7439 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7440 | if (IS_ERR(rdev)) |
| 7441 | continue; |
| 7442 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7443 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7444 | MD_BUG(); |
| 7445 | continue; |
| 7446 | } |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 7447 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7448 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7449 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7450 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7451 | |
| 7452 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 7453 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7454 | |
| 7455 | autorun_devices(part); |
| 7456 | } |
| 7457 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 7458 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7459 | |
| 7460 | static __exit void md_exit(void) |
| 7461 | { |
| 7462 | mddev_t *mddev; |
| 7463 | struct list_head *tmp; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 7464 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7465 | blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS); |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 7466 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7467 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7468 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7469 | unregister_blkdev(mdp_major, "mdp"); |
| 7470 | unregister_reboot_notifier(&md_notifier); |
| 7471 | unregister_sysctl_table(raid_table_header); |
| 7472 | remove_proc_entry("mdstat", NULL); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7473 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7474 | export_array(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7475 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7476 | } |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 7477 | destroy_workqueue(md_misc_wq); |
| 7478 | destroy_workqueue(md_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7479 | } |
| 7480 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 7481 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7482 | module_exit(md_exit) |
| 7483 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7484 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 7485 | { |
| 7486 | return sprintf(buffer, "%d", start_readonly); |
| 7487 | } |
| 7488 | static int set_ro(const char *val, struct kernel_param *kp) |
| 7489 | { |
| 7490 | char *e; |
| 7491 | int num = simple_strtoul(val, &e, 10); |
| 7492 | if (*val && (*e == '\0' || *e == '\n')) { |
| 7493 | start_readonly = num; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 7494 | return 0; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7495 | } |
| 7496 | return -EINVAL; |
| 7497 | } |
| 7498 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 7499 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 7500 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 7501 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7502 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7503 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7504 | EXPORT_SYMBOL(register_md_personality); |
| 7505 | EXPORT_SYMBOL(unregister_md_personality); |
| 7506 | EXPORT_SYMBOL(md_error); |
| 7507 | EXPORT_SYMBOL(md_done_sync); |
| 7508 | EXPORT_SYMBOL(md_write_start); |
| 7509 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7510 | EXPORT_SYMBOL(md_register_thread); |
| 7511 | EXPORT_SYMBOL(md_unregister_thread); |
| 7512 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7513 | EXPORT_SYMBOL(md_check_recovery); |
| 7514 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 7515 | MODULE_DESCRIPTION("MD RAID framework"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 7516 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 7517 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |