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 | |
| 556 | retry: |
| 557 | spin_lock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 559 | if (unit) { |
| 560 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 561 | if (mddev->unit == unit) { |
| 562 | mddev_get(mddev); |
| 563 | spin_unlock(&all_mddevs_lock); |
| 564 | kfree(new); |
| 565 | return mddev; |
| 566 | } |
| 567 | |
| 568 | if (new) { |
| 569 | list_add(&new->all_mddevs, &all_mddevs); |
| 570 | spin_unlock(&all_mddevs_lock); |
| 571 | new->hold_active = UNTIL_IOCTL; |
| 572 | return new; |
| 573 | } |
| 574 | } else if (new) { |
| 575 | /* find an unused unit number */ |
| 576 | static int next_minor = 512; |
| 577 | int start = next_minor; |
| 578 | int is_free = 0; |
| 579 | int dev = 0; |
| 580 | while (!is_free) { |
| 581 | dev = MKDEV(MD_MAJOR, next_minor); |
| 582 | next_minor++; |
| 583 | if (next_minor > MINORMASK) |
| 584 | next_minor = 0; |
| 585 | if (next_minor == start) { |
| 586 | /* Oh dear, all in use. */ |
| 587 | spin_unlock(&all_mddevs_lock); |
| 588 | kfree(new); |
| 589 | return NULL; |
| 590 | } |
| 591 | |
| 592 | is_free = 1; |
| 593 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 594 | if (mddev->unit == dev) { |
| 595 | is_free = 0; |
| 596 | break; |
| 597 | } |
| 598 | } |
| 599 | new->unit = dev; |
| 600 | new->md_minor = MINOR(dev); |
| 601 | new->hold_active = UNTIL_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | list_add(&new->all_mddevs, &all_mddevs); |
| 603 | spin_unlock(&all_mddevs_lock); |
| 604 | return new; |
| 605 | } |
| 606 | spin_unlock(&all_mddevs_lock); |
| 607 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 608 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | if (!new) |
| 610 | return NULL; |
| 611 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | new->unit = unit; |
| 613 | if (MAJOR(unit) == MD_MAJOR) |
| 614 | new->md_minor = MINOR(unit); |
| 615 | else |
| 616 | new->md_minor = MINOR(unit) >> MdpMinorShift; |
| 617 | |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 618 | mddev_init(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | goto retry; |
| 621 | } |
| 622 | |
| 623 | static inline int mddev_lock(mddev_t * mddev) |
| 624 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 625 | return mutex_lock_interruptible(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | } |
| 627 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 628 | static inline int mddev_is_locked(mddev_t *mddev) |
| 629 | { |
| 630 | return mutex_is_locked(&mddev->reconfig_mutex); |
| 631 | } |
| 632 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | static inline int mddev_trylock(mddev_t * mddev) |
| 634 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 635 | return mutex_trylock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | } |
| 637 | |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 638 | static struct attribute_group md_redundancy_group; |
| 639 | |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 640 | static void mddev_unlock(mddev_t * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | { |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 642 | if (mddev->to_remove) { |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 643 | /* These cannot be removed under reconfig_mutex as |
| 644 | * an access to the files will try to take reconfig_mutex |
| 645 | * while holding the file unremovable, which leads to |
| 646 | * a deadlock. |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 647 | * So hold set sysfs_active while the remove in happeing, |
| 648 | * and anything else which might set ->to_remove or my |
| 649 | * otherwise change the sysfs namespace will fail with |
| 650 | * -EBUSY if sysfs_active is still set. |
| 651 | * We set sysfs_active under reconfig_mutex and elsewhere |
| 652 | * test it under the same mutex to ensure its correct value |
| 653 | * is seen. |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 654 | */ |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 655 | struct attribute_group *to_remove = mddev->to_remove; |
| 656 | mddev->to_remove = NULL; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 657 | mddev->sysfs_active = 1; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 658 | mutex_unlock(&mddev->reconfig_mutex); |
| 659 | |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 660 | if (mddev->kobj.sd) { |
| 661 | if (to_remove != &md_redundancy_group) |
| 662 | sysfs_remove_group(&mddev->kobj, to_remove); |
| 663 | if (mddev->pers == NULL || |
| 664 | mddev->pers->sync_request == NULL) { |
| 665 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 666 | if (mddev->sysfs_action) |
| 667 | sysfs_put(mddev->sysfs_action); |
| 668 | mddev->sysfs_action = NULL; |
| 669 | } |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 670 | } |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 671 | mddev->sysfs_active = 0; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 672 | } else |
| 673 | mutex_unlock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 675 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | } |
| 677 | |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 678 | static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 680 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 682 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | if (rdev->desc_nr == nr) |
| 684 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 685 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | return NULL; |
| 687 | } |
| 688 | |
| 689 | static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev) |
| 690 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | mdk_rdev_t *rdev; |
| 692 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 693 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | if (rdev->bdev->bd_dev == dev) |
| 695 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 696 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | return NULL; |
| 698 | } |
| 699 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 700 | static struct mdk_personality *find_pers(int level, char *clevel) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 701 | { |
| 702 | struct mdk_personality *pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 703 | list_for_each_entry(pers, &pers_list, list) { |
| 704 | if (level != LEVEL_NONE && pers->level == level) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 705 | return pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 706 | if (strcmp(pers->name, clevel)==0) |
| 707 | return pers; |
| 708 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 709 | return NULL; |
| 710 | } |
| 711 | |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 712 | /* return the offset of the super block in 512byte sectors */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 713 | static inline sector_t calc_dev_sboffset(mdk_rdev_t *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | { |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 715 | sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512; |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 716 | return MD_NEW_SIZE_SECTORS(num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | } |
| 718 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | static int alloc_disk_sb(mdk_rdev_t * rdev) |
| 720 | { |
| 721 | if (rdev->sb_page) |
| 722 | MD_BUG(); |
| 723 | |
| 724 | rdev->sb_page = alloc_page(GFP_KERNEL); |
| 725 | if (!rdev->sb_page) { |
| 726 | printk(KERN_ALERT "md: out of memory.\n"); |
Andre Noll | ebc2433 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 727 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | return 0; |
| 731 | } |
| 732 | |
| 733 | static void free_disk_sb(mdk_rdev_t * rdev) |
| 734 | { |
| 735 | if (rdev->sb_page) { |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 736 | put_page(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | rdev->sb_loaded = 0; |
| 738 | rdev->sb_page = NULL; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 739 | rdev->sb_start = 0; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 740 | rdev->sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | } |
| 742 | } |
| 743 | |
| 744 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 745 | static void super_written(struct bio *bio, int error) |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 746 | { |
| 747 | mdk_rdev_t *rdev = bio->bi_private; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 748 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 749 | |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 750 | if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) { |
| 751 | printk("md: super_written gets error=%d, uptodate=%d\n", |
| 752 | error, test_bit(BIO_UPTODATE, &bio->bi_flags)); |
| 753 | WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags)); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 754 | md_error(mddev, rdev); |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 755 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 756 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 757 | if (atomic_dec_and_test(&mddev->pending_writes)) |
| 758 | wake_up(&mddev->sb_wait); |
Neil Brown | f8b58ed | 2005-06-27 22:29:34 -0700 | [diff] [blame] | 759 | bio_put(bio); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev, |
| 763 | sector_t sector, int size, struct page *page) |
| 764 | { |
| 765 | /* write first size bytes of page to sector of rdev |
| 766 | * Increment mddev->pending_writes before returning |
| 767 | * and decrement it on completion, waking up sb_wait |
| 768 | * if zero is reached. |
| 769 | * If an error occurred, call md_error |
| 770 | */ |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 771 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 772 | |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 773 | bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 774 | bio->bi_sector = sector; |
| 775 | bio_add_page(bio, page, size, 0); |
| 776 | bio->bi_private = rdev; |
| 777 | bio->bi_end_io = super_written; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 778 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 779 | atomic_inc(&mddev->pending_writes); |
Jens Axboe | 721a960 | 2011-03-09 11:56:30 +0100 | [diff] [blame^] | 780 | submit_bio(REQ_WRITE | REQ_SYNC | REQ_FLUSH | REQ_FUA, bio); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | void md_super_wait(mddev_t *mddev) |
| 784 | { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 785 | /* wait for all superblock writes that were scheduled to complete */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 786 | DEFINE_WAIT(wq); |
| 787 | for(;;) { |
| 788 | prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE); |
| 789 | if (atomic_read(&mddev->pending_writes)==0) |
| 790 | break; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 791 | schedule(); |
| 792 | } |
| 793 | finish_wait(&mddev->sb_wait, &wq); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 794 | } |
| 795 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 796 | static void bi_complete(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | complete((struct completion*)bio->bi_private); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | } |
| 800 | |
NeilBrown | 2b19336 | 2010-10-27 15:16:40 +1100 | [diff] [blame] | 801 | 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] | 802 | struct page *page, int rw, bool metadata_op) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 804 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | struct completion event; |
| 806 | int ret; |
| 807 | |
Jens Axboe | 721a960 | 2011-03-09 11:56:30 +0100 | [diff] [blame^] | 808 | rw |= REQ_SYNC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 810 | bio->bi_bdev = (metadata_op && rdev->meta_bdev) ? |
| 811 | rdev->meta_bdev : rdev->bdev; |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 812 | if (metadata_op) |
| 813 | bio->bi_sector = sector + rdev->sb_start; |
| 814 | else |
| 815 | bio->bi_sector = sector + rdev->data_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | bio_add_page(bio, page, size, 0); |
| 817 | init_completion(&event); |
| 818 | bio->bi_private = &event; |
| 819 | bio->bi_end_io = bi_complete; |
| 820 | submit_bio(rw, bio); |
| 821 | wait_for_completion(&event); |
| 822 | |
| 823 | ret = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 824 | bio_put(bio); |
| 825 | return ret; |
| 826 | } |
NeilBrown | a8745db | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 827 | EXPORT_SYMBOL_GPL(sync_page_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 829 | static int read_disk_sb(mdk_rdev_t * rdev, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | { |
| 831 | char b[BDEVNAME_SIZE]; |
| 832 | if (!rdev->sb_page) { |
| 833 | MD_BUG(); |
| 834 | return -EINVAL; |
| 835 | } |
| 836 | if (rdev->sb_loaded) |
| 837 | return 0; |
| 838 | |
| 839 | |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 840 | if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | goto fail; |
| 842 | rdev->sb_loaded = 1; |
| 843 | return 0; |
| 844 | |
| 845 | fail: |
| 846 | printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n", |
| 847 | bdevname(rdev->bdev,b)); |
| 848 | return -EINVAL; |
| 849 | } |
| 850 | |
| 851 | static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 852 | { |
Andre Noll | 0571046 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 853 | return sb1->set_uuid0 == sb2->set_uuid0 && |
| 854 | sb1->set_uuid1 == sb2->set_uuid1 && |
| 855 | sb1->set_uuid2 == sb2->set_uuid2 && |
| 856 | sb1->set_uuid3 == sb2->set_uuid3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | } |
| 858 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 860 | { |
| 861 | int ret; |
| 862 | mdp_super_t *tmp1, *tmp2; |
| 863 | |
| 864 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); |
| 865 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); |
| 866 | |
| 867 | if (!tmp1 || !tmp2) { |
| 868 | ret = 0; |
Andre Noll | 35020f1 | 2008-03-23 15:10:33 +0100 | [diff] [blame] | 869 | printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | goto abort; |
| 871 | } |
| 872 | |
| 873 | *tmp1 = *sb1; |
| 874 | *tmp2 = *sb2; |
| 875 | |
| 876 | /* |
| 877 | * nr_disks is not constant |
| 878 | */ |
| 879 | tmp1->nr_disks = 0; |
| 880 | tmp2->nr_disks = 0; |
| 881 | |
Andre Noll | ce0c8e0 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 882 | ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | abort: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 884 | kfree(tmp1); |
| 885 | kfree(tmp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | return ret; |
| 887 | } |
| 888 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 889 | |
| 890 | static u32 md_csum_fold(u32 csum) |
| 891 | { |
| 892 | csum = (csum & 0xffff) + (csum >> 16); |
| 893 | return (csum & 0xffff) + (csum >> 16); |
| 894 | } |
| 895 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | static unsigned int calc_sb_csum(mdp_super_t * sb) |
| 897 | { |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 898 | u64 newcsum = 0; |
| 899 | u32 *sb32 = (u32*)sb; |
| 900 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | unsigned int disk_csum, csum; |
| 902 | |
| 903 | disk_csum = sb->sb_csum; |
| 904 | sb->sb_csum = 0; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 905 | |
| 906 | for (i = 0; i < MD_SB_BYTES/4 ; i++) |
| 907 | newcsum += sb32[i]; |
| 908 | csum = (newcsum & 0xffffffff) + (newcsum>>32); |
| 909 | |
| 910 | |
| 911 | #ifdef CONFIG_ALPHA |
| 912 | /* This used to use csum_partial, which was wrong for several |
| 913 | * reasons including that different results are returned on |
| 914 | * different architectures. It isn't critical that we get exactly |
| 915 | * the same return value as before (we always csum_fold before |
| 916 | * testing, and that removes any differences). However as we |
| 917 | * know that csum_partial always returned a 16bit value on |
| 918 | * alphas, do a fold to maximise conformity to previous behaviour. |
| 919 | */ |
| 920 | sb->sb_csum = md_csum_fold(disk_csum); |
| 921 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | sb->sb_csum = disk_csum; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 923 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | return csum; |
| 925 | } |
| 926 | |
| 927 | |
| 928 | /* |
| 929 | * Handle superblock details. |
| 930 | * We want to be able to handle multiple superblock formats |
| 931 | * so we have a common interface to them all, and an array of |
| 932 | * different handlers. |
| 933 | * We rely on user-space to write the initial superblock, and support |
| 934 | * reading and updating of superblocks. |
| 935 | * Interface methods are: |
| 936 | * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version) |
| 937 | * loads and validates a superblock on dev. |
| 938 | * if refdev != NULL, compare superblocks on both devices |
| 939 | * Return: |
| 940 | * 0 - dev has a superblock that is compatible with refdev |
| 941 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 942 | * so dev should be used as the refdev in future |
| 943 | * -EINVAL superblock incompatible or invalid |
| 944 | * -othererror e.g. -EIO |
| 945 | * |
| 946 | * int validate_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 947 | * Verify that dev is acceptable into mddev. |
| 948 | * The first time, mddev->raid_disks will be 0, and data from |
| 949 | * dev should be merged in. Subsequent calls check that dev |
| 950 | * is new enough. Return 0 or -EINVAL |
| 951 | * |
| 952 | * void sync_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 953 | * Update the superblock for rdev with data in mddev |
| 954 | * This does not write to disc. |
| 955 | * |
| 956 | */ |
| 957 | |
| 958 | struct super_type { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 959 | char *name; |
| 960 | struct module *owner; |
| 961 | int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, |
| 962 | int minor_version); |
| 963 | int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 964 | void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 965 | unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev, |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 966 | sector_t num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | }; |
| 968 | |
| 969 | /* |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 970 | * Check that the given mddev has no bitmap. |
| 971 | * |
| 972 | * This function is called from the run method of all personalities that do not |
| 973 | * support bitmaps. It prints an error message and returns non-zero if mddev |
| 974 | * has a bitmap. Otherwise, it returns 0. |
| 975 | * |
| 976 | */ |
| 977 | int md_check_no_bitmap(mddev_t *mddev) |
| 978 | { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 979 | if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 980 | return 0; |
| 981 | printk(KERN_ERR "%s: bitmaps are not supported for %s\n", |
| 982 | mdname(mddev), mddev->pers->name); |
| 983 | return 1; |
| 984 | } |
| 985 | EXPORT_SYMBOL(md_check_no_bitmap); |
| 986 | |
| 987 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | * load_super for 0.90.0 |
| 989 | */ |
| 990 | static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 991 | { |
| 992 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 993 | mdp_super_t *sb; |
| 994 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | |
| 996 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 997 | * Calculate the position of the superblock (512byte sectors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | * it's at the end of the disk. |
| 999 | * |
| 1000 | * It also happens to be a multiple of 4Kb. |
| 1001 | */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1002 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1004 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | if (ret) return ret; |
| 1006 | |
| 1007 | ret = -EINVAL; |
| 1008 | |
| 1009 | bdevname(rdev->bdev, b); |
| 1010 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 1011 | |
| 1012 | if (sb->md_magic != MD_SB_MAGIC) { |
| 1013 | printk(KERN_ERR "md: invalid raid superblock magic on %s\n", |
| 1014 | b); |
| 1015 | goto abort; |
| 1016 | } |
| 1017 | |
| 1018 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1019 | sb->minor_version < 90 || |
| 1020 | sb->minor_version > 91) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | printk(KERN_WARNING "Bad version number %d.%d on %s\n", |
| 1022 | sb->major_version, sb->minor_version, |
| 1023 | b); |
| 1024 | goto abort; |
| 1025 | } |
| 1026 | |
| 1027 | if (sb->raid_disks <= 0) |
| 1028 | goto abort; |
| 1029 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 1030 | 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] | 1031 | printk(KERN_WARNING "md: invalid superblock checksum on %s\n", |
| 1032 | b); |
| 1033 | goto abort; |
| 1034 | } |
| 1035 | |
| 1036 | rdev->preferred_minor = sb->md_minor; |
| 1037 | rdev->data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1038 | rdev->sb_size = MD_SB_BYTES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | |
| 1040 | if (sb->level == LEVEL_MULTIPATH) |
| 1041 | rdev->desc_nr = -1; |
| 1042 | else |
| 1043 | rdev->desc_nr = sb->this_disk.number; |
| 1044 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1045 | if (!refdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1047 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | __u64 ev1, ev2; |
| 1049 | mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page); |
| 1050 | if (!uuid_equal(refsb, sb)) { |
| 1051 | printk(KERN_WARNING "md: %s has different UUID to %s\n", |
| 1052 | b, bdevname(refdev->bdev,b2)); |
| 1053 | goto abort; |
| 1054 | } |
| 1055 | if (!sb_equal(refsb, sb)) { |
| 1056 | printk(KERN_WARNING "md: %s has same UUID" |
| 1057 | " but different superblock to %s\n", |
| 1058 | b, bdevname(refdev->bdev, b2)); |
| 1059 | goto abort; |
| 1060 | } |
| 1061 | ev1 = md_event(sb); |
| 1062 | ev2 = md_event(refsb); |
| 1063 | if (ev1 > ev2) |
| 1064 | ret = 1; |
| 1065 | else |
| 1066 | ret = 0; |
| 1067 | } |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 1068 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1070 | if (rdev->sectors < sb->size * 2 && sb->level > 1) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1071 | /* "this cannot possibly happen" ... */ |
| 1072 | ret = -EINVAL; |
| 1073 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | abort: |
| 1075 | return ret; |
| 1076 | } |
| 1077 | |
| 1078 | /* |
| 1079 | * validate_super for 0.90.0 |
| 1080 | */ |
| 1081 | static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1082 | { |
| 1083 | mdp_disk_t *desc; |
| 1084 | mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1085 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1087 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1088 | clear_bit(Faulty, &rdev->flags); |
| 1089 | clear_bit(In_sync, &rdev->flags); |
| 1090 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1091 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1092 | if (mddev->raid_disks == 0) { |
| 1093 | mddev->major_version = 0; |
| 1094 | mddev->minor_version = sb->minor_version; |
| 1095 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1096 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1097 | mddev->chunk_sectors = sb->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1098 | mddev->ctime = sb->ctime; |
| 1099 | mddev->utime = sb->utime; |
| 1100 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1101 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | mddev->layout = sb->layout; |
| 1103 | mddev->raid_disks = sb->raid_disks; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1104 | mddev->dev_sectors = sb->size * 2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1105 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1106 | mddev->bitmap_info.offset = 0; |
| 1107 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1108 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1109 | if (mddev->minor_version >= 91) { |
| 1110 | mddev->reshape_position = sb->reshape_position; |
| 1111 | mddev->delta_disks = sb->delta_disks; |
| 1112 | mddev->new_level = sb->new_level; |
| 1113 | mddev->new_layout = sb->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1114 | mddev->new_chunk_sectors = sb->new_chunk >> 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1115 | } else { |
| 1116 | mddev->reshape_position = MaxSector; |
| 1117 | mddev->delta_disks = 0; |
| 1118 | mddev->new_level = mddev->level; |
| 1119 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1120 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1121 | } |
| 1122 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1123 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 1124 | mddev->recovery_cp = MaxSector; |
| 1125 | else { |
| 1126 | if (sb->events_hi == sb->cp_events_hi && |
| 1127 | sb->events_lo == sb->cp_events_lo) { |
| 1128 | mddev->recovery_cp = sb->recovery_cp; |
| 1129 | } else |
| 1130 | mddev->recovery_cp = 0; |
| 1131 | } |
| 1132 | |
| 1133 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 1134 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 1135 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 1136 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 1137 | |
| 1138 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1139 | |
| 1140 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1141 | mddev->bitmap_info.file == NULL) |
| 1142 | mddev->bitmap_info.offset = |
| 1143 | mddev->bitmap_info.default_offset; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1144 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1145 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1146 | /* Insist on good event counter while assembling, except |
| 1147 | * for spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1149 | if (sb->disks[rdev->desc_nr].state & ( |
| 1150 | (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE))) |
| 1151 | if (ev1 < mddev->events) |
| 1152 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1153 | } else if (mddev->bitmap) { |
| 1154 | /* if adding to array with a bitmap, then we can accept an |
| 1155 | * older device ... but not too old. |
| 1156 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1157 | if (ev1 < mddev->bitmap->events_cleared) |
| 1158 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1159 | } else { |
| 1160 | if (ev1 < mddev->events) |
| 1161 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1162 | return 0; |
| 1163 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1164 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1166 | desc = sb->disks + rdev->desc_nr; |
| 1167 | |
| 1168 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1169 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 1170 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 1171 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1172 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1173 | rdev->raid_disk = desc->raid_disk; |
NeilBrown | 0261cd9 | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1174 | } else if (desc->state & (1<<MD_DISK_ACTIVE)) { |
| 1175 | /* active but not in sync implies recovery up to |
| 1176 | * reshape position. We don't know exactly where |
| 1177 | * that is, so set to zero for now */ |
| 1178 | if (mddev->minor_version >= 91) { |
| 1179 | rdev->recovery_offset = 0; |
| 1180 | rdev->raid_disk = desc->raid_disk; |
| 1181 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1183 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 1184 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1185 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1186 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | return 0; |
| 1188 | } |
| 1189 | |
| 1190 | /* |
| 1191 | * sync_super for 0.90.0 |
| 1192 | */ |
| 1193 | static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1194 | { |
| 1195 | mdp_super_t *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | mdk_rdev_t *rdev2; |
| 1197 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1198 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1199 | |
| 1200 | /* make rdev->sb match mddev data.. |
| 1201 | * |
| 1202 | * 1/ zero out disks |
| 1203 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 1204 | * 3/ any empty disks < next_spare become removed |
| 1205 | * |
| 1206 | * disks[0] gets initialised to REMOVED because |
| 1207 | * we cannot be sure from other fields if it has |
| 1208 | * been initialised or not. |
| 1209 | */ |
| 1210 | int i; |
| 1211 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 1212 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 1213 | rdev->sb_size = MD_SB_BYTES; |
| 1214 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1215 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 1216 | |
| 1217 | memset(sb, 0, sizeof(*sb)); |
| 1218 | |
| 1219 | sb->md_magic = MD_SB_MAGIC; |
| 1220 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | sb->patch_version = mddev->patch_version; |
| 1222 | sb->gvalid_words = 0; /* ignored */ |
| 1223 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 1224 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 1225 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 1226 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 1227 | |
| 1228 | sb->ctime = mddev->ctime; |
| 1229 | sb->level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1230 | sb->size = mddev->dev_sectors / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | sb->raid_disks = mddev->raid_disks; |
| 1232 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1233 | sb->not_persistent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1234 | sb->utime = mddev->utime; |
| 1235 | sb->state = 0; |
| 1236 | sb->events_hi = (mddev->events>>32); |
| 1237 | sb->events_lo = (u32)mddev->events; |
| 1238 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1239 | if (mddev->reshape_position == MaxSector) |
| 1240 | sb->minor_version = 90; |
| 1241 | else { |
| 1242 | sb->minor_version = 91; |
| 1243 | sb->reshape_position = mddev->reshape_position; |
| 1244 | sb->new_level = mddev->new_level; |
| 1245 | sb->delta_disks = mddev->delta_disks; |
| 1246 | sb->new_layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1247 | sb->new_chunk = mddev->new_chunk_sectors << 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1248 | } |
| 1249 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | if (mddev->in_sync) |
| 1251 | { |
| 1252 | sb->recovery_cp = mddev->recovery_cp; |
| 1253 | sb->cp_events_hi = (mddev->events>>32); |
| 1254 | sb->cp_events_lo = (u32)mddev->events; |
| 1255 | if (mddev->recovery_cp == MaxSector) |
| 1256 | sb->state = (1<< MD_SB_CLEAN); |
| 1257 | } else |
| 1258 | sb->recovery_cp = 0; |
| 1259 | |
| 1260 | sb->layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1261 | sb->chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1263 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1264 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 1265 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1266 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1267 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1269 | int desc_nr; |
NeilBrown | 0261cd9 | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1270 | int is_active = test_bit(In_sync, &rdev2->flags); |
| 1271 | |
| 1272 | if (rdev2->raid_disk >= 0 && |
| 1273 | sb->minor_version >= 91) |
| 1274 | /* we have nowhere to store the recovery_offset, |
| 1275 | * but if it is not below the reshape_position, |
| 1276 | * we can piggy-back on that. |
| 1277 | */ |
| 1278 | is_active = 1; |
| 1279 | if (rdev2->raid_disk < 0 || |
| 1280 | test_bit(Faulty, &rdev2->flags)) |
| 1281 | is_active = 0; |
| 1282 | if (is_active) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1283 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1284 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1285 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1286 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | d = &sb->disks[rdev2->desc_nr]; |
| 1288 | nr_disks++; |
| 1289 | d->number = rdev2->desc_nr; |
| 1290 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 1291 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | 0261cd9 | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1292 | if (is_active) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1293 | d->raid_disk = rdev2->raid_disk; |
| 1294 | else |
| 1295 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 1296 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1297 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 0261cd9 | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1298 | else if (is_active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1299 | d->state = (1<<MD_DISK_ACTIVE); |
NeilBrown | 0261cd9 | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1300 | if (test_bit(In_sync, &rdev2->flags)) |
| 1301 | d->state |= (1<<MD_DISK_SYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | active++; |
| 1303 | working++; |
| 1304 | } else { |
| 1305 | d->state = 0; |
| 1306 | spare++; |
| 1307 | working++; |
| 1308 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1309 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 1310 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1311 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1312 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 1313 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 1314 | mdp_disk_t *d = &sb->disks[i]; |
| 1315 | if (d->state == 0 && d->number == 0) { |
| 1316 | d->number = i; |
| 1317 | d->raid_disk = i; |
| 1318 | d->state = (1<<MD_DISK_REMOVED); |
| 1319 | d->state |= (1<<MD_DISK_FAULTY); |
| 1320 | failed++; |
| 1321 | } |
| 1322 | } |
| 1323 | sb->nr_disks = nr_disks; |
| 1324 | sb->active_disks = active; |
| 1325 | sb->working_disks = working; |
| 1326 | sb->failed_disks = failed; |
| 1327 | sb->spare_disks = spare; |
| 1328 | |
| 1329 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 1330 | sb->sb_csum = calc_sb_csum(sb); |
| 1331 | } |
| 1332 | |
| 1333 | /* |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1334 | * rdev_size_change for 0.90.0 |
| 1335 | */ |
| 1336 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1337 | 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] | 1338 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1339 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1340 | return 0; /* component must fit device */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1341 | if (rdev->mddev->bitmap_info.offset) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1342 | return 0; /* can't move bitmap */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1343 | rdev->sb_start = calc_dev_sboffset(rdev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1344 | if (!num_sectors || num_sectors > rdev->sb_start) |
| 1345 | num_sectors = rdev->sb_start; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1346 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1347 | rdev->sb_page); |
| 1348 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1349 | return num_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1350 | } |
| 1351 | |
| 1352 | |
| 1353 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1354 | * version 1 superblock |
| 1355 | */ |
| 1356 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1357 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1358 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1359 | __le32 disk_csum; |
| 1360 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1361 | unsigned long long newcsum; |
| 1362 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1363 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1364 | int i; |
| 1365 | |
| 1366 | disk_csum = sb->sb_csum; |
| 1367 | sb->sb_csum = 0; |
| 1368 | newcsum = 0; |
| 1369 | for (i=0; size>=4; size -= 4 ) |
| 1370 | newcsum += le32_to_cpu(*isuper++); |
| 1371 | |
| 1372 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1373 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1374 | |
| 1375 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1376 | sb->sb_csum = disk_csum; |
| 1377 | return cpu_to_le32(csum); |
| 1378 | } |
| 1379 | |
| 1380 | static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 1381 | { |
| 1382 | struct mdp_superblock_1 *sb; |
| 1383 | int ret; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1384 | sector_t sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1385 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1386 | int bmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1387 | |
| 1388 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1389 | * Calculate the position of the superblock in 512byte sectors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1390 | * It is always aligned to a 4K boundary and |
| 1391 | * depeding on minor_version, it can be: |
| 1392 | * 0: At least 8K, but less than 12K, from end of device |
| 1393 | * 1: At start of device |
| 1394 | * 2: 4K from start of device. |
| 1395 | */ |
| 1396 | switch(minor_version) { |
| 1397 | case 0: |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1398 | sb_start = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1399 | sb_start -= 8*2; |
| 1400 | sb_start &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | break; |
| 1402 | case 1: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1403 | sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1404 | break; |
| 1405 | case 2: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1406 | sb_start = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1407 | break; |
| 1408 | default: |
| 1409 | return -EINVAL; |
| 1410 | } |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1411 | rdev->sb_start = sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1412 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1413 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1414 | * and it is safe to read 4k, so we do that |
| 1415 | */ |
| 1416 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1417 | if (ret) return ret; |
| 1418 | |
| 1419 | |
| 1420 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1421 | |
| 1422 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1423 | sb->major_version != cpu_to_le32(1) || |
| 1424 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1425 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1426 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1427 | return -EINVAL; |
| 1428 | |
| 1429 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
| 1430 | printk("md: invalid superblock checksum on %s\n", |
| 1431 | bdevname(rdev->bdev,b)); |
| 1432 | return -EINVAL; |
| 1433 | } |
| 1434 | if (le64_to_cpu(sb->data_size) < 10) { |
| 1435 | printk("md: data_size too small on %s\n", |
| 1436 | bdevname(rdev->bdev,b)); |
| 1437 | return -EINVAL; |
| 1438 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1439 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1440 | rdev->preferred_minor = 0xffff; |
| 1441 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1442 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1443 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1444 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1445 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1446 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1447 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1448 | |
| 1449 | if (minor_version |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1450 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1451 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1452 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1453 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1454 | rdev->desc_nr = -1; |
| 1455 | else |
| 1456 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1457 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1458 | if (!refdev) { |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1459 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1460 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1461 | __u64 ev1, ev2; |
| 1462 | struct mdp_superblock_1 *refsb = |
| 1463 | (struct mdp_superblock_1*)page_address(refdev->sb_page); |
| 1464 | |
| 1465 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1466 | sb->level != refsb->level || |
| 1467 | sb->layout != refsb->layout || |
| 1468 | sb->chunksize != refsb->chunksize) { |
| 1469 | printk(KERN_WARNING "md: %s has strangely different" |
| 1470 | " superblock to %s\n", |
| 1471 | bdevname(rdev->bdev,b), |
| 1472 | bdevname(refdev->bdev,b2)); |
| 1473 | return -EINVAL; |
| 1474 | } |
| 1475 | ev1 = le64_to_cpu(sb->events); |
| 1476 | ev2 = le64_to_cpu(refsb->events); |
| 1477 | |
| 1478 | if (ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1479 | ret = 1; |
| 1480 | else |
| 1481 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1482 | } |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1483 | if (minor_version) |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1484 | rdev->sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) - |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1485 | le64_to_cpu(sb->data_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1486 | else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1487 | rdev->sectors = rdev->sb_start; |
| 1488 | if (rdev->sectors < le64_to_cpu(sb->data_size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1489 | return -EINVAL; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1490 | rdev->sectors = le64_to_cpu(sb->data_size); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1491 | if (le64_to_cpu(sb->size) > rdev->sectors) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1492 | return -EINVAL; |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1493 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1494 | } |
| 1495 | |
| 1496 | static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1497 | { |
| 1498 | 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] | 1499 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1500 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1501 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1502 | clear_bit(Faulty, &rdev->flags); |
| 1503 | clear_bit(In_sync, &rdev->flags); |
| 1504 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1505 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1506 | if (mddev->raid_disks == 0) { |
| 1507 | mddev->major_version = 1; |
| 1508 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1509 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1510 | mddev->chunk_sectors = le32_to_cpu(sb->chunksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1511 | mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1); |
| 1512 | mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1); |
| 1513 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1514 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1515 | mddev->layout = le32_to_cpu(sb->layout); |
| 1516 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1517 | mddev->dev_sectors = le64_to_cpu(sb->size); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1518 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1519 | mddev->bitmap_info.offset = 0; |
| 1520 | mddev->bitmap_info.default_offset = 1024 >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1521 | |
| 1522 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1523 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1524 | |
| 1525 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1526 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1527 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1528 | mddev->bitmap_info.file == NULL ) |
| 1529 | mddev->bitmap_info.offset = |
| 1530 | (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1531 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1532 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1533 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1534 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1535 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1536 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1537 | mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1538 | } else { |
| 1539 | mddev->reshape_position = MaxSector; |
| 1540 | mddev->delta_disks = 0; |
| 1541 | mddev->new_level = mddev->level; |
| 1542 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1543 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1544 | } |
| 1545 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1546 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1547 | /* Insist of good event counter while assembling, except for |
| 1548 | * spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1549 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1550 | if (rdev->desc_nr >= 0 && |
| 1551 | rdev->desc_nr < le32_to_cpu(sb->max_dev) && |
| 1552 | le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe) |
| 1553 | if (ev1 < mddev->events) |
| 1554 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1555 | } else if (mddev->bitmap) { |
| 1556 | /* If adding to array with a bitmap, then we can accept an |
| 1557 | * older device, but not too old. |
| 1558 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1559 | if (ev1 < mddev->bitmap->events_cleared) |
| 1560 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1561 | } else { |
| 1562 | if (ev1 < mddev->events) |
| 1563 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1564 | return 0; |
| 1565 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1566 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1567 | int role; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1568 | if (rdev->desc_nr < 0 || |
| 1569 | rdev->desc_nr >= le32_to_cpu(sb->max_dev)) { |
| 1570 | role = 0xffff; |
| 1571 | rdev->desc_nr = -1; |
| 1572 | } else |
| 1573 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1574 | switch(role) { |
| 1575 | case 0xffff: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1576 | break; |
| 1577 | case 0xfffe: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1578 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1579 | break; |
| 1580 | default: |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1581 | if ((le32_to_cpu(sb->feature_map) & |
| 1582 | MD_FEATURE_RECOVERY_OFFSET)) |
| 1583 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 1584 | else |
| 1585 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1586 | rdev->raid_disk = role; |
| 1587 | break; |
| 1588 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1589 | if (sb->devflags & WriteMostly1) |
| 1590 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1591 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1592 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1593 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1594 | return 0; |
| 1595 | } |
| 1596 | |
| 1597 | static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1598 | { |
| 1599 | struct mdp_superblock_1 *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1600 | mdk_rdev_t *rdev2; |
| 1601 | int max_dev, i; |
| 1602 | /* make rdev->sb match mddev and rdev data. */ |
| 1603 | |
| 1604 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1605 | |
| 1606 | sb->feature_map = 0; |
| 1607 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1608 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1609 | memset(sb->pad1, 0, sizeof(sb->pad1)); |
| 1610 | memset(sb->pad2, 0, sizeof(sb->pad2)); |
| 1611 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1612 | |
| 1613 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1614 | sb->events = cpu_to_le64(mddev->events); |
| 1615 | if (mddev->in_sync) |
| 1616 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
| 1617 | else |
| 1618 | sb->resync_offset = cpu_to_le64(0); |
| 1619 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1620 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1621 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1622 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1623 | sb->size = cpu_to_le64(mddev->dev_sectors); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1624 | sb->chunksize = cpu_to_le32(mddev->chunk_sectors); |
NeilBrown | 62e1e38 | 2009-05-26 09:40:59 +1000 | [diff] [blame] | 1625 | sb->level = cpu_to_le32(mddev->level); |
| 1626 | sb->layout = cpu_to_le32(mddev->layout); |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1627 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1628 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) { |
| 1629 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1630 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1631 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1632 | |
| 1633 | if (rdev->raid_disk >= 0 && |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1634 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1635 | sb->feature_map |= |
| 1636 | cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 1637 | sb->recovery_offset = |
| 1638 | cpu_to_le64(rdev->recovery_offset); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1639 | } |
| 1640 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1641 | if (mddev->reshape_position != MaxSector) { |
| 1642 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 1643 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1644 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1645 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1646 | sb->new_level = cpu_to_le32(mddev->new_level); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1647 | sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1648 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1649 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1650 | max_dev = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1651 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1652 | if (rdev2->desc_nr+1 > max_dev) |
| 1653 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1654 | |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1655 | if (max_dev > le32_to_cpu(sb->max_dev)) { |
| 1656 | int bmask; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1657 | sb->max_dev = cpu_to_le32(max_dev); |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1658 | rdev->sb_size = max_dev * 2 + 256; |
| 1659 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
| 1660 | if (rdev->sb_size & bmask) |
| 1661 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
NeilBrown | ddcf352 | 2010-09-08 16:48:17 +1000 | [diff] [blame] | 1662 | } else |
| 1663 | max_dev = le32_to_cpu(sb->max_dev); |
| 1664 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1665 | for (i=0; i<max_dev;i++) |
| 1666 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
| 1667 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1668 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1669 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1670 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1671 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1672 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1673 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1674 | else if (rdev2->raid_disk >= 0) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1675 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1676 | else |
| 1677 | sb->dev_roles[i] = cpu_to_le16(0xffff); |
| 1678 | } |
| 1679 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1680 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1681 | } |
| 1682 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1683 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1684 | 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] | 1685 | { |
| 1686 | struct mdp_superblock_1 *sb; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1687 | sector_t max_sectors; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1688 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1689 | return 0; /* component must fit device */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1690 | if (rdev->sb_start < rdev->data_offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1691 | /* minor versions 1 and 2; superblock before data */ |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1692 | max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1693 | max_sectors -= rdev->data_offset; |
| 1694 | if (!num_sectors || num_sectors > max_sectors) |
| 1695 | num_sectors = max_sectors; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1696 | } else if (rdev->mddev->bitmap_info.offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1697 | /* minor version 0 with bitmap we can't move */ |
| 1698 | return 0; |
| 1699 | } else { |
| 1700 | /* minor version 0; superblock after data */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1701 | sector_t sb_start; |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1702 | sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1703 | sb_start &= ~(sector_t)(4*2 - 1); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1704 | max_sectors = rdev->sectors + sb_start - rdev->sb_start; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1705 | if (!num_sectors || num_sectors > max_sectors) |
| 1706 | num_sectors = max_sectors; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1707 | rdev->sb_start = sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1708 | } |
| 1709 | sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1710 | sb->data_size = cpu_to_le64(num_sectors); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1711 | sb->super_offset = rdev->sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1712 | sb->sb_csum = calc_sb_1_csum(sb); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1713 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1714 | rdev->sb_page); |
| 1715 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1716 | return num_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1717 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1718 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 1719 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1720 | [0] = { |
| 1721 | .name = "0.90.0", |
| 1722 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1723 | .load_super = super_90_load, |
| 1724 | .validate_super = super_90_validate, |
| 1725 | .sync_super = super_90_sync, |
| 1726 | .rdev_size_change = super_90_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1727 | }, |
| 1728 | [1] = { |
| 1729 | .name = "md-1", |
| 1730 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1731 | .load_super = super_1_load, |
| 1732 | .validate_super = super_1_validate, |
| 1733 | .sync_super = super_1_sync, |
| 1734 | .rdev_size_change = super_1_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1735 | }, |
| 1736 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1737 | |
| 1738 | static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2) |
| 1739 | { |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1740 | mdk_rdev_t *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1741 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1742 | rcu_read_lock(); |
| 1743 | rdev_for_each_rcu(rdev, mddev1) |
| 1744 | rdev_for_each_rcu(rdev2, mddev2) |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1745 | if (rdev->bdev->bd_contains == |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1746 | rdev2->bdev->bd_contains) { |
| 1747 | rcu_read_unlock(); |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1748 | return 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1749 | } |
| 1750 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1751 | return 0; |
| 1752 | } |
| 1753 | |
| 1754 | static LIST_HEAD(pending_raid_disks); |
| 1755 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1756 | /* |
| 1757 | * Try to register data integrity profile for an mddev |
| 1758 | * |
| 1759 | * This is called when an array is started and after a disk has been kicked |
| 1760 | * from the array. It only succeeds if all working and active component devices |
| 1761 | * are integrity capable with matching profiles. |
| 1762 | */ |
| 1763 | int md_integrity_register(mddev_t *mddev) |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1764 | { |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1765 | mdk_rdev_t *rdev, *reference = NULL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1766 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1767 | if (list_empty(&mddev->disks)) |
| 1768 | return 0; /* nothing to do */ |
| 1769 | if (blk_get_integrity(mddev->gendisk)) |
| 1770 | return 0; /* already registered */ |
| 1771 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 1772 | /* skip spares and non-functional disks */ |
| 1773 | if (test_bit(Faulty, &rdev->flags)) |
| 1774 | continue; |
| 1775 | if (rdev->raid_disk < 0) |
| 1776 | continue; |
| 1777 | /* |
| 1778 | * If at least one rdev is not integrity capable, we can not |
| 1779 | * enable data integrity for the md device. |
| 1780 | */ |
| 1781 | if (!bdev_get_integrity(rdev->bdev)) |
| 1782 | return -EINVAL; |
| 1783 | if (!reference) { |
| 1784 | /* Use the first rdev as the reference */ |
| 1785 | reference = rdev; |
| 1786 | continue; |
| 1787 | } |
| 1788 | /* does this rdev's profile match the reference profile? */ |
| 1789 | if (blk_integrity_compare(reference->bdev->bd_disk, |
| 1790 | rdev->bdev->bd_disk) < 0) |
| 1791 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1792 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1793 | /* |
| 1794 | * All component devices are integrity capable and have matching |
| 1795 | * profiles, register the common profile for the md device. |
| 1796 | */ |
| 1797 | if (blk_integrity_register(mddev->gendisk, |
| 1798 | bdev_get_integrity(reference->bdev)) != 0) { |
| 1799 | printk(KERN_ERR "md: failed to register integrity for %s\n", |
| 1800 | mdname(mddev)); |
| 1801 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1802 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1803 | printk(KERN_NOTICE "md: data integrity on %s enabled\n", |
| 1804 | mdname(mddev)); |
| 1805 | return 0; |
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 | EXPORT_SYMBOL(md_integrity_register); |
| 1808 | |
| 1809 | /* Disable data integrity if non-capable/non-matching disk is being added */ |
| 1810 | void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 1811 | { |
| 1812 | struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev); |
| 1813 | struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk); |
| 1814 | |
| 1815 | if (!bi_mddev) /* nothing to do */ |
| 1816 | return; |
| 1817 | if (rdev->raid_disk < 0) /* skip spares */ |
| 1818 | return; |
| 1819 | if (bi_rdev && blk_integrity_compare(mddev->gendisk, |
| 1820 | rdev->bdev->bd_disk) >= 0) |
| 1821 | return; |
| 1822 | printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev)); |
| 1823 | blk_integrity_unregister(mddev->gendisk); |
| 1824 | } |
| 1825 | EXPORT_SYMBOL(md_integrity_add_rdev); |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1826 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1827 | static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev) |
| 1828 | { |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1829 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 1830 | struct kobject *ko; |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1831 | char *s; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1832 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1833 | |
| 1834 | if (rdev->mddev) { |
| 1835 | MD_BUG(); |
| 1836 | return -EINVAL; |
| 1837 | } |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1838 | |
| 1839 | /* prevent duplicates */ |
| 1840 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 1841 | return -EEXIST; |
| 1842 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1843 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
| 1844 | if (rdev->sectors && (mddev->dev_sectors == 0 || |
| 1845 | rdev->sectors < mddev->dev_sectors)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1846 | if (mddev->pers) { |
| 1847 | /* Cannot change size, so fail |
| 1848 | * If mddev->level <= 0, then we don't care |
| 1849 | * about aligning sizes (e.g. linear) |
| 1850 | */ |
| 1851 | if (mddev->level > 0) |
| 1852 | return -ENOSPC; |
| 1853 | } else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1854 | mddev->dev_sectors = rdev->sectors; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1855 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1856 | |
| 1857 | /* Verify rdev->desc_nr is unique. |
| 1858 | * If it is -1, assign a free number, else |
| 1859 | * check number is not in use |
| 1860 | */ |
| 1861 | if (rdev->desc_nr < 0) { |
| 1862 | int choice = 0; |
| 1863 | if (mddev->pers) choice = mddev->raid_disks; |
| 1864 | while (find_rdev_nr(mddev, choice)) |
| 1865 | choice++; |
| 1866 | rdev->desc_nr = choice; |
| 1867 | } else { |
| 1868 | if (find_rdev_nr(mddev, rdev->desc_nr)) |
| 1869 | return -EBUSY; |
| 1870 | } |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 1871 | if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
| 1872 | printk(KERN_WARNING "md: %s: array is limited to %d devices\n", |
| 1873 | mdname(mddev), mddev->max_disks); |
| 1874 | return -EBUSY; |
| 1875 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1876 | bdevname(rdev->bdev,b); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1877 | while ( (s=strchr(b, '/')) != NULL) |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1878 | *s = '!'; |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1879 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1880 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1881 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1882 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1883 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1884 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1885 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1886 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 1887 | if (sysfs_create_link(&rdev->kobj, ko, "block")) |
| 1888 | /* failure here is OK */; |
| 1889 | rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 1890 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1891 | list_add_rcu(&rdev->same_set, &mddev->disks); |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1892 | bd_link_disk_holder(rdev->bdev, mddev->gendisk); |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 1893 | |
| 1894 | /* May as well allow recovery to be retried once */ |
| 1895 | mddev->recovery_disabled = 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1896 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1897 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1898 | |
| 1899 | fail: |
| 1900 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", |
| 1901 | b, mdname(mddev)); |
| 1902 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1903 | } |
| 1904 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1905 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1906 | { |
| 1907 | mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work); |
| 1908 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1909 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1910 | } |
| 1911 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1912 | static void unbind_rdev_from_array(mdk_rdev_t * rdev) |
| 1913 | { |
| 1914 | char b[BDEVNAME_SIZE]; |
| 1915 | if (!rdev->mddev) { |
| 1916 | MD_BUG(); |
| 1917 | return; |
| 1918 | } |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 1919 | bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1920 | list_del_rcu(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1921 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 1922 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1923 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 1924 | sysfs_put(rdev->sysfs_state); |
| 1925 | rdev->sysfs_state = NULL; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1926 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1927 | * writing to 'remove' to "dev/state". We also need |
| 1928 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1929 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1930 | synchronize_rcu(); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1931 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 1932 | kobject_get(&rdev->kobj); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 1933 | queue_work(md_misc_wq, &rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1934 | } |
| 1935 | |
| 1936 | /* |
| 1937 | * prevent the device from being mounted, repartitioned or |
| 1938 | * otherwise reused by a RAID array (or any other kernel |
| 1939 | * subsystem), by bd_claiming the device. |
| 1940 | */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1941 | 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] | 1942 | { |
| 1943 | int err = 0; |
| 1944 | struct block_device *bdev; |
| 1945 | char b[BDEVNAME_SIZE]; |
| 1946 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1947 | bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, |
| 1948 | shared ? (mdk_rdev_t *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1949 | if (IS_ERR(bdev)) { |
| 1950 | printk(KERN_ERR "md: could not open %s.\n", |
| 1951 | __bdevname(dev, b)); |
| 1952 | return PTR_ERR(bdev); |
| 1953 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1954 | rdev->bdev = bdev; |
| 1955 | return err; |
| 1956 | } |
| 1957 | |
| 1958 | static void unlock_rdev(mdk_rdev_t *rdev) |
| 1959 | { |
| 1960 | struct block_device *bdev = rdev->bdev; |
| 1961 | rdev->bdev = NULL; |
| 1962 | if (!bdev) |
| 1963 | MD_BUG(); |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1964 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1965 | } |
| 1966 | |
| 1967 | void md_autodetect_dev(dev_t dev); |
| 1968 | |
| 1969 | static void export_rdev(mdk_rdev_t * rdev) |
| 1970 | { |
| 1971 | char b[BDEVNAME_SIZE]; |
| 1972 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 1973 | bdevname(rdev->bdev,b)); |
| 1974 | if (rdev->mddev) |
| 1975 | MD_BUG(); |
| 1976 | free_disk_sb(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1977 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1978 | if (test_bit(AutoDetected, &rdev->flags)) |
| 1979 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1980 | #endif |
| 1981 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1982 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1983 | } |
| 1984 | |
| 1985 | static void kick_rdev_from_array(mdk_rdev_t * rdev) |
| 1986 | { |
| 1987 | unbind_rdev_from_array(rdev); |
| 1988 | export_rdev(rdev); |
| 1989 | } |
| 1990 | |
| 1991 | static void export_array(mddev_t *mddev) |
| 1992 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1993 | mdk_rdev_t *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1994 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1995 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1996 | if (!rdev->mddev) { |
| 1997 | MD_BUG(); |
| 1998 | continue; |
| 1999 | } |
| 2000 | kick_rdev_from_array(rdev); |
| 2001 | } |
| 2002 | if (!list_empty(&mddev->disks)) |
| 2003 | MD_BUG(); |
| 2004 | mddev->raid_disks = 0; |
| 2005 | mddev->major_version = 0; |
| 2006 | } |
| 2007 | |
| 2008 | static void print_desc(mdp_disk_t *desc) |
| 2009 | { |
| 2010 | printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number, |
| 2011 | desc->major,desc->minor,desc->raid_disk,desc->state); |
| 2012 | } |
| 2013 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2014 | static void print_sb_90(mdp_super_t *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2015 | { |
| 2016 | int i; |
| 2017 | |
| 2018 | printk(KERN_INFO |
| 2019 | "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n", |
| 2020 | sb->major_version, sb->minor_version, sb->patch_version, |
| 2021 | sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3, |
| 2022 | sb->ctime); |
| 2023 | printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n", |
| 2024 | sb->level, sb->size, sb->nr_disks, sb->raid_disks, |
| 2025 | sb->md_minor, sb->layout, sb->chunk_size); |
| 2026 | printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d" |
| 2027 | " FD:%d SD:%d CSUM:%08x E:%08lx\n", |
| 2028 | sb->utime, sb->state, sb->active_disks, sb->working_disks, |
| 2029 | sb->failed_disks, sb->spare_disks, |
| 2030 | sb->sb_csum, (unsigned long)sb->events_lo); |
| 2031 | |
| 2032 | printk(KERN_INFO); |
| 2033 | for (i = 0; i < MD_SB_DISKS; i++) { |
| 2034 | mdp_disk_t *desc; |
| 2035 | |
| 2036 | desc = sb->disks + i; |
| 2037 | if (desc->number || desc->major || desc->minor || |
| 2038 | desc->raid_disk || (desc->state && (desc->state != 4))) { |
| 2039 | printk(" D %2d: ", i); |
| 2040 | print_desc(desc); |
| 2041 | } |
| 2042 | } |
| 2043 | printk(KERN_INFO "md: THIS: "); |
| 2044 | print_desc(&sb->this_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2045 | } |
| 2046 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2047 | static void print_sb_1(struct mdp_superblock_1 *sb) |
| 2048 | { |
| 2049 | __u8 *uuid; |
| 2050 | |
| 2051 | uuid = sb->set_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2052 | printk(KERN_INFO |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2053 | "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2054 | "md: Name: \"%s\" CT:%llu\n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2055 | le32_to_cpu(sb->major_version), |
| 2056 | le32_to_cpu(sb->feature_map), |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2057 | uuid, |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2058 | sb->set_name, |
| 2059 | (unsigned long long)le64_to_cpu(sb->ctime) |
| 2060 | & MD_SUPERBLOCK_1_TIME_SEC_MASK); |
| 2061 | |
| 2062 | uuid = sb->device_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2063 | printk(KERN_INFO |
| 2064 | "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] | 2065 | " RO:%llu\n" |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2066 | "md: Dev:%08x UUID: %pU\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2067 | "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n" |
| 2068 | "md: (MaxDev:%u) \n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2069 | le32_to_cpu(sb->level), |
| 2070 | (unsigned long long)le64_to_cpu(sb->size), |
| 2071 | le32_to_cpu(sb->raid_disks), |
| 2072 | le32_to_cpu(sb->layout), |
| 2073 | le32_to_cpu(sb->chunksize), |
| 2074 | (unsigned long long)le64_to_cpu(sb->data_offset), |
| 2075 | (unsigned long long)le64_to_cpu(sb->data_size), |
| 2076 | (unsigned long long)le64_to_cpu(sb->super_offset), |
| 2077 | (unsigned long long)le64_to_cpu(sb->recovery_offset), |
| 2078 | le32_to_cpu(sb->dev_number), |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2079 | uuid, |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2080 | sb->devflags, |
| 2081 | (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK, |
| 2082 | (unsigned long long)le64_to_cpu(sb->events), |
| 2083 | (unsigned long long)le64_to_cpu(sb->resync_offset), |
| 2084 | le32_to_cpu(sb->sb_csum), |
| 2085 | le32_to_cpu(sb->max_dev) |
| 2086 | ); |
| 2087 | } |
| 2088 | |
| 2089 | static void print_rdev(mdk_rdev_t *rdev, int major_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2090 | { |
| 2091 | char b[BDEVNAME_SIZE]; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2092 | printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n", |
| 2093 | bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors, |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2094 | test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags), |
| 2095 | rdev->desc_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2096 | if (rdev->sb_loaded) { |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2097 | printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version); |
| 2098 | switch (major_version) { |
| 2099 | case 0: |
| 2100 | print_sb_90((mdp_super_t*)page_address(rdev->sb_page)); |
| 2101 | break; |
| 2102 | case 1: |
| 2103 | print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page)); |
| 2104 | break; |
| 2105 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2106 | } else |
| 2107 | printk(KERN_INFO "md: no rdev superblock!\n"); |
| 2108 | } |
| 2109 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 2110 | static void md_print_devices(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2111 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2112 | struct list_head *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2113 | mdk_rdev_t *rdev; |
| 2114 | mddev_t *mddev; |
| 2115 | char b[BDEVNAME_SIZE]; |
| 2116 | |
| 2117 | printk("\n"); |
| 2118 | printk("md: **********************************\n"); |
| 2119 | printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n"); |
| 2120 | printk("md: **********************************\n"); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2121 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2122 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 2123 | if (mddev->bitmap) |
| 2124 | bitmap_print_sb(mddev->bitmap); |
| 2125 | else |
| 2126 | printk("%s: ", mdname(mddev)); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2127 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2128 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 2129 | printk("\n"); |
| 2130 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2131 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2132 | print_rdev(rdev, mddev->major_version); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2133 | } |
| 2134 | printk("md: **********************************\n"); |
| 2135 | printk("\n"); |
| 2136 | } |
| 2137 | |
| 2138 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2139 | static void sync_sbs(mddev_t * mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2140 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2141 | /* Update each superblock (in-memory image), but |
| 2142 | * if we are allowed to, skip spares which already |
| 2143 | * have the right event counter, or have one earlier |
| 2144 | * (which would mean they aren't being marked as dirty |
| 2145 | * with the rest of the array) |
| 2146 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2147 | mdk_rdev_t *rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2148 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2149 | if (rdev->sb_events == mddev->events || |
| 2150 | (nospares && |
| 2151 | rdev->raid_disk < 0 && |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2152 | rdev->sb_events+1 == mddev->events)) { |
| 2153 | /* Don't update this superblock */ |
| 2154 | rdev->sb_loaded = 2; |
| 2155 | } else { |
| 2156 | super_types[mddev->major_version]. |
| 2157 | sync_super(mddev, rdev); |
| 2158 | rdev->sb_loaded = 1; |
| 2159 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2160 | } |
| 2161 | } |
| 2162 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2163 | static void md_update_sb(mddev_t * mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2164 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2165 | mdk_rdev_t *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2166 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2167 | int nospares = 0; |
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 | repeat: |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2170 | /* First make sure individual recovery_offsets are correct */ |
| 2171 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 2172 | if (rdev->raid_disk >= 0 && |
| 2173 | mddev->delta_disks >= 0 && |
| 2174 | !test_bit(In_sync, &rdev->flags) && |
| 2175 | mddev->curr_resync_completed > rdev->recovery_offset) |
| 2176 | rdev->recovery_offset = mddev->curr_resync_completed; |
| 2177 | |
| 2178 | } |
Dan Williams | bd52b74 | 2010-08-30 17:33:33 +1000 | [diff] [blame] | 2179 | if (!mddev->persistent) { |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 2180 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2181 | clear_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | d97a41d | 2010-10-28 17:30:20 +1100 | [diff] [blame] | 2182 | if (!mddev->external) |
| 2183 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2184 | wake_up(&mddev->sb_wait); |
| 2185 | return; |
| 2186 | } |
| 2187 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2188 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2189 | |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2190 | mddev->utime = get_seconds(); |
| 2191 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2192 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 2193 | force_change = 1; |
| 2194 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 2195 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 2196 | * though if events isn't the right even/odd, we will have to do |
| 2197 | * spares after all |
| 2198 | */ |
| 2199 | nospares = 1; |
| 2200 | if (force_change) |
| 2201 | nospares = 0; |
| 2202 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2203 | /* If the array is degraded, then skipping spares is both |
| 2204 | * dangerous and fairly pointless. |
| 2205 | * Dangerous because a device that was removed from the array |
| 2206 | * might have a event_count that still looks up-to-date, |
| 2207 | * so it can be re-added without a resync. |
| 2208 | * Pointless because if there are any spares to skip, |
| 2209 | * then a recovery will happen and soon that array won't |
| 2210 | * be degraded any more and the spare can go back to sleep then. |
| 2211 | */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2212 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2213 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2214 | sync_req = mddev->in_sync; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2215 | |
| 2216 | /* If this is just a dirty<->clean transition, and the array is clean |
| 2217 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2218 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2219 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2220 | && mddev->can_decrease_events |
| 2221 | && mddev->events != 1) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2222 | mddev->events--; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2223 | mddev->can_decrease_events = 0; |
| 2224 | } else { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2225 | /* otherwise we have to go forward and ... */ |
| 2226 | mddev->events ++; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2227 | mddev->can_decrease_events = nospares; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2228 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2229 | |
| 2230 | if (!mddev->events) { |
| 2231 | /* |
| 2232 | * oops, this 64-bit counter should never wrap. |
| 2233 | * Either we are in around ~1 trillion A.C., assuming |
| 2234 | * 1 reboot per second, or we have a bug: |
| 2235 | */ |
| 2236 | MD_BUG(); |
| 2237 | mddev->events --; |
| 2238 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2239 | sync_sbs(mddev, nospares); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2240 | spin_unlock_irq(&mddev->write_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2241 | |
| 2242 | dprintk(KERN_INFO |
| 2243 | "md: updating %s RAID superblock on device (in sync %d)\n", |
| 2244 | mdname(mddev),mddev->in_sync); |
| 2245 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 2246 | bitmap_update_sb(mddev->bitmap); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2247 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2248 | char b[BDEVNAME_SIZE]; |
| 2249 | dprintk(KERN_INFO "md: "); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2250 | if (rdev->sb_loaded != 1) |
| 2251 | continue; /* no noise on spare devices */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2252 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2253 | dprintk("(skipping faulty "); |
| 2254 | |
| 2255 | dprintk("%s ", bdevname(rdev->bdev,b)); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2256 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2257 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2258 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2259 | rdev->sb_page); |
| 2260 | dprintk(KERN_INFO "(write) %s's sb offset: %llu\n", |
| 2261 | bdevname(rdev->bdev,b), |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2262 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2263 | rdev->sb_events = mddev->events; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2264 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2265 | } else |
| 2266 | dprintk(")\n"); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2267 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2268 | /* only need to write one superblock... */ |
| 2269 | break; |
| 2270 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2271 | md_super_wait(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2272 | /* 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] | 2273 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2274 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2275 | if (mddev->in_sync != sync_req || |
| 2276 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2277 | /* have to write it out again */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2278 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2279 | goto repeat; |
| 2280 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2281 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2282 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2283 | wake_up(&mddev->sb_wait); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 2284 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 2285 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2286 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2287 | } |
| 2288 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 2289 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2290 | * We want to accept with case. For this we use cmd_match. |
| 2291 | */ |
| 2292 | static int cmd_match(const char *cmd, const char *str) |
| 2293 | { |
| 2294 | /* See if cmd, written into a sysfs file, matches |
| 2295 | * str. They must either be the same, or cmd can |
| 2296 | * have a trailing newline |
| 2297 | */ |
| 2298 | while (*cmd && *str && *cmd == *str) { |
| 2299 | cmd++; |
| 2300 | str++; |
| 2301 | } |
| 2302 | if (*cmd == '\n') |
| 2303 | cmd++; |
| 2304 | if (*str || *cmd) |
| 2305 | return 0; |
| 2306 | return 1; |
| 2307 | } |
| 2308 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2309 | struct rdev_sysfs_entry { |
| 2310 | struct attribute attr; |
| 2311 | ssize_t (*show)(mdk_rdev_t *, char *); |
| 2312 | ssize_t (*store)(mdk_rdev_t *, const char *, size_t); |
| 2313 | }; |
| 2314 | |
| 2315 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2316 | state_show(mdk_rdev_t *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2317 | { |
| 2318 | char *sep = ""; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2319 | size_t len = 0; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2320 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2321 | if (test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2322 | len+= sprintf(page+len, "%sfaulty",sep); |
| 2323 | sep = ","; |
| 2324 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2325 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2326 | len += sprintf(page+len, "%sin_sync",sep); |
| 2327 | sep = ","; |
| 2328 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2329 | if (test_bit(WriteMostly, &rdev->flags)) { |
| 2330 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 2331 | sep = ","; |
| 2332 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2333 | if (test_bit(Blocked, &rdev->flags)) { |
| 2334 | len += sprintf(page+len, "%sblocked", sep); |
| 2335 | sep = ","; |
| 2336 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2337 | if (!test_bit(Faulty, &rdev->flags) && |
| 2338 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2339 | len += sprintf(page+len, "%sspare", sep); |
| 2340 | sep = ","; |
| 2341 | } |
| 2342 | return len+sprintf(page+len, "\n"); |
| 2343 | } |
| 2344 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2345 | static ssize_t |
| 2346 | state_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2347 | { |
| 2348 | /* can write |
| 2349 | * faulty - simulates and error |
| 2350 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2351 | * writemostly - sets write_mostly |
| 2352 | * -writemostly - clears write_mostly |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2353 | * blocked - sets the Blocked flag |
| 2354 | * -blocked - clears the Blocked flag |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2355 | * insync - sets Insync providing device isn't active |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2356 | */ |
| 2357 | int err = -EINVAL; |
| 2358 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 2359 | md_error(rdev->mddev, rdev); |
| 2360 | err = 0; |
| 2361 | } else if (cmd_match(buf, "remove")) { |
| 2362 | if (rdev->raid_disk >= 0) |
| 2363 | err = -EBUSY; |
| 2364 | else { |
| 2365 | mddev_t *mddev = rdev->mddev; |
| 2366 | kick_rdev_from_array(rdev); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2367 | if (mddev->pers) |
| 2368 | md_update_sb(mddev, 1); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2369 | md_new_event(mddev); |
| 2370 | err = 0; |
| 2371 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2372 | } else if (cmd_match(buf, "writemostly")) { |
| 2373 | set_bit(WriteMostly, &rdev->flags); |
| 2374 | err = 0; |
| 2375 | } else if (cmd_match(buf, "-writemostly")) { |
| 2376 | clear_bit(WriteMostly, &rdev->flags); |
| 2377 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2378 | } else if (cmd_match(buf, "blocked")) { |
| 2379 | set_bit(Blocked, &rdev->flags); |
| 2380 | err = 0; |
| 2381 | } else if (cmd_match(buf, "-blocked")) { |
| 2382 | clear_bit(Blocked, &rdev->flags); |
| 2383 | wake_up(&rdev->blocked_wait); |
| 2384 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2385 | md_wakeup_thread(rdev->mddev->thread); |
| 2386 | |
| 2387 | err = 0; |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2388 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { |
| 2389 | set_bit(In_sync, &rdev->flags); |
| 2390 | err = 0; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2391 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2392 | if (!err) |
| 2393 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2394 | return err ? err : len; |
| 2395 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2396 | static struct rdev_sysfs_entry rdev_state = |
| 2397 | __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2398 | |
| 2399 | static ssize_t |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2400 | errors_show(mdk_rdev_t *rdev, char *page) |
| 2401 | { |
| 2402 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 2403 | } |
| 2404 | |
| 2405 | static ssize_t |
| 2406 | errors_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2407 | { |
| 2408 | char *e; |
| 2409 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2410 | if (*buf && (*e == 0 || *e == '\n')) { |
| 2411 | atomic_set(&rdev->corrected_errors, n); |
| 2412 | return len; |
| 2413 | } |
| 2414 | return -EINVAL; |
| 2415 | } |
| 2416 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2417 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2418 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2419 | static ssize_t |
| 2420 | slot_show(mdk_rdev_t *rdev, char *page) |
| 2421 | { |
| 2422 | if (rdev->raid_disk < 0) |
| 2423 | return sprintf(page, "none\n"); |
| 2424 | else |
| 2425 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 2426 | } |
| 2427 | |
| 2428 | static ssize_t |
| 2429 | slot_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2430 | { |
| 2431 | char *e; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2432 | int err; |
| 2433 | char nm[20]; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2434 | int slot = simple_strtoul(buf, &e, 10); |
| 2435 | if (strncmp(buf, "none", 4)==0) |
| 2436 | slot = -1; |
| 2437 | else if (e==buf || (*e && *e!= '\n')) |
| 2438 | return -EINVAL; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2439 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2440 | /* Setting 'slot' on an active array requires also |
| 2441 | * updating the 'rd%d' link, and communicating |
| 2442 | * with the personality with ->hot_*_disk. |
| 2443 | * For now we only support removing |
| 2444 | * failed/spare devices. This normally happens automatically, |
| 2445 | * but not when the metadata is externally managed. |
| 2446 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2447 | if (rdev->raid_disk == -1) |
| 2448 | return -EEXIST; |
| 2449 | /* personality does all needed checks */ |
| 2450 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2451 | return -EINVAL; |
| 2452 | err = rdev->mddev->pers-> |
| 2453 | hot_remove_disk(rdev->mddev, rdev->raid_disk); |
| 2454 | if (err) |
| 2455 | return err; |
| 2456 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2457 | sysfs_remove_link(&rdev->mddev->kobj, nm); |
Maciej Trela | b710310 | 2010-04-14 16:58:16 +1000 | [diff] [blame] | 2458 | rdev->raid_disk = -1; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2459 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2460 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2461 | } else if (rdev->mddev->pers) { |
| 2462 | mdk_rdev_t *rdev2; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2463 | /* Activating a spare .. or possibly reactivating |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2464 | * if we ever get bitmaps working here. |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2465 | */ |
| 2466 | |
| 2467 | if (rdev->raid_disk != -1) |
| 2468 | return -EBUSY; |
| 2469 | |
NeilBrown | c6751b2 | 2011-02-02 11:57:13 +1100 | [diff] [blame] | 2470 | if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery)) |
| 2471 | return -EBUSY; |
| 2472 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2473 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2474 | return -EINVAL; |
| 2475 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2476 | list_for_each_entry(rdev2, &rdev->mddev->disks, same_set) |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2477 | if (rdev2->raid_disk == slot) |
| 2478 | return -EEXIST; |
| 2479 | |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2480 | if (slot >= rdev->mddev->raid_disks && |
| 2481 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
| 2482 | return -ENOSPC; |
| 2483 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2484 | rdev->raid_disk = slot; |
| 2485 | if (test_bit(In_sync, &rdev->flags)) |
| 2486 | rdev->saved_raid_disk = slot; |
| 2487 | else |
| 2488 | rdev->saved_raid_disk = -1; |
| 2489 | err = rdev->mddev->pers-> |
| 2490 | hot_add_disk(rdev->mddev, rdev); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 2491 | if (err) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2492 | rdev->raid_disk = -1; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2493 | return err; |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 2494 | } else |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2495 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2496 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2497 | if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2498 | /* failure here is OK */; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2499 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2500 | } else { |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2501 | if (slot >= rdev->mddev->raid_disks && |
| 2502 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2503 | return -ENOSPC; |
| 2504 | rdev->raid_disk = slot; |
| 2505 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2506 | clear_bit(Faulty, &rdev->flags); |
| 2507 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2508 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2509 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2510 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2511 | return len; |
| 2512 | } |
| 2513 | |
| 2514 | |
| 2515 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2516 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2517 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2518 | static ssize_t |
| 2519 | offset_show(mdk_rdev_t *rdev, char *page) |
| 2520 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2521 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2522 | } |
| 2523 | |
| 2524 | static ssize_t |
| 2525 | offset_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2526 | { |
| 2527 | char *e; |
| 2528 | unsigned long long offset = simple_strtoull(buf, &e, 10); |
| 2529 | if (e==buf || (*e && *e != '\n')) |
| 2530 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2531 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2532 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2533 | if (rdev->sectors && rdev->mddev->external) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2534 | /* Must set offset before size, so overlap checks |
| 2535 | * can be sane */ |
| 2536 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2537 | rdev->data_offset = offset; |
| 2538 | return len; |
| 2539 | } |
| 2540 | |
| 2541 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2542 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2543 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2544 | static ssize_t |
| 2545 | rdev_size_show(mdk_rdev_t *rdev, char *page) |
| 2546 | { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2547 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2548 | } |
| 2549 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2550 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 2551 | { |
| 2552 | /* check if two start/length pairs overlap */ |
| 2553 | if (s1+l1 <= s2) |
| 2554 | return 0; |
| 2555 | if (s2+l2 <= s1) |
| 2556 | return 0; |
| 2557 | return 1; |
| 2558 | } |
| 2559 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2560 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
| 2561 | { |
| 2562 | unsigned long long blocks; |
| 2563 | sector_t new; |
| 2564 | |
| 2565 | if (strict_strtoull(buf, 10, &blocks) < 0) |
| 2566 | return -EINVAL; |
| 2567 | |
| 2568 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) |
| 2569 | return -EINVAL; /* sector conversion overflow */ |
| 2570 | |
| 2571 | new = blocks * 2; |
| 2572 | if (new != blocks * 2) |
| 2573 | return -EINVAL; /* unsigned long long to sector_t overflow */ |
| 2574 | |
| 2575 | *sectors = new; |
| 2576 | return 0; |
| 2577 | } |
| 2578 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2579 | static ssize_t |
| 2580 | rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2581 | { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2582 | mddev_t *my_mddev = rdev->mddev; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2583 | sector_t oldsectors = rdev->sectors; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2584 | sector_t sectors; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2585 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2586 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2587 | return -EINVAL; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2588 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2589 | if (my_mddev->persistent) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2590 | sectors = super_types[my_mddev->major_version]. |
| 2591 | rdev_size_change(rdev, sectors); |
| 2592 | if (!sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2593 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2594 | } else if (!sectors) |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 2595 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) - |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2596 | rdev->data_offset; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2597 | } |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2598 | if (sectors < my_mddev->dev_sectors) |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 2599 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2600 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2601 | rdev->sectors = sectors; |
| 2602 | if (sectors > oldsectors && my_mddev->external) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2603 | /* need to check that all other rdevs with the same ->bdev |
| 2604 | * do not overlap. We need to unlock the mddev to avoid |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2605 | * a deadlock. We have already changed rdev->sectors, and if |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2606 | * we have to change it back, we will have the lock again. |
| 2607 | */ |
| 2608 | mddev_t *mddev; |
| 2609 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2610 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2611 | |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2612 | mddev_unlock(my_mddev); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2613 | for_each_mddev(mddev, tmp) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2614 | mdk_rdev_t *rdev2; |
| 2615 | |
| 2616 | mddev_lock(mddev); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2617 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
NeilBrown | f21e9ff | 2011-01-31 12:10:09 +1100 | [diff] [blame] | 2618 | if (rdev->bdev == rdev2->bdev && |
| 2619 | rdev != rdev2 && |
| 2620 | overlaps(rdev->data_offset, rdev->sectors, |
| 2621 | rdev2->data_offset, |
| 2622 | rdev2->sectors)) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2623 | overlap = 1; |
| 2624 | break; |
| 2625 | } |
| 2626 | mddev_unlock(mddev); |
| 2627 | if (overlap) { |
| 2628 | mddev_put(mddev); |
| 2629 | break; |
| 2630 | } |
| 2631 | } |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2632 | mddev_lock(my_mddev); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2633 | if (overlap) { |
| 2634 | /* Someone else could have slipped in a size |
| 2635 | * change here, but doing so is just silly. |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2636 | * We put oldsectors back because we *know* it is |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2637 | * safe, and trust userspace not to race with |
| 2638 | * itself |
| 2639 | */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2640 | rdev->sectors = oldsectors; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2641 | return -EBUSY; |
| 2642 | } |
| 2643 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2644 | return len; |
| 2645 | } |
| 2646 | |
| 2647 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2648 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2649 | |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2650 | |
| 2651 | static ssize_t recovery_start_show(mdk_rdev_t *rdev, char *page) |
| 2652 | { |
| 2653 | unsigned long long recovery_start = rdev->recovery_offset; |
| 2654 | |
| 2655 | if (test_bit(In_sync, &rdev->flags) || |
| 2656 | recovery_start == MaxSector) |
| 2657 | return sprintf(page, "none\n"); |
| 2658 | |
| 2659 | return sprintf(page, "%llu\n", recovery_start); |
| 2660 | } |
| 2661 | |
| 2662 | static ssize_t recovery_start_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2663 | { |
| 2664 | unsigned long long recovery_start; |
| 2665 | |
| 2666 | if (cmd_match(buf, "none")) |
| 2667 | recovery_start = MaxSector; |
| 2668 | else if (strict_strtoull(buf, 10, &recovery_start)) |
| 2669 | return -EINVAL; |
| 2670 | |
| 2671 | if (rdev->mddev->pers && |
| 2672 | rdev->raid_disk >= 0) |
| 2673 | return -EBUSY; |
| 2674 | |
| 2675 | rdev->recovery_offset = recovery_start; |
| 2676 | if (recovery_start == MaxSector) |
| 2677 | set_bit(In_sync, &rdev->flags); |
| 2678 | else |
| 2679 | clear_bit(In_sync, &rdev->flags); |
| 2680 | return len; |
| 2681 | } |
| 2682 | |
| 2683 | static struct rdev_sysfs_entry rdev_recovery_start = |
| 2684 | __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store); |
| 2685 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2686 | static struct attribute *rdev_default_attrs[] = { |
| 2687 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2688 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2689 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2690 | &rdev_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2691 | &rdev_size.attr, |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2692 | &rdev_recovery_start.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2693 | NULL, |
| 2694 | }; |
| 2695 | static ssize_t |
| 2696 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 2697 | { |
| 2698 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2699 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2700 | mddev_t *mddev = rdev->mddev; |
| 2701 | ssize_t rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2702 | |
| 2703 | if (!entry->show) |
| 2704 | return -EIO; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2705 | |
| 2706 | rv = mddev ? mddev_lock(mddev) : -EBUSY; |
| 2707 | if (!rv) { |
| 2708 | if (rdev->mddev == NULL) |
| 2709 | rv = -EBUSY; |
| 2710 | else |
| 2711 | rv = entry->show(rdev, page); |
| 2712 | mddev_unlock(mddev); |
| 2713 | } |
| 2714 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2715 | } |
| 2716 | |
| 2717 | static ssize_t |
| 2718 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 2719 | const char *page, size_t length) |
| 2720 | { |
| 2721 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2722 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2723 | ssize_t rv; |
| 2724 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2725 | |
| 2726 | if (!entry->store) |
| 2727 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 2728 | if (!capable(CAP_SYS_ADMIN)) |
| 2729 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2730 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2731 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2732 | if (rdev->mddev == NULL) |
| 2733 | rv = -EBUSY; |
| 2734 | else |
| 2735 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 2736 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2737 | } |
| 2738 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2739 | } |
| 2740 | |
| 2741 | static void rdev_free(struct kobject *ko) |
| 2742 | { |
| 2743 | mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj); |
| 2744 | kfree(rdev); |
| 2745 | } |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 2746 | static const struct sysfs_ops rdev_sysfs_ops = { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2747 | .show = rdev_attr_show, |
| 2748 | .store = rdev_attr_store, |
| 2749 | }; |
| 2750 | static struct kobj_type rdev_ktype = { |
| 2751 | .release = rdev_free, |
| 2752 | .sysfs_ops = &rdev_sysfs_ops, |
| 2753 | .default_attrs = rdev_default_attrs, |
| 2754 | }; |
| 2755 | |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 2756 | void md_rdev_init(mdk_rdev_t *rdev) |
| 2757 | { |
| 2758 | rdev->desc_nr = -1; |
| 2759 | rdev->saved_raid_disk = -1; |
| 2760 | rdev->raid_disk = -1; |
| 2761 | rdev->flags = 0; |
| 2762 | rdev->data_offset = 0; |
| 2763 | rdev->sb_events = 0; |
| 2764 | rdev->last_read_error.tv_sec = 0; |
| 2765 | rdev->last_read_error.tv_nsec = 0; |
| 2766 | atomic_set(&rdev->nr_pending, 0); |
| 2767 | atomic_set(&rdev->read_errors, 0); |
| 2768 | atomic_set(&rdev->corrected_errors, 0); |
| 2769 | |
| 2770 | INIT_LIST_HEAD(&rdev->same_set); |
| 2771 | init_waitqueue_head(&rdev->blocked_wait); |
| 2772 | } |
| 2773 | EXPORT_SYMBOL_GPL(md_rdev_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2774 | /* |
| 2775 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 2776 | * |
| 2777 | * mark the device faulty if: |
| 2778 | * |
| 2779 | * - the device is nonexistent (zero size) |
| 2780 | * - the device has no valid superblock |
| 2781 | * |
| 2782 | * a faulty rdev _never_ has rdev->sb set. |
| 2783 | */ |
| 2784 | static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor) |
| 2785 | { |
| 2786 | char b[BDEVNAME_SIZE]; |
| 2787 | int err; |
| 2788 | mdk_rdev_t *rdev; |
| 2789 | sector_t size; |
| 2790 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 2791 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2792 | if (!rdev) { |
| 2793 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 2794 | return ERR_PTR(-ENOMEM); |
| 2795 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2796 | |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 2797 | md_rdev_init(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2798 | if ((err = alloc_disk_sb(rdev))) |
| 2799 | goto abort_free; |
| 2800 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2801 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2802 | if (err) |
| 2803 | goto abort_free; |
| 2804 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2805 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2806 | |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 2807 | size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2808 | if (!size) { |
| 2809 | printk(KERN_WARNING |
| 2810 | "md: %s has zero or unknown size, marking faulty!\n", |
| 2811 | bdevname(rdev->bdev,b)); |
| 2812 | err = -EINVAL; |
| 2813 | goto abort_free; |
| 2814 | } |
| 2815 | |
| 2816 | if (super_format >= 0) { |
| 2817 | err = super_types[super_format]. |
| 2818 | load_super(rdev, NULL, super_minor); |
| 2819 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 2820 | printk(KERN_WARNING |
| 2821 | "md: %s does not have a valid v%d.%d " |
| 2822 | "superblock, not importing!\n", |
| 2823 | bdevname(rdev->bdev,b), |
| 2824 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2825 | goto abort_free; |
| 2826 | } |
| 2827 | if (err < 0) { |
| 2828 | printk(KERN_WARNING |
| 2829 | "md: could not read %s's sb, not importing!\n", |
| 2830 | bdevname(rdev->bdev,b)); |
| 2831 | goto abort_free; |
| 2832 | } |
| 2833 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2834 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2835 | return rdev; |
| 2836 | |
| 2837 | abort_free: |
| 2838 | if (rdev->sb_page) { |
| 2839 | if (rdev->bdev) |
| 2840 | unlock_rdev(rdev); |
| 2841 | free_disk_sb(rdev); |
| 2842 | } |
| 2843 | kfree(rdev); |
| 2844 | return ERR_PTR(err); |
| 2845 | } |
| 2846 | |
| 2847 | /* |
| 2848 | * Check a full RAID array for plausibility |
| 2849 | */ |
| 2850 | |
| 2851 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 2852 | static void analyze_sbs(mddev_t * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2853 | { |
| 2854 | int i; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2855 | mdk_rdev_t *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2856 | char b[BDEVNAME_SIZE]; |
| 2857 | |
| 2858 | freshest = NULL; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2859 | rdev_for_each(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2860 | switch (super_types[mddev->major_version]. |
| 2861 | load_super(rdev, freshest, mddev->minor_version)) { |
| 2862 | case 1: |
| 2863 | freshest = rdev; |
| 2864 | break; |
| 2865 | case 0: |
| 2866 | break; |
| 2867 | default: |
| 2868 | printk( KERN_ERR \ |
| 2869 | "md: fatal superblock inconsistency in %s" |
| 2870 | " -- removing from array\n", |
| 2871 | bdevname(rdev->bdev,b)); |
| 2872 | kick_rdev_from_array(rdev); |
| 2873 | } |
| 2874 | |
| 2875 | |
| 2876 | super_types[mddev->major_version]. |
| 2877 | validate_super(mddev, freshest); |
| 2878 | |
| 2879 | i = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2880 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 2881 | if (mddev->max_disks && |
| 2882 | (rdev->desc_nr >= mddev->max_disks || |
| 2883 | i > mddev->max_disks)) { |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 2884 | printk(KERN_WARNING |
| 2885 | "md: %s: %s: only %d devices permitted\n", |
| 2886 | mdname(mddev), bdevname(rdev->bdev, b), |
| 2887 | mddev->max_disks); |
| 2888 | kick_rdev_from_array(rdev); |
| 2889 | continue; |
| 2890 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2891 | if (rdev != freshest) |
| 2892 | if (super_types[mddev->major_version]. |
| 2893 | validate_super(mddev, rdev)) { |
| 2894 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 2895 | " from array!\n", |
| 2896 | bdevname(rdev->bdev,b)); |
| 2897 | kick_rdev_from_array(rdev); |
| 2898 | continue; |
| 2899 | } |
| 2900 | if (mddev->level == LEVEL_MULTIPATH) { |
| 2901 | rdev->desc_nr = i++; |
| 2902 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2903 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 2904 | } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2905 | rdev->raid_disk = -1; |
| 2906 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2907 | } |
| 2908 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2909 | } |
| 2910 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2911 | /* Read a fixed-point number. |
| 2912 | * Numbers in sysfs attributes should be in "standard" units where |
| 2913 | * possible, so time should be in seconds. |
| 2914 | * However we internally use a a much smaller unit such as |
| 2915 | * milliseconds or jiffies. |
| 2916 | * This function takes a decimal number with a possible fractional |
| 2917 | * component, and produces an integer which is the result of |
| 2918 | * multiplying that number by 10^'scale'. |
| 2919 | * all without any floating-point arithmetic. |
| 2920 | */ |
| 2921 | int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale) |
| 2922 | { |
| 2923 | unsigned long result = 0; |
| 2924 | long decimals = -1; |
| 2925 | while (isdigit(*cp) || (*cp == '.' && decimals < 0)) { |
| 2926 | if (*cp == '.') |
| 2927 | decimals = 0; |
| 2928 | else if (decimals < scale) { |
| 2929 | unsigned int value; |
| 2930 | value = *cp - '0'; |
| 2931 | result = result * 10 + value; |
| 2932 | if (decimals >= 0) |
| 2933 | decimals++; |
| 2934 | } |
| 2935 | cp++; |
| 2936 | } |
| 2937 | if (*cp == '\n') |
| 2938 | cp++; |
| 2939 | if (*cp) |
| 2940 | return -EINVAL; |
| 2941 | if (decimals < 0) |
| 2942 | decimals = 0; |
| 2943 | while (decimals < scale) { |
| 2944 | result *= 10; |
| 2945 | decimals ++; |
| 2946 | } |
| 2947 | *res = result; |
| 2948 | return 0; |
| 2949 | } |
| 2950 | |
| 2951 | |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2952 | static void md_safemode_timeout(unsigned long data); |
| 2953 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2954 | static ssize_t |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2955 | safe_delay_show(mddev_t *mddev, char *page) |
| 2956 | { |
| 2957 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 2958 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 2959 | } |
| 2960 | static ssize_t |
| 2961 | safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len) |
| 2962 | { |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2963 | unsigned long msec; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 2964 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2965 | if (strict_strtoul_scaled(cbuf, &msec, 3) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2966 | return -EINVAL; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2967 | if (msec == 0) |
| 2968 | mddev->safemode_delay = 0; |
| 2969 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2970 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2971 | mddev->safemode_delay = (msec*HZ)/1000; |
| 2972 | if (mddev->safemode_delay == 0) |
| 2973 | mddev->safemode_delay = 1; |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2974 | if (mddev->safemode_delay < old_delay) |
| 2975 | md_safemode_timeout((unsigned long)mddev); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2976 | } |
| 2977 | return len; |
| 2978 | } |
| 2979 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2980 | __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] | 2981 | |
| 2982 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2983 | level_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2984 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 2985 | struct mdk_personality *p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2986 | if (p) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2987 | return sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2988 | else if (mddev->clevel[0]) |
| 2989 | return sprintf(page, "%s\n", mddev->clevel); |
| 2990 | else if (mddev->level != LEVEL_NONE) |
| 2991 | return sprintf(page, "%d\n", mddev->level); |
| 2992 | else |
| 2993 | return 0; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2994 | } |
| 2995 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2996 | static ssize_t |
| 2997 | level_store(mddev_t *mddev, const char *buf, size_t len) |
| 2998 | { |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 2999 | char clevel[16]; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 3000 | ssize_t rv = len; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3001 | struct mdk_personality *pers; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3002 | long level; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3003 | void *priv; |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3004 | mdk_rdev_t *rdev; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3005 | |
| 3006 | if (mddev->pers == NULL) { |
| 3007 | if (len == 0) |
| 3008 | return 0; |
| 3009 | if (len >= sizeof(mddev->clevel)) |
| 3010 | return -ENOSPC; |
| 3011 | strncpy(mddev->clevel, buf, len); |
| 3012 | if (mddev->clevel[len-1] == '\n') |
| 3013 | len--; |
| 3014 | mddev->clevel[len] = 0; |
| 3015 | mddev->level = LEVEL_NONE; |
| 3016 | return rv; |
| 3017 | } |
| 3018 | |
| 3019 | /* request to change the personality. Need to ensure: |
| 3020 | * - array is not engaged in resync/recovery/reshape |
| 3021 | * - old personality can be suspended |
| 3022 | * - new personality will access other array. |
| 3023 | */ |
| 3024 | |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 3025 | if (mddev->sync_thread || |
| 3026 | mddev->reshape_position != MaxSector || |
| 3027 | mddev->sysfs_active) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3028 | return -EBUSY; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3029 | |
| 3030 | if (!mddev->pers->quiesce) { |
| 3031 | printk(KERN_WARNING "md: %s: %s does not support online personality change\n", |
| 3032 | mdname(mddev), mddev->pers->name); |
| 3033 | return -EINVAL; |
| 3034 | } |
| 3035 | |
| 3036 | /* Now find the new personality */ |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3037 | if (len == 0 || len >= sizeof(clevel)) |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3038 | return -EINVAL; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3039 | strncpy(clevel, buf, len); |
| 3040 | if (clevel[len-1] == '\n') |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3041 | len--; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3042 | clevel[len] = 0; |
| 3043 | if (strict_strtol(clevel, 10, &level)) |
| 3044 | level = LEVEL_NONE; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3045 | |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3046 | if (request_module("md-%s", clevel) != 0) |
| 3047 | request_module("md-level-%s", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3048 | spin_lock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3049 | pers = find_pers(level, clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3050 | if (!pers || !try_module_get(pers->owner)) { |
| 3051 | spin_unlock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3052 | printk(KERN_WARNING "md: personality %s not loaded\n", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3053 | return -EINVAL; |
| 3054 | } |
| 3055 | spin_unlock(&pers_lock); |
| 3056 | |
| 3057 | if (pers == mddev->pers) { |
| 3058 | /* Nothing to do! */ |
| 3059 | module_put(pers->owner); |
| 3060 | return rv; |
| 3061 | } |
| 3062 | if (!pers->takeover) { |
| 3063 | module_put(pers->owner); |
| 3064 | printk(KERN_WARNING "md: %s: %s does not support personality takeover\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3065 | mdname(mddev), clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3066 | return -EINVAL; |
| 3067 | } |
| 3068 | |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3069 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 3070 | rdev->new_raid_disk = rdev->raid_disk; |
| 3071 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3072 | /* ->takeover must set new_* and/or delta_disks |
| 3073 | * if it succeeds, and may set them when it fails. |
| 3074 | */ |
| 3075 | priv = pers->takeover(mddev); |
| 3076 | if (IS_ERR(priv)) { |
| 3077 | mddev->new_level = mddev->level; |
| 3078 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3079 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3080 | mddev->raid_disks -= mddev->delta_disks; |
| 3081 | mddev->delta_disks = 0; |
| 3082 | module_put(pers->owner); |
| 3083 | printk(KERN_WARNING "md: %s: %s would not accept array\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3084 | mdname(mddev), clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3085 | return PTR_ERR(priv); |
| 3086 | } |
| 3087 | |
| 3088 | /* Looks like we have a winner */ |
| 3089 | mddev_suspend(mddev); |
| 3090 | mddev->pers->stop(mddev); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 3091 | |
| 3092 | if (mddev->pers->sync_request == NULL && |
| 3093 | pers->sync_request != NULL) { |
| 3094 | /* need to add the md_redundancy_group */ |
| 3095 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 3096 | printk(KERN_WARNING |
| 3097 | "md: cannot register extra attributes for %s\n", |
| 3098 | mdname(mddev)); |
NeilBrown | 19fdb9e | 2010-05-22 08:31:36 +1000 | [diff] [blame] | 3099 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action"); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 3100 | } |
| 3101 | if (mddev->pers->sync_request != NULL && |
| 3102 | pers->sync_request == NULL) { |
| 3103 | /* need to remove the md_redundancy_group */ |
| 3104 | if (mddev->to_remove == NULL) |
| 3105 | mddev->to_remove = &md_redundancy_group; |
| 3106 | } |
| 3107 | |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 3108 | if (mddev->pers->sync_request == NULL && |
| 3109 | mddev->external) { |
| 3110 | /* We are converting from a no-redundancy array |
| 3111 | * to a redundancy array and metadata is managed |
| 3112 | * externally so we need to be sure that writes |
| 3113 | * won't block due to a need to transition |
| 3114 | * clean->dirty |
| 3115 | * until external management is started. |
| 3116 | */ |
| 3117 | mddev->in_sync = 0; |
| 3118 | mddev->safemode_delay = 0; |
| 3119 | mddev->safemode = 0; |
| 3120 | } |
| 3121 | |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3122 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 3123 | char nm[20]; |
| 3124 | if (rdev->raid_disk < 0) |
| 3125 | continue; |
NeilBrown | bf2cb0d | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 3126 | if (rdev->new_raid_disk >= mddev->raid_disks) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3127 | rdev->new_raid_disk = -1; |
| 3128 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3129 | continue; |
| 3130 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 3131 | sysfs_remove_link(&mddev->kobj, nm); |
| 3132 | } |
| 3133 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 3134 | if (rdev->raid_disk < 0) |
| 3135 | continue; |
| 3136 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3137 | continue; |
| 3138 | rdev->raid_disk = rdev->new_raid_disk; |
| 3139 | if (rdev->raid_disk < 0) |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3140 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3141 | else { |
| 3142 | char nm[20]; |
| 3143 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 3144 | if(sysfs_create_link(&mddev->kobj, &rdev->kobj, nm)) |
| 3145 | printk("md: cannot register %s for %s after level change\n", |
| 3146 | nm, mdname(mddev)); |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3147 | } |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3148 | } |
| 3149 | |
| 3150 | module_put(mddev->pers->owner); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3151 | mddev->pers = pers; |
| 3152 | mddev->private = priv; |
| 3153 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
| 3154 | mddev->level = mddev->new_level; |
| 3155 | mddev->layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3156 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3157 | mddev->delta_disks = 0; |
Trela, Maciej | 9af204c | 2010-03-08 16:02:44 +1100 | [diff] [blame] | 3158 | if (mddev->pers->sync_request == NULL) { |
| 3159 | /* this is now an array without redundancy, so |
| 3160 | * it must always be in_sync |
| 3161 | */ |
| 3162 | mddev->in_sync = 1; |
| 3163 | del_timer_sync(&mddev->safemode_timer); |
| 3164 | } |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3165 | pers->run(mddev); |
| 3166 | mddev_resume(mddev); |
| 3167 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 3168 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3169 | md_wakeup_thread(mddev->thread); |
Maciej Trela | 5cac7861 | 2010-04-14 17:17:39 +1000 | [diff] [blame] | 3170 | sysfs_notify(&mddev->kobj, NULL, "level"); |
Dan Williams | bb7f8d2 | 2010-05-01 18:14:57 -0700 | [diff] [blame] | 3171 | md_new_event(mddev); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3172 | return rv; |
| 3173 | } |
| 3174 | |
| 3175 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3176 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3177 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3178 | |
| 3179 | static ssize_t |
| 3180 | layout_show(mddev_t *mddev, char *page) |
| 3181 | { |
| 3182 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3183 | if (mddev->reshape_position != MaxSector && |
| 3184 | mddev->layout != mddev->new_layout) |
| 3185 | return sprintf(page, "%d (%d)\n", |
| 3186 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3187 | return sprintf(page, "%d\n", mddev->layout); |
| 3188 | } |
| 3189 | |
| 3190 | static ssize_t |
| 3191 | layout_store(mddev_t *mddev, const char *buf, size_t len) |
| 3192 | { |
| 3193 | char *e; |
| 3194 | unsigned long n = simple_strtoul(buf, &e, 10); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3195 | |
| 3196 | if (!*buf || (*e && *e != '\n')) |
| 3197 | return -EINVAL; |
| 3198 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3199 | if (mddev->pers) { |
| 3200 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3201 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3202 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3203 | mddev->new_layout = n; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3204 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3205 | if (err) { |
| 3206 | mddev->new_layout = mddev->layout; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3207 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3208 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3209 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3210 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3211 | if (mddev->reshape_position == MaxSector) |
| 3212 | mddev->layout = n; |
| 3213 | } |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3214 | return len; |
| 3215 | } |
| 3216 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3217 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3218 | |
| 3219 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3220 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3221 | raid_disks_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3222 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 3223 | if (mddev->raid_disks == 0) |
| 3224 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3225 | if (mddev->reshape_position != MaxSector && |
| 3226 | mddev->delta_disks != 0) |
| 3227 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 3228 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3229 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 3230 | } |
| 3231 | |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3232 | static int update_raid_disks(mddev_t *mddev, int raid_disks); |
| 3233 | |
| 3234 | static ssize_t |
| 3235 | raid_disks_store(mddev_t *mddev, const char *buf, size_t len) |
| 3236 | { |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3237 | char *e; |
| 3238 | int rv = 0; |
| 3239 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3240 | |
| 3241 | if (!*buf || (*e && *e != '\n')) |
| 3242 | return -EINVAL; |
| 3243 | |
| 3244 | if (mddev->pers) |
| 3245 | rv = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3246 | else if (mddev->reshape_position != MaxSector) { |
| 3247 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
| 3248 | mddev->delta_disks = n - olddisks; |
| 3249 | mddev->raid_disks = n; |
| 3250 | } else |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3251 | mddev->raid_disks = n; |
| 3252 | return rv ? rv : len; |
| 3253 | } |
| 3254 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3255 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3256 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3257 | static ssize_t |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3258 | chunk_size_show(mddev_t *mddev, char *page) |
| 3259 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3260 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3261 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 3262 | return sprintf(page, "%d (%d)\n", |
| 3263 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3264 | mddev->chunk_sectors << 9); |
| 3265 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3266 | } |
| 3267 | |
| 3268 | static ssize_t |
| 3269 | chunk_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3270 | { |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3271 | char *e; |
| 3272 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3273 | |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3274 | if (!*buf || (*e && *e != '\n')) |
| 3275 | return -EINVAL; |
| 3276 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3277 | if (mddev->pers) { |
| 3278 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3279 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3280 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3281 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3282 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3283 | if (err) { |
| 3284 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3285 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3286 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3287 | } else { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3288 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3289 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3290 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3291 | } |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3292 | return len; |
| 3293 | } |
| 3294 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3295 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3296 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3297 | static ssize_t |
| 3298 | resync_start_show(mddev_t *mddev, char *page) |
| 3299 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3300 | if (mddev->recovery_cp == MaxSector) |
| 3301 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3302 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 3303 | } |
| 3304 | |
| 3305 | static ssize_t |
| 3306 | resync_start_store(mddev_t *mddev, const char *buf, size_t len) |
| 3307 | { |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3308 | char *e; |
| 3309 | unsigned long long n = simple_strtoull(buf, &e, 10); |
| 3310 | |
| 3311 | if (mddev->pers) |
| 3312 | return -EBUSY; |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3313 | if (cmd_match(buf, "none")) |
| 3314 | n = MaxSector; |
| 3315 | else if (!*buf || (*e && *e != '\n')) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3316 | return -EINVAL; |
| 3317 | |
| 3318 | mddev->recovery_cp = n; |
| 3319 | return len; |
| 3320 | } |
| 3321 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3322 | __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3323 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3324 | /* |
| 3325 | * The array state can be: |
| 3326 | * |
| 3327 | * clear |
| 3328 | * No devices, no size, no level |
| 3329 | * Equivalent to STOP_ARRAY ioctl |
| 3330 | * inactive |
| 3331 | * May have some settings, but array is not active |
| 3332 | * all IO results in error |
| 3333 | * When written, doesn't tear down array, but just stops it |
| 3334 | * suspended (not supported yet) |
| 3335 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 3336 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3337 | * readonly |
| 3338 | * no resync can happen. no superblocks get written. |
| 3339 | * write requests fail |
| 3340 | * read-auto |
| 3341 | * like readonly, but behaves like 'clean' on a write request. |
| 3342 | * |
| 3343 | * clean - no pending writes, but otherwise active. |
| 3344 | * When written to inactive array, starts without resync |
| 3345 | * If a write request arrives then |
| 3346 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 3347 | * if not known, block and switch to write-pending |
| 3348 | * If written to an active array that has pending writes, then fails. |
| 3349 | * active |
| 3350 | * fully active: IO and resync can be happening. |
| 3351 | * When written to inactive array, starts with resync |
| 3352 | * |
| 3353 | * write-pending |
| 3354 | * clean, but writes are blocked waiting for 'active' to be written. |
| 3355 | * |
| 3356 | * active-idle |
| 3357 | * like active, but no writes have been seen for a while (100msec). |
| 3358 | * |
| 3359 | */ |
| 3360 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 3361 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3362 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3363 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 3364 | "write-pending", "active-idle", NULL }; |
| 3365 | |
| 3366 | static int match_word(const char *word, char **list) |
| 3367 | { |
| 3368 | int n; |
| 3369 | for (n=0; list[n]; n++) |
| 3370 | if (cmd_match(word, list[n])) |
| 3371 | break; |
| 3372 | return n; |
| 3373 | } |
| 3374 | |
| 3375 | static ssize_t |
| 3376 | array_state_show(mddev_t *mddev, char *page) |
| 3377 | { |
| 3378 | enum array_state st = inactive; |
| 3379 | |
| 3380 | if (mddev->pers) |
| 3381 | switch(mddev->ro) { |
| 3382 | case 1: |
| 3383 | st = readonly; |
| 3384 | break; |
| 3385 | case 2: |
| 3386 | st = read_auto; |
| 3387 | break; |
| 3388 | case 0: |
| 3389 | if (mddev->in_sync) |
| 3390 | st = clean; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3391 | else if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3392 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3393 | else if (mddev->safemode) |
| 3394 | st = active_idle; |
| 3395 | else |
| 3396 | st = active; |
| 3397 | } |
| 3398 | else { |
| 3399 | if (list_empty(&mddev->disks) && |
| 3400 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3401 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3402 | st = clear; |
| 3403 | else |
| 3404 | st = inactive; |
| 3405 | } |
| 3406 | return sprintf(page, "%s\n", array_states[st]); |
| 3407 | } |
| 3408 | |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3409 | static int do_md_stop(mddev_t * mddev, int ro, int is_open); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3410 | static int md_set_readonly(mddev_t * mddev, int is_open); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3411 | static int do_md_run(mddev_t * mddev); |
| 3412 | static int restart_array(mddev_t *mddev); |
| 3413 | |
| 3414 | static ssize_t |
| 3415 | array_state_store(mddev_t *mddev, const char *buf, size_t len) |
| 3416 | { |
| 3417 | int err = -EINVAL; |
| 3418 | enum array_state st = match_word(buf, array_states); |
| 3419 | switch(st) { |
| 3420 | case bad_word: |
| 3421 | break; |
| 3422 | case clear: |
| 3423 | /* stopping an active array */ |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3424 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3425 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3426 | err = do_md_stop(mddev, 0, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3427 | break; |
| 3428 | case inactive: |
| 3429 | /* stopping an active array */ |
| 3430 | if (mddev->pers) { |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3431 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3432 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3433 | err = do_md_stop(mddev, 2, 0); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3434 | } else |
| 3435 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3436 | break; |
| 3437 | case suspended: |
| 3438 | break; /* not supported yet */ |
| 3439 | case readonly: |
| 3440 | if (mddev->pers) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3441 | err = md_set_readonly(mddev, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3442 | else { |
| 3443 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3444 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3445 | err = do_md_run(mddev); |
| 3446 | } |
| 3447 | break; |
| 3448 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3449 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3450 | if (mddev->ro == 0) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3451 | err = md_set_readonly(mddev, 0); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3452 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3453 | err = restart_array(mddev); |
| 3454 | if (err == 0) { |
| 3455 | mddev->ro = 2; |
| 3456 | set_disk_ro(mddev->gendisk, 0); |
| 3457 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3458 | } else { |
| 3459 | mddev->ro = 2; |
| 3460 | err = do_md_run(mddev); |
| 3461 | } |
| 3462 | break; |
| 3463 | case clean: |
| 3464 | if (mddev->pers) { |
| 3465 | restart_array(mddev); |
| 3466 | spin_lock_irq(&mddev->write_lock); |
| 3467 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3468 | if (mddev->in_sync == 0) { |
| 3469 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3470 | if (mddev->safemode == 1) |
| 3471 | mddev->safemode = 0; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3472 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3473 | } |
| 3474 | err = 0; |
| 3475 | } else |
| 3476 | err = -EBUSY; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3477 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 3478 | } else |
| 3479 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3480 | break; |
| 3481 | case active: |
| 3482 | if (mddev->pers) { |
| 3483 | restart_array(mddev); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3484 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3485 | wake_up(&mddev->sb_wait); |
| 3486 | err = 0; |
| 3487 | } else { |
| 3488 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3489 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3490 | err = do_md_run(mddev); |
| 3491 | } |
| 3492 | break; |
| 3493 | case write_pending: |
| 3494 | case active_idle: |
| 3495 | /* these cannot be set */ |
| 3496 | break; |
| 3497 | } |
| 3498 | if (err) |
| 3499 | return err; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3500 | else { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 3501 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3502 | return len; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3503 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3504 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3505 | static struct md_sysfs_entry md_array_state = |
| 3506 | __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3507 | |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3508 | static ssize_t |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 3509 | max_corrected_read_errors_show(mddev_t *mddev, char *page) { |
| 3510 | return sprintf(page, "%d\n", |
| 3511 | atomic_read(&mddev->max_corr_read_errors)); |
| 3512 | } |
| 3513 | |
| 3514 | static ssize_t |
| 3515 | max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len) |
| 3516 | { |
| 3517 | char *e; |
| 3518 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3519 | |
| 3520 | if (*buf && (*e == 0 || *e == '\n')) { |
| 3521 | atomic_set(&mddev->max_corr_read_errors, n); |
| 3522 | return len; |
| 3523 | } |
| 3524 | return -EINVAL; |
| 3525 | } |
| 3526 | |
| 3527 | static struct md_sysfs_entry max_corr_read_errors = |
| 3528 | __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show, |
| 3529 | max_corrected_read_errors_store); |
| 3530 | |
| 3531 | static ssize_t |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3532 | null_show(mddev_t *mddev, char *page) |
| 3533 | { |
| 3534 | return -EINVAL; |
| 3535 | } |
| 3536 | |
| 3537 | static ssize_t |
| 3538 | new_dev_store(mddev_t *mddev, const char *buf, size_t len) |
| 3539 | { |
| 3540 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 3541 | /* The new device is added to the array. |
| 3542 | * If the array has a persistent superblock, we read the |
| 3543 | * superblock to initialise info and check validity. |
| 3544 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 3545 | * which mainly checks size. |
| 3546 | */ |
| 3547 | char *e; |
| 3548 | int major = simple_strtoul(buf, &e, 10); |
| 3549 | int minor; |
| 3550 | dev_t dev; |
| 3551 | mdk_rdev_t *rdev; |
| 3552 | int err; |
| 3553 | |
| 3554 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 3555 | return -EINVAL; |
| 3556 | minor = simple_strtoul(e+1, &e, 10); |
| 3557 | if (*e && *e != '\n') |
| 3558 | return -EINVAL; |
| 3559 | dev = MKDEV(major, minor); |
| 3560 | if (major != MAJOR(dev) || |
| 3561 | minor != MINOR(dev)) |
| 3562 | return -EOVERFLOW; |
| 3563 | |
| 3564 | |
| 3565 | if (mddev->persistent) { |
| 3566 | rdev = md_import_device(dev, mddev->major_version, |
| 3567 | mddev->minor_version); |
| 3568 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
| 3569 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 3570 | mdk_rdev_t, same_set); |
| 3571 | err = super_types[mddev->major_version] |
| 3572 | .load_super(rdev, rdev0, mddev->minor_version); |
| 3573 | if (err < 0) |
| 3574 | goto out; |
| 3575 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3576 | } else if (mddev->external) |
| 3577 | rdev = md_import_device(dev, -2, -1); |
| 3578 | else |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3579 | rdev = md_import_device(dev, -1, -1); |
| 3580 | |
| 3581 | if (IS_ERR(rdev)) |
| 3582 | return PTR_ERR(rdev); |
| 3583 | err = bind_rdev_to_array(rdev, mddev); |
| 3584 | out: |
| 3585 | if (err) |
| 3586 | export_rdev(rdev); |
| 3587 | return err ? err : len; |
| 3588 | } |
| 3589 | |
| 3590 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3591 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3592 | |
| 3593 | static ssize_t |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3594 | bitmap_store(mddev_t *mddev, const char *buf, size_t len) |
| 3595 | { |
| 3596 | char *end; |
| 3597 | unsigned long chunk, end_chunk; |
| 3598 | |
| 3599 | if (!mddev->bitmap) |
| 3600 | goto out; |
| 3601 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 3602 | while (*buf) { |
| 3603 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 3604 | if (buf == end) break; |
| 3605 | if (*end == '-') { /* range */ |
| 3606 | buf = end + 1; |
| 3607 | end_chunk = simple_strtoul(buf, &end, 0); |
| 3608 | if (buf == end) break; |
| 3609 | } |
| 3610 | if (*end && !isspace(*end)) break; |
| 3611 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 3612 | buf = skip_spaces(end); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3613 | } |
| 3614 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 3615 | out: |
| 3616 | return len; |
| 3617 | } |
| 3618 | |
| 3619 | static struct md_sysfs_entry md_bitmap = |
| 3620 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 3621 | |
| 3622 | static ssize_t |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3623 | size_show(mddev_t *mddev, char *page) |
| 3624 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3625 | return sprintf(page, "%llu\n", |
| 3626 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3627 | } |
| 3628 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3629 | static int update_size(mddev_t *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3630 | |
| 3631 | static ssize_t |
| 3632 | size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3633 | { |
| 3634 | /* If array is inactive, we can reduce the component size, but |
| 3635 | * not increase it (except from 0). |
| 3636 | * If array is active, we can try an on-line resize |
| 3637 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3638 | sector_t sectors; |
| 3639 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3640 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3641 | if (err < 0) |
| 3642 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3643 | if (mddev->pers) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3644 | err = update_size(mddev, sectors); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3645 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3646 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3647 | if (mddev->dev_sectors == 0 || |
| 3648 | mddev->dev_sectors > sectors) |
| 3649 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3650 | else |
| 3651 | err = -ENOSPC; |
| 3652 | } |
| 3653 | return err ? err : len; |
| 3654 | } |
| 3655 | |
| 3656 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3657 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3658 | |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3659 | |
| 3660 | /* Metdata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3661 | * This is one of |
| 3662 | * 'none' for arrays with no metadata (good luck...) |
| 3663 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3664 | * or N.M for internally known formats |
| 3665 | */ |
| 3666 | static ssize_t |
| 3667 | metadata_show(mddev_t *mddev, char *page) |
| 3668 | { |
| 3669 | if (mddev->persistent) |
| 3670 | return sprintf(page, "%d.%d\n", |
| 3671 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3672 | else if (mddev->external) |
| 3673 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3674 | else |
| 3675 | return sprintf(page, "none\n"); |
| 3676 | } |
| 3677 | |
| 3678 | static ssize_t |
| 3679 | metadata_store(mddev_t *mddev, const char *buf, size_t len) |
| 3680 | { |
| 3681 | int major, minor; |
| 3682 | char *e; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 3683 | /* Changing the details of 'external' metadata is |
| 3684 | * always permitted. Otherwise there must be |
| 3685 | * no devices attached to the array. |
| 3686 | */ |
| 3687 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 3688 | ; |
| 3689 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3690 | return -EBUSY; |
| 3691 | |
| 3692 | if (cmd_match(buf, "none")) { |
| 3693 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3694 | mddev->external = 0; |
| 3695 | mddev->major_version = 0; |
| 3696 | mddev->minor_version = 90; |
| 3697 | return len; |
| 3698 | } |
| 3699 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 3700 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3701 | if (namelen >= sizeof(mddev->metadata_type)) |
| 3702 | namelen = sizeof(mddev->metadata_type)-1; |
| 3703 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 3704 | mddev->metadata_type[namelen] = 0; |
| 3705 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 3706 | mddev->metadata_type[--namelen] = 0; |
| 3707 | mddev->persistent = 0; |
| 3708 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3709 | mddev->major_version = 0; |
| 3710 | mddev->minor_version = 90; |
| 3711 | return len; |
| 3712 | } |
| 3713 | major = simple_strtoul(buf, &e, 10); |
| 3714 | if (e==buf || *e != '.') |
| 3715 | return -EINVAL; |
| 3716 | buf = e+1; |
| 3717 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 3718 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3719 | return -EINVAL; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 3720 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3721 | return -ENOENT; |
| 3722 | mddev->major_version = major; |
| 3723 | mddev->minor_version = minor; |
| 3724 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3725 | mddev->external = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3726 | return len; |
| 3727 | } |
| 3728 | |
| 3729 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3730 | __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3731 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3732 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3733 | action_show(mddev_t *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3734 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3735 | char *type = "idle"; |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3736 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 3737 | type = "frozen"; |
| 3738 | else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 2b12ab6 | 2007-10-16 23:30:53 -0700 | [diff] [blame] | 3739 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3740 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 3741 | type = "reshape"; |
| 3742 | else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3743 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 3744 | type = "resync"; |
| 3745 | else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 3746 | type = "check"; |
| 3747 | else |
| 3748 | type = "repair"; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3749 | } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3750 | type = "recover"; |
| 3751 | } |
| 3752 | return sprintf(page, "%s\n", type); |
| 3753 | } |
| 3754 | |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 3755 | static void reap_sync_thread(mddev_t *mddev); |
| 3756 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3757 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3758 | action_store(mddev_t *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3759 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3760 | if (!mddev->pers || !mddev->pers->sync_request) |
| 3761 | return -EINVAL; |
| 3762 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3763 | if (cmd_match(page, "frozen")) |
| 3764 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3765 | else |
| 3766 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3767 | |
| 3768 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3769 | if (mddev->sync_thread) { |
| 3770 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 3771 | reap_sync_thread(mddev); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3772 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3773 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 3774 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3775 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3776 | else if (cmd_match(page, "resync")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3777 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3778 | else if (cmd_match(page, "recover")) { |
| 3779 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 3780 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3781 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3782 | int err; |
| 3783 | if (mddev->pers->start_reshape == NULL) |
| 3784 | return -EINVAL; |
| 3785 | err = mddev->pers->start_reshape(mddev); |
| 3786 | if (err) |
| 3787 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 3788 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3789 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 3790 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3791 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 3792 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3793 | return -EINVAL; |
| 3794 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 3795 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3796 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3797 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3798 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 3799 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3800 | return len; |
| 3801 | } |
| 3802 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3803 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3804 | mismatch_cnt_show(mddev_t *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3805 | { |
| 3806 | return sprintf(page, "%llu\n", |
| 3807 | (unsigned long long) mddev->resync_mismatches); |
| 3808 | } |
| 3809 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3810 | static struct md_sysfs_entry md_scan_mode = |
| 3811 | __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3812 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3813 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3814 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3815 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3816 | static ssize_t |
| 3817 | sync_min_show(mddev_t *mddev, char *page) |
| 3818 | { |
| 3819 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 3820 | mddev->sync_speed_min ? "local": "system"); |
| 3821 | } |
| 3822 | |
| 3823 | static ssize_t |
| 3824 | sync_min_store(mddev_t *mddev, const char *buf, size_t len) |
| 3825 | { |
| 3826 | int min; |
| 3827 | char *e; |
| 3828 | if (strncmp(buf, "system", 6)==0) { |
| 3829 | mddev->sync_speed_min = 0; |
| 3830 | return len; |
| 3831 | } |
| 3832 | min = simple_strtoul(buf, &e, 10); |
| 3833 | if (buf == e || (*e && *e != '\n') || min <= 0) |
| 3834 | return -EINVAL; |
| 3835 | mddev->sync_speed_min = min; |
| 3836 | return len; |
| 3837 | } |
| 3838 | |
| 3839 | static struct md_sysfs_entry md_sync_min = |
| 3840 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 3841 | |
| 3842 | static ssize_t |
| 3843 | sync_max_show(mddev_t *mddev, char *page) |
| 3844 | { |
| 3845 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 3846 | mddev->sync_speed_max ? "local": "system"); |
| 3847 | } |
| 3848 | |
| 3849 | static ssize_t |
| 3850 | sync_max_store(mddev_t *mddev, const char *buf, size_t len) |
| 3851 | { |
| 3852 | int max; |
| 3853 | char *e; |
| 3854 | if (strncmp(buf, "system", 6)==0) { |
| 3855 | mddev->sync_speed_max = 0; |
| 3856 | return len; |
| 3857 | } |
| 3858 | max = simple_strtoul(buf, &e, 10); |
| 3859 | if (buf == e || (*e && *e != '\n') || max <= 0) |
| 3860 | return -EINVAL; |
| 3861 | mddev->sync_speed_max = max; |
| 3862 | return len; |
| 3863 | } |
| 3864 | |
| 3865 | static struct md_sysfs_entry md_sync_max = |
| 3866 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 3867 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 3868 | static ssize_t |
| 3869 | degraded_show(mddev_t *mddev, char *page) |
| 3870 | { |
| 3871 | return sprintf(page, "%d\n", mddev->degraded); |
| 3872 | } |
| 3873 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3874 | |
| 3875 | static ssize_t |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 3876 | sync_force_parallel_show(mddev_t *mddev, char *page) |
| 3877 | { |
| 3878 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 3879 | } |
| 3880 | |
| 3881 | static ssize_t |
| 3882 | sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len) |
| 3883 | { |
| 3884 | long n; |
| 3885 | |
| 3886 | if (strict_strtol(buf, 10, &n)) |
| 3887 | return -EINVAL; |
| 3888 | |
| 3889 | if (n != 0 && n != 1) |
| 3890 | return -EINVAL; |
| 3891 | |
| 3892 | mddev->parallel_resync = n; |
| 3893 | |
| 3894 | if (mddev->sync_thread) |
| 3895 | wake_up(&resync_wait); |
| 3896 | |
| 3897 | return len; |
| 3898 | } |
| 3899 | |
| 3900 | /* force parallel resync, even with shared block devices */ |
| 3901 | static struct md_sysfs_entry md_sync_force_parallel = |
| 3902 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 3903 | sync_force_parallel_show, sync_force_parallel_store); |
| 3904 | |
| 3905 | static ssize_t |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3906 | sync_speed_show(mddev_t *mddev, char *page) |
| 3907 | { |
| 3908 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3909 | if (mddev->curr_resync == 0) |
| 3910 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3911 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 3912 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3913 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3914 | db = resync - mddev->resync_mark_cnt; |
| 3915 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3916 | } |
| 3917 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3918 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3919 | |
| 3920 | static ssize_t |
| 3921 | sync_completed_show(mddev_t *mddev, char *page) |
| 3922 | { |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 3923 | unsigned long long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3924 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 3925 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3926 | return sprintf(page, "none\n"); |
| 3927 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3928 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3929 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3930 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3931 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3932 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 3933 | resync = mddev->curr_resync_completed; |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 3934 | return sprintf(page, "%llu / %llu\n", resync, max_sectors); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3935 | } |
| 3936 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3937 | static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3938 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3939 | static ssize_t |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3940 | min_sync_show(mddev_t *mddev, char *page) |
| 3941 | { |
| 3942 | return sprintf(page, "%llu\n", |
| 3943 | (unsigned long long)mddev->resync_min); |
| 3944 | } |
| 3945 | static ssize_t |
| 3946 | min_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3947 | { |
| 3948 | unsigned long long min; |
| 3949 | if (strict_strtoull(buf, 10, &min)) |
| 3950 | return -EINVAL; |
| 3951 | if (min > mddev->resync_max) |
| 3952 | return -EINVAL; |
| 3953 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3954 | return -EBUSY; |
| 3955 | |
| 3956 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3957 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 3958 | sector_t temp = min; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3959 | if (sector_div(temp, mddev->chunk_sectors)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3960 | return -EINVAL; |
| 3961 | } |
| 3962 | mddev->resync_min = min; |
| 3963 | |
| 3964 | return len; |
| 3965 | } |
| 3966 | |
| 3967 | static struct md_sysfs_entry md_min_sync = |
| 3968 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 3969 | |
| 3970 | static ssize_t |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3971 | max_sync_show(mddev_t *mddev, char *page) |
| 3972 | { |
| 3973 | if (mddev->resync_max == MaxSector) |
| 3974 | return sprintf(page, "max\n"); |
| 3975 | else |
| 3976 | return sprintf(page, "%llu\n", |
| 3977 | (unsigned long long)mddev->resync_max); |
| 3978 | } |
| 3979 | static ssize_t |
| 3980 | max_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3981 | { |
| 3982 | if (strncmp(buf, "max", 3) == 0) |
| 3983 | mddev->resync_max = MaxSector; |
| 3984 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3985 | unsigned long long max; |
| 3986 | if (strict_strtoull(buf, 10, &max)) |
| 3987 | return -EINVAL; |
| 3988 | if (max < mddev->resync_min) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3989 | return -EINVAL; |
| 3990 | if (max < mddev->resync_max && |
NeilBrown | 4d484a4 | 2009-08-13 10:41:50 +1000 | [diff] [blame] | 3991 | mddev->ro == 0 && |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3992 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3993 | return -EBUSY; |
| 3994 | |
| 3995 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3996 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 3997 | sector_t temp = max; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3998 | if (sector_div(temp, mddev->chunk_sectors)) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3999 | return -EINVAL; |
| 4000 | } |
| 4001 | mddev->resync_max = max; |
| 4002 | } |
| 4003 | wake_up(&mddev->recovery_wait); |
| 4004 | return len; |
| 4005 | } |
| 4006 | |
| 4007 | static struct md_sysfs_entry md_max_sync = |
| 4008 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 4009 | |
| 4010 | static ssize_t |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4011 | suspend_lo_show(mddev_t *mddev, char *page) |
| 4012 | { |
| 4013 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 4014 | } |
| 4015 | |
| 4016 | static ssize_t |
| 4017 | suspend_lo_store(mddev_t *mddev, const char *buf, size_t len) |
| 4018 | { |
| 4019 | char *e; |
| 4020 | unsigned long long new = simple_strtoull(buf, &e, 10); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4021 | unsigned long long old = mddev->suspend_lo; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4022 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 4023 | if (mddev->pers == NULL || |
| 4024 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4025 | return -EINVAL; |
| 4026 | if (buf == e || (*e && *e != '\n')) |
| 4027 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4028 | |
| 4029 | mddev->suspend_lo = new; |
| 4030 | if (new >= old) |
| 4031 | /* Shrinking suspended region */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4032 | mddev->pers->quiesce(mddev, 2); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4033 | else { |
| 4034 | /* Expanding suspended region - need to wait */ |
| 4035 | mddev->pers->quiesce(mddev, 1); |
| 4036 | mddev->pers->quiesce(mddev, 0); |
| 4037 | } |
| 4038 | return len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4039 | } |
| 4040 | static struct md_sysfs_entry md_suspend_lo = |
| 4041 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 4042 | |
| 4043 | |
| 4044 | static ssize_t |
| 4045 | suspend_hi_show(mddev_t *mddev, char *page) |
| 4046 | { |
| 4047 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 4048 | } |
| 4049 | |
| 4050 | static ssize_t |
| 4051 | suspend_hi_store(mddev_t *mddev, const char *buf, size_t len) |
| 4052 | { |
| 4053 | char *e; |
| 4054 | unsigned long long new = simple_strtoull(buf, &e, 10); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4055 | unsigned long long old = mddev->suspend_hi; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4056 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 4057 | if (mddev->pers == NULL || |
| 4058 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4059 | return -EINVAL; |
| 4060 | if (buf == e || (*e && *e != '\n')) |
| 4061 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4062 | |
| 4063 | mddev->suspend_hi = new; |
| 4064 | if (new <= old) |
| 4065 | /* Shrinking suspended region */ |
| 4066 | mddev->pers->quiesce(mddev, 2); |
| 4067 | else { |
| 4068 | /* Expanding suspended region - need to wait */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4069 | mddev->pers->quiesce(mddev, 1); |
| 4070 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4071 | } |
| 4072 | return len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4073 | } |
| 4074 | static struct md_sysfs_entry md_suspend_hi = |
| 4075 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 4076 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4077 | static ssize_t |
| 4078 | reshape_position_show(mddev_t *mddev, char *page) |
| 4079 | { |
| 4080 | if (mddev->reshape_position != MaxSector) |
| 4081 | return sprintf(page, "%llu\n", |
| 4082 | (unsigned long long)mddev->reshape_position); |
| 4083 | strcpy(page, "none\n"); |
| 4084 | return 5; |
| 4085 | } |
| 4086 | |
| 4087 | static ssize_t |
| 4088 | reshape_position_store(mddev_t *mddev, const char *buf, size_t len) |
| 4089 | { |
| 4090 | char *e; |
| 4091 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 4092 | if (mddev->pers) |
| 4093 | return -EBUSY; |
| 4094 | if (buf == e || (*e && *e != '\n')) |
| 4095 | return -EINVAL; |
| 4096 | mddev->reshape_position = new; |
| 4097 | mddev->delta_disks = 0; |
| 4098 | mddev->new_level = mddev->level; |
| 4099 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4100 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4101 | return len; |
| 4102 | } |
| 4103 | |
| 4104 | static struct md_sysfs_entry md_reshape_position = |
| 4105 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 4106 | reshape_position_store); |
| 4107 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4108 | static ssize_t |
| 4109 | array_size_show(mddev_t *mddev, char *page) |
| 4110 | { |
| 4111 | if (mddev->external_size) |
| 4112 | return sprintf(page, "%llu\n", |
| 4113 | (unsigned long long)mddev->array_sectors/2); |
| 4114 | else |
| 4115 | return sprintf(page, "default\n"); |
| 4116 | } |
| 4117 | |
| 4118 | static ssize_t |
| 4119 | array_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 4120 | { |
| 4121 | sector_t sectors; |
| 4122 | |
| 4123 | if (strncmp(buf, "default", 7) == 0) { |
| 4124 | if (mddev->pers) |
| 4125 | sectors = mddev->pers->size(mddev, 0, 0); |
| 4126 | else |
| 4127 | sectors = mddev->array_sectors; |
| 4128 | |
| 4129 | mddev->external_size = 0; |
| 4130 | } else { |
| 4131 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
| 4132 | return -EINVAL; |
| 4133 | if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
NeilBrown | 2b69c83 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4134 | return -E2BIG; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4135 | |
| 4136 | mddev->external_size = 1; |
| 4137 | } |
| 4138 | |
| 4139 | mddev->array_sectors = sectors; |
| 4140 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 4141 | if (mddev->pers) |
| 4142 | revalidate_disk(mddev->gendisk); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4143 | |
| 4144 | return len; |
| 4145 | } |
| 4146 | |
| 4147 | static struct md_sysfs_entry md_array_size = |
| 4148 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 4149 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4150 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4151 | static struct attribute *md_default_attrs[] = { |
| 4152 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4153 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4154 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4155 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4156 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4157 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4158 | &md_metadata.attr, |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4159 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4160 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4161 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4162 | &md_reshape_position.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4163 | &md_array_size.attr, |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4164 | &max_corr_read_errors.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4165 | NULL, |
| 4166 | }; |
| 4167 | |
| 4168 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4169 | &md_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4170 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4171 | &md_sync_min.attr, |
| 4172 | &md_sync_max.attr, |
| 4173 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4174 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4175 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4176 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4177 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4178 | &md_suspend_lo.attr, |
| 4179 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4180 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4181 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4182 | NULL, |
| 4183 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4184 | static struct attribute_group md_redundancy_group = { |
| 4185 | .name = NULL, |
| 4186 | .attrs = md_redundancy_attrs, |
| 4187 | }; |
| 4188 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4189 | |
| 4190 | static ssize_t |
| 4191 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 4192 | { |
| 4193 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 4194 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4195 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4196 | |
| 4197 | if (!entry->show) |
| 4198 | return -EIO; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4199 | rv = mddev_lock(mddev); |
| 4200 | if (!rv) { |
| 4201 | rv = entry->show(mddev, page); |
| 4202 | mddev_unlock(mddev); |
| 4203 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4204 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4205 | } |
| 4206 | |
| 4207 | static ssize_t |
| 4208 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 4209 | const char *page, size_t length) |
| 4210 | { |
| 4211 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 4212 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4213 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4214 | |
| 4215 | if (!entry->store) |
| 4216 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 4217 | if (!capable(CAP_SYS_ADMIN)) |
| 4218 | return -EACCES; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4219 | rv = mddev_lock(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4220 | if (mddev->hold_active == UNTIL_IOCTL) |
| 4221 | mddev->hold_active = 0; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4222 | if (!rv) { |
| 4223 | rv = entry->store(mddev, page, length); |
| 4224 | mddev_unlock(mddev); |
| 4225 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4226 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4227 | } |
| 4228 | |
| 4229 | static void md_free(struct kobject *ko) |
| 4230 | { |
| 4231 | mddev_t *mddev = container_of(ko, mddev_t, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4232 | |
| 4233 | if (mddev->sysfs_state) |
| 4234 | sysfs_put(mddev->sysfs_state); |
| 4235 | |
| 4236 | if (mddev->gendisk) { |
| 4237 | del_gendisk(mddev->gendisk); |
| 4238 | put_disk(mddev->gendisk); |
| 4239 | } |
| 4240 | if (mddev->queue) |
| 4241 | blk_cleanup_queue(mddev->queue); |
| 4242 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4243 | kfree(mddev); |
| 4244 | } |
| 4245 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 4246 | static const struct sysfs_ops md_sysfs_ops = { |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4247 | .show = md_attr_show, |
| 4248 | .store = md_attr_store, |
| 4249 | }; |
| 4250 | static struct kobj_type md_ktype = { |
| 4251 | .release = md_free, |
| 4252 | .sysfs_ops = &md_sysfs_ops, |
| 4253 | .default_attrs = md_default_attrs, |
| 4254 | }; |
| 4255 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4256 | int mdp_major = 0; |
| 4257 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4258 | static void mddev_delayed_delete(struct work_struct *ws) |
| 4259 | { |
| 4260 | mddev_t *mddev = container_of(ws, mddev_t, del_work); |
| 4261 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4262 | sysfs_remove_group(&mddev->kobj, &md_bitmap_group); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4263 | kobject_del(&mddev->kobj); |
| 4264 | kobject_put(&mddev->kobj); |
| 4265 | } |
| 4266 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4267 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4268 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4269 | static DEFINE_MUTEX(disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4270 | mddev_t *mddev = mddev_find(dev); |
| 4271 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4272 | int partitioned; |
| 4273 | int shift; |
| 4274 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4275 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4276 | |
| 4277 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4278 | return -ENODEV; |
| 4279 | |
| 4280 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 4281 | shift = partitioned ? MdpMinorShift : 0; |
| 4282 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4283 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4284 | /* wait for any previous instance of this device to be |
| 4285 | * completely removed (mddev_delayed_delete). |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4286 | */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4287 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4288 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4289 | mutex_lock(&disks_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4290 | error = -EEXIST; |
| 4291 | if (mddev->gendisk) |
| 4292 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4293 | |
| 4294 | if (name) { |
| 4295 | /* Need to ensure that 'name' is not a duplicate. |
| 4296 | */ |
| 4297 | mddev_t *mddev2; |
| 4298 | spin_lock(&all_mddevs_lock); |
| 4299 | |
| 4300 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 4301 | if (mddev2->gendisk && |
| 4302 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 4303 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4304 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4305 | } |
| 4306 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4307 | } |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4308 | |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4309 | error = -ENOMEM; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4310 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4311 | if (!mddev->queue) |
| 4312 | goto abort; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4313 | mddev->queue->queuedata = mddev; |
| 4314 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4315 | blk_queue_make_request(mddev->queue, md_make_request); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4316 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4317 | disk = alloc_disk(1 << shift); |
| 4318 | if (!disk) { |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4319 | blk_cleanup_queue(mddev->queue); |
| 4320 | mddev->queue = NULL; |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4321 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4322 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4323 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4324 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4325 | if (name) |
| 4326 | strcpy(disk->disk_name, name); |
| 4327 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4328 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 4329 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4330 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4331 | disk->fops = &md_fops; |
| 4332 | disk->private_data = mddev; |
| 4333 | disk->queue = mddev->queue; |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4334 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4335 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4336 | * remove it now. |
| 4337 | */ |
| 4338 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4339 | add_disk(disk); |
| 4340 | mddev->gendisk = disk; |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 4341 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
| 4342 | &disk_to_dev(disk)->kobj, "%s", "md"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4343 | if (error) { |
| 4344 | /* This isn't possible, but as kobject_init_and_add is marked |
| 4345 | * __must_check, we must do something with the result |
| 4346 | */ |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4347 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 4348 | disk->disk_name); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4349 | error = 0; |
| 4350 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4351 | if (mddev->kobj.sd && |
| 4352 | sysfs_create_group(&mddev->kobj, &md_bitmap_group)) |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4353 | printk(KERN_DEBUG "pointless warning\n"); |
Darrick J. Wong | be20e6c | 2010-11-24 16:40:33 +1100 | [diff] [blame] | 4354 | |
| 4355 | blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4356 | abort: |
| 4357 | mutex_unlock(&disks_mutex); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4358 | if (!error && mddev->kobj.sd) { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4359 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4360 | mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state"); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4361 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4362 | mddev_put(mddev); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4363 | return error; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4364 | } |
| 4365 | |
| 4366 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 4367 | { |
| 4368 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4369 | return NULL; |
| 4370 | } |
| 4371 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4372 | static int add_named_array(const char *val, struct kernel_param *kp) |
| 4373 | { |
| 4374 | /* val must be "md_*" where * is not all digits. |
| 4375 | * We allocate an array with a large free minor number, and |
| 4376 | * set the name to val. val must not already be an active name. |
| 4377 | */ |
| 4378 | int len = strlen(val); |
| 4379 | char buf[DISK_NAME_LEN]; |
| 4380 | |
| 4381 | while (len && val[len-1] == '\n') |
| 4382 | len--; |
| 4383 | if (len >= DISK_NAME_LEN) |
| 4384 | return -E2BIG; |
| 4385 | strlcpy(buf, val, len+1); |
| 4386 | if (strncmp(buf, "md_", 3) != 0) |
| 4387 | return -EINVAL; |
| 4388 | return md_alloc(0, buf); |
| 4389 | } |
| 4390 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4391 | static void md_safemode_timeout(unsigned long data) |
| 4392 | { |
| 4393 | mddev_t *mddev = (mddev_t *) data; |
| 4394 | |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4395 | if (!atomic_read(&mddev->writes_pending)) { |
| 4396 | mddev->safemode = 1; |
| 4397 | if (mddev->external) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4398 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4399 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4400 | md_wakeup_thread(mddev->thread); |
| 4401 | } |
| 4402 | |
NeilBrown | 6ff8d8e | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4403 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4404 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4405 | int md_run(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4406 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4407 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4408 | mdk_rdev_t *rdev; |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4409 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4410 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4411 | if (list_empty(&mddev->disks)) |
| 4412 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4413 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4414 | |
| 4415 | if (mddev->pers) |
| 4416 | return -EBUSY; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 4417 | /* Cannot run until previous stop completes properly */ |
| 4418 | if (mddev->sysfs_active) |
| 4419 | return -EBUSY; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 4420 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4421 | /* |
| 4422 | * Analyze all RAID superblock(s) |
| 4423 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4424 | if (!mddev->raid_disks) { |
| 4425 | if (!mddev->persistent) |
| 4426 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4427 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4428 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4429 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4430 | if (mddev->level != LEVEL_NONE) |
| 4431 | request_module("md-level-%d", mddev->level); |
| 4432 | else if (mddev->clevel[0]) |
| 4433 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4434 | |
| 4435 | /* |
| 4436 | * Drop all container device buffers, from now on |
| 4437 | * the only valid external interface is through the md |
| 4438 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4439 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4440 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4441 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4442 | continue; |
| 4443 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 4444 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4445 | |
| 4446 | /* perform some consistency tests on the device. |
| 4447 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4448 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4449 | */ |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4450 | if (rdev->meta_bdev) { |
| 4451 | /* Nothing to check */; |
| 4452 | } else if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4453 | if (mddev->dev_sectors && |
| 4454 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4455 | > rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4456 | printk("md: %s: data overlaps metadata\n", |
| 4457 | mdname(mddev)); |
| 4458 | return -EINVAL; |
| 4459 | } |
| 4460 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4461 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4462 | > rdev->data_offset) { |
| 4463 | printk("md: %s: metadata overlaps data\n", |
| 4464 | mdname(mddev)); |
| 4465 | return -EINVAL; |
| 4466 | } |
| 4467 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4468 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4469 | } |
| 4470 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 4471 | if (mddev->bio_set == NULL) |
| 4472 | mddev->bio_set = bioset_create(BIO_POOL_SIZE, sizeof(mddev)); |
| 4473 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4474 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4475 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4476 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4477 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4478 | if (mddev->level != LEVEL_NONE) |
| 4479 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 4480 | mddev->level); |
| 4481 | else |
| 4482 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 4483 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4484 | return -EINVAL; |
| 4485 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4486 | mddev->pers = pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4487 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4488 | if (mddev->level != pers->level) { |
| 4489 | mddev->level = pers->level; |
| 4490 | mddev->new_level = pers->level; |
| 4491 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4492 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4493 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4494 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4495 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4496 | /* This personality cannot handle reshaping... */ |
| 4497 | mddev->pers = NULL; |
| 4498 | module_put(pers->owner); |
| 4499 | return -EINVAL; |
| 4500 | } |
| 4501 | |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4502 | if (pers->sync_request) { |
| 4503 | /* Warn if this is a potentially silly |
| 4504 | * configuration. |
| 4505 | */ |
| 4506 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 4507 | mdk_rdev_t *rdev2; |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4508 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4509 | |
| 4510 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4511 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4512 | if (rdev < rdev2 && |
| 4513 | rdev->bdev->bd_contains == |
| 4514 | rdev2->bdev->bd_contains) { |
| 4515 | printk(KERN_WARNING |
| 4516 | "%s: WARNING: %s appears to be" |
| 4517 | " on the same physical disk as" |
| 4518 | " %s.\n", |
| 4519 | mdname(mddev), |
| 4520 | bdevname(rdev->bdev,b), |
| 4521 | bdevname(rdev2->bdev,b2)); |
| 4522 | warned = 1; |
| 4523 | } |
| 4524 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4525 | |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4526 | if (warned) |
| 4527 | printk(KERN_WARNING |
| 4528 | "True protection against single-disk" |
| 4529 | " failure might be compromised.\n"); |
| 4530 | } |
| 4531 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 4532 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4533 | /* may be over-ridden by personality */ |
| 4534 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 4535 | |
NeilBrown | 6ff8d8e | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4536 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4537 | |
NeilBrown | 0f9552b | 2009-12-30 12:08:50 +1100 | [diff] [blame] | 4538 | if (start_readonly && mddev->ro == 0) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4539 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 4540 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4541 | err = mddev->pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 4542 | if (err) |
| 4543 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4544 | else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) { |
| 4545 | WARN_ONCE(!mddev->external_size, "%s: default size too small," |
| 4546 | " but 'external_size' not in effect?\n", __func__); |
| 4547 | printk(KERN_ERR |
| 4548 | "md: invalid array_size %llu > default size %llu\n", |
| 4549 | (unsigned long long)mddev->array_sectors / 2, |
| 4550 | (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2); |
| 4551 | err = -EINVAL; |
| 4552 | mddev->pers->stop(mddev); |
| 4553 | } |
| 4554 | if (err == 0 && mddev->pers->sync_request) { |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4555 | err = bitmap_create(mddev); |
| 4556 | if (err) { |
| 4557 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 4558 | mdname(mddev), err); |
| 4559 | mddev->pers->stop(mddev); |
| 4560 | } |
| 4561 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4562 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4563 | module_put(mddev->pers->owner); |
| 4564 | mddev->pers = NULL; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4565 | bitmap_destroy(mddev); |
| 4566 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4567 | } |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4568 | if (mddev->pers->sync_request) { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4569 | if (mddev->kobj.sd && |
| 4570 | sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4571 | printk(KERN_WARNING |
| 4572 | "md: cannot register extra attributes for %s\n", |
| 4573 | mdname(mddev)); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4574 | mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4575 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 4576 | mddev->ro = 0; |
| 4577 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4578 | atomic_set(&mddev->writes_pending,0); |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4579 | atomic_set(&mddev->max_corr_read_errors, |
| 4580 | MD_DEFAULT_MAX_CORRECTED_READ_ERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4581 | mddev->safemode = 0; |
| 4582 | mddev->safemode_timer.function = md_safemode_timeout; |
| 4583 | mddev->safemode_timer.data = (unsigned long) mddev; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4584 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4585 | mddev->in_sync = 1; |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4586 | smp_wmb(); |
| 4587 | mddev->ready = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4588 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4589 | if (rdev->raid_disk >= 0) { |
| 4590 | char nm[20]; |
| 4591 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4592 | if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4593 | /* failure here is OK */; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4594 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4595 | |
| 4596 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4597 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4598 | if (mddev->flags) |
| 4599 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4600 | |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 4601 | md_wakeup_thread(mddev->thread); |
| 4602 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4603 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4604 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4605 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
| 4606 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4607 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4608 | return 0; |
| 4609 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4610 | EXPORT_SYMBOL_GPL(md_run); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4611 | |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 4612 | static int do_md_run(mddev_t *mddev) |
| 4613 | { |
| 4614 | int err; |
| 4615 | |
| 4616 | err = md_run(mddev); |
| 4617 | if (err) |
| 4618 | goto out; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 4619 | err = bitmap_load(mddev); |
| 4620 | if (err) { |
| 4621 | bitmap_destroy(mddev); |
| 4622 | goto out; |
| 4623 | } |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 4624 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 4625 | revalidate_disk(mddev->gendisk); |
| 4626 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
| 4627 | out: |
| 4628 | return err; |
| 4629 | } |
| 4630 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4631 | static int restart_array(mddev_t *mddev) |
| 4632 | { |
| 4633 | struct gendisk *disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4634 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4635 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4636 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4637 | return -ENXIO; |
| 4638 | if (!mddev->pers) |
| 4639 | return -EINVAL; |
| 4640 | if (!mddev->ro) |
| 4641 | return -EBUSY; |
| 4642 | mddev->safemode = 0; |
| 4643 | mddev->ro = 0; |
| 4644 | set_disk_ro(disk, 0); |
| 4645 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 4646 | mdname(mddev)); |
| 4647 | /* Kick recovery or resync if necessary */ |
| 4648 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4649 | md_wakeup_thread(mddev->thread); |
| 4650 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4651 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4652 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4653 | } |
| 4654 | |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4655 | /* similar to deny_write_access, but accounts for our holding a reference |
| 4656 | * to the file ourselves */ |
| 4657 | static int deny_bitmap_write_access(struct file * file) |
| 4658 | { |
| 4659 | struct inode *inode = file->f_mapping->host; |
| 4660 | |
| 4661 | spin_lock(&inode->i_lock); |
| 4662 | if (atomic_read(&inode->i_writecount) > 1) { |
| 4663 | spin_unlock(&inode->i_lock); |
| 4664 | return -ETXTBSY; |
| 4665 | } |
| 4666 | atomic_set(&inode->i_writecount, -1); |
| 4667 | spin_unlock(&inode->i_lock); |
| 4668 | |
| 4669 | return 0; |
| 4670 | } |
| 4671 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4672 | void restore_bitmap_write_access(struct file *file) |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4673 | { |
| 4674 | struct inode *inode = file->f_mapping->host; |
| 4675 | |
| 4676 | spin_lock(&inode->i_lock); |
| 4677 | atomic_set(&inode->i_writecount, 1); |
| 4678 | spin_unlock(&inode->i_lock); |
| 4679 | } |
| 4680 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4681 | static void md_clean(mddev_t *mddev) |
| 4682 | { |
| 4683 | mddev->array_sectors = 0; |
| 4684 | mddev->external_size = 0; |
| 4685 | mddev->dev_sectors = 0; |
| 4686 | mddev->raid_disks = 0; |
| 4687 | mddev->recovery_cp = 0; |
| 4688 | mddev->resync_min = 0; |
| 4689 | mddev->resync_max = MaxSector; |
| 4690 | mddev->reshape_position = MaxSector; |
| 4691 | mddev->external = 0; |
| 4692 | mddev->persistent = 0; |
| 4693 | mddev->level = LEVEL_NONE; |
| 4694 | mddev->clevel[0] = 0; |
| 4695 | mddev->flags = 0; |
| 4696 | mddev->ro = 0; |
| 4697 | mddev->metadata_type[0] = 0; |
| 4698 | mddev->chunk_sectors = 0; |
| 4699 | mddev->ctime = mddev->utime = 0; |
| 4700 | mddev->layout = 0; |
| 4701 | mddev->max_disks = 0; |
| 4702 | mddev->events = 0; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 4703 | mddev->can_decrease_events = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4704 | mddev->delta_disks = 0; |
| 4705 | mddev->new_level = LEVEL_NONE; |
| 4706 | mddev->new_layout = 0; |
| 4707 | mddev->new_chunk_sectors = 0; |
| 4708 | mddev->curr_resync = 0; |
| 4709 | mddev->resync_mismatches = 0; |
| 4710 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 4711 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 4712 | mddev->recovery = 0; |
| 4713 | mddev->in_sync = 0; |
| 4714 | mddev->degraded = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4715 | mddev->safemode = 0; |
| 4716 | mddev->bitmap_info.offset = 0; |
| 4717 | mddev->bitmap_info.default_offset = 0; |
| 4718 | mddev->bitmap_info.chunksize = 0; |
| 4719 | mddev->bitmap_info.daemon_sleep = 0; |
| 4720 | mddev->bitmap_info.max_write_behind = 0; |
NeilBrown | 252ac52 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 4721 | mddev->plug = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4722 | } |
| 4723 | |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4724 | static void __md_stop_writes(mddev_t *mddev) |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 4725 | { |
| 4726 | if (mddev->sync_thread) { |
| 4727 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4728 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4729 | reap_sync_thread(mddev); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 4730 | } |
| 4731 | |
| 4732 | del_timer_sync(&mddev->safemode_timer); |
| 4733 | |
| 4734 | bitmap_flush(mddev); |
| 4735 | md_super_wait(mddev); |
| 4736 | |
| 4737 | if (!mddev->in_sync || mddev->flags) { |
| 4738 | /* mark array as shutdown cleanly */ |
| 4739 | mddev->in_sync = 1; |
| 4740 | md_update_sb(mddev, 1); |
| 4741 | } |
| 4742 | } |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4743 | |
| 4744 | void md_stop_writes(mddev_t *mddev) |
| 4745 | { |
| 4746 | mddev_lock(mddev); |
| 4747 | __md_stop_writes(mddev); |
| 4748 | mddev_unlock(mddev); |
| 4749 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4750 | EXPORT_SYMBOL_GPL(md_stop_writes); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 4751 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4752 | void md_stop(mddev_t *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4753 | { |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4754 | mddev->ready = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4755 | mddev->pers->stop(mddev); |
| 4756 | if (mddev->pers->sync_request && mddev->to_remove == NULL) |
| 4757 | mddev->to_remove = &md_redundancy_group; |
| 4758 | module_put(mddev->pers->owner); |
| 4759 | mddev->pers = NULL; |
NeilBrown | cca9cf9 | 2010-04-01 12:08:16 +1100 | [diff] [blame] | 4760 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4761 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4762 | EXPORT_SYMBOL_GPL(md_stop); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4763 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4764 | static int md_set_readonly(mddev_t *mddev, int is_open) |
| 4765 | { |
| 4766 | int err = 0; |
| 4767 | mutex_lock(&mddev->open_mutex); |
| 4768 | if (atomic_read(&mddev->openers) > is_open) { |
| 4769 | printk("md: %s still in use.\n",mdname(mddev)); |
| 4770 | err = -EBUSY; |
| 4771 | goto out; |
| 4772 | } |
| 4773 | if (mddev->pers) { |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4774 | __md_stop_writes(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4775 | |
| 4776 | err = -ENXIO; |
| 4777 | if (mddev->ro==1) |
| 4778 | goto out; |
| 4779 | mddev->ro = 1; |
| 4780 | set_disk_ro(mddev->gendisk, 1); |
| 4781 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4782 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4783 | err = 0; |
| 4784 | } |
| 4785 | out: |
| 4786 | mutex_unlock(&mddev->open_mutex); |
| 4787 | return err; |
| 4788 | } |
| 4789 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4790 | /* mode: |
| 4791 | * 0 - completely stop and dis-assemble array |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4792 | * 2 - stop but do not disassemble array |
| 4793 | */ |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4794 | 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] | 4795 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4796 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 4797 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4798 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 4799 | mutex_lock(&mddev->open_mutex); |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 4800 | if (atomic_read(&mddev->openers) > is_open || |
| 4801 | mddev->sysfs_active) { |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4802 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 4803 | mutex_unlock(&mddev->open_mutex); |
| 4804 | return -EBUSY; |
| 4805 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4806 | |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 4807 | if (mddev->pers) { |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4808 | if (mddev->ro) |
| 4809 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4810 | |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4811 | __md_stop_writes(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4812 | md_stop(mddev); |
| 4813 | mddev->queue->merge_bvec_fn = NULL; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4814 | mddev->queue->backing_dev_info.congested_fn = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4815 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4816 | /* tell userspace to handle 'inactive' */ |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4817 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4818 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4819 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4820 | if (rdev->raid_disk >= 0) { |
| 4821 | char nm[20]; |
| 4822 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 4823 | sysfs_remove_link(&mddev->kobj, nm); |
| 4824 | } |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 4825 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4826 | set_capacity(disk, 0); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 4827 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4828 | revalidate_disk(disk); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4829 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4830 | if (mddev->ro) |
| 4831 | mddev->ro = 0; |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 4832 | } else |
| 4833 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4834 | /* |
| 4835 | * Free resources if final stop |
| 4836 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4837 | if (mode == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4838 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 4839 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4840 | bitmap_destroy(mddev); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4841 | if (mddev->bitmap_info.file) { |
| 4842 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 4843 | fput(mddev->bitmap_info.file); |
| 4844 | mddev->bitmap_info.file = NULL; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4845 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4846 | mddev->bitmap_info.offset = 0; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4847 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4848 | export_array(mddev); |
| 4849 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4850 | md_clean(mddev); |
NeilBrown | 934d9c2 | 2008-10-28 17:01:23 +1100 | [diff] [blame] | 4851 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4852 | if (mddev->hold_active == UNTIL_STOP) |
| 4853 | mddev->hold_active = 0; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4854 | } |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 4855 | blk_integrity_unregister(disk); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4856 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4857 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 4858 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4859 | } |
| 4860 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4861 | #ifndef MODULE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4862 | static void autorun_array(mddev_t *mddev) |
| 4863 | { |
| 4864 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4865 | int err; |
| 4866 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4867 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4868 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4869 | |
| 4870 | printk(KERN_INFO "md: running: "); |
| 4871 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4872 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4873 | char b[BDEVNAME_SIZE]; |
| 4874 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 4875 | } |
| 4876 | printk("\n"); |
| 4877 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4878 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4879 | if (err) { |
| 4880 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4881 | do_md_stop(mddev, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4882 | } |
| 4883 | } |
| 4884 | |
| 4885 | /* |
| 4886 | * lets try to run arrays based on all disks that have arrived |
| 4887 | * until now. (those are in pending_raid_disks) |
| 4888 | * |
| 4889 | * the method: pick the first pending disk, collect all disks with |
| 4890 | * the same UUID, remove all from the pending list and put them into |
| 4891 | * the 'same_array' list. Then order this list based on superblock |
| 4892 | * update time (freshest comes first), kick out 'old' disks and |
| 4893 | * compare superblocks. If everything's fine then run it. |
| 4894 | * |
| 4895 | * If "unit" is allocated, then bump its reference count |
| 4896 | */ |
| 4897 | static void autorun_devices(int part) |
| 4898 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4899 | mdk_rdev_t *rdev0, *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4900 | mddev_t *mddev; |
| 4901 | char b[BDEVNAME_SIZE]; |
| 4902 | |
| 4903 | printk(KERN_INFO "md: autorun ...\n"); |
| 4904 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4905 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4906 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 4907 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4908 | rdev0 = list_entry(pending_raid_disks.next, |
| 4909 | mdk_rdev_t, same_set); |
| 4910 | |
| 4911 | printk(KERN_INFO "md: considering %s ...\n", |
| 4912 | bdevname(rdev0->bdev,b)); |
| 4913 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4914 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4915 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 4916 | printk(KERN_INFO "md: adding %s ...\n", |
| 4917 | bdevname(rdev->bdev,b)); |
| 4918 | list_move(&rdev->same_set, &candidates); |
| 4919 | } |
| 4920 | /* |
| 4921 | * now we have a set of devices, with all of them having |
| 4922 | * mostly sane superblocks. It's time to allocate the |
| 4923 | * mddev. |
| 4924 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4925 | if (part) { |
| 4926 | dev = MKDEV(mdp_major, |
| 4927 | rdev0->preferred_minor << MdpMinorShift); |
| 4928 | unit = MINOR(dev) >> MdpMinorShift; |
| 4929 | } else { |
| 4930 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 4931 | unit = MINOR(dev); |
| 4932 | } |
| 4933 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4934 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 4935 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 4936 | break; |
| 4937 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4938 | |
| 4939 | md_probe(dev, NULL, NULL); |
| 4940 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 4941 | if (!mddev || !mddev->gendisk) { |
| 4942 | if (mddev) |
| 4943 | mddev_put(mddev); |
| 4944 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4945 | "md: cannot allocate memory for md drive.\n"); |
| 4946 | break; |
| 4947 | } |
| 4948 | if (mddev_lock(mddev)) |
| 4949 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 4950 | mdname(mddev)); |
| 4951 | else if (mddev->raid_disks || mddev->major_version |
| 4952 | || !list_empty(&mddev->disks)) { |
| 4953 | printk(KERN_WARNING |
| 4954 | "md: %s already running, cannot run %s\n", |
| 4955 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 4956 | mddev_unlock(mddev); |
| 4957 | } else { |
| 4958 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4959 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4960 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4961 | list_del_init(&rdev->same_set); |
| 4962 | if (bind_rdev_to_array(rdev, mddev)) |
| 4963 | export_rdev(rdev); |
| 4964 | } |
| 4965 | autorun_array(mddev); |
| 4966 | mddev_unlock(mddev); |
| 4967 | } |
| 4968 | /* on success, candidates will be empty, on error |
| 4969 | * it won't... |
| 4970 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4971 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4972 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4973 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4974 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4975 | mddev_put(mddev); |
| 4976 | } |
| 4977 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 4978 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4979 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4980 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4981 | static int get_version(void __user * arg) |
| 4982 | { |
| 4983 | mdu_version_t ver; |
| 4984 | |
| 4985 | ver.major = MD_MAJOR_VERSION; |
| 4986 | ver.minor = MD_MINOR_VERSION; |
| 4987 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 4988 | |
| 4989 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 4990 | return -EFAULT; |
| 4991 | |
| 4992 | return 0; |
| 4993 | } |
| 4994 | |
| 4995 | static int get_array_info(mddev_t * mddev, void __user * arg) |
| 4996 | { |
| 4997 | mdu_array_info_t info; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4998 | int nr,working,insync,failed,spare; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4999 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5000 | |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5001 | nr=working=insync=failed=spare=0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5002 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5003 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5004 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5005 | failed++; |
| 5006 | else { |
| 5007 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5008 | if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5009 | insync++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5010 | else |
| 5011 | spare++; |
| 5012 | } |
| 5013 | } |
| 5014 | |
| 5015 | info.major_version = mddev->major_version; |
| 5016 | info.minor_version = mddev->minor_version; |
| 5017 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 5018 | info.ctime = mddev->ctime; |
| 5019 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5020 | info.size = mddev->dev_sectors / 2; |
| 5021 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 5022 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5023 | info.nr_disks = nr; |
| 5024 | info.raid_disks = mddev->raid_disks; |
| 5025 | info.md_minor = mddev->md_minor; |
| 5026 | info.not_persistent= !mddev->persistent; |
| 5027 | |
| 5028 | info.utime = mddev->utime; |
| 5029 | info.state = 0; |
| 5030 | if (mddev->in_sync) |
| 5031 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5032 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5033 | info.state = (1<<MD_SB_BITMAP_PRESENT); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5034 | info.active_disks = insync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5035 | info.working_disks = working; |
| 5036 | info.failed_disks = failed; |
| 5037 | info.spare_disks = spare; |
| 5038 | |
| 5039 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5040 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5041 | |
| 5042 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5043 | return -EFAULT; |
| 5044 | |
| 5045 | return 0; |
| 5046 | } |
| 5047 | |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 5048 | static int get_bitmap_file(mddev_t * mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5049 | { |
| 5050 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
| 5051 | char *ptr, *buf = NULL; |
| 5052 | int err = -ENOMEM; |
| 5053 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5054 | if (md_allow_write(mddev)) |
| 5055 | file = kmalloc(sizeof(*file), GFP_NOIO); |
| 5056 | else |
| 5057 | file = kmalloc(sizeof(*file), GFP_KERNEL); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5058 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5059 | if (!file) |
| 5060 | goto out; |
| 5061 | |
| 5062 | /* bitmap disabled, zero the first byte and copy out */ |
| 5063 | if (!mddev->bitmap || !mddev->bitmap->file) { |
| 5064 | file->pathname[0] = '\0'; |
| 5065 | goto copy_out; |
| 5066 | } |
| 5067 | |
| 5068 | buf = kmalloc(sizeof(file->pathname), GFP_KERNEL); |
| 5069 | if (!buf) |
| 5070 | goto out; |
| 5071 | |
Christoph Hellwig | 6bcfd60 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 5072 | ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname)); |
| 5073 | if (IS_ERR(ptr)) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5074 | goto out; |
| 5075 | |
| 5076 | strcpy(file->pathname, ptr); |
| 5077 | |
| 5078 | copy_out: |
| 5079 | err = 0; |
| 5080 | if (copy_to_user(arg, file, sizeof(*file))) |
| 5081 | err = -EFAULT; |
| 5082 | out: |
| 5083 | kfree(buf); |
| 5084 | kfree(file); |
| 5085 | return err; |
| 5086 | } |
| 5087 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5088 | static int get_disk_info(mddev_t * mddev, void __user * arg) |
| 5089 | { |
| 5090 | mdu_disk_info_t info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5091 | mdk_rdev_t *rdev; |
| 5092 | |
| 5093 | if (copy_from_user(&info, arg, sizeof(info))) |
| 5094 | return -EFAULT; |
| 5095 | |
Andre Noll | 26ef379 | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5096 | rdev = find_rdev_nr(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5097 | if (rdev) { |
| 5098 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 5099 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 5100 | info.raid_disk = rdev->raid_disk; |
| 5101 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5102 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5103 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5104 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5105 | info.state |= (1<<MD_DISK_ACTIVE); |
| 5106 | info.state |= (1<<MD_DISK_SYNC); |
| 5107 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5108 | if (test_bit(WriteMostly, &rdev->flags)) |
| 5109 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5110 | } else { |
| 5111 | info.major = info.minor = 0; |
| 5112 | info.raid_disk = -1; |
| 5113 | info.state = (1<<MD_DISK_REMOVED); |
| 5114 | } |
| 5115 | |
| 5116 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5117 | return -EFAULT; |
| 5118 | |
| 5119 | return 0; |
| 5120 | } |
| 5121 | |
| 5122 | static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info) |
| 5123 | { |
| 5124 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 5125 | mdk_rdev_t *rdev; |
| 5126 | dev_t dev = MKDEV(info->major,info->minor); |
| 5127 | |
| 5128 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 5129 | return -EOVERFLOW; |
| 5130 | |
| 5131 | if (!mddev->raid_disks) { |
| 5132 | int err; |
| 5133 | /* expecting a device which has a superblock */ |
| 5134 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 5135 | if (IS_ERR(rdev)) { |
| 5136 | printk(KERN_WARNING |
| 5137 | "md: md_import_device returned %ld\n", |
| 5138 | PTR_ERR(rdev)); |
| 5139 | return PTR_ERR(rdev); |
| 5140 | } |
| 5141 | if (!list_empty(&mddev->disks)) { |
| 5142 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 5143 | mdk_rdev_t, same_set); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5144 | err = super_types[mddev->major_version] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5145 | .load_super(rdev, rdev0, mddev->minor_version); |
| 5146 | if (err < 0) { |
| 5147 | printk(KERN_WARNING |
| 5148 | "md: %s has different UUID to %s\n", |
| 5149 | bdevname(rdev->bdev,b), |
| 5150 | bdevname(rdev0->bdev,b2)); |
| 5151 | export_rdev(rdev); |
| 5152 | return -EINVAL; |
| 5153 | } |
| 5154 | } |
| 5155 | err = bind_rdev_to_array(rdev, mddev); |
| 5156 | if (err) |
| 5157 | export_rdev(rdev); |
| 5158 | return err; |
| 5159 | } |
| 5160 | |
| 5161 | /* |
| 5162 | * add_new_disk can be used once the array is assembled |
| 5163 | * to add "hot spares". They must already have a superblock |
| 5164 | * written |
| 5165 | */ |
| 5166 | if (mddev->pers) { |
| 5167 | int err; |
| 5168 | if (!mddev->pers->hot_add_disk) { |
| 5169 | printk(KERN_WARNING |
| 5170 | "%s: personality does not support diskops!\n", |
| 5171 | mdname(mddev)); |
| 5172 | return -EINVAL; |
| 5173 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 5174 | if (mddev->persistent) |
| 5175 | rdev = md_import_device(dev, mddev->major_version, |
| 5176 | mddev->minor_version); |
| 5177 | else |
| 5178 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5179 | if (IS_ERR(rdev)) { |
| 5180 | printk(KERN_WARNING |
| 5181 | "md: md_import_device returned %ld\n", |
| 5182 | PTR_ERR(rdev)); |
| 5183 | return PTR_ERR(rdev); |
| 5184 | } |
NeilBrown | 1a855a0 | 2010-12-09 16:36:28 +1100 | [diff] [blame] | 5185 | /* set saved_raid_disk if appropriate */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5186 | if (!mddev->persistent) { |
| 5187 | if (info->state & (1<<MD_DISK_SYNC) && |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 5188 | info->raid_disk < mddev->raid_disks) { |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5189 | rdev->raid_disk = info->raid_disk; |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 5190 | set_bit(In_sync, &rdev->flags); |
| 5191 | } else |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5192 | rdev->raid_disk = -1; |
| 5193 | } else |
| 5194 | super_types[mddev->major_version]. |
| 5195 | validate_super(mddev, rdev); |
NeilBrown | 1a855a0 | 2010-12-09 16:36:28 +1100 | [diff] [blame] | 5196 | if (test_bit(In_sync, &rdev->flags)) |
| 5197 | rdev->saved_raid_disk = rdev->raid_disk; |
| 5198 | else |
| 5199 | rdev->saved_raid_disk = -1; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5200 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5201 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5202 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5203 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5204 | else |
| 5205 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5206 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5207 | rdev->raid_disk = -1; |
| 5208 | err = bind_rdev_to_array(rdev, mddev); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 5209 | if (!err && !mddev->pers->hot_remove_disk) { |
| 5210 | /* If there is hot_add_disk but no hot_remove_disk |
| 5211 | * then added disks for geometry changes, |
| 5212 | * and should be added immediately. |
| 5213 | */ |
| 5214 | super_types[mddev->major_version]. |
| 5215 | validate_super(mddev, rdev); |
| 5216 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 5217 | if (err) |
| 5218 | unbind_rdev_from_array(rdev); |
| 5219 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5220 | if (err) |
| 5221 | export_rdev(rdev); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 5222 | else |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5223 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5224 | |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 5225 | md_update_sb(mddev, 1); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 5226 | if (mddev->degraded) |
| 5227 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5228 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 5229 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5230 | return err; |
| 5231 | } |
| 5232 | |
| 5233 | /* otherwise, add_new_disk is only allowed |
| 5234 | * for major_version==0 superblocks |
| 5235 | */ |
| 5236 | if (mddev->major_version != 0) { |
| 5237 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 5238 | mdname(mddev)); |
| 5239 | return -EINVAL; |
| 5240 | } |
| 5241 | |
| 5242 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 5243 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5244 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5245 | if (IS_ERR(rdev)) { |
| 5246 | printk(KERN_WARNING |
| 5247 | "md: error, md_import_device() returned %ld\n", |
| 5248 | PTR_ERR(rdev)); |
| 5249 | return PTR_ERR(rdev); |
| 5250 | } |
| 5251 | rdev->desc_nr = info->number; |
| 5252 | if (info->raid_disk < mddev->raid_disks) |
| 5253 | rdev->raid_disk = info->raid_disk; |
| 5254 | else |
| 5255 | rdev->raid_disk = -1; |
| 5256 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5257 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5258 | if (info->state & (1<<MD_DISK_SYNC)) |
| 5259 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5260 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5261 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5262 | set_bit(WriteMostly, &rdev->flags); |
| 5263 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5264 | if (!mddev->persistent) { |
| 5265 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 5266 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
| 5267 | } else |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5268 | rdev->sb_start = calc_dev_sboffset(rdev); |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5269 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5270 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5271 | err = bind_rdev_to_array(rdev, mddev); |
| 5272 | if (err) { |
| 5273 | export_rdev(rdev); |
| 5274 | return err; |
| 5275 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5276 | } |
| 5277 | |
| 5278 | return 0; |
| 5279 | } |
| 5280 | |
| 5281 | static int hot_remove_disk(mddev_t * mddev, dev_t dev) |
| 5282 | { |
| 5283 | char b[BDEVNAME_SIZE]; |
| 5284 | mdk_rdev_t *rdev; |
| 5285 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5286 | rdev = find_rdev(mddev, dev); |
| 5287 | if (!rdev) |
| 5288 | return -ENXIO; |
| 5289 | |
| 5290 | if (rdev->raid_disk >= 0) |
| 5291 | goto busy; |
| 5292 | |
| 5293 | kick_rdev_from_array(rdev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5294 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5295 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5296 | |
| 5297 | return 0; |
| 5298 | busy: |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 5299 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5300 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5301 | return -EBUSY; |
| 5302 | } |
| 5303 | |
| 5304 | static int hot_add_disk(mddev_t * mddev, dev_t dev) |
| 5305 | { |
| 5306 | char b[BDEVNAME_SIZE]; |
| 5307 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5308 | mdk_rdev_t *rdev; |
| 5309 | |
| 5310 | if (!mddev->pers) |
| 5311 | return -ENODEV; |
| 5312 | |
| 5313 | if (mddev->major_version != 0) { |
| 5314 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 5315 | " version-0 superblocks.\n", |
| 5316 | mdname(mddev)); |
| 5317 | return -EINVAL; |
| 5318 | } |
| 5319 | if (!mddev->pers->hot_add_disk) { |
| 5320 | printk(KERN_WARNING |
| 5321 | "%s: personality does not support diskops!\n", |
| 5322 | mdname(mddev)); |
| 5323 | return -EINVAL; |
| 5324 | } |
| 5325 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5326 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5327 | if (IS_ERR(rdev)) { |
| 5328 | printk(KERN_WARNING |
| 5329 | "md: error, md_import_device() returned %ld\n", |
| 5330 | PTR_ERR(rdev)); |
| 5331 | return -EINVAL; |
| 5332 | } |
| 5333 | |
| 5334 | if (mddev->persistent) |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5335 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5336 | else |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 5337 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5338 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5339 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5340 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5341 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5342 | printk(KERN_WARNING |
| 5343 | "md: can not hot-add faulty %s disk to %s!\n", |
| 5344 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5345 | err = -EINVAL; |
| 5346 | goto abort_export; |
| 5347 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5348 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5349 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 5350 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5351 | err = bind_rdev_to_array(rdev, mddev); |
| 5352 | if (err) |
| 5353 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5354 | |
| 5355 | /* |
| 5356 | * The rest should better be atomic, we can have disk failures |
| 5357 | * noticed in interrupt contexts ... |
| 5358 | */ |
| 5359 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5360 | rdev->raid_disk = -1; |
| 5361 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5362 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5363 | |
| 5364 | /* |
| 5365 | * Kick recovery, maybe this spare has to be added to the |
| 5366 | * array immediately. |
| 5367 | */ |
| 5368 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5369 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5370 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5371 | return 0; |
| 5372 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5373 | abort_export: |
| 5374 | export_rdev(rdev); |
| 5375 | return err; |
| 5376 | } |
| 5377 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5378 | static int set_bitmap_file(mddev_t *mddev, int fd) |
| 5379 | { |
| 5380 | int err; |
| 5381 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5382 | if (mddev->pers) { |
| 5383 | if (!mddev->pers->quiesce) |
| 5384 | return -EBUSY; |
| 5385 | if (mddev->recovery || mddev->sync_thread) |
| 5386 | return -EBUSY; |
| 5387 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5388 | } |
| 5389 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5390 | |
| 5391 | if (fd >= 0) { |
| 5392 | if (mddev->bitmap) |
| 5393 | return -EEXIST; /* cannot add when bitmap is present */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5394 | mddev->bitmap_info.file = fget(fd); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5395 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5396 | if (mddev->bitmap_info.file == NULL) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5397 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 5398 | mdname(mddev)); |
| 5399 | return -EBADF; |
| 5400 | } |
| 5401 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5402 | err = deny_bitmap_write_access(mddev->bitmap_info.file); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5403 | if (err) { |
| 5404 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 5405 | mdname(mddev)); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5406 | fput(mddev->bitmap_info.file); |
| 5407 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5408 | return err; |
| 5409 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5410 | mddev->bitmap_info.offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5411 | } else if (mddev->bitmap == NULL) |
| 5412 | return -ENOENT; /* cannot remove what isn't there */ |
| 5413 | err = 0; |
| 5414 | if (mddev->pers) { |
| 5415 | mddev->pers->quiesce(mddev, 1); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5416 | if (fd >= 0) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5417 | err = bitmap_create(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5418 | if (!err) |
| 5419 | err = bitmap_load(mddev); |
| 5420 | } |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5421 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5422 | bitmap_destroy(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5423 | fd = -1; /* make sure to put the file */ |
| 5424 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5425 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5426 | } |
| 5427 | if (fd < 0) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5428 | if (mddev->bitmap_info.file) { |
| 5429 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 5430 | fput(mddev->bitmap_info.file); |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 5431 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5432 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5433 | } |
| 5434 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5435 | return err; |
| 5436 | } |
| 5437 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5438 | /* |
| 5439 | * set_array_info is used two different ways |
| 5440 | * The original usage is when creating a new array. |
| 5441 | * In this usage, raid_disks is > 0 and it together with |
| 5442 | * level, size, not_persistent,layout,chunksize determine the |
| 5443 | * shape of the array. |
| 5444 | * This will always create an array with a type-0.90.0 superblock. |
| 5445 | * The newer usage is when assembling an array. |
| 5446 | * In this case raid_disks will be 0, and the major_version field is |
| 5447 | * use to determine which style super-blocks are to be found on the devices. |
| 5448 | * The minor and patch _version numbers are also kept incase the |
| 5449 | * super_block handler wishes to interpret them. |
| 5450 | */ |
| 5451 | static int set_array_info(mddev_t * mddev, mdu_array_info_t *info) |
| 5452 | { |
| 5453 | |
| 5454 | if (info->raid_disks == 0) { |
| 5455 | /* just setting version number for superblock loading */ |
| 5456 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 5457 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5458 | super_types[info->major_version].name == NULL) { |
| 5459 | /* maybe try to auto-load a module? */ |
| 5460 | printk(KERN_INFO |
| 5461 | "md: superblock version %d not known\n", |
| 5462 | info->major_version); |
| 5463 | return -EINVAL; |
| 5464 | } |
| 5465 | mddev->major_version = info->major_version; |
| 5466 | mddev->minor_version = info->minor_version; |
| 5467 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5468 | mddev->persistent = !info->not_persistent; |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 5469 | /* ensure mddev_put doesn't delete this now that there |
| 5470 | * is some minimal configuration. |
| 5471 | */ |
| 5472 | mddev->ctime = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5473 | return 0; |
| 5474 | } |
| 5475 | mddev->major_version = MD_MAJOR_VERSION; |
| 5476 | mddev->minor_version = MD_MINOR_VERSION; |
| 5477 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 5478 | mddev->ctime = get_seconds(); |
| 5479 | |
| 5480 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 5481 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5482 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5483 | mddev->raid_disks = info->raid_disks; |
| 5484 | /* don't set md_minor, it is determined by which /dev/md* was |
| 5485 | * openned |
| 5486 | */ |
| 5487 | if (info->state & (1<<MD_SB_CLEAN)) |
| 5488 | mddev->recovery_cp = MaxSector; |
| 5489 | else |
| 5490 | mddev->recovery_cp = 0; |
| 5491 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5492 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5493 | |
| 5494 | mddev->layout = info->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5495 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5496 | |
| 5497 | mddev->max_disks = MD_SB_DISKS; |
| 5498 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5499 | if (mddev->persistent) |
| 5500 | mddev->flags = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5501 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5502 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5503 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
| 5504 | mddev->bitmap_info.offset = 0; |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 5505 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5506 | mddev->reshape_position = MaxSector; |
| 5507 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5508 | /* |
| 5509 | * Generate a 128 bit UUID |
| 5510 | */ |
| 5511 | get_random_bytes(mddev->uuid, 16); |
| 5512 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5513 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5514 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5515 | mddev->new_layout = mddev->layout; |
| 5516 | mddev->delta_disks = 0; |
| 5517 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5518 | return 0; |
| 5519 | } |
| 5520 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5521 | void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors) |
| 5522 | { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5523 | WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__); |
| 5524 | |
| 5525 | if (mddev->external_size) |
| 5526 | return; |
| 5527 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5528 | mddev->array_sectors = array_sectors; |
| 5529 | } |
| 5530 | EXPORT_SYMBOL(md_set_array_sectors); |
| 5531 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5532 | static int update_size(mddev_t *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5533 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5534 | mdk_rdev_t *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5535 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5536 | int fit = (num_sectors == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5537 | |
| 5538 | if (mddev->pers->resize == NULL) |
| 5539 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5540 | /* The "num_sectors" is the number of sectors of each device that |
| 5541 | * is used. This can only make sense for arrays with redundancy. |
| 5542 | * linear and raid0 always use whatever space is available. We can only |
| 5543 | * consider changing this number if no resync or reconstruction is |
| 5544 | * 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] | 5545 | * 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] | 5546 | * of each device. If num_sectors is zero, we find the largest size |
| 5547 | * that fits. |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5548 | */ |
| 5549 | if (mddev->sync_thread) |
| 5550 | return -EBUSY; |
NeilBrown | dba034e | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 5551 | if (mddev->bitmap) |
| 5552 | /* Sorry, cannot grow a bitmap yet, just remove it, |
| 5553 | * grow, and re-add. |
| 5554 | */ |
| 5555 | return -EBUSY; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5556 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5557 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 5558 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5559 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 5560 | num_sectors = avail; |
| 5561 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5562 | return -ENOSPC; |
| 5563 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5564 | rv = mddev->pers->resize(mddev, num_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5565 | if (!rv) |
| 5566 | revalidate_disk(mddev->gendisk); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5567 | return rv; |
| 5568 | } |
| 5569 | |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5570 | static int update_raid_disks(mddev_t *mddev, int raid_disks) |
| 5571 | { |
| 5572 | int rv; |
| 5573 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5574 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5575 | return -EINVAL; |
| 5576 | if (raid_disks <= 0 || |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 5577 | (mddev->max_disks && raid_disks >= mddev->max_disks)) |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5578 | return -EINVAL; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5579 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5580 | return -EBUSY; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5581 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
| 5582 | |
| 5583 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | de171cb | 2011-01-31 11:57:42 +1100 | [diff] [blame] | 5584 | if (rv < 0) |
| 5585 | mddev->delta_disks = 0; |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5586 | return rv; |
| 5587 | } |
| 5588 | |
| 5589 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5590 | /* |
| 5591 | * update_array_info is used to change the configuration of an |
| 5592 | * on-line array. |
| 5593 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 5594 | * fields in the info are checked against the array. |
| 5595 | * Any differences that cannot be handled will cause an error. |
| 5596 | * Normally, only one change can be managed at a time. |
| 5597 | */ |
| 5598 | static int update_array_info(mddev_t *mddev, mdu_array_info_t *info) |
| 5599 | { |
| 5600 | int rv = 0; |
| 5601 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5602 | int state = 0; |
| 5603 | |
| 5604 | /* calculate expected state,ignoring low bits */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5605 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5606 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5607 | |
| 5608 | if (mddev->major_version != info->major_version || |
| 5609 | mddev->minor_version != info->minor_version || |
| 5610 | /* mddev->patch_version != info->patch_version || */ |
| 5611 | mddev->ctime != info->ctime || |
| 5612 | mddev->level != info->level || |
| 5613 | /* mddev->layout != info->layout || */ |
| 5614 | !mddev->persistent != info->not_persistent|| |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5615 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5616 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 5617 | ((state^info->state) & 0xfffffe00) |
| 5618 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5619 | return -EINVAL; |
| 5620 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5621 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 5622 | cnt++; |
| 5623 | if (mddev->raid_disks != info->raid_disks) |
| 5624 | cnt++; |
| 5625 | if (mddev->layout != info->layout) |
| 5626 | cnt++; |
| 5627 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 5628 | cnt++; |
| 5629 | if (cnt == 0) |
| 5630 | return 0; |
| 5631 | if (cnt > 1) |
| 5632 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5633 | |
| 5634 | if (mddev->layout != info->layout) { |
| 5635 | /* Change layout |
| 5636 | * we don't need to do anything at the md level, the |
| 5637 | * personality will take care of it all. |
| 5638 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5639 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5640 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5641 | else { |
| 5642 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5643 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5644 | if (rv) |
| 5645 | mddev->new_layout = mddev->layout; |
| 5646 | return rv; |
| 5647 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5648 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5649 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5650 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5651 | |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5652 | if (mddev->raid_disks != info->raid_disks) |
| 5653 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5654 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5655 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5656 | if (mddev->pers->quiesce == NULL) |
| 5657 | return -EINVAL; |
| 5658 | if (mddev->recovery || mddev->sync_thread) |
| 5659 | return -EBUSY; |
| 5660 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5661 | /* add the bitmap */ |
| 5662 | if (mddev->bitmap) |
| 5663 | return -EEXIST; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5664 | if (mddev->bitmap_info.default_offset == 0) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5665 | return -EINVAL; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5666 | mddev->bitmap_info.offset = |
| 5667 | mddev->bitmap_info.default_offset; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5668 | mddev->pers->quiesce(mddev, 1); |
| 5669 | rv = bitmap_create(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5670 | if (!rv) |
| 5671 | rv = bitmap_load(mddev); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5672 | if (rv) |
| 5673 | bitmap_destroy(mddev); |
| 5674 | mddev->pers->quiesce(mddev, 0); |
| 5675 | } else { |
| 5676 | /* remove the bitmap */ |
| 5677 | if (!mddev->bitmap) |
| 5678 | return -ENOENT; |
| 5679 | if (mddev->bitmap->file) |
| 5680 | return -EINVAL; |
| 5681 | mddev->pers->quiesce(mddev, 1); |
| 5682 | bitmap_destroy(mddev); |
| 5683 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5684 | mddev->bitmap_info.offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5685 | } |
| 5686 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5687 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5688 | return rv; |
| 5689 | } |
| 5690 | |
| 5691 | static int set_disk_faulty(mddev_t *mddev, dev_t dev) |
| 5692 | { |
| 5693 | mdk_rdev_t *rdev; |
| 5694 | |
| 5695 | if (mddev->pers == NULL) |
| 5696 | return -ENODEV; |
| 5697 | |
| 5698 | rdev = find_rdev(mddev, dev); |
| 5699 | if (!rdev) |
| 5700 | return -ENODEV; |
| 5701 | |
| 5702 | md_error(mddev, rdev); |
| 5703 | return 0; |
| 5704 | } |
| 5705 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 5706 | /* |
| 5707 | * We have a problem here : there is no easy way to give a CHS |
| 5708 | * virtual geometry. We currently pretend that we have a 2 heads |
| 5709 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 5710 | * dosfs just mad... ;-) |
| 5711 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5712 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 5713 | { |
| 5714 | mddev_t *mddev = bdev->bd_disk->private_data; |
| 5715 | |
| 5716 | geo->heads = 2; |
| 5717 | geo->sectors = 4; |
NeilBrown | 49ce6ce | 2010-03-29 10:51:42 +1100 | [diff] [blame] | 5718 | geo->cylinders = mddev->array_sectors / 8; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5719 | return 0; |
| 5720 | } |
| 5721 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5722 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5723 | unsigned int cmd, unsigned long arg) |
| 5724 | { |
| 5725 | int err = 0; |
| 5726 | void __user *argp = (void __user *)arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5727 | mddev_t *mddev = NULL; |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 5728 | int ro; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5729 | |
| 5730 | if (!capable(CAP_SYS_ADMIN)) |
| 5731 | return -EACCES; |
| 5732 | |
| 5733 | /* |
| 5734 | * Commands dealing with the RAID driver but not any |
| 5735 | * particular array: |
| 5736 | */ |
| 5737 | switch (cmd) |
| 5738 | { |
| 5739 | case RAID_VERSION: |
| 5740 | err = get_version(argp); |
| 5741 | goto done; |
| 5742 | |
| 5743 | case PRINT_RAID_DEBUG: |
| 5744 | err = 0; |
| 5745 | md_print_devices(); |
| 5746 | goto done; |
| 5747 | |
| 5748 | #ifndef MODULE |
| 5749 | case RAID_AUTORUN: |
| 5750 | err = 0; |
| 5751 | autostart_arrays(arg); |
| 5752 | goto done; |
| 5753 | #endif |
| 5754 | default:; |
| 5755 | } |
| 5756 | |
| 5757 | /* |
| 5758 | * Commands creating/starting a new array: |
| 5759 | */ |
| 5760 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5761 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5762 | |
| 5763 | if (!mddev) { |
| 5764 | BUG(); |
| 5765 | goto abort; |
| 5766 | } |
| 5767 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5768 | err = mddev_lock(mddev); |
| 5769 | if (err) { |
| 5770 | printk(KERN_INFO |
| 5771 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 5772 | err, cmd); |
| 5773 | goto abort; |
| 5774 | } |
| 5775 | |
| 5776 | switch (cmd) |
| 5777 | { |
| 5778 | case SET_ARRAY_INFO: |
| 5779 | { |
| 5780 | mdu_array_info_t info; |
| 5781 | if (!arg) |
| 5782 | memset(&info, 0, sizeof(info)); |
| 5783 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 5784 | err = -EFAULT; |
| 5785 | goto abort_unlock; |
| 5786 | } |
| 5787 | if (mddev->pers) { |
| 5788 | err = update_array_info(mddev, &info); |
| 5789 | if (err) { |
| 5790 | printk(KERN_WARNING "md: couldn't update" |
| 5791 | " array info. %d\n", err); |
| 5792 | goto abort_unlock; |
| 5793 | } |
| 5794 | goto done_unlock; |
| 5795 | } |
| 5796 | if (!list_empty(&mddev->disks)) { |
| 5797 | printk(KERN_WARNING |
| 5798 | "md: array %s already has disks!\n", |
| 5799 | mdname(mddev)); |
| 5800 | err = -EBUSY; |
| 5801 | goto abort_unlock; |
| 5802 | } |
| 5803 | if (mddev->raid_disks) { |
| 5804 | printk(KERN_WARNING |
| 5805 | "md: array %s already initialised!\n", |
| 5806 | mdname(mddev)); |
| 5807 | err = -EBUSY; |
| 5808 | goto abort_unlock; |
| 5809 | } |
| 5810 | err = set_array_info(mddev, &info); |
| 5811 | if (err) { |
| 5812 | printk(KERN_WARNING "md: couldn't set" |
| 5813 | " array info. %d\n", err); |
| 5814 | goto abort_unlock; |
| 5815 | } |
| 5816 | } |
| 5817 | goto done_unlock; |
| 5818 | |
| 5819 | default:; |
| 5820 | } |
| 5821 | |
| 5822 | /* |
| 5823 | * Commands querying/configuring an existing array: |
| 5824 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5825 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5826 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 5827 | if ((!mddev->raid_disks && !mddev->external) |
| 5828 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 5829 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 5830 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5831 | err = -ENODEV; |
| 5832 | goto abort_unlock; |
| 5833 | } |
| 5834 | |
| 5835 | /* |
| 5836 | * Commands even a read-only array can execute: |
| 5837 | */ |
| 5838 | switch (cmd) |
| 5839 | { |
| 5840 | case GET_ARRAY_INFO: |
| 5841 | err = get_array_info(mddev, argp); |
| 5842 | goto done_unlock; |
| 5843 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5844 | case GET_BITMAP_FILE: |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 5845 | err = get_bitmap_file(mddev, argp); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5846 | goto done_unlock; |
| 5847 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5848 | case GET_DISK_INFO: |
| 5849 | err = get_disk_info(mddev, argp); |
| 5850 | goto done_unlock; |
| 5851 | |
| 5852 | case RESTART_ARRAY_RW: |
| 5853 | err = restart_array(mddev); |
| 5854 | goto done_unlock; |
| 5855 | |
| 5856 | case STOP_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5857 | err = do_md_stop(mddev, 0, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5858 | goto done_unlock; |
| 5859 | |
| 5860 | case STOP_ARRAY_RO: |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5861 | err = md_set_readonly(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5862 | goto done_unlock; |
| 5863 | |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 5864 | case BLKROSET: |
| 5865 | if (get_user(ro, (int __user *)(arg))) { |
| 5866 | err = -EFAULT; |
| 5867 | goto done_unlock; |
| 5868 | } |
| 5869 | err = -EINVAL; |
| 5870 | |
| 5871 | /* if the bdev is going readonly the value of mddev->ro |
| 5872 | * does not matter, no writes are coming |
| 5873 | */ |
| 5874 | if (ro) |
| 5875 | goto done_unlock; |
| 5876 | |
| 5877 | /* are we are already prepared for writes? */ |
| 5878 | if (mddev->ro != 1) |
| 5879 | goto done_unlock; |
| 5880 | |
| 5881 | /* transitioning to readauto need only happen for |
| 5882 | * arrays that call md_write_start |
| 5883 | */ |
| 5884 | if (mddev->pers) { |
| 5885 | err = restart_array(mddev); |
| 5886 | if (err == 0) { |
| 5887 | mddev->ro = 2; |
| 5888 | set_disk_ro(mddev->gendisk, 0); |
| 5889 | } |
| 5890 | } |
| 5891 | goto done_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5892 | } |
| 5893 | |
| 5894 | /* |
| 5895 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5896 | * superblock, so we do not allow them on read-only arrays. |
| 5897 | * However non-MD ioctls (e.g. get-size) will still come through |
| 5898 | * here and hit the 'default' below, so only disallow |
| 5899 | * 'md' ioctls, and switch to rw mode if started auto-readonly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5900 | */ |
Andre Noll | bb57fc6 | 2008-04-25 19:06:35 +0200 | [diff] [blame] | 5901 | if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5902 | if (mddev->ro == 2) { |
| 5903 | mddev->ro = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5904 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5905 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5906 | md_wakeup_thread(mddev->thread); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5907 | } else { |
| 5908 | err = -EROFS; |
| 5909 | goto abort_unlock; |
| 5910 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5911 | } |
| 5912 | |
| 5913 | switch (cmd) |
| 5914 | { |
| 5915 | case ADD_NEW_DISK: |
| 5916 | { |
| 5917 | mdu_disk_info_t info; |
| 5918 | if (copy_from_user(&info, argp, sizeof(info))) |
| 5919 | err = -EFAULT; |
| 5920 | else |
| 5921 | err = add_new_disk(mddev, &info); |
| 5922 | goto done_unlock; |
| 5923 | } |
| 5924 | |
| 5925 | case HOT_REMOVE_DISK: |
| 5926 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
| 5927 | goto done_unlock; |
| 5928 | |
| 5929 | case HOT_ADD_DISK: |
| 5930 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
| 5931 | goto done_unlock; |
| 5932 | |
| 5933 | case SET_DISK_FAULTY: |
| 5934 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
| 5935 | goto done_unlock; |
| 5936 | |
| 5937 | case RUN_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5938 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5939 | goto done_unlock; |
| 5940 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5941 | case SET_BITMAP_FILE: |
| 5942 | err = set_bitmap_file(mddev, (int)arg); |
| 5943 | goto done_unlock; |
| 5944 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5945 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5946 | err = -EINVAL; |
| 5947 | goto abort_unlock; |
| 5948 | } |
| 5949 | |
| 5950 | done_unlock: |
| 5951 | abort_unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5952 | if (mddev->hold_active == UNTIL_IOCTL && |
| 5953 | err != -EINVAL) |
| 5954 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5955 | mddev_unlock(mddev); |
| 5956 | |
| 5957 | return err; |
| 5958 | done: |
| 5959 | if (err) |
| 5960 | MD_BUG(); |
| 5961 | abort: |
| 5962 | return err; |
| 5963 | } |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 5964 | #ifdef CONFIG_COMPAT |
| 5965 | static int md_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 5966 | unsigned int cmd, unsigned long arg) |
| 5967 | { |
| 5968 | switch (cmd) { |
| 5969 | case HOT_REMOVE_DISK: |
| 5970 | case HOT_ADD_DISK: |
| 5971 | case SET_DISK_FAULTY: |
| 5972 | case SET_BITMAP_FILE: |
| 5973 | /* These take in integer arg, do not convert */ |
| 5974 | break; |
| 5975 | default: |
| 5976 | arg = (unsigned long)compat_ptr(arg); |
| 5977 | break; |
| 5978 | } |
| 5979 | |
| 5980 | return md_ioctl(bdev, mode, cmd, arg); |
| 5981 | } |
| 5982 | #endif /* CONFIG_COMPAT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5983 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5984 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5985 | { |
| 5986 | /* |
| 5987 | * Succeed if we can lock the mddev, which confirms that |
| 5988 | * it isn't being stopped right now. |
| 5989 | */ |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5990 | mddev_t *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5991 | int err; |
| 5992 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5993 | if (mddev->gendisk != bdev->bd_disk) { |
| 5994 | /* we are racing with mddev_put which is discarding this |
| 5995 | * bd_disk. |
| 5996 | */ |
| 5997 | mddev_put(mddev); |
| 5998 | /* Wait until bdev->bd_disk is definitely gone */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 5999 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6000 | /* Then retry the open from the top */ |
| 6001 | return -ERESTARTSYS; |
| 6002 | } |
| 6003 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 6004 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 6005 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6006 | goto out; |
| 6007 | |
| 6008 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6009 | atomic_inc(&mddev->openers); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 6010 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6011 | |
NeilBrown | f3b99be | 2010-06-24 13:31:03 +1000 | [diff] [blame] | 6012 | check_disk_size_change(mddev->gendisk, bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6013 | out: |
| 6014 | return err; |
| 6015 | } |
| 6016 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6017 | static int md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6018 | { |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6019 | mddev_t *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6020 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 6021 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6022 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6023 | mddev_put(mddev); |
| 6024 | |
| 6025 | return 0; |
| 6026 | } |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 6027 | static const struct block_device_operations md_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6028 | { |
| 6029 | .owner = THIS_MODULE, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6030 | .open = md_open, |
| 6031 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 6032 | .ioctl = md_ioctl, |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 6033 | #ifdef CONFIG_COMPAT |
| 6034 | .compat_ioctl = md_compat_ioctl, |
| 6035 | #endif |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6036 | .getgeo = md_getgeo, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6037 | }; |
| 6038 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6039 | static int md_thread(void * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6040 | { |
| 6041 | mdk_thread_t *thread = arg; |
| 6042 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6043 | /* |
| 6044 | * md_thread is a 'system-thread', it's priority should be very |
| 6045 | * high. We avoid resource deadlocks individually in each |
| 6046 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 6047 | * the very same RT priority as kswapd, thus we will never get |
| 6048 | * into a priority inversion deadlock. |
| 6049 | * |
| 6050 | * we definitely have to have equal or higher priority than |
| 6051 | * bdflush, otherwise bdflush will deadlock if there are too |
| 6052 | * many dirty RAID5 blocks. |
| 6053 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6054 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 6055 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6056 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6057 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 6058 | /* We need to wait INTERRUPTIBLE so that |
| 6059 | * we don't add to the load-average. |
| 6060 | * That means we need to be sure no signals are |
| 6061 | * pending |
| 6062 | */ |
| 6063 | if (signal_pending(current)) |
| 6064 | flush_signals(current); |
| 6065 | |
| 6066 | wait_event_interruptible_timeout |
| 6067 | (thread->wqueue, |
| 6068 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 6069 | || kthread_should_stop(), |
| 6070 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6071 | |
NeilBrown | 6c98791 | 2011-01-14 09:13:53 +1100 | [diff] [blame] | 6072 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 6073 | if (!kthread_should_stop()) |
NeilBrown | 589a594 | 2010-12-09 17:02:14 +1100 | [diff] [blame] | 6074 | thread->run(thread->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6075 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6076 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6077 | return 0; |
| 6078 | } |
| 6079 | |
| 6080 | void md_wakeup_thread(mdk_thread_t *thread) |
| 6081 | { |
| 6082 | if (thread) { |
| 6083 | dprintk("md: waking up MD thread %s.\n", thread->tsk->comm); |
| 6084 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 6085 | wake_up(&thread->wqueue); |
| 6086 | } |
| 6087 | } |
| 6088 | |
| 6089 | mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev, |
| 6090 | const char *name) |
| 6091 | { |
| 6092 | mdk_thread_t *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6093 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 6094 | thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6095 | if (!thread) |
| 6096 | return NULL; |
| 6097 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6098 | init_waitqueue_head(&thread->wqueue); |
| 6099 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6100 | thread->run = run; |
| 6101 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6102 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 6103 | thread->tsk = kthread_run(md_thread, thread, |
| 6104 | "%s_%s", |
| 6105 | mdname(thread->mddev), |
| 6106 | name ?: mddev->pers->name); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6107 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6108 | kfree(thread); |
| 6109 | return NULL; |
| 6110 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6111 | return thread; |
| 6112 | } |
| 6113 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6114 | void md_unregister_thread(mdk_thread_t *thread) |
| 6115 | { |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6116 | if (!thread) |
| 6117 | return; |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 6118 | dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6119 | |
| 6120 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6121 | kfree(thread); |
| 6122 | } |
| 6123 | |
| 6124 | void md_error(mddev_t *mddev, mdk_rdev_t *rdev) |
| 6125 | { |
| 6126 | if (!mddev) { |
| 6127 | MD_BUG(); |
| 6128 | return; |
| 6129 | } |
| 6130 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6131 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6132 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6133 | |
| 6134 | if (mddev->external) |
| 6135 | set_bit(Blocked, &rdev->flags); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6136 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6137 | dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n", |
| 6138 | mdname(mddev), |
| 6139 | MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev), |
| 6140 | __builtin_return_address(0),__builtin_return_address(1), |
| 6141 | __builtin_return_address(2),__builtin_return_address(3)); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6142 | */ |
Andrew Morton | d0a0a5e | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 6143 | if (!mddev->pers) |
| 6144 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6145 | if (!mddev->pers->error_handler) |
| 6146 | return; |
| 6147 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6148 | if (mddev->degraded) |
| 6149 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6150 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6151 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6152 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6153 | md_wakeup_thread(mddev->thread); |
NeilBrown | 768a418 | 2010-07-26 11:49:55 +1000 | [diff] [blame] | 6154 | if (mddev->event_work.func) |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 6155 | queue_work(md_misc_wq, &mddev->event_work); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 6156 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6157 | } |
| 6158 | |
| 6159 | /* seq_file implementation /proc/mdstat */ |
| 6160 | |
| 6161 | static void status_unused(struct seq_file *seq) |
| 6162 | { |
| 6163 | int i = 0; |
| 6164 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6165 | |
| 6166 | seq_printf(seq, "unused devices: "); |
| 6167 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6168 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6169 | char b[BDEVNAME_SIZE]; |
| 6170 | i++; |
| 6171 | seq_printf(seq, "%s ", |
| 6172 | bdevname(rdev->bdev,b)); |
| 6173 | } |
| 6174 | if (!i) |
| 6175 | seq_printf(seq, "<none>"); |
| 6176 | |
| 6177 | seq_printf(seq, "\n"); |
| 6178 | } |
| 6179 | |
| 6180 | |
| 6181 | static void status_resync(struct seq_file *seq, mddev_t * mddev) |
| 6182 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6183 | sector_t max_sectors, resync, res; |
| 6184 | unsigned long dt, db; |
| 6185 | sector_t rt; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6186 | int scale; |
| 6187 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6188 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6189 | resync = mddev->curr_resync - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6190 | |
| 6191 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6192 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6193 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6194 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6195 | |
| 6196 | /* |
| 6197 | * Should not happen. |
| 6198 | */ |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6199 | if (!max_sectors) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6200 | MD_BUG(); |
| 6201 | return; |
| 6202 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6203 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6204 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6205 | * u32, as those are the requirements for sector_div. |
| 6206 | * Thus 'scale' must be at least 10 |
| 6207 | */ |
| 6208 | scale = 10; |
| 6209 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6210 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6211 | scale++; |
| 6212 | } |
| 6213 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6214 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6215 | |
| 6216 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6217 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6218 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6219 | seq_printf(seq, "["); |
| 6220 | for (i = 0; i < x; i++) |
| 6221 | seq_printf(seq, "="); |
| 6222 | seq_printf(seq, ">"); |
| 6223 | for (i = 0; i < y; i++) |
| 6224 | seq_printf(seq, "."); |
| 6225 | seq_printf(seq, "] "); |
| 6226 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6227 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6228 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 6229 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6230 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 6231 | "check" : |
| 6232 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 6233 | "resync" : "recovery"))), |
| 6234 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6235 | (unsigned long long) resync/2, |
| 6236 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6237 | |
| 6238 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6239 | * dt: time from mark until now |
| 6240 | * db: blocks written from mark until now |
| 6241 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6242 | * |
| 6243 | * rt is a sector_t, so could be 32bit or 64bit. |
| 6244 | * So we divide before multiply in case it is 32bit and close |
| 6245 | * to the limit. |
| 6246 | * We scale the divisor (db) by 32 to avoid loosing precision |
| 6247 | * near the end of resync when the number of remaining sectors |
| 6248 | * is close to 'db'. |
| 6249 | * We then divide rt by 32 after multiplying by db to compensate. |
| 6250 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6251 | */ |
| 6252 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 6253 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6254 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 6255 | - mddev->resync_mark_cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6256 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6257 | rt = max_sectors - resync; /* number of remaining sectors */ |
| 6258 | sector_div(rt, db/32+1); |
| 6259 | rt *= dt; |
| 6260 | rt >>= 5; |
| 6261 | |
| 6262 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 6263 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6264 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6265 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6266 | } |
| 6267 | |
| 6268 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 6269 | { |
| 6270 | struct list_head *tmp; |
| 6271 | loff_t l = *pos; |
| 6272 | mddev_t *mddev; |
| 6273 | |
| 6274 | if (l >= 0x10000) |
| 6275 | return NULL; |
| 6276 | if (!l--) |
| 6277 | /* header */ |
| 6278 | return (void*)1; |
| 6279 | |
| 6280 | spin_lock(&all_mddevs_lock); |
| 6281 | list_for_each(tmp,&all_mddevs) |
| 6282 | if (!l--) { |
| 6283 | mddev = list_entry(tmp, mddev_t, all_mddevs); |
| 6284 | mddev_get(mddev); |
| 6285 | spin_unlock(&all_mddevs_lock); |
| 6286 | return mddev; |
| 6287 | } |
| 6288 | spin_unlock(&all_mddevs_lock); |
| 6289 | if (!l--) |
| 6290 | return (void*)2;/* tail */ |
| 6291 | return NULL; |
| 6292 | } |
| 6293 | |
| 6294 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 6295 | { |
| 6296 | struct list_head *tmp; |
| 6297 | mddev_t *next_mddev, *mddev = v; |
| 6298 | |
| 6299 | ++*pos; |
| 6300 | if (v == (void*)2) |
| 6301 | return NULL; |
| 6302 | |
| 6303 | spin_lock(&all_mddevs_lock); |
| 6304 | if (v == (void*)1) |
| 6305 | tmp = all_mddevs.next; |
| 6306 | else |
| 6307 | tmp = mddev->all_mddevs.next; |
| 6308 | if (tmp != &all_mddevs) |
| 6309 | next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs)); |
| 6310 | else { |
| 6311 | next_mddev = (void*)2; |
| 6312 | *pos = 0x10000; |
| 6313 | } |
| 6314 | spin_unlock(&all_mddevs_lock); |
| 6315 | |
| 6316 | if (v != (void*)1) |
| 6317 | mddev_put(mddev); |
| 6318 | return next_mddev; |
| 6319 | |
| 6320 | } |
| 6321 | |
| 6322 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 6323 | { |
| 6324 | mddev_t *mddev = v; |
| 6325 | |
| 6326 | if (mddev && v != (void*)1 && v != (void*)2) |
| 6327 | mddev_put(mddev); |
| 6328 | } |
| 6329 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6330 | struct mdstat_info { |
| 6331 | int event; |
| 6332 | }; |
| 6333 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6334 | static int md_seq_show(struct seq_file *seq, void *v) |
| 6335 | { |
| 6336 | mddev_t *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6337 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6338 | mdk_rdev_t *rdev; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6339 | struct mdstat_info *mi = seq->private; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6340 | struct bitmap *bitmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6341 | |
| 6342 | if (v == (void*)1) { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6343 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6344 | seq_printf(seq, "Personalities : "); |
| 6345 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6346 | list_for_each_entry(pers, &pers_list, list) |
| 6347 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6348 | |
| 6349 | spin_unlock(&pers_lock); |
| 6350 | seq_printf(seq, "\n"); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6351 | mi->event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6352 | return 0; |
| 6353 | } |
| 6354 | if (v == (void*)2) { |
| 6355 | status_unused(seq); |
| 6356 | return 0; |
| 6357 | } |
| 6358 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6359 | if (mddev_lock(mddev) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6360 | return -EINTR; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6361 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6362 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 6363 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 6364 | mddev->pers ? "" : "in"); |
| 6365 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6366 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6367 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6368 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 6369 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6370 | seq_printf(seq, " %s", mddev->pers->name); |
| 6371 | } |
| 6372 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6373 | sectors = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6374 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6375 | char b[BDEVNAME_SIZE]; |
| 6376 | seq_printf(seq, " %s[%d]", |
| 6377 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6378 | if (test_bit(WriteMostly, &rdev->flags)) |
| 6379 | seq_printf(seq, "(W)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6380 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6381 | seq_printf(seq, "(F)"); |
| 6382 | continue; |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6383 | } else if (rdev->raid_disk < 0) |
| 6384 | seq_printf(seq, "(S)"); /* spare */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6385 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6386 | } |
| 6387 | |
| 6388 | if (!list_empty(&mddev->disks)) { |
| 6389 | if (mddev->pers) |
| 6390 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 6391 | (unsigned long long) |
| 6392 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6393 | else |
| 6394 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6395 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6396 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6397 | if (mddev->persistent) { |
| 6398 | if (mddev->major_version != 0 || |
| 6399 | mddev->minor_version != 90) { |
| 6400 | seq_printf(seq," super %d.%d", |
| 6401 | mddev->major_version, |
| 6402 | mddev->minor_version); |
| 6403 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6404 | } else if (mddev->external) |
| 6405 | seq_printf(seq, " super external:%s", |
| 6406 | mddev->metadata_type); |
| 6407 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6408 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6409 | |
| 6410 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6411 | mddev->pers->status(seq, mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6412 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6413 | if (mddev->pers->sync_request) { |
| 6414 | if (mddev->curr_resync > 2) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6415 | status_resync(seq, mddev); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6416 | seq_printf(seq, "\n "); |
| 6417 | } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2) |
| 6418 | seq_printf(seq, "\tresync=DELAYED\n "); |
| 6419 | else if (mddev->recovery_cp < MaxSector) |
| 6420 | seq_printf(seq, "\tresync=PENDING\n "); |
| 6421 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6422 | } else |
| 6423 | seq_printf(seq, "\n "); |
| 6424 | |
| 6425 | if ((bitmap = mddev->bitmap)) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6426 | unsigned long chunk_kb; |
| 6427 | unsigned long flags; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6428 | spin_lock_irqsave(&bitmap->lock, flags); |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 6429 | chunk_kb = mddev->bitmap_info.chunksize >> 10; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6430 | seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], " |
| 6431 | "%lu%s chunk", |
| 6432 | bitmap->pages - bitmap->missing_pages, |
| 6433 | bitmap->pages, |
| 6434 | (bitmap->pages - bitmap->missing_pages) |
| 6435 | << (PAGE_SHIFT - 10), |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 6436 | chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize, |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6437 | chunk_kb ? "KB" : "B"); |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 6438 | if (bitmap->file) { |
| 6439 | seq_printf(seq, ", file: "); |
Jan Blunck | c32c2f6 | 2008-02-14 19:38:43 -0800 | [diff] [blame] | 6440 | seq_path(seq, &bitmap->file->f_path, " \t\n"); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6441 | } |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 6442 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6443 | seq_printf(seq, "\n"); |
| 6444 | spin_unlock_irqrestore(&bitmap->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6445 | } |
| 6446 | |
| 6447 | seq_printf(seq, "\n"); |
| 6448 | } |
| 6449 | mddev_unlock(mddev); |
| 6450 | |
| 6451 | return 0; |
| 6452 | } |
| 6453 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 6454 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6455 | .start = md_seq_start, |
| 6456 | .next = md_seq_next, |
| 6457 | .stop = md_seq_stop, |
| 6458 | .show = md_seq_show, |
| 6459 | }; |
| 6460 | |
| 6461 | static int md_seq_open(struct inode *inode, struct file *file) |
| 6462 | { |
| 6463 | int error; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6464 | struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL); |
| 6465 | if (mi == NULL) |
| 6466 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6467 | |
| 6468 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6469 | if (error) |
| 6470 | kfree(mi); |
| 6471 | else { |
| 6472 | struct seq_file *p = file->private_data; |
| 6473 | p->private = mi; |
| 6474 | mi->event = atomic_read(&md_event_count); |
| 6475 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6476 | return error; |
| 6477 | } |
| 6478 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6479 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 6480 | { |
| 6481 | struct seq_file *m = filp->private_data; |
| 6482 | struct mdstat_info *mi = m->private; |
| 6483 | int mask; |
| 6484 | |
| 6485 | poll_wait(filp, &md_event_waiters, wait); |
| 6486 | |
| 6487 | /* always allow read */ |
| 6488 | mask = POLLIN | POLLRDNORM; |
| 6489 | |
| 6490 | if (mi->event != atomic_read(&md_event_count)) |
| 6491 | mask |= POLLERR | POLLPRI; |
| 6492 | return mask; |
| 6493 | } |
| 6494 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 6495 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 6496 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6497 | .open = md_seq_open, |
| 6498 | .read = seq_read, |
| 6499 | .llseek = seq_lseek, |
Martin Peschke | c3f94b4 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 6500 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6501 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6502 | }; |
| 6503 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6504 | int register_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6505 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6506 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6507 | list_add_tail(&p->list, &pers_list); |
| 6508 | 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] | 6509 | spin_unlock(&pers_lock); |
| 6510 | return 0; |
| 6511 | } |
| 6512 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6513 | int unregister_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6514 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6515 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6516 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6517 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6518 | spin_unlock(&pers_lock); |
| 6519 | return 0; |
| 6520 | } |
| 6521 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6522 | static int is_mddev_idle(mddev_t *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6523 | { |
| 6524 | mdk_rdev_t * rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6525 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6526 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6527 | |
| 6528 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6529 | rcu_read_lock(); |
| 6530 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6531 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6532 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
| 6533 | (int)part_stat_read(&disk->part0, sectors[1]) - |
| 6534 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 6535 | /* sync IO will cause sync_io to increase before the disk_stats |
| 6536 | * as sync_io is counted when a request starts, and |
| 6537 | * disk_stats is counted when it completes. |
| 6538 | * So resync activity will cause curr_events to be smaller than |
| 6539 | * when there was no such activity. |
| 6540 | * non-sync IO will cause disk_stat to increase without |
| 6541 | * increasing sync_io so curr_events will (eventually) |
| 6542 | * be larger than it was before. Once it becomes |
| 6543 | * substantially larger, the test below will cause |
| 6544 | * the array to appear non-idle, and resync will slow |
| 6545 | * down. |
| 6546 | * If there is a lot of outstanding resync activity when |
| 6547 | * we set last_event to curr_events, then all that activity |
| 6548 | * completing might cause the array to appear non-idle |
| 6549 | * and resync will be slowed down even though there might |
| 6550 | * not have been non-resync activity. This will only |
| 6551 | * happen once though. 'last_events' will soon reflect |
| 6552 | * the state where there is little or no outstanding |
| 6553 | * resync requests, and further resync activity will |
| 6554 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6555 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6556 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6557 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6558 | rdev->last_events = curr_events; |
| 6559 | idle = 0; |
| 6560 | } |
| 6561 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6562 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6563 | return idle; |
| 6564 | } |
| 6565 | |
| 6566 | void md_done_sync(mddev_t *mddev, int blocks, int ok) |
| 6567 | { |
| 6568 | /* another "blocks" (512byte) blocks have been synced */ |
| 6569 | atomic_sub(blocks, &mddev->recovery_active); |
| 6570 | wake_up(&mddev->recovery_wait); |
| 6571 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6572 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6573 | md_wakeup_thread(mddev->thread); |
| 6574 | // stop recovery, signal do_sync .... |
| 6575 | } |
| 6576 | } |
| 6577 | |
| 6578 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6579 | /* md_write_start(mddev, bi) |
| 6580 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6581 | * in superblock) before writing, schedule a superblock update |
| 6582 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6583 | */ |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6584 | void md_write_start(mddev_t *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6585 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6586 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6587 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6588 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6589 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6590 | BUG_ON(mddev->ro == 1); |
| 6591 | if (mddev->ro == 2) { |
| 6592 | /* need to switch to read/write */ |
| 6593 | mddev->ro = 0; |
| 6594 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6595 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 6596 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6597 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6598 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6599 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6600 | if (mddev->safemode == 1) |
| 6601 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6602 | if (mddev->in_sync) { |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6603 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6604 | if (mddev->in_sync) { |
| 6605 | mddev->in_sync = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6606 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 6607 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6608 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6609 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6610 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6611 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6612 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6613 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6614 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 6615 | wait_event(mddev->sb_wait, |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 6616 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6617 | } |
| 6618 | |
| 6619 | void md_write_end(mddev_t *mddev) |
| 6620 | { |
| 6621 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 6622 | if (mddev->safemode == 2) |
| 6623 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 6624 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6625 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 6626 | } |
| 6627 | } |
| 6628 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6629 | /* md_allow_write(mddev) |
| 6630 | * Calling this ensures that the array is marked 'active' so that writes |
| 6631 | * may proceed without blocking. It is important to call this before |
| 6632 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 6633 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6634 | * |
| 6635 | * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock |
| 6636 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6637 | */ |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6638 | int md_allow_write(mddev_t *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6639 | { |
| 6640 | if (!mddev->pers) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6641 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6642 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6643 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 6644 | if (!mddev->pers->sync_request) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6645 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6646 | |
| 6647 | spin_lock_irq(&mddev->write_lock); |
| 6648 | if (mddev->in_sync) { |
| 6649 | mddev->in_sync = 0; |
| 6650 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 6651 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6652 | if (mddev->safemode_delay && |
| 6653 | mddev->safemode == 0) |
| 6654 | mddev->safemode = 1; |
| 6655 | spin_unlock_irq(&mddev->write_lock); |
| 6656 | md_update_sb(mddev, 0); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6657 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6658 | } else |
| 6659 | spin_unlock_irq(&mddev->write_lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6660 | |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 6661 | if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6662 | return -EAGAIN; |
| 6663 | else |
| 6664 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6665 | } |
| 6666 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 6667 | |
NeilBrown | b63d7c2 | 2010-06-01 19:37:33 +1000 | [diff] [blame] | 6668 | void md_unplug(mddev_t *mddev) |
NeilBrown | 252ac52 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 6669 | { |
NeilBrown | 252ac52 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 6670 | if (mddev->plug) |
| 6671 | mddev->plug->unplug_fn(mddev->plug); |
| 6672 | } |
| 6673 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6674 | #define SYNC_MARKS 10 |
| 6675 | #define SYNC_MARK_STEP (3*HZ) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6676 | void md_do_sync(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6677 | { |
| 6678 | mddev_t *mddev2; |
| 6679 | unsigned int currspeed = 0, |
| 6680 | window; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6681 | sector_t max_sectors,j, io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6682 | unsigned long mark[SYNC_MARKS]; |
| 6683 | sector_t mark_cnt[SYNC_MARKS]; |
| 6684 | int last_mark,m; |
| 6685 | struct list_head *tmp; |
| 6686 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6687 | int skipped = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6688 | mdk_rdev_t *rdev; |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6689 | char *desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6690 | |
| 6691 | /* just incase thread restarts... */ |
| 6692 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 6693 | return; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6694 | if (mddev->ro) /* never try to sync a read-only array */ |
| 6695 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6696 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6697 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6698 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 6699 | desc = "data-check"; |
| 6700 | else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6701 | desc = "requested-resync"; |
| 6702 | else |
| 6703 | desc = "resync"; |
| 6704 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 6705 | desc = "reshape"; |
| 6706 | else |
| 6707 | desc = "recovery"; |
| 6708 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6709 | /* we overload curr_resync somewhat here. |
| 6710 | * 0 == not engaged in resync at all |
| 6711 | * 2 == checking that there is no conflict with another sync |
| 6712 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 6713 | * commense |
| 6714 | * other == active in resync - this many blocks |
| 6715 | * |
| 6716 | * Before starting a resync we must have set curr_resync to |
| 6717 | * 2, and then checked that every "conflicting" array has curr_resync |
| 6718 | * less than ours. When we find one that is the same or higher |
| 6719 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 6720 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 6721 | * This will mean we have to start checking from the beginning again. |
| 6722 | * |
| 6723 | */ |
| 6724 | |
| 6725 | do { |
| 6726 | mddev->curr_resync = 2; |
| 6727 | |
| 6728 | try_again: |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 6729 | if (kthread_should_stop()) |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 6730 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 6731 | |
| 6732 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6733 | goto skip; |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6734 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6735 | if (mddev2 == mddev) |
| 6736 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 6737 | if (!mddev->parallel_resync |
| 6738 | && mddev2->curr_resync |
| 6739 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6740 | DEFINE_WAIT(wq); |
| 6741 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 6742 | /* arbitrarily yield */ |
| 6743 | mddev->curr_resync = 1; |
| 6744 | wake_up(&resync_wait); |
| 6745 | } |
| 6746 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 6747 | /* no need to wait here, we can wait the next |
| 6748 | * time 'round when curr_resync == 2 |
| 6749 | */ |
| 6750 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 6751 | /* We need to wait 'interruptible' so as not to |
| 6752 | * contribute to the load average, and not to |
| 6753 | * be caught by 'softlockup' |
| 6754 | */ |
| 6755 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 6756 | if (!kthread_should_stop() && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 6757 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6758 | printk(KERN_INFO "md: delaying %s of %s" |
| 6759 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6760 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6761 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6762 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 6763 | if (signal_pending(current)) |
| 6764 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6765 | schedule(); |
| 6766 | finish_wait(&resync_wait, &wq); |
| 6767 | goto try_again; |
| 6768 | } |
| 6769 | finish_wait(&resync_wait, &wq); |
| 6770 | } |
| 6771 | } |
| 6772 | } while (mddev->curr_resync < 2); |
| 6773 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6774 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6775 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6776 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6777 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6778 | */ |
| 6779 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6780 | mddev->resync_mismatches = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6781 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6782 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6783 | j = mddev->resync_min; |
| 6784 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6785 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6786 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6787 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6788 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6789 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6790 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6791 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6792 | j = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6793 | rcu_read_lock(); |
| 6794 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6795 | if (rdev->raid_disk >= 0 && |
| 6796 | !test_bit(Faulty, &rdev->flags) && |
| 6797 | !test_bit(In_sync, &rdev->flags) && |
| 6798 | rdev->recovery_offset < j) |
| 6799 | j = rdev->recovery_offset; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6800 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6801 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6802 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6803 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 6804 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 6805 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 6806 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6807 | "(but not more than %d KB/sec) for %s.\n", |
| 6808 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6809 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6810 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6811 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6812 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6813 | for (m = 0; m < SYNC_MARKS; m++) { |
| 6814 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6815 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6816 | } |
| 6817 | last_mark = 0; |
| 6818 | mddev->resync_mark = mark[last_mark]; |
| 6819 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 6820 | |
| 6821 | /* |
| 6822 | * Tune reconstruction: |
| 6823 | */ |
| 6824 | window = 32*(PAGE_SIZE/512); |
| 6825 | printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n", |
| 6826 | window/2,(unsigned long long) max_sectors/2); |
| 6827 | |
| 6828 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6829 | last_check = 0; |
| 6830 | |
| 6831 | if (j>2) { |
| 6832 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6833 | "md: resuming %s of %s from checkpoint.\n", |
| 6834 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6835 | mddev->curr_resync = j; |
| 6836 | } |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 6837 | mddev->curr_resync_completed = j; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6838 | |
| 6839 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6840 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6841 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6842 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6843 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 6844 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 6845 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 6846 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 6847 | > (max_sectors >> 4)) || |
| 6848 | (j - mddev->curr_resync_completed)*2 |
| 6849 | >= mddev->resync_max - mddev->curr_resync_completed |
| 6850 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6851 | /* time to update curr_resync_completed */ |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6852 | wait_event(mddev->recovery_wait, |
| 6853 | atomic_read(&mddev->recovery_active) == 0); |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 6854 | mddev->curr_resync_completed = j; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 6855 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6856 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6857 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6858 | |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 6859 | while (j >= mddev->resync_max && !kthread_should_stop()) { |
| 6860 | /* As this condition is controlled by user-space, |
| 6861 | * we can block indefinitely, so use '_interruptible' |
| 6862 | * to avoid triggering warnings. |
| 6863 | */ |
| 6864 | flush_signals(current); /* just in case */ |
| 6865 | wait_event_interruptible(mddev->recovery_wait, |
| 6866 | mddev->resync_max > j |
| 6867 | || kthread_should_stop()); |
| 6868 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6869 | |
| 6870 | if (kthread_should_stop()) |
| 6871 | goto interrupted; |
| 6872 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6873 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6874 | currspeed < speed_min(mddev)); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6875 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6876 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6877 | goto out; |
| 6878 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6879 | |
| 6880 | if (!skipped) { /* actual IO requested */ |
| 6881 | io_sectors += sectors; |
| 6882 | atomic_add(sectors, &mddev->recovery_active); |
| 6883 | } |
| 6884 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6885 | j += sectors; |
| 6886 | if (j>1) mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6887 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6888 | if (last_check == 0) |
| 6889 | /* this is the earliers that rebuilt will be |
| 6890 | * visible in /proc/mdstat |
| 6891 | */ |
| 6892 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6893 | |
| 6894 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6895 | continue; |
| 6896 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6897 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6898 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6899 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6900 | break; |
| 6901 | |
| 6902 | repeat: |
| 6903 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 6904 | /* step marks */ |
| 6905 | int next = (last_mark+1) % SYNC_MARKS; |
| 6906 | |
| 6907 | mddev->resync_mark = mark[next]; |
| 6908 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 6909 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6910 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6911 | last_mark = next; |
| 6912 | } |
| 6913 | |
| 6914 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6915 | if (kthread_should_stop()) |
| 6916 | goto interrupted; |
| 6917 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6918 | |
| 6919 | /* |
| 6920 | * this loop exits only if either when we are slower than |
| 6921 | * the 'hard' speed limit, or the system was IO-idle for |
| 6922 | * a jiffy. |
| 6923 | * the system might be non-idle CPU-wise, but we only care |
| 6924 | * about not overloading the IO subsystem. (things like an |
| 6925 | * e2fsck being done on the RAID array should execute fast) |
| 6926 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6927 | cond_resched(); |
| 6928 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6929 | currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2 |
| 6930 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6931 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 6932 | if (currspeed > speed_min(mddev)) { |
| 6933 | if ((currspeed > speed_max(mddev)) || |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6934 | !is_mddev_idle(mddev, 0)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6935 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6936 | goto repeat; |
| 6937 | } |
| 6938 | } |
| 6939 | } |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6940 | printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6941 | /* |
| 6942 | * this also signals 'finished resyncing' to md_stop |
| 6943 | */ |
| 6944 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6945 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 6946 | |
| 6947 | /* tell personality that we are finished */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6948 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6949 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6950 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6951 | mddev->curr_resync > 2) { |
| 6952 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6953 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 6954 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 6955 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6956 | "md: checkpointing %s of %s.\n", |
| 6957 | desc, mdname(mddev)); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6958 | mddev->recovery_cp = mddev->curr_resync; |
| 6959 | } |
| 6960 | } else |
| 6961 | mddev->recovery_cp = MaxSector; |
| 6962 | } else { |
| 6963 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 6964 | mddev->curr_resync = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6965 | rcu_read_lock(); |
| 6966 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6967 | if (rdev->raid_disk >= 0 && |
NeilBrown | 70fffd0 | 2010-06-16 17:01:25 +1000 | [diff] [blame] | 6968 | mddev->delta_disks >= 0 && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6969 | !test_bit(Faulty, &rdev->flags) && |
| 6970 | !test_bit(In_sync, &rdev->flags) && |
| 6971 | rdev->recovery_offset < mddev->curr_resync) |
| 6972 | rdev->recovery_offset = mddev->curr_resync; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6973 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6974 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6975 | } |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 6976 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6977 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6978 | skip: |
NeilBrown | c07b70a | 2009-12-14 12:49:48 +1100 | [diff] [blame] | 6979 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 6980 | /* We completed so min/max setting can be forgotten if used. */ |
| 6981 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6982 | mddev->resync_min = 0; |
| 6983 | mddev->resync_max = MaxSector; |
| 6984 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6985 | mddev->resync_min = mddev->curr_resync_completed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6986 | mddev->curr_resync = 0; |
| 6987 | wake_up(&resync_wait); |
| 6988 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
| 6989 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6990 | return; |
| 6991 | |
| 6992 | interrupted: |
| 6993 | /* |
| 6994 | * got a signal, exit. |
| 6995 | */ |
| 6996 | printk(KERN_INFO |
| 6997 | "md: md_do_sync() got signal ... exiting\n"); |
| 6998 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6999 | goto out; |
| 7000 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7001 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7002 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7003 | |
| 7004 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7005 | static int remove_and_add_spares(mddev_t *mddev) |
| 7006 | { |
| 7007 | mdk_rdev_t *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7008 | int spares = 0; |
| 7009 | |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7010 | mddev->curr_resync_completed = 0; |
| 7011 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 7012 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7013 | if (rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7014 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7015 | (test_bit(Faulty, &rdev->flags) || |
| 7016 | ! test_bit(In_sync, &rdev->flags)) && |
| 7017 | atomic_read(&rdev->nr_pending)==0) { |
| 7018 | if (mddev->pers->hot_remove_disk( |
| 7019 | mddev, rdev->raid_disk)==0) { |
| 7020 | char nm[20]; |
| 7021 | sprintf(nm,"rd%d", rdev->raid_disk); |
| 7022 | sysfs_remove_link(&mddev->kobj, nm); |
| 7023 | rdev->raid_disk = -1; |
| 7024 | } |
| 7025 | } |
| 7026 | |
NeilBrown | a8c42c7 | 2011-01-31 13:47:13 +1100 | [diff] [blame] | 7027 | if (mddev->degraded && !mddev->recovery_disabled) { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 7028 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7029 | if (rdev->raid_disk >= 0 && |
Dan Williams | e542713 | 2008-07-28 23:28:06 -0700 | [diff] [blame] | 7030 | !test_bit(In_sync, &rdev->flags) && |
| 7031 | !test_bit(Blocked, &rdev->flags)) |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7032 | spares++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7033 | if (rdev->raid_disk < 0 |
| 7034 | && !test_bit(Faulty, &rdev->flags)) { |
| 7035 | rdev->recovery_offset = 0; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 7036 | if (mddev->pers-> |
| 7037 | hot_add_disk(mddev, rdev) == 0) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7038 | char nm[20]; |
| 7039 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 7040 | if (sysfs_create_link(&mddev->kobj, |
| 7041 | &rdev->kobj, nm)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7042 | /* failure here is OK */; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7043 | spares++; |
| 7044 | md_new_event(mddev); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 7045 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7046 | } else |
| 7047 | break; |
| 7048 | } |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7049 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7050 | } |
| 7051 | return spares; |
| 7052 | } |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7053 | |
| 7054 | static void reap_sync_thread(mddev_t *mddev) |
| 7055 | { |
| 7056 | mdk_rdev_t *rdev; |
| 7057 | |
| 7058 | /* resync has finished, collect result */ |
| 7059 | md_unregister_thread(mddev->sync_thread); |
| 7060 | mddev->sync_thread = NULL; |
| 7061 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 7062 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
| 7063 | /* success...*/ |
| 7064 | /* activate any spares */ |
| 7065 | if (mddev->pers->spare_active(mddev)) |
| 7066 | sysfs_notify(&mddev->kobj, NULL, |
| 7067 | "degraded"); |
| 7068 | } |
| 7069 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 7070 | mddev->pers->finish_reshape) |
| 7071 | mddev->pers->finish_reshape(mddev); |
| 7072 | md_update_sb(mddev, 1); |
| 7073 | |
| 7074 | /* if array is no-longer degraded, then any saved_raid_disk |
| 7075 | * information must be scrapped |
| 7076 | */ |
| 7077 | if (!mddev->degraded) |
| 7078 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 7079 | rdev->saved_raid_disk = -1; |
| 7080 | |
| 7081 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7082 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7083 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 7084 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 7085 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 7086 | /* flag recovery needed just to double check */ |
| 7087 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7088 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 7089 | md_new_event(mddev); |
| 7090 | } |
| 7091 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7092 | /* |
| 7093 | * This routine is regularly called by all per-raid-array threads to |
| 7094 | * deal with generic issues like resync and super-block update. |
| 7095 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 7096 | * need this as they never do any recovery or update the superblock. |
| 7097 | * |
| 7098 | * It does not do any resync itself, but rather "forks" off other threads |
| 7099 | * to do that as needed. |
| 7100 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 7101 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7102 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7103 | * and wakeups up this thread which will reap the thread and finish up. |
| 7104 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 7105 | * |
| 7106 | * The overall approach is: |
| 7107 | * 1/ if the superblock needs updating, update it. |
| 7108 | * 2/ If a recovery thread is running, don't do anything else. |
| 7109 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 7110 | * 4/ If there are any faulty devices, remove them. |
| 7111 | * 5/ If array is degraded, try to add spares devices |
| 7112 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 7113 | */ |
| 7114 | void md_check_recovery(mddev_t *mddev) |
| 7115 | { |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 7116 | if (mddev->bitmap) |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 7117 | bitmap_daemon_work(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7118 | |
| 7119 | if (mddev->ro) |
| 7120 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7121 | |
| 7122 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7123 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7124 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 7125 | mdname(mddev)); |
| 7126 | mddev->safemode = 2; |
| 7127 | } |
| 7128 | flush_signals(current); |
| 7129 | } |
| 7130 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7131 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 7132 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7133 | if ( ! ( |
NeilBrown | 126925c | 2010-09-07 17:02:47 +1000 | [diff] [blame] | 7134 | (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7135 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7136 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7137 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7138 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 7139 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7140 | )) |
| 7141 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7142 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 7143 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7144 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7145 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7146 | if (mddev->ro) { |
| 7147 | /* Only thing we do on a ro array is remove |
| 7148 | * failed devices. |
| 7149 | */ |
NeilBrown | a8c42c7 | 2011-01-31 13:47:13 +1100 | [diff] [blame] | 7150 | mdk_rdev_t *rdev; |
| 7151 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 7152 | if (rdev->raid_disk >= 0 && |
| 7153 | !test_bit(Blocked, &rdev->flags) && |
| 7154 | test_bit(Faulty, &rdev->flags) && |
| 7155 | atomic_read(&rdev->nr_pending)==0) { |
| 7156 | if (mddev->pers->hot_remove_disk( |
| 7157 | mddev, rdev->raid_disk)==0) { |
| 7158 | char nm[20]; |
| 7159 | sprintf(nm,"rd%d", rdev->raid_disk); |
| 7160 | sysfs_remove_link(&mddev->kobj, nm); |
| 7161 | rdev->raid_disk = -1; |
| 7162 | } |
| 7163 | } |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7164 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7165 | goto unlock; |
| 7166 | } |
| 7167 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7168 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7169 | int did_change = 0; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7170 | spin_lock_irq(&mddev->write_lock); |
| 7171 | if (mddev->safemode && |
| 7172 | !atomic_read(&mddev->writes_pending) && |
| 7173 | !mddev->in_sync && |
| 7174 | mddev->recovery_cp == MaxSector) { |
| 7175 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7176 | did_change = 1; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7177 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7178 | } |
| 7179 | if (mddev->safemode == 1) |
| 7180 | mddev->safemode = 0; |
| 7181 | spin_unlock_irq(&mddev->write_lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7182 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7183 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7184 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7185 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7186 | if (mddev->flags) |
| 7187 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7188 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7189 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 7190 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 7191 | /* resync/recovery still happening */ |
| 7192 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7193 | goto unlock; |
| 7194 | } |
| 7195 | if (mddev->sync_thread) { |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7196 | reap_sync_thread(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7197 | goto unlock; |
| 7198 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7199 | /* Set RUNNING before clearing NEEDED to avoid |
| 7200 | * any transients in the value of "sync_action". |
| 7201 | */ |
| 7202 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7203 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7204 | /* Clear some bits that don't mean anything, but |
| 7205 | * might be left set |
| 7206 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7207 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7208 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7209 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7210 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 7211 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7212 | /* no recovery is running. |
| 7213 | * remove any failed drives, then |
| 7214 | * add spares if possible. |
| 7215 | * Spare are also removed and re-added, to allow |
| 7216 | * the personality to fail the re-add. |
| 7217 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7218 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7219 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 7220 | if (mddev->pers->check_reshape == NULL || |
| 7221 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7222 | /* Cannot proceed */ |
| 7223 | goto unlock; |
| 7224 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7225 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7226 | } else if ((spares = remove_and_add_spares(mddev))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7227 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7228 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 7229 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7230 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7231 | } else if (mddev->recovery_cp < MaxSector) { |
| 7232 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7233 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7234 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 7235 | /* nothing to be done ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7236 | goto unlock; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7237 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7238 | if (mddev->pers->sync_request) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 7239 | if (spares && mddev->bitmap && ! mddev->bitmap->file) { |
| 7240 | /* We are adding a device or devices to an array |
| 7241 | * which has the bitmap stored on all devices. |
| 7242 | * So make sure all bitmap pages get written |
| 7243 | */ |
| 7244 | bitmap_write_all(mddev->bitmap); |
| 7245 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7246 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 7247 | mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 7248 | "resync"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7249 | if (!mddev->sync_thread) { |
| 7250 | printk(KERN_ERR "%s: could not start resync" |
| 7251 | " thread...\n", |
| 7252 | mdname(mddev)); |
| 7253 | /* leave the spares where they are, it shouldn't hurt */ |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7254 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7255 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7256 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 7257 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 7258 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7259 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7260 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7261 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7262 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7263 | } |
| 7264 | unlock: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7265 | if (!mddev->sync_thread) { |
| 7266 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7267 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 7268 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 7269 | if (mddev->sysfs_action) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7270 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7271 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7272 | mddev_unlock(mddev); |
| 7273 | } |
| 7274 | } |
| 7275 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7276 | void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 7277 | { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7278 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7279 | wait_event_timeout(rdev->blocked_wait, |
| 7280 | !test_bit(Blocked, &rdev->flags), |
| 7281 | msecs_to_jiffies(5000)); |
| 7282 | rdev_dec_pending(rdev, mddev); |
| 7283 | } |
| 7284 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 7285 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7286 | static int md_notify_reboot(struct notifier_block *this, |
| 7287 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7288 | { |
| 7289 | struct list_head *tmp; |
| 7290 | mddev_t *mddev; |
| 7291 | |
| 7292 | if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) { |
| 7293 | |
| 7294 | printk(KERN_INFO "md: stopping all md devices.\n"); |
| 7295 | |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7296 | for_each_mddev(mddev, tmp) |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 7297 | if (mddev_trylock(mddev)) { |
NeilBrown | 2b25000 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7298 | /* Force a switch to readonly even array |
| 7299 | * appears to still be in use. Hence |
| 7300 | * the '100'. |
| 7301 | */ |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 7302 | md_set_readonly(mddev, 100); |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 7303 | mddev_unlock(mddev); |
| 7304 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7305 | /* |
| 7306 | * certain more exotic SCSI devices are known to be |
| 7307 | * volatile wrt too early system reboots. While the |
| 7308 | * right place to handle this issue is the given |
| 7309 | * driver, we do want to have a safe RAID driver ... |
| 7310 | */ |
| 7311 | mdelay(1000*1); |
| 7312 | } |
| 7313 | return NOTIFY_DONE; |
| 7314 | } |
| 7315 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7316 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7317 | .notifier_call = md_notify_reboot, |
| 7318 | .next = NULL, |
| 7319 | .priority = INT_MAX, /* before any real devices */ |
| 7320 | }; |
| 7321 | |
| 7322 | static void md_geninit(void) |
| 7323 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7324 | dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t)); |
| 7325 | |
Denis V. Lunev | c7705f3 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 7326 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7327 | } |
| 7328 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7329 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7330 | { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 7331 | int ret = -ENOMEM; |
| 7332 | |
| 7333 | md_wq = alloc_workqueue("md", WQ_RESCUER, 0); |
| 7334 | if (!md_wq) |
| 7335 | goto err_wq; |
| 7336 | |
| 7337 | md_misc_wq = alloc_workqueue("md_misc", 0, 0); |
| 7338 | if (!md_misc_wq) |
| 7339 | goto err_misc_wq; |
| 7340 | |
| 7341 | if ((ret = register_blkdev(MD_MAJOR, "md")) < 0) |
| 7342 | goto err_md; |
| 7343 | |
| 7344 | if ((ret = register_blkdev(0, "mdp")) < 0) |
| 7345 | goto err_mdp; |
| 7346 | mdp_major = ret; |
| 7347 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7348 | blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE, |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 7349 | md_probe, NULL, NULL); |
| 7350 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7351 | md_probe, NULL, NULL); |
| 7352 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7353 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 7354 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7355 | |
| 7356 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 7357 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7358 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 7359 | err_mdp: |
| 7360 | unregister_blkdev(MD_MAJOR, "md"); |
| 7361 | err_md: |
| 7362 | destroy_workqueue(md_misc_wq); |
| 7363 | err_misc_wq: |
| 7364 | destroy_workqueue(md_wq); |
| 7365 | err_wq: |
| 7366 | return ret; |
| 7367 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7368 | |
| 7369 | #ifndef MODULE |
| 7370 | |
| 7371 | /* |
| 7372 | * Searches all registered partitions for autorun RAID arrays |
| 7373 | * at boot time. |
| 7374 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7375 | |
| 7376 | static LIST_HEAD(all_detected_devices); |
| 7377 | struct detected_devices_node { |
| 7378 | struct list_head list; |
| 7379 | dev_t dev; |
| 7380 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7381 | |
| 7382 | void md_autodetect_dev(dev_t dev) |
| 7383 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7384 | struct detected_devices_node *node_detected_dev; |
| 7385 | |
| 7386 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 7387 | if (node_detected_dev) { |
| 7388 | node_detected_dev->dev = dev; |
| 7389 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 7390 | } else { |
| 7391 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 7392 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 7393 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7394 | } |
| 7395 | |
| 7396 | |
| 7397 | static void autostart_arrays(int part) |
| 7398 | { |
| 7399 | mdk_rdev_t *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7400 | struct detected_devices_node *node_detected_dev; |
| 7401 | dev_t dev; |
| 7402 | int i_scanned, i_passed; |
| 7403 | |
| 7404 | i_scanned = 0; |
| 7405 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7406 | |
| 7407 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 7408 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7409 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 7410 | i_scanned++; |
| 7411 | node_detected_dev = list_entry(all_detected_devices.next, |
| 7412 | struct detected_devices_node, list); |
| 7413 | list_del(&node_detected_dev->list); |
| 7414 | dev = node_detected_dev->dev; |
| 7415 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 7416 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7417 | if (IS_ERR(rdev)) |
| 7418 | continue; |
| 7419 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7420 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7421 | MD_BUG(); |
| 7422 | continue; |
| 7423 | } |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 7424 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7425 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7426 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7427 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7428 | |
| 7429 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 7430 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7431 | |
| 7432 | autorun_devices(part); |
| 7433 | } |
| 7434 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 7435 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7436 | |
| 7437 | static __exit void md_exit(void) |
| 7438 | { |
| 7439 | mddev_t *mddev; |
| 7440 | struct list_head *tmp; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 7441 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7442 | blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS); |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 7443 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7444 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7445 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7446 | unregister_blkdev(mdp_major, "mdp"); |
| 7447 | unregister_reboot_notifier(&md_notifier); |
| 7448 | unregister_sysctl_table(raid_table_header); |
| 7449 | remove_proc_entry("mdstat", NULL); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7450 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7451 | export_array(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7452 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7453 | } |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 7454 | destroy_workqueue(md_misc_wq); |
| 7455 | destroy_workqueue(md_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7456 | } |
| 7457 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 7458 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7459 | module_exit(md_exit) |
| 7460 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7461 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 7462 | { |
| 7463 | return sprintf(buffer, "%d", start_readonly); |
| 7464 | } |
| 7465 | static int set_ro(const char *val, struct kernel_param *kp) |
| 7466 | { |
| 7467 | char *e; |
| 7468 | int num = simple_strtoul(val, &e, 10); |
| 7469 | if (*val && (*e == '\0' || *e == '\n')) { |
| 7470 | start_readonly = num; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 7471 | return 0; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7472 | } |
| 7473 | return -EINVAL; |
| 7474 | } |
| 7475 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 7476 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 7477 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | 6ff8d8e | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 7478 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7479 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7480 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7481 | EXPORT_SYMBOL(register_md_personality); |
| 7482 | EXPORT_SYMBOL(unregister_md_personality); |
| 7483 | EXPORT_SYMBOL(md_error); |
| 7484 | EXPORT_SYMBOL(md_done_sync); |
| 7485 | EXPORT_SYMBOL(md_write_start); |
| 7486 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7487 | EXPORT_SYMBOL(md_register_thread); |
| 7488 | EXPORT_SYMBOL(md_unregister_thread); |
| 7489 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7490 | EXPORT_SYMBOL(md_check_recovery); |
| 7491 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 7492 | MODULE_DESCRIPTION("MD RAID framework"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 7493 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 7494 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |