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); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 783 | submit_bio(REQ_WRITE | REQ_SYNC | REQ_UNPLUG | REQ_FLUSH | REQ_FUA, |
| 784 | bio); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | void md_super_wait(mddev_t *mddev) |
| 788 | { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 789 | /* wait for all superblock writes that were scheduled to complete */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 790 | DEFINE_WAIT(wq); |
| 791 | for(;;) { |
| 792 | prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE); |
| 793 | if (atomic_read(&mddev->pending_writes)==0) |
| 794 | break; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 795 | schedule(); |
| 796 | } |
| 797 | finish_wait(&mddev->sb_wait, &wq); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 798 | } |
| 799 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 800 | static void bi_complete(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | complete((struct completion*)bio->bi_private); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | } |
| 804 | |
NeilBrown | 2b19336 | 2010-10-27 15:16:40 +1100 | [diff] [blame] | 805 | 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] | 806 | struct page *page, int rw, bool metadata_op) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 808 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | struct completion event; |
| 810 | int ret; |
| 811 | |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 812 | rw |= REQ_SYNC | REQ_UNPLUG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 814 | bio->bi_bdev = (metadata_op && rdev->meta_bdev) ? |
| 815 | rdev->meta_bdev : rdev->bdev; |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 816 | if (metadata_op) |
| 817 | bio->bi_sector = sector + rdev->sb_start; |
| 818 | else |
| 819 | bio->bi_sector = sector + rdev->data_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | bio_add_page(bio, page, size, 0); |
| 821 | init_completion(&event); |
| 822 | bio->bi_private = &event; |
| 823 | bio->bi_end_io = bi_complete; |
| 824 | submit_bio(rw, bio); |
| 825 | wait_for_completion(&event); |
| 826 | |
| 827 | ret = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 828 | bio_put(bio); |
| 829 | return ret; |
| 830 | } |
NeilBrown | a8745db | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 831 | EXPORT_SYMBOL_GPL(sync_page_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 833 | static int read_disk_sb(mdk_rdev_t * rdev, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | { |
| 835 | char b[BDEVNAME_SIZE]; |
| 836 | if (!rdev->sb_page) { |
| 837 | MD_BUG(); |
| 838 | return -EINVAL; |
| 839 | } |
| 840 | if (rdev->sb_loaded) |
| 841 | return 0; |
| 842 | |
| 843 | |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 844 | if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | goto fail; |
| 846 | rdev->sb_loaded = 1; |
| 847 | return 0; |
| 848 | |
| 849 | fail: |
| 850 | printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n", |
| 851 | bdevname(rdev->bdev,b)); |
| 852 | return -EINVAL; |
| 853 | } |
| 854 | |
| 855 | static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 856 | { |
Andre Noll | 0571046 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 857 | return sb1->set_uuid0 == sb2->set_uuid0 && |
| 858 | sb1->set_uuid1 == sb2->set_uuid1 && |
| 859 | sb1->set_uuid2 == sb2->set_uuid2 && |
| 860 | sb1->set_uuid3 == sb2->set_uuid3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | } |
| 862 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 864 | { |
| 865 | int ret; |
| 866 | mdp_super_t *tmp1, *tmp2; |
| 867 | |
| 868 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); |
| 869 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); |
| 870 | |
| 871 | if (!tmp1 || !tmp2) { |
| 872 | ret = 0; |
Andre Noll | 35020f1 | 2008-03-23 15:10:33 +0100 | [diff] [blame] | 873 | printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | goto abort; |
| 875 | } |
| 876 | |
| 877 | *tmp1 = *sb1; |
| 878 | *tmp2 = *sb2; |
| 879 | |
| 880 | /* |
| 881 | * nr_disks is not constant |
| 882 | */ |
| 883 | tmp1->nr_disks = 0; |
| 884 | tmp2->nr_disks = 0; |
| 885 | |
Andre Noll | ce0c8e0 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 886 | ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | abort: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 888 | kfree(tmp1); |
| 889 | kfree(tmp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | return ret; |
| 891 | } |
| 892 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 893 | |
| 894 | static u32 md_csum_fold(u32 csum) |
| 895 | { |
| 896 | csum = (csum & 0xffff) + (csum >> 16); |
| 897 | return (csum & 0xffff) + (csum >> 16); |
| 898 | } |
| 899 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | static unsigned int calc_sb_csum(mdp_super_t * sb) |
| 901 | { |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 902 | u64 newcsum = 0; |
| 903 | u32 *sb32 = (u32*)sb; |
| 904 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | unsigned int disk_csum, csum; |
| 906 | |
| 907 | disk_csum = sb->sb_csum; |
| 908 | sb->sb_csum = 0; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 909 | |
| 910 | for (i = 0; i < MD_SB_BYTES/4 ; i++) |
| 911 | newcsum += sb32[i]; |
| 912 | csum = (newcsum & 0xffffffff) + (newcsum>>32); |
| 913 | |
| 914 | |
| 915 | #ifdef CONFIG_ALPHA |
| 916 | /* This used to use csum_partial, which was wrong for several |
| 917 | * reasons including that different results are returned on |
| 918 | * different architectures. It isn't critical that we get exactly |
| 919 | * the same return value as before (we always csum_fold before |
| 920 | * testing, and that removes any differences). However as we |
| 921 | * know that csum_partial always returned a 16bit value on |
| 922 | * alphas, do a fold to maximise conformity to previous behaviour. |
| 923 | */ |
| 924 | sb->sb_csum = md_csum_fold(disk_csum); |
| 925 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | sb->sb_csum = disk_csum; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 927 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | return csum; |
| 929 | } |
| 930 | |
| 931 | |
| 932 | /* |
| 933 | * Handle superblock details. |
| 934 | * We want to be able to handle multiple superblock formats |
| 935 | * so we have a common interface to them all, and an array of |
| 936 | * different handlers. |
| 937 | * We rely on user-space to write the initial superblock, and support |
| 938 | * reading and updating of superblocks. |
| 939 | * Interface methods are: |
| 940 | * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version) |
| 941 | * loads and validates a superblock on dev. |
| 942 | * if refdev != NULL, compare superblocks on both devices |
| 943 | * Return: |
| 944 | * 0 - dev has a superblock that is compatible with refdev |
| 945 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 946 | * so dev should be used as the refdev in future |
| 947 | * -EINVAL superblock incompatible or invalid |
| 948 | * -othererror e.g. -EIO |
| 949 | * |
| 950 | * int validate_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 951 | * Verify that dev is acceptable into mddev. |
| 952 | * The first time, mddev->raid_disks will be 0, and data from |
| 953 | * dev should be merged in. Subsequent calls check that dev |
| 954 | * is new enough. Return 0 or -EINVAL |
| 955 | * |
| 956 | * void sync_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 957 | * Update the superblock for rdev with data in mddev |
| 958 | * This does not write to disc. |
| 959 | * |
| 960 | */ |
| 961 | |
| 962 | struct super_type { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 963 | char *name; |
| 964 | struct module *owner; |
| 965 | int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, |
| 966 | int minor_version); |
| 967 | int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 968 | void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 969 | unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev, |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 970 | sector_t num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | }; |
| 972 | |
| 973 | /* |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 974 | * Check that the given mddev has no bitmap. |
| 975 | * |
| 976 | * This function is called from the run method of all personalities that do not |
| 977 | * support bitmaps. It prints an error message and returns non-zero if mddev |
| 978 | * has a bitmap. Otherwise, it returns 0. |
| 979 | * |
| 980 | */ |
| 981 | int md_check_no_bitmap(mddev_t *mddev) |
| 982 | { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 983 | if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 984 | return 0; |
| 985 | printk(KERN_ERR "%s: bitmaps are not supported for %s\n", |
| 986 | mdname(mddev), mddev->pers->name); |
| 987 | return 1; |
| 988 | } |
| 989 | EXPORT_SYMBOL(md_check_no_bitmap); |
| 990 | |
| 991 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | * load_super for 0.90.0 |
| 993 | */ |
| 994 | static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 995 | { |
| 996 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 997 | mdp_super_t *sb; |
| 998 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | |
| 1000 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1001 | * Calculate the position of the superblock (512byte sectors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | * it's at the end of the disk. |
| 1003 | * |
| 1004 | * It also happens to be a multiple of 4Kb. |
| 1005 | */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1006 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1008 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | if (ret) return ret; |
| 1010 | |
| 1011 | ret = -EINVAL; |
| 1012 | |
| 1013 | bdevname(rdev->bdev, b); |
| 1014 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 1015 | |
| 1016 | if (sb->md_magic != MD_SB_MAGIC) { |
| 1017 | printk(KERN_ERR "md: invalid raid superblock magic on %s\n", |
| 1018 | b); |
| 1019 | goto abort; |
| 1020 | } |
| 1021 | |
| 1022 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1023 | sb->minor_version < 90 || |
| 1024 | sb->minor_version > 91) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | printk(KERN_WARNING "Bad version number %d.%d on %s\n", |
| 1026 | sb->major_version, sb->minor_version, |
| 1027 | b); |
| 1028 | goto abort; |
| 1029 | } |
| 1030 | |
| 1031 | if (sb->raid_disks <= 0) |
| 1032 | goto abort; |
| 1033 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 1034 | 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] | 1035 | printk(KERN_WARNING "md: invalid superblock checksum on %s\n", |
| 1036 | b); |
| 1037 | goto abort; |
| 1038 | } |
| 1039 | |
| 1040 | rdev->preferred_minor = sb->md_minor; |
| 1041 | rdev->data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1042 | rdev->sb_size = MD_SB_BYTES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | |
| 1044 | if (sb->level == LEVEL_MULTIPATH) |
| 1045 | rdev->desc_nr = -1; |
| 1046 | else |
| 1047 | rdev->desc_nr = sb->this_disk.number; |
| 1048 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1049 | if (!refdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1051 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | __u64 ev1, ev2; |
| 1053 | mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page); |
| 1054 | if (!uuid_equal(refsb, sb)) { |
| 1055 | printk(KERN_WARNING "md: %s has different UUID to %s\n", |
| 1056 | b, bdevname(refdev->bdev,b2)); |
| 1057 | goto abort; |
| 1058 | } |
| 1059 | if (!sb_equal(refsb, sb)) { |
| 1060 | printk(KERN_WARNING "md: %s has same UUID" |
| 1061 | " but different superblock to %s\n", |
| 1062 | b, bdevname(refdev->bdev, b2)); |
| 1063 | goto abort; |
| 1064 | } |
| 1065 | ev1 = md_event(sb); |
| 1066 | ev2 = md_event(refsb); |
| 1067 | if (ev1 > ev2) |
| 1068 | ret = 1; |
| 1069 | else |
| 1070 | ret = 0; |
| 1071 | } |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 1072 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1074 | if (rdev->sectors < sb->size * 2 && sb->level > 1) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1075 | /* "this cannot possibly happen" ... */ |
| 1076 | ret = -EINVAL; |
| 1077 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | abort: |
| 1079 | return ret; |
| 1080 | } |
| 1081 | |
| 1082 | /* |
| 1083 | * validate_super for 0.90.0 |
| 1084 | */ |
| 1085 | static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1086 | { |
| 1087 | mdp_disk_t *desc; |
| 1088 | mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1089 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1090 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1091 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1092 | clear_bit(Faulty, &rdev->flags); |
| 1093 | clear_bit(In_sync, &rdev->flags); |
| 1094 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1095 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | if (mddev->raid_disks == 0) { |
| 1097 | mddev->major_version = 0; |
| 1098 | mddev->minor_version = sb->minor_version; |
| 1099 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1100 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1101 | mddev->chunk_sectors = sb->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | mddev->ctime = sb->ctime; |
| 1103 | mddev->utime = sb->utime; |
| 1104 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1105 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | mddev->layout = sb->layout; |
| 1107 | mddev->raid_disks = sb->raid_disks; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1108 | mddev->dev_sectors = sb->size * 2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1109 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1110 | mddev->bitmap_info.offset = 0; |
| 1111 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1112 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1113 | if (mddev->minor_version >= 91) { |
| 1114 | mddev->reshape_position = sb->reshape_position; |
| 1115 | mddev->delta_disks = sb->delta_disks; |
| 1116 | mddev->new_level = sb->new_level; |
| 1117 | mddev->new_layout = sb->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1118 | mddev->new_chunk_sectors = sb->new_chunk >> 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1119 | } else { |
| 1120 | mddev->reshape_position = MaxSector; |
| 1121 | mddev->delta_disks = 0; |
| 1122 | mddev->new_level = mddev->level; |
| 1123 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1124 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1125 | } |
| 1126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 1128 | mddev->recovery_cp = MaxSector; |
| 1129 | else { |
| 1130 | if (sb->events_hi == sb->cp_events_hi && |
| 1131 | sb->events_lo == sb->cp_events_lo) { |
| 1132 | mddev->recovery_cp = sb->recovery_cp; |
| 1133 | } else |
| 1134 | mddev->recovery_cp = 0; |
| 1135 | } |
| 1136 | |
| 1137 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 1138 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 1139 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 1140 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 1141 | |
| 1142 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1143 | |
| 1144 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1145 | mddev->bitmap_info.file == NULL) |
| 1146 | mddev->bitmap_info.offset = |
| 1147 | mddev->bitmap_info.default_offset; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1148 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1149 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1150 | /* Insist on good event counter while assembling, except |
| 1151 | * for spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1153 | if (sb->disks[rdev->desc_nr].state & ( |
| 1154 | (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE))) |
| 1155 | if (ev1 < mddev->events) |
| 1156 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1157 | } else if (mddev->bitmap) { |
| 1158 | /* if adding to array with a bitmap, then we can accept an |
| 1159 | * older device ... but not too old. |
| 1160 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1161 | if (ev1 < mddev->bitmap->events_cleared) |
| 1162 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1163 | } else { |
| 1164 | if (ev1 < mddev->events) |
| 1165 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1166 | return 0; |
| 1167 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1168 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1170 | desc = sb->disks + rdev->desc_nr; |
| 1171 | |
| 1172 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1173 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 1174 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 1175 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1176 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | rdev->raid_disk = desc->raid_disk; |
NeilBrown | 0261cd9 | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1178 | } else if (desc->state & (1<<MD_DISK_ACTIVE)) { |
| 1179 | /* active but not in sync implies recovery up to |
| 1180 | * reshape position. We don't know exactly where |
| 1181 | * that is, so set to zero for now */ |
| 1182 | if (mddev->minor_version >= 91) { |
| 1183 | rdev->recovery_offset = 0; |
| 1184 | rdev->raid_disk = desc->raid_disk; |
| 1185 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1187 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 1188 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1189 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1190 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1191 | return 0; |
| 1192 | } |
| 1193 | |
| 1194 | /* |
| 1195 | * sync_super for 0.90.0 |
| 1196 | */ |
| 1197 | static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1198 | { |
| 1199 | mdp_super_t *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | mdk_rdev_t *rdev2; |
| 1201 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1202 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1203 | |
| 1204 | /* make rdev->sb match mddev data.. |
| 1205 | * |
| 1206 | * 1/ zero out disks |
| 1207 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 1208 | * 3/ any empty disks < next_spare become removed |
| 1209 | * |
| 1210 | * disks[0] gets initialised to REMOVED because |
| 1211 | * we cannot be sure from other fields if it has |
| 1212 | * been initialised or not. |
| 1213 | */ |
| 1214 | int i; |
| 1215 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 1216 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 1217 | rdev->sb_size = MD_SB_BYTES; |
| 1218 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 1220 | |
| 1221 | memset(sb, 0, sizeof(*sb)); |
| 1222 | |
| 1223 | sb->md_magic = MD_SB_MAGIC; |
| 1224 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1225 | sb->patch_version = mddev->patch_version; |
| 1226 | sb->gvalid_words = 0; /* ignored */ |
| 1227 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 1228 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 1229 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 1230 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 1231 | |
| 1232 | sb->ctime = mddev->ctime; |
| 1233 | sb->level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1234 | sb->size = mddev->dev_sectors / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 | sb->raid_disks = mddev->raid_disks; |
| 1236 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1237 | sb->not_persistent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | sb->utime = mddev->utime; |
| 1239 | sb->state = 0; |
| 1240 | sb->events_hi = (mddev->events>>32); |
| 1241 | sb->events_lo = (u32)mddev->events; |
| 1242 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1243 | if (mddev->reshape_position == MaxSector) |
| 1244 | sb->minor_version = 90; |
| 1245 | else { |
| 1246 | sb->minor_version = 91; |
| 1247 | sb->reshape_position = mddev->reshape_position; |
| 1248 | sb->new_level = mddev->new_level; |
| 1249 | sb->delta_disks = mddev->delta_disks; |
| 1250 | sb->new_layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1251 | sb->new_chunk = mddev->new_chunk_sectors << 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1252 | } |
| 1253 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | if (mddev->in_sync) |
| 1255 | { |
| 1256 | sb->recovery_cp = mddev->recovery_cp; |
| 1257 | sb->cp_events_hi = (mddev->events>>32); |
| 1258 | sb->cp_events_lo = (u32)mddev->events; |
| 1259 | if (mddev->recovery_cp == MaxSector) |
| 1260 | sb->state = (1<< MD_SB_CLEAN); |
| 1261 | } else |
| 1262 | sb->recovery_cp = 0; |
| 1263 | |
| 1264 | sb->layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1265 | sb->chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1266 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1267 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1268 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 1269 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1270 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1271 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1272 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1273 | int desc_nr; |
NeilBrown | 0261cd9 | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1274 | int is_active = test_bit(In_sync, &rdev2->flags); |
| 1275 | |
| 1276 | if (rdev2->raid_disk >= 0 && |
| 1277 | sb->minor_version >= 91) |
| 1278 | /* we have nowhere to store the recovery_offset, |
| 1279 | * but if it is not below the reshape_position, |
| 1280 | * we can piggy-back on that. |
| 1281 | */ |
| 1282 | is_active = 1; |
| 1283 | if (rdev2->raid_disk < 0 || |
| 1284 | test_bit(Faulty, &rdev2->flags)) |
| 1285 | is_active = 0; |
| 1286 | if (is_active) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1287 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1288 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1289 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1290 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | d = &sb->disks[rdev2->desc_nr]; |
| 1292 | nr_disks++; |
| 1293 | d->number = rdev2->desc_nr; |
| 1294 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 1295 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | 0261cd9 | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1296 | if (is_active) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1297 | d->raid_disk = rdev2->raid_disk; |
| 1298 | else |
| 1299 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 1300 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1301 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 0261cd9 | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1302 | else if (is_active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1303 | d->state = (1<<MD_DISK_ACTIVE); |
NeilBrown | 0261cd9 | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1304 | if (test_bit(In_sync, &rdev2->flags)) |
| 1305 | d->state |= (1<<MD_DISK_SYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1306 | active++; |
| 1307 | working++; |
| 1308 | } else { |
| 1309 | d->state = 0; |
| 1310 | spare++; |
| 1311 | working++; |
| 1312 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1313 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 1314 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1315 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1316 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 1317 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 1318 | mdp_disk_t *d = &sb->disks[i]; |
| 1319 | if (d->state == 0 && d->number == 0) { |
| 1320 | d->number = i; |
| 1321 | d->raid_disk = i; |
| 1322 | d->state = (1<<MD_DISK_REMOVED); |
| 1323 | d->state |= (1<<MD_DISK_FAULTY); |
| 1324 | failed++; |
| 1325 | } |
| 1326 | } |
| 1327 | sb->nr_disks = nr_disks; |
| 1328 | sb->active_disks = active; |
| 1329 | sb->working_disks = working; |
| 1330 | sb->failed_disks = failed; |
| 1331 | sb->spare_disks = spare; |
| 1332 | |
| 1333 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 1334 | sb->sb_csum = calc_sb_csum(sb); |
| 1335 | } |
| 1336 | |
| 1337 | /* |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1338 | * rdev_size_change for 0.90.0 |
| 1339 | */ |
| 1340 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1341 | 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] | 1342 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1343 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1344 | return 0; /* component must fit device */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1345 | if (rdev->mddev->bitmap_info.offset) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1346 | return 0; /* can't move bitmap */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1347 | rdev->sb_start = calc_dev_sboffset(rdev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1348 | if (!num_sectors || num_sectors > rdev->sb_start) |
| 1349 | num_sectors = rdev->sb_start; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1350 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1351 | rdev->sb_page); |
| 1352 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1353 | return num_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1354 | } |
| 1355 | |
| 1356 | |
| 1357 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1358 | * version 1 superblock |
| 1359 | */ |
| 1360 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1361 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1362 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1363 | __le32 disk_csum; |
| 1364 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1365 | unsigned long long newcsum; |
| 1366 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1367 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1368 | int i; |
| 1369 | |
| 1370 | disk_csum = sb->sb_csum; |
| 1371 | sb->sb_csum = 0; |
| 1372 | newcsum = 0; |
| 1373 | for (i=0; size>=4; size -= 4 ) |
| 1374 | newcsum += le32_to_cpu(*isuper++); |
| 1375 | |
| 1376 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1377 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1378 | |
| 1379 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1380 | sb->sb_csum = disk_csum; |
| 1381 | return cpu_to_le32(csum); |
| 1382 | } |
| 1383 | |
| 1384 | static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 1385 | { |
| 1386 | struct mdp_superblock_1 *sb; |
| 1387 | int ret; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1388 | sector_t sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1390 | int bmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | |
| 1392 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1393 | * Calculate the position of the superblock in 512byte sectors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1394 | * It is always aligned to a 4K boundary and |
| 1395 | * depeding on minor_version, it can be: |
| 1396 | * 0: At least 8K, but less than 12K, from end of device |
| 1397 | * 1: At start of device |
| 1398 | * 2: 4K from start of device. |
| 1399 | */ |
| 1400 | switch(minor_version) { |
| 1401 | case 0: |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1402 | sb_start = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1403 | sb_start -= 8*2; |
| 1404 | sb_start &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1405 | break; |
| 1406 | case 1: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1407 | sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1408 | break; |
| 1409 | case 2: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1410 | sb_start = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1411 | break; |
| 1412 | default: |
| 1413 | return -EINVAL; |
| 1414 | } |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1415 | rdev->sb_start = sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1416 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1417 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1418 | * and it is safe to read 4k, so we do that |
| 1419 | */ |
| 1420 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1421 | if (ret) return ret; |
| 1422 | |
| 1423 | |
| 1424 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1425 | |
| 1426 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1427 | sb->major_version != cpu_to_le32(1) || |
| 1428 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1429 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1430 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1431 | return -EINVAL; |
| 1432 | |
| 1433 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
| 1434 | printk("md: invalid superblock checksum on %s\n", |
| 1435 | bdevname(rdev->bdev,b)); |
| 1436 | return -EINVAL; |
| 1437 | } |
| 1438 | if (le64_to_cpu(sb->data_size) < 10) { |
| 1439 | printk("md: data_size too small on %s\n", |
| 1440 | bdevname(rdev->bdev,b)); |
| 1441 | return -EINVAL; |
| 1442 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1443 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1444 | rdev->preferred_minor = 0xffff; |
| 1445 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1446 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1447 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1448 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1449 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1450 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1451 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1452 | |
| 1453 | if (minor_version |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1454 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1455 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1456 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1457 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1458 | rdev->desc_nr = -1; |
| 1459 | else |
| 1460 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1461 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1462 | if (!refdev) { |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1463 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1464 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1465 | __u64 ev1, ev2; |
| 1466 | struct mdp_superblock_1 *refsb = |
| 1467 | (struct mdp_superblock_1*)page_address(refdev->sb_page); |
| 1468 | |
| 1469 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1470 | sb->level != refsb->level || |
| 1471 | sb->layout != refsb->layout || |
| 1472 | sb->chunksize != refsb->chunksize) { |
| 1473 | printk(KERN_WARNING "md: %s has strangely different" |
| 1474 | " superblock to %s\n", |
| 1475 | bdevname(rdev->bdev,b), |
| 1476 | bdevname(refdev->bdev,b2)); |
| 1477 | return -EINVAL; |
| 1478 | } |
| 1479 | ev1 = le64_to_cpu(sb->events); |
| 1480 | ev2 = le64_to_cpu(refsb->events); |
| 1481 | |
| 1482 | if (ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1483 | ret = 1; |
| 1484 | else |
| 1485 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1486 | } |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1487 | if (minor_version) |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1488 | rdev->sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) - |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1489 | le64_to_cpu(sb->data_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1490 | else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1491 | rdev->sectors = rdev->sb_start; |
| 1492 | if (rdev->sectors < le64_to_cpu(sb->data_size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1493 | return -EINVAL; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1494 | rdev->sectors = le64_to_cpu(sb->data_size); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1495 | if (le64_to_cpu(sb->size) > rdev->sectors) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1496 | return -EINVAL; |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1497 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1498 | } |
| 1499 | |
| 1500 | static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1501 | { |
| 1502 | 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] | 1503 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1505 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1506 | clear_bit(Faulty, &rdev->flags); |
| 1507 | clear_bit(In_sync, &rdev->flags); |
| 1508 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1509 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1510 | if (mddev->raid_disks == 0) { |
| 1511 | mddev->major_version = 1; |
| 1512 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1513 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1514 | mddev->chunk_sectors = le32_to_cpu(sb->chunksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1515 | mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1); |
| 1516 | mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1); |
| 1517 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1518 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1519 | mddev->layout = le32_to_cpu(sb->layout); |
| 1520 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1521 | mddev->dev_sectors = le64_to_cpu(sb->size); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1522 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1523 | mddev->bitmap_info.offset = 0; |
| 1524 | mddev->bitmap_info.default_offset = 1024 >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1525 | |
| 1526 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1527 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1528 | |
| 1529 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1530 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1531 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1532 | mddev->bitmap_info.file == NULL ) |
| 1533 | mddev->bitmap_info.offset = |
| 1534 | (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1535 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1536 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1537 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1538 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1539 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1540 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1541 | mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1542 | } else { |
| 1543 | mddev->reshape_position = MaxSector; |
| 1544 | mddev->delta_disks = 0; |
| 1545 | mddev->new_level = mddev->level; |
| 1546 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1547 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1548 | } |
| 1549 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1550 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1551 | /* Insist of good event counter while assembling, except for |
| 1552 | * spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1553 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1554 | if (rdev->desc_nr >= 0 && |
| 1555 | rdev->desc_nr < le32_to_cpu(sb->max_dev) && |
| 1556 | le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe) |
| 1557 | if (ev1 < mddev->events) |
| 1558 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1559 | } else if (mddev->bitmap) { |
| 1560 | /* If adding to array with a bitmap, then we can accept an |
| 1561 | * older device, but not too old. |
| 1562 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1563 | if (ev1 < mddev->bitmap->events_cleared) |
| 1564 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1565 | } else { |
| 1566 | if (ev1 < mddev->events) |
| 1567 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1568 | return 0; |
| 1569 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1570 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1571 | int role; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1572 | if (rdev->desc_nr < 0 || |
| 1573 | rdev->desc_nr >= le32_to_cpu(sb->max_dev)) { |
| 1574 | role = 0xffff; |
| 1575 | rdev->desc_nr = -1; |
| 1576 | } else |
| 1577 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1578 | switch(role) { |
| 1579 | case 0xffff: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1580 | break; |
| 1581 | case 0xfffe: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1582 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1583 | break; |
| 1584 | default: |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1585 | if ((le32_to_cpu(sb->feature_map) & |
| 1586 | MD_FEATURE_RECOVERY_OFFSET)) |
| 1587 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 1588 | else |
| 1589 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1590 | rdev->raid_disk = role; |
| 1591 | break; |
| 1592 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1593 | if (sb->devflags & WriteMostly1) |
| 1594 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1595 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1596 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1597 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1598 | return 0; |
| 1599 | } |
| 1600 | |
| 1601 | static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1602 | { |
| 1603 | struct mdp_superblock_1 *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1604 | mdk_rdev_t *rdev2; |
| 1605 | int max_dev, i; |
| 1606 | /* make rdev->sb match mddev and rdev data. */ |
| 1607 | |
| 1608 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1609 | |
| 1610 | sb->feature_map = 0; |
| 1611 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1612 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1613 | memset(sb->pad1, 0, sizeof(sb->pad1)); |
| 1614 | memset(sb->pad2, 0, sizeof(sb->pad2)); |
| 1615 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1616 | |
| 1617 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1618 | sb->events = cpu_to_le64(mddev->events); |
| 1619 | if (mddev->in_sync) |
| 1620 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
| 1621 | else |
| 1622 | sb->resync_offset = cpu_to_le64(0); |
| 1623 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1624 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1625 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1626 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1627 | sb->size = cpu_to_le64(mddev->dev_sectors); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1628 | sb->chunksize = cpu_to_le32(mddev->chunk_sectors); |
NeilBrown | 62e1e38 | 2009-05-26 09:40:59 +1000 | [diff] [blame] | 1629 | sb->level = cpu_to_le32(mddev->level); |
| 1630 | sb->layout = cpu_to_le32(mddev->layout); |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1631 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1632 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) { |
| 1633 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1634 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1635 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1636 | |
| 1637 | if (rdev->raid_disk >= 0 && |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1638 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1639 | sb->feature_map |= |
| 1640 | cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 1641 | sb->recovery_offset = |
| 1642 | cpu_to_le64(rdev->recovery_offset); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1643 | } |
| 1644 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1645 | if (mddev->reshape_position != MaxSector) { |
| 1646 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 1647 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1648 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1649 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1650 | sb->new_level = cpu_to_le32(mddev->new_level); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1651 | sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1652 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1653 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1654 | max_dev = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1655 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1656 | if (rdev2->desc_nr+1 > max_dev) |
| 1657 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1658 | |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1659 | if (max_dev > le32_to_cpu(sb->max_dev)) { |
| 1660 | int bmask; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1661 | sb->max_dev = cpu_to_le32(max_dev); |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1662 | rdev->sb_size = max_dev * 2 + 256; |
| 1663 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
| 1664 | if (rdev->sb_size & bmask) |
| 1665 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
NeilBrown | ddcf352 | 2010-09-08 16:48:17 +1000 | [diff] [blame] | 1666 | } else |
| 1667 | max_dev = le32_to_cpu(sb->max_dev); |
| 1668 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1669 | for (i=0; i<max_dev;i++) |
| 1670 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
| 1671 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1672 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1673 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1674 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1675 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1676 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1677 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1678 | else if (rdev2->raid_disk >= 0) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1679 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1680 | else |
| 1681 | sb->dev_roles[i] = cpu_to_le16(0xffff); |
| 1682 | } |
| 1683 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1684 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1685 | } |
| 1686 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1687 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1688 | 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] | 1689 | { |
| 1690 | struct mdp_superblock_1 *sb; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1691 | sector_t max_sectors; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1692 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1693 | return 0; /* component must fit device */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1694 | if (rdev->sb_start < rdev->data_offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1695 | /* minor versions 1 and 2; superblock before data */ |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1696 | max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1697 | max_sectors -= rdev->data_offset; |
| 1698 | if (!num_sectors || num_sectors > max_sectors) |
| 1699 | num_sectors = max_sectors; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1700 | } else if (rdev->mddev->bitmap_info.offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1701 | /* minor version 0 with bitmap we can't move */ |
| 1702 | return 0; |
| 1703 | } else { |
| 1704 | /* minor version 0; superblock after data */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1705 | sector_t sb_start; |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1706 | sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1707 | sb_start &= ~(sector_t)(4*2 - 1); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1708 | max_sectors = rdev->sectors + sb_start - rdev->sb_start; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1709 | if (!num_sectors || num_sectors > max_sectors) |
| 1710 | num_sectors = max_sectors; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1711 | rdev->sb_start = sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1712 | } |
| 1713 | sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1714 | sb->data_size = cpu_to_le64(num_sectors); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1715 | sb->super_offset = rdev->sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1716 | sb->sb_csum = calc_sb_1_csum(sb); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1717 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1718 | rdev->sb_page); |
| 1719 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1720 | return num_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1721 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1722 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 1723 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1724 | [0] = { |
| 1725 | .name = "0.90.0", |
| 1726 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1727 | .load_super = super_90_load, |
| 1728 | .validate_super = super_90_validate, |
| 1729 | .sync_super = super_90_sync, |
| 1730 | .rdev_size_change = super_90_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1731 | }, |
| 1732 | [1] = { |
| 1733 | .name = "md-1", |
| 1734 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1735 | .load_super = super_1_load, |
| 1736 | .validate_super = super_1_validate, |
| 1737 | .sync_super = super_1_sync, |
| 1738 | .rdev_size_change = super_1_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1739 | }, |
| 1740 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1741 | |
| 1742 | static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2) |
| 1743 | { |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1744 | mdk_rdev_t *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1745 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1746 | rcu_read_lock(); |
| 1747 | rdev_for_each_rcu(rdev, mddev1) |
| 1748 | rdev_for_each_rcu(rdev2, mddev2) |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1749 | if (rdev->bdev->bd_contains == |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1750 | rdev2->bdev->bd_contains) { |
| 1751 | rcu_read_unlock(); |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1752 | return 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1753 | } |
| 1754 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1755 | return 0; |
| 1756 | } |
| 1757 | |
| 1758 | static LIST_HEAD(pending_raid_disks); |
| 1759 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1760 | /* |
| 1761 | * Try to register data integrity profile for an mddev |
| 1762 | * |
| 1763 | * This is called when an array is started and after a disk has been kicked |
| 1764 | * from the array. It only succeeds if all working and active component devices |
| 1765 | * are integrity capable with matching profiles. |
| 1766 | */ |
| 1767 | int md_integrity_register(mddev_t *mddev) |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1768 | { |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1769 | mdk_rdev_t *rdev, *reference = NULL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1770 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1771 | if (list_empty(&mddev->disks)) |
| 1772 | return 0; /* nothing to do */ |
| 1773 | if (blk_get_integrity(mddev->gendisk)) |
| 1774 | return 0; /* already registered */ |
| 1775 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 1776 | /* skip spares and non-functional disks */ |
| 1777 | if (test_bit(Faulty, &rdev->flags)) |
| 1778 | continue; |
| 1779 | if (rdev->raid_disk < 0) |
| 1780 | continue; |
| 1781 | /* |
| 1782 | * If at least one rdev is not integrity capable, we can not |
| 1783 | * enable data integrity for the md device. |
| 1784 | */ |
| 1785 | if (!bdev_get_integrity(rdev->bdev)) |
| 1786 | return -EINVAL; |
| 1787 | if (!reference) { |
| 1788 | /* Use the first rdev as the reference */ |
| 1789 | reference = rdev; |
| 1790 | continue; |
| 1791 | } |
| 1792 | /* does this rdev's profile match the reference profile? */ |
| 1793 | if (blk_integrity_compare(reference->bdev->bd_disk, |
| 1794 | rdev->bdev->bd_disk) < 0) |
| 1795 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1796 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1797 | /* |
| 1798 | * All component devices are integrity capable and have matching |
| 1799 | * profiles, register the common profile for the md device. |
| 1800 | */ |
| 1801 | if (blk_integrity_register(mddev->gendisk, |
| 1802 | bdev_get_integrity(reference->bdev)) != 0) { |
| 1803 | printk(KERN_ERR "md: failed to register integrity for %s\n", |
| 1804 | mdname(mddev)); |
| 1805 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1806 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1807 | printk(KERN_NOTICE "md: data integrity on %s enabled\n", |
| 1808 | mdname(mddev)); |
| 1809 | return 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1810 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1811 | EXPORT_SYMBOL(md_integrity_register); |
| 1812 | |
| 1813 | /* Disable data integrity if non-capable/non-matching disk is being added */ |
| 1814 | void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 1815 | { |
| 1816 | struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev); |
| 1817 | struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk); |
| 1818 | |
| 1819 | if (!bi_mddev) /* nothing to do */ |
| 1820 | return; |
| 1821 | if (rdev->raid_disk < 0) /* skip spares */ |
| 1822 | return; |
| 1823 | if (bi_rdev && blk_integrity_compare(mddev->gendisk, |
| 1824 | rdev->bdev->bd_disk) >= 0) |
| 1825 | return; |
| 1826 | printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev)); |
| 1827 | blk_integrity_unregister(mddev->gendisk); |
| 1828 | } |
| 1829 | EXPORT_SYMBOL(md_integrity_add_rdev); |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1830 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1831 | static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev) |
| 1832 | { |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1833 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 1834 | struct kobject *ko; |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1835 | char *s; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1836 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1837 | |
| 1838 | if (rdev->mddev) { |
| 1839 | MD_BUG(); |
| 1840 | return -EINVAL; |
| 1841 | } |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1842 | |
| 1843 | /* prevent duplicates */ |
| 1844 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 1845 | return -EEXIST; |
| 1846 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1847 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
| 1848 | if (rdev->sectors && (mddev->dev_sectors == 0 || |
| 1849 | rdev->sectors < mddev->dev_sectors)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1850 | if (mddev->pers) { |
| 1851 | /* Cannot change size, so fail |
| 1852 | * If mddev->level <= 0, then we don't care |
| 1853 | * about aligning sizes (e.g. linear) |
| 1854 | */ |
| 1855 | if (mddev->level > 0) |
| 1856 | return -ENOSPC; |
| 1857 | } else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1858 | mddev->dev_sectors = rdev->sectors; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1859 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1860 | |
| 1861 | /* Verify rdev->desc_nr is unique. |
| 1862 | * If it is -1, assign a free number, else |
| 1863 | * check number is not in use |
| 1864 | */ |
| 1865 | if (rdev->desc_nr < 0) { |
| 1866 | int choice = 0; |
| 1867 | if (mddev->pers) choice = mddev->raid_disks; |
| 1868 | while (find_rdev_nr(mddev, choice)) |
| 1869 | choice++; |
| 1870 | rdev->desc_nr = choice; |
| 1871 | } else { |
| 1872 | if (find_rdev_nr(mddev, rdev->desc_nr)) |
| 1873 | return -EBUSY; |
| 1874 | } |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 1875 | if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
| 1876 | printk(KERN_WARNING "md: %s: array is limited to %d devices\n", |
| 1877 | mdname(mddev), mddev->max_disks); |
| 1878 | return -EBUSY; |
| 1879 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1880 | bdevname(rdev->bdev,b); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1881 | while ( (s=strchr(b, '/')) != NULL) |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1882 | *s = '!'; |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1883 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1884 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1885 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1886 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1887 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1888 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1889 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1890 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 1891 | if (sysfs_create_link(&rdev->kobj, ko, "block")) |
| 1892 | /* failure here is OK */; |
| 1893 | rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 1894 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1895 | list_add_rcu(&rdev->same_set, &mddev->disks); |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1896 | bd_link_disk_holder(rdev->bdev, mddev->gendisk); |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 1897 | |
| 1898 | /* May as well allow recovery to be retried once */ |
| 1899 | mddev->recovery_disabled = 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1900 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1901 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1902 | |
| 1903 | fail: |
| 1904 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", |
| 1905 | b, mdname(mddev)); |
| 1906 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1907 | } |
| 1908 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1909 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1910 | { |
| 1911 | mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work); |
| 1912 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1913 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1914 | } |
| 1915 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1916 | static void unbind_rdev_from_array(mdk_rdev_t * rdev) |
| 1917 | { |
| 1918 | char b[BDEVNAME_SIZE]; |
| 1919 | if (!rdev->mddev) { |
| 1920 | MD_BUG(); |
| 1921 | return; |
| 1922 | } |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 1923 | bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1924 | list_del_rcu(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1925 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 1926 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1927 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 1928 | sysfs_put(rdev->sysfs_state); |
| 1929 | rdev->sysfs_state = NULL; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1930 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1931 | * writing to 'remove' to "dev/state". We also need |
| 1932 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1933 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1934 | synchronize_rcu(); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1935 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 1936 | kobject_get(&rdev->kobj); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 1937 | queue_work(md_misc_wq, &rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1938 | } |
| 1939 | |
| 1940 | /* |
| 1941 | * prevent the device from being mounted, repartitioned or |
| 1942 | * otherwise reused by a RAID array (or any other kernel |
| 1943 | * subsystem), by bd_claiming the device. |
| 1944 | */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1945 | 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] | 1946 | { |
| 1947 | int err = 0; |
| 1948 | struct block_device *bdev; |
| 1949 | char b[BDEVNAME_SIZE]; |
| 1950 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1951 | bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, |
| 1952 | shared ? (mdk_rdev_t *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1953 | if (IS_ERR(bdev)) { |
| 1954 | printk(KERN_ERR "md: could not open %s.\n", |
| 1955 | __bdevname(dev, b)); |
| 1956 | return PTR_ERR(bdev); |
| 1957 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1958 | rdev->bdev = bdev; |
| 1959 | return err; |
| 1960 | } |
| 1961 | |
| 1962 | static void unlock_rdev(mdk_rdev_t *rdev) |
| 1963 | { |
| 1964 | struct block_device *bdev = rdev->bdev; |
| 1965 | rdev->bdev = NULL; |
| 1966 | if (!bdev) |
| 1967 | MD_BUG(); |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1968 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1969 | } |
| 1970 | |
| 1971 | void md_autodetect_dev(dev_t dev); |
| 1972 | |
| 1973 | static void export_rdev(mdk_rdev_t * rdev) |
| 1974 | { |
| 1975 | char b[BDEVNAME_SIZE]; |
| 1976 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 1977 | bdevname(rdev->bdev,b)); |
| 1978 | if (rdev->mddev) |
| 1979 | MD_BUG(); |
| 1980 | free_disk_sb(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1981 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1982 | if (test_bit(AutoDetected, &rdev->flags)) |
| 1983 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1984 | #endif |
| 1985 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1986 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1987 | } |
| 1988 | |
| 1989 | static void kick_rdev_from_array(mdk_rdev_t * rdev) |
| 1990 | { |
| 1991 | unbind_rdev_from_array(rdev); |
| 1992 | export_rdev(rdev); |
| 1993 | } |
| 1994 | |
| 1995 | static void export_array(mddev_t *mddev) |
| 1996 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1997 | mdk_rdev_t *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1998 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1999 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2000 | if (!rdev->mddev) { |
| 2001 | MD_BUG(); |
| 2002 | continue; |
| 2003 | } |
| 2004 | kick_rdev_from_array(rdev); |
| 2005 | } |
| 2006 | if (!list_empty(&mddev->disks)) |
| 2007 | MD_BUG(); |
| 2008 | mddev->raid_disks = 0; |
| 2009 | mddev->major_version = 0; |
| 2010 | } |
| 2011 | |
| 2012 | static void print_desc(mdp_disk_t *desc) |
| 2013 | { |
| 2014 | printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number, |
| 2015 | desc->major,desc->minor,desc->raid_disk,desc->state); |
| 2016 | } |
| 2017 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2018 | static void print_sb_90(mdp_super_t *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2019 | { |
| 2020 | int i; |
| 2021 | |
| 2022 | printk(KERN_INFO |
| 2023 | "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n", |
| 2024 | sb->major_version, sb->minor_version, sb->patch_version, |
| 2025 | sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3, |
| 2026 | sb->ctime); |
| 2027 | printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n", |
| 2028 | sb->level, sb->size, sb->nr_disks, sb->raid_disks, |
| 2029 | sb->md_minor, sb->layout, sb->chunk_size); |
| 2030 | printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d" |
| 2031 | " FD:%d SD:%d CSUM:%08x E:%08lx\n", |
| 2032 | sb->utime, sb->state, sb->active_disks, sb->working_disks, |
| 2033 | sb->failed_disks, sb->spare_disks, |
| 2034 | sb->sb_csum, (unsigned long)sb->events_lo); |
| 2035 | |
| 2036 | printk(KERN_INFO); |
| 2037 | for (i = 0; i < MD_SB_DISKS; i++) { |
| 2038 | mdp_disk_t *desc; |
| 2039 | |
| 2040 | desc = sb->disks + i; |
| 2041 | if (desc->number || desc->major || desc->minor || |
| 2042 | desc->raid_disk || (desc->state && (desc->state != 4))) { |
| 2043 | printk(" D %2d: ", i); |
| 2044 | print_desc(desc); |
| 2045 | } |
| 2046 | } |
| 2047 | printk(KERN_INFO "md: THIS: "); |
| 2048 | print_desc(&sb->this_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2049 | } |
| 2050 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2051 | static void print_sb_1(struct mdp_superblock_1 *sb) |
| 2052 | { |
| 2053 | __u8 *uuid; |
| 2054 | |
| 2055 | uuid = sb->set_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2056 | printk(KERN_INFO |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2057 | "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2058 | "md: Name: \"%s\" CT:%llu\n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2059 | le32_to_cpu(sb->major_version), |
| 2060 | le32_to_cpu(sb->feature_map), |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2061 | uuid, |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2062 | sb->set_name, |
| 2063 | (unsigned long long)le64_to_cpu(sb->ctime) |
| 2064 | & MD_SUPERBLOCK_1_TIME_SEC_MASK); |
| 2065 | |
| 2066 | uuid = sb->device_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2067 | printk(KERN_INFO |
| 2068 | "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] | 2069 | " RO:%llu\n" |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2070 | "md: Dev:%08x UUID: %pU\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2071 | "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n" |
| 2072 | "md: (MaxDev:%u) \n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2073 | le32_to_cpu(sb->level), |
| 2074 | (unsigned long long)le64_to_cpu(sb->size), |
| 2075 | le32_to_cpu(sb->raid_disks), |
| 2076 | le32_to_cpu(sb->layout), |
| 2077 | le32_to_cpu(sb->chunksize), |
| 2078 | (unsigned long long)le64_to_cpu(sb->data_offset), |
| 2079 | (unsigned long long)le64_to_cpu(sb->data_size), |
| 2080 | (unsigned long long)le64_to_cpu(sb->super_offset), |
| 2081 | (unsigned long long)le64_to_cpu(sb->recovery_offset), |
| 2082 | le32_to_cpu(sb->dev_number), |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2083 | uuid, |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2084 | sb->devflags, |
| 2085 | (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK, |
| 2086 | (unsigned long long)le64_to_cpu(sb->events), |
| 2087 | (unsigned long long)le64_to_cpu(sb->resync_offset), |
| 2088 | le32_to_cpu(sb->sb_csum), |
| 2089 | le32_to_cpu(sb->max_dev) |
| 2090 | ); |
| 2091 | } |
| 2092 | |
| 2093 | static void print_rdev(mdk_rdev_t *rdev, int major_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2094 | { |
| 2095 | char b[BDEVNAME_SIZE]; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2096 | printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n", |
| 2097 | bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors, |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2098 | test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags), |
| 2099 | rdev->desc_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2100 | if (rdev->sb_loaded) { |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2101 | printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version); |
| 2102 | switch (major_version) { |
| 2103 | case 0: |
| 2104 | print_sb_90((mdp_super_t*)page_address(rdev->sb_page)); |
| 2105 | break; |
| 2106 | case 1: |
| 2107 | print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page)); |
| 2108 | break; |
| 2109 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2110 | } else |
| 2111 | printk(KERN_INFO "md: no rdev superblock!\n"); |
| 2112 | } |
| 2113 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 2114 | static void md_print_devices(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2115 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2116 | struct list_head *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2117 | mdk_rdev_t *rdev; |
| 2118 | mddev_t *mddev; |
| 2119 | char b[BDEVNAME_SIZE]; |
| 2120 | |
| 2121 | printk("\n"); |
| 2122 | printk("md: **********************************\n"); |
| 2123 | printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n"); |
| 2124 | printk("md: **********************************\n"); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2125 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2126 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 2127 | if (mddev->bitmap) |
| 2128 | bitmap_print_sb(mddev->bitmap); |
| 2129 | else |
| 2130 | printk("%s: ", mdname(mddev)); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2131 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2132 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 2133 | printk("\n"); |
| 2134 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2135 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2136 | print_rdev(rdev, mddev->major_version); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2137 | } |
| 2138 | printk("md: **********************************\n"); |
| 2139 | printk("\n"); |
| 2140 | } |
| 2141 | |
| 2142 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2143 | static void sync_sbs(mddev_t * mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2144 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2145 | /* Update each superblock (in-memory image), but |
| 2146 | * if we are allowed to, skip spares which already |
| 2147 | * have the right event counter, or have one earlier |
| 2148 | * (which would mean they aren't being marked as dirty |
| 2149 | * with the rest of the array) |
| 2150 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2151 | mdk_rdev_t *rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2152 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2153 | if (rdev->sb_events == mddev->events || |
| 2154 | (nospares && |
| 2155 | rdev->raid_disk < 0 && |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2156 | rdev->sb_events+1 == mddev->events)) { |
| 2157 | /* Don't update this superblock */ |
| 2158 | rdev->sb_loaded = 2; |
| 2159 | } else { |
| 2160 | super_types[mddev->major_version]. |
| 2161 | sync_super(mddev, rdev); |
| 2162 | rdev->sb_loaded = 1; |
| 2163 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2164 | } |
| 2165 | } |
| 2166 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2167 | static void md_update_sb(mddev_t * mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2168 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2169 | mdk_rdev_t *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2170 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2171 | int nospares = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2172 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2173 | repeat: |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2174 | /* First make sure individual recovery_offsets are correct */ |
| 2175 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 2176 | if (rdev->raid_disk >= 0 && |
| 2177 | mddev->delta_disks >= 0 && |
| 2178 | !test_bit(In_sync, &rdev->flags) && |
| 2179 | mddev->curr_resync_completed > rdev->recovery_offset) |
| 2180 | rdev->recovery_offset = mddev->curr_resync_completed; |
| 2181 | |
| 2182 | } |
Dan Williams | bd52b74 | 2010-08-30 17:33:33 +1000 | [diff] [blame] | 2183 | if (!mddev->persistent) { |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 2184 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2185 | clear_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | d97a41d | 2010-10-28 17:30:20 +1100 | [diff] [blame] | 2186 | if (!mddev->external) |
| 2187 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2188 | wake_up(&mddev->sb_wait); |
| 2189 | return; |
| 2190 | } |
| 2191 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2192 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2193 | |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2194 | mddev->utime = get_seconds(); |
| 2195 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2196 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 2197 | force_change = 1; |
| 2198 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 2199 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 2200 | * though if events isn't the right even/odd, we will have to do |
| 2201 | * spares after all |
| 2202 | */ |
| 2203 | nospares = 1; |
| 2204 | if (force_change) |
| 2205 | nospares = 0; |
| 2206 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2207 | /* If the array is degraded, then skipping spares is both |
| 2208 | * dangerous and fairly pointless. |
| 2209 | * Dangerous because a device that was removed from the array |
| 2210 | * might have a event_count that still looks up-to-date, |
| 2211 | * so it can be re-added without a resync. |
| 2212 | * Pointless because if there are any spares to skip, |
| 2213 | * then a recovery will happen and soon that array won't |
| 2214 | * be degraded any more and the spare can go back to sleep then. |
| 2215 | */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2216 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2217 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2218 | sync_req = mddev->in_sync; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2219 | |
| 2220 | /* If this is just a dirty<->clean transition, and the array is clean |
| 2221 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2222 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2223 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2224 | && mddev->can_decrease_events |
| 2225 | && mddev->events != 1) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2226 | mddev->events--; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2227 | mddev->can_decrease_events = 0; |
| 2228 | } else { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2229 | /* otherwise we have to go forward and ... */ |
| 2230 | mddev->events ++; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2231 | mddev->can_decrease_events = nospares; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2232 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2233 | |
| 2234 | if (!mddev->events) { |
| 2235 | /* |
| 2236 | * oops, this 64-bit counter should never wrap. |
| 2237 | * Either we are in around ~1 trillion A.C., assuming |
| 2238 | * 1 reboot per second, or we have a bug: |
| 2239 | */ |
| 2240 | MD_BUG(); |
| 2241 | mddev->events --; |
| 2242 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2243 | sync_sbs(mddev, nospares); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2244 | spin_unlock_irq(&mddev->write_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2245 | |
| 2246 | dprintk(KERN_INFO |
| 2247 | "md: updating %s RAID superblock on device (in sync %d)\n", |
| 2248 | mdname(mddev),mddev->in_sync); |
| 2249 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 2250 | bitmap_update_sb(mddev->bitmap); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2251 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2252 | char b[BDEVNAME_SIZE]; |
| 2253 | dprintk(KERN_INFO "md: "); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2254 | if (rdev->sb_loaded != 1) |
| 2255 | continue; /* no noise on spare devices */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2256 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2257 | dprintk("(skipping faulty "); |
| 2258 | |
| 2259 | dprintk("%s ", bdevname(rdev->bdev,b)); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2260 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2261 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2262 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2263 | rdev->sb_page); |
| 2264 | dprintk(KERN_INFO "(write) %s's sb offset: %llu\n", |
| 2265 | bdevname(rdev->bdev,b), |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2266 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2267 | rdev->sb_events = mddev->events; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2268 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2269 | } else |
| 2270 | dprintk(")\n"); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2271 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2272 | /* only need to write one superblock... */ |
| 2273 | break; |
| 2274 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2275 | md_super_wait(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2276 | /* 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] | 2277 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2278 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2279 | if (mddev->in_sync != sync_req || |
| 2280 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2281 | /* have to write it out again */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2282 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2283 | goto repeat; |
| 2284 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2285 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2286 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2287 | wake_up(&mddev->sb_wait); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 2288 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 2289 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2290 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2291 | } |
| 2292 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 2293 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2294 | * We want to accept with case. For this we use cmd_match. |
| 2295 | */ |
| 2296 | static int cmd_match(const char *cmd, const char *str) |
| 2297 | { |
| 2298 | /* See if cmd, written into a sysfs file, matches |
| 2299 | * str. They must either be the same, or cmd can |
| 2300 | * have a trailing newline |
| 2301 | */ |
| 2302 | while (*cmd && *str && *cmd == *str) { |
| 2303 | cmd++; |
| 2304 | str++; |
| 2305 | } |
| 2306 | if (*cmd == '\n') |
| 2307 | cmd++; |
| 2308 | if (*str || *cmd) |
| 2309 | return 0; |
| 2310 | return 1; |
| 2311 | } |
| 2312 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2313 | struct rdev_sysfs_entry { |
| 2314 | struct attribute attr; |
| 2315 | ssize_t (*show)(mdk_rdev_t *, char *); |
| 2316 | ssize_t (*store)(mdk_rdev_t *, const char *, size_t); |
| 2317 | }; |
| 2318 | |
| 2319 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2320 | state_show(mdk_rdev_t *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2321 | { |
| 2322 | char *sep = ""; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2323 | size_t len = 0; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2324 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2325 | if (test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2326 | len+= sprintf(page+len, "%sfaulty",sep); |
| 2327 | sep = ","; |
| 2328 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2329 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2330 | len += sprintf(page+len, "%sin_sync",sep); |
| 2331 | sep = ","; |
| 2332 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2333 | if (test_bit(WriteMostly, &rdev->flags)) { |
| 2334 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 2335 | sep = ","; |
| 2336 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2337 | if (test_bit(Blocked, &rdev->flags)) { |
| 2338 | len += sprintf(page+len, "%sblocked", sep); |
| 2339 | sep = ","; |
| 2340 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2341 | if (!test_bit(Faulty, &rdev->flags) && |
| 2342 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2343 | len += sprintf(page+len, "%sspare", sep); |
| 2344 | sep = ","; |
| 2345 | } |
| 2346 | return len+sprintf(page+len, "\n"); |
| 2347 | } |
| 2348 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2349 | static ssize_t |
| 2350 | state_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2351 | { |
| 2352 | /* can write |
| 2353 | * faulty - simulates and error |
| 2354 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2355 | * writemostly - sets write_mostly |
| 2356 | * -writemostly - clears write_mostly |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2357 | * blocked - sets the Blocked flag |
| 2358 | * -blocked - clears the Blocked flag |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2359 | * insync - sets Insync providing device isn't active |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2360 | */ |
| 2361 | int err = -EINVAL; |
| 2362 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 2363 | md_error(rdev->mddev, rdev); |
| 2364 | err = 0; |
| 2365 | } else if (cmd_match(buf, "remove")) { |
| 2366 | if (rdev->raid_disk >= 0) |
| 2367 | err = -EBUSY; |
| 2368 | else { |
| 2369 | mddev_t *mddev = rdev->mddev; |
| 2370 | kick_rdev_from_array(rdev); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2371 | if (mddev->pers) |
| 2372 | md_update_sb(mddev, 1); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2373 | md_new_event(mddev); |
| 2374 | err = 0; |
| 2375 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2376 | } else if (cmd_match(buf, "writemostly")) { |
| 2377 | set_bit(WriteMostly, &rdev->flags); |
| 2378 | err = 0; |
| 2379 | } else if (cmd_match(buf, "-writemostly")) { |
| 2380 | clear_bit(WriteMostly, &rdev->flags); |
| 2381 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2382 | } else if (cmd_match(buf, "blocked")) { |
| 2383 | set_bit(Blocked, &rdev->flags); |
| 2384 | err = 0; |
| 2385 | } else if (cmd_match(buf, "-blocked")) { |
| 2386 | clear_bit(Blocked, &rdev->flags); |
| 2387 | wake_up(&rdev->blocked_wait); |
| 2388 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2389 | md_wakeup_thread(rdev->mddev->thread); |
| 2390 | |
| 2391 | err = 0; |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2392 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { |
| 2393 | set_bit(In_sync, &rdev->flags); |
| 2394 | err = 0; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2395 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2396 | if (!err) |
| 2397 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2398 | return err ? err : len; |
| 2399 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2400 | static struct rdev_sysfs_entry rdev_state = |
| 2401 | __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2402 | |
| 2403 | static ssize_t |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2404 | errors_show(mdk_rdev_t *rdev, char *page) |
| 2405 | { |
| 2406 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 2407 | } |
| 2408 | |
| 2409 | static ssize_t |
| 2410 | errors_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2411 | { |
| 2412 | char *e; |
| 2413 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2414 | if (*buf && (*e == 0 || *e == '\n')) { |
| 2415 | atomic_set(&rdev->corrected_errors, n); |
| 2416 | return len; |
| 2417 | } |
| 2418 | return -EINVAL; |
| 2419 | } |
| 2420 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2421 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2422 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2423 | static ssize_t |
| 2424 | slot_show(mdk_rdev_t *rdev, char *page) |
| 2425 | { |
| 2426 | if (rdev->raid_disk < 0) |
| 2427 | return sprintf(page, "none\n"); |
| 2428 | else |
| 2429 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 2430 | } |
| 2431 | |
| 2432 | static ssize_t |
| 2433 | slot_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2434 | { |
| 2435 | char *e; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2436 | int err; |
| 2437 | char nm[20]; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2438 | int slot = simple_strtoul(buf, &e, 10); |
| 2439 | if (strncmp(buf, "none", 4)==0) |
| 2440 | slot = -1; |
| 2441 | else if (e==buf || (*e && *e!= '\n')) |
| 2442 | return -EINVAL; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2443 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2444 | /* Setting 'slot' on an active array requires also |
| 2445 | * updating the 'rd%d' link, and communicating |
| 2446 | * with the personality with ->hot_*_disk. |
| 2447 | * For now we only support removing |
| 2448 | * failed/spare devices. This normally happens automatically, |
| 2449 | * but not when the metadata is externally managed. |
| 2450 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2451 | if (rdev->raid_disk == -1) |
| 2452 | return -EEXIST; |
| 2453 | /* personality does all needed checks */ |
| 2454 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2455 | return -EINVAL; |
| 2456 | err = rdev->mddev->pers-> |
| 2457 | hot_remove_disk(rdev->mddev, rdev->raid_disk); |
| 2458 | if (err) |
| 2459 | return err; |
| 2460 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2461 | sysfs_remove_link(&rdev->mddev->kobj, nm); |
Maciej Trela | b710310 | 2010-04-14 16:58:16 +1000 | [diff] [blame] | 2462 | rdev->raid_disk = -1; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2463 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2464 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2465 | } else if (rdev->mddev->pers) { |
| 2466 | mdk_rdev_t *rdev2; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2467 | /* Activating a spare .. or possibly reactivating |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2468 | * if we ever get bitmaps working here. |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2469 | */ |
| 2470 | |
| 2471 | if (rdev->raid_disk != -1) |
| 2472 | return -EBUSY; |
| 2473 | |
NeilBrown | c6751b2 | 2011-02-02 11:57:13 +1100 | [diff] [blame] | 2474 | if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery)) |
| 2475 | return -EBUSY; |
| 2476 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2477 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2478 | return -EINVAL; |
| 2479 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2480 | list_for_each_entry(rdev2, &rdev->mddev->disks, same_set) |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2481 | if (rdev2->raid_disk == slot) |
| 2482 | return -EEXIST; |
| 2483 | |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2484 | if (slot >= rdev->mddev->raid_disks && |
| 2485 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
| 2486 | return -ENOSPC; |
| 2487 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2488 | rdev->raid_disk = slot; |
| 2489 | if (test_bit(In_sync, &rdev->flags)) |
| 2490 | rdev->saved_raid_disk = slot; |
| 2491 | else |
| 2492 | rdev->saved_raid_disk = -1; |
| 2493 | err = rdev->mddev->pers-> |
| 2494 | hot_add_disk(rdev->mddev, rdev); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 2495 | if (err) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2496 | rdev->raid_disk = -1; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2497 | return err; |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 2498 | } else |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2499 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2500 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2501 | if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2502 | /* failure here is OK */; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2503 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2504 | } else { |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2505 | if (slot >= rdev->mddev->raid_disks && |
| 2506 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2507 | return -ENOSPC; |
| 2508 | rdev->raid_disk = slot; |
| 2509 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2510 | clear_bit(Faulty, &rdev->flags); |
| 2511 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2512 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2513 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2514 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2515 | return len; |
| 2516 | } |
| 2517 | |
| 2518 | |
| 2519 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2520 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2521 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2522 | static ssize_t |
| 2523 | offset_show(mdk_rdev_t *rdev, char *page) |
| 2524 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2525 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2526 | } |
| 2527 | |
| 2528 | static ssize_t |
| 2529 | offset_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2530 | { |
| 2531 | char *e; |
| 2532 | unsigned long long offset = simple_strtoull(buf, &e, 10); |
| 2533 | if (e==buf || (*e && *e != '\n')) |
| 2534 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2535 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2536 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2537 | if (rdev->sectors && rdev->mddev->external) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2538 | /* Must set offset before size, so overlap checks |
| 2539 | * can be sane */ |
| 2540 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2541 | rdev->data_offset = offset; |
| 2542 | return len; |
| 2543 | } |
| 2544 | |
| 2545 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2546 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2547 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2548 | static ssize_t |
| 2549 | rdev_size_show(mdk_rdev_t *rdev, char *page) |
| 2550 | { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2551 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2552 | } |
| 2553 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2554 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 2555 | { |
| 2556 | /* check if two start/length pairs overlap */ |
| 2557 | if (s1+l1 <= s2) |
| 2558 | return 0; |
| 2559 | if (s2+l2 <= s1) |
| 2560 | return 0; |
| 2561 | return 1; |
| 2562 | } |
| 2563 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2564 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
| 2565 | { |
| 2566 | unsigned long long blocks; |
| 2567 | sector_t new; |
| 2568 | |
| 2569 | if (strict_strtoull(buf, 10, &blocks) < 0) |
| 2570 | return -EINVAL; |
| 2571 | |
| 2572 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) |
| 2573 | return -EINVAL; /* sector conversion overflow */ |
| 2574 | |
| 2575 | new = blocks * 2; |
| 2576 | if (new != blocks * 2) |
| 2577 | return -EINVAL; /* unsigned long long to sector_t overflow */ |
| 2578 | |
| 2579 | *sectors = new; |
| 2580 | return 0; |
| 2581 | } |
| 2582 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2583 | static ssize_t |
| 2584 | rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2585 | { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2586 | mddev_t *my_mddev = rdev->mddev; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2587 | sector_t oldsectors = rdev->sectors; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2588 | sector_t sectors; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2589 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2590 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2591 | return -EINVAL; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2592 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2593 | if (my_mddev->persistent) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2594 | sectors = super_types[my_mddev->major_version]. |
| 2595 | rdev_size_change(rdev, sectors); |
| 2596 | if (!sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2597 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2598 | } else if (!sectors) |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 2599 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) - |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2600 | rdev->data_offset; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2601 | } |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2602 | if (sectors < my_mddev->dev_sectors) |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 2603 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2604 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2605 | rdev->sectors = sectors; |
| 2606 | if (sectors > oldsectors && my_mddev->external) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2607 | /* need to check that all other rdevs with the same ->bdev |
| 2608 | * do not overlap. We need to unlock the mddev to avoid |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2609 | * a deadlock. We have already changed rdev->sectors, and if |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2610 | * we have to change it back, we will have the lock again. |
| 2611 | */ |
| 2612 | mddev_t *mddev; |
| 2613 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2614 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2615 | |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2616 | mddev_unlock(my_mddev); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2617 | for_each_mddev(mddev, tmp) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2618 | mdk_rdev_t *rdev2; |
| 2619 | |
| 2620 | mddev_lock(mddev); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2621 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
NeilBrown | f21e9ff | 2011-01-31 12:10:09 +1100 | [diff] [blame] | 2622 | if (rdev->bdev == rdev2->bdev && |
| 2623 | rdev != rdev2 && |
| 2624 | overlaps(rdev->data_offset, rdev->sectors, |
| 2625 | rdev2->data_offset, |
| 2626 | rdev2->sectors)) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2627 | overlap = 1; |
| 2628 | break; |
| 2629 | } |
| 2630 | mddev_unlock(mddev); |
| 2631 | if (overlap) { |
| 2632 | mddev_put(mddev); |
| 2633 | break; |
| 2634 | } |
| 2635 | } |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2636 | mddev_lock(my_mddev); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2637 | if (overlap) { |
| 2638 | /* Someone else could have slipped in a size |
| 2639 | * change here, but doing so is just silly. |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2640 | * We put oldsectors back because we *know* it is |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2641 | * safe, and trust userspace not to race with |
| 2642 | * itself |
| 2643 | */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2644 | rdev->sectors = oldsectors; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2645 | return -EBUSY; |
| 2646 | } |
| 2647 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2648 | return len; |
| 2649 | } |
| 2650 | |
| 2651 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2652 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2653 | |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2654 | |
| 2655 | static ssize_t recovery_start_show(mdk_rdev_t *rdev, char *page) |
| 2656 | { |
| 2657 | unsigned long long recovery_start = rdev->recovery_offset; |
| 2658 | |
| 2659 | if (test_bit(In_sync, &rdev->flags) || |
| 2660 | recovery_start == MaxSector) |
| 2661 | return sprintf(page, "none\n"); |
| 2662 | |
| 2663 | return sprintf(page, "%llu\n", recovery_start); |
| 2664 | } |
| 2665 | |
| 2666 | static ssize_t recovery_start_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2667 | { |
| 2668 | unsigned long long recovery_start; |
| 2669 | |
| 2670 | if (cmd_match(buf, "none")) |
| 2671 | recovery_start = MaxSector; |
| 2672 | else if (strict_strtoull(buf, 10, &recovery_start)) |
| 2673 | return -EINVAL; |
| 2674 | |
| 2675 | if (rdev->mddev->pers && |
| 2676 | rdev->raid_disk >= 0) |
| 2677 | return -EBUSY; |
| 2678 | |
| 2679 | rdev->recovery_offset = recovery_start; |
| 2680 | if (recovery_start == MaxSector) |
| 2681 | set_bit(In_sync, &rdev->flags); |
| 2682 | else |
| 2683 | clear_bit(In_sync, &rdev->flags); |
| 2684 | return len; |
| 2685 | } |
| 2686 | |
| 2687 | static struct rdev_sysfs_entry rdev_recovery_start = |
| 2688 | __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store); |
| 2689 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2690 | static struct attribute *rdev_default_attrs[] = { |
| 2691 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2692 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2693 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2694 | &rdev_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2695 | &rdev_size.attr, |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2696 | &rdev_recovery_start.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2697 | NULL, |
| 2698 | }; |
| 2699 | static ssize_t |
| 2700 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 2701 | { |
| 2702 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2703 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2704 | mddev_t *mddev = rdev->mddev; |
| 2705 | ssize_t rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2706 | |
| 2707 | if (!entry->show) |
| 2708 | return -EIO; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2709 | |
| 2710 | rv = mddev ? mddev_lock(mddev) : -EBUSY; |
| 2711 | if (!rv) { |
| 2712 | if (rdev->mddev == NULL) |
| 2713 | rv = -EBUSY; |
| 2714 | else |
| 2715 | rv = entry->show(rdev, page); |
| 2716 | mddev_unlock(mddev); |
| 2717 | } |
| 2718 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2719 | } |
| 2720 | |
| 2721 | static ssize_t |
| 2722 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 2723 | const char *page, size_t length) |
| 2724 | { |
| 2725 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2726 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2727 | ssize_t rv; |
| 2728 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2729 | |
| 2730 | if (!entry->store) |
| 2731 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 2732 | if (!capable(CAP_SYS_ADMIN)) |
| 2733 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2734 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2735 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2736 | if (rdev->mddev == NULL) |
| 2737 | rv = -EBUSY; |
| 2738 | else |
| 2739 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 2740 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2741 | } |
| 2742 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2743 | } |
| 2744 | |
| 2745 | static void rdev_free(struct kobject *ko) |
| 2746 | { |
| 2747 | mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj); |
| 2748 | kfree(rdev); |
| 2749 | } |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 2750 | static const struct sysfs_ops rdev_sysfs_ops = { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2751 | .show = rdev_attr_show, |
| 2752 | .store = rdev_attr_store, |
| 2753 | }; |
| 2754 | static struct kobj_type rdev_ktype = { |
| 2755 | .release = rdev_free, |
| 2756 | .sysfs_ops = &rdev_sysfs_ops, |
| 2757 | .default_attrs = rdev_default_attrs, |
| 2758 | }; |
| 2759 | |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 2760 | void md_rdev_init(mdk_rdev_t *rdev) |
| 2761 | { |
| 2762 | rdev->desc_nr = -1; |
| 2763 | rdev->saved_raid_disk = -1; |
| 2764 | rdev->raid_disk = -1; |
| 2765 | rdev->flags = 0; |
| 2766 | rdev->data_offset = 0; |
| 2767 | rdev->sb_events = 0; |
| 2768 | rdev->last_read_error.tv_sec = 0; |
| 2769 | rdev->last_read_error.tv_nsec = 0; |
| 2770 | atomic_set(&rdev->nr_pending, 0); |
| 2771 | atomic_set(&rdev->read_errors, 0); |
| 2772 | atomic_set(&rdev->corrected_errors, 0); |
| 2773 | |
| 2774 | INIT_LIST_HEAD(&rdev->same_set); |
| 2775 | init_waitqueue_head(&rdev->blocked_wait); |
| 2776 | } |
| 2777 | EXPORT_SYMBOL_GPL(md_rdev_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2778 | /* |
| 2779 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 2780 | * |
| 2781 | * mark the device faulty if: |
| 2782 | * |
| 2783 | * - the device is nonexistent (zero size) |
| 2784 | * - the device has no valid superblock |
| 2785 | * |
| 2786 | * a faulty rdev _never_ has rdev->sb set. |
| 2787 | */ |
| 2788 | static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor) |
| 2789 | { |
| 2790 | char b[BDEVNAME_SIZE]; |
| 2791 | int err; |
| 2792 | mdk_rdev_t *rdev; |
| 2793 | sector_t size; |
| 2794 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 2795 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2796 | if (!rdev) { |
| 2797 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 2798 | return ERR_PTR(-ENOMEM); |
| 2799 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2800 | |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 2801 | md_rdev_init(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2802 | if ((err = alloc_disk_sb(rdev))) |
| 2803 | goto abort_free; |
| 2804 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2805 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2806 | if (err) |
| 2807 | goto abort_free; |
| 2808 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2809 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2810 | |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 2811 | size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2812 | if (!size) { |
| 2813 | printk(KERN_WARNING |
| 2814 | "md: %s has zero or unknown size, marking faulty!\n", |
| 2815 | bdevname(rdev->bdev,b)); |
| 2816 | err = -EINVAL; |
| 2817 | goto abort_free; |
| 2818 | } |
| 2819 | |
| 2820 | if (super_format >= 0) { |
| 2821 | err = super_types[super_format]. |
| 2822 | load_super(rdev, NULL, super_minor); |
| 2823 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 2824 | printk(KERN_WARNING |
| 2825 | "md: %s does not have a valid v%d.%d " |
| 2826 | "superblock, not importing!\n", |
| 2827 | bdevname(rdev->bdev,b), |
| 2828 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2829 | goto abort_free; |
| 2830 | } |
| 2831 | if (err < 0) { |
| 2832 | printk(KERN_WARNING |
| 2833 | "md: could not read %s's sb, not importing!\n", |
| 2834 | bdevname(rdev->bdev,b)); |
| 2835 | goto abort_free; |
| 2836 | } |
| 2837 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2838 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2839 | return rdev; |
| 2840 | |
| 2841 | abort_free: |
| 2842 | if (rdev->sb_page) { |
| 2843 | if (rdev->bdev) |
| 2844 | unlock_rdev(rdev); |
| 2845 | free_disk_sb(rdev); |
| 2846 | } |
| 2847 | kfree(rdev); |
| 2848 | return ERR_PTR(err); |
| 2849 | } |
| 2850 | |
| 2851 | /* |
| 2852 | * Check a full RAID array for plausibility |
| 2853 | */ |
| 2854 | |
| 2855 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 2856 | static void analyze_sbs(mddev_t * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2857 | { |
| 2858 | int i; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2859 | mdk_rdev_t *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2860 | char b[BDEVNAME_SIZE]; |
| 2861 | |
| 2862 | freshest = NULL; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2863 | rdev_for_each(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2864 | switch (super_types[mddev->major_version]. |
| 2865 | load_super(rdev, freshest, mddev->minor_version)) { |
| 2866 | case 1: |
| 2867 | freshest = rdev; |
| 2868 | break; |
| 2869 | case 0: |
| 2870 | break; |
| 2871 | default: |
| 2872 | printk( KERN_ERR \ |
| 2873 | "md: fatal superblock inconsistency in %s" |
| 2874 | " -- removing from array\n", |
| 2875 | bdevname(rdev->bdev,b)); |
| 2876 | kick_rdev_from_array(rdev); |
| 2877 | } |
| 2878 | |
| 2879 | |
| 2880 | super_types[mddev->major_version]. |
| 2881 | validate_super(mddev, freshest); |
| 2882 | |
| 2883 | i = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2884 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 2885 | if (mddev->max_disks && |
| 2886 | (rdev->desc_nr >= mddev->max_disks || |
| 2887 | i > mddev->max_disks)) { |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 2888 | printk(KERN_WARNING |
| 2889 | "md: %s: %s: only %d devices permitted\n", |
| 2890 | mdname(mddev), bdevname(rdev->bdev, b), |
| 2891 | mddev->max_disks); |
| 2892 | kick_rdev_from_array(rdev); |
| 2893 | continue; |
| 2894 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2895 | if (rdev != freshest) |
| 2896 | if (super_types[mddev->major_version]. |
| 2897 | validate_super(mddev, rdev)) { |
| 2898 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 2899 | " from array!\n", |
| 2900 | bdevname(rdev->bdev,b)); |
| 2901 | kick_rdev_from_array(rdev); |
| 2902 | continue; |
| 2903 | } |
| 2904 | if (mddev->level == LEVEL_MULTIPATH) { |
| 2905 | rdev->desc_nr = i++; |
| 2906 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2907 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 2908 | } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2909 | rdev->raid_disk = -1; |
| 2910 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2911 | } |
| 2912 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2913 | } |
| 2914 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2915 | /* Read a fixed-point number. |
| 2916 | * Numbers in sysfs attributes should be in "standard" units where |
| 2917 | * possible, so time should be in seconds. |
| 2918 | * However we internally use a a much smaller unit such as |
| 2919 | * milliseconds or jiffies. |
| 2920 | * This function takes a decimal number with a possible fractional |
| 2921 | * component, and produces an integer which is the result of |
| 2922 | * multiplying that number by 10^'scale'. |
| 2923 | * all without any floating-point arithmetic. |
| 2924 | */ |
| 2925 | int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale) |
| 2926 | { |
| 2927 | unsigned long result = 0; |
| 2928 | long decimals = -1; |
| 2929 | while (isdigit(*cp) || (*cp == '.' && decimals < 0)) { |
| 2930 | if (*cp == '.') |
| 2931 | decimals = 0; |
| 2932 | else if (decimals < scale) { |
| 2933 | unsigned int value; |
| 2934 | value = *cp - '0'; |
| 2935 | result = result * 10 + value; |
| 2936 | if (decimals >= 0) |
| 2937 | decimals++; |
| 2938 | } |
| 2939 | cp++; |
| 2940 | } |
| 2941 | if (*cp == '\n') |
| 2942 | cp++; |
| 2943 | if (*cp) |
| 2944 | return -EINVAL; |
| 2945 | if (decimals < 0) |
| 2946 | decimals = 0; |
| 2947 | while (decimals < scale) { |
| 2948 | result *= 10; |
| 2949 | decimals ++; |
| 2950 | } |
| 2951 | *res = result; |
| 2952 | return 0; |
| 2953 | } |
| 2954 | |
| 2955 | |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2956 | static void md_safemode_timeout(unsigned long data); |
| 2957 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2958 | static ssize_t |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2959 | safe_delay_show(mddev_t *mddev, char *page) |
| 2960 | { |
| 2961 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 2962 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 2963 | } |
| 2964 | static ssize_t |
| 2965 | safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len) |
| 2966 | { |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2967 | unsigned long msec; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 2968 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2969 | if (strict_strtoul_scaled(cbuf, &msec, 3) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2970 | return -EINVAL; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2971 | if (msec == 0) |
| 2972 | mddev->safemode_delay = 0; |
| 2973 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2974 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2975 | mddev->safemode_delay = (msec*HZ)/1000; |
| 2976 | if (mddev->safemode_delay == 0) |
| 2977 | mddev->safemode_delay = 1; |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2978 | if (mddev->safemode_delay < old_delay) |
| 2979 | md_safemode_timeout((unsigned long)mddev); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2980 | } |
| 2981 | return len; |
| 2982 | } |
| 2983 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2984 | __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] | 2985 | |
| 2986 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2987 | level_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2988 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 2989 | struct mdk_personality *p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2990 | if (p) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2991 | return sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2992 | else if (mddev->clevel[0]) |
| 2993 | return sprintf(page, "%s\n", mddev->clevel); |
| 2994 | else if (mddev->level != LEVEL_NONE) |
| 2995 | return sprintf(page, "%d\n", mddev->level); |
| 2996 | else |
| 2997 | return 0; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2998 | } |
| 2999 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3000 | static ssize_t |
| 3001 | level_store(mddev_t *mddev, const char *buf, size_t len) |
| 3002 | { |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3003 | char clevel[16]; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 3004 | ssize_t rv = len; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3005 | struct mdk_personality *pers; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3006 | long level; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3007 | void *priv; |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3008 | mdk_rdev_t *rdev; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3009 | |
| 3010 | if (mddev->pers == NULL) { |
| 3011 | if (len == 0) |
| 3012 | return 0; |
| 3013 | if (len >= sizeof(mddev->clevel)) |
| 3014 | return -ENOSPC; |
| 3015 | strncpy(mddev->clevel, buf, len); |
| 3016 | if (mddev->clevel[len-1] == '\n') |
| 3017 | len--; |
| 3018 | mddev->clevel[len] = 0; |
| 3019 | mddev->level = LEVEL_NONE; |
| 3020 | return rv; |
| 3021 | } |
| 3022 | |
| 3023 | /* request to change the personality. Need to ensure: |
| 3024 | * - array is not engaged in resync/recovery/reshape |
| 3025 | * - old personality can be suspended |
| 3026 | * - new personality will access other array. |
| 3027 | */ |
| 3028 | |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 3029 | if (mddev->sync_thread || |
| 3030 | mddev->reshape_position != MaxSector || |
| 3031 | mddev->sysfs_active) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3032 | return -EBUSY; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3033 | |
| 3034 | if (!mddev->pers->quiesce) { |
| 3035 | printk(KERN_WARNING "md: %s: %s does not support online personality change\n", |
| 3036 | mdname(mddev), mddev->pers->name); |
| 3037 | return -EINVAL; |
| 3038 | } |
| 3039 | |
| 3040 | /* Now find the new personality */ |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3041 | if (len == 0 || len >= sizeof(clevel)) |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3042 | return -EINVAL; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3043 | strncpy(clevel, buf, len); |
| 3044 | if (clevel[len-1] == '\n') |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3045 | len--; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3046 | clevel[len] = 0; |
| 3047 | if (strict_strtol(clevel, 10, &level)) |
| 3048 | level = LEVEL_NONE; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3049 | |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3050 | if (request_module("md-%s", clevel) != 0) |
| 3051 | request_module("md-level-%s", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3052 | spin_lock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3053 | pers = find_pers(level, clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3054 | if (!pers || !try_module_get(pers->owner)) { |
| 3055 | spin_unlock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3056 | printk(KERN_WARNING "md: personality %s not loaded\n", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3057 | return -EINVAL; |
| 3058 | } |
| 3059 | spin_unlock(&pers_lock); |
| 3060 | |
| 3061 | if (pers == mddev->pers) { |
| 3062 | /* Nothing to do! */ |
| 3063 | module_put(pers->owner); |
| 3064 | return rv; |
| 3065 | } |
| 3066 | if (!pers->takeover) { |
| 3067 | module_put(pers->owner); |
| 3068 | printk(KERN_WARNING "md: %s: %s does not support personality takeover\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3069 | mdname(mddev), clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3070 | return -EINVAL; |
| 3071 | } |
| 3072 | |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3073 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 3074 | rdev->new_raid_disk = rdev->raid_disk; |
| 3075 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3076 | /* ->takeover must set new_* and/or delta_disks |
| 3077 | * if it succeeds, and may set them when it fails. |
| 3078 | */ |
| 3079 | priv = pers->takeover(mddev); |
| 3080 | if (IS_ERR(priv)) { |
| 3081 | mddev->new_level = mddev->level; |
| 3082 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3083 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3084 | mddev->raid_disks -= mddev->delta_disks; |
| 3085 | mddev->delta_disks = 0; |
| 3086 | module_put(pers->owner); |
| 3087 | printk(KERN_WARNING "md: %s: %s would not accept array\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3088 | mdname(mddev), clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3089 | return PTR_ERR(priv); |
| 3090 | } |
| 3091 | |
| 3092 | /* Looks like we have a winner */ |
| 3093 | mddev_suspend(mddev); |
| 3094 | mddev->pers->stop(mddev); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 3095 | |
| 3096 | if (mddev->pers->sync_request == NULL && |
| 3097 | pers->sync_request != NULL) { |
| 3098 | /* need to add the md_redundancy_group */ |
| 3099 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 3100 | printk(KERN_WARNING |
| 3101 | "md: cannot register extra attributes for %s\n", |
| 3102 | mdname(mddev)); |
NeilBrown | 19fdb9e | 2010-05-22 08:31:36 +1000 | [diff] [blame] | 3103 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action"); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 3104 | } |
| 3105 | if (mddev->pers->sync_request != NULL && |
| 3106 | pers->sync_request == NULL) { |
| 3107 | /* need to remove the md_redundancy_group */ |
| 3108 | if (mddev->to_remove == NULL) |
| 3109 | mddev->to_remove = &md_redundancy_group; |
| 3110 | } |
| 3111 | |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 3112 | if (mddev->pers->sync_request == NULL && |
| 3113 | mddev->external) { |
| 3114 | /* We are converting from a no-redundancy array |
| 3115 | * to a redundancy array and metadata is managed |
| 3116 | * externally so we need to be sure that writes |
| 3117 | * won't block due to a need to transition |
| 3118 | * clean->dirty |
| 3119 | * until external management is started. |
| 3120 | */ |
| 3121 | mddev->in_sync = 0; |
| 3122 | mddev->safemode_delay = 0; |
| 3123 | mddev->safemode = 0; |
| 3124 | } |
| 3125 | |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3126 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 3127 | char nm[20]; |
| 3128 | if (rdev->raid_disk < 0) |
| 3129 | continue; |
NeilBrown | bf2cb0d | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 3130 | if (rdev->new_raid_disk >= mddev->raid_disks) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3131 | rdev->new_raid_disk = -1; |
| 3132 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3133 | continue; |
| 3134 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 3135 | sysfs_remove_link(&mddev->kobj, nm); |
| 3136 | } |
| 3137 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 3138 | if (rdev->raid_disk < 0) |
| 3139 | continue; |
| 3140 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3141 | continue; |
| 3142 | rdev->raid_disk = rdev->new_raid_disk; |
| 3143 | if (rdev->raid_disk < 0) |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3144 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3145 | else { |
| 3146 | char nm[20]; |
| 3147 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 3148 | if(sysfs_create_link(&mddev->kobj, &rdev->kobj, nm)) |
| 3149 | printk("md: cannot register %s for %s after level change\n", |
| 3150 | nm, mdname(mddev)); |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3151 | } |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3152 | } |
| 3153 | |
| 3154 | module_put(mddev->pers->owner); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3155 | mddev->pers = pers; |
| 3156 | mddev->private = priv; |
| 3157 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
| 3158 | mddev->level = mddev->new_level; |
| 3159 | mddev->layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3160 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3161 | mddev->delta_disks = 0; |
Trela, Maciej | 9af204c | 2010-03-08 16:02:44 +1100 | [diff] [blame] | 3162 | if (mddev->pers->sync_request == NULL) { |
| 3163 | /* this is now an array without redundancy, so |
| 3164 | * it must always be in_sync |
| 3165 | */ |
| 3166 | mddev->in_sync = 1; |
| 3167 | del_timer_sync(&mddev->safemode_timer); |
| 3168 | } |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3169 | pers->run(mddev); |
| 3170 | mddev_resume(mddev); |
| 3171 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 3172 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3173 | md_wakeup_thread(mddev->thread); |
Maciej Trela | 5cac7861 | 2010-04-14 17:17:39 +1000 | [diff] [blame] | 3174 | sysfs_notify(&mddev->kobj, NULL, "level"); |
Dan Williams | bb7f8d2 | 2010-05-01 18:14:57 -0700 | [diff] [blame] | 3175 | md_new_event(mddev); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3176 | return rv; |
| 3177 | } |
| 3178 | |
| 3179 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3180 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3181 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3182 | |
| 3183 | static ssize_t |
| 3184 | layout_show(mddev_t *mddev, char *page) |
| 3185 | { |
| 3186 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3187 | if (mddev->reshape_position != MaxSector && |
| 3188 | mddev->layout != mddev->new_layout) |
| 3189 | return sprintf(page, "%d (%d)\n", |
| 3190 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3191 | return sprintf(page, "%d\n", mddev->layout); |
| 3192 | } |
| 3193 | |
| 3194 | static ssize_t |
| 3195 | layout_store(mddev_t *mddev, const char *buf, size_t len) |
| 3196 | { |
| 3197 | char *e; |
| 3198 | unsigned long n = simple_strtoul(buf, &e, 10); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3199 | |
| 3200 | if (!*buf || (*e && *e != '\n')) |
| 3201 | return -EINVAL; |
| 3202 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3203 | if (mddev->pers) { |
| 3204 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3205 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3206 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3207 | mddev->new_layout = n; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3208 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3209 | if (err) { |
| 3210 | mddev->new_layout = mddev->layout; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3211 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3212 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3213 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3214 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3215 | if (mddev->reshape_position == MaxSector) |
| 3216 | mddev->layout = n; |
| 3217 | } |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3218 | return len; |
| 3219 | } |
| 3220 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3221 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3222 | |
| 3223 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3224 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3225 | raid_disks_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3226 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 3227 | if (mddev->raid_disks == 0) |
| 3228 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3229 | if (mddev->reshape_position != MaxSector && |
| 3230 | mddev->delta_disks != 0) |
| 3231 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 3232 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3233 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 3234 | } |
| 3235 | |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3236 | static int update_raid_disks(mddev_t *mddev, int raid_disks); |
| 3237 | |
| 3238 | static ssize_t |
| 3239 | raid_disks_store(mddev_t *mddev, const char *buf, size_t len) |
| 3240 | { |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3241 | char *e; |
| 3242 | int rv = 0; |
| 3243 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3244 | |
| 3245 | if (!*buf || (*e && *e != '\n')) |
| 3246 | return -EINVAL; |
| 3247 | |
| 3248 | if (mddev->pers) |
| 3249 | rv = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3250 | else if (mddev->reshape_position != MaxSector) { |
| 3251 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
| 3252 | mddev->delta_disks = n - olddisks; |
| 3253 | mddev->raid_disks = n; |
| 3254 | } else |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3255 | mddev->raid_disks = n; |
| 3256 | return rv ? rv : len; |
| 3257 | } |
| 3258 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3259 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3260 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3261 | static ssize_t |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3262 | chunk_size_show(mddev_t *mddev, char *page) |
| 3263 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3264 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3265 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 3266 | return sprintf(page, "%d (%d)\n", |
| 3267 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3268 | mddev->chunk_sectors << 9); |
| 3269 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3270 | } |
| 3271 | |
| 3272 | static ssize_t |
| 3273 | chunk_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3274 | { |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3275 | char *e; |
| 3276 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3277 | |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3278 | if (!*buf || (*e && *e != '\n')) |
| 3279 | return -EINVAL; |
| 3280 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3281 | if (mddev->pers) { |
| 3282 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3283 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3284 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3285 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3286 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3287 | if (err) { |
| 3288 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3289 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3290 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3291 | } else { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3292 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3293 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3294 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3295 | } |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3296 | return len; |
| 3297 | } |
| 3298 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3299 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3300 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3301 | static ssize_t |
| 3302 | resync_start_show(mddev_t *mddev, char *page) |
| 3303 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3304 | if (mddev->recovery_cp == MaxSector) |
| 3305 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3306 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 3307 | } |
| 3308 | |
| 3309 | static ssize_t |
| 3310 | resync_start_store(mddev_t *mddev, const char *buf, size_t len) |
| 3311 | { |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3312 | char *e; |
| 3313 | unsigned long long n = simple_strtoull(buf, &e, 10); |
| 3314 | |
| 3315 | if (mddev->pers) |
| 3316 | return -EBUSY; |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3317 | if (cmd_match(buf, "none")) |
| 3318 | n = MaxSector; |
| 3319 | else if (!*buf || (*e && *e != '\n')) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3320 | return -EINVAL; |
| 3321 | |
| 3322 | mddev->recovery_cp = n; |
| 3323 | return len; |
| 3324 | } |
| 3325 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3326 | __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3327 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3328 | /* |
| 3329 | * The array state can be: |
| 3330 | * |
| 3331 | * clear |
| 3332 | * No devices, no size, no level |
| 3333 | * Equivalent to STOP_ARRAY ioctl |
| 3334 | * inactive |
| 3335 | * May have some settings, but array is not active |
| 3336 | * all IO results in error |
| 3337 | * When written, doesn't tear down array, but just stops it |
| 3338 | * suspended (not supported yet) |
| 3339 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 3340 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3341 | * readonly |
| 3342 | * no resync can happen. no superblocks get written. |
| 3343 | * write requests fail |
| 3344 | * read-auto |
| 3345 | * like readonly, but behaves like 'clean' on a write request. |
| 3346 | * |
| 3347 | * clean - no pending writes, but otherwise active. |
| 3348 | * When written to inactive array, starts without resync |
| 3349 | * If a write request arrives then |
| 3350 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 3351 | * if not known, block and switch to write-pending |
| 3352 | * If written to an active array that has pending writes, then fails. |
| 3353 | * active |
| 3354 | * fully active: IO and resync can be happening. |
| 3355 | * When written to inactive array, starts with resync |
| 3356 | * |
| 3357 | * write-pending |
| 3358 | * clean, but writes are blocked waiting for 'active' to be written. |
| 3359 | * |
| 3360 | * active-idle |
| 3361 | * like active, but no writes have been seen for a while (100msec). |
| 3362 | * |
| 3363 | */ |
| 3364 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 3365 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3366 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3367 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 3368 | "write-pending", "active-idle", NULL }; |
| 3369 | |
| 3370 | static int match_word(const char *word, char **list) |
| 3371 | { |
| 3372 | int n; |
| 3373 | for (n=0; list[n]; n++) |
| 3374 | if (cmd_match(word, list[n])) |
| 3375 | break; |
| 3376 | return n; |
| 3377 | } |
| 3378 | |
| 3379 | static ssize_t |
| 3380 | array_state_show(mddev_t *mddev, char *page) |
| 3381 | { |
| 3382 | enum array_state st = inactive; |
| 3383 | |
| 3384 | if (mddev->pers) |
| 3385 | switch(mddev->ro) { |
| 3386 | case 1: |
| 3387 | st = readonly; |
| 3388 | break; |
| 3389 | case 2: |
| 3390 | st = read_auto; |
| 3391 | break; |
| 3392 | case 0: |
| 3393 | if (mddev->in_sync) |
| 3394 | st = clean; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3395 | else if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3396 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3397 | else if (mddev->safemode) |
| 3398 | st = active_idle; |
| 3399 | else |
| 3400 | st = active; |
| 3401 | } |
| 3402 | else { |
| 3403 | if (list_empty(&mddev->disks) && |
| 3404 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3405 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3406 | st = clear; |
| 3407 | else |
| 3408 | st = inactive; |
| 3409 | } |
| 3410 | return sprintf(page, "%s\n", array_states[st]); |
| 3411 | } |
| 3412 | |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3413 | static int do_md_stop(mddev_t * mddev, int ro, int is_open); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3414 | static int md_set_readonly(mddev_t * mddev, int is_open); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3415 | static int do_md_run(mddev_t * mddev); |
| 3416 | static int restart_array(mddev_t *mddev); |
| 3417 | |
| 3418 | static ssize_t |
| 3419 | array_state_store(mddev_t *mddev, const char *buf, size_t len) |
| 3420 | { |
| 3421 | int err = -EINVAL; |
| 3422 | enum array_state st = match_word(buf, array_states); |
| 3423 | switch(st) { |
| 3424 | case bad_word: |
| 3425 | break; |
| 3426 | case clear: |
| 3427 | /* stopping an active array */ |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3428 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3429 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3430 | err = do_md_stop(mddev, 0, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3431 | break; |
| 3432 | case inactive: |
| 3433 | /* stopping an active array */ |
| 3434 | if (mddev->pers) { |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3435 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3436 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3437 | err = do_md_stop(mddev, 2, 0); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3438 | } else |
| 3439 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3440 | break; |
| 3441 | case suspended: |
| 3442 | break; /* not supported yet */ |
| 3443 | case readonly: |
| 3444 | if (mddev->pers) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3445 | err = md_set_readonly(mddev, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3446 | else { |
| 3447 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3448 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3449 | err = do_md_run(mddev); |
| 3450 | } |
| 3451 | break; |
| 3452 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3453 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3454 | if (mddev->ro == 0) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3455 | err = md_set_readonly(mddev, 0); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3456 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3457 | err = restart_array(mddev); |
| 3458 | if (err == 0) { |
| 3459 | mddev->ro = 2; |
| 3460 | set_disk_ro(mddev->gendisk, 0); |
| 3461 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3462 | } else { |
| 3463 | mddev->ro = 2; |
| 3464 | err = do_md_run(mddev); |
| 3465 | } |
| 3466 | break; |
| 3467 | case clean: |
| 3468 | if (mddev->pers) { |
| 3469 | restart_array(mddev); |
| 3470 | spin_lock_irq(&mddev->write_lock); |
| 3471 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3472 | if (mddev->in_sync == 0) { |
| 3473 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3474 | if (mddev->safemode == 1) |
| 3475 | mddev->safemode = 0; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3476 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3477 | } |
| 3478 | err = 0; |
| 3479 | } else |
| 3480 | err = -EBUSY; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3481 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 3482 | } else |
| 3483 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3484 | break; |
| 3485 | case active: |
| 3486 | if (mddev->pers) { |
| 3487 | restart_array(mddev); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3488 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3489 | wake_up(&mddev->sb_wait); |
| 3490 | err = 0; |
| 3491 | } else { |
| 3492 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3493 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3494 | err = do_md_run(mddev); |
| 3495 | } |
| 3496 | break; |
| 3497 | case write_pending: |
| 3498 | case active_idle: |
| 3499 | /* these cannot be set */ |
| 3500 | break; |
| 3501 | } |
| 3502 | if (err) |
| 3503 | return err; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3504 | else { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 3505 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3506 | return len; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3507 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3508 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3509 | static struct md_sysfs_entry md_array_state = |
| 3510 | __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3511 | |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3512 | static ssize_t |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 3513 | max_corrected_read_errors_show(mddev_t *mddev, char *page) { |
| 3514 | return sprintf(page, "%d\n", |
| 3515 | atomic_read(&mddev->max_corr_read_errors)); |
| 3516 | } |
| 3517 | |
| 3518 | static ssize_t |
| 3519 | max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len) |
| 3520 | { |
| 3521 | char *e; |
| 3522 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3523 | |
| 3524 | if (*buf && (*e == 0 || *e == '\n')) { |
| 3525 | atomic_set(&mddev->max_corr_read_errors, n); |
| 3526 | return len; |
| 3527 | } |
| 3528 | return -EINVAL; |
| 3529 | } |
| 3530 | |
| 3531 | static struct md_sysfs_entry max_corr_read_errors = |
| 3532 | __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show, |
| 3533 | max_corrected_read_errors_store); |
| 3534 | |
| 3535 | static ssize_t |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3536 | null_show(mddev_t *mddev, char *page) |
| 3537 | { |
| 3538 | return -EINVAL; |
| 3539 | } |
| 3540 | |
| 3541 | static ssize_t |
| 3542 | new_dev_store(mddev_t *mddev, const char *buf, size_t len) |
| 3543 | { |
| 3544 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 3545 | /* The new device is added to the array. |
| 3546 | * If the array has a persistent superblock, we read the |
| 3547 | * superblock to initialise info and check validity. |
| 3548 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 3549 | * which mainly checks size. |
| 3550 | */ |
| 3551 | char *e; |
| 3552 | int major = simple_strtoul(buf, &e, 10); |
| 3553 | int minor; |
| 3554 | dev_t dev; |
| 3555 | mdk_rdev_t *rdev; |
| 3556 | int err; |
| 3557 | |
| 3558 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 3559 | return -EINVAL; |
| 3560 | minor = simple_strtoul(e+1, &e, 10); |
| 3561 | if (*e && *e != '\n') |
| 3562 | return -EINVAL; |
| 3563 | dev = MKDEV(major, minor); |
| 3564 | if (major != MAJOR(dev) || |
| 3565 | minor != MINOR(dev)) |
| 3566 | return -EOVERFLOW; |
| 3567 | |
| 3568 | |
| 3569 | if (mddev->persistent) { |
| 3570 | rdev = md_import_device(dev, mddev->major_version, |
| 3571 | mddev->minor_version); |
| 3572 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
| 3573 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 3574 | mdk_rdev_t, same_set); |
| 3575 | err = super_types[mddev->major_version] |
| 3576 | .load_super(rdev, rdev0, mddev->minor_version); |
| 3577 | if (err < 0) |
| 3578 | goto out; |
| 3579 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3580 | } else if (mddev->external) |
| 3581 | rdev = md_import_device(dev, -2, -1); |
| 3582 | else |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3583 | rdev = md_import_device(dev, -1, -1); |
| 3584 | |
| 3585 | if (IS_ERR(rdev)) |
| 3586 | return PTR_ERR(rdev); |
| 3587 | err = bind_rdev_to_array(rdev, mddev); |
| 3588 | out: |
| 3589 | if (err) |
| 3590 | export_rdev(rdev); |
| 3591 | return err ? err : len; |
| 3592 | } |
| 3593 | |
| 3594 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3595 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3596 | |
| 3597 | static ssize_t |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3598 | bitmap_store(mddev_t *mddev, const char *buf, size_t len) |
| 3599 | { |
| 3600 | char *end; |
| 3601 | unsigned long chunk, end_chunk; |
| 3602 | |
| 3603 | if (!mddev->bitmap) |
| 3604 | goto out; |
| 3605 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 3606 | while (*buf) { |
| 3607 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 3608 | if (buf == end) break; |
| 3609 | if (*end == '-') { /* range */ |
| 3610 | buf = end + 1; |
| 3611 | end_chunk = simple_strtoul(buf, &end, 0); |
| 3612 | if (buf == end) break; |
| 3613 | } |
| 3614 | if (*end && !isspace(*end)) break; |
| 3615 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 3616 | buf = skip_spaces(end); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3617 | } |
| 3618 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 3619 | out: |
| 3620 | return len; |
| 3621 | } |
| 3622 | |
| 3623 | static struct md_sysfs_entry md_bitmap = |
| 3624 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 3625 | |
| 3626 | static ssize_t |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3627 | size_show(mddev_t *mddev, char *page) |
| 3628 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3629 | return sprintf(page, "%llu\n", |
| 3630 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3631 | } |
| 3632 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3633 | static int update_size(mddev_t *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3634 | |
| 3635 | static ssize_t |
| 3636 | size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3637 | { |
| 3638 | /* If array is inactive, we can reduce the component size, but |
| 3639 | * not increase it (except from 0). |
| 3640 | * If array is active, we can try an on-line resize |
| 3641 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3642 | sector_t sectors; |
| 3643 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3644 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3645 | if (err < 0) |
| 3646 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3647 | if (mddev->pers) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3648 | err = update_size(mddev, sectors); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3649 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3650 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3651 | if (mddev->dev_sectors == 0 || |
| 3652 | mddev->dev_sectors > sectors) |
| 3653 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3654 | else |
| 3655 | err = -ENOSPC; |
| 3656 | } |
| 3657 | return err ? err : len; |
| 3658 | } |
| 3659 | |
| 3660 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3661 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3662 | |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3663 | |
| 3664 | /* Metdata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3665 | * This is one of |
| 3666 | * 'none' for arrays with no metadata (good luck...) |
| 3667 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3668 | * or N.M for internally known formats |
| 3669 | */ |
| 3670 | static ssize_t |
| 3671 | metadata_show(mddev_t *mddev, char *page) |
| 3672 | { |
| 3673 | if (mddev->persistent) |
| 3674 | return sprintf(page, "%d.%d\n", |
| 3675 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3676 | else if (mddev->external) |
| 3677 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3678 | else |
| 3679 | return sprintf(page, "none\n"); |
| 3680 | } |
| 3681 | |
| 3682 | static ssize_t |
| 3683 | metadata_store(mddev_t *mddev, const char *buf, size_t len) |
| 3684 | { |
| 3685 | int major, minor; |
| 3686 | char *e; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 3687 | /* Changing the details of 'external' metadata is |
| 3688 | * always permitted. Otherwise there must be |
| 3689 | * no devices attached to the array. |
| 3690 | */ |
| 3691 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 3692 | ; |
| 3693 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3694 | return -EBUSY; |
| 3695 | |
| 3696 | if (cmd_match(buf, "none")) { |
| 3697 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3698 | mddev->external = 0; |
| 3699 | mddev->major_version = 0; |
| 3700 | mddev->minor_version = 90; |
| 3701 | return len; |
| 3702 | } |
| 3703 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 3704 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3705 | if (namelen >= sizeof(mddev->metadata_type)) |
| 3706 | namelen = sizeof(mddev->metadata_type)-1; |
| 3707 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 3708 | mddev->metadata_type[namelen] = 0; |
| 3709 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 3710 | mddev->metadata_type[--namelen] = 0; |
| 3711 | mddev->persistent = 0; |
| 3712 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3713 | mddev->major_version = 0; |
| 3714 | mddev->minor_version = 90; |
| 3715 | return len; |
| 3716 | } |
| 3717 | major = simple_strtoul(buf, &e, 10); |
| 3718 | if (e==buf || *e != '.') |
| 3719 | return -EINVAL; |
| 3720 | buf = e+1; |
| 3721 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 3722 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3723 | return -EINVAL; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 3724 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3725 | return -ENOENT; |
| 3726 | mddev->major_version = major; |
| 3727 | mddev->minor_version = minor; |
| 3728 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3729 | mddev->external = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3730 | return len; |
| 3731 | } |
| 3732 | |
| 3733 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3734 | __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3735 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3736 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3737 | action_show(mddev_t *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3738 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3739 | char *type = "idle"; |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3740 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 3741 | type = "frozen"; |
| 3742 | else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 2b12ab6 | 2007-10-16 23:30:53 -0700 | [diff] [blame] | 3743 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3744 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 3745 | type = "reshape"; |
| 3746 | else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3747 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 3748 | type = "resync"; |
| 3749 | else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 3750 | type = "check"; |
| 3751 | else |
| 3752 | type = "repair"; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3753 | } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3754 | type = "recover"; |
| 3755 | } |
| 3756 | return sprintf(page, "%s\n", type); |
| 3757 | } |
| 3758 | |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 3759 | static void reap_sync_thread(mddev_t *mddev); |
| 3760 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3761 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3762 | action_store(mddev_t *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3763 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3764 | if (!mddev->pers || !mddev->pers->sync_request) |
| 3765 | return -EINVAL; |
| 3766 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3767 | if (cmd_match(page, "frozen")) |
| 3768 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3769 | else |
| 3770 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3771 | |
| 3772 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3773 | if (mddev->sync_thread) { |
| 3774 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 3775 | reap_sync_thread(mddev); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3776 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3777 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 3778 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3779 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3780 | else if (cmd_match(page, "resync")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3781 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3782 | else if (cmd_match(page, "recover")) { |
| 3783 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 3784 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3785 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3786 | int err; |
| 3787 | if (mddev->pers->start_reshape == NULL) |
| 3788 | return -EINVAL; |
| 3789 | err = mddev->pers->start_reshape(mddev); |
| 3790 | if (err) |
| 3791 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 3792 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3793 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 3794 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3795 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 3796 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3797 | return -EINVAL; |
| 3798 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 3799 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3800 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3801 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3802 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 3803 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3804 | return len; |
| 3805 | } |
| 3806 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3807 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3808 | mismatch_cnt_show(mddev_t *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3809 | { |
| 3810 | return sprintf(page, "%llu\n", |
| 3811 | (unsigned long long) mddev->resync_mismatches); |
| 3812 | } |
| 3813 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3814 | static struct md_sysfs_entry md_scan_mode = |
| 3815 | __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3816 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3817 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3818 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3819 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3820 | static ssize_t |
| 3821 | sync_min_show(mddev_t *mddev, char *page) |
| 3822 | { |
| 3823 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 3824 | mddev->sync_speed_min ? "local": "system"); |
| 3825 | } |
| 3826 | |
| 3827 | static ssize_t |
| 3828 | sync_min_store(mddev_t *mddev, const char *buf, size_t len) |
| 3829 | { |
| 3830 | int min; |
| 3831 | char *e; |
| 3832 | if (strncmp(buf, "system", 6)==0) { |
| 3833 | mddev->sync_speed_min = 0; |
| 3834 | return len; |
| 3835 | } |
| 3836 | min = simple_strtoul(buf, &e, 10); |
| 3837 | if (buf == e || (*e && *e != '\n') || min <= 0) |
| 3838 | return -EINVAL; |
| 3839 | mddev->sync_speed_min = min; |
| 3840 | return len; |
| 3841 | } |
| 3842 | |
| 3843 | static struct md_sysfs_entry md_sync_min = |
| 3844 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 3845 | |
| 3846 | static ssize_t |
| 3847 | sync_max_show(mddev_t *mddev, char *page) |
| 3848 | { |
| 3849 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 3850 | mddev->sync_speed_max ? "local": "system"); |
| 3851 | } |
| 3852 | |
| 3853 | static ssize_t |
| 3854 | sync_max_store(mddev_t *mddev, const char *buf, size_t len) |
| 3855 | { |
| 3856 | int max; |
| 3857 | char *e; |
| 3858 | if (strncmp(buf, "system", 6)==0) { |
| 3859 | mddev->sync_speed_max = 0; |
| 3860 | return len; |
| 3861 | } |
| 3862 | max = simple_strtoul(buf, &e, 10); |
| 3863 | if (buf == e || (*e && *e != '\n') || max <= 0) |
| 3864 | return -EINVAL; |
| 3865 | mddev->sync_speed_max = max; |
| 3866 | return len; |
| 3867 | } |
| 3868 | |
| 3869 | static struct md_sysfs_entry md_sync_max = |
| 3870 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 3871 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 3872 | static ssize_t |
| 3873 | degraded_show(mddev_t *mddev, char *page) |
| 3874 | { |
| 3875 | return sprintf(page, "%d\n", mddev->degraded); |
| 3876 | } |
| 3877 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3878 | |
| 3879 | static ssize_t |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 3880 | sync_force_parallel_show(mddev_t *mddev, char *page) |
| 3881 | { |
| 3882 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 3883 | } |
| 3884 | |
| 3885 | static ssize_t |
| 3886 | sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len) |
| 3887 | { |
| 3888 | long n; |
| 3889 | |
| 3890 | if (strict_strtol(buf, 10, &n)) |
| 3891 | return -EINVAL; |
| 3892 | |
| 3893 | if (n != 0 && n != 1) |
| 3894 | return -EINVAL; |
| 3895 | |
| 3896 | mddev->parallel_resync = n; |
| 3897 | |
| 3898 | if (mddev->sync_thread) |
| 3899 | wake_up(&resync_wait); |
| 3900 | |
| 3901 | return len; |
| 3902 | } |
| 3903 | |
| 3904 | /* force parallel resync, even with shared block devices */ |
| 3905 | static struct md_sysfs_entry md_sync_force_parallel = |
| 3906 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 3907 | sync_force_parallel_show, sync_force_parallel_store); |
| 3908 | |
| 3909 | static ssize_t |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3910 | sync_speed_show(mddev_t *mddev, char *page) |
| 3911 | { |
| 3912 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3913 | if (mddev->curr_resync == 0) |
| 3914 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3915 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 3916 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3917 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3918 | db = resync - mddev->resync_mark_cnt; |
| 3919 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3920 | } |
| 3921 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3922 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3923 | |
| 3924 | static ssize_t |
| 3925 | sync_completed_show(mddev_t *mddev, char *page) |
| 3926 | { |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 3927 | unsigned long long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3928 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 3929 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3930 | return sprintf(page, "none\n"); |
| 3931 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3932 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3933 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3934 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3935 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3936 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 3937 | resync = mddev->curr_resync_completed; |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 3938 | return sprintf(page, "%llu / %llu\n", resync, max_sectors); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3939 | } |
| 3940 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3941 | static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3942 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3943 | static ssize_t |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3944 | min_sync_show(mddev_t *mddev, char *page) |
| 3945 | { |
| 3946 | return sprintf(page, "%llu\n", |
| 3947 | (unsigned long long)mddev->resync_min); |
| 3948 | } |
| 3949 | static ssize_t |
| 3950 | min_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3951 | { |
| 3952 | unsigned long long min; |
| 3953 | if (strict_strtoull(buf, 10, &min)) |
| 3954 | return -EINVAL; |
| 3955 | if (min > mddev->resync_max) |
| 3956 | return -EINVAL; |
| 3957 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3958 | return -EBUSY; |
| 3959 | |
| 3960 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3961 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 3962 | sector_t temp = min; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3963 | if (sector_div(temp, mddev->chunk_sectors)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3964 | return -EINVAL; |
| 3965 | } |
| 3966 | mddev->resync_min = min; |
| 3967 | |
| 3968 | return len; |
| 3969 | } |
| 3970 | |
| 3971 | static struct md_sysfs_entry md_min_sync = |
| 3972 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 3973 | |
| 3974 | static ssize_t |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3975 | max_sync_show(mddev_t *mddev, char *page) |
| 3976 | { |
| 3977 | if (mddev->resync_max == MaxSector) |
| 3978 | return sprintf(page, "max\n"); |
| 3979 | else |
| 3980 | return sprintf(page, "%llu\n", |
| 3981 | (unsigned long long)mddev->resync_max); |
| 3982 | } |
| 3983 | static ssize_t |
| 3984 | max_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3985 | { |
| 3986 | if (strncmp(buf, "max", 3) == 0) |
| 3987 | mddev->resync_max = MaxSector; |
| 3988 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3989 | unsigned long long max; |
| 3990 | if (strict_strtoull(buf, 10, &max)) |
| 3991 | return -EINVAL; |
| 3992 | if (max < mddev->resync_min) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3993 | return -EINVAL; |
| 3994 | if (max < mddev->resync_max && |
NeilBrown | 4d484a4 | 2009-08-13 10:41:50 +1000 | [diff] [blame] | 3995 | mddev->ro == 0 && |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3996 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3997 | return -EBUSY; |
| 3998 | |
| 3999 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4000 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 4001 | sector_t temp = max; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4002 | if (sector_div(temp, mddev->chunk_sectors)) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4003 | return -EINVAL; |
| 4004 | } |
| 4005 | mddev->resync_max = max; |
| 4006 | } |
| 4007 | wake_up(&mddev->recovery_wait); |
| 4008 | return len; |
| 4009 | } |
| 4010 | |
| 4011 | static struct md_sysfs_entry md_max_sync = |
| 4012 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 4013 | |
| 4014 | static ssize_t |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4015 | suspend_lo_show(mddev_t *mddev, char *page) |
| 4016 | { |
| 4017 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 4018 | } |
| 4019 | |
| 4020 | static ssize_t |
| 4021 | suspend_lo_store(mddev_t *mddev, const char *buf, size_t len) |
| 4022 | { |
| 4023 | char *e; |
| 4024 | unsigned long long new = simple_strtoull(buf, &e, 10); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4025 | unsigned long long old = mddev->suspend_lo; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4026 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 4027 | if (mddev->pers == NULL || |
| 4028 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4029 | return -EINVAL; |
| 4030 | if (buf == e || (*e && *e != '\n')) |
| 4031 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4032 | |
| 4033 | mddev->suspend_lo = new; |
| 4034 | if (new >= old) |
| 4035 | /* Shrinking suspended region */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4036 | mddev->pers->quiesce(mddev, 2); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4037 | else { |
| 4038 | /* Expanding suspended region - need to wait */ |
| 4039 | mddev->pers->quiesce(mddev, 1); |
| 4040 | mddev->pers->quiesce(mddev, 0); |
| 4041 | } |
| 4042 | return len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4043 | } |
| 4044 | static struct md_sysfs_entry md_suspend_lo = |
| 4045 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 4046 | |
| 4047 | |
| 4048 | static ssize_t |
| 4049 | suspend_hi_show(mddev_t *mddev, char *page) |
| 4050 | { |
| 4051 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 4052 | } |
| 4053 | |
| 4054 | static ssize_t |
| 4055 | suspend_hi_store(mddev_t *mddev, const char *buf, size_t len) |
| 4056 | { |
| 4057 | char *e; |
| 4058 | unsigned long long new = simple_strtoull(buf, &e, 10); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4059 | unsigned long long old = mddev->suspend_hi; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4060 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 4061 | if (mddev->pers == NULL || |
| 4062 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4063 | return -EINVAL; |
| 4064 | if (buf == e || (*e && *e != '\n')) |
| 4065 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4066 | |
| 4067 | mddev->suspend_hi = new; |
| 4068 | if (new <= old) |
| 4069 | /* Shrinking suspended region */ |
| 4070 | mddev->pers->quiesce(mddev, 2); |
| 4071 | else { |
| 4072 | /* Expanding suspended region - need to wait */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4073 | mddev->pers->quiesce(mddev, 1); |
| 4074 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4075 | } |
| 4076 | return len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4077 | } |
| 4078 | static struct md_sysfs_entry md_suspend_hi = |
| 4079 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 4080 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4081 | static ssize_t |
| 4082 | reshape_position_show(mddev_t *mddev, char *page) |
| 4083 | { |
| 4084 | if (mddev->reshape_position != MaxSector) |
| 4085 | return sprintf(page, "%llu\n", |
| 4086 | (unsigned long long)mddev->reshape_position); |
| 4087 | strcpy(page, "none\n"); |
| 4088 | return 5; |
| 4089 | } |
| 4090 | |
| 4091 | static ssize_t |
| 4092 | reshape_position_store(mddev_t *mddev, const char *buf, size_t len) |
| 4093 | { |
| 4094 | char *e; |
| 4095 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 4096 | if (mddev->pers) |
| 4097 | return -EBUSY; |
| 4098 | if (buf == e || (*e && *e != '\n')) |
| 4099 | return -EINVAL; |
| 4100 | mddev->reshape_position = new; |
| 4101 | mddev->delta_disks = 0; |
| 4102 | mddev->new_level = mddev->level; |
| 4103 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4104 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4105 | return len; |
| 4106 | } |
| 4107 | |
| 4108 | static struct md_sysfs_entry md_reshape_position = |
| 4109 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 4110 | reshape_position_store); |
| 4111 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4112 | static ssize_t |
| 4113 | array_size_show(mddev_t *mddev, char *page) |
| 4114 | { |
| 4115 | if (mddev->external_size) |
| 4116 | return sprintf(page, "%llu\n", |
| 4117 | (unsigned long long)mddev->array_sectors/2); |
| 4118 | else |
| 4119 | return sprintf(page, "default\n"); |
| 4120 | } |
| 4121 | |
| 4122 | static ssize_t |
| 4123 | array_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 4124 | { |
| 4125 | sector_t sectors; |
| 4126 | |
| 4127 | if (strncmp(buf, "default", 7) == 0) { |
| 4128 | if (mddev->pers) |
| 4129 | sectors = mddev->pers->size(mddev, 0, 0); |
| 4130 | else |
| 4131 | sectors = mddev->array_sectors; |
| 4132 | |
| 4133 | mddev->external_size = 0; |
| 4134 | } else { |
| 4135 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
| 4136 | return -EINVAL; |
| 4137 | if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
NeilBrown | 2b69c83 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4138 | return -E2BIG; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4139 | |
| 4140 | mddev->external_size = 1; |
| 4141 | } |
| 4142 | |
| 4143 | mddev->array_sectors = sectors; |
NeilBrown | cbe6ef1 | 2011-02-16 13:58:38 +1100 | [diff] [blame] | 4144 | if (mddev->pers) { |
| 4145 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 4146 | revalidate_disk(mddev->gendisk); |
NeilBrown | cbe6ef1 | 2011-02-16 13:58:38 +1100 | [diff] [blame] | 4147 | } |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4148 | return len; |
| 4149 | } |
| 4150 | |
| 4151 | static struct md_sysfs_entry md_array_size = |
| 4152 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 4153 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4154 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4155 | static struct attribute *md_default_attrs[] = { |
| 4156 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4157 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4158 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4159 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4160 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4161 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4162 | &md_metadata.attr, |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4163 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4164 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4165 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4166 | &md_reshape_position.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4167 | &md_array_size.attr, |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4168 | &max_corr_read_errors.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4169 | NULL, |
| 4170 | }; |
| 4171 | |
| 4172 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4173 | &md_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4174 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4175 | &md_sync_min.attr, |
| 4176 | &md_sync_max.attr, |
| 4177 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4178 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4179 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4180 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4181 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4182 | &md_suspend_lo.attr, |
| 4183 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4184 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4185 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4186 | NULL, |
| 4187 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4188 | static struct attribute_group md_redundancy_group = { |
| 4189 | .name = NULL, |
| 4190 | .attrs = md_redundancy_attrs, |
| 4191 | }; |
| 4192 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4193 | |
| 4194 | static ssize_t |
| 4195 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 4196 | { |
| 4197 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 4198 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4199 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4200 | |
| 4201 | if (!entry->show) |
| 4202 | return -EIO; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4203 | rv = mddev_lock(mddev); |
| 4204 | if (!rv) { |
| 4205 | rv = entry->show(mddev, page); |
| 4206 | mddev_unlock(mddev); |
| 4207 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4208 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4209 | } |
| 4210 | |
| 4211 | static ssize_t |
| 4212 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 4213 | const char *page, size_t length) |
| 4214 | { |
| 4215 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 4216 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4217 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4218 | |
| 4219 | if (!entry->store) |
| 4220 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 4221 | if (!capable(CAP_SYS_ADMIN)) |
| 4222 | return -EACCES; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4223 | rv = mddev_lock(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4224 | if (mddev->hold_active == UNTIL_IOCTL) |
| 4225 | mddev->hold_active = 0; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4226 | if (!rv) { |
| 4227 | rv = entry->store(mddev, page, length); |
| 4228 | mddev_unlock(mddev); |
| 4229 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4230 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4231 | } |
| 4232 | |
| 4233 | static void md_free(struct kobject *ko) |
| 4234 | { |
| 4235 | mddev_t *mddev = container_of(ko, mddev_t, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4236 | |
| 4237 | if (mddev->sysfs_state) |
| 4238 | sysfs_put(mddev->sysfs_state); |
| 4239 | |
| 4240 | if (mddev->gendisk) { |
| 4241 | del_gendisk(mddev->gendisk); |
| 4242 | put_disk(mddev->gendisk); |
| 4243 | } |
| 4244 | if (mddev->queue) |
| 4245 | blk_cleanup_queue(mddev->queue); |
| 4246 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4247 | kfree(mddev); |
| 4248 | } |
| 4249 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 4250 | static const struct sysfs_ops md_sysfs_ops = { |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4251 | .show = md_attr_show, |
| 4252 | .store = md_attr_store, |
| 4253 | }; |
| 4254 | static struct kobj_type md_ktype = { |
| 4255 | .release = md_free, |
| 4256 | .sysfs_ops = &md_sysfs_ops, |
| 4257 | .default_attrs = md_default_attrs, |
| 4258 | }; |
| 4259 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4260 | int mdp_major = 0; |
| 4261 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4262 | static void mddev_delayed_delete(struct work_struct *ws) |
| 4263 | { |
| 4264 | mddev_t *mddev = container_of(ws, mddev_t, del_work); |
| 4265 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4266 | sysfs_remove_group(&mddev->kobj, &md_bitmap_group); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4267 | kobject_del(&mddev->kobj); |
| 4268 | kobject_put(&mddev->kobj); |
| 4269 | } |
| 4270 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4271 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4272 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4273 | static DEFINE_MUTEX(disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4274 | mddev_t *mddev = mddev_find(dev); |
| 4275 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4276 | int partitioned; |
| 4277 | int shift; |
| 4278 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4279 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4280 | |
| 4281 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4282 | return -ENODEV; |
| 4283 | |
| 4284 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 4285 | shift = partitioned ? MdpMinorShift : 0; |
| 4286 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4287 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4288 | /* wait for any previous instance of this device to be |
| 4289 | * completely removed (mddev_delayed_delete). |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4290 | */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4291 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4292 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4293 | mutex_lock(&disks_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4294 | error = -EEXIST; |
| 4295 | if (mddev->gendisk) |
| 4296 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4297 | |
| 4298 | if (name) { |
| 4299 | /* Need to ensure that 'name' is not a duplicate. |
| 4300 | */ |
| 4301 | mddev_t *mddev2; |
| 4302 | spin_lock(&all_mddevs_lock); |
| 4303 | |
| 4304 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 4305 | if (mddev2->gendisk && |
| 4306 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 4307 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4308 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4309 | } |
| 4310 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4311 | } |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4312 | |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4313 | error = -ENOMEM; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4314 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4315 | if (!mddev->queue) |
| 4316 | goto abort; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4317 | mddev->queue->queuedata = mddev; |
| 4318 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4319 | blk_queue_make_request(mddev->queue, md_make_request); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4320 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4321 | disk = alloc_disk(1 << shift); |
| 4322 | if (!disk) { |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4323 | blk_cleanup_queue(mddev->queue); |
| 4324 | mddev->queue = NULL; |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4325 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4326 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4327 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4328 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4329 | if (name) |
| 4330 | strcpy(disk->disk_name, name); |
| 4331 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4332 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 4333 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4334 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4335 | disk->fops = &md_fops; |
| 4336 | disk->private_data = mddev; |
| 4337 | disk->queue = mddev->queue; |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4338 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4339 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4340 | * remove it now. |
| 4341 | */ |
| 4342 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4343 | add_disk(disk); |
| 4344 | mddev->gendisk = disk; |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 4345 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
| 4346 | &disk_to_dev(disk)->kobj, "%s", "md"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4347 | if (error) { |
| 4348 | /* This isn't possible, but as kobject_init_and_add is marked |
| 4349 | * __must_check, we must do something with the result |
| 4350 | */ |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4351 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 4352 | disk->disk_name); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4353 | error = 0; |
| 4354 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4355 | if (mddev->kobj.sd && |
| 4356 | sysfs_create_group(&mddev->kobj, &md_bitmap_group)) |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4357 | printk(KERN_DEBUG "pointless warning\n"); |
Darrick J. Wong | be20e6c | 2010-11-24 16:40:33 +1100 | [diff] [blame] | 4358 | |
| 4359 | blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4360 | abort: |
| 4361 | mutex_unlock(&disks_mutex); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4362 | if (!error && mddev->kobj.sd) { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4363 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4364 | mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state"); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4365 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4366 | mddev_put(mddev); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4367 | return error; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4368 | } |
| 4369 | |
| 4370 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 4371 | { |
| 4372 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4373 | return NULL; |
| 4374 | } |
| 4375 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4376 | static int add_named_array(const char *val, struct kernel_param *kp) |
| 4377 | { |
| 4378 | /* val must be "md_*" where * is not all digits. |
| 4379 | * We allocate an array with a large free minor number, and |
| 4380 | * set the name to val. val must not already be an active name. |
| 4381 | */ |
| 4382 | int len = strlen(val); |
| 4383 | char buf[DISK_NAME_LEN]; |
| 4384 | |
| 4385 | while (len && val[len-1] == '\n') |
| 4386 | len--; |
| 4387 | if (len >= DISK_NAME_LEN) |
| 4388 | return -E2BIG; |
| 4389 | strlcpy(buf, val, len+1); |
| 4390 | if (strncmp(buf, "md_", 3) != 0) |
| 4391 | return -EINVAL; |
| 4392 | return md_alloc(0, buf); |
| 4393 | } |
| 4394 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4395 | static void md_safemode_timeout(unsigned long data) |
| 4396 | { |
| 4397 | mddev_t *mddev = (mddev_t *) data; |
| 4398 | |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4399 | if (!atomic_read(&mddev->writes_pending)) { |
| 4400 | mddev->safemode = 1; |
| 4401 | if (mddev->external) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4402 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4403 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4404 | md_wakeup_thread(mddev->thread); |
| 4405 | } |
| 4406 | |
NeilBrown | 6ff8d8e | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4407 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4408 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4409 | int md_run(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4410 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4411 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4412 | mdk_rdev_t *rdev; |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4413 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4414 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4415 | if (list_empty(&mddev->disks)) |
| 4416 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4417 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4418 | |
| 4419 | if (mddev->pers) |
| 4420 | return -EBUSY; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 4421 | /* Cannot run until previous stop completes properly */ |
| 4422 | if (mddev->sysfs_active) |
| 4423 | return -EBUSY; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 4424 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4425 | /* |
| 4426 | * Analyze all RAID superblock(s) |
| 4427 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4428 | if (!mddev->raid_disks) { |
| 4429 | if (!mddev->persistent) |
| 4430 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4431 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4432 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4433 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4434 | if (mddev->level != LEVEL_NONE) |
| 4435 | request_module("md-level-%d", mddev->level); |
| 4436 | else if (mddev->clevel[0]) |
| 4437 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4438 | |
| 4439 | /* |
| 4440 | * Drop all container device buffers, from now on |
| 4441 | * the only valid external interface is through the md |
| 4442 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4443 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4444 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4445 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4446 | continue; |
| 4447 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 4448 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4449 | |
| 4450 | /* perform some consistency tests on the device. |
| 4451 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4452 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4453 | */ |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4454 | if (rdev->meta_bdev) { |
| 4455 | /* Nothing to check */; |
| 4456 | } else if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4457 | if (mddev->dev_sectors && |
| 4458 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4459 | > rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4460 | printk("md: %s: data overlaps metadata\n", |
| 4461 | mdname(mddev)); |
| 4462 | return -EINVAL; |
| 4463 | } |
| 4464 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4465 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4466 | > rdev->data_offset) { |
| 4467 | printk("md: %s: metadata overlaps data\n", |
| 4468 | mdname(mddev)); |
| 4469 | return -EINVAL; |
| 4470 | } |
| 4471 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4472 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4473 | } |
| 4474 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 4475 | if (mddev->bio_set == NULL) |
| 4476 | mddev->bio_set = bioset_create(BIO_POOL_SIZE, sizeof(mddev)); |
| 4477 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4478 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4479 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4480 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4481 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4482 | if (mddev->level != LEVEL_NONE) |
| 4483 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 4484 | mddev->level); |
| 4485 | else |
| 4486 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 4487 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4488 | return -EINVAL; |
| 4489 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4490 | mddev->pers = pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4491 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4492 | if (mddev->level != pers->level) { |
| 4493 | mddev->level = pers->level; |
| 4494 | mddev->new_level = pers->level; |
| 4495 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4496 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4497 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4498 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4499 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4500 | /* This personality cannot handle reshaping... */ |
| 4501 | mddev->pers = NULL; |
| 4502 | module_put(pers->owner); |
| 4503 | return -EINVAL; |
| 4504 | } |
| 4505 | |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4506 | if (pers->sync_request) { |
| 4507 | /* Warn if this is a potentially silly |
| 4508 | * configuration. |
| 4509 | */ |
| 4510 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 4511 | mdk_rdev_t *rdev2; |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4512 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4513 | |
| 4514 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4515 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4516 | if (rdev < rdev2 && |
| 4517 | rdev->bdev->bd_contains == |
| 4518 | rdev2->bdev->bd_contains) { |
| 4519 | printk(KERN_WARNING |
| 4520 | "%s: WARNING: %s appears to be" |
| 4521 | " on the same physical disk as" |
| 4522 | " %s.\n", |
| 4523 | mdname(mddev), |
| 4524 | bdevname(rdev->bdev,b), |
| 4525 | bdevname(rdev2->bdev,b2)); |
| 4526 | warned = 1; |
| 4527 | } |
| 4528 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4529 | |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4530 | if (warned) |
| 4531 | printk(KERN_WARNING |
| 4532 | "True protection against single-disk" |
| 4533 | " failure might be compromised.\n"); |
| 4534 | } |
| 4535 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 4536 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4537 | /* may be over-ridden by personality */ |
| 4538 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 4539 | |
NeilBrown | 6ff8d8e | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4540 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4541 | |
NeilBrown | 0f9552b | 2009-12-30 12:08:50 +1100 | [diff] [blame] | 4542 | if (start_readonly && mddev->ro == 0) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4543 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 4544 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4545 | err = mddev->pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 4546 | if (err) |
| 4547 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4548 | else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) { |
| 4549 | WARN_ONCE(!mddev->external_size, "%s: default size too small," |
| 4550 | " but 'external_size' not in effect?\n", __func__); |
| 4551 | printk(KERN_ERR |
| 4552 | "md: invalid array_size %llu > default size %llu\n", |
| 4553 | (unsigned long long)mddev->array_sectors / 2, |
| 4554 | (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2); |
| 4555 | err = -EINVAL; |
| 4556 | mddev->pers->stop(mddev); |
| 4557 | } |
| 4558 | if (err == 0 && mddev->pers->sync_request) { |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4559 | err = bitmap_create(mddev); |
| 4560 | if (err) { |
| 4561 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 4562 | mdname(mddev), err); |
| 4563 | mddev->pers->stop(mddev); |
| 4564 | } |
| 4565 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4566 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4567 | module_put(mddev->pers->owner); |
| 4568 | mddev->pers = NULL; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4569 | bitmap_destroy(mddev); |
| 4570 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4571 | } |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4572 | if (mddev->pers->sync_request) { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4573 | if (mddev->kobj.sd && |
| 4574 | sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4575 | printk(KERN_WARNING |
| 4576 | "md: cannot register extra attributes for %s\n", |
| 4577 | mdname(mddev)); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4578 | mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4579 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 4580 | mddev->ro = 0; |
| 4581 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4582 | atomic_set(&mddev->writes_pending,0); |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4583 | atomic_set(&mddev->max_corr_read_errors, |
| 4584 | MD_DEFAULT_MAX_CORRECTED_READ_ERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4585 | mddev->safemode = 0; |
| 4586 | mddev->safemode_timer.function = md_safemode_timeout; |
| 4587 | mddev->safemode_timer.data = (unsigned long) mddev; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4588 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4589 | mddev->in_sync = 1; |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4590 | smp_wmb(); |
| 4591 | mddev->ready = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4592 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4593 | if (rdev->raid_disk >= 0) { |
| 4594 | char nm[20]; |
| 4595 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4596 | if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4597 | /* failure here is OK */; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4598 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4599 | |
| 4600 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4601 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4602 | if (mddev->flags) |
| 4603 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4604 | |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 4605 | md_wakeup_thread(mddev->thread); |
| 4606 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4607 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4608 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4609 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
| 4610 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4611 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4612 | return 0; |
| 4613 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4614 | EXPORT_SYMBOL_GPL(md_run); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4615 | |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 4616 | static int do_md_run(mddev_t *mddev) |
| 4617 | { |
| 4618 | int err; |
| 4619 | |
| 4620 | err = md_run(mddev); |
| 4621 | if (err) |
| 4622 | goto out; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 4623 | err = bitmap_load(mddev); |
| 4624 | if (err) { |
| 4625 | bitmap_destroy(mddev); |
| 4626 | goto out; |
| 4627 | } |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 4628 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 4629 | revalidate_disk(mddev->gendisk); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 4630 | mddev->changed = 1; |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 4631 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
| 4632 | out: |
| 4633 | return err; |
| 4634 | } |
| 4635 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4636 | static int restart_array(mddev_t *mddev) |
| 4637 | { |
| 4638 | struct gendisk *disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4639 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4640 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4641 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4642 | return -ENXIO; |
| 4643 | if (!mddev->pers) |
| 4644 | return -EINVAL; |
| 4645 | if (!mddev->ro) |
| 4646 | return -EBUSY; |
| 4647 | mddev->safemode = 0; |
| 4648 | mddev->ro = 0; |
| 4649 | set_disk_ro(disk, 0); |
| 4650 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 4651 | mdname(mddev)); |
| 4652 | /* Kick recovery or resync if necessary */ |
| 4653 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4654 | md_wakeup_thread(mddev->thread); |
| 4655 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4656 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4657 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4658 | } |
| 4659 | |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4660 | /* similar to deny_write_access, but accounts for our holding a reference |
| 4661 | * to the file ourselves */ |
| 4662 | static int deny_bitmap_write_access(struct file * file) |
| 4663 | { |
| 4664 | struct inode *inode = file->f_mapping->host; |
| 4665 | |
| 4666 | spin_lock(&inode->i_lock); |
| 4667 | if (atomic_read(&inode->i_writecount) > 1) { |
| 4668 | spin_unlock(&inode->i_lock); |
| 4669 | return -ETXTBSY; |
| 4670 | } |
| 4671 | atomic_set(&inode->i_writecount, -1); |
| 4672 | spin_unlock(&inode->i_lock); |
| 4673 | |
| 4674 | return 0; |
| 4675 | } |
| 4676 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4677 | void restore_bitmap_write_access(struct file *file) |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4678 | { |
| 4679 | struct inode *inode = file->f_mapping->host; |
| 4680 | |
| 4681 | spin_lock(&inode->i_lock); |
| 4682 | atomic_set(&inode->i_writecount, 1); |
| 4683 | spin_unlock(&inode->i_lock); |
| 4684 | } |
| 4685 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4686 | static void md_clean(mddev_t *mddev) |
| 4687 | { |
| 4688 | mddev->array_sectors = 0; |
| 4689 | mddev->external_size = 0; |
| 4690 | mddev->dev_sectors = 0; |
| 4691 | mddev->raid_disks = 0; |
| 4692 | mddev->recovery_cp = 0; |
| 4693 | mddev->resync_min = 0; |
| 4694 | mddev->resync_max = MaxSector; |
| 4695 | mddev->reshape_position = MaxSector; |
| 4696 | mddev->external = 0; |
| 4697 | mddev->persistent = 0; |
| 4698 | mddev->level = LEVEL_NONE; |
| 4699 | mddev->clevel[0] = 0; |
| 4700 | mddev->flags = 0; |
| 4701 | mddev->ro = 0; |
| 4702 | mddev->metadata_type[0] = 0; |
| 4703 | mddev->chunk_sectors = 0; |
| 4704 | mddev->ctime = mddev->utime = 0; |
| 4705 | mddev->layout = 0; |
| 4706 | mddev->max_disks = 0; |
| 4707 | mddev->events = 0; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 4708 | mddev->can_decrease_events = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4709 | mddev->delta_disks = 0; |
| 4710 | mddev->new_level = LEVEL_NONE; |
| 4711 | mddev->new_layout = 0; |
| 4712 | mddev->new_chunk_sectors = 0; |
| 4713 | mddev->curr_resync = 0; |
| 4714 | mddev->resync_mismatches = 0; |
| 4715 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 4716 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 4717 | mddev->recovery = 0; |
| 4718 | mddev->in_sync = 0; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 4719 | mddev->changed = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4720 | mddev->degraded = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4721 | mddev->safemode = 0; |
| 4722 | mddev->bitmap_info.offset = 0; |
| 4723 | mddev->bitmap_info.default_offset = 0; |
| 4724 | mddev->bitmap_info.chunksize = 0; |
| 4725 | mddev->bitmap_info.daemon_sleep = 0; |
| 4726 | mddev->bitmap_info.max_write_behind = 0; |
NeilBrown | 252ac52 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 4727 | mddev->plug = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4728 | } |
| 4729 | |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4730 | static void __md_stop_writes(mddev_t *mddev) |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 4731 | { |
| 4732 | if (mddev->sync_thread) { |
| 4733 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4734 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4735 | reap_sync_thread(mddev); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 4736 | } |
| 4737 | |
| 4738 | del_timer_sync(&mddev->safemode_timer); |
| 4739 | |
| 4740 | bitmap_flush(mddev); |
| 4741 | md_super_wait(mddev); |
| 4742 | |
| 4743 | if (!mddev->in_sync || mddev->flags) { |
| 4744 | /* mark array as shutdown cleanly */ |
| 4745 | mddev->in_sync = 1; |
| 4746 | md_update_sb(mddev, 1); |
| 4747 | } |
| 4748 | } |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4749 | |
| 4750 | void md_stop_writes(mddev_t *mddev) |
| 4751 | { |
| 4752 | mddev_lock(mddev); |
| 4753 | __md_stop_writes(mddev); |
| 4754 | mddev_unlock(mddev); |
| 4755 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4756 | EXPORT_SYMBOL_GPL(md_stop_writes); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 4757 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4758 | void md_stop(mddev_t *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4759 | { |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4760 | mddev->ready = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4761 | mddev->pers->stop(mddev); |
| 4762 | if (mddev->pers->sync_request && mddev->to_remove == NULL) |
| 4763 | mddev->to_remove = &md_redundancy_group; |
| 4764 | module_put(mddev->pers->owner); |
| 4765 | mddev->pers = NULL; |
NeilBrown | cca9cf9 | 2010-04-01 12:08:16 +1100 | [diff] [blame] | 4766 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4767 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4768 | EXPORT_SYMBOL_GPL(md_stop); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4769 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4770 | static int md_set_readonly(mddev_t *mddev, int is_open) |
| 4771 | { |
| 4772 | int err = 0; |
| 4773 | mutex_lock(&mddev->open_mutex); |
| 4774 | if (atomic_read(&mddev->openers) > is_open) { |
| 4775 | printk("md: %s still in use.\n",mdname(mddev)); |
| 4776 | err = -EBUSY; |
| 4777 | goto out; |
| 4778 | } |
| 4779 | if (mddev->pers) { |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4780 | __md_stop_writes(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4781 | |
| 4782 | err = -ENXIO; |
| 4783 | if (mddev->ro==1) |
| 4784 | goto out; |
| 4785 | mddev->ro = 1; |
| 4786 | set_disk_ro(mddev->gendisk, 1); |
| 4787 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4788 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4789 | err = 0; |
| 4790 | } |
| 4791 | out: |
| 4792 | mutex_unlock(&mddev->open_mutex); |
| 4793 | return err; |
| 4794 | } |
| 4795 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4796 | /* mode: |
| 4797 | * 0 - completely stop and dis-assemble array |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4798 | * 2 - stop but do not disassemble array |
| 4799 | */ |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4800 | 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] | 4801 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4802 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 4803 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4804 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 4805 | mutex_lock(&mddev->open_mutex); |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 4806 | if (atomic_read(&mddev->openers) > is_open || |
| 4807 | mddev->sysfs_active) { |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4808 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 4809 | mutex_unlock(&mddev->open_mutex); |
| 4810 | return -EBUSY; |
| 4811 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4812 | |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 4813 | if (mddev->pers) { |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4814 | if (mddev->ro) |
| 4815 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4816 | |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4817 | __md_stop_writes(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4818 | md_stop(mddev); |
| 4819 | mddev->queue->merge_bvec_fn = NULL; |
| 4820 | mddev->queue->unplug_fn = NULL; |
| 4821 | mddev->queue->backing_dev_info.congested_fn = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4822 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4823 | /* tell userspace to handle 'inactive' */ |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4824 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4825 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4826 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4827 | if (rdev->raid_disk >= 0) { |
| 4828 | char nm[20]; |
| 4829 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 4830 | sysfs_remove_link(&mddev->kobj, nm); |
| 4831 | } |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 4832 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4833 | set_capacity(disk, 0); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 4834 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 4835 | mddev->changed = 1; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4836 | revalidate_disk(disk); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4837 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4838 | if (mddev->ro) |
| 4839 | mddev->ro = 0; |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 4840 | } else |
| 4841 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4842 | /* |
| 4843 | * Free resources if final stop |
| 4844 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4845 | if (mode == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4846 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 4847 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4848 | bitmap_destroy(mddev); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4849 | if (mddev->bitmap_info.file) { |
| 4850 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 4851 | fput(mddev->bitmap_info.file); |
| 4852 | mddev->bitmap_info.file = NULL; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4853 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4854 | mddev->bitmap_info.offset = 0; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4855 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4856 | export_array(mddev); |
| 4857 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4858 | md_clean(mddev); |
NeilBrown | 934d9c2 | 2008-10-28 17:01:23 +1100 | [diff] [blame] | 4859 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4860 | if (mddev->hold_active == UNTIL_STOP) |
| 4861 | mddev->hold_active = 0; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4862 | } |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 4863 | blk_integrity_unregister(disk); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4864 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4865 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 4866 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4867 | } |
| 4868 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4869 | #ifndef MODULE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4870 | static void autorun_array(mddev_t *mddev) |
| 4871 | { |
| 4872 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4873 | int err; |
| 4874 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4875 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4876 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4877 | |
| 4878 | printk(KERN_INFO "md: running: "); |
| 4879 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4880 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4881 | char b[BDEVNAME_SIZE]; |
| 4882 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 4883 | } |
| 4884 | printk("\n"); |
| 4885 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4886 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4887 | if (err) { |
| 4888 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4889 | do_md_stop(mddev, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4890 | } |
| 4891 | } |
| 4892 | |
| 4893 | /* |
| 4894 | * lets try to run arrays based on all disks that have arrived |
| 4895 | * until now. (those are in pending_raid_disks) |
| 4896 | * |
| 4897 | * the method: pick the first pending disk, collect all disks with |
| 4898 | * the same UUID, remove all from the pending list and put them into |
| 4899 | * the 'same_array' list. Then order this list based on superblock |
| 4900 | * update time (freshest comes first), kick out 'old' disks and |
| 4901 | * compare superblocks. If everything's fine then run it. |
| 4902 | * |
| 4903 | * If "unit" is allocated, then bump its reference count |
| 4904 | */ |
| 4905 | static void autorun_devices(int part) |
| 4906 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4907 | mdk_rdev_t *rdev0, *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4908 | mddev_t *mddev; |
| 4909 | char b[BDEVNAME_SIZE]; |
| 4910 | |
| 4911 | printk(KERN_INFO "md: autorun ...\n"); |
| 4912 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4913 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4914 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 4915 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4916 | rdev0 = list_entry(pending_raid_disks.next, |
| 4917 | mdk_rdev_t, same_set); |
| 4918 | |
| 4919 | printk(KERN_INFO "md: considering %s ...\n", |
| 4920 | bdevname(rdev0->bdev,b)); |
| 4921 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4922 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4923 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 4924 | printk(KERN_INFO "md: adding %s ...\n", |
| 4925 | bdevname(rdev->bdev,b)); |
| 4926 | list_move(&rdev->same_set, &candidates); |
| 4927 | } |
| 4928 | /* |
| 4929 | * now we have a set of devices, with all of them having |
| 4930 | * mostly sane superblocks. It's time to allocate the |
| 4931 | * mddev. |
| 4932 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4933 | if (part) { |
| 4934 | dev = MKDEV(mdp_major, |
| 4935 | rdev0->preferred_minor << MdpMinorShift); |
| 4936 | unit = MINOR(dev) >> MdpMinorShift; |
| 4937 | } else { |
| 4938 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 4939 | unit = MINOR(dev); |
| 4940 | } |
| 4941 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4942 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 4943 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 4944 | break; |
| 4945 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4946 | |
| 4947 | md_probe(dev, NULL, NULL); |
| 4948 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 4949 | if (!mddev || !mddev->gendisk) { |
| 4950 | if (mddev) |
| 4951 | mddev_put(mddev); |
| 4952 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4953 | "md: cannot allocate memory for md drive.\n"); |
| 4954 | break; |
| 4955 | } |
| 4956 | if (mddev_lock(mddev)) |
| 4957 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 4958 | mdname(mddev)); |
| 4959 | else if (mddev->raid_disks || mddev->major_version |
| 4960 | || !list_empty(&mddev->disks)) { |
| 4961 | printk(KERN_WARNING |
| 4962 | "md: %s already running, cannot run %s\n", |
| 4963 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 4964 | mddev_unlock(mddev); |
| 4965 | } else { |
| 4966 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4967 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4968 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4969 | list_del_init(&rdev->same_set); |
| 4970 | if (bind_rdev_to_array(rdev, mddev)) |
| 4971 | export_rdev(rdev); |
| 4972 | } |
| 4973 | autorun_array(mddev); |
| 4974 | mddev_unlock(mddev); |
| 4975 | } |
| 4976 | /* on success, candidates will be empty, on error |
| 4977 | * it won't... |
| 4978 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4979 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4980 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4981 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4982 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4983 | mddev_put(mddev); |
| 4984 | } |
| 4985 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 4986 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4987 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4988 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4989 | static int get_version(void __user * arg) |
| 4990 | { |
| 4991 | mdu_version_t ver; |
| 4992 | |
| 4993 | ver.major = MD_MAJOR_VERSION; |
| 4994 | ver.minor = MD_MINOR_VERSION; |
| 4995 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 4996 | |
| 4997 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 4998 | return -EFAULT; |
| 4999 | |
| 5000 | return 0; |
| 5001 | } |
| 5002 | |
| 5003 | static int get_array_info(mddev_t * mddev, void __user * arg) |
| 5004 | { |
| 5005 | mdu_array_info_t info; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5006 | int nr,working,insync,failed,spare; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5007 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5008 | |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5009 | nr=working=insync=failed=spare=0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5010 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5011 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5012 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5013 | failed++; |
| 5014 | else { |
| 5015 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5016 | if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5017 | insync++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5018 | else |
| 5019 | spare++; |
| 5020 | } |
| 5021 | } |
| 5022 | |
| 5023 | info.major_version = mddev->major_version; |
| 5024 | info.minor_version = mddev->minor_version; |
| 5025 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 5026 | info.ctime = mddev->ctime; |
| 5027 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5028 | info.size = mddev->dev_sectors / 2; |
| 5029 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 5030 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5031 | info.nr_disks = nr; |
| 5032 | info.raid_disks = mddev->raid_disks; |
| 5033 | info.md_minor = mddev->md_minor; |
| 5034 | info.not_persistent= !mddev->persistent; |
| 5035 | |
| 5036 | info.utime = mddev->utime; |
| 5037 | info.state = 0; |
| 5038 | if (mddev->in_sync) |
| 5039 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5040 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5041 | info.state = (1<<MD_SB_BITMAP_PRESENT); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5042 | info.active_disks = insync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5043 | info.working_disks = working; |
| 5044 | info.failed_disks = failed; |
| 5045 | info.spare_disks = spare; |
| 5046 | |
| 5047 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5048 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5049 | |
| 5050 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5051 | return -EFAULT; |
| 5052 | |
| 5053 | return 0; |
| 5054 | } |
| 5055 | |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 5056 | static int get_bitmap_file(mddev_t * mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5057 | { |
| 5058 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
| 5059 | char *ptr, *buf = NULL; |
| 5060 | int err = -ENOMEM; |
| 5061 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5062 | if (md_allow_write(mddev)) |
| 5063 | file = kmalloc(sizeof(*file), GFP_NOIO); |
| 5064 | else |
| 5065 | file = kmalloc(sizeof(*file), GFP_KERNEL); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5066 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5067 | if (!file) |
| 5068 | goto out; |
| 5069 | |
| 5070 | /* bitmap disabled, zero the first byte and copy out */ |
| 5071 | if (!mddev->bitmap || !mddev->bitmap->file) { |
| 5072 | file->pathname[0] = '\0'; |
| 5073 | goto copy_out; |
| 5074 | } |
| 5075 | |
| 5076 | buf = kmalloc(sizeof(file->pathname), GFP_KERNEL); |
| 5077 | if (!buf) |
| 5078 | goto out; |
| 5079 | |
Christoph Hellwig | 6bcfd60 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 5080 | ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname)); |
| 5081 | if (IS_ERR(ptr)) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5082 | goto out; |
| 5083 | |
| 5084 | strcpy(file->pathname, ptr); |
| 5085 | |
| 5086 | copy_out: |
| 5087 | err = 0; |
| 5088 | if (copy_to_user(arg, file, sizeof(*file))) |
| 5089 | err = -EFAULT; |
| 5090 | out: |
| 5091 | kfree(buf); |
| 5092 | kfree(file); |
| 5093 | return err; |
| 5094 | } |
| 5095 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5096 | static int get_disk_info(mddev_t * mddev, void __user * arg) |
| 5097 | { |
| 5098 | mdu_disk_info_t info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5099 | mdk_rdev_t *rdev; |
| 5100 | |
| 5101 | if (copy_from_user(&info, arg, sizeof(info))) |
| 5102 | return -EFAULT; |
| 5103 | |
Andre Noll | 26ef379 | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5104 | rdev = find_rdev_nr(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5105 | if (rdev) { |
| 5106 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 5107 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 5108 | info.raid_disk = rdev->raid_disk; |
| 5109 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5110 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5111 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5112 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5113 | info.state |= (1<<MD_DISK_ACTIVE); |
| 5114 | info.state |= (1<<MD_DISK_SYNC); |
| 5115 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5116 | if (test_bit(WriteMostly, &rdev->flags)) |
| 5117 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5118 | } else { |
| 5119 | info.major = info.minor = 0; |
| 5120 | info.raid_disk = -1; |
| 5121 | info.state = (1<<MD_DISK_REMOVED); |
| 5122 | } |
| 5123 | |
| 5124 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5125 | return -EFAULT; |
| 5126 | |
| 5127 | return 0; |
| 5128 | } |
| 5129 | |
| 5130 | static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info) |
| 5131 | { |
| 5132 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 5133 | mdk_rdev_t *rdev; |
| 5134 | dev_t dev = MKDEV(info->major,info->minor); |
| 5135 | |
| 5136 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 5137 | return -EOVERFLOW; |
| 5138 | |
| 5139 | if (!mddev->raid_disks) { |
| 5140 | int err; |
| 5141 | /* expecting a device which has a superblock */ |
| 5142 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 5143 | if (IS_ERR(rdev)) { |
| 5144 | printk(KERN_WARNING |
| 5145 | "md: md_import_device returned %ld\n", |
| 5146 | PTR_ERR(rdev)); |
| 5147 | return PTR_ERR(rdev); |
| 5148 | } |
| 5149 | if (!list_empty(&mddev->disks)) { |
| 5150 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 5151 | mdk_rdev_t, same_set); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5152 | err = super_types[mddev->major_version] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5153 | .load_super(rdev, rdev0, mddev->minor_version); |
| 5154 | if (err < 0) { |
| 5155 | printk(KERN_WARNING |
| 5156 | "md: %s has different UUID to %s\n", |
| 5157 | bdevname(rdev->bdev,b), |
| 5158 | bdevname(rdev0->bdev,b2)); |
| 5159 | export_rdev(rdev); |
| 5160 | return -EINVAL; |
| 5161 | } |
| 5162 | } |
| 5163 | err = bind_rdev_to_array(rdev, mddev); |
| 5164 | if (err) |
| 5165 | export_rdev(rdev); |
| 5166 | return err; |
| 5167 | } |
| 5168 | |
| 5169 | /* |
| 5170 | * add_new_disk can be used once the array is assembled |
| 5171 | * to add "hot spares". They must already have a superblock |
| 5172 | * written |
| 5173 | */ |
| 5174 | if (mddev->pers) { |
| 5175 | int err; |
| 5176 | if (!mddev->pers->hot_add_disk) { |
| 5177 | printk(KERN_WARNING |
| 5178 | "%s: personality does not support diskops!\n", |
| 5179 | mdname(mddev)); |
| 5180 | return -EINVAL; |
| 5181 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 5182 | if (mddev->persistent) |
| 5183 | rdev = md_import_device(dev, mddev->major_version, |
| 5184 | mddev->minor_version); |
| 5185 | else |
| 5186 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5187 | if (IS_ERR(rdev)) { |
| 5188 | printk(KERN_WARNING |
| 5189 | "md: md_import_device returned %ld\n", |
| 5190 | PTR_ERR(rdev)); |
| 5191 | return PTR_ERR(rdev); |
| 5192 | } |
NeilBrown | 1a855a0 | 2010-12-09 16:36:28 +1100 | [diff] [blame] | 5193 | /* set saved_raid_disk if appropriate */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5194 | if (!mddev->persistent) { |
| 5195 | if (info->state & (1<<MD_DISK_SYNC) && |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 5196 | info->raid_disk < mddev->raid_disks) { |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5197 | rdev->raid_disk = info->raid_disk; |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 5198 | set_bit(In_sync, &rdev->flags); |
| 5199 | } else |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5200 | rdev->raid_disk = -1; |
| 5201 | } else |
| 5202 | super_types[mddev->major_version]. |
| 5203 | validate_super(mddev, rdev); |
NeilBrown | 1a855a0 | 2010-12-09 16:36:28 +1100 | [diff] [blame] | 5204 | if (test_bit(In_sync, &rdev->flags)) |
| 5205 | rdev->saved_raid_disk = rdev->raid_disk; |
| 5206 | else |
| 5207 | rdev->saved_raid_disk = -1; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5208 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5209 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5210 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5211 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5212 | else |
| 5213 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5214 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5215 | rdev->raid_disk = -1; |
| 5216 | err = bind_rdev_to_array(rdev, mddev); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 5217 | if (!err && !mddev->pers->hot_remove_disk) { |
| 5218 | /* If there is hot_add_disk but no hot_remove_disk |
| 5219 | * then added disks for geometry changes, |
| 5220 | * and should be added immediately. |
| 5221 | */ |
| 5222 | super_types[mddev->major_version]. |
| 5223 | validate_super(mddev, rdev); |
| 5224 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 5225 | if (err) |
| 5226 | unbind_rdev_from_array(rdev); |
| 5227 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5228 | if (err) |
| 5229 | export_rdev(rdev); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 5230 | else |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5231 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5232 | |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 5233 | md_update_sb(mddev, 1); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 5234 | if (mddev->degraded) |
| 5235 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5236 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 5237 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5238 | return err; |
| 5239 | } |
| 5240 | |
| 5241 | /* otherwise, add_new_disk is only allowed |
| 5242 | * for major_version==0 superblocks |
| 5243 | */ |
| 5244 | if (mddev->major_version != 0) { |
| 5245 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 5246 | mdname(mddev)); |
| 5247 | return -EINVAL; |
| 5248 | } |
| 5249 | |
| 5250 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 5251 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5252 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5253 | if (IS_ERR(rdev)) { |
| 5254 | printk(KERN_WARNING |
| 5255 | "md: error, md_import_device() returned %ld\n", |
| 5256 | PTR_ERR(rdev)); |
| 5257 | return PTR_ERR(rdev); |
| 5258 | } |
| 5259 | rdev->desc_nr = info->number; |
| 5260 | if (info->raid_disk < mddev->raid_disks) |
| 5261 | rdev->raid_disk = info->raid_disk; |
| 5262 | else |
| 5263 | rdev->raid_disk = -1; |
| 5264 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5265 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5266 | if (info->state & (1<<MD_DISK_SYNC)) |
| 5267 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5268 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5269 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5270 | set_bit(WriteMostly, &rdev->flags); |
| 5271 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5272 | if (!mddev->persistent) { |
| 5273 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 5274 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
| 5275 | } else |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5276 | rdev->sb_start = calc_dev_sboffset(rdev); |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5277 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5278 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5279 | err = bind_rdev_to_array(rdev, mddev); |
| 5280 | if (err) { |
| 5281 | export_rdev(rdev); |
| 5282 | return err; |
| 5283 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5284 | } |
| 5285 | |
| 5286 | return 0; |
| 5287 | } |
| 5288 | |
| 5289 | static int hot_remove_disk(mddev_t * mddev, dev_t dev) |
| 5290 | { |
| 5291 | char b[BDEVNAME_SIZE]; |
| 5292 | mdk_rdev_t *rdev; |
| 5293 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5294 | rdev = find_rdev(mddev, dev); |
| 5295 | if (!rdev) |
| 5296 | return -ENXIO; |
| 5297 | |
| 5298 | if (rdev->raid_disk >= 0) |
| 5299 | goto busy; |
| 5300 | |
| 5301 | kick_rdev_from_array(rdev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5302 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5303 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5304 | |
| 5305 | return 0; |
| 5306 | busy: |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 5307 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5308 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5309 | return -EBUSY; |
| 5310 | } |
| 5311 | |
| 5312 | static int hot_add_disk(mddev_t * mddev, dev_t dev) |
| 5313 | { |
| 5314 | char b[BDEVNAME_SIZE]; |
| 5315 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5316 | mdk_rdev_t *rdev; |
| 5317 | |
| 5318 | if (!mddev->pers) |
| 5319 | return -ENODEV; |
| 5320 | |
| 5321 | if (mddev->major_version != 0) { |
| 5322 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 5323 | " version-0 superblocks.\n", |
| 5324 | mdname(mddev)); |
| 5325 | return -EINVAL; |
| 5326 | } |
| 5327 | if (!mddev->pers->hot_add_disk) { |
| 5328 | printk(KERN_WARNING |
| 5329 | "%s: personality does not support diskops!\n", |
| 5330 | mdname(mddev)); |
| 5331 | return -EINVAL; |
| 5332 | } |
| 5333 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5334 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5335 | if (IS_ERR(rdev)) { |
| 5336 | printk(KERN_WARNING |
| 5337 | "md: error, md_import_device() returned %ld\n", |
| 5338 | PTR_ERR(rdev)); |
| 5339 | return -EINVAL; |
| 5340 | } |
| 5341 | |
| 5342 | if (mddev->persistent) |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5343 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5344 | else |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 5345 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5346 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5347 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5348 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5349 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5350 | printk(KERN_WARNING |
| 5351 | "md: can not hot-add faulty %s disk to %s!\n", |
| 5352 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5353 | err = -EINVAL; |
| 5354 | goto abort_export; |
| 5355 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5356 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5357 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 5358 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5359 | err = bind_rdev_to_array(rdev, mddev); |
| 5360 | if (err) |
| 5361 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5362 | |
| 5363 | /* |
| 5364 | * The rest should better be atomic, we can have disk failures |
| 5365 | * noticed in interrupt contexts ... |
| 5366 | */ |
| 5367 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5368 | rdev->raid_disk = -1; |
| 5369 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5370 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5371 | |
| 5372 | /* |
| 5373 | * Kick recovery, maybe this spare has to be added to the |
| 5374 | * array immediately. |
| 5375 | */ |
| 5376 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5377 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5378 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5379 | return 0; |
| 5380 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5381 | abort_export: |
| 5382 | export_rdev(rdev); |
| 5383 | return err; |
| 5384 | } |
| 5385 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5386 | static int set_bitmap_file(mddev_t *mddev, int fd) |
| 5387 | { |
| 5388 | int err; |
| 5389 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5390 | if (mddev->pers) { |
| 5391 | if (!mddev->pers->quiesce) |
| 5392 | return -EBUSY; |
| 5393 | if (mddev->recovery || mddev->sync_thread) |
| 5394 | return -EBUSY; |
| 5395 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5396 | } |
| 5397 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5398 | |
| 5399 | if (fd >= 0) { |
| 5400 | if (mddev->bitmap) |
| 5401 | return -EEXIST; /* cannot add when bitmap is present */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5402 | mddev->bitmap_info.file = fget(fd); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5403 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5404 | if (mddev->bitmap_info.file == NULL) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5405 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 5406 | mdname(mddev)); |
| 5407 | return -EBADF; |
| 5408 | } |
| 5409 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5410 | err = deny_bitmap_write_access(mddev->bitmap_info.file); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5411 | if (err) { |
| 5412 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 5413 | mdname(mddev)); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5414 | fput(mddev->bitmap_info.file); |
| 5415 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5416 | return err; |
| 5417 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5418 | mddev->bitmap_info.offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5419 | } else if (mddev->bitmap == NULL) |
| 5420 | return -ENOENT; /* cannot remove what isn't there */ |
| 5421 | err = 0; |
| 5422 | if (mddev->pers) { |
| 5423 | mddev->pers->quiesce(mddev, 1); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5424 | if (fd >= 0) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5425 | err = bitmap_create(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5426 | if (!err) |
| 5427 | err = bitmap_load(mddev); |
| 5428 | } |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5429 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5430 | bitmap_destroy(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5431 | fd = -1; /* make sure to put the file */ |
| 5432 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5433 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5434 | } |
| 5435 | if (fd < 0) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5436 | if (mddev->bitmap_info.file) { |
| 5437 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 5438 | fput(mddev->bitmap_info.file); |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 5439 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5440 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5441 | } |
| 5442 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5443 | return err; |
| 5444 | } |
| 5445 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5446 | /* |
| 5447 | * set_array_info is used two different ways |
| 5448 | * The original usage is when creating a new array. |
| 5449 | * In this usage, raid_disks is > 0 and it together with |
| 5450 | * level, size, not_persistent,layout,chunksize determine the |
| 5451 | * shape of the array. |
| 5452 | * This will always create an array with a type-0.90.0 superblock. |
| 5453 | * The newer usage is when assembling an array. |
| 5454 | * In this case raid_disks will be 0, and the major_version field is |
| 5455 | * use to determine which style super-blocks are to be found on the devices. |
| 5456 | * The minor and patch _version numbers are also kept incase the |
| 5457 | * super_block handler wishes to interpret them. |
| 5458 | */ |
| 5459 | static int set_array_info(mddev_t * mddev, mdu_array_info_t *info) |
| 5460 | { |
| 5461 | |
| 5462 | if (info->raid_disks == 0) { |
| 5463 | /* just setting version number for superblock loading */ |
| 5464 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 5465 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5466 | super_types[info->major_version].name == NULL) { |
| 5467 | /* maybe try to auto-load a module? */ |
| 5468 | printk(KERN_INFO |
| 5469 | "md: superblock version %d not known\n", |
| 5470 | info->major_version); |
| 5471 | return -EINVAL; |
| 5472 | } |
| 5473 | mddev->major_version = info->major_version; |
| 5474 | mddev->minor_version = info->minor_version; |
| 5475 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5476 | mddev->persistent = !info->not_persistent; |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 5477 | /* ensure mddev_put doesn't delete this now that there |
| 5478 | * is some minimal configuration. |
| 5479 | */ |
| 5480 | mddev->ctime = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5481 | return 0; |
| 5482 | } |
| 5483 | mddev->major_version = MD_MAJOR_VERSION; |
| 5484 | mddev->minor_version = MD_MINOR_VERSION; |
| 5485 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 5486 | mddev->ctime = get_seconds(); |
| 5487 | |
| 5488 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 5489 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5490 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5491 | mddev->raid_disks = info->raid_disks; |
| 5492 | /* don't set md_minor, it is determined by which /dev/md* was |
| 5493 | * openned |
| 5494 | */ |
| 5495 | if (info->state & (1<<MD_SB_CLEAN)) |
| 5496 | mddev->recovery_cp = MaxSector; |
| 5497 | else |
| 5498 | mddev->recovery_cp = 0; |
| 5499 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5500 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5501 | |
| 5502 | mddev->layout = info->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5503 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5504 | |
| 5505 | mddev->max_disks = MD_SB_DISKS; |
| 5506 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5507 | if (mddev->persistent) |
| 5508 | mddev->flags = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5509 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5510 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5511 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
| 5512 | mddev->bitmap_info.offset = 0; |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 5513 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5514 | mddev->reshape_position = MaxSector; |
| 5515 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5516 | /* |
| 5517 | * Generate a 128 bit UUID |
| 5518 | */ |
| 5519 | get_random_bytes(mddev->uuid, 16); |
| 5520 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5521 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5522 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5523 | mddev->new_layout = mddev->layout; |
| 5524 | mddev->delta_disks = 0; |
| 5525 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5526 | return 0; |
| 5527 | } |
| 5528 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5529 | void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors) |
| 5530 | { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5531 | WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__); |
| 5532 | |
| 5533 | if (mddev->external_size) |
| 5534 | return; |
| 5535 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5536 | mddev->array_sectors = array_sectors; |
| 5537 | } |
| 5538 | EXPORT_SYMBOL(md_set_array_sectors); |
| 5539 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5540 | static int update_size(mddev_t *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5541 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5542 | mdk_rdev_t *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5543 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5544 | int fit = (num_sectors == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5545 | |
| 5546 | if (mddev->pers->resize == NULL) |
| 5547 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5548 | /* The "num_sectors" is the number of sectors of each device that |
| 5549 | * is used. This can only make sense for arrays with redundancy. |
| 5550 | * linear and raid0 always use whatever space is available. We can only |
| 5551 | * consider changing this number if no resync or reconstruction is |
| 5552 | * 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] | 5553 | * 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] | 5554 | * of each device. If num_sectors is zero, we find the largest size |
| 5555 | * that fits. |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5556 | */ |
| 5557 | if (mddev->sync_thread) |
| 5558 | return -EBUSY; |
NeilBrown | dba034e | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 5559 | if (mddev->bitmap) |
| 5560 | /* Sorry, cannot grow a bitmap yet, just remove it, |
| 5561 | * grow, and re-add. |
| 5562 | */ |
| 5563 | return -EBUSY; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5564 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5565 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 5566 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5567 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 5568 | num_sectors = avail; |
| 5569 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5570 | return -ENOSPC; |
| 5571 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5572 | rv = mddev->pers->resize(mddev, num_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5573 | if (!rv) |
| 5574 | revalidate_disk(mddev->gendisk); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5575 | return rv; |
| 5576 | } |
| 5577 | |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5578 | static int update_raid_disks(mddev_t *mddev, int raid_disks) |
| 5579 | { |
| 5580 | int rv; |
| 5581 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5582 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5583 | return -EINVAL; |
| 5584 | if (raid_disks <= 0 || |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 5585 | (mddev->max_disks && raid_disks >= mddev->max_disks)) |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5586 | return -EINVAL; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5587 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5588 | return -EBUSY; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5589 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
| 5590 | |
| 5591 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | de171cb | 2011-01-31 11:57:42 +1100 | [diff] [blame] | 5592 | if (rv < 0) |
| 5593 | mddev->delta_disks = 0; |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5594 | return rv; |
| 5595 | } |
| 5596 | |
| 5597 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5598 | /* |
| 5599 | * update_array_info is used to change the configuration of an |
| 5600 | * on-line array. |
| 5601 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 5602 | * fields in the info are checked against the array. |
| 5603 | * Any differences that cannot be handled will cause an error. |
| 5604 | * Normally, only one change can be managed at a time. |
| 5605 | */ |
| 5606 | static int update_array_info(mddev_t *mddev, mdu_array_info_t *info) |
| 5607 | { |
| 5608 | int rv = 0; |
| 5609 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5610 | int state = 0; |
| 5611 | |
| 5612 | /* calculate expected state,ignoring low bits */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5613 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5614 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5615 | |
| 5616 | if (mddev->major_version != info->major_version || |
| 5617 | mddev->minor_version != info->minor_version || |
| 5618 | /* mddev->patch_version != info->patch_version || */ |
| 5619 | mddev->ctime != info->ctime || |
| 5620 | mddev->level != info->level || |
| 5621 | /* mddev->layout != info->layout || */ |
| 5622 | !mddev->persistent != info->not_persistent|| |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5623 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5624 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 5625 | ((state^info->state) & 0xfffffe00) |
| 5626 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5627 | return -EINVAL; |
| 5628 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5629 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 5630 | cnt++; |
| 5631 | if (mddev->raid_disks != info->raid_disks) |
| 5632 | cnt++; |
| 5633 | if (mddev->layout != info->layout) |
| 5634 | cnt++; |
| 5635 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 5636 | cnt++; |
| 5637 | if (cnt == 0) |
| 5638 | return 0; |
| 5639 | if (cnt > 1) |
| 5640 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5641 | |
| 5642 | if (mddev->layout != info->layout) { |
| 5643 | /* Change layout |
| 5644 | * we don't need to do anything at the md level, the |
| 5645 | * personality will take care of it all. |
| 5646 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5647 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5648 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5649 | else { |
| 5650 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5651 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5652 | if (rv) |
| 5653 | mddev->new_layout = mddev->layout; |
| 5654 | return rv; |
| 5655 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5656 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5657 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5658 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5659 | |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5660 | if (mddev->raid_disks != info->raid_disks) |
| 5661 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5662 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5663 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5664 | if (mddev->pers->quiesce == NULL) |
| 5665 | return -EINVAL; |
| 5666 | if (mddev->recovery || mddev->sync_thread) |
| 5667 | return -EBUSY; |
| 5668 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5669 | /* add the bitmap */ |
| 5670 | if (mddev->bitmap) |
| 5671 | return -EEXIST; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5672 | if (mddev->bitmap_info.default_offset == 0) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5673 | return -EINVAL; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5674 | mddev->bitmap_info.offset = |
| 5675 | mddev->bitmap_info.default_offset; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5676 | mddev->pers->quiesce(mddev, 1); |
| 5677 | rv = bitmap_create(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5678 | if (!rv) |
| 5679 | rv = bitmap_load(mddev); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5680 | if (rv) |
| 5681 | bitmap_destroy(mddev); |
| 5682 | mddev->pers->quiesce(mddev, 0); |
| 5683 | } else { |
| 5684 | /* remove the bitmap */ |
| 5685 | if (!mddev->bitmap) |
| 5686 | return -ENOENT; |
| 5687 | if (mddev->bitmap->file) |
| 5688 | return -EINVAL; |
| 5689 | mddev->pers->quiesce(mddev, 1); |
| 5690 | bitmap_destroy(mddev); |
| 5691 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5692 | mddev->bitmap_info.offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5693 | } |
| 5694 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5695 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5696 | return rv; |
| 5697 | } |
| 5698 | |
| 5699 | static int set_disk_faulty(mddev_t *mddev, dev_t dev) |
| 5700 | { |
| 5701 | mdk_rdev_t *rdev; |
| 5702 | |
| 5703 | if (mddev->pers == NULL) |
| 5704 | return -ENODEV; |
| 5705 | |
| 5706 | rdev = find_rdev(mddev, dev); |
| 5707 | if (!rdev) |
| 5708 | return -ENODEV; |
| 5709 | |
| 5710 | md_error(mddev, rdev); |
| 5711 | return 0; |
| 5712 | } |
| 5713 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 5714 | /* |
| 5715 | * We have a problem here : there is no easy way to give a CHS |
| 5716 | * virtual geometry. We currently pretend that we have a 2 heads |
| 5717 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 5718 | * dosfs just mad... ;-) |
| 5719 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5720 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 5721 | { |
| 5722 | mddev_t *mddev = bdev->bd_disk->private_data; |
| 5723 | |
| 5724 | geo->heads = 2; |
| 5725 | geo->sectors = 4; |
NeilBrown | 49ce6ce | 2010-03-29 10:51:42 +1100 | [diff] [blame] | 5726 | geo->cylinders = mddev->array_sectors / 8; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5727 | return 0; |
| 5728 | } |
| 5729 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5730 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5731 | unsigned int cmd, unsigned long arg) |
| 5732 | { |
| 5733 | int err = 0; |
| 5734 | void __user *argp = (void __user *)arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5735 | mddev_t *mddev = NULL; |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 5736 | int ro; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5737 | |
| 5738 | if (!capable(CAP_SYS_ADMIN)) |
| 5739 | return -EACCES; |
| 5740 | |
| 5741 | /* |
| 5742 | * Commands dealing with the RAID driver but not any |
| 5743 | * particular array: |
| 5744 | */ |
| 5745 | switch (cmd) |
| 5746 | { |
| 5747 | case RAID_VERSION: |
| 5748 | err = get_version(argp); |
| 5749 | goto done; |
| 5750 | |
| 5751 | case PRINT_RAID_DEBUG: |
| 5752 | err = 0; |
| 5753 | md_print_devices(); |
| 5754 | goto done; |
| 5755 | |
| 5756 | #ifndef MODULE |
| 5757 | case RAID_AUTORUN: |
| 5758 | err = 0; |
| 5759 | autostart_arrays(arg); |
| 5760 | goto done; |
| 5761 | #endif |
| 5762 | default:; |
| 5763 | } |
| 5764 | |
| 5765 | /* |
| 5766 | * Commands creating/starting a new array: |
| 5767 | */ |
| 5768 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5769 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5770 | |
| 5771 | if (!mddev) { |
| 5772 | BUG(); |
| 5773 | goto abort; |
| 5774 | } |
| 5775 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5776 | err = mddev_lock(mddev); |
| 5777 | if (err) { |
| 5778 | printk(KERN_INFO |
| 5779 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 5780 | err, cmd); |
| 5781 | goto abort; |
| 5782 | } |
| 5783 | |
| 5784 | switch (cmd) |
| 5785 | { |
| 5786 | case SET_ARRAY_INFO: |
| 5787 | { |
| 5788 | mdu_array_info_t info; |
| 5789 | if (!arg) |
| 5790 | memset(&info, 0, sizeof(info)); |
| 5791 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 5792 | err = -EFAULT; |
| 5793 | goto abort_unlock; |
| 5794 | } |
| 5795 | if (mddev->pers) { |
| 5796 | err = update_array_info(mddev, &info); |
| 5797 | if (err) { |
| 5798 | printk(KERN_WARNING "md: couldn't update" |
| 5799 | " array info. %d\n", err); |
| 5800 | goto abort_unlock; |
| 5801 | } |
| 5802 | goto done_unlock; |
| 5803 | } |
| 5804 | if (!list_empty(&mddev->disks)) { |
| 5805 | printk(KERN_WARNING |
| 5806 | "md: array %s already has disks!\n", |
| 5807 | mdname(mddev)); |
| 5808 | err = -EBUSY; |
| 5809 | goto abort_unlock; |
| 5810 | } |
| 5811 | if (mddev->raid_disks) { |
| 5812 | printk(KERN_WARNING |
| 5813 | "md: array %s already initialised!\n", |
| 5814 | mdname(mddev)); |
| 5815 | err = -EBUSY; |
| 5816 | goto abort_unlock; |
| 5817 | } |
| 5818 | err = set_array_info(mddev, &info); |
| 5819 | if (err) { |
| 5820 | printk(KERN_WARNING "md: couldn't set" |
| 5821 | " array info. %d\n", err); |
| 5822 | goto abort_unlock; |
| 5823 | } |
| 5824 | } |
| 5825 | goto done_unlock; |
| 5826 | |
| 5827 | default:; |
| 5828 | } |
| 5829 | |
| 5830 | /* |
| 5831 | * Commands querying/configuring an existing array: |
| 5832 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5833 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5834 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 5835 | if ((!mddev->raid_disks && !mddev->external) |
| 5836 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 5837 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 5838 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5839 | err = -ENODEV; |
| 5840 | goto abort_unlock; |
| 5841 | } |
| 5842 | |
| 5843 | /* |
| 5844 | * Commands even a read-only array can execute: |
| 5845 | */ |
| 5846 | switch (cmd) |
| 5847 | { |
| 5848 | case GET_ARRAY_INFO: |
| 5849 | err = get_array_info(mddev, argp); |
| 5850 | goto done_unlock; |
| 5851 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5852 | case GET_BITMAP_FILE: |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 5853 | err = get_bitmap_file(mddev, argp); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5854 | goto done_unlock; |
| 5855 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5856 | case GET_DISK_INFO: |
| 5857 | err = get_disk_info(mddev, argp); |
| 5858 | goto done_unlock; |
| 5859 | |
| 5860 | case RESTART_ARRAY_RW: |
| 5861 | err = restart_array(mddev); |
| 5862 | goto done_unlock; |
| 5863 | |
| 5864 | case STOP_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5865 | err = do_md_stop(mddev, 0, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5866 | goto done_unlock; |
| 5867 | |
| 5868 | case STOP_ARRAY_RO: |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5869 | err = md_set_readonly(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5870 | goto done_unlock; |
| 5871 | |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 5872 | case BLKROSET: |
| 5873 | if (get_user(ro, (int __user *)(arg))) { |
| 5874 | err = -EFAULT; |
| 5875 | goto done_unlock; |
| 5876 | } |
| 5877 | err = -EINVAL; |
| 5878 | |
| 5879 | /* if the bdev is going readonly the value of mddev->ro |
| 5880 | * does not matter, no writes are coming |
| 5881 | */ |
| 5882 | if (ro) |
| 5883 | goto done_unlock; |
| 5884 | |
| 5885 | /* are we are already prepared for writes? */ |
| 5886 | if (mddev->ro != 1) |
| 5887 | goto done_unlock; |
| 5888 | |
| 5889 | /* transitioning to readauto need only happen for |
| 5890 | * arrays that call md_write_start |
| 5891 | */ |
| 5892 | if (mddev->pers) { |
| 5893 | err = restart_array(mddev); |
| 5894 | if (err == 0) { |
| 5895 | mddev->ro = 2; |
| 5896 | set_disk_ro(mddev->gendisk, 0); |
| 5897 | } |
| 5898 | } |
| 5899 | goto done_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5900 | } |
| 5901 | |
| 5902 | /* |
| 5903 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5904 | * superblock, so we do not allow them on read-only arrays. |
| 5905 | * However non-MD ioctls (e.g. get-size) will still come through |
| 5906 | * here and hit the 'default' below, so only disallow |
| 5907 | * 'md' ioctls, and switch to rw mode if started auto-readonly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5908 | */ |
Andre Noll | bb57fc6 | 2008-04-25 19:06:35 +0200 | [diff] [blame] | 5909 | if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5910 | if (mddev->ro == 2) { |
| 5911 | mddev->ro = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5912 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5913 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5914 | md_wakeup_thread(mddev->thread); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5915 | } else { |
| 5916 | err = -EROFS; |
| 5917 | goto abort_unlock; |
| 5918 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5919 | } |
| 5920 | |
| 5921 | switch (cmd) |
| 5922 | { |
| 5923 | case ADD_NEW_DISK: |
| 5924 | { |
| 5925 | mdu_disk_info_t info; |
| 5926 | if (copy_from_user(&info, argp, sizeof(info))) |
| 5927 | err = -EFAULT; |
| 5928 | else |
| 5929 | err = add_new_disk(mddev, &info); |
| 5930 | goto done_unlock; |
| 5931 | } |
| 5932 | |
| 5933 | case HOT_REMOVE_DISK: |
| 5934 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
| 5935 | goto done_unlock; |
| 5936 | |
| 5937 | case HOT_ADD_DISK: |
| 5938 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
| 5939 | goto done_unlock; |
| 5940 | |
| 5941 | case SET_DISK_FAULTY: |
| 5942 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
| 5943 | goto done_unlock; |
| 5944 | |
| 5945 | case RUN_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5946 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5947 | goto done_unlock; |
| 5948 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5949 | case SET_BITMAP_FILE: |
| 5950 | err = set_bitmap_file(mddev, (int)arg); |
| 5951 | goto done_unlock; |
| 5952 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5953 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5954 | err = -EINVAL; |
| 5955 | goto abort_unlock; |
| 5956 | } |
| 5957 | |
| 5958 | done_unlock: |
| 5959 | abort_unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5960 | if (mddev->hold_active == UNTIL_IOCTL && |
| 5961 | err != -EINVAL) |
| 5962 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5963 | mddev_unlock(mddev); |
| 5964 | |
| 5965 | return err; |
| 5966 | done: |
| 5967 | if (err) |
| 5968 | MD_BUG(); |
| 5969 | abort: |
| 5970 | return err; |
| 5971 | } |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 5972 | #ifdef CONFIG_COMPAT |
| 5973 | static int md_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 5974 | unsigned int cmd, unsigned long arg) |
| 5975 | { |
| 5976 | switch (cmd) { |
| 5977 | case HOT_REMOVE_DISK: |
| 5978 | case HOT_ADD_DISK: |
| 5979 | case SET_DISK_FAULTY: |
| 5980 | case SET_BITMAP_FILE: |
| 5981 | /* These take in integer arg, do not convert */ |
| 5982 | break; |
| 5983 | default: |
| 5984 | arg = (unsigned long)compat_ptr(arg); |
| 5985 | break; |
| 5986 | } |
| 5987 | |
| 5988 | return md_ioctl(bdev, mode, cmd, arg); |
| 5989 | } |
| 5990 | #endif /* CONFIG_COMPAT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5991 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5992 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5993 | { |
| 5994 | /* |
| 5995 | * Succeed if we can lock the mddev, which confirms that |
| 5996 | * it isn't being stopped right now. |
| 5997 | */ |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5998 | mddev_t *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5999 | int err; |
| 6000 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6001 | if (mddev->gendisk != bdev->bd_disk) { |
| 6002 | /* we are racing with mddev_put which is discarding this |
| 6003 | * bd_disk. |
| 6004 | */ |
| 6005 | mddev_put(mddev); |
| 6006 | /* Wait until bdev->bd_disk is definitely gone */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 6007 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6008 | /* Then retry the open from the top */ |
| 6009 | return -ERESTARTSYS; |
| 6010 | } |
| 6011 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 6012 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 6013 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6014 | goto out; |
| 6015 | |
| 6016 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6017 | atomic_inc(&mddev->openers); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 6018 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6019 | |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6020 | check_disk_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6021 | out: |
| 6022 | return err; |
| 6023 | } |
| 6024 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6025 | static int md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6026 | { |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6027 | mddev_t *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6028 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 6029 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6030 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6031 | mddev_put(mddev); |
| 6032 | |
| 6033 | return 0; |
| 6034 | } |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6035 | |
| 6036 | static int md_media_changed(struct gendisk *disk) |
| 6037 | { |
| 6038 | mddev_t *mddev = disk->private_data; |
| 6039 | |
| 6040 | return mddev->changed; |
| 6041 | } |
| 6042 | |
| 6043 | static int md_revalidate(struct gendisk *disk) |
| 6044 | { |
| 6045 | mddev_t *mddev = disk->private_data; |
| 6046 | |
| 6047 | mddev->changed = 0; |
| 6048 | return 0; |
| 6049 | } |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 6050 | static const struct block_device_operations md_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6051 | { |
| 6052 | .owner = THIS_MODULE, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6053 | .open = md_open, |
| 6054 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 6055 | .ioctl = md_ioctl, |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 6056 | #ifdef CONFIG_COMPAT |
| 6057 | .compat_ioctl = md_compat_ioctl, |
| 6058 | #endif |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6059 | .getgeo = md_getgeo, |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6060 | .media_changed = md_media_changed, |
| 6061 | .revalidate_disk= md_revalidate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6062 | }; |
| 6063 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6064 | static int md_thread(void * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6065 | { |
| 6066 | mdk_thread_t *thread = arg; |
| 6067 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6068 | /* |
| 6069 | * md_thread is a 'system-thread', it's priority should be very |
| 6070 | * high. We avoid resource deadlocks individually in each |
| 6071 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 6072 | * the very same RT priority as kswapd, thus we will never get |
| 6073 | * into a priority inversion deadlock. |
| 6074 | * |
| 6075 | * we definitely have to have equal or higher priority than |
| 6076 | * bdflush, otherwise bdflush will deadlock if there are too |
| 6077 | * many dirty RAID5 blocks. |
| 6078 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6079 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 6080 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6081 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6082 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 6083 | /* We need to wait INTERRUPTIBLE so that |
| 6084 | * we don't add to the load-average. |
| 6085 | * That means we need to be sure no signals are |
| 6086 | * pending |
| 6087 | */ |
| 6088 | if (signal_pending(current)) |
| 6089 | flush_signals(current); |
| 6090 | |
| 6091 | wait_event_interruptible_timeout |
| 6092 | (thread->wqueue, |
| 6093 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 6094 | || kthread_should_stop(), |
| 6095 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6096 | |
NeilBrown | 6c98791 | 2011-01-14 09:13:53 +1100 | [diff] [blame] | 6097 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 6098 | if (!kthread_should_stop()) |
NeilBrown | 589a594 | 2010-12-09 17:02:14 +1100 | [diff] [blame] | 6099 | thread->run(thread->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6100 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6101 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6102 | return 0; |
| 6103 | } |
| 6104 | |
| 6105 | void md_wakeup_thread(mdk_thread_t *thread) |
| 6106 | { |
| 6107 | if (thread) { |
| 6108 | dprintk("md: waking up MD thread %s.\n", thread->tsk->comm); |
| 6109 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 6110 | wake_up(&thread->wqueue); |
| 6111 | } |
| 6112 | } |
| 6113 | |
| 6114 | mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev, |
| 6115 | const char *name) |
| 6116 | { |
| 6117 | mdk_thread_t *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6118 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 6119 | thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6120 | if (!thread) |
| 6121 | return NULL; |
| 6122 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6123 | init_waitqueue_head(&thread->wqueue); |
| 6124 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6125 | thread->run = run; |
| 6126 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6127 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 6128 | thread->tsk = kthread_run(md_thread, thread, |
| 6129 | "%s_%s", |
| 6130 | mdname(thread->mddev), |
| 6131 | name ?: mddev->pers->name); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6132 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6133 | kfree(thread); |
| 6134 | return NULL; |
| 6135 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6136 | return thread; |
| 6137 | } |
| 6138 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6139 | void md_unregister_thread(mdk_thread_t *thread) |
| 6140 | { |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6141 | if (!thread) |
| 6142 | return; |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 6143 | dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6144 | |
| 6145 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6146 | kfree(thread); |
| 6147 | } |
| 6148 | |
| 6149 | void md_error(mddev_t *mddev, mdk_rdev_t *rdev) |
| 6150 | { |
| 6151 | if (!mddev) { |
| 6152 | MD_BUG(); |
| 6153 | return; |
| 6154 | } |
| 6155 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6156 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6157 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6158 | |
| 6159 | if (mddev->external) |
| 6160 | set_bit(Blocked, &rdev->flags); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6161 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6162 | dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n", |
| 6163 | mdname(mddev), |
| 6164 | MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev), |
| 6165 | __builtin_return_address(0),__builtin_return_address(1), |
| 6166 | __builtin_return_address(2),__builtin_return_address(3)); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6167 | */ |
Andrew Morton | d0a0a5e | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 6168 | if (!mddev->pers) |
| 6169 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6170 | if (!mddev->pers->error_handler) |
| 6171 | return; |
| 6172 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6173 | if (mddev->degraded) |
| 6174 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6175 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6176 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6177 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6178 | md_wakeup_thread(mddev->thread); |
NeilBrown | 768a418 | 2010-07-26 11:49:55 +1000 | [diff] [blame] | 6179 | if (mddev->event_work.func) |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 6180 | queue_work(md_misc_wq, &mddev->event_work); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 6181 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6182 | } |
| 6183 | |
| 6184 | /* seq_file implementation /proc/mdstat */ |
| 6185 | |
| 6186 | static void status_unused(struct seq_file *seq) |
| 6187 | { |
| 6188 | int i = 0; |
| 6189 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6190 | |
| 6191 | seq_printf(seq, "unused devices: "); |
| 6192 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6193 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6194 | char b[BDEVNAME_SIZE]; |
| 6195 | i++; |
| 6196 | seq_printf(seq, "%s ", |
| 6197 | bdevname(rdev->bdev,b)); |
| 6198 | } |
| 6199 | if (!i) |
| 6200 | seq_printf(seq, "<none>"); |
| 6201 | |
| 6202 | seq_printf(seq, "\n"); |
| 6203 | } |
| 6204 | |
| 6205 | |
| 6206 | static void status_resync(struct seq_file *seq, mddev_t * mddev) |
| 6207 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6208 | sector_t max_sectors, resync, res; |
| 6209 | unsigned long dt, db; |
| 6210 | sector_t rt; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6211 | int scale; |
| 6212 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6213 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6214 | resync = mddev->curr_resync - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6215 | |
| 6216 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6217 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6218 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6219 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6220 | |
| 6221 | /* |
| 6222 | * Should not happen. |
| 6223 | */ |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6224 | if (!max_sectors) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6225 | MD_BUG(); |
| 6226 | return; |
| 6227 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6228 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6229 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6230 | * u32, as those are the requirements for sector_div. |
| 6231 | * Thus 'scale' must be at least 10 |
| 6232 | */ |
| 6233 | scale = 10; |
| 6234 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6235 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6236 | scale++; |
| 6237 | } |
| 6238 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6239 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6240 | |
| 6241 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6242 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6243 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6244 | seq_printf(seq, "["); |
| 6245 | for (i = 0; i < x; i++) |
| 6246 | seq_printf(seq, "="); |
| 6247 | seq_printf(seq, ">"); |
| 6248 | for (i = 0; i < y; i++) |
| 6249 | seq_printf(seq, "."); |
| 6250 | seq_printf(seq, "] "); |
| 6251 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6252 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6253 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 6254 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6255 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 6256 | "check" : |
| 6257 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 6258 | "resync" : "recovery"))), |
| 6259 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6260 | (unsigned long long) resync/2, |
| 6261 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6262 | |
| 6263 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6264 | * dt: time from mark until now |
| 6265 | * db: blocks written from mark until now |
| 6266 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6267 | * |
| 6268 | * rt is a sector_t, so could be 32bit or 64bit. |
| 6269 | * So we divide before multiply in case it is 32bit and close |
| 6270 | * to the limit. |
| 6271 | * We scale the divisor (db) by 32 to avoid loosing precision |
| 6272 | * near the end of resync when the number of remaining sectors |
| 6273 | * is close to 'db'. |
| 6274 | * We then divide rt by 32 after multiplying by db to compensate. |
| 6275 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6276 | */ |
| 6277 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 6278 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6279 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 6280 | - mddev->resync_mark_cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6281 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6282 | rt = max_sectors - resync; /* number of remaining sectors */ |
| 6283 | sector_div(rt, db/32+1); |
| 6284 | rt *= dt; |
| 6285 | rt >>= 5; |
| 6286 | |
| 6287 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 6288 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6289 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6290 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6291 | } |
| 6292 | |
| 6293 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 6294 | { |
| 6295 | struct list_head *tmp; |
| 6296 | loff_t l = *pos; |
| 6297 | mddev_t *mddev; |
| 6298 | |
| 6299 | if (l >= 0x10000) |
| 6300 | return NULL; |
| 6301 | if (!l--) |
| 6302 | /* header */ |
| 6303 | return (void*)1; |
| 6304 | |
| 6305 | spin_lock(&all_mddevs_lock); |
| 6306 | list_for_each(tmp,&all_mddevs) |
| 6307 | if (!l--) { |
| 6308 | mddev = list_entry(tmp, mddev_t, all_mddevs); |
| 6309 | mddev_get(mddev); |
| 6310 | spin_unlock(&all_mddevs_lock); |
| 6311 | return mddev; |
| 6312 | } |
| 6313 | spin_unlock(&all_mddevs_lock); |
| 6314 | if (!l--) |
| 6315 | return (void*)2;/* tail */ |
| 6316 | return NULL; |
| 6317 | } |
| 6318 | |
| 6319 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 6320 | { |
| 6321 | struct list_head *tmp; |
| 6322 | mddev_t *next_mddev, *mddev = v; |
| 6323 | |
| 6324 | ++*pos; |
| 6325 | if (v == (void*)2) |
| 6326 | return NULL; |
| 6327 | |
| 6328 | spin_lock(&all_mddevs_lock); |
| 6329 | if (v == (void*)1) |
| 6330 | tmp = all_mddevs.next; |
| 6331 | else |
| 6332 | tmp = mddev->all_mddevs.next; |
| 6333 | if (tmp != &all_mddevs) |
| 6334 | next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs)); |
| 6335 | else { |
| 6336 | next_mddev = (void*)2; |
| 6337 | *pos = 0x10000; |
| 6338 | } |
| 6339 | spin_unlock(&all_mddevs_lock); |
| 6340 | |
| 6341 | if (v != (void*)1) |
| 6342 | mddev_put(mddev); |
| 6343 | return next_mddev; |
| 6344 | |
| 6345 | } |
| 6346 | |
| 6347 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 6348 | { |
| 6349 | mddev_t *mddev = v; |
| 6350 | |
| 6351 | if (mddev && v != (void*)1 && v != (void*)2) |
| 6352 | mddev_put(mddev); |
| 6353 | } |
| 6354 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6355 | struct mdstat_info { |
| 6356 | int event; |
| 6357 | }; |
| 6358 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6359 | static int md_seq_show(struct seq_file *seq, void *v) |
| 6360 | { |
| 6361 | mddev_t *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6362 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6363 | mdk_rdev_t *rdev; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6364 | struct mdstat_info *mi = seq->private; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6365 | struct bitmap *bitmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6366 | |
| 6367 | if (v == (void*)1) { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6368 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6369 | seq_printf(seq, "Personalities : "); |
| 6370 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6371 | list_for_each_entry(pers, &pers_list, list) |
| 6372 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6373 | |
| 6374 | spin_unlock(&pers_lock); |
| 6375 | seq_printf(seq, "\n"); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6376 | mi->event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6377 | return 0; |
| 6378 | } |
| 6379 | if (v == (void*)2) { |
| 6380 | status_unused(seq); |
| 6381 | return 0; |
| 6382 | } |
| 6383 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6384 | if (mddev_lock(mddev) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6385 | return -EINTR; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6386 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6387 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 6388 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 6389 | mddev->pers ? "" : "in"); |
| 6390 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6391 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6392 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6393 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 6394 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6395 | seq_printf(seq, " %s", mddev->pers->name); |
| 6396 | } |
| 6397 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6398 | sectors = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6399 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6400 | char b[BDEVNAME_SIZE]; |
| 6401 | seq_printf(seq, " %s[%d]", |
| 6402 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6403 | if (test_bit(WriteMostly, &rdev->flags)) |
| 6404 | seq_printf(seq, "(W)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6405 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6406 | seq_printf(seq, "(F)"); |
| 6407 | continue; |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6408 | } else if (rdev->raid_disk < 0) |
| 6409 | seq_printf(seq, "(S)"); /* spare */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6410 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6411 | } |
| 6412 | |
| 6413 | if (!list_empty(&mddev->disks)) { |
| 6414 | if (mddev->pers) |
| 6415 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 6416 | (unsigned long long) |
| 6417 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6418 | else |
| 6419 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6420 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6421 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6422 | if (mddev->persistent) { |
| 6423 | if (mddev->major_version != 0 || |
| 6424 | mddev->minor_version != 90) { |
| 6425 | seq_printf(seq," super %d.%d", |
| 6426 | mddev->major_version, |
| 6427 | mddev->minor_version); |
| 6428 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6429 | } else if (mddev->external) |
| 6430 | seq_printf(seq, " super external:%s", |
| 6431 | mddev->metadata_type); |
| 6432 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6433 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6434 | |
| 6435 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6436 | mddev->pers->status(seq, mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6437 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6438 | if (mddev->pers->sync_request) { |
| 6439 | if (mddev->curr_resync > 2) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6440 | status_resync(seq, mddev); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6441 | seq_printf(seq, "\n "); |
| 6442 | } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2) |
| 6443 | seq_printf(seq, "\tresync=DELAYED\n "); |
| 6444 | else if (mddev->recovery_cp < MaxSector) |
| 6445 | seq_printf(seq, "\tresync=PENDING\n "); |
| 6446 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6447 | } else |
| 6448 | seq_printf(seq, "\n "); |
| 6449 | |
| 6450 | if ((bitmap = mddev->bitmap)) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6451 | unsigned long chunk_kb; |
| 6452 | unsigned long flags; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6453 | spin_lock_irqsave(&bitmap->lock, flags); |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 6454 | chunk_kb = mddev->bitmap_info.chunksize >> 10; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6455 | seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], " |
| 6456 | "%lu%s chunk", |
| 6457 | bitmap->pages - bitmap->missing_pages, |
| 6458 | bitmap->pages, |
| 6459 | (bitmap->pages - bitmap->missing_pages) |
| 6460 | << (PAGE_SHIFT - 10), |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 6461 | chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize, |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6462 | chunk_kb ? "KB" : "B"); |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 6463 | if (bitmap->file) { |
| 6464 | seq_printf(seq, ", file: "); |
Jan Blunck | c32c2f6 | 2008-02-14 19:38:43 -0800 | [diff] [blame] | 6465 | seq_path(seq, &bitmap->file->f_path, " \t\n"); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6466 | } |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 6467 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6468 | seq_printf(seq, "\n"); |
| 6469 | spin_unlock_irqrestore(&bitmap->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6470 | } |
| 6471 | |
| 6472 | seq_printf(seq, "\n"); |
| 6473 | } |
| 6474 | mddev_unlock(mddev); |
| 6475 | |
| 6476 | return 0; |
| 6477 | } |
| 6478 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 6479 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6480 | .start = md_seq_start, |
| 6481 | .next = md_seq_next, |
| 6482 | .stop = md_seq_stop, |
| 6483 | .show = md_seq_show, |
| 6484 | }; |
| 6485 | |
| 6486 | static int md_seq_open(struct inode *inode, struct file *file) |
| 6487 | { |
| 6488 | int error; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6489 | struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL); |
| 6490 | if (mi == NULL) |
| 6491 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6492 | |
| 6493 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6494 | if (error) |
| 6495 | kfree(mi); |
| 6496 | else { |
| 6497 | struct seq_file *p = file->private_data; |
| 6498 | p->private = mi; |
| 6499 | mi->event = atomic_read(&md_event_count); |
| 6500 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6501 | return error; |
| 6502 | } |
| 6503 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6504 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 6505 | { |
| 6506 | struct seq_file *m = filp->private_data; |
| 6507 | struct mdstat_info *mi = m->private; |
| 6508 | int mask; |
| 6509 | |
| 6510 | poll_wait(filp, &md_event_waiters, wait); |
| 6511 | |
| 6512 | /* always allow read */ |
| 6513 | mask = POLLIN | POLLRDNORM; |
| 6514 | |
| 6515 | if (mi->event != atomic_read(&md_event_count)) |
| 6516 | mask |= POLLERR | POLLPRI; |
| 6517 | return mask; |
| 6518 | } |
| 6519 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 6520 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 6521 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6522 | .open = md_seq_open, |
| 6523 | .read = seq_read, |
| 6524 | .llseek = seq_lseek, |
Martin Peschke | c3f94b4 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 6525 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6526 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6527 | }; |
| 6528 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6529 | int register_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6530 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6531 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6532 | list_add_tail(&p->list, &pers_list); |
| 6533 | 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] | 6534 | spin_unlock(&pers_lock); |
| 6535 | return 0; |
| 6536 | } |
| 6537 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6538 | int unregister_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6539 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6540 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6541 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6542 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6543 | spin_unlock(&pers_lock); |
| 6544 | return 0; |
| 6545 | } |
| 6546 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6547 | static int is_mddev_idle(mddev_t *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6548 | { |
| 6549 | mdk_rdev_t * rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6550 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6551 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6552 | |
| 6553 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6554 | rcu_read_lock(); |
| 6555 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6556 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6557 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
| 6558 | (int)part_stat_read(&disk->part0, sectors[1]) - |
| 6559 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 6560 | /* sync IO will cause sync_io to increase before the disk_stats |
| 6561 | * as sync_io is counted when a request starts, and |
| 6562 | * disk_stats is counted when it completes. |
| 6563 | * So resync activity will cause curr_events to be smaller than |
| 6564 | * when there was no such activity. |
| 6565 | * non-sync IO will cause disk_stat to increase without |
| 6566 | * increasing sync_io so curr_events will (eventually) |
| 6567 | * be larger than it was before. Once it becomes |
| 6568 | * substantially larger, the test below will cause |
| 6569 | * the array to appear non-idle, and resync will slow |
| 6570 | * down. |
| 6571 | * If there is a lot of outstanding resync activity when |
| 6572 | * we set last_event to curr_events, then all that activity |
| 6573 | * completing might cause the array to appear non-idle |
| 6574 | * and resync will be slowed down even though there might |
| 6575 | * not have been non-resync activity. This will only |
| 6576 | * happen once though. 'last_events' will soon reflect |
| 6577 | * the state where there is little or no outstanding |
| 6578 | * resync requests, and further resync activity will |
| 6579 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6580 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6581 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6582 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6583 | rdev->last_events = curr_events; |
| 6584 | idle = 0; |
| 6585 | } |
| 6586 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6587 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6588 | return idle; |
| 6589 | } |
| 6590 | |
| 6591 | void md_done_sync(mddev_t *mddev, int blocks, int ok) |
| 6592 | { |
| 6593 | /* another "blocks" (512byte) blocks have been synced */ |
| 6594 | atomic_sub(blocks, &mddev->recovery_active); |
| 6595 | wake_up(&mddev->recovery_wait); |
| 6596 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6597 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6598 | md_wakeup_thread(mddev->thread); |
| 6599 | // stop recovery, signal do_sync .... |
| 6600 | } |
| 6601 | } |
| 6602 | |
| 6603 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6604 | /* md_write_start(mddev, bi) |
| 6605 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6606 | * in superblock) before writing, schedule a superblock update |
| 6607 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6608 | */ |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6609 | void md_write_start(mddev_t *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6610 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6611 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6612 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6613 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6614 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6615 | BUG_ON(mddev->ro == 1); |
| 6616 | if (mddev->ro == 2) { |
| 6617 | /* need to switch to read/write */ |
| 6618 | mddev->ro = 0; |
| 6619 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6620 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 6621 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6622 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6623 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6624 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6625 | if (mddev->safemode == 1) |
| 6626 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6627 | if (mddev->in_sync) { |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6628 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6629 | if (mddev->in_sync) { |
| 6630 | mddev->in_sync = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6631 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 6632 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6633 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6634 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6635 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6636 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6637 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6638 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6639 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 6640 | wait_event(mddev->sb_wait, |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 6641 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6642 | } |
| 6643 | |
| 6644 | void md_write_end(mddev_t *mddev) |
| 6645 | { |
| 6646 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 6647 | if (mddev->safemode == 2) |
| 6648 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 6649 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6650 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 6651 | } |
| 6652 | } |
| 6653 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6654 | /* md_allow_write(mddev) |
| 6655 | * Calling this ensures that the array is marked 'active' so that writes |
| 6656 | * may proceed without blocking. It is important to call this before |
| 6657 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 6658 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6659 | * |
| 6660 | * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock |
| 6661 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6662 | */ |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6663 | int md_allow_write(mddev_t *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6664 | { |
| 6665 | if (!mddev->pers) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6666 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6667 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6668 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 6669 | if (!mddev->pers->sync_request) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6670 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6671 | |
| 6672 | spin_lock_irq(&mddev->write_lock); |
| 6673 | if (mddev->in_sync) { |
| 6674 | mddev->in_sync = 0; |
| 6675 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 6676 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6677 | if (mddev->safemode_delay && |
| 6678 | mddev->safemode == 0) |
| 6679 | mddev->safemode = 1; |
| 6680 | spin_unlock_irq(&mddev->write_lock); |
| 6681 | md_update_sb(mddev, 0); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6682 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6683 | } else |
| 6684 | spin_unlock_irq(&mddev->write_lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6685 | |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 6686 | if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6687 | return -EAGAIN; |
| 6688 | else |
| 6689 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6690 | } |
| 6691 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 6692 | |
NeilBrown | b63d7c2 | 2010-06-01 19:37:33 +1000 | [diff] [blame] | 6693 | void md_unplug(mddev_t *mddev) |
NeilBrown | 252ac52 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 6694 | { |
| 6695 | if (mddev->queue) |
| 6696 | blk_unplug(mddev->queue); |
| 6697 | if (mddev->plug) |
| 6698 | mddev->plug->unplug_fn(mddev->plug); |
| 6699 | } |
| 6700 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6701 | #define SYNC_MARKS 10 |
| 6702 | #define SYNC_MARK_STEP (3*HZ) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6703 | void md_do_sync(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6704 | { |
| 6705 | mddev_t *mddev2; |
| 6706 | unsigned int currspeed = 0, |
| 6707 | window; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6708 | sector_t max_sectors,j, io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6709 | unsigned long mark[SYNC_MARKS]; |
| 6710 | sector_t mark_cnt[SYNC_MARKS]; |
| 6711 | int last_mark,m; |
| 6712 | struct list_head *tmp; |
| 6713 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6714 | int skipped = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6715 | mdk_rdev_t *rdev; |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6716 | char *desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6717 | |
| 6718 | /* just incase thread restarts... */ |
| 6719 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 6720 | return; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6721 | if (mddev->ro) /* never try to sync a read-only array */ |
| 6722 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6723 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6724 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6725 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 6726 | desc = "data-check"; |
| 6727 | else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6728 | desc = "requested-resync"; |
| 6729 | else |
| 6730 | desc = "resync"; |
| 6731 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 6732 | desc = "reshape"; |
| 6733 | else |
| 6734 | desc = "recovery"; |
| 6735 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6736 | /* we overload curr_resync somewhat here. |
| 6737 | * 0 == not engaged in resync at all |
| 6738 | * 2 == checking that there is no conflict with another sync |
| 6739 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 6740 | * commense |
| 6741 | * other == active in resync - this many blocks |
| 6742 | * |
| 6743 | * Before starting a resync we must have set curr_resync to |
| 6744 | * 2, and then checked that every "conflicting" array has curr_resync |
| 6745 | * less than ours. When we find one that is the same or higher |
| 6746 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 6747 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 6748 | * This will mean we have to start checking from the beginning again. |
| 6749 | * |
| 6750 | */ |
| 6751 | |
| 6752 | do { |
| 6753 | mddev->curr_resync = 2; |
| 6754 | |
| 6755 | try_again: |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 6756 | if (kthread_should_stop()) |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 6757 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 6758 | |
| 6759 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6760 | goto skip; |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6761 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6762 | if (mddev2 == mddev) |
| 6763 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 6764 | if (!mddev->parallel_resync |
| 6765 | && mddev2->curr_resync |
| 6766 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6767 | DEFINE_WAIT(wq); |
| 6768 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 6769 | /* arbitrarily yield */ |
| 6770 | mddev->curr_resync = 1; |
| 6771 | wake_up(&resync_wait); |
| 6772 | } |
| 6773 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 6774 | /* no need to wait here, we can wait the next |
| 6775 | * time 'round when curr_resync == 2 |
| 6776 | */ |
| 6777 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 6778 | /* We need to wait 'interruptible' so as not to |
| 6779 | * contribute to the load average, and not to |
| 6780 | * be caught by 'softlockup' |
| 6781 | */ |
| 6782 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 6783 | if (!kthread_should_stop() && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 6784 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6785 | printk(KERN_INFO "md: delaying %s of %s" |
| 6786 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6787 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6788 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6789 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 6790 | if (signal_pending(current)) |
| 6791 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6792 | schedule(); |
| 6793 | finish_wait(&resync_wait, &wq); |
| 6794 | goto try_again; |
| 6795 | } |
| 6796 | finish_wait(&resync_wait, &wq); |
| 6797 | } |
| 6798 | } |
| 6799 | } while (mddev->curr_resync < 2); |
| 6800 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6801 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6802 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6803 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6804 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6805 | */ |
| 6806 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6807 | mddev->resync_mismatches = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6808 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6809 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6810 | j = mddev->resync_min; |
| 6811 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6812 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6813 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6814 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6815 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6816 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6817 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6818 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6819 | j = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6820 | rcu_read_lock(); |
| 6821 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6822 | if (rdev->raid_disk >= 0 && |
| 6823 | !test_bit(Faulty, &rdev->flags) && |
| 6824 | !test_bit(In_sync, &rdev->flags) && |
| 6825 | rdev->recovery_offset < j) |
| 6826 | j = rdev->recovery_offset; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6827 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6828 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6829 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6830 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 6831 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 6832 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 6833 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6834 | "(but not more than %d KB/sec) for %s.\n", |
| 6835 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6836 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6837 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6838 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6839 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6840 | for (m = 0; m < SYNC_MARKS; m++) { |
| 6841 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6842 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6843 | } |
| 6844 | last_mark = 0; |
| 6845 | mddev->resync_mark = mark[last_mark]; |
| 6846 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 6847 | |
| 6848 | /* |
| 6849 | * Tune reconstruction: |
| 6850 | */ |
| 6851 | window = 32*(PAGE_SIZE/512); |
| 6852 | printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n", |
| 6853 | window/2,(unsigned long long) max_sectors/2); |
| 6854 | |
| 6855 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6856 | last_check = 0; |
| 6857 | |
| 6858 | if (j>2) { |
| 6859 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6860 | "md: resuming %s of %s from checkpoint.\n", |
| 6861 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6862 | mddev->curr_resync = j; |
| 6863 | } |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 6864 | mddev->curr_resync_completed = j; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6865 | |
| 6866 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6867 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6868 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6869 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6870 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 6871 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 6872 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 6873 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 6874 | > (max_sectors >> 4)) || |
| 6875 | (j - mddev->curr_resync_completed)*2 |
| 6876 | >= mddev->resync_max - mddev->curr_resync_completed |
| 6877 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6878 | /* time to update curr_resync_completed */ |
NeilBrown | 252ac52 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 6879 | md_unplug(mddev); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6880 | wait_event(mddev->recovery_wait, |
| 6881 | atomic_read(&mddev->recovery_active) == 0); |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 6882 | mddev->curr_resync_completed = j; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 6883 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6884 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6885 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6886 | |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 6887 | while (j >= mddev->resync_max && !kthread_should_stop()) { |
| 6888 | /* As this condition is controlled by user-space, |
| 6889 | * we can block indefinitely, so use '_interruptible' |
| 6890 | * to avoid triggering warnings. |
| 6891 | */ |
| 6892 | flush_signals(current); /* just in case */ |
| 6893 | wait_event_interruptible(mddev->recovery_wait, |
| 6894 | mddev->resync_max > j |
| 6895 | || kthread_should_stop()); |
| 6896 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6897 | |
| 6898 | if (kthread_should_stop()) |
| 6899 | goto interrupted; |
| 6900 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6901 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6902 | currspeed < speed_min(mddev)); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6903 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6904 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6905 | goto out; |
| 6906 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6907 | |
| 6908 | if (!skipped) { /* actual IO requested */ |
| 6909 | io_sectors += sectors; |
| 6910 | atomic_add(sectors, &mddev->recovery_active); |
| 6911 | } |
| 6912 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6913 | j += sectors; |
| 6914 | if (j>1) mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6915 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6916 | if (last_check == 0) |
| 6917 | /* this is the earliers that rebuilt will be |
| 6918 | * visible in /proc/mdstat |
| 6919 | */ |
| 6920 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6921 | |
| 6922 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6923 | continue; |
| 6924 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6925 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6926 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6927 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6928 | break; |
| 6929 | |
| 6930 | repeat: |
| 6931 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 6932 | /* step marks */ |
| 6933 | int next = (last_mark+1) % SYNC_MARKS; |
| 6934 | |
| 6935 | mddev->resync_mark = mark[next]; |
| 6936 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 6937 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6938 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6939 | last_mark = next; |
| 6940 | } |
| 6941 | |
| 6942 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6943 | if (kthread_should_stop()) |
| 6944 | goto interrupted; |
| 6945 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6946 | |
| 6947 | /* |
| 6948 | * this loop exits only if either when we are slower than |
| 6949 | * the 'hard' speed limit, or the system was IO-idle for |
| 6950 | * a jiffy. |
| 6951 | * the system might be non-idle CPU-wise, but we only care |
| 6952 | * about not overloading the IO subsystem. (things like an |
| 6953 | * e2fsck being done on the RAID array should execute fast) |
| 6954 | */ |
NeilBrown | 252ac52 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 6955 | md_unplug(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6956 | cond_resched(); |
| 6957 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6958 | currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2 |
| 6959 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6960 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 6961 | if (currspeed > speed_min(mddev)) { |
| 6962 | if ((currspeed > speed_max(mddev)) || |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6963 | !is_mddev_idle(mddev, 0)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6964 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6965 | goto repeat; |
| 6966 | } |
| 6967 | } |
| 6968 | } |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6969 | printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6970 | /* |
| 6971 | * this also signals 'finished resyncing' to md_stop |
| 6972 | */ |
| 6973 | out: |
NeilBrown | 252ac52 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 6974 | md_unplug(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6975 | |
| 6976 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 6977 | |
| 6978 | /* tell personality that we are finished */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6979 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6980 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6981 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6982 | mddev->curr_resync > 2) { |
| 6983 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6984 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 6985 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 6986 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6987 | "md: checkpointing %s of %s.\n", |
| 6988 | desc, mdname(mddev)); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6989 | mddev->recovery_cp = mddev->curr_resync; |
| 6990 | } |
| 6991 | } else |
| 6992 | mddev->recovery_cp = MaxSector; |
| 6993 | } else { |
| 6994 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 6995 | mddev->curr_resync = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6996 | rcu_read_lock(); |
| 6997 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6998 | if (rdev->raid_disk >= 0 && |
NeilBrown | 70fffd0 | 2010-06-16 17:01:25 +1000 | [diff] [blame] | 6999 | mddev->delta_disks >= 0 && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7000 | !test_bit(Faulty, &rdev->flags) && |
| 7001 | !test_bit(In_sync, &rdev->flags) && |
| 7002 | rdev->recovery_offset < mddev->curr_resync) |
| 7003 | rdev->recovery_offset = mddev->curr_resync; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7004 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7005 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7006 | } |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 7007 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7008 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7009 | skip: |
NeilBrown | c07b70a | 2009-12-14 12:49:48 +1100 | [diff] [blame] | 7010 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 7011 | /* We completed so min/max setting can be forgotten if used. */ |
| 7012 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7013 | mddev->resync_min = 0; |
| 7014 | mddev->resync_max = MaxSector; |
| 7015 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7016 | mddev->resync_min = mddev->curr_resync_completed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7017 | mddev->curr_resync = 0; |
| 7018 | wake_up(&resync_wait); |
| 7019 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
| 7020 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 7021 | return; |
| 7022 | |
| 7023 | interrupted: |
| 7024 | /* |
| 7025 | * got a signal, exit. |
| 7026 | */ |
| 7027 | printk(KERN_INFO |
| 7028 | "md: md_do_sync() got signal ... exiting\n"); |
| 7029 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7030 | goto out; |
| 7031 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7032 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7033 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7034 | |
| 7035 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7036 | static int remove_and_add_spares(mddev_t *mddev) |
| 7037 | { |
| 7038 | mdk_rdev_t *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7039 | int spares = 0; |
| 7040 | |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7041 | mddev->curr_resync_completed = 0; |
| 7042 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 7043 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7044 | if (rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7045 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7046 | (test_bit(Faulty, &rdev->flags) || |
| 7047 | ! test_bit(In_sync, &rdev->flags)) && |
| 7048 | atomic_read(&rdev->nr_pending)==0) { |
| 7049 | if (mddev->pers->hot_remove_disk( |
| 7050 | mddev, rdev->raid_disk)==0) { |
| 7051 | char nm[20]; |
| 7052 | sprintf(nm,"rd%d", rdev->raid_disk); |
| 7053 | sysfs_remove_link(&mddev->kobj, nm); |
| 7054 | rdev->raid_disk = -1; |
| 7055 | } |
| 7056 | } |
| 7057 | |
NeilBrown | a8c42c7 | 2011-01-31 13:47:13 +1100 | [diff] [blame] | 7058 | if (mddev->degraded && !mddev->recovery_disabled) { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 7059 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7060 | if (rdev->raid_disk >= 0 && |
Dan Williams | e542713 | 2008-07-28 23:28:06 -0700 | [diff] [blame] | 7061 | !test_bit(In_sync, &rdev->flags) && |
| 7062 | !test_bit(Blocked, &rdev->flags)) |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7063 | spares++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7064 | if (rdev->raid_disk < 0 |
| 7065 | && !test_bit(Faulty, &rdev->flags)) { |
| 7066 | rdev->recovery_offset = 0; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 7067 | if (mddev->pers-> |
| 7068 | hot_add_disk(mddev, rdev) == 0) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7069 | char nm[20]; |
| 7070 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 7071 | if (sysfs_create_link(&mddev->kobj, |
| 7072 | &rdev->kobj, nm)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7073 | /* failure here is OK */; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7074 | spares++; |
| 7075 | md_new_event(mddev); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 7076 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7077 | } else |
| 7078 | break; |
| 7079 | } |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7080 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7081 | } |
| 7082 | return spares; |
| 7083 | } |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7084 | |
| 7085 | static void reap_sync_thread(mddev_t *mddev) |
| 7086 | { |
| 7087 | mdk_rdev_t *rdev; |
| 7088 | |
| 7089 | /* resync has finished, collect result */ |
| 7090 | md_unregister_thread(mddev->sync_thread); |
| 7091 | mddev->sync_thread = NULL; |
| 7092 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 7093 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
| 7094 | /* success...*/ |
| 7095 | /* activate any spares */ |
| 7096 | if (mddev->pers->spare_active(mddev)) |
| 7097 | sysfs_notify(&mddev->kobj, NULL, |
| 7098 | "degraded"); |
| 7099 | } |
| 7100 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 7101 | mddev->pers->finish_reshape) |
| 7102 | mddev->pers->finish_reshape(mddev); |
| 7103 | md_update_sb(mddev, 1); |
| 7104 | |
| 7105 | /* if array is no-longer degraded, then any saved_raid_disk |
| 7106 | * information must be scrapped |
| 7107 | */ |
| 7108 | if (!mddev->degraded) |
| 7109 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 7110 | rdev->saved_raid_disk = -1; |
| 7111 | |
| 7112 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7113 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7114 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 7115 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 7116 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 7117 | /* flag recovery needed just to double check */ |
| 7118 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7119 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 7120 | md_new_event(mddev); |
| 7121 | } |
| 7122 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7123 | /* |
| 7124 | * This routine is regularly called by all per-raid-array threads to |
| 7125 | * deal with generic issues like resync and super-block update. |
| 7126 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 7127 | * need this as they never do any recovery or update the superblock. |
| 7128 | * |
| 7129 | * It does not do any resync itself, but rather "forks" off other threads |
| 7130 | * to do that as needed. |
| 7131 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 7132 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7133 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7134 | * and wakeups up this thread which will reap the thread and finish up. |
| 7135 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 7136 | * |
| 7137 | * The overall approach is: |
| 7138 | * 1/ if the superblock needs updating, update it. |
| 7139 | * 2/ If a recovery thread is running, don't do anything else. |
| 7140 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 7141 | * 4/ If there are any faulty devices, remove them. |
| 7142 | * 5/ If array is degraded, try to add spares devices |
| 7143 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 7144 | */ |
| 7145 | void md_check_recovery(mddev_t *mddev) |
| 7146 | { |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 7147 | if (mddev->bitmap) |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 7148 | bitmap_daemon_work(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7149 | |
| 7150 | if (mddev->ro) |
| 7151 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7152 | |
| 7153 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7154 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7155 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 7156 | mdname(mddev)); |
| 7157 | mddev->safemode = 2; |
| 7158 | } |
| 7159 | flush_signals(current); |
| 7160 | } |
| 7161 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7162 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 7163 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7164 | if ( ! ( |
NeilBrown | 126925c | 2010-09-07 17:02:47 +1000 | [diff] [blame] | 7165 | (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7166 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7167 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7168 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7169 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 7170 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7171 | )) |
| 7172 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7173 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 7174 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7175 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7176 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7177 | if (mddev->ro) { |
| 7178 | /* Only thing we do on a ro array is remove |
| 7179 | * failed devices. |
| 7180 | */ |
NeilBrown | a8c42c7 | 2011-01-31 13:47:13 +1100 | [diff] [blame] | 7181 | mdk_rdev_t *rdev; |
| 7182 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 7183 | if (rdev->raid_disk >= 0 && |
| 7184 | !test_bit(Blocked, &rdev->flags) && |
| 7185 | test_bit(Faulty, &rdev->flags) && |
| 7186 | atomic_read(&rdev->nr_pending)==0) { |
| 7187 | if (mddev->pers->hot_remove_disk( |
| 7188 | mddev, rdev->raid_disk)==0) { |
| 7189 | char nm[20]; |
| 7190 | sprintf(nm,"rd%d", rdev->raid_disk); |
| 7191 | sysfs_remove_link(&mddev->kobj, nm); |
| 7192 | rdev->raid_disk = -1; |
| 7193 | } |
| 7194 | } |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7195 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7196 | goto unlock; |
| 7197 | } |
| 7198 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7199 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7200 | int did_change = 0; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7201 | spin_lock_irq(&mddev->write_lock); |
| 7202 | if (mddev->safemode && |
| 7203 | !atomic_read(&mddev->writes_pending) && |
| 7204 | !mddev->in_sync && |
| 7205 | mddev->recovery_cp == MaxSector) { |
| 7206 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7207 | did_change = 1; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7208 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7209 | } |
| 7210 | if (mddev->safemode == 1) |
| 7211 | mddev->safemode = 0; |
| 7212 | spin_unlock_irq(&mddev->write_lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7213 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7214 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7215 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7216 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7217 | if (mddev->flags) |
| 7218 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7219 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7220 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 7221 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 7222 | /* resync/recovery still happening */ |
| 7223 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7224 | goto unlock; |
| 7225 | } |
| 7226 | if (mddev->sync_thread) { |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7227 | reap_sync_thread(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7228 | goto unlock; |
| 7229 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7230 | /* Set RUNNING before clearing NEEDED to avoid |
| 7231 | * any transients in the value of "sync_action". |
| 7232 | */ |
| 7233 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7234 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7235 | /* Clear some bits that don't mean anything, but |
| 7236 | * might be left set |
| 7237 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7238 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7239 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7240 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7241 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 7242 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7243 | /* no recovery is running. |
| 7244 | * remove any failed drives, then |
| 7245 | * add spares if possible. |
| 7246 | * Spare are also removed and re-added, to allow |
| 7247 | * the personality to fail the re-add. |
| 7248 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7249 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7250 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 7251 | if (mddev->pers->check_reshape == NULL || |
| 7252 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7253 | /* Cannot proceed */ |
| 7254 | goto unlock; |
| 7255 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7256 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7257 | } else if ((spares = remove_and_add_spares(mddev))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7258 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7259 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 7260 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7261 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7262 | } else if (mddev->recovery_cp < MaxSector) { |
| 7263 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7264 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7265 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 7266 | /* nothing to be done ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7267 | goto unlock; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7268 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7269 | if (mddev->pers->sync_request) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 7270 | if (spares && mddev->bitmap && ! mddev->bitmap->file) { |
| 7271 | /* We are adding a device or devices to an array |
| 7272 | * which has the bitmap stored on all devices. |
| 7273 | * So make sure all bitmap pages get written |
| 7274 | */ |
| 7275 | bitmap_write_all(mddev->bitmap); |
| 7276 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7277 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 7278 | mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 7279 | "resync"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7280 | if (!mddev->sync_thread) { |
| 7281 | printk(KERN_ERR "%s: could not start resync" |
| 7282 | " thread...\n", |
| 7283 | mdname(mddev)); |
| 7284 | /* leave the spares where they are, it shouldn't hurt */ |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7285 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7286 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7287 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 7288 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 7289 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7290 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7291 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7292 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7293 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7294 | } |
| 7295 | unlock: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7296 | if (!mddev->sync_thread) { |
| 7297 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7298 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 7299 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 7300 | if (mddev->sysfs_action) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7301 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7302 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7303 | mddev_unlock(mddev); |
| 7304 | } |
| 7305 | } |
| 7306 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7307 | void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 7308 | { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7309 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7310 | wait_event_timeout(rdev->blocked_wait, |
| 7311 | !test_bit(Blocked, &rdev->flags), |
| 7312 | msecs_to_jiffies(5000)); |
| 7313 | rdev_dec_pending(rdev, mddev); |
| 7314 | } |
| 7315 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 7316 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7317 | static int md_notify_reboot(struct notifier_block *this, |
| 7318 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7319 | { |
| 7320 | struct list_head *tmp; |
| 7321 | mddev_t *mddev; |
| 7322 | |
| 7323 | if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) { |
| 7324 | |
| 7325 | printk(KERN_INFO "md: stopping all md devices.\n"); |
| 7326 | |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7327 | for_each_mddev(mddev, tmp) |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 7328 | if (mddev_trylock(mddev)) { |
NeilBrown | 2b25000 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7329 | /* Force a switch to readonly even array |
| 7330 | * appears to still be in use. Hence |
| 7331 | * the '100'. |
| 7332 | */ |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 7333 | md_set_readonly(mddev, 100); |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 7334 | mddev_unlock(mddev); |
| 7335 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7336 | /* |
| 7337 | * certain more exotic SCSI devices are known to be |
| 7338 | * volatile wrt too early system reboots. While the |
| 7339 | * right place to handle this issue is the given |
| 7340 | * driver, we do want to have a safe RAID driver ... |
| 7341 | */ |
| 7342 | mdelay(1000*1); |
| 7343 | } |
| 7344 | return NOTIFY_DONE; |
| 7345 | } |
| 7346 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7347 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7348 | .notifier_call = md_notify_reboot, |
| 7349 | .next = NULL, |
| 7350 | .priority = INT_MAX, /* before any real devices */ |
| 7351 | }; |
| 7352 | |
| 7353 | static void md_geninit(void) |
| 7354 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7355 | dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t)); |
| 7356 | |
Denis V. Lunev | c7705f3 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 7357 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7358 | } |
| 7359 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7360 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7361 | { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 7362 | int ret = -ENOMEM; |
| 7363 | |
Tejun Heo | ada609e | 2011-01-25 14:35:54 +0100 | [diff] [blame] | 7364 | md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 7365 | if (!md_wq) |
| 7366 | goto err_wq; |
| 7367 | |
| 7368 | md_misc_wq = alloc_workqueue("md_misc", 0, 0); |
| 7369 | if (!md_misc_wq) |
| 7370 | goto err_misc_wq; |
| 7371 | |
| 7372 | if ((ret = register_blkdev(MD_MAJOR, "md")) < 0) |
| 7373 | goto err_md; |
| 7374 | |
| 7375 | if ((ret = register_blkdev(0, "mdp")) < 0) |
| 7376 | goto err_mdp; |
| 7377 | mdp_major = ret; |
| 7378 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7379 | blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE, |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 7380 | md_probe, NULL, NULL); |
| 7381 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7382 | md_probe, NULL, NULL); |
| 7383 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7384 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 7385 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7386 | |
| 7387 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 7388 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7389 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 7390 | err_mdp: |
| 7391 | unregister_blkdev(MD_MAJOR, "md"); |
| 7392 | err_md: |
| 7393 | destroy_workqueue(md_misc_wq); |
| 7394 | err_misc_wq: |
| 7395 | destroy_workqueue(md_wq); |
| 7396 | err_wq: |
| 7397 | return ret; |
| 7398 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7399 | |
| 7400 | #ifndef MODULE |
| 7401 | |
| 7402 | /* |
| 7403 | * Searches all registered partitions for autorun RAID arrays |
| 7404 | * at boot time. |
| 7405 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7406 | |
| 7407 | static LIST_HEAD(all_detected_devices); |
| 7408 | struct detected_devices_node { |
| 7409 | struct list_head list; |
| 7410 | dev_t dev; |
| 7411 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7412 | |
| 7413 | void md_autodetect_dev(dev_t dev) |
| 7414 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7415 | struct detected_devices_node *node_detected_dev; |
| 7416 | |
| 7417 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 7418 | if (node_detected_dev) { |
| 7419 | node_detected_dev->dev = dev; |
| 7420 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 7421 | } else { |
| 7422 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 7423 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 7424 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7425 | } |
| 7426 | |
| 7427 | |
| 7428 | static void autostart_arrays(int part) |
| 7429 | { |
| 7430 | mdk_rdev_t *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7431 | struct detected_devices_node *node_detected_dev; |
| 7432 | dev_t dev; |
| 7433 | int i_scanned, i_passed; |
| 7434 | |
| 7435 | i_scanned = 0; |
| 7436 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7437 | |
| 7438 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 7439 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7440 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 7441 | i_scanned++; |
| 7442 | node_detected_dev = list_entry(all_detected_devices.next, |
| 7443 | struct detected_devices_node, list); |
| 7444 | list_del(&node_detected_dev->list); |
| 7445 | dev = node_detected_dev->dev; |
| 7446 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 7447 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7448 | if (IS_ERR(rdev)) |
| 7449 | continue; |
| 7450 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7451 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7452 | MD_BUG(); |
| 7453 | continue; |
| 7454 | } |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 7455 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7456 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7457 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7458 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7459 | |
| 7460 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 7461 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7462 | |
| 7463 | autorun_devices(part); |
| 7464 | } |
| 7465 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 7466 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7467 | |
| 7468 | static __exit void md_exit(void) |
| 7469 | { |
| 7470 | mddev_t *mddev; |
| 7471 | struct list_head *tmp; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 7472 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7473 | blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS); |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 7474 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7475 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7476 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7477 | unregister_blkdev(mdp_major, "mdp"); |
| 7478 | unregister_reboot_notifier(&md_notifier); |
| 7479 | unregister_sysctl_table(raid_table_header); |
| 7480 | remove_proc_entry("mdstat", NULL); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7481 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7482 | export_array(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7483 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7484 | } |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 7485 | destroy_workqueue(md_misc_wq); |
| 7486 | destroy_workqueue(md_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7487 | } |
| 7488 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 7489 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7490 | module_exit(md_exit) |
| 7491 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7492 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 7493 | { |
| 7494 | return sprintf(buffer, "%d", start_readonly); |
| 7495 | } |
| 7496 | static int set_ro(const char *val, struct kernel_param *kp) |
| 7497 | { |
| 7498 | char *e; |
| 7499 | int num = simple_strtoul(val, &e, 10); |
| 7500 | if (*val && (*e == '\0' || *e == '\n')) { |
| 7501 | start_readonly = num; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 7502 | return 0; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7503 | } |
| 7504 | return -EINVAL; |
| 7505 | } |
| 7506 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 7507 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 7508 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | 6ff8d8e | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 7509 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7510 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7511 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7512 | EXPORT_SYMBOL(register_md_personality); |
| 7513 | EXPORT_SYMBOL(unregister_md_personality); |
| 7514 | EXPORT_SYMBOL(md_error); |
| 7515 | EXPORT_SYMBOL(md_done_sync); |
| 7516 | EXPORT_SYMBOL(md_write_start); |
| 7517 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7518 | EXPORT_SYMBOL(md_register_thread); |
| 7519 | EXPORT_SYMBOL(md_unregister_thread); |
| 7520 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7521 | EXPORT_SYMBOL(md_check_recovery); |
| 7522 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 7523 | MODULE_DESCRIPTION("MD RAID framework"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 7524 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 7525 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |