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; |
| 290 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 291 | if (mddev == NULL || mddev->pers == NULL) { |
| 292 | bio_io_error(bio); |
| 293 | return 0; |
| 294 | } |
| 295 | rcu_read_lock(); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 296 | if (mddev->suspended) { |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 297 | DEFINE_WAIT(__wait); |
| 298 | for (;;) { |
| 299 | prepare_to_wait(&mddev->sb_wait, &__wait, |
| 300 | TASK_UNINTERRUPTIBLE); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 301 | if (!mddev->suspended) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 302 | break; |
| 303 | rcu_read_unlock(); |
| 304 | schedule(); |
| 305 | rcu_read_lock(); |
| 306 | } |
| 307 | finish_wait(&mddev->sb_wait, &__wait); |
| 308 | } |
| 309 | atomic_inc(&mddev->active_io); |
| 310 | rcu_read_unlock(); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 311 | |
NeilBrown | 21a52c6 | 2010-04-01 15:02:13 +1100 | [diff] [blame] | 312 | rv = mddev->pers->make_request(mddev, bio); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 313 | |
| 314 | cpu = part_stat_lock(); |
| 315 | part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]); |
| 316 | part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], |
| 317 | bio_sectors(bio)); |
| 318 | part_stat_unlock(); |
| 319 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 320 | if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended) |
| 321 | wake_up(&mddev->sb_wait); |
| 322 | |
| 323 | return rv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | } |
| 325 | |
NeilBrown | 9e35b99 | 2010-04-06 14:23:02 +1000 | [diff] [blame] | 326 | /* mddev_suspend makes sure no new requests are submitted |
| 327 | * to the device, and that any requests that have been submitted |
| 328 | * are completely handled. |
| 329 | * Once ->stop is called and completes, the module will be completely |
| 330 | * unused. |
| 331 | */ |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 332 | void mddev_suspend(mddev_t *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 333 | { |
| 334 | BUG_ON(mddev->suspended); |
| 335 | mddev->suspended = 1; |
| 336 | synchronize_rcu(); |
| 337 | wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0); |
| 338 | mddev->pers->quiesce(mddev, 1); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 339 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 340 | EXPORT_SYMBOL_GPL(mddev_suspend); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 341 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 342 | void mddev_resume(mddev_t *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 343 | { |
| 344 | mddev->suspended = 0; |
| 345 | wake_up(&mddev->sb_wait); |
| 346 | mddev->pers->quiesce(mddev, 0); |
| 347 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 348 | EXPORT_SYMBOL_GPL(mddev_resume); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 349 | |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 350 | int mddev_congested(mddev_t *mddev, int bits) |
| 351 | { |
| 352 | return mddev->suspended; |
| 353 | } |
| 354 | EXPORT_SYMBOL(mddev_congested); |
| 355 | |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 356 | /* |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 357 | * Generic flush handling for md |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 358 | */ |
| 359 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 360 | static void md_end_flush(struct bio *bio, int err) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 361 | { |
| 362 | mdk_rdev_t *rdev = bio->bi_private; |
| 363 | mddev_t *mddev = rdev->mddev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 364 | |
| 365 | rdev_dec_pending(rdev, mddev); |
| 366 | |
| 367 | if (atomic_dec_and_test(&mddev->flush_pending)) { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 368 | /* The pre-request flush has finished */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 369 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 370 | } |
| 371 | bio_put(bio); |
| 372 | } |
| 373 | |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 374 | static void md_submit_flush_data(struct work_struct *ws); |
| 375 | |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 376 | static void submit_flushes(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 377 | { |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 378 | mddev_t *mddev = container_of(ws, mddev_t, flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 379 | mdk_rdev_t *rdev; |
| 380 | |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 381 | INIT_WORK(&mddev->flush_work, md_submit_flush_data); |
| 382 | atomic_set(&mddev->flush_pending, 1); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 383 | rcu_read_lock(); |
| 384 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
| 385 | if (rdev->raid_disk >= 0 && |
| 386 | !test_bit(Faulty, &rdev->flags)) { |
| 387 | /* Take two references, one is dropped |
| 388 | * when request finishes, one after |
| 389 | * we reclaim rcu_read_lock |
| 390 | */ |
| 391 | struct bio *bi; |
| 392 | atomic_inc(&rdev->nr_pending); |
| 393 | atomic_inc(&rdev->nr_pending); |
| 394 | rcu_read_unlock(); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 395 | bi = bio_alloc_mddev(GFP_KERNEL, 0, mddev); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 396 | bi->bi_end_io = md_end_flush; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 397 | bi->bi_private = rdev; |
| 398 | bi->bi_bdev = rdev->bdev; |
| 399 | atomic_inc(&mddev->flush_pending); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 400 | submit_bio(WRITE_FLUSH, bi); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 401 | rcu_read_lock(); |
| 402 | rdev_dec_pending(rdev, mddev); |
| 403 | } |
| 404 | rcu_read_unlock(); |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 405 | if (atomic_dec_and_test(&mddev->flush_pending)) |
| 406 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 407 | } |
| 408 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 409 | static void md_submit_flush_data(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 410 | { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 411 | mddev_t *mddev = container_of(ws, mddev_t, flush_work); |
| 412 | struct bio *bio = mddev->flush_bio; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 413 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 414 | if (bio->bi_size == 0) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 415 | /* an empty barrier - all done */ |
| 416 | bio_endio(bio, 0); |
| 417 | else { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 418 | bio->bi_rw &= ~REQ_FLUSH; |
NeilBrown | 21a52c6 | 2010-04-01 15:02:13 +1100 | [diff] [blame] | 419 | if (mddev->pers->make_request(mddev, bio)) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 420 | generic_make_request(bio); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 421 | } |
NeilBrown | 2b74e12 | 2010-12-09 15:59:01 +1100 | [diff] [blame] | 422 | |
| 423 | mddev->flush_bio = NULL; |
| 424 | wake_up(&mddev->sb_wait); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 425 | } |
| 426 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 427 | void md_flush_request(mddev_t *mddev, struct bio *bio) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 428 | { |
| 429 | spin_lock_irq(&mddev->write_lock); |
| 430 | wait_event_lock_irq(mddev->sb_wait, |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 431 | !mddev->flush_bio, |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 432 | mddev->write_lock, /*nothing*/); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 433 | mddev->flush_bio = bio; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 434 | spin_unlock_irq(&mddev->write_lock); |
| 435 | |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 436 | INIT_WORK(&mddev->flush_work, submit_flushes); |
| 437 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 438 | } |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 439 | EXPORT_SYMBOL(md_flush_request); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 440 | |
NeilBrown | 2ac8740 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 441 | /* Support for plugging. |
| 442 | * This mirrors the plugging support in request_queue, but does not |
| 443 | * require having a whole queue |
| 444 | */ |
| 445 | static void plugger_work(struct work_struct *work) |
| 446 | { |
| 447 | struct plug_handle *plug = |
| 448 | container_of(work, struct plug_handle, unplug_work); |
| 449 | plug->unplug_fn(plug); |
| 450 | } |
| 451 | static void plugger_timeout(unsigned long data) |
| 452 | { |
| 453 | struct plug_handle *plug = (void *)data; |
| 454 | kblockd_schedule_work(NULL, &plug->unplug_work); |
| 455 | } |
| 456 | void plugger_init(struct plug_handle *plug, |
| 457 | void (*unplug_fn)(struct plug_handle *)) |
| 458 | { |
| 459 | plug->unplug_flag = 0; |
| 460 | plug->unplug_fn = unplug_fn; |
| 461 | init_timer(&plug->unplug_timer); |
| 462 | plug->unplug_timer.function = plugger_timeout; |
| 463 | plug->unplug_timer.data = (unsigned long)plug; |
| 464 | INIT_WORK(&plug->unplug_work, plugger_work); |
| 465 | } |
| 466 | EXPORT_SYMBOL_GPL(plugger_init); |
| 467 | |
| 468 | void plugger_set_plug(struct plug_handle *plug) |
| 469 | { |
| 470 | if (!test_and_set_bit(PLUGGED_FLAG, &plug->unplug_flag)) |
| 471 | mod_timer(&plug->unplug_timer, jiffies + msecs_to_jiffies(3)+1); |
| 472 | } |
| 473 | EXPORT_SYMBOL_GPL(plugger_set_plug); |
| 474 | |
| 475 | int plugger_remove_plug(struct plug_handle *plug) |
| 476 | { |
| 477 | if (test_and_clear_bit(PLUGGED_FLAG, &plug->unplug_flag)) { |
| 478 | del_timer(&plug->unplug_timer); |
| 479 | return 1; |
| 480 | } else |
| 481 | return 0; |
| 482 | } |
| 483 | EXPORT_SYMBOL_GPL(plugger_remove_plug); |
| 484 | |
| 485 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | static inline mddev_t *mddev_get(mddev_t *mddev) |
| 487 | { |
| 488 | atomic_inc(&mddev->active); |
| 489 | return mddev; |
| 490 | } |
| 491 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 492 | static void mddev_delayed_delete(struct work_struct *ws); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 493 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | static void mddev_put(mddev_t *mddev) |
| 495 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 496 | struct bio_set *bs = NULL; |
| 497 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
| 499 | return; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 500 | if (!mddev->raid_disks && list_empty(&mddev->disks) && |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 501 | mddev->ctime == 0 && !mddev->hold_active) { |
| 502 | /* Array is not configured at all, and not held active, |
| 503 | * so destroy it */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | list_del(&mddev->all_mddevs); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 505 | bs = mddev->bio_set; |
| 506 | mddev->bio_set = NULL; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 507 | if (mddev->gendisk) { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 508 | /* We did a probe so need to clean up. Call |
| 509 | * queue_work inside the spinlock so that |
| 510 | * flush_workqueue() after mddev_find will |
| 511 | * succeed in waiting for the work to be done. |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 512 | */ |
| 513 | INIT_WORK(&mddev->del_work, mddev_delayed_delete); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 514 | queue_work(md_misc_wq, &mddev->del_work); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 515 | } else |
| 516 | kfree(mddev); |
| 517 | } |
| 518 | spin_unlock(&all_mddevs_lock); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 519 | if (bs) |
| 520 | bioset_free(bs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | } |
| 522 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 523 | void mddev_init(mddev_t *mddev) |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 524 | { |
| 525 | mutex_init(&mddev->open_mutex); |
| 526 | mutex_init(&mddev->reconfig_mutex); |
| 527 | mutex_init(&mddev->bitmap_info.mutex); |
| 528 | INIT_LIST_HEAD(&mddev->disks); |
| 529 | INIT_LIST_HEAD(&mddev->all_mddevs); |
| 530 | init_timer(&mddev->safemode_timer); |
| 531 | atomic_set(&mddev->active, 1); |
| 532 | atomic_set(&mddev->openers, 0); |
| 533 | atomic_set(&mddev->active_io, 0); |
| 534 | spin_lock_init(&mddev->write_lock); |
| 535 | atomic_set(&mddev->flush_pending, 0); |
| 536 | init_waitqueue_head(&mddev->sb_wait); |
| 537 | init_waitqueue_head(&mddev->recovery_wait); |
| 538 | mddev->reshape_position = MaxSector; |
| 539 | mddev->resync_min = 0; |
| 540 | mddev->resync_max = MaxSector; |
| 541 | mddev->level = LEVEL_NONE; |
| 542 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 543 | EXPORT_SYMBOL_GPL(mddev_init); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 544 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | static mddev_t * mddev_find(dev_t unit) |
| 546 | { |
| 547 | mddev_t *mddev, *new = NULL; |
| 548 | |
| 549 | retry: |
| 550 | spin_lock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 552 | if (unit) { |
| 553 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 554 | if (mddev->unit == unit) { |
| 555 | mddev_get(mddev); |
| 556 | spin_unlock(&all_mddevs_lock); |
| 557 | kfree(new); |
| 558 | return mddev; |
| 559 | } |
| 560 | |
| 561 | if (new) { |
| 562 | list_add(&new->all_mddevs, &all_mddevs); |
| 563 | spin_unlock(&all_mddevs_lock); |
| 564 | new->hold_active = UNTIL_IOCTL; |
| 565 | return new; |
| 566 | } |
| 567 | } else if (new) { |
| 568 | /* find an unused unit number */ |
| 569 | static int next_minor = 512; |
| 570 | int start = next_minor; |
| 571 | int is_free = 0; |
| 572 | int dev = 0; |
| 573 | while (!is_free) { |
| 574 | dev = MKDEV(MD_MAJOR, next_minor); |
| 575 | next_minor++; |
| 576 | if (next_minor > MINORMASK) |
| 577 | next_minor = 0; |
| 578 | if (next_minor == start) { |
| 579 | /* Oh dear, all in use. */ |
| 580 | spin_unlock(&all_mddevs_lock); |
| 581 | kfree(new); |
| 582 | return NULL; |
| 583 | } |
| 584 | |
| 585 | is_free = 1; |
| 586 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 587 | if (mddev->unit == dev) { |
| 588 | is_free = 0; |
| 589 | break; |
| 590 | } |
| 591 | } |
| 592 | new->unit = dev; |
| 593 | new->md_minor = MINOR(dev); |
| 594 | new->hold_active = UNTIL_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | list_add(&new->all_mddevs, &all_mddevs); |
| 596 | spin_unlock(&all_mddevs_lock); |
| 597 | return new; |
| 598 | } |
| 599 | spin_unlock(&all_mddevs_lock); |
| 600 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 601 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | if (!new) |
| 603 | return NULL; |
| 604 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | new->unit = unit; |
| 606 | if (MAJOR(unit) == MD_MAJOR) |
| 607 | new->md_minor = MINOR(unit); |
| 608 | else |
| 609 | new->md_minor = MINOR(unit) >> MdpMinorShift; |
| 610 | |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 611 | mddev_init(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | goto retry; |
| 614 | } |
| 615 | |
| 616 | static inline int mddev_lock(mddev_t * mddev) |
| 617 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 618 | return mutex_lock_interruptible(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | } |
| 620 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 621 | static inline int mddev_is_locked(mddev_t *mddev) |
| 622 | { |
| 623 | return mutex_is_locked(&mddev->reconfig_mutex); |
| 624 | } |
| 625 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | static inline int mddev_trylock(mddev_t * mddev) |
| 627 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 628 | return mutex_trylock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | } |
| 630 | |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 631 | static struct attribute_group md_redundancy_group; |
| 632 | |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 633 | static void mddev_unlock(mddev_t * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | { |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 635 | if (mddev->to_remove) { |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 636 | /* These cannot be removed under reconfig_mutex as |
| 637 | * an access to the files will try to take reconfig_mutex |
| 638 | * while holding the file unremovable, which leads to |
| 639 | * a deadlock. |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 640 | * So hold set sysfs_active while the remove in happeing, |
| 641 | * and anything else which might set ->to_remove or my |
| 642 | * otherwise change the sysfs namespace will fail with |
| 643 | * -EBUSY if sysfs_active is still set. |
| 644 | * We set sysfs_active under reconfig_mutex and elsewhere |
| 645 | * test it under the same mutex to ensure its correct value |
| 646 | * is seen. |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 647 | */ |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 648 | struct attribute_group *to_remove = mddev->to_remove; |
| 649 | mddev->to_remove = NULL; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 650 | mddev->sysfs_active = 1; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 651 | mutex_unlock(&mddev->reconfig_mutex); |
| 652 | |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 653 | if (mddev->kobj.sd) { |
| 654 | if (to_remove != &md_redundancy_group) |
| 655 | sysfs_remove_group(&mddev->kobj, to_remove); |
| 656 | if (mddev->pers == NULL || |
| 657 | mddev->pers->sync_request == NULL) { |
| 658 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 659 | if (mddev->sysfs_action) |
| 660 | sysfs_put(mddev->sysfs_action); |
| 661 | mddev->sysfs_action = NULL; |
| 662 | } |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 663 | } |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 664 | mddev->sysfs_active = 0; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 665 | } else |
| 666 | mutex_unlock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 668 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | } |
| 670 | |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 671 | static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 673 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 675 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | if (rdev->desc_nr == nr) |
| 677 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 678 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | return NULL; |
| 680 | } |
| 681 | |
| 682 | static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev) |
| 683 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | mdk_rdev_t *rdev; |
| 685 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 686 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | if (rdev->bdev->bd_dev == dev) |
| 688 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 689 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | return NULL; |
| 691 | } |
| 692 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 693 | static struct mdk_personality *find_pers(int level, char *clevel) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 694 | { |
| 695 | struct mdk_personality *pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 696 | list_for_each_entry(pers, &pers_list, list) { |
| 697 | if (level != LEVEL_NONE && pers->level == level) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 698 | return pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 699 | if (strcmp(pers->name, clevel)==0) |
| 700 | return pers; |
| 701 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 702 | return NULL; |
| 703 | } |
| 704 | |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 705 | /* return the offset of the super block in 512byte sectors */ |
Jesper Juhl | 77933d7 | 2005-07-27 11:46:09 -0700 | [diff] [blame] | 706 | static inline sector_t calc_dev_sboffset(struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | { |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 708 | sector_t num_sectors = i_size_read(bdev->bd_inode) / 512; |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 709 | return MD_NEW_SIZE_SECTORS(num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | } |
| 711 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | static int alloc_disk_sb(mdk_rdev_t * rdev) |
| 713 | { |
| 714 | if (rdev->sb_page) |
| 715 | MD_BUG(); |
| 716 | |
| 717 | rdev->sb_page = alloc_page(GFP_KERNEL); |
| 718 | if (!rdev->sb_page) { |
| 719 | printk(KERN_ALERT "md: out of memory.\n"); |
Andre Noll | ebc2433 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 720 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | return 0; |
| 724 | } |
| 725 | |
| 726 | static void free_disk_sb(mdk_rdev_t * rdev) |
| 727 | { |
| 728 | if (rdev->sb_page) { |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 729 | put_page(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | rdev->sb_loaded = 0; |
| 731 | rdev->sb_page = NULL; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 732 | rdev->sb_start = 0; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 733 | rdev->sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | } |
| 735 | } |
| 736 | |
| 737 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 738 | static void super_written(struct bio *bio, int error) |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 739 | { |
| 740 | mdk_rdev_t *rdev = bio->bi_private; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 741 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 742 | |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 743 | if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) { |
| 744 | printk("md: super_written gets error=%d, uptodate=%d\n", |
| 745 | error, test_bit(BIO_UPTODATE, &bio->bi_flags)); |
| 746 | WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags)); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 747 | md_error(mddev, rdev); |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 748 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 749 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 750 | if (atomic_dec_and_test(&mddev->pending_writes)) |
| 751 | wake_up(&mddev->sb_wait); |
Neil Brown | f8b58ed | 2005-06-27 22:29:34 -0700 | [diff] [blame] | 752 | bio_put(bio); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev, |
| 756 | sector_t sector, int size, struct page *page) |
| 757 | { |
| 758 | /* write first size bytes of page to sector of rdev |
| 759 | * Increment mddev->pending_writes before returning |
| 760 | * and decrement it on completion, waking up sb_wait |
| 761 | * if zero is reached. |
| 762 | * If an error occurred, call md_error |
| 763 | */ |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 764 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 765 | |
| 766 | bio->bi_bdev = rdev->bdev; |
| 767 | bio->bi_sector = sector; |
| 768 | bio_add_page(bio, page, size, 0); |
| 769 | bio->bi_private = rdev; |
| 770 | bio->bi_end_io = super_written; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 771 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 772 | atomic_inc(&mddev->pending_writes); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 773 | submit_bio(REQ_WRITE | REQ_SYNC | REQ_UNPLUG | REQ_FLUSH | REQ_FUA, |
| 774 | bio); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | void md_super_wait(mddev_t *mddev) |
| 778 | { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 779 | /* wait for all superblock writes that were scheduled to complete */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 780 | DEFINE_WAIT(wq); |
| 781 | for(;;) { |
| 782 | prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE); |
| 783 | if (atomic_read(&mddev->pending_writes)==0) |
| 784 | break; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 785 | schedule(); |
| 786 | } |
| 787 | finish_wait(&mddev->sb_wait, &wq); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 788 | } |
| 789 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 790 | static void bi_complete(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | complete((struct completion*)bio->bi_private); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | } |
| 794 | |
NeilBrown | 2b19336 | 2010-10-27 15:16:40 +1100 | [diff] [blame] | 795 | int sync_page_io(mdk_rdev_t *rdev, sector_t sector, int size, |
| 796 | struct page *page, int rw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 798 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | struct completion event; |
| 800 | int ret; |
| 801 | |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 802 | rw |= REQ_SYNC | REQ_UNPLUG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | |
NeilBrown | 2b19336 | 2010-10-27 15:16:40 +1100 | [diff] [blame] | 804 | bio->bi_bdev = rdev->bdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | bio->bi_sector = sector; |
| 806 | bio_add_page(bio, page, size, 0); |
| 807 | init_completion(&event); |
| 808 | bio->bi_private = &event; |
| 809 | bio->bi_end_io = bi_complete; |
| 810 | submit_bio(rw, bio); |
| 811 | wait_for_completion(&event); |
| 812 | |
| 813 | ret = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 814 | bio_put(bio); |
| 815 | return ret; |
| 816 | } |
NeilBrown | a8745db | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 817 | EXPORT_SYMBOL_GPL(sync_page_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 819 | static int read_disk_sb(mdk_rdev_t * rdev, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | { |
| 821 | char b[BDEVNAME_SIZE]; |
| 822 | if (!rdev->sb_page) { |
| 823 | MD_BUG(); |
| 824 | return -EINVAL; |
| 825 | } |
| 826 | if (rdev->sb_loaded) |
| 827 | return 0; |
| 828 | |
| 829 | |
NeilBrown | 2b19336 | 2010-10-27 15:16:40 +1100 | [diff] [blame] | 830 | if (!sync_page_io(rdev, rdev->sb_start, size, rdev->sb_page, READ)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | goto fail; |
| 832 | rdev->sb_loaded = 1; |
| 833 | return 0; |
| 834 | |
| 835 | fail: |
| 836 | printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n", |
| 837 | bdevname(rdev->bdev,b)); |
| 838 | return -EINVAL; |
| 839 | } |
| 840 | |
| 841 | static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 842 | { |
Andre Noll | 0571046 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 843 | return sb1->set_uuid0 == sb2->set_uuid0 && |
| 844 | sb1->set_uuid1 == sb2->set_uuid1 && |
| 845 | sb1->set_uuid2 == sb2->set_uuid2 && |
| 846 | sb1->set_uuid3 == sb2->set_uuid3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | } |
| 848 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 850 | { |
| 851 | int ret; |
| 852 | mdp_super_t *tmp1, *tmp2; |
| 853 | |
| 854 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); |
| 855 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); |
| 856 | |
| 857 | if (!tmp1 || !tmp2) { |
| 858 | ret = 0; |
Andre Noll | 35020f1 | 2008-03-23 15:10:33 +0100 | [diff] [blame] | 859 | printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | goto abort; |
| 861 | } |
| 862 | |
| 863 | *tmp1 = *sb1; |
| 864 | *tmp2 = *sb2; |
| 865 | |
| 866 | /* |
| 867 | * nr_disks is not constant |
| 868 | */ |
| 869 | tmp1->nr_disks = 0; |
| 870 | tmp2->nr_disks = 0; |
| 871 | |
Andre Noll | ce0c8e0 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 872 | ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | abort: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 874 | kfree(tmp1); |
| 875 | kfree(tmp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | return ret; |
| 877 | } |
| 878 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 879 | |
| 880 | static u32 md_csum_fold(u32 csum) |
| 881 | { |
| 882 | csum = (csum & 0xffff) + (csum >> 16); |
| 883 | return (csum & 0xffff) + (csum >> 16); |
| 884 | } |
| 885 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | static unsigned int calc_sb_csum(mdp_super_t * sb) |
| 887 | { |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 888 | u64 newcsum = 0; |
| 889 | u32 *sb32 = (u32*)sb; |
| 890 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | unsigned int disk_csum, csum; |
| 892 | |
| 893 | disk_csum = sb->sb_csum; |
| 894 | sb->sb_csum = 0; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 895 | |
| 896 | for (i = 0; i < MD_SB_BYTES/4 ; i++) |
| 897 | newcsum += sb32[i]; |
| 898 | csum = (newcsum & 0xffffffff) + (newcsum>>32); |
| 899 | |
| 900 | |
| 901 | #ifdef CONFIG_ALPHA |
| 902 | /* This used to use csum_partial, which was wrong for several |
| 903 | * reasons including that different results are returned on |
| 904 | * different architectures. It isn't critical that we get exactly |
| 905 | * the same return value as before (we always csum_fold before |
| 906 | * testing, and that removes any differences). However as we |
| 907 | * know that csum_partial always returned a 16bit value on |
| 908 | * alphas, do a fold to maximise conformity to previous behaviour. |
| 909 | */ |
| 910 | sb->sb_csum = md_csum_fold(disk_csum); |
| 911 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | sb->sb_csum = disk_csum; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 913 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | return csum; |
| 915 | } |
| 916 | |
| 917 | |
| 918 | /* |
| 919 | * Handle superblock details. |
| 920 | * We want to be able to handle multiple superblock formats |
| 921 | * so we have a common interface to them all, and an array of |
| 922 | * different handlers. |
| 923 | * We rely on user-space to write the initial superblock, and support |
| 924 | * reading and updating of superblocks. |
| 925 | * Interface methods are: |
| 926 | * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version) |
| 927 | * loads and validates a superblock on dev. |
| 928 | * if refdev != NULL, compare superblocks on both devices |
| 929 | * Return: |
| 930 | * 0 - dev has a superblock that is compatible with refdev |
| 931 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 932 | * so dev should be used as the refdev in future |
| 933 | * -EINVAL superblock incompatible or invalid |
| 934 | * -othererror e.g. -EIO |
| 935 | * |
| 936 | * int validate_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 937 | * Verify that dev is acceptable into mddev. |
| 938 | * The first time, mddev->raid_disks will be 0, and data from |
| 939 | * dev should be merged in. Subsequent calls check that dev |
| 940 | * is new enough. Return 0 or -EINVAL |
| 941 | * |
| 942 | * void sync_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 943 | * Update the superblock for rdev with data in mddev |
| 944 | * This does not write to disc. |
| 945 | * |
| 946 | */ |
| 947 | |
| 948 | struct super_type { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 949 | char *name; |
| 950 | struct module *owner; |
| 951 | int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, |
| 952 | int minor_version); |
| 953 | int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 954 | void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 955 | unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev, |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 956 | sector_t num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | }; |
| 958 | |
| 959 | /* |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 960 | * Check that the given mddev has no bitmap. |
| 961 | * |
| 962 | * This function is called from the run method of all personalities that do not |
| 963 | * support bitmaps. It prints an error message and returns non-zero if mddev |
| 964 | * has a bitmap. Otherwise, it returns 0. |
| 965 | * |
| 966 | */ |
| 967 | int md_check_no_bitmap(mddev_t *mddev) |
| 968 | { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 969 | if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 970 | return 0; |
| 971 | printk(KERN_ERR "%s: bitmaps are not supported for %s\n", |
| 972 | mdname(mddev), mddev->pers->name); |
| 973 | return 1; |
| 974 | } |
| 975 | EXPORT_SYMBOL(md_check_no_bitmap); |
| 976 | |
| 977 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | * load_super for 0.90.0 |
| 979 | */ |
| 980 | static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 981 | { |
| 982 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 983 | mdp_super_t *sb; |
| 984 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | |
| 986 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 987 | * Calculate the position of the superblock (512byte sectors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | * it's at the end of the disk. |
| 989 | * |
| 990 | * It also happens to be a multiple of 4Kb. |
| 991 | */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 992 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 994 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | if (ret) return ret; |
| 996 | |
| 997 | ret = -EINVAL; |
| 998 | |
| 999 | bdevname(rdev->bdev, b); |
| 1000 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 1001 | |
| 1002 | if (sb->md_magic != MD_SB_MAGIC) { |
| 1003 | printk(KERN_ERR "md: invalid raid superblock magic on %s\n", |
| 1004 | b); |
| 1005 | goto abort; |
| 1006 | } |
| 1007 | |
| 1008 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1009 | sb->minor_version < 90 || |
| 1010 | sb->minor_version > 91) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | printk(KERN_WARNING "Bad version number %d.%d on %s\n", |
| 1012 | sb->major_version, sb->minor_version, |
| 1013 | b); |
| 1014 | goto abort; |
| 1015 | } |
| 1016 | |
| 1017 | if (sb->raid_disks <= 0) |
| 1018 | goto abort; |
| 1019 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 1020 | 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] | 1021 | printk(KERN_WARNING "md: invalid superblock checksum on %s\n", |
| 1022 | b); |
| 1023 | goto abort; |
| 1024 | } |
| 1025 | |
| 1026 | rdev->preferred_minor = sb->md_minor; |
| 1027 | rdev->data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1028 | rdev->sb_size = MD_SB_BYTES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | |
| 1030 | if (sb->level == LEVEL_MULTIPATH) |
| 1031 | rdev->desc_nr = -1; |
| 1032 | else |
| 1033 | rdev->desc_nr = sb->this_disk.number; |
| 1034 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1035 | if (!refdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1037 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | __u64 ev1, ev2; |
| 1039 | mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page); |
| 1040 | if (!uuid_equal(refsb, sb)) { |
| 1041 | printk(KERN_WARNING "md: %s has different UUID to %s\n", |
| 1042 | b, bdevname(refdev->bdev,b2)); |
| 1043 | goto abort; |
| 1044 | } |
| 1045 | if (!sb_equal(refsb, sb)) { |
| 1046 | printk(KERN_WARNING "md: %s has same UUID" |
| 1047 | " but different superblock to %s\n", |
| 1048 | b, bdevname(refdev->bdev, b2)); |
| 1049 | goto abort; |
| 1050 | } |
| 1051 | ev1 = md_event(sb); |
| 1052 | ev2 = md_event(refsb); |
| 1053 | if (ev1 > ev2) |
| 1054 | ret = 1; |
| 1055 | else |
| 1056 | ret = 0; |
| 1057 | } |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 1058 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1060 | if (rdev->sectors < sb->size * 2 && sb->level > 1) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1061 | /* "this cannot possibly happen" ... */ |
| 1062 | ret = -EINVAL; |
| 1063 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | abort: |
| 1065 | return ret; |
| 1066 | } |
| 1067 | |
| 1068 | /* |
| 1069 | * validate_super for 0.90.0 |
| 1070 | */ |
| 1071 | static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1072 | { |
| 1073 | mdp_disk_t *desc; |
| 1074 | mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1075 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1077 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1078 | clear_bit(Faulty, &rdev->flags); |
| 1079 | clear_bit(In_sync, &rdev->flags); |
| 1080 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1081 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | if (mddev->raid_disks == 0) { |
| 1083 | mddev->major_version = 0; |
| 1084 | mddev->minor_version = sb->minor_version; |
| 1085 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1086 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1087 | mddev->chunk_sectors = sb->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1088 | mddev->ctime = sb->ctime; |
| 1089 | mddev->utime = sb->utime; |
| 1090 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1091 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1092 | mddev->layout = sb->layout; |
| 1093 | mddev->raid_disks = sb->raid_disks; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1094 | mddev->dev_sectors = sb->size * 2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1095 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1096 | mddev->bitmap_info.offset = 0; |
| 1097 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1098 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1099 | if (mddev->minor_version >= 91) { |
| 1100 | mddev->reshape_position = sb->reshape_position; |
| 1101 | mddev->delta_disks = sb->delta_disks; |
| 1102 | mddev->new_level = sb->new_level; |
| 1103 | mddev->new_layout = sb->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1104 | mddev->new_chunk_sectors = sb->new_chunk >> 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1105 | } else { |
| 1106 | mddev->reshape_position = MaxSector; |
| 1107 | mddev->delta_disks = 0; |
| 1108 | mddev->new_level = mddev->level; |
| 1109 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1110 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1111 | } |
| 1112 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 1114 | mddev->recovery_cp = MaxSector; |
| 1115 | else { |
| 1116 | if (sb->events_hi == sb->cp_events_hi && |
| 1117 | sb->events_lo == sb->cp_events_lo) { |
| 1118 | mddev->recovery_cp = sb->recovery_cp; |
| 1119 | } else |
| 1120 | mddev->recovery_cp = 0; |
| 1121 | } |
| 1122 | |
| 1123 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 1124 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 1125 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 1126 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 1127 | |
| 1128 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1129 | |
| 1130 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1131 | mddev->bitmap_info.file == NULL) |
| 1132 | mddev->bitmap_info.offset = |
| 1133 | mddev->bitmap_info.default_offset; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1134 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1135 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1136 | /* Insist on good event counter while assembling, except |
| 1137 | * for spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1139 | if (sb->disks[rdev->desc_nr].state & ( |
| 1140 | (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE))) |
| 1141 | if (ev1 < mddev->events) |
| 1142 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1143 | } else if (mddev->bitmap) { |
| 1144 | /* if adding to array with a bitmap, then we can accept an |
| 1145 | * older device ... but not too old. |
| 1146 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1147 | if (ev1 < mddev->bitmap->events_cleared) |
| 1148 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1149 | } else { |
| 1150 | if (ev1 < mddev->events) |
| 1151 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1152 | return 0; |
| 1153 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1154 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1155 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1156 | desc = sb->disks + rdev->desc_nr; |
| 1157 | |
| 1158 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1159 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 1160 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 1161 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1162 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1163 | rdev->raid_disk = desc->raid_disk; |
NeilBrown | 0261cd9 | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1164 | } else if (desc->state & (1<<MD_DISK_ACTIVE)) { |
| 1165 | /* active but not in sync implies recovery up to |
| 1166 | * reshape position. We don't know exactly where |
| 1167 | * that is, so set to zero for now */ |
| 1168 | if (mddev->minor_version >= 91) { |
| 1169 | rdev->recovery_offset = 0; |
| 1170 | rdev->raid_disk = desc->raid_disk; |
| 1171 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1173 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 1174 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1175 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1176 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | return 0; |
| 1178 | } |
| 1179 | |
| 1180 | /* |
| 1181 | * sync_super for 0.90.0 |
| 1182 | */ |
| 1183 | static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1184 | { |
| 1185 | mdp_super_t *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | mdk_rdev_t *rdev2; |
| 1187 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1188 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | |
| 1190 | /* make rdev->sb match mddev data.. |
| 1191 | * |
| 1192 | * 1/ zero out disks |
| 1193 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 1194 | * 3/ any empty disks < next_spare become removed |
| 1195 | * |
| 1196 | * disks[0] gets initialised to REMOVED because |
| 1197 | * we cannot be sure from other fields if it has |
| 1198 | * been initialised or not. |
| 1199 | */ |
| 1200 | int i; |
| 1201 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 1202 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 1203 | rdev->sb_size = MD_SB_BYTES; |
| 1204 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 1206 | |
| 1207 | memset(sb, 0, sizeof(*sb)); |
| 1208 | |
| 1209 | sb->md_magic = MD_SB_MAGIC; |
| 1210 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1211 | sb->patch_version = mddev->patch_version; |
| 1212 | sb->gvalid_words = 0; /* ignored */ |
| 1213 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 1214 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 1215 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 1216 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 1217 | |
| 1218 | sb->ctime = mddev->ctime; |
| 1219 | sb->level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1220 | sb->size = mddev->dev_sectors / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | sb->raid_disks = mddev->raid_disks; |
| 1222 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1223 | sb->not_persistent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1224 | sb->utime = mddev->utime; |
| 1225 | sb->state = 0; |
| 1226 | sb->events_hi = (mddev->events>>32); |
| 1227 | sb->events_lo = (u32)mddev->events; |
| 1228 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1229 | if (mddev->reshape_position == MaxSector) |
| 1230 | sb->minor_version = 90; |
| 1231 | else { |
| 1232 | sb->minor_version = 91; |
| 1233 | sb->reshape_position = mddev->reshape_position; |
| 1234 | sb->new_level = mddev->new_level; |
| 1235 | sb->delta_disks = mddev->delta_disks; |
| 1236 | sb->new_layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1237 | sb->new_chunk = mddev->new_chunk_sectors << 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1238 | } |
| 1239 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | if (mddev->in_sync) |
| 1241 | { |
| 1242 | sb->recovery_cp = mddev->recovery_cp; |
| 1243 | sb->cp_events_hi = (mddev->events>>32); |
| 1244 | sb->cp_events_lo = (u32)mddev->events; |
| 1245 | if (mddev->recovery_cp == MaxSector) |
| 1246 | sb->state = (1<< MD_SB_CLEAN); |
| 1247 | } else |
| 1248 | sb->recovery_cp = 0; |
| 1249 | |
| 1250 | sb->layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1251 | sb->chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1253 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1254 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 1255 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1257 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1259 | int desc_nr; |
NeilBrown | 0261cd9 | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1260 | int is_active = test_bit(In_sync, &rdev2->flags); |
| 1261 | |
| 1262 | if (rdev2->raid_disk >= 0 && |
| 1263 | sb->minor_version >= 91) |
| 1264 | /* we have nowhere to store the recovery_offset, |
| 1265 | * but if it is not below the reshape_position, |
| 1266 | * we can piggy-back on that. |
| 1267 | */ |
| 1268 | is_active = 1; |
| 1269 | if (rdev2->raid_disk < 0 || |
| 1270 | test_bit(Faulty, &rdev2->flags)) |
| 1271 | is_active = 0; |
| 1272 | if (is_active) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1273 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1274 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1275 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1276 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1277 | d = &sb->disks[rdev2->desc_nr]; |
| 1278 | nr_disks++; |
| 1279 | d->number = rdev2->desc_nr; |
| 1280 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 1281 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | 0261cd9 | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1282 | if (is_active) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | d->raid_disk = rdev2->raid_disk; |
| 1284 | else |
| 1285 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 1286 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 0261cd9 | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1288 | else if (is_active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1289 | d->state = (1<<MD_DISK_ACTIVE); |
NeilBrown | 0261cd9 | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1290 | if (test_bit(In_sync, &rdev2->flags)) |
| 1291 | d->state |= (1<<MD_DISK_SYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1292 | active++; |
| 1293 | working++; |
| 1294 | } else { |
| 1295 | d->state = 0; |
| 1296 | spare++; |
| 1297 | working++; |
| 1298 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1299 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 1300 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1301 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 1303 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 1304 | mdp_disk_t *d = &sb->disks[i]; |
| 1305 | if (d->state == 0 && d->number == 0) { |
| 1306 | d->number = i; |
| 1307 | d->raid_disk = i; |
| 1308 | d->state = (1<<MD_DISK_REMOVED); |
| 1309 | d->state |= (1<<MD_DISK_FAULTY); |
| 1310 | failed++; |
| 1311 | } |
| 1312 | } |
| 1313 | sb->nr_disks = nr_disks; |
| 1314 | sb->active_disks = active; |
| 1315 | sb->working_disks = working; |
| 1316 | sb->failed_disks = failed; |
| 1317 | sb->spare_disks = spare; |
| 1318 | |
| 1319 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 1320 | sb->sb_csum = calc_sb_csum(sb); |
| 1321 | } |
| 1322 | |
| 1323 | /* |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1324 | * rdev_size_change for 0.90.0 |
| 1325 | */ |
| 1326 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1327 | 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] | 1328 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1329 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1330 | return 0; /* component must fit device */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1331 | if (rdev->mddev->bitmap_info.offset) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1332 | return 0; /* can't move bitmap */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1333 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1334 | if (!num_sectors || num_sectors > rdev->sb_start) |
| 1335 | num_sectors = rdev->sb_start; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1336 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1337 | rdev->sb_page); |
| 1338 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1339 | return num_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1340 | } |
| 1341 | |
| 1342 | |
| 1343 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1344 | * version 1 superblock |
| 1345 | */ |
| 1346 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1347 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1348 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1349 | __le32 disk_csum; |
| 1350 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1351 | unsigned long long newcsum; |
| 1352 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1353 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1354 | int i; |
| 1355 | |
| 1356 | disk_csum = sb->sb_csum; |
| 1357 | sb->sb_csum = 0; |
| 1358 | newcsum = 0; |
| 1359 | for (i=0; size>=4; size -= 4 ) |
| 1360 | newcsum += le32_to_cpu(*isuper++); |
| 1361 | |
| 1362 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1363 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1364 | |
| 1365 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1366 | sb->sb_csum = disk_csum; |
| 1367 | return cpu_to_le32(csum); |
| 1368 | } |
| 1369 | |
| 1370 | static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 1371 | { |
| 1372 | struct mdp_superblock_1 *sb; |
| 1373 | int ret; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1374 | sector_t sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1376 | int bmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1377 | |
| 1378 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1379 | * Calculate the position of the superblock in 512byte sectors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1380 | * It is always aligned to a 4K boundary and |
| 1381 | * depeding on minor_version, it can be: |
| 1382 | * 0: At least 8K, but less than 12K, from end of device |
| 1383 | * 1: At start of device |
| 1384 | * 2: 4K from start of device. |
| 1385 | */ |
| 1386 | switch(minor_version) { |
| 1387 | case 0: |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1388 | sb_start = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1389 | sb_start -= 8*2; |
| 1390 | sb_start &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | break; |
| 1392 | case 1: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1393 | sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1394 | break; |
| 1395 | case 2: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1396 | sb_start = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1397 | break; |
| 1398 | default: |
| 1399 | return -EINVAL; |
| 1400 | } |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1401 | rdev->sb_start = sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1403 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1404 | * and it is safe to read 4k, so we do that |
| 1405 | */ |
| 1406 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1407 | if (ret) return ret; |
| 1408 | |
| 1409 | |
| 1410 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1411 | |
| 1412 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1413 | sb->major_version != cpu_to_le32(1) || |
| 1414 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1415 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1416 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1417 | return -EINVAL; |
| 1418 | |
| 1419 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
| 1420 | printk("md: invalid superblock checksum on %s\n", |
| 1421 | bdevname(rdev->bdev,b)); |
| 1422 | return -EINVAL; |
| 1423 | } |
| 1424 | if (le64_to_cpu(sb->data_size) < 10) { |
| 1425 | printk("md: data_size too small on %s\n", |
| 1426 | bdevname(rdev->bdev,b)); |
| 1427 | return -EINVAL; |
| 1428 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1429 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1430 | rdev->preferred_minor = 0xffff; |
| 1431 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1432 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1433 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1434 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1435 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1436 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1437 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1438 | |
| 1439 | if (minor_version |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1440 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1441 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1442 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1443 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1444 | rdev->desc_nr = -1; |
| 1445 | else |
| 1446 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1447 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1448 | if (!refdev) { |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1449 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1450 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | __u64 ev1, ev2; |
| 1452 | struct mdp_superblock_1 *refsb = |
| 1453 | (struct mdp_superblock_1*)page_address(refdev->sb_page); |
| 1454 | |
| 1455 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1456 | sb->level != refsb->level || |
| 1457 | sb->layout != refsb->layout || |
| 1458 | sb->chunksize != refsb->chunksize) { |
| 1459 | printk(KERN_WARNING "md: %s has strangely different" |
| 1460 | " superblock to %s\n", |
| 1461 | bdevname(rdev->bdev,b), |
| 1462 | bdevname(refdev->bdev,b2)); |
| 1463 | return -EINVAL; |
| 1464 | } |
| 1465 | ev1 = le64_to_cpu(sb->events); |
| 1466 | ev2 = le64_to_cpu(refsb->events); |
| 1467 | |
| 1468 | if (ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1469 | ret = 1; |
| 1470 | else |
| 1471 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | } |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1473 | if (minor_version) |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1474 | rdev->sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) - |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1475 | le64_to_cpu(sb->data_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1476 | else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1477 | rdev->sectors = rdev->sb_start; |
| 1478 | if (rdev->sectors < le64_to_cpu(sb->data_size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1479 | return -EINVAL; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1480 | rdev->sectors = le64_to_cpu(sb->data_size); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1481 | if (le64_to_cpu(sb->size) > rdev->sectors) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1482 | return -EINVAL; |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1483 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1484 | } |
| 1485 | |
| 1486 | static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1487 | { |
| 1488 | 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] | 1489 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1490 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1491 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1492 | clear_bit(Faulty, &rdev->flags); |
| 1493 | clear_bit(In_sync, &rdev->flags); |
| 1494 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1495 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1496 | if (mddev->raid_disks == 0) { |
| 1497 | mddev->major_version = 1; |
| 1498 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1499 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1500 | mddev->chunk_sectors = le32_to_cpu(sb->chunksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1501 | mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1); |
| 1502 | mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1); |
| 1503 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1504 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1505 | mddev->layout = le32_to_cpu(sb->layout); |
| 1506 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1507 | mddev->dev_sectors = le64_to_cpu(sb->size); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1508 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1509 | mddev->bitmap_info.offset = 0; |
| 1510 | mddev->bitmap_info.default_offset = 1024 >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1511 | |
| 1512 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1513 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1514 | |
| 1515 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1516 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1517 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1518 | mddev->bitmap_info.file == NULL ) |
| 1519 | mddev->bitmap_info.offset = |
| 1520 | (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1521 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1522 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1523 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1524 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1525 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1526 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1527 | mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1528 | } else { |
| 1529 | mddev->reshape_position = MaxSector; |
| 1530 | mddev->delta_disks = 0; |
| 1531 | mddev->new_level = mddev->level; |
| 1532 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1533 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1534 | } |
| 1535 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1536 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1537 | /* Insist of good event counter while assembling, except for |
| 1538 | * spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1539 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1540 | if (rdev->desc_nr >= 0 && |
| 1541 | rdev->desc_nr < le32_to_cpu(sb->max_dev) && |
| 1542 | le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe) |
| 1543 | if (ev1 < mddev->events) |
| 1544 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1545 | } else if (mddev->bitmap) { |
| 1546 | /* If adding to array with a bitmap, then we can accept an |
| 1547 | * older device, but not too old. |
| 1548 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1549 | if (ev1 < mddev->bitmap->events_cleared) |
| 1550 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1551 | } else { |
| 1552 | if (ev1 < mddev->events) |
| 1553 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1554 | return 0; |
| 1555 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1556 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1557 | int role; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1558 | if (rdev->desc_nr < 0 || |
| 1559 | rdev->desc_nr >= le32_to_cpu(sb->max_dev)) { |
| 1560 | role = 0xffff; |
| 1561 | rdev->desc_nr = -1; |
| 1562 | } else |
| 1563 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1564 | switch(role) { |
| 1565 | case 0xffff: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1566 | break; |
| 1567 | case 0xfffe: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1568 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1569 | break; |
| 1570 | default: |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1571 | if ((le32_to_cpu(sb->feature_map) & |
| 1572 | MD_FEATURE_RECOVERY_OFFSET)) |
| 1573 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 1574 | else |
| 1575 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1576 | rdev->raid_disk = role; |
| 1577 | break; |
| 1578 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1579 | if (sb->devflags & WriteMostly1) |
| 1580 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1581 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1582 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1583 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1584 | return 0; |
| 1585 | } |
| 1586 | |
| 1587 | static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1588 | { |
| 1589 | struct mdp_superblock_1 *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1590 | mdk_rdev_t *rdev2; |
| 1591 | int max_dev, i; |
| 1592 | /* make rdev->sb match mddev and rdev data. */ |
| 1593 | |
| 1594 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1595 | |
| 1596 | sb->feature_map = 0; |
| 1597 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1598 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1599 | memset(sb->pad1, 0, sizeof(sb->pad1)); |
| 1600 | memset(sb->pad2, 0, sizeof(sb->pad2)); |
| 1601 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1602 | |
| 1603 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1604 | sb->events = cpu_to_le64(mddev->events); |
| 1605 | if (mddev->in_sync) |
| 1606 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
| 1607 | else |
| 1608 | sb->resync_offset = cpu_to_le64(0); |
| 1609 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1610 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1611 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1612 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1613 | sb->size = cpu_to_le64(mddev->dev_sectors); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1614 | sb->chunksize = cpu_to_le32(mddev->chunk_sectors); |
NeilBrown | 62e1e38 | 2009-05-26 09:40:59 +1000 | [diff] [blame] | 1615 | sb->level = cpu_to_le32(mddev->level); |
| 1616 | sb->layout = cpu_to_le32(mddev->layout); |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1617 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1618 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) { |
| 1619 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1620 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1621 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1622 | |
| 1623 | if (rdev->raid_disk >= 0 && |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1624 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1625 | sb->feature_map |= |
| 1626 | cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 1627 | sb->recovery_offset = |
| 1628 | cpu_to_le64(rdev->recovery_offset); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1629 | } |
| 1630 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1631 | if (mddev->reshape_position != MaxSector) { |
| 1632 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 1633 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1634 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1635 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1636 | sb->new_level = cpu_to_le32(mddev->new_level); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1637 | sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1638 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1639 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1640 | max_dev = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1641 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1642 | if (rdev2->desc_nr+1 > max_dev) |
| 1643 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1644 | |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1645 | if (max_dev > le32_to_cpu(sb->max_dev)) { |
| 1646 | int bmask; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1647 | sb->max_dev = cpu_to_le32(max_dev); |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1648 | rdev->sb_size = max_dev * 2 + 256; |
| 1649 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
| 1650 | if (rdev->sb_size & bmask) |
| 1651 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
NeilBrown | ddcf352 | 2010-09-08 16:48:17 +1000 | [diff] [blame] | 1652 | } else |
| 1653 | max_dev = le32_to_cpu(sb->max_dev); |
| 1654 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1655 | for (i=0; i<max_dev;i++) |
| 1656 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
| 1657 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1658 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1659 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1660 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1661 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1662 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1663 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1664 | else if (rdev2->raid_disk >= 0) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1665 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1666 | else |
| 1667 | sb->dev_roles[i] = cpu_to_le16(0xffff); |
| 1668 | } |
| 1669 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1670 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1671 | } |
| 1672 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1673 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1674 | 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] | 1675 | { |
| 1676 | struct mdp_superblock_1 *sb; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1677 | sector_t max_sectors; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1678 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1679 | return 0; /* component must fit device */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1680 | if (rdev->sb_start < rdev->data_offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1681 | /* minor versions 1 and 2; superblock before data */ |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1682 | max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1683 | max_sectors -= rdev->data_offset; |
| 1684 | if (!num_sectors || num_sectors > max_sectors) |
| 1685 | num_sectors = max_sectors; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1686 | } else if (rdev->mddev->bitmap_info.offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1687 | /* minor version 0 with bitmap we can't move */ |
| 1688 | return 0; |
| 1689 | } else { |
| 1690 | /* minor version 0; superblock after data */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1691 | sector_t sb_start; |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1692 | sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1693 | sb_start &= ~(sector_t)(4*2 - 1); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1694 | max_sectors = rdev->sectors + sb_start - rdev->sb_start; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1695 | if (!num_sectors || num_sectors > max_sectors) |
| 1696 | num_sectors = max_sectors; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1697 | rdev->sb_start = sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1698 | } |
| 1699 | sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1700 | sb->data_size = cpu_to_le64(num_sectors); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1701 | sb->super_offset = rdev->sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1702 | sb->sb_csum = calc_sb_1_csum(sb); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1703 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1704 | rdev->sb_page); |
| 1705 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1706 | return num_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1707 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1708 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 1709 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1710 | [0] = { |
| 1711 | .name = "0.90.0", |
| 1712 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1713 | .load_super = super_90_load, |
| 1714 | .validate_super = super_90_validate, |
| 1715 | .sync_super = super_90_sync, |
| 1716 | .rdev_size_change = super_90_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1717 | }, |
| 1718 | [1] = { |
| 1719 | .name = "md-1", |
| 1720 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1721 | .load_super = super_1_load, |
| 1722 | .validate_super = super_1_validate, |
| 1723 | .sync_super = super_1_sync, |
| 1724 | .rdev_size_change = super_1_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1725 | }, |
| 1726 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1727 | |
| 1728 | static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2) |
| 1729 | { |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1730 | mdk_rdev_t *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1731 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1732 | rcu_read_lock(); |
| 1733 | rdev_for_each_rcu(rdev, mddev1) |
| 1734 | rdev_for_each_rcu(rdev2, mddev2) |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1735 | if (rdev->bdev->bd_contains == |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1736 | rdev2->bdev->bd_contains) { |
| 1737 | rcu_read_unlock(); |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1738 | return 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1739 | } |
| 1740 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1741 | return 0; |
| 1742 | } |
| 1743 | |
| 1744 | static LIST_HEAD(pending_raid_disks); |
| 1745 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1746 | /* |
| 1747 | * Try to register data integrity profile for an mddev |
| 1748 | * |
| 1749 | * This is called when an array is started and after a disk has been kicked |
| 1750 | * from the array. It only succeeds if all working and active component devices |
| 1751 | * are integrity capable with matching profiles. |
| 1752 | */ |
| 1753 | int md_integrity_register(mddev_t *mddev) |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1754 | { |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1755 | mdk_rdev_t *rdev, *reference = NULL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1756 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1757 | if (list_empty(&mddev->disks)) |
| 1758 | return 0; /* nothing to do */ |
| 1759 | if (blk_get_integrity(mddev->gendisk)) |
| 1760 | return 0; /* already registered */ |
| 1761 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 1762 | /* skip spares and non-functional disks */ |
| 1763 | if (test_bit(Faulty, &rdev->flags)) |
| 1764 | continue; |
| 1765 | if (rdev->raid_disk < 0) |
| 1766 | continue; |
| 1767 | /* |
| 1768 | * If at least one rdev is not integrity capable, we can not |
| 1769 | * enable data integrity for the md device. |
| 1770 | */ |
| 1771 | if (!bdev_get_integrity(rdev->bdev)) |
| 1772 | return -EINVAL; |
| 1773 | if (!reference) { |
| 1774 | /* Use the first rdev as the reference */ |
| 1775 | reference = rdev; |
| 1776 | continue; |
| 1777 | } |
| 1778 | /* does this rdev's profile match the reference profile? */ |
| 1779 | if (blk_integrity_compare(reference->bdev->bd_disk, |
| 1780 | rdev->bdev->bd_disk) < 0) |
| 1781 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1782 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1783 | /* |
| 1784 | * All component devices are integrity capable and have matching |
| 1785 | * profiles, register the common profile for the md device. |
| 1786 | */ |
| 1787 | if (blk_integrity_register(mddev->gendisk, |
| 1788 | bdev_get_integrity(reference->bdev)) != 0) { |
| 1789 | printk(KERN_ERR "md: failed to register integrity for %s\n", |
| 1790 | mdname(mddev)); |
| 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 | printk(KERN_NOTICE "md: data integrity on %s enabled\n", |
| 1794 | mdname(mddev)); |
| 1795 | return 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1796 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1797 | EXPORT_SYMBOL(md_integrity_register); |
| 1798 | |
| 1799 | /* Disable data integrity if non-capable/non-matching disk is being added */ |
| 1800 | void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 1801 | { |
| 1802 | struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev); |
| 1803 | struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk); |
| 1804 | |
| 1805 | if (!bi_mddev) /* nothing to do */ |
| 1806 | return; |
| 1807 | if (rdev->raid_disk < 0) /* skip spares */ |
| 1808 | return; |
| 1809 | if (bi_rdev && blk_integrity_compare(mddev->gendisk, |
| 1810 | rdev->bdev->bd_disk) >= 0) |
| 1811 | return; |
| 1812 | printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev)); |
| 1813 | blk_integrity_unregister(mddev->gendisk); |
| 1814 | } |
| 1815 | EXPORT_SYMBOL(md_integrity_add_rdev); |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1816 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1817 | static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev) |
| 1818 | { |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1819 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 1820 | struct kobject *ko; |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1821 | char *s; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1822 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1823 | |
| 1824 | if (rdev->mddev) { |
| 1825 | MD_BUG(); |
| 1826 | return -EINVAL; |
| 1827 | } |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1828 | |
| 1829 | /* prevent duplicates */ |
| 1830 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 1831 | return -EEXIST; |
| 1832 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1833 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
| 1834 | if (rdev->sectors && (mddev->dev_sectors == 0 || |
| 1835 | rdev->sectors < mddev->dev_sectors)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1836 | if (mddev->pers) { |
| 1837 | /* Cannot change size, so fail |
| 1838 | * If mddev->level <= 0, then we don't care |
| 1839 | * about aligning sizes (e.g. linear) |
| 1840 | */ |
| 1841 | if (mddev->level > 0) |
| 1842 | return -ENOSPC; |
| 1843 | } else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1844 | mddev->dev_sectors = rdev->sectors; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1845 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1846 | |
| 1847 | /* Verify rdev->desc_nr is unique. |
| 1848 | * If it is -1, assign a free number, else |
| 1849 | * check number is not in use |
| 1850 | */ |
| 1851 | if (rdev->desc_nr < 0) { |
| 1852 | int choice = 0; |
| 1853 | if (mddev->pers) choice = mddev->raid_disks; |
| 1854 | while (find_rdev_nr(mddev, choice)) |
| 1855 | choice++; |
| 1856 | rdev->desc_nr = choice; |
| 1857 | } else { |
| 1858 | if (find_rdev_nr(mddev, rdev->desc_nr)) |
| 1859 | return -EBUSY; |
| 1860 | } |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 1861 | if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
| 1862 | printk(KERN_WARNING "md: %s: array is limited to %d devices\n", |
| 1863 | mdname(mddev), mddev->max_disks); |
| 1864 | return -EBUSY; |
| 1865 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1866 | bdevname(rdev->bdev,b); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1867 | while ( (s=strchr(b, '/')) != NULL) |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1868 | *s = '!'; |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1869 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1870 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1871 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1872 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1873 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1874 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1875 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1876 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 1877 | if (sysfs_create_link(&rdev->kobj, ko, "block")) |
| 1878 | /* failure here is OK */; |
| 1879 | rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 1880 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1881 | list_add_rcu(&rdev->same_set, &mddev->disks); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1882 | bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk); |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 1883 | |
| 1884 | /* May as well allow recovery to be retried once */ |
| 1885 | mddev->recovery_disabled = 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1886 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1887 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1888 | |
| 1889 | fail: |
| 1890 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", |
| 1891 | b, mdname(mddev)); |
| 1892 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1893 | } |
| 1894 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1895 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1896 | { |
| 1897 | mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work); |
| 1898 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1899 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1900 | } |
| 1901 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1902 | static void unbind_rdev_from_array(mdk_rdev_t * rdev) |
| 1903 | { |
| 1904 | char b[BDEVNAME_SIZE]; |
| 1905 | if (!rdev->mddev) { |
| 1906 | MD_BUG(); |
| 1907 | return; |
| 1908 | } |
Jun'ichi Nomura | 5463c79 | 2006-03-27 01:17:58 -0800 | [diff] [blame] | 1909 | bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1910 | list_del_rcu(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1911 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 1912 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1913 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 1914 | sysfs_put(rdev->sysfs_state); |
| 1915 | rdev->sysfs_state = NULL; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1916 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1917 | * writing to 'remove' to "dev/state". We also need |
| 1918 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1919 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1920 | synchronize_rcu(); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1921 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 1922 | kobject_get(&rdev->kobj); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 1923 | queue_work(md_misc_wq, &rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1924 | } |
| 1925 | |
| 1926 | /* |
| 1927 | * prevent the device from being mounted, repartitioned or |
| 1928 | * otherwise reused by a RAID array (or any other kernel |
| 1929 | * subsystem), by bd_claiming the device. |
| 1930 | */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1931 | 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] | 1932 | { |
| 1933 | int err = 0; |
| 1934 | struct block_device *bdev; |
| 1935 | char b[BDEVNAME_SIZE]; |
| 1936 | |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1937 | bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1938 | if (IS_ERR(bdev)) { |
| 1939 | printk(KERN_ERR "md: could not open %s.\n", |
| 1940 | __bdevname(dev, b)); |
| 1941 | return PTR_ERR(bdev); |
| 1942 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1943 | err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1944 | if (err) { |
| 1945 | printk(KERN_ERR "md: could not bd_claim %s.\n", |
| 1946 | bdevname(bdev, b)); |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1947 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1948 | return err; |
| 1949 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1950 | if (!shared) |
| 1951 | set_bit(AllReserved, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1952 | rdev->bdev = bdev; |
| 1953 | return err; |
| 1954 | } |
| 1955 | |
| 1956 | static void unlock_rdev(mdk_rdev_t *rdev) |
| 1957 | { |
| 1958 | struct block_device *bdev = rdev->bdev; |
| 1959 | rdev->bdev = NULL; |
| 1960 | if (!bdev) |
| 1961 | MD_BUG(); |
| 1962 | bd_release(bdev); |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1963 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1964 | } |
| 1965 | |
| 1966 | void md_autodetect_dev(dev_t dev); |
| 1967 | |
| 1968 | static void export_rdev(mdk_rdev_t * rdev) |
| 1969 | { |
| 1970 | char b[BDEVNAME_SIZE]; |
| 1971 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 1972 | bdevname(rdev->bdev,b)); |
| 1973 | if (rdev->mddev) |
| 1974 | MD_BUG(); |
| 1975 | free_disk_sb(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1976 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1977 | if (test_bit(AutoDetected, &rdev->flags)) |
| 1978 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1979 | #endif |
| 1980 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1981 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1982 | } |
| 1983 | |
| 1984 | static void kick_rdev_from_array(mdk_rdev_t * rdev) |
| 1985 | { |
| 1986 | unbind_rdev_from_array(rdev); |
| 1987 | export_rdev(rdev); |
| 1988 | } |
| 1989 | |
| 1990 | static void export_array(mddev_t *mddev) |
| 1991 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1992 | mdk_rdev_t *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1993 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1994 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1995 | if (!rdev->mddev) { |
| 1996 | MD_BUG(); |
| 1997 | continue; |
| 1998 | } |
| 1999 | kick_rdev_from_array(rdev); |
| 2000 | } |
| 2001 | if (!list_empty(&mddev->disks)) |
| 2002 | MD_BUG(); |
| 2003 | mddev->raid_disks = 0; |
| 2004 | mddev->major_version = 0; |
| 2005 | } |
| 2006 | |
| 2007 | static void print_desc(mdp_disk_t *desc) |
| 2008 | { |
| 2009 | printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number, |
| 2010 | desc->major,desc->minor,desc->raid_disk,desc->state); |
| 2011 | } |
| 2012 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2013 | static void print_sb_90(mdp_super_t *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2014 | { |
| 2015 | int i; |
| 2016 | |
| 2017 | printk(KERN_INFO |
| 2018 | "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n", |
| 2019 | sb->major_version, sb->minor_version, sb->patch_version, |
| 2020 | sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3, |
| 2021 | sb->ctime); |
| 2022 | printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n", |
| 2023 | sb->level, sb->size, sb->nr_disks, sb->raid_disks, |
| 2024 | sb->md_minor, sb->layout, sb->chunk_size); |
| 2025 | printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d" |
| 2026 | " FD:%d SD:%d CSUM:%08x E:%08lx\n", |
| 2027 | sb->utime, sb->state, sb->active_disks, sb->working_disks, |
| 2028 | sb->failed_disks, sb->spare_disks, |
| 2029 | sb->sb_csum, (unsigned long)sb->events_lo); |
| 2030 | |
| 2031 | printk(KERN_INFO); |
| 2032 | for (i = 0; i < MD_SB_DISKS; i++) { |
| 2033 | mdp_disk_t *desc; |
| 2034 | |
| 2035 | desc = sb->disks + i; |
| 2036 | if (desc->number || desc->major || desc->minor || |
| 2037 | desc->raid_disk || (desc->state && (desc->state != 4))) { |
| 2038 | printk(" D %2d: ", i); |
| 2039 | print_desc(desc); |
| 2040 | } |
| 2041 | } |
| 2042 | printk(KERN_INFO "md: THIS: "); |
| 2043 | print_desc(&sb->this_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2044 | } |
| 2045 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2046 | static void print_sb_1(struct mdp_superblock_1 *sb) |
| 2047 | { |
| 2048 | __u8 *uuid; |
| 2049 | |
| 2050 | uuid = sb->set_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2051 | printk(KERN_INFO |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2052 | "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2053 | "md: Name: \"%s\" CT:%llu\n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2054 | le32_to_cpu(sb->major_version), |
| 2055 | le32_to_cpu(sb->feature_map), |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2056 | uuid, |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2057 | sb->set_name, |
| 2058 | (unsigned long long)le64_to_cpu(sb->ctime) |
| 2059 | & MD_SUPERBLOCK_1_TIME_SEC_MASK); |
| 2060 | |
| 2061 | uuid = sb->device_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2062 | printk(KERN_INFO |
| 2063 | "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] | 2064 | " RO:%llu\n" |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2065 | "md: Dev:%08x UUID: %pU\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2066 | "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n" |
| 2067 | "md: (MaxDev:%u) \n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2068 | le32_to_cpu(sb->level), |
| 2069 | (unsigned long long)le64_to_cpu(sb->size), |
| 2070 | le32_to_cpu(sb->raid_disks), |
| 2071 | le32_to_cpu(sb->layout), |
| 2072 | le32_to_cpu(sb->chunksize), |
| 2073 | (unsigned long long)le64_to_cpu(sb->data_offset), |
| 2074 | (unsigned long long)le64_to_cpu(sb->data_size), |
| 2075 | (unsigned long long)le64_to_cpu(sb->super_offset), |
| 2076 | (unsigned long long)le64_to_cpu(sb->recovery_offset), |
| 2077 | le32_to_cpu(sb->dev_number), |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2078 | uuid, |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2079 | sb->devflags, |
| 2080 | (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK, |
| 2081 | (unsigned long long)le64_to_cpu(sb->events), |
| 2082 | (unsigned long long)le64_to_cpu(sb->resync_offset), |
| 2083 | le32_to_cpu(sb->sb_csum), |
| 2084 | le32_to_cpu(sb->max_dev) |
| 2085 | ); |
| 2086 | } |
| 2087 | |
| 2088 | static void print_rdev(mdk_rdev_t *rdev, int major_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2089 | { |
| 2090 | char b[BDEVNAME_SIZE]; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2091 | printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n", |
| 2092 | bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors, |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2093 | test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags), |
| 2094 | rdev->desc_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2095 | if (rdev->sb_loaded) { |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2096 | printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version); |
| 2097 | switch (major_version) { |
| 2098 | case 0: |
| 2099 | print_sb_90((mdp_super_t*)page_address(rdev->sb_page)); |
| 2100 | break; |
| 2101 | case 1: |
| 2102 | print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page)); |
| 2103 | break; |
| 2104 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2105 | } else |
| 2106 | printk(KERN_INFO "md: no rdev superblock!\n"); |
| 2107 | } |
| 2108 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 2109 | static void md_print_devices(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2110 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2111 | struct list_head *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2112 | mdk_rdev_t *rdev; |
| 2113 | mddev_t *mddev; |
| 2114 | char b[BDEVNAME_SIZE]; |
| 2115 | |
| 2116 | printk("\n"); |
| 2117 | printk("md: **********************************\n"); |
| 2118 | printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n"); |
| 2119 | printk("md: **********************************\n"); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2120 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2121 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 2122 | if (mddev->bitmap) |
| 2123 | bitmap_print_sb(mddev->bitmap); |
| 2124 | else |
| 2125 | printk("%s: ", mdname(mddev)); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2126 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2127 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 2128 | printk("\n"); |
| 2129 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2130 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2131 | print_rdev(rdev, mddev->major_version); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2132 | } |
| 2133 | printk("md: **********************************\n"); |
| 2134 | printk("\n"); |
| 2135 | } |
| 2136 | |
| 2137 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2138 | static void sync_sbs(mddev_t * mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2139 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2140 | /* Update each superblock (in-memory image), but |
| 2141 | * if we are allowed to, skip spares which already |
| 2142 | * have the right event counter, or have one earlier |
| 2143 | * (which would mean they aren't being marked as dirty |
| 2144 | * with the rest of the array) |
| 2145 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2146 | mdk_rdev_t *rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2147 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2148 | if (rdev->sb_events == mddev->events || |
| 2149 | (nospares && |
| 2150 | rdev->raid_disk < 0 && |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2151 | rdev->sb_events+1 == mddev->events)) { |
| 2152 | /* Don't update this superblock */ |
| 2153 | rdev->sb_loaded = 2; |
| 2154 | } else { |
| 2155 | super_types[mddev->major_version]. |
| 2156 | sync_super(mddev, rdev); |
| 2157 | rdev->sb_loaded = 1; |
| 2158 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2159 | } |
| 2160 | } |
| 2161 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2162 | static void md_update_sb(mddev_t * mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2163 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2164 | mdk_rdev_t *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2165 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2166 | int nospares = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2167 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2168 | repeat: |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2169 | /* First make sure individual recovery_offsets are correct */ |
| 2170 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 2171 | if (rdev->raid_disk >= 0 && |
| 2172 | mddev->delta_disks >= 0 && |
| 2173 | !test_bit(In_sync, &rdev->flags) && |
| 2174 | mddev->curr_resync_completed > rdev->recovery_offset) |
| 2175 | rdev->recovery_offset = mddev->curr_resync_completed; |
| 2176 | |
| 2177 | } |
Dan Williams | bd52b74 | 2010-08-30 17:33:33 +1000 | [diff] [blame] | 2178 | if (!mddev->persistent) { |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 2179 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2180 | clear_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | d97a41d | 2010-10-28 17:30:20 +1100 | [diff] [blame] | 2181 | if (!mddev->external) |
| 2182 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2183 | wake_up(&mddev->sb_wait); |
| 2184 | return; |
| 2185 | } |
| 2186 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2187 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2188 | |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2189 | mddev->utime = get_seconds(); |
| 2190 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2191 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 2192 | force_change = 1; |
| 2193 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 2194 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 2195 | * though if events isn't the right even/odd, we will have to do |
| 2196 | * spares after all |
| 2197 | */ |
| 2198 | nospares = 1; |
| 2199 | if (force_change) |
| 2200 | nospares = 0; |
| 2201 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2202 | /* If the array is degraded, then skipping spares is both |
| 2203 | * dangerous and fairly pointless. |
| 2204 | * Dangerous because a device that was removed from the array |
| 2205 | * might have a event_count that still looks up-to-date, |
| 2206 | * so it can be re-added without a resync. |
| 2207 | * Pointless because if there are any spares to skip, |
| 2208 | * then a recovery will happen and soon that array won't |
| 2209 | * be degraded any more and the spare can go back to sleep then. |
| 2210 | */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2211 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2212 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2213 | sync_req = mddev->in_sync; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2214 | |
| 2215 | /* If this is just a dirty<->clean transition, and the array is clean |
| 2216 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2217 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2218 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2219 | && mddev->can_decrease_events |
| 2220 | && mddev->events != 1) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2221 | mddev->events--; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2222 | mddev->can_decrease_events = 0; |
| 2223 | } else { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2224 | /* otherwise we have to go forward and ... */ |
| 2225 | mddev->events ++; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2226 | mddev->can_decrease_events = nospares; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2227 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2228 | |
| 2229 | if (!mddev->events) { |
| 2230 | /* |
| 2231 | * oops, this 64-bit counter should never wrap. |
| 2232 | * Either we are in around ~1 trillion A.C., assuming |
| 2233 | * 1 reboot per second, or we have a bug: |
| 2234 | */ |
| 2235 | MD_BUG(); |
| 2236 | mddev->events --; |
| 2237 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2238 | sync_sbs(mddev, nospares); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2239 | spin_unlock_irq(&mddev->write_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2240 | |
| 2241 | dprintk(KERN_INFO |
| 2242 | "md: updating %s RAID superblock on device (in sync %d)\n", |
| 2243 | mdname(mddev),mddev->in_sync); |
| 2244 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 2245 | bitmap_update_sb(mddev->bitmap); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2246 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2247 | char b[BDEVNAME_SIZE]; |
| 2248 | dprintk(KERN_INFO "md: "); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2249 | if (rdev->sb_loaded != 1) |
| 2250 | continue; /* no noise on spare devices */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2251 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2252 | dprintk("(skipping faulty "); |
| 2253 | |
| 2254 | dprintk("%s ", bdevname(rdev->bdev,b)); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2255 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2256 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2257 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2258 | rdev->sb_page); |
| 2259 | dprintk(KERN_INFO "(write) %s's sb offset: %llu\n", |
| 2260 | bdevname(rdev->bdev,b), |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2261 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2262 | rdev->sb_events = mddev->events; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2263 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2264 | } else |
| 2265 | dprintk(")\n"); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2266 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2267 | /* only need to write one superblock... */ |
| 2268 | break; |
| 2269 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2270 | md_super_wait(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2271 | /* 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] | 2272 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2273 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2274 | if (mddev->in_sync != sync_req || |
| 2275 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2276 | /* have to write it out again */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2277 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2278 | goto repeat; |
| 2279 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2280 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2281 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2282 | wake_up(&mddev->sb_wait); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 2283 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 2284 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2285 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2286 | } |
| 2287 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 2288 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2289 | * We want to accept with case. For this we use cmd_match. |
| 2290 | */ |
| 2291 | static int cmd_match(const char *cmd, const char *str) |
| 2292 | { |
| 2293 | /* See if cmd, written into a sysfs file, matches |
| 2294 | * str. They must either be the same, or cmd can |
| 2295 | * have a trailing newline |
| 2296 | */ |
| 2297 | while (*cmd && *str && *cmd == *str) { |
| 2298 | cmd++; |
| 2299 | str++; |
| 2300 | } |
| 2301 | if (*cmd == '\n') |
| 2302 | cmd++; |
| 2303 | if (*str || *cmd) |
| 2304 | return 0; |
| 2305 | return 1; |
| 2306 | } |
| 2307 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2308 | struct rdev_sysfs_entry { |
| 2309 | struct attribute attr; |
| 2310 | ssize_t (*show)(mdk_rdev_t *, char *); |
| 2311 | ssize_t (*store)(mdk_rdev_t *, const char *, size_t); |
| 2312 | }; |
| 2313 | |
| 2314 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2315 | state_show(mdk_rdev_t *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2316 | { |
| 2317 | char *sep = ""; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2318 | size_t len = 0; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2319 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2320 | if (test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2321 | len+= sprintf(page+len, "%sfaulty",sep); |
| 2322 | sep = ","; |
| 2323 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2324 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2325 | len += sprintf(page+len, "%sin_sync",sep); |
| 2326 | sep = ","; |
| 2327 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2328 | if (test_bit(WriteMostly, &rdev->flags)) { |
| 2329 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 2330 | sep = ","; |
| 2331 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2332 | if (test_bit(Blocked, &rdev->flags)) { |
| 2333 | len += sprintf(page+len, "%sblocked", sep); |
| 2334 | sep = ","; |
| 2335 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2336 | if (!test_bit(Faulty, &rdev->flags) && |
| 2337 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2338 | len += sprintf(page+len, "%sspare", sep); |
| 2339 | sep = ","; |
| 2340 | } |
| 2341 | return len+sprintf(page+len, "\n"); |
| 2342 | } |
| 2343 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2344 | static ssize_t |
| 2345 | state_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2346 | { |
| 2347 | /* can write |
| 2348 | * faulty - simulates and error |
| 2349 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2350 | * writemostly - sets write_mostly |
| 2351 | * -writemostly - clears write_mostly |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2352 | * blocked - sets the Blocked flag |
| 2353 | * -blocked - clears the Blocked flag |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2354 | * insync - sets Insync providing device isn't active |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2355 | */ |
| 2356 | int err = -EINVAL; |
| 2357 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 2358 | md_error(rdev->mddev, rdev); |
| 2359 | err = 0; |
| 2360 | } else if (cmd_match(buf, "remove")) { |
| 2361 | if (rdev->raid_disk >= 0) |
| 2362 | err = -EBUSY; |
| 2363 | else { |
| 2364 | mddev_t *mddev = rdev->mddev; |
| 2365 | kick_rdev_from_array(rdev); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2366 | if (mddev->pers) |
| 2367 | md_update_sb(mddev, 1); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2368 | md_new_event(mddev); |
| 2369 | err = 0; |
| 2370 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2371 | } else if (cmd_match(buf, "writemostly")) { |
| 2372 | set_bit(WriteMostly, &rdev->flags); |
| 2373 | err = 0; |
| 2374 | } else if (cmd_match(buf, "-writemostly")) { |
| 2375 | clear_bit(WriteMostly, &rdev->flags); |
| 2376 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2377 | } else if (cmd_match(buf, "blocked")) { |
| 2378 | set_bit(Blocked, &rdev->flags); |
| 2379 | err = 0; |
| 2380 | } else if (cmd_match(buf, "-blocked")) { |
| 2381 | clear_bit(Blocked, &rdev->flags); |
| 2382 | wake_up(&rdev->blocked_wait); |
| 2383 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2384 | md_wakeup_thread(rdev->mddev->thread); |
| 2385 | |
| 2386 | err = 0; |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2387 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { |
| 2388 | set_bit(In_sync, &rdev->flags); |
| 2389 | err = 0; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2390 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2391 | if (!err) |
| 2392 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2393 | return err ? err : len; |
| 2394 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2395 | static struct rdev_sysfs_entry rdev_state = |
| 2396 | __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2397 | |
| 2398 | static ssize_t |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2399 | errors_show(mdk_rdev_t *rdev, char *page) |
| 2400 | { |
| 2401 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 2402 | } |
| 2403 | |
| 2404 | static ssize_t |
| 2405 | errors_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2406 | { |
| 2407 | char *e; |
| 2408 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2409 | if (*buf && (*e == 0 || *e == '\n')) { |
| 2410 | atomic_set(&rdev->corrected_errors, n); |
| 2411 | return len; |
| 2412 | } |
| 2413 | return -EINVAL; |
| 2414 | } |
| 2415 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2416 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2417 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2418 | static ssize_t |
| 2419 | slot_show(mdk_rdev_t *rdev, char *page) |
| 2420 | { |
| 2421 | if (rdev->raid_disk < 0) |
| 2422 | return sprintf(page, "none\n"); |
| 2423 | else |
| 2424 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 2425 | } |
| 2426 | |
| 2427 | static ssize_t |
| 2428 | slot_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2429 | { |
| 2430 | char *e; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2431 | int err; |
| 2432 | char nm[20]; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2433 | int slot = simple_strtoul(buf, &e, 10); |
| 2434 | if (strncmp(buf, "none", 4)==0) |
| 2435 | slot = -1; |
| 2436 | else if (e==buf || (*e && *e!= '\n')) |
| 2437 | return -EINVAL; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2438 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2439 | /* Setting 'slot' on an active array requires also |
| 2440 | * updating the 'rd%d' link, and communicating |
| 2441 | * with the personality with ->hot_*_disk. |
| 2442 | * For now we only support removing |
| 2443 | * failed/spare devices. This normally happens automatically, |
| 2444 | * but not when the metadata is externally managed. |
| 2445 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2446 | if (rdev->raid_disk == -1) |
| 2447 | return -EEXIST; |
| 2448 | /* personality does all needed checks */ |
| 2449 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2450 | return -EINVAL; |
| 2451 | err = rdev->mddev->pers-> |
| 2452 | hot_remove_disk(rdev->mddev, rdev->raid_disk); |
| 2453 | if (err) |
| 2454 | return err; |
| 2455 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2456 | sysfs_remove_link(&rdev->mddev->kobj, nm); |
Maciej Trela | b710310 | 2010-04-14 16:58:16 +1000 | [diff] [blame] | 2457 | rdev->raid_disk = -1; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2458 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2459 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2460 | } else if (rdev->mddev->pers) { |
| 2461 | mdk_rdev_t *rdev2; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2462 | /* Activating a spare .. or possibly reactivating |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2463 | * if we ever get bitmaps working here. |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2464 | */ |
| 2465 | |
| 2466 | if (rdev->raid_disk != -1) |
| 2467 | return -EBUSY; |
| 2468 | |
| 2469 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2470 | return -EINVAL; |
| 2471 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2472 | list_for_each_entry(rdev2, &rdev->mddev->disks, same_set) |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2473 | if (rdev2->raid_disk == slot) |
| 2474 | return -EEXIST; |
| 2475 | |
| 2476 | rdev->raid_disk = slot; |
| 2477 | if (test_bit(In_sync, &rdev->flags)) |
| 2478 | rdev->saved_raid_disk = slot; |
| 2479 | else |
| 2480 | rdev->saved_raid_disk = -1; |
| 2481 | err = rdev->mddev->pers-> |
| 2482 | hot_add_disk(rdev->mddev, rdev); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 2483 | if (err) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2484 | rdev->raid_disk = -1; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2485 | return err; |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 2486 | } else |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2487 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2488 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2489 | if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2490 | /* failure here is OK */; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2491 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2492 | } else { |
| 2493 | if (slot >= rdev->mddev->raid_disks) |
| 2494 | return -ENOSPC; |
| 2495 | rdev->raid_disk = slot; |
| 2496 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2497 | clear_bit(Faulty, &rdev->flags); |
| 2498 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2499 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2500 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2501 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2502 | return len; |
| 2503 | } |
| 2504 | |
| 2505 | |
| 2506 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2507 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2508 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2509 | static ssize_t |
| 2510 | offset_show(mdk_rdev_t *rdev, char *page) |
| 2511 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2512 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2513 | } |
| 2514 | |
| 2515 | static ssize_t |
| 2516 | offset_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2517 | { |
| 2518 | char *e; |
| 2519 | unsigned long long offset = simple_strtoull(buf, &e, 10); |
| 2520 | if (e==buf || (*e && *e != '\n')) |
| 2521 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2522 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2523 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2524 | if (rdev->sectors && rdev->mddev->external) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2525 | /* Must set offset before size, so overlap checks |
| 2526 | * can be sane */ |
| 2527 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2528 | rdev->data_offset = offset; |
| 2529 | return len; |
| 2530 | } |
| 2531 | |
| 2532 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2533 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2534 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2535 | static ssize_t |
| 2536 | rdev_size_show(mdk_rdev_t *rdev, char *page) |
| 2537 | { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2538 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2539 | } |
| 2540 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2541 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 2542 | { |
| 2543 | /* check if two start/length pairs overlap */ |
| 2544 | if (s1+l1 <= s2) |
| 2545 | return 0; |
| 2546 | if (s2+l2 <= s1) |
| 2547 | return 0; |
| 2548 | return 1; |
| 2549 | } |
| 2550 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2551 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
| 2552 | { |
| 2553 | unsigned long long blocks; |
| 2554 | sector_t new; |
| 2555 | |
| 2556 | if (strict_strtoull(buf, 10, &blocks) < 0) |
| 2557 | return -EINVAL; |
| 2558 | |
| 2559 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) |
| 2560 | return -EINVAL; /* sector conversion overflow */ |
| 2561 | |
| 2562 | new = blocks * 2; |
| 2563 | if (new != blocks * 2) |
| 2564 | return -EINVAL; /* unsigned long long to sector_t overflow */ |
| 2565 | |
| 2566 | *sectors = new; |
| 2567 | return 0; |
| 2568 | } |
| 2569 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2570 | static ssize_t |
| 2571 | rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2572 | { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2573 | mddev_t *my_mddev = rdev->mddev; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2574 | sector_t oldsectors = rdev->sectors; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2575 | sector_t sectors; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2576 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2577 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2578 | return -EINVAL; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2579 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2580 | if (my_mddev->persistent) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2581 | sectors = super_types[my_mddev->major_version]. |
| 2582 | rdev_size_change(rdev, sectors); |
| 2583 | if (!sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2584 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2585 | } else if (!sectors) |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 2586 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) - |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2587 | rdev->data_offset; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2588 | } |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2589 | if (sectors < my_mddev->dev_sectors) |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 2590 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2591 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2592 | rdev->sectors = sectors; |
| 2593 | if (sectors > oldsectors && my_mddev->external) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2594 | /* need to check that all other rdevs with the same ->bdev |
| 2595 | * do not overlap. We need to unlock the mddev to avoid |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2596 | * a deadlock. We have already changed rdev->sectors, and if |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2597 | * we have to change it back, we will have the lock again. |
| 2598 | */ |
| 2599 | mddev_t *mddev; |
| 2600 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2601 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2602 | |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2603 | mddev_unlock(my_mddev); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2604 | for_each_mddev(mddev, tmp) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2605 | mdk_rdev_t *rdev2; |
| 2606 | |
| 2607 | mddev_lock(mddev); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2608 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2609 | if (test_bit(AllReserved, &rdev2->flags) || |
| 2610 | (rdev->bdev == rdev2->bdev && |
| 2611 | rdev != rdev2 && |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2612 | overlaps(rdev->data_offset, rdev->sectors, |
Andre Noll | d07bd3b | 2008-07-21 14:42:07 +1000 | [diff] [blame] | 2613 | rdev2->data_offset, |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2614 | rdev2->sectors))) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2615 | overlap = 1; |
| 2616 | break; |
| 2617 | } |
| 2618 | mddev_unlock(mddev); |
| 2619 | if (overlap) { |
| 2620 | mddev_put(mddev); |
| 2621 | break; |
| 2622 | } |
| 2623 | } |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2624 | mddev_lock(my_mddev); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2625 | if (overlap) { |
| 2626 | /* Someone else could have slipped in a size |
| 2627 | * change here, but doing so is just silly. |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2628 | * We put oldsectors back because we *know* it is |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2629 | * safe, and trust userspace not to race with |
| 2630 | * itself |
| 2631 | */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2632 | rdev->sectors = oldsectors; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2633 | return -EBUSY; |
| 2634 | } |
| 2635 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2636 | return len; |
| 2637 | } |
| 2638 | |
| 2639 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2640 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2641 | |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2642 | |
| 2643 | static ssize_t recovery_start_show(mdk_rdev_t *rdev, char *page) |
| 2644 | { |
| 2645 | unsigned long long recovery_start = rdev->recovery_offset; |
| 2646 | |
| 2647 | if (test_bit(In_sync, &rdev->flags) || |
| 2648 | recovery_start == MaxSector) |
| 2649 | return sprintf(page, "none\n"); |
| 2650 | |
| 2651 | return sprintf(page, "%llu\n", recovery_start); |
| 2652 | } |
| 2653 | |
| 2654 | static ssize_t recovery_start_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2655 | { |
| 2656 | unsigned long long recovery_start; |
| 2657 | |
| 2658 | if (cmd_match(buf, "none")) |
| 2659 | recovery_start = MaxSector; |
| 2660 | else if (strict_strtoull(buf, 10, &recovery_start)) |
| 2661 | return -EINVAL; |
| 2662 | |
| 2663 | if (rdev->mddev->pers && |
| 2664 | rdev->raid_disk >= 0) |
| 2665 | return -EBUSY; |
| 2666 | |
| 2667 | rdev->recovery_offset = recovery_start; |
| 2668 | if (recovery_start == MaxSector) |
| 2669 | set_bit(In_sync, &rdev->flags); |
| 2670 | else |
| 2671 | clear_bit(In_sync, &rdev->flags); |
| 2672 | return len; |
| 2673 | } |
| 2674 | |
| 2675 | static struct rdev_sysfs_entry rdev_recovery_start = |
| 2676 | __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store); |
| 2677 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2678 | static struct attribute *rdev_default_attrs[] = { |
| 2679 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2680 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2681 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2682 | &rdev_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2683 | &rdev_size.attr, |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2684 | &rdev_recovery_start.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2685 | NULL, |
| 2686 | }; |
| 2687 | static ssize_t |
| 2688 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 2689 | { |
| 2690 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2691 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2692 | mddev_t *mddev = rdev->mddev; |
| 2693 | ssize_t rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2694 | |
| 2695 | if (!entry->show) |
| 2696 | return -EIO; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2697 | |
| 2698 | rv = mddev ? mddev_lock(mddev) : -EBUSY; |
| 2699 | if (!rv) { |
| 2700 | if (rdev->mddev == NULL) |
| 2701 | rv = -EBUSY; |
| 2702 | else |
| 2703 | rv = entry->show(rdev, page); |
| 2704 | mddev_unlock(mddev); |
| 2705 | } |
| 2706 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2707 | } |
| 2708 | |
| 2709 | static ssize_t |
| 2710 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 2711 | const char *page, size_t length) |
| 2712 | { |
| 2713 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2714 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2715 | ssize_t rv; |
| 2716 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2717 | |
| 2718 | if (!entry->store) |
| 2719 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 2720 | if (!capable(CAP_SYS_ADMIN)) |
| 2721 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2722 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2723 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2724 | if (rdev->mddev == NULL) |
| 2725 | rv = -EBUSY; |
| 2726 | else |
| 2727 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 2728 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2729 | } |
| 2730 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2731 | } |
| 2732 | |
| 2733 | static void rdev_free(struct kobject *ko) |
| 2734 | { |
| 2735 | mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj); |
| 2736 | kfree(rdev); |
| 2737 | } |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 2738 | static const struct sysfs_ops rdev_sysfs_ops = { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2739 | .show = rdev_attr_show, |
| 2740 | .store = rdev_attr_store, |
| 2741 | }; |
| 2742 | static struct kobj_type rdev_ktype = { |
| 2743 | .release = rdev_free, |
| 2744 | .sysfs_ops = &rdev_sysfs_ops, |
| 2745 | .default_attrs = rdev_default_attrs, |
| 2746 | }; |
| 2747 | |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 2748 | void md_rdev_init(mdk_rdev_t *rdev) |
| 2749 | { |
| 2750 | rdev->desc_nr = -1; |
| 2751 | rdev->saved_raid_disk = -1; |
| 2752 | rdev->raid_disk = -1; |
| 2753 | rdev->flags = 0; |
| 2754 | rdev->data_offset = 0; |
| 2755 | rdev->sb_events = 0; |
| 2756 | rdev->last_read_error.tv_sec = 0; |
| 2757 | rdev->last_read_error.tv_nsec = 0; |
| 2758 | atomic_set(&rdev->nr_pending, 0); |
| 2759 | atomic_set(&rdev->read_errors, 0); |
| 2760 | atomic_set(&rdev->corrected_errors, 0); |
| 2761 | |
| 2762 | INIT_LIST_HEAD(&rdev->same_set); |
| 2763 | init_waitqueue_head(&rdev->blocked_wait); |
| 2764 | } |
| 2765 | EXPORT_SYMBOL_GPL(md_rdev_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2766 | /* |
| 2767 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 2768 | * |
| 2769 | * mark the device faulty if: |
| 2770 | * |
| 2771 | * - the device is nonexistent (zero size) |
| 2772 | * - the device has no valid superblock |
| 2773 | * |
| 2774 | * a faulty rdev _never_ has rdev->sb set. |
| 2775 | */ |
| 2776 | static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor) |
| 2777 | { |
| 2778 | char b[BDEVNAME_SIZE]; |
| 2779 | int err; |
| 2780 | mdk_rdev_t *rdev; |
| 2781 | sector_t size; |
| 2782 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 2783 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2784 | if (!rdev) { |
| 2785 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 2786 | return ERR_PTR(-ENOMEM); |
| 2787 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2788 | |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 2789 | md_rdev_init(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2790 | if ((err = alloc_disk_sb(rdev))) |
| 2791 | goto abort_free; |
| 2792 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2793 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2794 | if (err) |
| 2795 | goto abort_free; |
| 2796 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2797 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2798 | |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 2799 | size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2800 | if (!size) { |
| 2801 | printk(KERN_WARNING |
| 2802 | "md: %s has zero or unknown size, marking faulty!\n", |
| 2803 | bdevname(rdev->bdev,b)); |
| 2804 | err = -EINVAL; |
| 2805 | goto abort_free; |
| 2806 | } |
| 2807 | |
| 2808 | if (super_format >= 0) { |
| 2809 | err = super_types[super_format]. |
| 2810 | load_super(rdev, NULL, super_minor); |
| 2811 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 2812 | printk(KERN_WARNING |
| 2813 | "md: %s does not have a valid v%d.%d " |
| 2814 | "superblock, not importing!\n", |
| 2815 | bdevname(rdev->bdev,b), |
| 2816 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2817 | goto abort_free; |
| 2818 | } |
| 2819 | if (err < 0) { |
| 2820 | printk(KERN_WARNING |
| 2821 | "md: could not read %s's sb, not importing!\n", |
| 2822 | bdevname(rdev->bdev,b)); |
| 2823 | goto abort_free; |
| 2824 | } |
| 2825 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2826 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2827 | return rdev; |
| 2828 | |
| 2829 | abort_free: |
| 2830 | if (rdev->sb_page) { |
| 2831 | if (rdev->bdev) |
| 2832 | unlock_rdev(rdev); |
| 2833 | free_disk_sb(rdev); |
| 2834 | } |
| 2835 | kfree(rdev); |
| 2836 | return ERR_PTR(err); |
| 2837 | } |
| 2838 | |
| 2839 | /* |
| 2840 | * Check a full RAID array for plausibility |
| 2841 | */ |
| 2842 | |
| 2843 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 2844 | static void analyze_sbs(mddev_t * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2845 | { |
| 2846 | int i; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2847 | mdk_rdev_t *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2848 | char b[BDEVNAME_SIZE]; |
| 2849 | |
| 2850 | freshest = NULL; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2851 | rdev_for_each(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2852 | switch (super_types[mddev->major_version]. |
| 2853 | load_super(rdev, freshest, mddev->minor_version)) { |
| 2854 | case 1: |
| 2855 | freshest = rdev; |
| 2856 | break; |
| 2857 | case 0: |
| 2858 | break; |
| 2859 | default: |
| 2860 | printk( KERN_ERR \ |
| 2861 | "md: fatal superblock inconsistency in %s" |
| 2862 | " -- removing from array\n", |
| 2863 | bdevname(rdev->bdev,b)); |
| 2864 | kick_rdev_from_array(rdev); |
| 2865 | } |
| 2866 | |
| 2867 | |
| 2868 | super_types[mddev->major_version]. |
| 2869 | validate_super(mddev, freshest); |
| 2870 | |
| 2871 | i = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2872 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 2873 | if (mddev->max_disks && |
| 2874 | (rdev->desc_nr >= mddev->max_disks || |
| 2875 | i > mddev->max_disks)) { |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 2876 | printk(KERN_WARNING |
| 2877 | "md: %s: %s: only %d devices permitted\n", |
| 2878 | mdname(mddev), bdevname(rdev->bdev, b), |
| 2879 | mddev->max_disks); |
| 2880 | kick_rdev_from_array(rdev); |
| 2881 | continue; |
| 2882 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2883 | if (rdev != freshest) |
| 2884 | if (super_types[mddev->major_version]. |
| 2885 | validate_super(mddev, rdev)) { |
| 2886 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 2887 | " from array!\n", |
| 2888 | bdevname(rdev->bdev,b)); |
| 2889 | kick_rdev_from_array(rdev); |
| 2890 | continue; |
| 2891 | } |
| 2892 | if (mddev->level == LEVEL_MULTIPATH) { |
| 2893 | rdev->desc_nr = i++; |
| 2894 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2895 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 2896 | } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2897 | rdev->raid_disk = -1; |
| 2898 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2899 | } |
| 2900 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2901 | } |
| 2902 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2903 | /* Read a fixed-point number. |
| 2904 | * Numbers in sysfs attributes should be in "standard" units where |
| 2905 | * possible, so time should be in seconds. |
| 2906 | * However we internally use a a much smaller unit such as |
| 2907 | * milliseconds or jiffies. |
| 2908 | * This function takes a decimal number with a possible fractional |
| 2909 | * component, and produces an integer which is the result of |
| 2910 | * multiplying that number by 10^'scale'. |
| 2911 | * all without any floating-point arithmetic. |
| 2912 | */ |
| 2913 | int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale) |
| 2914 | { |
| 2915 | unsigned long result = 0; |
| 2916 | long decimals = -1; |
| 2917 | while (isdigit(*cp) || (*cp == '.' && decimals < 0)) { |
| 2918 | if (*cp == '.') |
| 2919 | decimals = 0; |
| 2920 | else if (decimals < scale) { |
| 2921 | unsigned int value; |
| 2922 | value = *cp - '0'; |
| 2923 | result = result * 10 + value; |
| 2924 | if (decimals >= 0) |
| 2925 | decimals++; |
| 2926 | } |
| 2927 | cp++; |
| 2928 | } |
| 2929 | if (*cp == '\n') |
| 2930 | cp++; |
| 2931 | if (*cp) |
| 2932 | return -EINVAL; |
| 2933 | if (decimals < 0) |
| 2934 | decimals = 0; |
| 2935 | while (decimals < scale) { |
| 2936 | result *= 10; |
| 2937 | decimals ++; |
| 2938 | } |
| 2939 | *res = result; |
| 2940 | return 0; |
| 2941 | } |
| 2942 | |
| 2943 | |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2944 | static void md_safemode_timeout(unsigned long data); |
| 2945 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2946 | static ssize_t |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2947 | safe_delay_show(mddev_t *mddev, char *page) |
| 2948 | { |
| 2949 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 2950 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 2951 | } |
| 2952 | static ssize_t |
| 2953 | safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len) |
| 2954 | { |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2955 | unsigned long msec; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 2956 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2957 | if (strict_strtoul_scaled(cbuf, &msec, 3) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2958 | return -EINVAL; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2959 | if (msec == 0) |
| 2960 | mddev->safemode_delay = 0; |
| 2961 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2962 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2963 | mddev->safemode_delay = (msec*HZ)/1000; |
| 2964 | if (mddev->safemode_delay == 0) |
| 2965 | mddev->safemode_delay = 1; |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2966 | if (mddev->safemode_delay < old_delay) |
| 2967 | md_safemode_timeout((unsigned long)mddev); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2968 | } |
| 2969 | return len; |
| 2970 | } |
| 2971 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2972 | __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] | 2973 | |
| 2974 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2975 | level_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2976 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 2977 | struct mdk_personality *p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2978 | if (p) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2979 | return sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2980 | else if (mddev->clevel[0]) |
| 2981 | return sprintf(page, "%s\n", mddev->clevel); |
| 2982 | else if (mddev->level != LEVEL_NONE) |
| 2983 | return sprintf(page, "%d\n", mddev->level); |
| 2984 | else |
| 2985 | return 0; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2986 | } |
| 2987 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2988 | static ssize_t |
| 2989 | level_store(mddev_t *mddev, const char *buf, size_t len) |
| 2990 | { |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 2991 | char clevel[16]; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2992 | ssize_t rv = len; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2993 | struct mdk_personality *pers; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 2994 | long level; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2995 | void *priv; |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 2996 | mdk_rdev_t *rdev; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2997 | |
| 2998 | if (mddev->pers == NULL) { |
| 2999 | if (len == 0) |
| 3000 | return 0; |
| 3001 | if (len >= sizeof(mddev->clevel)) |
| 3002 | return -ENOSPC; |
| 3003 | strncpy(mddev->clevel, buf, len); |
| 3004 | if (mddev->clevel[len-1] == '\n') |
| 3005 | len--; |
| 3006 | mddev->clevel[len] = 0; |
| 3007 | mddev->level = LEVEL_NONE; |
| 3008 | return rv; |
| 3009 | } |
| 3010 | |
| 3011 | /* request to change the personality. Need to ensure: |
| 3012 | * - array is not engaged in resync/recovery/reshape |
| 3013 | * - old personality can be suspended |
| 3014 | * - new personality will access other array. |
| 3015 | */ |
| 3016 | |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 3017 | if (mddev->sync_thread || |
| 3018 | mddev->reshape_position != MaxSector || |
| 3019 | mddev->sysfs_active) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3020 | return -EBUSY; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3021 | |
| 3022 | if (!mddev->pers->quiesce) { |
| 3023 | printk(KERN_WARNING "md: %s: %s does not support online personality change\n", |
| 3024 | mdname(mddev), mddev->pers->name); |
| 3025 | return -EINVAL; |
| 3026 | } |
| 3027 | |
| 3028 | /* Now find the new personality */ |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3029 | if (len == 0 || len >= sizeof(clevel)) |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3030 | return -EINVAL; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3031 | strncpy(clevel, buf, len); |
| 3032 | if (clevel[len-1] == '\n') |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3033 | len--; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3034 | clevel[len] = 0; |
| 3035 | if (strict_strtol(clevel, 10, &level)) |
| 3036 | level = LEVEL_NONE; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3037 | |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3038 | if (request_module("md-%s", clevel) != 0) |
| 3039 | request_module("md-level-%s", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3040 | spin_lock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3041 | pers = find_pers(level, clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3042 | if (!pers || !try_module_get(pers->owner)) { |
| 3043 | spin_unlock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3044 | printk(KERN_WARNING "md: personality %s not loaded\n", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3045 | return -EINVAL; |
| 3046 | } |
| 3047 | spin_unlock(&pers_lock); |
| 3048 | |
| 3049 | if (pers == mddev->pers) { |
| 3050 | /* Nothing to do! */ |
| 3051 | module_put(pers->owner); |
| 3052 | return rv; |
| 3053 | } |
| 3054 | if (!pers->takeover) { |
| 3055 | module_put(pers->owner); |
| 3056 | printk(KERN_WARNING "md: %s: %s does not support personality takeover\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3057 | mdname(mddev), clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3058 | return -EINVAL; |
| 3059 | } |
| 3060 | |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3061 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 3062 | rdev->new_raid_disk = rdev->raid_disk; |
| 3063 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3064 | /* ->takeover must set new_* and/or delta_disks |
| 3065 | * if it succeeds, and may set them when it fails. |
| 3066 | */ |
| 3067 | priv = pers->takeover(mddev); |
| 3068 | if (IS_ERR(priv)) { |
| 3069 | mddev->new_level = mddev->level; |
| 3070 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3071 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3072 | mddev->raid_disks -= mddev->delta_disks; |
| 3073 | mddev->delta_disks = 0; |
| 3074 | module_put(pers->owner); |
| 3075 | printk(KERN_WARNING "md: %s: %s would not accept array\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3076 | mdname(mddev), clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3077 | return PTR_ERR(priv); |
| 3078 | } |
| 3079 | |
| 3080 | /* Looks like we have a winner */ |
| 3081 | mddev_suspend(mddev); |
| 3082 | mddev->pers->stop(mddev); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 3083 | |
| 3084 | if (mddev->pers->sync_request == NULL && |
| 3085 | pers->sync_request != NULL) { |
| 3086 | /* need to add the md_redundancy_group */ |
| 3087 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 3088 | printk(KERN_WARNING |
| 3089 | "md: cannot register extra attributes for %s\n", |
| 3090 | mdname(mddev)); |
NeilBrown | 19fdb9e | 2010-05-22 08:31:36 +1000 | [diff] [blame] | 3091 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action"); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 3092 | } |
| 3093 | if (mddev->pers->sync_request != NULL && |
| 3094 | pers->sync_request == NULL) { |
| 3095 | /* need to remove the md_redundancy_group */ |
| 3096 | if (mddev->to_remove == NULL) |
| 3097 | mddev->to_remove = &md_redundancy_group; |
| 3098 | } |
| 3099 | |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 3100 | if (mddev->pers->sync_request == NULL && |
| 3101 | mddev->external) { |
| 3102 | /* We are converting from a no-redundancy array |
| 3103 | * to a redundancy array and metadata is managed |
| 3104 | * externally so we need to be sure that writes |
| 3105 | * won't block due to a need to transition |
| 3106 | * clean->dirty |
| 3107 | * until external management is started. |
| 3108 | */ |
| 3109 | mddev->in_sync = 0; |
| 3110 | mddev->safemode_delay = 0; |
| 3111 | mddev->safemode = 0; |
| 3112 | } |
| 3113 | |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3114 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 3115 | char nm[20]; |
| 3116 | if (rdev->raid_disk < 0) |
| 3117 | continue; |
| 3118 | if (rdev->new_raid_disk > mddev->raid_disks) |
| 3119 | rdev->new_raid_disk = -1; |
| 3120 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3121 | continue; |
| 3122 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 3123 | sysfs_remove_link(&mddev->kobj, nm); |
| 3124 | } |
| 3125 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 3126 | if (rdev->raid_disk < 0) |
| 3127 | continue; |
| 3128 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3129 | continue; |
| 3130 | rdev->raid_disk = rdev->new_raid_disk; |
| 3131 | if (rdev->raid_disk < 0) |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3132 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3133 | else { |
| 3134 | char nm[20]; |
| 3135 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 3136 | if(sysfs_create_link(&mddev->kobj, &rdev->kobj, nm)) |
| 3137 | printk("md: cannot register %s for %s after level change\n", |
| 3138 | nm, mdname(mddev)); |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3139 | } |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3140 | } |
| 3141 | |
| 3142 | module_put(mddev->pers->owner); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3143 | mddev->pers = pers; |
| 3144 | mddev->private = priv; |
| 3145 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
| 3146 | mddev->level = mddev->new_level; |
| 3147 | mddev->layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3148 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3149 | mddev->delta_disks = 0; |
Trela, Maciej | 9af204c | 2010-03-08 16:02:44 +1100 | [diff] [blame] | 3150 | if (mddev->pers->sync_request == NULL) { |
| 3151 | /* this is now an array without redundancy, so |
| 3152 | * it must always be in_sync |
| 3153 | */ |
| 3154 | mddev->in_sync = 1; |
| 3155 | del_timer_sync(&mddev->safemode_timer); |
| 3156 | } |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3157 | pers->run(mddev); |
| 3158 | mddev_resume(mddev); |
| 3159 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 3160 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3161 | md_wakeup_thread(mddev->thread); |
Maciej Trela | 5cac7861 | 2010-04-14 17:17:39 +1000 | [diff] [blame] | 3162 | sysfs_notify(&mddev->kobj, NULL, "level"); |
Dan Williams | bb7f8d2 | 2010-05-01 18:14:57 -0700 | [diff] [blame] | 3163 | md_new_event(mddev); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3164 | return rv; |
| 3165 | } |
| 3166 | |
| 3167 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3168 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3169 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3170 | |
| 3171 | static ssize_t |
| 3172 | layout_show(mddev_t *mddev, char *page) |
| 3173 | { |
| 3174 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3175 | if (mddev->reshape_position != MaxSector && |
| 3176 | mddev->layout != mddev->new_layout) |
| 3177 | return sprintf(page, "%d (%d)\n", |
| 3178 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3179 | return sprintf(page, "%d\n", mddev->layout); |
| 3180 | } |
| 3181 | |
| 3182 | static ssize_t |
| 3183 | layout_store(mddev_t *mddev, const char *buf, size_t len) |
| 3184 | { |
| 3185 | char *e; |
| 3186 | unsigned long n = simple_strtoul(buf, &e, 10); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3187 | |
| 3188 | if (!*buf || (*e && *e != '\n')) |
| 3189 | return -EINVAL; |
| 3190 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3191 | if (mddev->pers) { |
| 3192 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3193 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3194 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3195 | mddev->new_layout = n; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3196 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3197 | if (err) { |
| 3198 | mddev->new_layout = mddev->layout; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3199 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3200 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3201 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3202 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3203 | if (mddev->reshape_position == MaxSector) |
| 3204 | mddev->layout = n; |
| 3205 | } |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3206 | return len; |
| 3207 | } |
| 3208 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3209 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3210 | |
| 3211 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3212 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3213 | raid_disks_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3214 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 3215 | if (mddev->raid_disks == 0) |
| 3216 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3217 | if (mddev->reshape_position != MaxSector && |
| 3218 | mddev->delta_disks != 0) |
| 3219 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 3220 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3221 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 3222 | } |
| 3223 | |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3224 | static int update_raid_disks(mddev_t *mddev, int raid_disks); |
| 3225 | |
| 3226 | static ssize_t |
| 3227 | raid_disks_store(mddev_t *mddev, const char *buf, size_t len) |
| 3228 | { |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3229 | char *e; |
| 3230 | int rv = 0; |
| 3231 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3232 | |
| 3233 | if (!*buf || (*e && *e != '\n')) |
| 3234 | return -EINVAL; |
| 3235 | |
| 3236 | if (mddev->pers) |
| 3237 | rv = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3238 | else if (mddev->reshape_position != MaxSector) { |
| 3239 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
| 3240 | mddev->delta_disks = n - olddisks; |
| 3241 | mddev->raid_disks = n; |
| 3242 | } else |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3243 | mddev->raid_disks = n; |
| 3244 | return rv ? rv : len; |
| 3245 | } |
| 3246 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3247 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3248 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3249 | static ssize_t |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3250 | chunk_size_show(mddev_t *mddev, char *page) |
| 3251 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3252 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3253 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 3254 | return sprintf(page, "%d (%d)\n", |
| 3255 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3256 | mddev->chunk_sectors << 9); |
| 3257 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3258 | } |
| 3259 | |
| 3260 | static ssize_t |
| 3261 | chunk_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3262 | { |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3263 | char *e; |
| 3264 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3265 | |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3266 | if (!*buf || (*e && *e != '\n')) |
| 3267 | return -EINVAL; |
| 3268 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3269 | if (mddev->pers) { |
| 3270 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3271 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3272 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3273 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3274 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3275 | if (err) { |
| 3276 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3277 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3278 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3279 | } else { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3280 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3281 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3282 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3283 | } |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3284 | return len; |
| 3285 | } |
| 3286 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3287 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3288 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3289 | static ssize_t |
| 3290 | resync_start_show(mddev_t *mddev, char *page) |
| 3291 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3292 | if (mddev->recovery_cp == MaxSector) |
| 3293 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3294 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 3295 | } |
| 3296 | |
| 3297 | static ssize_t |
| 3298 | resync_start_store(mddev_t *mddev, const char *buf, size_t len) |
| 3299 | { |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3300 | char *e; |
| 3301 | unsigned long long n = simple_strtoull(buf, &e, 10); |
| 3302 | |
| 3303 | if (mddev->pers) |
| 3304 | return -EBUSY; |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3305 | if (cmd_match(buf, "none")) |
| 3306 | n = MaxSector; |
| 3307 | else if (!*buf || (*e && *e != '\n')) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3308 | return -EINVAL; |
| 3309 | |
| 3310 | mddev->recovery_cp = n; |
| 3311 | return len; |
| 3312 | } |
| 3313 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3314 | __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3315 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3316 | /* |
| 3317 | * The array state can be: |
| 3318 | * |
| 3319 | * clear |
| 3320 | * No devices, no size, no level |
| 3321 | * Equivalent to STOP_ARRAY ioctl |
| 3322 | * inactive |
| 3323 | * May have some settings, but array is not active |
| 3324 | * all IO results in error |
| 3325 | * When written, doesn't tear down array, but just stops it |
| 3326 | * suspended (not supported yet) |
| 3327 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 3328 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3329 | * readonly |
| 3330 | * no resync can happen. no superblocks get written. |
| 3331 | * write requests fail |
| 3332 | * read-auto |
| 3333 | * like readonly, but behaves like 'clean' on a write request. |
| 3334 | * |
| 3335 | * clean - no pending writes, but otherwise active. |
| 3336 | * When written to inactive array, starts without resync |
| 3337 | * If a write request arrives then |
| 3338 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 3339 | * if not known, block and switch to write-pending |
| 3340 | * If written to an active array that has pending writes, then fails. |
| 3341 | * active |
| 3342 | * fully active: IO and resync can be happening. |
| 3343 | * When written to inactive array, starts with resync |
| 3344 | * |
| 3345 | * write-pending |
| 3346 | * clean, but writes are blocked waiting for 'active' to be written. |
| 3347 | * |
| 3348 | * active-idle |
| 3349 | * like active, but no writes have been seen for a while (100msec). |
| 3350 | * |
| 3351 | */ |
| 3352 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 3353 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3354 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3355 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 3356 | "write-pending", "active-idle", NULL }; |
| 3357 | |
| 3358 | static int match_word(const char *word, char **list) |
| 3359 | { |
| 3360 | int n; |
| 3361 | for (n=0; list[n]; n++) |
| 3362 | if (cmd_match(word, list[n])) |
| 3363 | break; |
| 3364 | return n; |
| 3365 | } |
| 3366 | |
| 3367 | static ssize_t |
| 3368 | array_state_show(mddev_t *mddev, char *page) |
| 3369 | { |
| 3370 | enum array_state st = inactive; |
| 3371 | |
| 3372 | if (mddev->pers) |
| 3373 | switch(mddev->ro) { |
| 3374 | case 1: |
| 3375 | st = readonly; |
| 3376 | break; |
| 3377 | case 2: |
| 3378 | st = read_auto; |
| 3379 | break; |
| 3380 | case 0: |
| 3381 | if (mddev->in_sync) |
| 3382 | st = clean; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3383 | else if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3384 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3385 | else if (mddev->safemode) |
| 3386 | st = active_idle; |
| 3387 | else |
| 3388 | st = active; |
| 3389 | } |
| 3390 | else { |
| 3391 | if (list_empty(&mddev->disks) && |
| 3392 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3393 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3394 | st = clear; |
| 3395 | else |
| 3396 | st = inactive; |
| 3397 | } |
| 3398 | return sprintf(page, "%s\n", array_states[st]); |
| 3399 | } |
| 3400 | |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3401 | static int do_md_stop(mddev_t * mddev, int ro, int is_open); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3402 | static int md_set_readonly(mddev_t * mddev, int is_open); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3403 | static int do_md_run(mddev_t * mddev); |
| 3404 | static int restart_array(mddev_t *mddev); |
| 3405 | |
| 3406 | static ssize_t |
| 3407 | array_state_store(mddev_t *mddev, const char *buf, size_t len) |
| 3408 | { |
| 3409 | int err = -EINVAL; |
| 3410 | enum array_state st = match_word(buf, array_states); |
| 3411 | switch(st) { |
| 3412 | case bad_word: |
| 3413 | break; |
| 3414 | case clear: |
| 3415 | /* stopping an active array */ |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3416 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3417 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3418 | err = do_md_stop(mddev, 0, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3419 | break; |
| 3420 | case inactive: |
| 3421 | /* stopping an active array */ |
| 3422 | if (mddev->pers) { |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3423 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3424 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3425 | err = do_md_stop(mddev, 2, 0); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3426 | } else |
| 3427 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3428 | break; |
| 3429 | case suspended: |
| 3430 | break; /* not supported yet */ |
| 3431 | case readonly: |
| 3432 | if (mddev->pers) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3433 | err = md_set_readonly(mddev, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3434 | else { |
| 3435 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3436 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3437 | err = do_md_run(mddev); |
| 3438 | } |
| 3439 | break; |
| 3440 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3441 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3442 | if (mddev->ro == 0) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3443 | err = md_set_readonly(mddev, 0); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3444 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3445 | err = restart_array(mddev); |
| 3446 | if (err == 0) { |
| 3447 | mddev->ro = 2; |
| 3448 | set_disk_ro(mddev->gendisk, 0); |
| 3449 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3450 | } else { |
| 3451 | mddev->ro = 2; |
| 3452 | err = do_md_run(mddev); |
| 3453 | } |
| 3454 | break; |
| 3455 | case clean: |
| 3456 | if (mddev->pers) { |
| 3457 | restart_array(mddev); |
| 3458 | spin_lock_irq(&mddev->write_lock); |
| 3459 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3460 | if (mddev->in_sync == 0) { |
| 3461 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3462 | if (mddev->safemode == 1) |
| 3463 | mddev->safemode = 0; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3464 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3465 | } |
| 3466 | err = 0; |
| 3467 | } else |
| 3468 | err = -EBUSY; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3469 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 3470 | } else |
| 3471 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3472 | break; |
| 3473 | case active: |
| 3474 | if (mddev->pers) { |
| 3475 | restart_array(mddev); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3476 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3477 | wake_up(&mddev->sb_wait); |
| 3478 | err = 0; |
| 3479 | } else { |
| 3480 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3481 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3482 | err = do_md_run(mddev); |
| 3483 | } |
| 3484 | break; |
| 3485 | case write_pending: |
| 3486 | case active_idle: |
| 3487 | /* these cannot be set */ |
| 3488 | break; |
| 3489 | } |
| 3490 | if (err) |
| 3491 | return err; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3492 | else { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 3493 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3494 | return len; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3495 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3496 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3497 | static struct md_sysfs_entry md_array_state = |
| 3498 | __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3499 | |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3500 | static ssize_t |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 3501 | max_corrected_read_errors_show(mddev_t *mddev, char *page) { |
| 3502 | return sprintf(page, "%d\n", |
| 3503 | atomic_read(&mddev->max_corr_read_errors)); |
| 3504 | } |
| 3505 | |
| 3506 | static ssize_t |
| 3507 | max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len) |
| 3508 | { |
| 3509 | char *e; |
| 3510 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3511 | |
| 3512 | if (*buf && (*e == 0 || *e == '\n')) { |
| 3513 | atomic_set(&mddev->max_corr_read_errors, n); |
| 3514 | return len; |
| 3515 | } |
| 3516 | return -EINVAL; |
| 3517 | } |
| 3518 | |
| 3519 | static struct md_sysfs_entry max_corr_read_errors = |
| 3520 | __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show, |
| 3521 | max_corrected_read_errors_store); |
| 3522 | |
| 3523 | static ssize_t |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3524 | null_show(mddev_t *mddev, char *page) |
| 3525 | { |
| 3526 | return -EINVAL; |
| 3527 | } |
| 3528 | |
| 3529 | static ssize_t |
| 3530 | new_dev_store(mddev_t *mddev, const char *buf, size_t len) |
| 3531 | { |
| 3532 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 3533 | /* The new device is added to the array. |
| 3534 | * If the array has a persistent superblock, we read the |
| 3535 | * superblock to initialise info and check validity. |
| 3536 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 3537 | * which mainly checks size. |
| 3538 | */ |
| 3539 | char *e; |
| 3540 | int major = simple_strtoul(buf, &e, 10); |
| 3541 | int minor; |
| 3542 | dev_t dev; |
| 3543 | mdk_rdev_t *rdev; |
| 3544 | int err; |
| 3545 | |
| 3546 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 3547 | return -EINVAL; |
| 3548 | minor = simple_strtoul(e+1, &e, 10); |
| 3549 | if (*e && *e != '\n') |
| 3550 | return -EINVAL; |
| 3551 | dev = MKDEV(major, minor); |
| 3552 | if (major != MAJOR(dev) || |
| 3553 | minor != MINOR(dev)) |
| 3554 | return -EOVERFLOW; |
| 3555 | |
| 3556 | |
| 3557 | if (mddev->persistent) { |
| 3558 | rdev = md_import_device(dev, mddev->major_version, |
| 3559 | mddev->minor_version); |
| 3560 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
| 3561 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 3562 | mdk_rdev_t, same_set); |
| 3563 | err = super_types[mddev->major_version] |
| 3564 | .load_super(rdev, rdev0, mddev->minor_version); |
| 3565 | if (err < 0) |
| 3566 | goto out; |
| 3567 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3568 | } else if (mddev->external) |
| 3569 | rdev = md_import_device(dev, -2, -1); |
| 3570 | else |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3571 | rdev = md_import_device(dev, -1, -1); |
| 3572 | |
| 3573 | if (IS_ERR(rdev)) |
| 3574 | return PTR_ERR(rdev); |
| 3575 | err = bind_rdev_to_array(rdev, mddev); |
| 3576 | out: |
| 3577 | if (err) |
| 3578 | export_rdev(rdev); |
| 3579 | return err ? err : len; |
| 3580 | } |
| 3581 | |
| 3582 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3583 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3584 | |
| 3585 | static ssize_t |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3586 | bitmap_store(mddev_t *mddev, const char *buf, size_t len) |
| 3587 | { |
| 3588 | char *end; |
| 3589 | unsigned long chunk, end_chunk; |
| 3590 | |
| 3591 | if (!mddev->bitmap) |
| 3592 | goto out; |
| 3593 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 3594 | while (*buf) { |
| 3595 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 3596 | if (buf == end) break; |
| 3597 | if (*end == '-') { /* range */ |
| 3598 | buf = end + 1; |
| 3599 | end_chunk = simple_strtoul(buf, &end, 0); |
| 3600 | if (buf == end) break; |
| 3601 | } |
| 3602 | if (*end && !isspace(*end)) break; |
| 3603 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 3604 | buf = skip_spaces(end); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3605 | } |
| 3606 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 3607 | out: |
| 3608 | return len; |
| 3609 | } |
| 3610 | |
| 3611 | static struct md_sysfs_entry md_bitmap = |
| 3612 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 3613 | |
| 3614 | static ssize_t |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3615 | size_show(mddev_t *mddev, char *page) |
| 3616 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3617 | return sprintf(page, "%llu\n", |
| 3618 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3619 | } |
| 3620 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3621 | static int update_size(mddev_t *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3622 | |
| 3623 | static ssize_t |
| 3624 | size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3625 | { |
| 3626 | /* If array is inactive, we can reduce the component size, but |
| 3627 | * not increase it (except from 0). |
| 3628 | * If array is active, we can try an on-line resize |
| 3629 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3630 | sector_t sectors; |
| 3631 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3632 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3633 | if (err < 0) |
| 3634 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3635 | if (mddev->pers) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3636 | err = update_size(mddev, sectors); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3637 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3638 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3639 | if (mddev->dev_sectors == 0 || |
| 3640 | mddev->dev_sectors > sectors) |
| 3641 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3642 | else |
| 3643 | err = -ENOSPC; |
| 3644 | } |
| 3645 | return err ? err : len; |
| 3646 | } |
| 3647 | |
| 3648 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3649 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3650 | |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3651 | |
| 3652 | /* Metdata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3653 | * This is one of |
| 3654 | * 'none' for arrays with no metadata (good luck...) |
| 3655 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3656 | * or N.M for internally known formats |
| 3657 | */ |
| 3658 | static ssize_t |
| 3659 | metadata_show(mddev_t *mddev, char *page) |
| 3660 | { |
| 3661 | if (mddev->persistent) |
| 3662 | return sprintf(page, "%d.%d\n", |
| 3663 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3664 | else if (mddev->external) |
| 3665 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3666 | else |
| 3667 | return sprintf(page, "none\n"); |
| 3668 | } |
| 3669 | |
| 3670 | static ssize_t |
| 3671 | metadata_store(mddev_t *mddev, const char *buf, size_t len) |
| 3672 | { |
| 3673 | int major, minor; |
| 3674 | char *e; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 3675 | /* Changing the details of 'external' metadata is |
| 3676 | * always permitted. Otherwise there must be |
| 3677 | * no devices attached to the array. |
| 3678 | */ |
| 3679 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 3680 | ; |
| 3681 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3682 | return -EBUSY; |
| 3683 | |
| 3684 | if (cmd_match(buf, "none")) { |
| 3685 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3686 | mddev->external = 0; |
| 3687 | mddev->major_version = 0; |
| 3688 | mddev->minor_version = 90; |
| 3689 | return len; |
| 3690 | } |
| 3691 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 3692 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3693 | if (namelen >= sizeof(mddev->metadata_type)) |
| 3694 | namelen = sizeof(mddev->metadata_type)-1; |
| 3695 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 3696 | mddev->metadata_type[namelen] = 0; |
| 3697 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 3698 | mddev->metadata_type[--namelen] = 0; |
| 3699 | mddev->persistent = 0; |
| 3700 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3701 | mddev->major_version = 0; |
| 3702 | mddev->minor_version = 90; |
| 3703 | return len; |
| 3704 | } |
| 3705 | major = simple_strtoul(buf, &e, 10); |
| 3706 | if (e==buf || *e != '.') |
| 3707 | return -EINVAL; |
| 3708 | buf = e+1; |
| 3709 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 3710 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3711 | return -EINVAL; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 3712 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3713 | return -ENOENT; |
| 3714 | mddev->major_version = major; |
| 3715 | mddev->minor_version = minor; |
| 3716 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3717 | mddev->external = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3718 | return len; |
| 3719 | } |
| 3720 | |
| 3721 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3722 | __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3723 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3724 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3725 | action_show(mddev_t *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3726 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3727 | char *type = "idle"; |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3728 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 3729 | type = "frozen"; |
| 3730 | else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 2b12ab6 | 2007-10-16 23:30:53 -0700 | [diff] [blame] | 3731 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3732 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 3733 | type = "reshape"; |
| 3734 | else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3735 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 3736 | type = "resync"; |
| 3737 | else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 3738 | type = "check"; |
| 3739 | else |
| 3740 | type = "repair"; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3741 | } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3742 | type = "recover"; |
| 3743 | } |
| 3744 | return sprintf(page, "%s\n", type); |
| 3745 | } |
| 3746 | |
| 3747 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3748 | action_store(mddev_t *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3749 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3750 | if (!mddev->pers || !mddev->pers->sync_request) |
| 3751 | return -EINVAL; |
| 3752 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3753 | if (cmd_match(page, "frozen")) |
| 3754 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3755 | else |
| 3756 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3757 | |
| 3758 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3759 | if (mddev->sync_thread) { |
| 3760 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 3761 | md_unregister_thread(mddev->sync_thread); |
| 3762 | mddev->sync_thread = NULL; |
| 3763 | mddev->recovery = 0; |
| 3764 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3765 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 3766 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3767 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3768 | else if (cmd_match(page, "resync")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3769 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3770 | else if (cmd_match(page, "recover")) { |
| 3771 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 3772 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3773 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3774 | int err; |
| 3775 | if (mddev->pers->start_reshape == NULL) |
| 3776 | return -EINVAL; |
| 3777 | err = mddev->pers->start_reshape(mddev); |
| 3778 | if (err) |
| 3779 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 3780 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3781 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 3782 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3783 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 3784 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3785 | return -EINVAL; |
| 3786 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 3787 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3788 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3789 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3790 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 3791 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3792 | return len; |
| 3793 | } |
| 3794 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3795 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3796 | mismatch_cnt_show(mddev_t *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3797 | { |
| 3798 | return sprintf(page, "%llu\n", |
| 3799 | (unsigned long long) mddev->resync_mismatches); |
| 3800 | } |
| 3801 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3802 | static struct md_sysfs_entry md_scan_mode = |
| 3803 | __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3804 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3805 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3806 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3807 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3808 | static ssize_t |
| 3809 | sync_min_show(mddev_t *mddev, char *page) |
| 3810 | { |
| 3811 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 3812 | mddev->sync_speed_min ? "local": "system"); |
| 3813 | } |
| 3814 | |
| 3815 | static ssize_t |
| 3816 | sync_min_store(mddev_t *mddev, const char *buf, size_t len) |
| 3817 | { |
| 3818 | int min; |
| 3819 | char *e; |
| 3820 | if (strncmp(buf, "system", 6)==0) { |
| 3821 | mddev->sync_speed_min = 0; |
| 3822 | return len; |
| 3823 | } |
| 3824 | min = simple_strtoul(buf, &e, 10); |
| 3825 | if (buf == e || (*e && *e != '\n') || min <= 0) |
| 3826 | return -EINVAL; |
| 3827 | mddev->sync_speed_min = min; |
| 3828 | return len; |
| 3829 | } |
| 3830 | |
| 3831 | static struct md_sysfs_entry md_sync_min = |
| 3832 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 3833 | |
| 3834 | static ssize_t |
| 3835 | sync_max_show(mddev_t *mddev, char *page) |
| 3836 | { |
| 3837 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 3838 | mddev->sync_speed_max ? "local": "system"); |
| 3839 | } |
| 3840 | |
| 3841 | static ssize_t |
| 3842 | sync_max_store(mddev_t *mddev, const char *buf, size_t len) |
| 3843 | { |
| 3844 | int max; |
| 3845 | char *e; |
| 3846 | if (strncmp(buf, "system", 6)==0) { |
| 3847 | mddev->sync_speed_max = 0; |
| 3848 | return len; |
| 3849 | } |
| 3850 | max = simple_strtoul(buf, &e, 10); |
| 3851 | if (buf == e || (*e && *e != '\n') || max <= 0) |
| 3852 | return -EINVAL; |
| 3853 | mddev->sync_speed_max = max; |
| 3854 | return len; |
| 3855 | } |
| 3856 | |
| 3857 | static struct md_sysfs_entry md_sync_max = |
| 3858 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 3859 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 3860 | static ssize_t |
| 3861 | degraded_show(mddev_t *mddev, char *page) |
| 3862 | { |
| 3863 | return sprintf(page, "%d\n", mddev->degraded); |
| 3864 | } |
| 3865 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3866 | |
| 3867 | static ssize_t |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 3868 | sync_force_parallel_show(mddev_t *mddev, char *page) |
| 3869 | { |
| 3870 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 3871 | } |
| 3872 | |
| 3873 | static ssize_t |
| 3874 | sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len) |
| 3875 | { |
| 3876 | long n; |
| 3877 | |
| 3878 | if (strict_strtol(buf, 10, &n)) |
| 3879 | return -EINVAL; |
| 3880 | |
| 3881 | if (n != 0 && n != 1) |
| 3882 | return -EINVAL; |
| 3883 | |
| 3884 | mddev->parallel_resync = n; |
| 3885 | |
| 3886 | if (mddev->sync_thread) |
| 3887 | wake_up(&resync_wait); |
| 3888 | |
| 3889 | return len; |
| 3890 | } |
| 3891 | |
| 3892 | /* force parallel resync, even with shared block devices */ |
| 3893 | static struct md_sysfs_entry md_sync_force_parallel = |
| 3894 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 3895 | sync_force_parallel_show, sync_force_parallel_store); |
| 3896 | |
| 3897 | static ssize_t |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3898 | sync_speed_show(mddev_t *mddev, char *page) |
| 3899 | { |
| 3900 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3901 | if (mddev->curr_resync == 0) |
| 3902 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3903 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 3904 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3905 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3906 | db = resync - mddev->resync_mark_cnt; |
| 3907 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3908 | } |
| 3909 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3910 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3911 | |
| 3912 | static ssize_t |
| 3913 | sync_completed_show(mddev_t *mddev, char *page) |
| 3914 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3915 | unsigned long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3916 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 3917 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3918 | return sprintf(page, "none\n"); |
| 3919 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3920 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3921 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3922 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3923 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3924 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 3925 | resync = mddev->curr_resync_completed; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3926 | return sprintf(page, "%lu / %lu\n", resync, max_sectors); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3927 | } |
| 3928 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3929 | static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3930 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3931 | static ssize_t |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3932 | min_sync_show(mddev_t *mddev, char *page) |
| 3933 | { |
| 3934 | return sprintf(page, "%llu\n", |
| 3935 | (unsigned long long)mddev->resync_min); |
| 3936 | } |
| 3937 | static ssize_t |
| 3938 | min_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3939 | { |
| 3940 | unsigned long long min; |
| 3941 | if (strict_strtoull(buf, 10, &min)) |
| 3942 | return -EINVAL; |
| 3943 | if (min > mddev->resync_max) |
| 3944 | return -EINVAL; |
| 3945 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3946 | return -EBUSY; |
| 3947 | |
| 3948 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3949 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 3950 | sector_t temp = min; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3951 | if (sector_div(temp, mddev->chunk_sectors)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3952 | return -EINVAL; |
| 3953 | } |
| 3954 | mddev->resync_min = min; |
| 3955 | |
| 3956 | return len; |
| 3957 | } |
| 3958 | |
| 3959 | static struct md_sysfs_entry md_min_sync = |
| 3960 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 3961 | |
| 3962 | static ssize_t |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3963 | max_sync_show(mddev_t *mddev, char *page) |
| 3964 | { |
| 3965 | if (mddev->resync_max == MaxSector) |
| 3966 | return sprintf(page, "max\n"); |
| 3967 | else |
| 3968 | return sprintf(page, "%llu\n", |
| 3969 | (unsigned long long)mddev->resync_max); |
| 3970 | } |
| 3971 | static ssize_t |
| 3972 | max_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3973 | { |
| 3974 | if (strncmp(buf, "max", 3) == 0) |
| 3975 | mddev->resync_max = MaxSector; |
| 3976 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3977 | unsigned long long max; |
| 3978 | if (strict_strtoull(buf, 10, &max)) |
| 3979 | return -EINVAL; |
| 3980 | if (max < mddev->resync_min) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3981 | return -EINVAL; |
| 3982 | if (max < mddev->resync_max && |
NeilBrown | 4d484a4 | 2009-08-13 10:41:50 +1000 | [diff] [blame] | 3983 | mddev->ro == 0 && |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3984 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3985 | return -EBUSY; |
| 3986 | |
| 3987 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3988 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 3989 | sector_t temp = max; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3990 | if (sector_div(temp, mddev->chunk_sectors)) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3991 | return -EINVAL; |
| 3992 | } |
| 3993 | mddev->resync_max = max; |
| 3994 | } |
| 3995 | wake_up(&mddev->recovery_wait); |
| 3996 | return len; |
| 3997 | } |
| 3998 | |
| 3999 | static struct md_sysfs_entry md_max_sync = |
| 4000 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 4001 | |
| 4002 | static ssize_t |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4003 | suspend_lo_show(mddev_t *mddev, char *page) |
| 4004 | { |
| 4005 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 4006 | } |
| 4007 | |
| 4008 | static ssize_t |
| 4009 | suspend_lo_store(mddev_t *mddev, const char *buf, size_t len) |
| 4010 | { |
| 4011 | char *e; |
| 4012 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 4013 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 4014 | if (mddev->pers == NULL || |
| 4015 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4016 | return -EINVAL; |
| 4017 | if (buf == e || (*e && *e != '\n')) |
| 4018 | return -EINVAL; |
| 4019 | if (new >= mddev->suspend_hi || |
| 4020 | (new > mddev->suspend_lo && new < mddev->suspend_hi)) { |
| 4021 | mddev->suspend_lo = new; |
| 4022 | mddev->pers->quiesce(mddev, 2); |
| 4023 | return len; |
| 4024 | } else |
| 4025 | return -EINVAL; |
| 4026 | } |
| 4027 | static struct md_sysfs_entry md_suspend_lo = |
| 4028 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 4029 | |
| 4030 | |
| 4031 | static ssize_t |
| 4032 | suspend_hi_show(mddev_t *mddev, char *page) |
| 4033 | { |
| 4034 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 4035 | } |
| 4036 | |
| 4037 | static ssize_t |
| 4038 | suspend_hi_store(mddev_t *mddev, const char *buf, size_t len) |
| 4039 | { |
| 4040 | char *e; |
| 4041 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 4042 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 4043 | if (mddev->pers == NULL || |
| 4044 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4045 | return -EINVAL; |
| 4046 | if (buf == e || (*e && *e != '\n')) |
| 4047 | return -EINVAL; |
| 4048 | if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) || |
| 4049 | (new > mddev->suspend_lo && new > mddev->suspend_hi)) { |
| 4050 | mddev->suspend_hi = new; |
| 4051 | mddev->pers->quiesce(mddev, 1); |
| 4052 | mddev->pers->quiesce(mddev, 0); |
| 4053 | return len; |
| 4054 | } else |
| 4055 | return -EINVAL; |
| 4056 | } |
| 4057 | static struct md_sysfs_entry md_suspend_hi = |
| 4058 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 4059 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4060 | static ssize_t |
| 4061 | reshape_position_show(mddev_t *mddev, char *page) |
| 4062 | { |
| 4063 | if (mddev->reshape_position != MaxSector) |
| 4064 | return sprintf(page, "%llu\n", |
| 4065 | (unsigned long long)mddev->reshape_position); |
| 4066 | strcpy(page, "none\n"); |
| 4067 | return 5; |
| 4068 | } |
| 4069 | |
| 4070 | static ssize_t |
| 4071 | reshape_position_store(mddev_t *mddev, const char *buf, size_t len) |
| 4072 | { |
| 4073 | char *e; |
| 4074 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 4075 | if (mddev->pers) |
| 4076 | return -EBUSY; |
| 4077 | if (buf == e || (*e && *e != '\n')) |
| 4078 | return -EINVAL; |
| 4079 | mddev->reshape_position = new; |
| 4080 | mddev->delta_disks = 0; |
| 4081 | mddev->new_level = mddev->level; |
| 4082 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4083 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4084 | return len; |
| 4085 | } |
| 4086 | |
| 4087 | static struct md_sysfs_entry md_reshape_position = |
| 4088 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 4089 | reshape_position_store); |
| 4090 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4091 | static ssize_t |
| 4092 | array_size_show(mddev_t *mddev, char *page) |
| 4093 | { |
| 4094 | if (mddev->external_size) |
| 4095 | return sprintf(page, "%llu\n", |
| 4096 | (unsigned long long)mddev->array_sectors/2); |
| 4097 | else |
| 4098 | return sprintf(page, "default\n"); |
| 4099 | } |
| 4100 | |
| 4101 | static ssize_t |
| 4102 | array_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 4103 | { |
| 4104 | sector_t sectors; |
| 4105 | |
| 4106 | if (strncmp(buf, "default", 7) == 0) { |
| 4107 | if (mddev->pers) |
| 4108 | sectors = mddev->pers->size(mddev, 0, 0); |
| 4109 | else |
| 4110 | sectors = mddev->array_sectors; |
| 4111 | |
| 4112 | mddev->external_size = 0; |
| 4113 | } else { |
| 4114 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
| 4115 | return -EINVAL; |
| 4116 | if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
NeilBrown | 2b69c83 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4117 | return -E2BIG; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4118 | |
| 4119 | mddev->external_size = 1; |
| 4120 | } |
| 4121 | |
| 4122 | mddev->array_sectors = sectors; |
| 4123 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 4124 | if (mddev->pers) |
| 4125 | revalidate_disk(mddev->gendisk); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4126 | |
| 4127 | return len; |
| 4128 | } |
| 4129 | |
| 4130 | static struct md_sysfs_entry md_array_size = |
| 4131 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 4132 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4133 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4134 | static struct attribute *md_default_attrs[] = { |
| 4135 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4136 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4137 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4138 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4139 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4140 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4141 | &md_metadata.attr, |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4142 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4143 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4144 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4145 | &md_reshape_position.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4146 | &md_array_size.attr, |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4147 | &max_corr_read_errors.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4148 | NULL, |
| 4149 | }; |
| 4150 | |
| 4151 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4152 | &md_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4153 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4154 | &md_sync_min.attr, |
| 4155 | &md_sync_max.attr, |
| 4156 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4157 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4158 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4159 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4160 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4161 | &md_suspend_lo.attr, |
| 4162 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4163 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4164 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4165 | NULL, |
| 4166 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4167 | static struct attribute_group md_redundancy_group = { |
| 4168 | .name = NULL, |
| 4169 | .attrs = md_redundancy_attrs, |
| 4170 | }; |
| 4171 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4172 | |
| 4173 | static ssize_t |
| 4174 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 4175 | { |
| 4176 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 4177 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4178 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4179 | |
| 4180 | if (!entry->show) |
| 4181 | return -EIO; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4182 | rv = mddev_lock(mddev); |
| 4183 | if (!rv) { |
| 4184 | rv = entry->show(mddev, page); |
| 4185 | mddev_unlock(mddev); |
| 4186 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4187 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4188 | } |
| 4189 | |
| 4190 | static ssize_t |
| 4191 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 4192 | const char *page, size_t length) |
| 4193 | { |
| 4194 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 4195 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4196 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4197 | |
| 4198 | if (!entry->store) |
| 4199 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 4200 | if (!capable(CAP_SYS_ADMIN)) |
| 4201 | return -EACCES; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4202 | rv = mddev_lock(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4203 | if (mddev->hold_active == UNTIL_IOCTL) |
| 4204 | mddev->hold_active = 0; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4205 | if (!rv) { |
| 4206 | rv = entry->store(mddev, page, length); |
| 4207 | mddev_unlock(mddev); |
| 4208 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4209 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4210 | } |
| 4211 | |
| 4212 | static void md_free(struct kobject *ko) |
| 4213 | { |
| 4214 | mddev_t *mddev = container_of(ko, mddev_t, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4215 | |
| 4216 | if (mddev->sysfs_state) |
| 4217 | sysfs_put(mddev->sysfs_state); |
| 4218 | |
| 4219 | if (mddev->gendisk) { |
| 4220 | del_gendisk(mddev->gendisk); |
| 4221 | put_disk(mddev->gendisk); |
| 4222 | } |
| 4223 | if (mddev->queue) |
| 4224 | blk_cleanup_queue(mddev->queue); |
| 4225 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4226 | kfree(mddev); |
| 4227 | } |
| 4228 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 4229 | static const struct sysfs_ops md_sysfs_ops = { |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4230 | .show = md_attr_show, |
| 4231 | .store = md_attr_store, |
| 4232 | }; |
| 4233 | static struct kobj_type md_ktype = { |
| 4234 | .release = md_free, |
| 4235 | .sysfs_ops = &md_sysfs_ops, |
| 4236 | .default_attrs = md_default_attrs, |
| 4237 | }; |
| 4238 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4239 | int mdp_major = 0; |
| 4240 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4241 | static void mddev_delayed_delete(struct work_struct *ws) |
| 4242 | { |
| 4243 | mddev_t *mddev = container_of(ws, mddev_t, del_work); |
| 4244 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4245 | sysfs_remove_group(&mddev->kobj, &md_bitmap_group); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4246 | kobject_del(&mddev->kobj); |
| 4247 | kobject_put(&mddev->kobj); |
| 4248 | } |
| 4249 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4250 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4251 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4252 | static DEFINE_MUTEX(disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4253 | mddev_t *mddev = mddev_find(dev); |
| 4254 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4255 | int partitioned; |
| 4256 | int shift; |
| 4257 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4258 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4259 | |
| 4260 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4261 | return -ENODEV; |
| 4262 | |
| 4263 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 4264 | shift = partitioned ? MdpMinorShift : 0; |
| 4265 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4266 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4267 | /* wait for any previous instance of this device to be |
| 4268 | * completely removed (mddev_delayed_delete). |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4269 | */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4270 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4271 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4272 | mutex_lock(&disks_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4273 | error = -EEXIST; |
| 4274 | if (mddev->gendisk) |
| 4275 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4276 | |
| 4277 | if (name) { |
| 4278 | /* Need to ensure that 'name' is not a duplicate. |
| 4279 | */ |
| 4280 | mddev_t *mddev2; |
| 4281 | spin_lock(&all_mddevs_lock); |
| 4282 | |
| 4283 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 4284 | if (mddev2->gendisk && |
| 4285 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 4286 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4287 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4288 | } |
| 4289 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4290 | } |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4291 | |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4292 | error = -ENOMEM; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4293 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4294 | if (!mddev->queue) |
| 4295 | goto abort; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4296 | mddev->queue->queuedata = mddev; |
| 4297 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4298 | blk_queue_make_request(mddev->queue, md_make_request); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4299 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4300 | disk = alloc_disk(1 << shift); |
| 4301 | if (!disk) { |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4302 | blk_cleanup_queue(mddev->queue); |
| 4303 | mddev->queue = NULL; |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4304 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4305 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4306 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4307 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4308 | if (name) |
| 4309 | strcpy(disk->disk_name, name); |
| 4310 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4311 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 4312 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4313 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4314 | disk->fops = &md_fops; |
| 4315 | disk->private_data = mddev; |
| 4316 | disk->queue = mddev->queue; |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4317 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4318 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4319 | * remove it now. |
| 4320 | */ |
| 4321 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4322 | add_disk(disk); |
| 4323 | mddev->gendisk = disk; |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 4324 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
| 4325 | &disk_to_dev(disk)->kobj, "%s", "md"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4326 | if (error) { |
| 4327 | /* This isn't possible, but as kobject_init_and_add is marked |
| 4328 | * __must_check, we must do something with the result |
| 4329 | */ |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4330 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 4331 | disk->disk_name); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4332 | error = 0; |
| 4333 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4334 | if (mddev->kobj.sd && |
| 4335 | sysfs_create_group(&mddev->kobj, &md_bitmap_group)) |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4336 | printk(KERN_DEBUG "pointless warning\n"); |
Darrick J. Wong | be20e6c | 2010-11-24 16:40:33 +1100 | [diff] [blame] | 4337 | |
| 4338 | blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4339 | abort: |
| 4340 | mutex_unlock(&disks_mutex); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4341 | if (!error && mddev->kobj.sd) { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4342 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4343 | mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state"); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4344 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4345 | mddev_put(mddev); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4346 | return error; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4347 | } |
| 4348 | |
| 4349 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 4350 | { |
| 4351 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4352 | return NULL; |
| 4353 | } |
| 4354 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4355 | static int add_named_array(const char *val, struct kernel_param *kp) |
| 4356 | { |
| 4357 | /* val must be "md_*" where * is not all digits. |
| 4358 | * We allocate an array with a large free minor number, and |
| 4359 | * set the name to val. val must not already be an active name. |
| 4360 | */ |
| 4361 | int len = strlen(val); |
| 4362 | char buf[DISK_NAME_LEN]; |
| 4363 | |
| 4364 | while (len && val[len-1] == '\n') |
| 4365 | len--; |
| 4366 | if (len >= DISK_NAME_LEN) |
| 4367 | return -E2BIG; |
| 4368 | strlcpy(buf, val, len+1); |
| 4369 | if (strncmp(buf, "md_", 3) != 0) |
| 4370 | return -EINVAL; |
| 4371 | return md_alloc(0, buf); |
| 4372 | } |
| 4373 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4374 | static void md_safemode_timeout(unsigned long data) |
| 4375 | { |
| 4376 | mddev_t *mddev = (mddev_t *) data; |
| 4377 | |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4378 | if (!atomic_read(&mddev->writes_pending)) { |
| 4379 | mddev->safemode = 1; |
| 4380 | if (mddev->external) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4381 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4382 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4383 | md_wakeup_thread(mddev->thread); |
| 4384 | } |
| 4385 | |
NeilBrown | 6ff8d8e | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4386 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4387 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4388 | int md_run(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4389 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4390 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4391 | mdk_rdev_t *rdev; |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4392 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4393 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4394 | if (list_empty(&mddev->disks)) |
| 4395 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4396 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4397 | |
| 4398 | if (mddev->pers) |
| 4399 | return -EBUSY; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 4400 | /* Cannot run until previous stop completes properly */ |
| 4401 | if (mddev->sysfs_active) |
| 4402 | return -EBUSY; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 4403 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4404 | /* |
| 4405 | * Analyze all RAID superblock(s) |
| 4406 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4407 | if (!mddev->raid_disks) { |
| 4408 | if (!mddev->persistent) |
| 4409 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4410 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4411 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4412 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4413 | if (mddev->level != LEVEL_NONE) |
| 4414 | request_module("md-level-%d", mddev->level); |
| 4415 | else if (mddev->clevel[0]) |
| 4416 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4417 | |
| 4418 | /* |
| 4419 | * Drop all container device buffers, from now on |
| 4420 | * the only valid external interface is through the md |
| 4421 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4422 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4423 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4424 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4425 | continue; |
| 4426 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 4427 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4428 | |
| 4429 | /* perform some consistency tests on the device. |
| 4430 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4431 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4432 | */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4433 | if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4434 | if (mddev->dev_sectors && |
| 4435 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4436 | > rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4437 | printk("md: %s: data overlaps metadata\n", |
| 4438 | mdname(mddev)); |
| 4439 | return -EINVAL; |
| 4440 | } |
| 4441 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4442 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4443 | > rdev->data_offset) { |
| 4444 | printk("md: %s: metadata overlaps data\n", |
| 4445 | mdname(mddev)); |
| 4446 | return -EINVAL; |
| 4447 | } |
| 4448 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4449 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4450 | } |
| 4451 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 4452 | if (mddev->bio_set == NULL) |
| 4453 | mddev->bio_set = bioset_create(BIO_POOL_SIZE, sizeof(mddev)); |
| 4454 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4455 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4456 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4457 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4458 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4459 | if (mddev->level != LEVEL_NONE) |
| 4460 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 4461 | mddev->level); |
| 4462 | else |
| 4463 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 4464 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4465 | return -EINVAL; |
| 4466 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4467 | mddev->pers = pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4468 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4469 | if (mddev->level != pers->level) { |
| 4470 | mddev->level = pers->level; |
| 4471 | mddev->new_level = pers->level; |
| 4472 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4473 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4474 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4475 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4476 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4477 | /* This personality cannot handle reshaping... */ |
| 4478 | mddev->pers = NULL; |
| 4479 | module_put(pers->owner); |
| 4480 | return -EINVAL; |
| 4481 | } |
| 4482 | |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4483 | if (pers->sync_request) { |
| 4484 | /* Warn if this is a potentially silly |
| 4485 | * configuration. |
| 4486 | */ |
| 4487 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 4488 | mdk_rdev_t *rdev2; |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4489 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4490 | |
| 4491 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4492 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4493 | if (rdev < rdev2 && |
| 4494 | rdev->bdev->bd_contains == |
| 4495 | rdev2->bdev->bd_contains) { |
| 4496 | printk(KERN_WARNING |
| 4497 | "%s: WARNING: %s appears to be" |
| 4498 | " on the same physical disk as" |
| 4499 | " %s.\n", |
| 4500 | mdname(mddev), |
| 4501 | bdevname(rdev->bdev,b), |
| 4502 | bdevname(rdev2->bdev,b2)); |
| 4503 | warned = 1; |
| 4504 | } |
| 4505 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4506 | |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4507 | if (warned) |
| 4508 | printk(KERN_WARNING |
| 4509 | "True protection against single-disk" |
| 4510 | " failure might be compromised.\n"); |
| 4511 | } |
| 4512 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 4513 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4514 | /* may be over-ridden by personality */ |
| 4515 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 4516 | |
NeilBrown | 6ff8d8e | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4517 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4518 | |
NeilBrown | 0f9552b | 2009-12-30 12:08:50 +1100 | [diff] [blame] | 4519 | if (start_readonly && mddev->ro == 0) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4520 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 4521 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4522 | err = mddev->pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 4523 | if (err) |
| 4524 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4525 | else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) { |
| 4526 | WARN_ONCE(!mddev->external_size, "%s: default size too small," |
| 4527 | " but 'external_size' not in effect?\n", __func__); |
| 4528 | printk(KERN_ERR |
| 4529 | "md: invalid array_size %llu > default size %llu\n", |
| 4530 | (unsigned long long)mddev->array_sectors / 2, |
| 4531 | (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2); |
| 4532 | err = -EINVAL; |
| 4533 | mddev->pers->stop(mddev); |
| 4534 | } |
| 4535 | if (err == 0 && mddev->pers->sync_request) { |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4536 | err = bitmap_create(mddev); |
| 4537 | if (err) { |
| 4538 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 4539 | mdname(mddev), err); |
| 4540 | mddev->pers->stop(mddev); |
| 4541 | } |
| 4542 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4543 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4544 | module_put(mddev->pers->owner); |
| 4545 | mddev->pers = NULL; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4546 | bitmap_destroy(mddev); |
| 4547 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4548 | } |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4549 | if (mddev->pers->sync_request) { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4550 | if (mddev->kobj.sd && |
| 4551 | sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4552 | printk(KERN_WARNING |
| 4553 | "md: cannot register extra attributes for %s\n", |
| 4554 | mdname(mddev)); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4555 | mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4556 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 4557 | mddev->ro = 0; |
| 4558 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4559 | atomic_set(&mddev->writes_pending,0); |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4560 | atomic_set(&mddev->max_corr_read_errors, |
| 4561 | MD_DEFAULT_MAX_CORRECTED_READ_ERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4562 | mddev->safemode = 0; |
| 4563 | mddev->safemode_timer.function = md_safemode_timeout; |
| 4564 | mddev->safemode_timer.data = (unsigned long) mddev; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4565 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4566 | mddev->in_sync = 1; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4567 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4568 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4569 | if (rdev->raid_disk >= 0) { |
| 4570 | char nm[20]; |
| 4571 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4572 | if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4573 | /* failure here is OK */; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4574 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4575 | |
| 4576 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4577 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4578 | if (mddev->flags) |
| 4579 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4580 | |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 4581 | md_wakeup_thread(mddev->thread); |
| 4582 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4583 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4584 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4585 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
| 4586 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4587 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4588 | return 0; |
| 4589 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4590 | EXPORT_SYMBOL_GPL(md_run); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4591 | |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 4592 | static int do_md_run(mddev_t *mddev) |
| 4593 | { |
| 4594 | int err; |
| 4595 | |
| 4596 | err = md_run(mddev); |
| 4597 | if (err) |
| 4598 | goto out; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 4599 | err = bitmap_load(mddev); |
| 4600 | if (err) { |
| 4601 | bitmap_destroy(mddev); |
| 4602 | goto out; |
| 4603 | } |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 4604 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 4605 | revalidate_disk(mddev->gendisk); |
| 4606 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
| 4607 | out: |
| 4608 | return err; |
| 4609 | } |
| 4610 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4611 | static int restart_array(mddev_t *mddev) |
| 4612 | { |
| 4613 | struct gendisk *disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4614 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4615 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4616 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4617 | return -ENXIO; |
| 4618 | if (!mddev->pers) |
| 4619 | return -EINVAL; |
| 4620 | if (!mddev->ro) |
| 4621 | return -EBUSY; |
| 4622 | mddev->safemode = 0; |
| 4623 | mddev->ro = 0; |
| 4624 | set_disk_ro(disk, 0); |
| 4625 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 4626 | mdname(mddev)); |
| 4627 | /* Kick recovery or resync if necessary */ |
| 4628 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4629 | md_wakeup_thread(mddev->thread); |
| 4630 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4631 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4632 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4633 | } |
| 4634 | |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4635 | /* similar to deny_write_access, but accounts for our holding a reference |
| 4636 | * to the file ourselves */ |
| 4637 | static int deny_bitmap_write_access(struct file * file) |
| 4638 | { |
| 4639 | struct inode *inode = file->f_mapping->host; |
| 4640 | |
| 4641 | spin_lock(&inode->i_lock); |
| 4642 | if (atomic_read(&inode->i_writecount) > 1) { |
| 4643 | spin_unlock(&inode->i_lock); |
| 4644 | return -ETXTBSY; |
| 4645 | } |
| 4646 | atomic_set(&inode->i_writecount, -1); |
| 4647 | spin_unlock(&inode->i_lock); |
| 4648 | |
| 4649 | return 0; |
| 4650 | } |
| 4651 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4652 | void restore_bitmap_write_access(struct file *file) |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4653 | { |
| 4654 | struct inode *inode = file->f_mapping->host; |
| 4655 | |
| 4656 | spin_lock(&inode->i_lock); |
| 4657 | atomic_set(&inode->i_writecount, 1); |
| 4658 | spin_unlock(&inode->i_lock); |
| 4659 | } |
| 4660 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4661 | static void md_clean(mddev_t *mddev) |
| 4662 | { |
| 4663 | mddev->array_sectors = 0; |
| 4664 | mddev->external_size = 0; |
| 4665 | mddev->dev_sectors = 0; |
| 4666 | mddev->raid_disks = 0; |
| 4667 | mddev->recovery_cp = 0; |
| 4668 | mddev->resync_min = 0; |
| 4669 | mddev->resync_max = MaxSector; |
| 4670 | mddev->reshape_position = MaxSector; |
| 4671 | mddev->external = 0; |
| 4672 | mddev->persistent = 0; |
| 4673 | mddev->level = LEVEL_NONE; |
| 4674 | mddev->clevel[0] = 0; |
| 4675 | mddev->flags = 0; |
| 4676 | mddev->ro = 0; |
| 4677 | mddev->metadata_type[0] = 0; |
| 4678 | mddev->chunk_sectors = 0; |
| 4679 | mddev->ctime = mddev->utime = 0; |
| 4680 | mddev->layout = 0; |
| 4681 | mddev->max_disks = 0; |
| 4682 | mddev->events = 0; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 4683 | mddev->can_decrease_events = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4684 | mddev->delta_disks = 0; |
| 4685 | mddev->new_level = LEVEL_NONE; |
| 4686 | mddev->new_layout = 0; |
| 4687 | mddev->new_chunk_sectors = 0; |
| 4688 | mddev->curr_resync = 0; |
| 4689 | mddev->resync_mismatches = 0; |
| 4690 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 4691 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 4692 | mddev->recovery = 0; |
| 4693 | mddev->in_sync = 0; |
| 4694 | mddev->degraded = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4695 | mddev->safemode = 0; |
| 4696 | mddev->bitmap_info.offset = 0; |
| 4697 | mddev->bitmap_info.default_offset = 0; |
| 4698 | mddev->bitmap_info.chunksize = 0; |
| 4699 | mddev->bitmap_info.daemon_sleep = 0; |
| 4700 | mddev->bitmap_info.max_write_behind = 0; |
NeilBrown | 252ac52 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 4701 | mddev->plug = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4702 | } |
| 4703 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4704 | void md_stop_writes(mddev_t *mddev) |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 4705 | { |
| 4706 | if (mddev->sync_thread) { |
| 4707 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4708 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 4709 | md_unregister_thread(mddev->sync_thread); |
| 4710 | mddev->sync_thread = NULL; |
| 4711 | } |
| 4712 | |
| 4713 | del_timer_sync(&mddev->safemode_timer); |
| 4714 | |
| 4715 | bitmap_flush(mddev); |
| 4716 | md_super_wait(mddev); |
| 4717 | |
| 4718 | if (!mddev->in_sync || mddev->flags) { |
| 4719 | /* mark array as shutdown cleanly */ |
| 4720 | mddev->in_sync = 1; |
| 4721 | md_update_sb(mddev, 1); |
| 4722 | } |
| 4723 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4724 | EXPORT_SYMBOL_GPL(md_stop_writes); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 4725 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4726 | void md_stop(mddev_t *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4727 | { |
| 4728 | mddev->pers->stop(mddev); |
| 4729 | if (mddev->pers->sync_request && mddev->to_remove == NULL) |
| 4730 | mddev->to_remove = &md_redundancy_group; |
| 4731 | module_put(mddev->pers->owner); |
| 4732 | mddev->pers = NULL; |
NeilBrown | cca9cf9 | 2010-04-01 12:08:16 +1100 | [diff] [blame] | 4733 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4734 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4735 | EXPORT_SYMBOL_GPL(md_stop); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4736 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4737 | static int md_set_readonly(mddev_t *mddev, int is_open) |
| 4738 | { |
| 4739 | int err = 0; |
| 4740 | mutex_lock(&mddev->open_mutex); |
| 4741 | if (atomic_read(&mddev->openers) > is_open) { |
| 4742 | printk("md: %s still in use.\n",mdname(mddev)); |
| 4743 | err = -EBUSY; |
| 4744 | goto out; |
| 4745 | } |
| 4746 | if (mddev->pers) { |
| 4747 | md_stop_writes(mddev); |
| 4748 | |
| 4749 | err = -ENXIO; |
| 4750 | if (mddev->ro==1) |
| 4751 | goto out; |
| 4752 | mddev->ro = 1; |
| 4753 | set_disk_ro(mddev->gendisk, 1); |
| 4754 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4755 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4756 | err = 0; |
| 4757 | } |
| 4758 | out: |
| 4759 | mutex_unlock(&mddev->open_mutex); |
| 4760 | return err; |
| 4761 | } |
| 4762 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4763 | /* mode: |
| 4764 | * 0 - completely stop and dis-assemble array |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4765 | * 2 - stop but do not disassemble array |
| 4766 | */ |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4767 | 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] | 4768 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4769 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 4770 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4771 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 4772 | mutex_lock(&mddev->open_mutex); |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 4773 | if (atomic_read(&mddev->openers) > is_open || |
| 4774 | mddev->sysfs_active) { |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4775 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 4776 | mutex_unlock(&mddev->open_mutex); |
| 4777 | return -EBUSY; |
| 4778 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4779 | |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 4780 | if (mddev->pers) { |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4781 | if (mddev->ro) |
| 4782 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4783 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4784 | md_stop_writes(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4785 | md_stop(mddev); |
| 4786 | mddev->queue->merge_bvec_fn = NULL; |
| 4787 | mddev->queue->unplug_fn = NULL; |
| 4788 | mddev->queue->backing_dev_info.congested_fn = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4789 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4790 | /* tell userspace to handle 'inactive' */ |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4791 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4792 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4793 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4794 | if (rdev->raid_disk >= 0) { |
| 4795 | char nm[20]; |
| 4796 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 4797 | sysfs_remove_link(&mddev->kobj, nm); |
| 4798 | } |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 4799 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4800 | set_capacity(disk, 0); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 4801 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4802 | revalidate_disk(disk); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4803 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4804 | if (mddev->ro) |
| 4805 | mddev->ro = 0; |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 4806 | } else |
| 4807 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4808 | /* |
| 4809 | * Free resources if final stop |
| 4810 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4811 | if (mode == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4812 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 4813 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4814 | bitmap_destroy(mddev); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4815 | if (mddev->bitmap_info.file) { |
| 4816 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 4817 | fput(mddev->bitmap_info.file); |
| 4818 | mddev->bitmap_info.file = NULL; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4819 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4820 | mddev->bitmap_info.offset = 0; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4821 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4822 | export_array(mddev); |
| 4823 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4824 | md_clean(mddev); |
NeilBrown | 934d9c2 | 2008-10-28 17:01:23 +1100 | [diff] [blame] | 4825 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4826 | if (mddev->hold_active == UNTIL_STOP) |
| 4827 | mddev->hold_active = 0; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4828 | } |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 4829 | blk_integrity_unregister(disk); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4830 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4831 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 4832 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4833 | } |
| 4834 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4835 | #ifndef MODULE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4836 | static void autorun_array(mddev_t *mddev) |
| 4837 | { |
| 4838 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4839 | int err; |
| 4840 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4841 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4842 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4843 | |
| 4844 | printk(KERN_INFO "md: running: "); |
| 4845 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4846 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4847 | char b[BDEVNAME_SIZE]; |
| 4848 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 4849 | } |
| 4850 | printk("\n"); |
| 4851 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4852 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4853 | if (err) { |
| 4854 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4855 | do_md_stop(mddev, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4856 | } |
| 4857 | } |
| 4858 | |
| 4859 | /* |
| 4860 | * lets try to run arrays based on all disks that have arrived |
| 4861 | * until now. (those are in pending_raid_disks) |
| 4862 | * |
| 4863 | * the method: pick the first pending disk, collect all disks with |
| 4864 | * the same UUID, remove all from the pending list and put them into |
| 4865 | * the 'same_array' list. Then order this list based on superblock |
| 4866 | * update time (freshest comes first), kick out 'old' disks and |
| 4867 | * compare superblocks. If everything's fine then run it. |
| 4868 | * |
| 4869 | * If "unit" is allocated, then bump its reference count |
| 4870 | */ |
| 4871 | static void autorun_devices(int part) |
| 4872 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4873 | mdk_rdev_t *rdev0, *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4874 | mddev_t *mddev; |
| 4875 | char b[BDEVNAME_SIZE]; |
| 4876 | |
| 4877 | printk(KERN_INFO "md: autorun ...\n"); |
| 4878 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4879 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4880 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 4881 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4882 | rdev0 = list_entry(pending_raid_disks.next, |
| 4883 | mdk_rdev_t, same_set); |
| 4884 | |
| 4885 | printk(KERN_INFO "md: considering %s ...\n", |
| 4886 | bdevname(rdev0->bdev,b)); |
| 4887 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4888 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4889 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 4890 | printk(KERN_INFO "md: adding %s ...\n", |
| 4891 | bdevname(rdev->bdev,b)); |
| 4892 | list_move(&rdev->same_set, &candidates); |
| 4893 | } |
| 4894 | /* |
| 4895 | * now we have a set of devices, with all of them having |
| 4896 | * mostly sane superblocks. It's time to allocate the |
| 4897 | * mddev. |
| 4898 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4899 | if (part) { |
| 4900 | dev = MKDEV(mdp_major, |
| 4901 | rdev0->preferred_minor << MdpMinorShift); |
| 4902 | unit = MINOR(dev) >> MdpMinorShift; |
| 4903 | } else { |
| 4904 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 4905 | unit = MINOR(dev); |
| 4906 | } |
| 4907 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4908 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 4909 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 4910 | break; |
| 4911 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4912 | |
| 4913 | md_probe(dev, NULL, NULL); |
| 4914 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 4915 | if (!mddev || !mddev->gendisk) { |
| 4916 | if (mddev) |
| 4917 | mddev_put(mddev); |
| 4918 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4919 | "md: cannot allocate memory for md drive.\n"); |
| 4920 | break; |
| 4921 | } |
| 4922 | if (mddev_lock(mddev)) |
| 4923 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 4924 | mdname(mddev)); |
| 4925 | else if (mddev->raid_disks || mddev->major_version |
| 4926 | || !list_empty(&mddev->disks)) { |
| 4927 | printk(KERN_WARNING |
| 4928 | "md: %s already running, cannot run %s\n", |
| 4929 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 4930 | mddev_unlock(mddev); |
| 4931 | } else { |
| 4932 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4933 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4934 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4935 | list_del_init(&rdev->same_set); |
| 4936 | if (bind_rdev_to_array(rdev, mddev)) |
| 4937 | export_rdev(rdev); |
| 4938 | } |
| 4939 | autorun_array(mddev); |
| 4940 | mddev_unlock(mddev); |
| 4941 | } |
| 4942 | /* on success, candidates will be empty, on error |
| 4943 | * it won't... |
| 4944 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4945 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4946 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4947 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4948 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4949 | mddev_put(mddev); |
| 4950 | } |
| 4951 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 4952 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4953 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4954 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4955 | static int get_version(void __user * arg) |
| 4956 | { |
| 4957 | mdu_version_t ver; |
| 4958 | |
| 4959 | ver.major = MD_MAJOR_VERSION; |
| 4960 | ver.minor = MD_MINOR_VERSION; |
| 4961 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 4962 | |
| 4963 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 4964 | return -EFAULT; |
| 4965 | |
| 4966 | return 0; |
| 4967 | } |
| 4968 | |
| 4969 | static int get_array_info(mddev_t * mddev, void __user * arg) |
| 4970 | { |
| 4971 | mdu_array_info_t info; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4972 | int nr,working,insync,failed,spare; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4973 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4974 | |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4975 | nr=working=insync=failed=spare=0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4976 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4977 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4978 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4979 | failed++; |
| 4980 | else { |
| 4981 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4982 | if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4983 | insync++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4984 | else |
| 4985 | spare++; |
| 4986 | } |
| 4987 | } |
| 4988 | |
| 4989 | info.major_version = mddev->major_version; |
| 4990 | info.minor_version = mddev->minor_version; |
| 4991 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 4992 | info.ctime = mddev->ctime; |
| 4993 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4994 | info.size = mddev->dev_sectors / 2; |
| 4995 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 4996 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4997 | info.nr_disks = nr; |
| 4998 | info.raid_disks = mddev->raid_disks; |
| 4999 | info.md_minor = mddev->md_minor; |
| 5000 | info.not_persistent= !mddev->persistent; |
| 5001 | |
| 5002 | info.utime = mddev->utime; |
| 5003 | info.state = 0; |
| 5004 | if (mddev->in_sync) |
| 5005 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5006 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5007 | info.state = (1<<MD_SB_BITMAP_PRESENT); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5008 | info.active_disks = insync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5009 | info.working_disks = working; |
| 5010 | info.failed_disks = failed; |
| 5011 | info.spare_disks = spare; |
| 5012 | |
| 5013 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5014 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5015 | |
| 5016 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5017 | return -EFAULT; |
| 5018 | |
| 5019 | return 0; |
| 5020 | } |
| 5021 | |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 5022 | static int get_bitmap_file(mddev_t * mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5023 | { |
| 5024 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
| 5025 | char *ptr, *buf = NULL; |
| 5026 | int err = -ENOMEM; |
| 5027 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5028 | if (md_allow_write(mddev)) |
| 5029 | file = kmalloc(sizeof(*file), GFP_NOIO); |
| 5030 | else |
| 5031 | file = kmalloc(sizeof(*file), GFP_KERNEL); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5032 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5033 | if (!file) |
| 5034 | goto out; |
| 5035 | |
| 5036 | /* bitmap disabled, zero the first byte and copy out */ |
| 5037 | if (!mddev->bitmap || !mddev->bitmap->file) { |
| 5038 | file->pathname[0] = '\0'; |
| 5039 | goto copy_out; |
| 5040 | } |
| 5041 | |
| 5042 | buf = kmalloc(sizeof(file->pathname), GFP_KERNEL); |
| 5043 | if (!buf) |
| 5044 | goto out; |
| 5045 | |
Christoph Hellwig | 6bcfd60 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 5046 | ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname)); |
| 5047 | if (IS_ERR(ptr)) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5048 | goto out; |
| 5049 | |
| 5050 | strcpy(file->pathname, ptr); |
| 5051 | |
| 5052 | copy_out: |
| 5053 | err = 0; |
| 5054 | if (copy_to_user(arg, file, sizeof(*file))) |
| 5055 | err = -EFAULT; |
| 5056 | out: |
| 5057 | kfree(buf); |
| 5058 | kfree(file); |
| 5059 | return err; |
| 5060 | } |
| 5061 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5062 | static int get_disk_info(mddev_t * mddev, void __user * arg) |
| 5063 | { |
| 5064 | mdu_disk_info_t info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5065 | mdk_rdev_t *rdev; |
| 5066 | |
| 5067 | if (copy_from_user(&info, arg, sizeof(info))) |
| 5068 | return -EFAULT; |
| 5069 | |
Andre Noll | 26ef379 | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5070 | rdev = find_rdev_nr(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5071 | if (rdev) { |
| 5072 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 5073 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 5074 | info.raid_disk = rdev->raid_disk; |
| 5075 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5076 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5077 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5078 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5079 | info.state |= (1<<MD_DISK_ACTIVE); |
| 5080 | info.state |= (1<<MD_DISK_SYNC); |
| 5081 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5082 | if (test_bit(WriteMostly, &rdev->flags)) |
| 5083 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5084 | } else { |
| 5085 | info.major = info.minor = 0; |
| 5086 | info.raid_disk = -1; |
| 5087 | info.state = (1<<MD_DISK_REMOVED); |
| 5088 | } |
| 5089 | |
| 5090 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5091 | return -EFAULT; |
| 5092 | |
| 5093 | return 0; |
| 5094 | } |
| 5095 | |
| 5096 | static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info) |
| 5097 | { |
| 5098 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 5099 | mdk_rdev_t *rdev; |
| 5100 | dev_t dev = MKDEV(info->major,info->minor); |
| 5101 | |
| 5102 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 5103 | return -EOVERFLOW; |
| 5104 | |
| 5105 | if (!mddev->raid_disks) { |
| 5106 | int err; |
| 5107 | /* expecting a device which has a superblock */ |
| 5108 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 5109 | if (IS_ERR(rdev)) { |
| 5110 | printk(KERN_WARNING |
| 5111 | "md: md_import_device returned %ld\n", |
| 5112 | PTR_ERR(rdev)); |
| 5113 | return PTR_ERR(rdev); |
| 5114 | } |
| 5115 | if (!list_empty(&mddev->disks)) { |
| 5116 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 5117 | mdk_rdev_t, same_set); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5118 | err = super_types[mddev->major_version] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5119 | .load_super(rdev, rdev0, mddev->minor_version); |
| 5120 | if (err < 0) { |
| 5121 | printk(KERN_WARNING |
| 5122 | "md: %s has different UUID to %s\n", |
| 5123 | bdevname(rdev->bdev,b), |
| 5124 | bdevname(rdev0->bdev,b2)); |
| 5125 | export_rdev(rdev); |
| 5126 | return -EINVAL; |
| 5127 | } |
| 5128 | } |
| 5129 | err = bind_rdev_to_array(rdev, mddev); |
| 5130 | if (err) |
| 5131 | export_rdev(rdev); |
| 5132 | return err; |
| 5133 | } |
| 5134 | |
| 5135 | /* |
| 5136 | * add_new_disk can be used once the array is assembled |
| 5137 | * to add "hot spares". They must already have a superblock |
| 5138 | * written |
| 5139 | */ |
| 5140 | if (mddev->pers) { |
| 5141 | int err; |
| 5142 | if (!mddev->pers->hot_add_disk) { |
| 5143 | printk(KERN_WARNING |
| 5144 | "%s: personality does not support diskops!\n", |
| 5145 | mdname(mddev)); |
| 5146 | return -EINVAL; |
| 5147 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 5148 | if (mddev->persistent) |
| 5149 | rdev = md_import_device(dev, mddev->major_version, |
| 5150 | mddev->minor_version); |
| 5151 | else |
| 5152 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5153 | if (IS_ERR(rdev)) { |
| 5154 | printk(KERN_WARNING |
| 5155 | "md: md_import_device returned %ld\n", |
| 5156 | PTR_ERR(rdev)); |
| 5157 | return PTR_ERR(rdev); |
| 5158 | } |
NeilBrown | 1a855a0 | 2010-12-09 16:36:28 +1100 | [diff] [blame] | 5159 | /* set saved_raid_disk if appropriate */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5160 | if (!mddev->persistent) { |
| 5161 | if (info->state & (1<<MD_DISK_SYNC) && |
| 5162 | info->raid_disk < mddev->raid_disks) |
| 5163 | rdev->raid_disk = info->raid_disk; |
| 5164 | else |
| 5165 | rdev->raid_disk = -1; |
| 5166 | } else |
| 5167 | super_types[mddev->major_version]. |
| 5168 | validate_super(mddev, rdev); |
NeilBrown | 1a855a0 | 2010-12-09 16:36:28 +1100 | [diff] [blame] | 5169 | if (test_bit(In_sync, &rdev->flags)) |
| 5170 | rdev->saved_raid_disk = rdev->raid_disk; |
| 5171 | else |
| 5172 | rdev->saved_raid_disk = -1; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5173 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5174 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5175 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5176 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5177 | else |
| 5178 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5179 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5180 | rdev->raid_disk = -1; |
| 5181 | err = bind_rdev_to_array(rdev, mddev); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 5182 | if (!err && !mddev->pers->hot_remove_disk) { |
| 5183 | /* If there is hot_add_disk but no hot_remove_disk |
| 5184 | * then added disks for geometry changes, |
| 5185 | * and should be added immediately. |
| 5186 | */ |
| 5187 | super_types[mddev->major_version]. |
| 5188 | validate_super(mddev, rdev); |
| 5189 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 5190 | if (err) |
| 5191 | unbind_rdev_from_array(rdev); |
| 5192 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5193 | if (err) |
| 5194 | export_rdev(rdev); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 5195 | else |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5196 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5197 | |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 5198 | md_update_sb(mddev, 1); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 5199 | if (mddev->degraded) |
| 5200 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5201 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 5202 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5203 | return err; |
| 5204 | } |
| 5205 | |
| 5206 | /* otherwise, add_new_disk is only allowed |
| 5207 | * for major_version==0 superblocks |
| 5208 | */ |
| 5209 | if (mddev->major_version != 0) { |
| 5210 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 5211 | mdname(mddev)); |
| 5212 | return -EINVAL; |
| 5213 | } |
| 5214 | |
| 5215 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 5216 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5217 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5218 | if (IS_ERR(rdev)) { |
| 5219 | printk(KERN_WARNING |
| 5220 | "md: error, md_import_device() returned %ld\n", |
| 5221 | PTR_ERR(rdev)); |
| 5222 | return PTR_ERR(rdev); |
| 5223 | } |
| 5224 | rdev->desc_nr = info->number; |
| 5225 | if (info->raid_disk < mddev->raid_disks) |
| 5226 | rdev->raid_disk = info->raid_disk; |
| 5227 | else |
| 5228 | rdev->raid_disk = -1; |
| 5229 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5230 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5231 | if (info->state & (1<<MD_DISK_SYNC)) |
| 5232 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5233 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5234 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5235 | set_bit(WriteMostly, &rdev->flags); |
| 5236 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5237 | if (!mddev->persistent) { |
| 5238 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 5239 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
| 5240 | } else |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5241 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5242 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5243 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5244 | err = bind_rdev_to_array(rdev, mddev); |
| 5245 | if (err) { |
| 5246 | export_rdev(rdev); |
| 5247 | return err; |
| 5248 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5249 | } |
| 5250 | |
| 5251 | return 0; |
| 5252 | } |
| 5253 | |
| 5254 | static int hot_remove_disk(mddev_t * mddev, dev_t dev) |
| 5255 | { |
| 5256 | char b[BDEVNAME_SIZE]; |
| 5257 | mdk_rdev_t *rdev; |
| 5258 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5259 | rdev = find_rdev(mddev, dev); |
| 5260 | if (!rdev) |
| 5261 | return -ENXIO; |
| 5262 | |
| 5263 | if (rdev->raid_disk >= 0) |
| 5264 | goto busy; |
| 5265 | |
| 5266 | kick_rdev_from_array(rdev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5267 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5268 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5269 | |
| 5270 | return 0; |
| 5271 | busy: |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 5272 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5273 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5274 | return -EBUSY; |
| 5275 | } |
| 5276 | |
| 5277 | static int hot_add_disk(mddev_t * mddev, dev_t dev) |
| 5278 | { |
| 5279 | char b[BDEVNAME_SIZE]; |
| 5280 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5281 | mdk_rdev_t *rdev; |
| 5282 | |
| 5283 | if (!mddev->pers) |
| 5284 | return -ENODEV; |
| 5285 | |
| 5286 | if (mddev->major_version != 0) { |
| 5287 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 5288 | " version-0 superblocks.\n", |
| 5289 | mdname(mddev)); |
| 5290 | return -EINVAL; |
| 5291 | } |
| 5292 | if (!mddev->pers->hot_add_disk) { |
| 5293 | printk(KERN_WARNING |
| 5294 | "%s: personality does not support diskops!\n", |
| 5295 | mdname(mddev)); |
| 5296 | return -EINVAL; |
| 5297 | } |
| 5298 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5299 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5300 | if (IS_ERR(rdev)) { |
| 5301 | printk(KERN_WARNING |
| 5302 | "md: error, md_import_device() returned %ld\n", |
| 5303 | PTR_ERR(rdev)); |
| 5304 | return -EINVAL; |
| 5305 | } |
| 5306 | |
| 5307 | if (mddev->persistent) |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5308 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5309 | else |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 5310 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5311 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5312 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5313 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5314 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5315 | printk(KERN_WARNING |
| 5316 | "md: can not hot-add faulty %s disk to %s!\n", |
| 5317 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5318 | err = -EINVAL; |
| 5319 | goto abort_export; |
| 5320 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5321 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5322 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 5323 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5324 | err = bind_rdev_to_array(rdev, mddev); |
| 5325 | if (err) |
| 5326 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5327 | |
| 5328 | /* |
| 5329 | * The rest should better be atomic, we can have disk failures |
| 5330 | * noticed in interrupt contexts ... |
| 5331 | */ |
| 5332 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5333 | rdev->raid_disk = -1; |
| 5334 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5335 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5336 | |
| 5337 | /* |
| 5338 | * Kick recovery, maybe this spare has to be added to the |
| 5339 | * array immediately. |
| 5340 | */ |
| 5341 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5342 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5343 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5344 | return 0; |
| 5345 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5346 | abort_export: |
| 5347 | export_rdev(rdev); |
| 5348 | return err; |
| 5349 | } |
| 5350 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5351 | static int set_bitmap_file(mddev_t *mddev, int fd) |
| 5352 | { |
| 5353 | int err; |
| 5354 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5355 | if (mddev->pers) { |
| 5356 | if (!mddev->pers->quiesce) |
| 5357 | return -EBUSY; |
| 5358 | if (mddev->recovery || mddev->sync_thread) |
| 5359 | return -EBUSY; |
| 5360 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5361 | } |
| 5362 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5363 | |
| 5364 | if (fd >= 0) { |
| 5365 | if (mddev->bitmap) |
| 5366 | return -EEXIST; /* cannot add when bitmap is present */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5367 | mddev->bitmap_info.file = fget(fd); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5368 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5369 | if (mddev->bitmap_info.file == NULL) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5370 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 5371 | mdname(mddev)); |
| 5372 | return -EBADF; |
| 5373 | } |
| 5374 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5375 | err = deny_bitmap_write_access(mddev->bitmap_info.file); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5376 | if (err) { |
| 5377 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 5378 | mdname(mddev)); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5379 | fput(mddev->bitmap_info.file); |
| 5380 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5381 | return err; |
| 5382 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5383 | mddev->bitmap_info.offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5384 | } else if (mddev->bitmap == NULL) |
| 5385 | return -ENOENT; /* cannot remove what isn't there */ |
| 5386 | err = 0; |
| 5387 | if (mddev->pers) { |
| 5388 | mddev->pers->quiesce(mddev, 1); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5389 | if (fd >= 0) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5390 | err = bitmap_create(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5391 | if (!err) |
| 5392 | err = bitmap_load(mddev); |
| 5393 | } |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5394 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5395 | bitmap_destroy(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5396 | fd = -1; /* make sure to put the file */ |
| 5397 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5398 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5399 | } |
| 5400 | if (fd < 0) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5401 | if (mddev->bitmap_info.file) { |
| 5402 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 5403 | fput(mddev->bitmap_info.file); |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 5404 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5405 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5406 | } |
| 5407 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5408 | return err; |
| 5409 | } |
| 5410 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5411 | /* |
| 5412 | * set_array_info is used two different ways |
| 5413 | * The original usage is when creating a new array. |
| 5414 | * In this usage, raid_disks is > 0 and it together with |
| 5415 | * level, size, not_persistent,layout,chunksize determine the |
| 5416 | * shape of the array. |
| 5417 | * This will always create an array with a type-0.90.0 superblock. |
| 5418 | * The newer usage is when assembling an array. |
| 5419 | * In this case raid_disks will be 0, and the major_version field is |
| 5420 | * use to determine which style super-blocks are to be found on the devices. |
| 5421 | * The minor and patch _version numbers are also kept incase the |
| 5422 | * super_block handler wishes to interpret them. |
| 5423 | */ |
| 5424 | static int set_array_info(mddev_t * mddev, mdu_array_info_t *info) |
| 5425 | { |
| 5426 | |
| 5427 | if (info->raid_disks == 0) { |
| 5428 | /* just setting version number for superblock loading */ |
| 5429 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 5430 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5431 | super_types[info->major_version].name == NULL) { |
| 5432 | /* maybe try to auto-load a module? */ |
| 5433 | printk(KERN_INFO |
| 5434 | "md: superblock version %d not known\n", |
| 5435 | info->major_version); |
| 5436 | return -EINVAL; |
| 5437 | } |
| 5438 | mddev->major_version = info->major_version; |
| 5439 | mddev->minor_version = info->minor_version; |
| 5440 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5441 | mddev->persistent = !info->not_persistent; |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 5442 | /* ensure mddev_put doesn't delete this now that there |
| 5443 | * is some minimal configuration. |
| 5444 | */ |
| 5445 | mddev->ctime = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5446 | return 0; |
| 5447 | } |
| 5448 | mddev->major_version = MD_MAJOR_VERSION; |
| 5449 | mddev->minor_version = MD_MINOR_VERSION; |
| 5450 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 5451 | mddev->ctime = get_seconds(); |
| 5452 | |
| 5453 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 5454 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5455 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5456 | mddev->raid_disks = info->raid_disks; |
| 5457 | /* don't set md_minor, it is determined by which /dev/md* was |
| 5458 | * openned |
| 5459 | */ |
| 5460 | if (info->state & (1<<MD_SB_CLEAN)) |
| 5461 | mddev->recovery_cp = MaxSector; |
| 5462 | else |
| 5463 | mddev->recovery_cp = 0; |
| 5464 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5465 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5466 | |
| 5467 | mddev->layout = info->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5468 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5469 | |
| 5470 | mddev->max_disks = MD_SB_DISKS; |
| 5471 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5472 | if (mddev->persistent) |
| 5473 | mddev->flags = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5474 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5475 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5476 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
| 5477 | mddev->bitmap_info.offset = 0; |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 5478 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5479 | mddev->reshape_position = MaxSector; |
| 5480 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5481 | /* |
| 5482 | * Generate a 128 bit UUID |
| 5483 | */ |
| 5484 | get_random_bytes(mddev->uuid, 16); |
| 5485 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5486 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5487 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5488 | mddev->new_layout = mddev->layout; |
| 5489 | mddev->delta_disks = 0; |
| 5490 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5491 | return 0; |
| 5492 | } |
| 5493 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5494 | void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors) |
| 5495 | { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5496 | WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__); |
| 5497 | |
| 5498 | if (mddev->external_size) |
| 5499 | return; |
| 5500 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5501 | mddev->array_sectors = array_sectors; |
| 5502 | } |
| 5503 | EXPORT_SYMBOL(md_set_array_sectors); |
| 5504 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5505 | static int update_size(mddev_t *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5506 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5507 | mdk_rdev_t *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5508 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5509 | int fit = (num_sectors == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5510 | |
| 5511 | if (mddev->pers->resize == NULL) |
| 5512 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5513 | /* The "num_sectors" is the number of sectors of each device that |
| 5514 | * is used. This can only make sense for arrays with redundancy. |
| 5515 | * linear and raid0 always use whatever space is available. We can only |
| 5516 | * consider changing this number if no resync or reconstruction is |
| 5517 | * 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] | 5518 | * 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] | 5519 | * of each device. If num_sectors is zero, we find the largest size |
| 5520 | * that fits. |
| 5521 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5522 | */ |
| 5523 | if (mddev->sync_thread) |
| 5524 | return -EBUSY; |
NeilBrown | dba034e | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 5525 | if (mddev->bitmap) |
| 5526 | /* Sorry, cannot grow a bitmap yet, just remove it, |
| 5527 | * grow, and re-add. |
| 5528 | */ |
| 5529 | return -EBUSY; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5530 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5531 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 5532 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5533 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 5534 | num_sectors = avail; |
| 5535 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5536 | return -ENOSPC; |
| 5537 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5538 | rv = mddev->pers->resize(mddev, num_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5539 | if (!rv) |
| 5540 | revalidate_disk(mddev->gendisk); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5541 | return rv; |
| 5542 | } |
| 5543 | |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5544 | static int update_raid_disks(mddev_t *mddev, int raid_disks) |
| 5545 | { |
| 5546 | int rv; |
| 5547 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5548 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5549 | return -EINVAL; |
| 5550 | if (raid_disks <= 0 || |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 5551 | (mddev->max_disks && raid_disks >= mddev->max_disks)) |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5552 | return -EINVAL; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5553 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5554 | return -EBUSY; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5555 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
| 5556 | |
| 5557 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5558 | return rv; |
| 5559 | } |
| 5560 | |
| 5561 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5562 | /* |
| 5563 | * update_array_info is used to change the configuration of an |
| 5564 | * on-line array. |
| 5565 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 5566 | * fields in the info are checked against the array. |
| 5567 | * Any differences that cannot be handled will cause an error. |
| 5568 | * Normally, only one change can be managed at a time. |
| 5569 | */ |
| 5570 | static int update_array_info(mddev_t *mddev, mdu_array_info_t *info) |
| 5571 | { |
| 5572 | int rv = 0; |
| 5573 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5574 | int state = 0; |
| 5575 | |
| 5576 | /* calculate expected state,ignoring low bits */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5577 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5578 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5579 | |
| 5580 | if (mddev->major_version != info->major_version || |
| 5581 | mddev->minor_version != info->minor_version || |
| 5582 | /* mddev->patch_version != info->patch_version || */ |
| 5583 | mddev->ctime != info->ctime || |
| 5584 | mddev->level != info->level || |
| 5585 | /* mddev->layout != info->layout || */ |
| 5586 | !mddev->persistent != info->not_persistent|| |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5587 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5588 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 5589 | ((state^info->state) & 0xfffffe00) |
| 5590 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5591 | return -EINVAL; |
| 5592 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5593 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 5594 | cnt++; |
| 5595 | if (mddev->raid_disks != info->raid_disks) |
| 5596 | cnt++; |
| 5597 | if (mddev->layout != info->layout) |
| 5598 | cnt++; |
| 5599 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 5600 | cnt++; |
| 5601 | if (cnt == 0) |
| 5602 | return 0; |
| 5603 | if (cnt > 1) |
| 5604 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5605 | |
| 5606 | if (mddev->layout != info->layout) { |
| 5607 | /* Change layout |
| 5608 | * we don't need to do anything at the md level, the |
| 5609 | * personality will take care of it all. |
| 5610 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5611 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5612 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5613 | else { |
| 5614 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5615 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5616 | if (rv) |
| 5617 | mddev->new_layout = mddev->layout; |
| 5618 | return rv; |
| 5619 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5620 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5621 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5622 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5623 | |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5624 | if (mddev->raid_disks != info->raid_disks) |
| 5625 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5626 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5627 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5628 | if (mddev->pers->quiesce == NULL) |
| 5629 | return -EINVAL; |
| 5630 | if (mddev->recovery || mddev->sync_thread) |
| 5631 | return -EBUSY; |
| 5632 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5633 | /* add the bitmap */ |
| 5634 | if (mddev->bitmap) |
| 5635 | return -EEXIST; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5636 | if (mddev->bitmap_info.default_offset == 0) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5637 | return -EINVAL; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5638 | mddev->bitmap_info.offset = |
| 5639 | mddev->bitmap_info.default_offset; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5640 | mddev->pers->quiesce(mddev, 1); |
| 5641 | rv = bitmap_create(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5642 | if (!rv) |
| 5643 | rv = bitmap_load(mddev); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5644 | if (rv) |
| 5645 | bitmap_destroy(mddev); |
| 5646 | mddev->pers->quiesce(mddev, 0); |
| 5647 | } else { |
| 5648 | /* remove the bitmap */ |
| 5649 | if (!mddev->bitmap) |
| 5650 | return -ENOENT; |
| 5651 | if (mddev->bitmap->file) |
| 5652 | return -EINVAL; |
| 5653 | mddev->pers->quiesce(mddev, 1); |
| 5654 | bitmap_destroy(mddev); |
| 5655 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5656 | mddev->bitmap_info.offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5657 | } |
| 5658 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5659 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5660 | return rv; |
| 5661 | } |
| 5662 | |
| 5663 | static int set_disk_faulty(mddev_t *mddev, dev_t dev) |
| 5664 | { |
| 5665 | mdk_rdev_t *rdev; |
| 5666 | |
| 5667 | if (mddev->pers == NULL) |
| 5668 | return -ENODEV; |
| 5669 | |
| 5670 | rdev = find_rdev(mddev, dev); |
| 5671 | if (!rdev) |
| 5672 | return -ENODEV; |
| 5673 | |
| 5674 | md_error(mddev, rdev); |
| 5675 | return 0; |
| 5676 | } |
| 5677 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 5678 | /* |
| 5679 | * We have a problem here : there is no easy way to give a CHS |
| 5680 | * virtual geometry. We currently pretend that we have a 2 heads |
| 5681 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 5682 | * dosfs just mad... ;-) |
| 5683 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5684 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 5685 | { |
| 5686 | mddev_t *mddev = bdev->bd_disk->private_data; |
| 5687 | |
| 5688 | geo->heads = 2; |
| 5689 | geo->sectors = 4; |
NeilBrown | 49ce6ce | 2010-03-29 10:51:42 +1100 | [diff] [blame] | 5690 | geo->cylinders = mddev->array_sectors / 8; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5691 | return 0; |
| 5692 | } |
| 5693 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5694 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5695 | unsigned int cmd, unsigned long arg) |
| 5696 | { |
| 5697 | int err = 0; |
| 5698 | void __user *argp = (void __user *)arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5699 | mddev_t *mddev = NULL; |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 5700 | int ro; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5701 | |
| 5702 | if (!capable(CAP_SYS_ADMIN)) |
| 5703 | return -EACCES; |
| 5704 | |
| 5705 | /* |
| 5706 | * Commands dealing with the RAID driver but not any |
| 5707 | * particular array: |
| 5708 | */ |
| 5709 | switch (cmd) |
| 5710 | { |
| 5711 | case RAID_VERSION: |
| 5712 | err = get_version(argp); |
| 5713 | goto done; |
| 5714 | |
| 5715 | case PRINT_RAID_DEBUG: |
| 5716 | err = 0; |
| 5717 | md_print_devices(); |
| 5718 | goto done; |
| 5719 | |
| 5720 | #ifndef MODULE |
| 5721 | case RAID_AUTORUN: |
| 5722 | err = 0; |
| 5723 | autostart_arrays(arg); |
| 5724 | goto done; |
| 5725 | #endif |
| 5726 | default:; |
| 5727 | } |
| 5728 | |
| 5729 | /* |
| 5730 | * Commands creating/starting a new array: |
| 5731 | */ |
| 5732 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5733 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5734 | |
| 5735 | if (!mddev) { |
| 5736 | BUG(); |
| 5737 | goto abort; |
| 5738 | } |
| 5739 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5740 | err = mddev_lock(mddev); |
| 5741 | if (err) { |
| 5742 | printk(KERN_INFO |
| 5743 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 5744 | err, cmd); |
| 5745 | goto abort; |
| 5746 | } |
| 5747 | |
| 5748 | switch (cmd) |
| 5749 | { |
| 5750 | case SET_ARRAY_INFO: |
| 5751 | { |
| 5752 | mdu_array_info_t info; |
| 5753 | if (!arg) |
| 5754 | memset(&info, 0, sizeof(info)); |
| 5755 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 5756 | err = -EFAULT; |
| 5757 | goto abort_unlock; |
| 5758 | } |
| 5759 | if (mddev->pers) { |
| 5760 | err = update_array_info(mddev, &info); |
| 5761 | if (err) { |
| 5762 | printk(KERN_WARNING "md: couldn't update" |
| 5763 | " array info. %d\n", err); |
| 5764 | goto abort_unlock; |
| 5765 | } |
| 5766 | goto done_unlock; |
| 5767 | } |
| 5768 | if (!list_empty(&mddev->disks)) { |
| 5769 | printk(KERN_WARNING |
| 5770 | "md: array %s already has disks!\n", |
| 5771 | mdname(mddev)); |
| 5772 | err = -EBUSY; |
| 5773 | goto abort_unlock; |
| 5774 | } |
| 5775 | if (mddev->raid_disks) { |
| 5776 | printk(KERN_WARNING |
| 5777 | "md: array %s already initialised!\n", |
| 5778 | mdname(mddev)); |
| 5779 | err = -EBUSY; |
| 5780 | goto abort_unlock; |
| 5781 | } |
| 5782 | err = set_array_info(mddev, &info); |
| 5783 | if (err) { |
| 5784 | printk(KERN_WARNING "md: couldn't set" |
| 5785 | " array info. %d\n", err); |
| 5786 | goto abort_unlock; |
| 5787 | } |
| 5788 | } |
| 5789 | goto done_unlock; |
| 5790 | |
| 5791 | default:; |
| 5792 | } |
| 5793 | |
| 5794 | /* |
| 5795 | * Commands querying/configuring an existing array: |
| 5796 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5797 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5798 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 5799 | if ((!mddev->raid_disks && !mddev->external) |
| 5800 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 5801 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 5802 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5803 | err = -ENODEV; |
| 5804 | goto abort_unlock; |
| 5805 | } |
| 5806 | |
| 5807 | /* |
| 5808 | * Commands even a read-only array can execute: |
| 5809 | */ |
| 5810 | switch (cmd) |
| 5811 | { |
| 5812 | case GET_ARRAY_INFO: |
| 5813 | err = get_array_info(mddev, argp); |
| 5814 | goto done_unlock; |
| 5815 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5816 | case GET_BITMAP_FILE: |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 5817 | err = get_bitmap_file(mddev, argp); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5818 | goto done_unlock; |
| 5819 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5820 | case GET_DISK_INFO: |
| 5821 | err = get_disk_info(mddev, argp); |
| 5822 | goto done_unlock; |
| 5823 | |
| 5824 | case RESTART_ARRAY_RW: |
| 5825 | err = restart_array(mddev); |
| 5826 | goto done_unlock; |
| 5827 | |
| 5828 | case STOP_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5829 | err = do_md_stop(mddev, 0, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5830 | goto done_unlock; |
| 5831 | |
| 5832 | case STOP_ARRAY_RO: |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5833 | err = md_set_readonly(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5834 | goto done_unlock; |
| 5835 | |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 5836 | case BLKROSET: |
| 5837 | if (get_user(ro, (int __user *)(arg))) { |
| 5838 | err = -EFAULT; |
| 5839 | goto done_unlock; |
| 5840 | } |
| 5841 | err = -EINVAL; |
| 5842 | |
| 5843 | /* if the bdev is going readonly the value of mddev->ro |
| 5844 | * does not matter, no writes are coming |
| 5845 | */ |
| 5846 | if (ro) |
| 5847 | goto done_unlock; |
| 5848 | |
| 5849 | /* are we are already prepared for writes? */ |
| 5850 | if (mddev->ro != 1) |
| 5851 | goto done_unlock; |
| 5852 | |
| 5853 | /* transitioning to readauto need only happen for |
| 5854 | * arrays that call md_write_start |
| 5855 | */ |
| 5856 | if (mddev->pers) { |
| 5857 | err = restart_array(mddev); |
| 5858 | if (err == 0) { |
| 5859 | mddev->ro = 2; |
| 5860 | set_disk_ro(mddev->gendisk, 0); |
| 5861 | } |
| 5862 | } |
| 5863 | goto done_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5864 | } |
| 5865 | |
| 5866 | /* |
| 5867 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5868 | * superblock, so we do not allow them on read-only arrays. |
| 5869 | * However non-MD ioctls (e.g. get-size) will still come through |
| 5870 | * here and hit the 'default' below, so only disallow |
| 5871 | * 'md' ioctls, and switch to rw mode if started auto-readonly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5872 | */ |
Andre Noll | bb57fc6 | 2008-04-25 19:06:35 +0200 | [diff] [blame] | 5873 | if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5874 | if (mddev->ro == 2) { |
| 5875 | mddev->ro = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5876 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5877 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5878 | md_wakeup_thread(mddev->thread); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5879 | } else { |
| 5880 | err = -EROFS; |
| 5881 | goto abort_unlock; |
| 5882 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5883 | } |
| 5884 | |
| 5885 | switch (cmd) |
| 5886 | { |
| 5887 | case ADD_NEW_DISK: |
| 5888 | { |
| 5889 | mdu_disk_info_t info; |
| 5890 | if (copy_from_user(&info, argp, sizeof(info))) |
| 5891 | err = -EFAULT; |
| 5892 | else |
| 5893 | err = add_new_disk(mddev, &info); |
| 5894 | goto done_unlock; |
| 5895 | } |
| 5896 | |
| 5897 | case HOT_REMOVE_DISK: |
| 5898 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
| 5899 | goto done_unlock; |
| 5900 | |
| 5901 | case HOT_ADD_DISK: |
| 5902 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
| 5903 | goto done_unlock; |
| 5904 | |
| 5905 | case SET_DISK_FAULTY: |
| 5906 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
| 5907 | goto done_unlock; |
| 5908 | |
| 5909 | case RUN_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5910 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5911 | goto done_unlock; |
| 5912 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5913 | case SET_BITMAP_FILE: |
| 5914 | err = set_bitmap_file(mddev, (int)arg); |
| 5915 | goto done_unlock; |
| 5916 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5917 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5918 | err = -EINVAL; |
| 5919 | goto abort_unlock; |
| 5920 | } |
| 5921 | |
| 5922 | done_unlock: |
| 5923 | abort_unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5924 | if (mddev->hold_active == UNTIL_IOCTL && |
| 5925 | err != -EINVAL) |
| 5926 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5927 | mddev_unlock(mddev); |
| 5928 | |
| 5929 | return err; |
| 5930 | done: |
| 5931 | if (err) |
| 5932 | MD_BUG(); |
| 5933 | abort: |
| 5934 | return err; |
| 5935 | } |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 5936 | #ifdef CONFIG_COMPAT |
| 5937 | static int md_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 5938 | unsigned int cmd, unsigned long arg) |
| 5939 | { |
| 5940 | switch (cmd) { |
| 5941 | case HOT_REMOVE_DISK: |
| 5942 | case HOT_ADD_DISK: |
| 5943 | case SET_DISK_FAULTY: |
| 5944 | case SET_BITMAP_FILE: |
| 5945 | /* These take in integer arg, do not convert */ |
| 5946 | break; |
| 5947 | default: |
| 5948 | arg = (unsigned long)compat_ptr(arg); |
| 5949 | break; |
| 5950 | } |
| 5951 | |
| 5952 | return md_ioctl(bdev, mode, cmd, arg); |
| 5953 | } |
| 5954 | #endif /* CONFIG_COMPAT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5955 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5956 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5957 | { |
| 5958 | /* |
| 5959 | * Succeed if we can lock the mddev, which confirms that |
| 5960 | * it isn't being stopped right now. |
| 5961 | */ |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5962 | mddev_t *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5963 | int err; |
| 5964 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5965 | if (mddev->gendisk != bdev->bd_disk) { |
| 5966 | /* we are racing with mddev_put which is discarding this |
| 5967 | * bd_disk. |
| 5968 | */ |
| 5969 | mddev_put(mddev); |
| 5970 | /* Wait until bdev->bd_disk is definitely gone */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 5971 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5972 | /* Then retry the open from the top */ |
| 5973 | return -ERESTARTSYS; |
| 5974 | } |
| 5975 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 5976 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5977 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5978 | goto out; |
| 5979 | |
| 5980 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5981 | atomic_inc(&mddev->openers); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5982 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5983 | |
NeilBrown | f3b99be | 2010-06-24 13:31:03 +1000 | [diff] [blame] | 5984 | check_disk_size_change(mddev->gendisk, bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5985 | out: |
| 5986 | return err; |
| 5987 | } |
| 5988 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5989 | static int md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5990 | { |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5991 | mddev_t *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5992 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 5993 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5994 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5995 | mddev_put(mddev); |
| 5996 | |
| 5997 | return 0; |
| 5998 | } |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 5999 | static const struct block_device_operations md_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6000 | { |
| 6001 | .owner = THIS_MODULE, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6002 | .open = md_open, |
| 6003 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 6004 | .ioctl = md_ioctl, |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 6005 | #ifdef CONFIG_COMPAT |
| 6006 | .compat_ioctl = md_compat_ioctl, |
| 6007 | #endif |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6008 | .getgeo = md_getgeo, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6009 | }; |
| 6010 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6011 | static int md_thread(void * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6012 | { |
| 6013 | mdk_thread_t *thread = arg; |
| 6014 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6015 | /* |
| 6016 | * md_thread is a 'system-thread', it's priority should be very |
| 6017 | * high. We avoid resource deadlocks individually in each |
| 6018 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 6019 | * the very same RT priority as kswapd, thus we will never get |
| 6020 | * into a priority inversion deadlock. |
| 6021 | * |
| 6022 | * we definitely have to have equal or higher priority than |
| 6023 | * bdflush, otherwise bdflush will deadlock if there are too |
| 6024 | * many dirty RAID5 blocks. |
| 6025 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6026 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 6027 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6028 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6029 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 6030 | /* We need to wait INTERRUPTIBLE so that |
| 6031 | * we don't add to the load-average. |
| 6032 | * That means we need to be sure no signals are |
| 6033 | * pending |
| 6034 | */ |
| 6035 | if (signal_pending(current)) |
| 6036 | flush_signals(current); |
| 6037 | |
| 6038 | wait_event_interruptible_timeout |
| 6039 | (thread->wqueue, |
| 6040 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 6041 | || kthread_should_stop(), |
| 6042 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6043 | |
NeilBrown | 589a594 | 2010-12-09 17:02:14 +1100 | [diff] [blame] | 6044 | if (test_and_clear_bit(THREAD_WAKEUP, &thread->flags)) |
| 6045 | thread->run(thread->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6046 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6047 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6048 | return 0; |
| 6049 | } |
| 6050 | |
| 6051 | void md_wakeup_thread(mdk_thread_t *thread) |
| 6052 | { |
| 6053 | if (thread) { |
| 6054 | dprintk("md: waking up MD thread %s.\n", thread->tsk->comm); |
| 6055 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 6056 | wake_up(&thread->wqueue); |
| 6057 | } |
| 6058 | } |
| 6059 | |
| 6060 | mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev, |
| 6061 | const char *name) |
| 6062 | { |
| 6063 | mdk_thread_t *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6064 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 6065 | thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6066 | if (!thread) |
| 6067 | return NULL; |
| 6068 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6069 | init_waitqueue_head(&thread->wqueue); |
| 6070 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6071 | thread->run = run; |
| 6072 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6073 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 6074 | thread->tsk = kthread_run(md_thread, thread, |
| 6075 | "%s_%s", |
| 6076 | mdname(thread->mddev), |
| 6077 | name ?: mddev->pers->name); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6078 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6079 | kfree(thread); |
| 6080 | return NULL; |
| 6081 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6082 | return thread; |
| 6083 | } |
| 6084 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6085 | void md_unregister_thread(mdk_thread_t *thread) |
| 6086 | { |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6087 | if (!thread) |
| 6088 | return; |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 6089 | dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6090 | |
| 6091 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6092 | kfree(thread); |
| 6093 | } |
| 6094 | |
| 6095 | void md_error(mddev_t *mddev, mdk_rdev_t *rdev) |
| 6096 | { |
| 6097 | if (!mddev) { |
| 6098 | MD_BUG(); |
| 6099 | return; |
| 6100 | } |
| 6101 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6102 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6103 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6104 | |
| 6105 | if (mddev->external) |
| 6106 | set_bit(Blocked, &rdev->flags); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6107 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6108 | dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n", |
| 6109 | mdname(mddev), |
| 6110 | MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev), |
| 6111 | __builtin_return_address(0),__builtin_return_address(1), |
| 6112 | __builtin_return_address(2),__builtin_return_address(3)); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6113 | */ |
Andrew Morton | d0a0a5e | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 6114 | if (!mddev->pers) |
| 6115 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6116 | if (!mddev->pers->error_handler) |
| 6117 | return; |
| 6118 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6119 | if (mddev->degraded) |
| 6120 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6121 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6122 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6123 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6124 | md_wakeup_thread(mddev->thread); |
NeilBrown | 768a418 | 2010-07-26 11:49:55 +1000 | [diff] [blame] | 6125 | if (mddev->event_work.func) |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 6126 | queue_work(md_misc_wq, &mddev->event_work); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 6127 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6128 | } |
| 6129 | |
| 6130 | /* seq_file implementation /proc/mdstat */ |
| 6131 | |
| 6132 | static void status_unused(struct seq_file *seq) |
| 6133 | { |
| 6134 | int i = 0; |
| 6135 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6136 | |
| 6137 | seq_printf(seq, "unused devices: "); |
| 6138 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6139 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6140 | char b[BDEVNAME_SIZE]; |
| 6141 | i++; |
| 6142 | seq_printf(seq, "%s ", |
| 6143 | bdevname(rdev->bdev,b)); |
| 6144 | } |
| 6145 | if (!i) |
| 6146 | seq_printf(seq, "<none>"); |
| 6147 | |
| 6148 | seq_printf(seq, "\n"); |
| 6149 | } |
| 6150 | |
| 6151 | |
| 6152 | static void status_resync(struct seq_file *seq, mddev_t * mddev) |
| 6153 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6154 | sector_t max_sectors, resync, res; |
| 6155 | unsigned long dt, db; |
| 6156 | sector_t rt; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6157 | int scale; |
| 6158 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6159 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6160 | resync = mddev->curr_resync - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6161 | |
| 6162 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6163 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6164 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6165 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6166 | |
| 6167 | /* |
| 6168 | * Should not happen. |
| 6169 | */ |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6170 | if (!max_sectors) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6171 | MD_BUG(); |
| 6172 | return; |
| 6173 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6174 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6175 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6176 | * u32, as those are the requirements for sector_div. |
| 6177 | * Thus 'scale' must be at least 10 |
| 6178 | */ |
| 6179 | scale = 10; |
| 6180 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6181 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6182 | scale++; |
| 6183 | } |
| 6184 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6185 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6186 | |
| 6187 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6188 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6189 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6190 | seq_printf(seq, "["); |
| 6191 | for (i = 0; i < x; i++) |
| 6192 | seq_printf(seq, "="); |
| 6193 | seq_printf(seq, ">"); |
| 6194 | for (i = 0; i < y; i++) |
| 6195 | seq_printf(seq, "."); |
| 6196 | seq_printf(seq, "] "); |
| 6197 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6198 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6199 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 6200 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6201 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 6202 | "check" : |
| 6203 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 6204 | "resync" : "recovery"))), |
| 6205 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6206 | (unsigned long long) resync/2, |
| 6207 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6208 | |
| 6209 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6210 | * dt: time from mark until now |
| 6211 | * db: blocks written from mark until now |
| 6212 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6213 | * |
| 6214 | * rt is a sector_t, so could be 32bit or 64bit. |
| 6215 | * So we divide before multiply in case it is 32bit and close |
| 6216 | * to the limit. |
| 6217 | * We scale the divisor (db) by 32 to avoid loosing precision |
| 6218 | * near the end of resync when the number of remaining sectors |
| 6219 | * is close to 'db'. |
| 6220 | * We then divide rt by 32 after multiplying by db to compensate. |
| 6221 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6222 | */ |
| 6223 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 6224 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6225 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 6226 | - mddev->resync_mark_cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6227 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6228 | rt = max_sectors - resync; /* number of remaining sectors */ |
| 6229 | sector_div(rt, db/32+1); |
| 6230 | rt *= dt; |
| 6231 | rt >>= 5; |
| 6232 | |
| 6233 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 6234 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6235 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6236 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6237 | } |
| 6238 | |
| 6239 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 6240 | { |
| 6241 | struct list_head *tmp; |
| 6242 | loff_t l = *pos; |
| 6243 | mddev_t *mddev; |
| 6244 | |
| 6245 | if (l >= 0x10000) |
| 6246 | return NULL; |
| 6247 | if (!l--) |
| 6248 | /* header */ |
| 6249 | return (void*)1; |
| 6250 | |
| 6251 | spin_lock(&all_mddevs_lock); |
| 6252 | list_for_each(tmp,&all_mddevs) |
| 6253 | if (!l--) { |
| 6254 | mddev = list_entry(tmp, mddev_t, all_mddevs); |
| 6255 | mddev_get(mddev); |
| 6256 | spin_unlock(&all_mddevs_lock); |
| 6257 | return mddev; |
| 6258 | } |
| 6259 | spin_unlock(&all_mddevs_lock); |
| 6260 | if (!l--) |
| 6261 | return (void*)2;/* tail */ |
| 6262 | return NULL; |
| 6263 | } |
| 6264 | |
| 6265 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 6266 | { |
| 6267 | struct list_head *tmp; |
| 6268 | mddev_t *next_mddev, *mddev = v; |
| 6269 | |
| 6270 | ++*pos; |
| 6271 | if (v == (void*)2) |
| 6272 | return NULL; |
| 6273 | |
| 6274 | spin_lock(&all_mddevs_lock); |
| 6275 | if (v == (void*)1) |
| 6276 | tmp = all_mddevs.next; |
| 6277 | else |
| 6278 | tmp = mddev->all_mddevs.next; |
| 6279 | if (tmp != &all_mddevs) |
| 6280 | next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs)); |
| 6281 | else { |
| 6282 | next_mddev = (void*)2; |
| 6283 | *pos = 0x10000; |
| 6284 | } |
| 6285 | spin_unlock(&all_mddevs_lock); |
| 6286 | |
| 6287 | if (v != (void*)1) |
| 6288 | mddev_put(mddev); |
| 6289 | return next_mddev; |
| 6290 | |
| 6291 | } |
| 6292 | |
| 6293 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 6294 | { |
| 6295 | mddev_t *mddev = v; |
| 6296 | |
| 6297 | if (mddev && v != (void*)1 && v != (void*)2) |
| 6298 | mddev_put(mddev); |
| 6299 | } |
| 6300 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6301 | struct mdstat_info { |
| 6302 | int event; |
| 6303 | }; |
| 6304 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6305 | static int md_seq_show(struct seq_file *seq, void *v) |
| 6306 | { |
| 6307 | mddev_t *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6308 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6309 | mdk_rdev_t *rdev; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6310 | struct mdstat_info *mi = seq->private; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6311 | struct bitmap *bitmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6312 | |
| 6313 | if (v == (void*)1) { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6314 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6315 | seq_printf(seq, "Personalities : "); |
| 6316 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6317 | list_for_each_entry(pers, &pers_list, list) |
| 6318 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6319 | |
| 6320 | spin_unlock(&pers_lock); |
| 6321 | seq_printf(seq, "\n"); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6322 | mi->event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6323 | return 0; |
| 6324 | } |
| 6325 | if (v == (void*)2) { |
| 6326 | status_unused(seq); |
| 6327 | return 0; |
| 6328 | } |
| 6329 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6330 | if (mddev_lock(mddev) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6331 | return -EINTR; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6332 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6333 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 6334 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 6335 | mddev->pers ? "" : "in"); |
| 6336 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6337 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6338 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6339 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 6340 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6341 | seq_printf(seq, " %s", mddev->pers->name); |
| 6342 | } |
| 6343 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6344 | sectors = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6345 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6346 | char b[BDEVNAME_SIZE]; |
| 6347 | seq_printf(seq, " %s[%d]", |
| 6348 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6349 | if (test_bit(WriteMostly, &rdev->flags)) |
| 6350 | seq_printf(seq, "(W)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6351 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6352 | seq_printf(seq, "(F)"); |
| 6353 | continue; |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6354 | } else if (rdev->raid_disk < 0) |
| 6355 | seq_printf(seq, "(S)"); /* spare */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6356 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6357 | } |
| 6358 | |
| 6359 | if (!list_empty(&mddev->disks)) { |
| 6360 | if (mddev->pers) |
| 6361 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 6362 | (unsigned long long) |
| 6363 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6364 | else |
| 6365 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6366 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6367 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6368 | if (mddev->persistent) { |
| 6369 | if (mddev->major_version != 0 || |
| 6370 | mddev->minor_version != 90) { |
| 6371 | seq_printf(seq," super %d.%d", |
| 6372 | mddev->major_version, |
| 6373 | mddev->minor_version); |
| 6374 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6375 | } else if (mddev->external) |
| 6376 | seq_printf(seq, " super external:%s", |
| 6377 | mddev->metadata_type); |
| 6378 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6379 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6380 | |
| 6381 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6382 | mddev->pers->status(seq, mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6383 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6384 | if (mddev->pers->sync_request) { |
| 6385 | if (mddev->curr_resync > 2) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6386 | status_resync(seq, mddev); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6387 | seq_printf(seq, "\n "); |
| 6388 | } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2) |
| 6389 | seq_printf(seq, "\tresync=DELAYED\n "); |
| 6390 | else if (mddev->recovery_cp < MaxSector) |
| 6391 | seq_printf(seq, "\tresync=PENDING\n "); |
| 6392 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6393 | } else |
| 6394 | seq_printf(seq, "\n "); |
| 6395 | |
| 6396 | if ((bitmap = mddev->bitmap)) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6397 | unsigned long chunk_kb; |
| 6398 | unsigned long flags; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6399 | spin_lock_irqsave(&bitmap->lock, flags); |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 6400 | chunk_kb = mddev->bitmap_info.chunksize >> 10; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6401 | seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], " |
| 6402 | "%lu%s chunk", |
| 6403 | bitmap->pages - bitmap->missing_pages, |
| 6404 | bitmap->pages, |
| 6405 | (bitmap->pages - bitmap->missing_pages) |
| 6406 | << (PAGE_SHIFT - 10), |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 6407 | chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize, |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6408 | chunk_kb ? "KB" : "B"); |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 6409 | if (bitmap->file) { |
| 6410 | seq_printf(seq, ", file: "); |
Jan Blunck | c32c2f6 | 2008-02-14 19:38:43 -0800 | [diff] [blame] | 6411 | seq_path(seq, &bitmap->file->f_path, " \t\n"); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6412 | } |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 6413 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6414 | seq_printf(seq, "\n"); |
| 6415 | spin_unlock_irqrestore(&bitmap->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6416 | } |
| 6417 | |
| 6418 | seq_printf(seq, "\n"); |
| 6419 | } |
| 6420 | mddev_unlock(mddev); |
| 6421 | |
| 6422 | return 0; |
| 6423 | } |
| 6424 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 6425 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6426 | .start = md_seq_start, |
| 6427 | .next = md_seq_next, |
| 6428 | .stop = md_seq_stop, |
| 6429 | .show = md_seq_show, |
| 6430 | }; |
| 6431 | |
| 6432 | static int md_seq_open(struct inode *inode, struct file *file) |
| 6433 | { |
| 6434 | int error; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6435 | struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL); |
| 6436 | if (mi == NULL) |
| 6437 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6438 | |
| 6439 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6440 | if (error) |
| 6441 | kfree(mi); |
| 6442 | else { |
| 6443 | struct seq_file *p = file->private_data; |
| 6444 | p->private = mi; |
| 6445 | mi->event = atomic_read(&md_event_count); |
| 6446 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6447 | return error; |
| 6448 | } |
| 6449 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6450 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 6451 | { |
| 6452 | struct seq_file *m = filp->private_data; |
| 6453 | struct mdstat_info *mi = m->private; |
| 6454 | int mask; |
| 6455 | |
| 6456 | poll_wait(filp, &md_event_waiters, wait); |
| 6457 | |
| 6458 | /* always allow read */ |
| 6459 | mask = POLLIN | POLLRDNORM; |
| 6460 | |
| 6461 | if (mi->event != atomic_read(&md_event_count)) |
| 6462 | mask |= POLLERR | POLLPRI; |
| 6463 | return mask; |
| 6464 | } |
| 6465 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 6466 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 6467 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6468 | .open = md_seq_open, |
| 6469 | .read = seq_read, |
| 6470 | .llseek = seq_lseek, |
Martin Peschke | c3f94b4 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 6471 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6472 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6473 | }; |
| 6474 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6475 | int register_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6476 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6477 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6478 | list_add_tail(&p->list, &pers_list); |
| 6479 | 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] | 6480 | spin_unlock(&pers_lock); |
| 6481 | return 0; |
| 6482 | } |
| 6483 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6484 | int unregister_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6485 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6486 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6487 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6488 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6489 | spin_unlock(&pers_lock); |
| 6490 | return 0; |
| 6491 | } |
| 6492 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6493 | static int is_mddev_idle(mddev_t *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6494 | { |
| 6495 | mdk_rdev_t * rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6496 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6497 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6498 | |
| 6499 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6500 | rcu_read_lock(); |
| 6501 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6502 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6503 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
| 6504 | (int)part_stat_read(&disk->part0, sectors[1]) - |
| 6505 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 6506 | /* sync IO will cause sync_io to increase before the disk_stats |
| 6507 | * as sync_io is counted when a request starts, and |
| 6508 | * disk_stats is counted when it completes. |
| 6509 | * So resync activity will cause curr_events to be smaller than |
| 6510 | * when there was no such activity. |
| 6511 | * non-sync IO will cause disk_stat to increase without |
| 6512 | * increasing sync_io so curr_events will (eventually) |
| 6513 | * be larger than it was before. Once it becomes |
| 6514 | * substantially larger, the test below will cause |
| 6515 | * the array to appear non-idle, and resync will slow |
| 6516 | * down. |
| 6517 | * If there is a lot of outstanding resync activity when |
| 6518 | * we set last_event to curr_events, then all that activity |
| 6519 | * completing might cause the array to appear non-idle |
| 6520 | * and resync will be slowed down even though there might |
| 6521 | * not have been non-resync activity. This will only |
| 6522 | * happen once though. 'last_events' will soon reflect |
| 6523 | * the state where there is little or no outstanding |
| 6524 | * resync requests, and further resync activity will |
| 6525 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6526 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6527 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6528 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6529 | rdev->last_events = curr_events; |
| 6530 | idle = 0; |
| 6531 | } |
| 6532 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6533 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6534 | return idle; |
| 6535 | } |
| 6536 | |
| 6537 | void md_done_sync(mddev_t *mddev, int blocks, int ok) |
| 6538 | { |
| 6539 | /* another "blocks" (512byte) blocks have been synced */ |
| 6540 | atomic_sub(blocks, &mddev->recovery_active); |
| 6541 | wake_up(&mddev->recovery_wait); |
| 6542 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6543 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6544 | md_wakeup_thread(mddev->thread); |
| 6545 | // stop recovery, signal do_sync .... |
| 6546 | } |
| 6547 | } |
| 6548 | |
| 6549 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6550 | /* md_write_start(mddev, bi) |
| 6551 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6552 | * in superblock) before writing, schedule a superblock update |
| 6553 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6554 | */ |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6555 | void md_write_start(mddev_t *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6556 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6557 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6558 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6559 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6560 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6561 | BUG_ON(mddev->ro == 1); |
| 6562 | if (mddev->ro == 2) { |
| 6563 | /* need to switch to read/write */ |
| 6564 | mddev->ro = 0; |
| 6565 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6566 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 6567 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6568 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6569 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6570 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6571 | if (mddev->safemode == 1) |
| 6572 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6573 | if (mddev->in_sync) { |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6574 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6575 | if (mddev->in_sync) { |
| 6576 | mddev->in_sync = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6577 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 6578 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6579 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6580 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6581 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6582 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6583 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6584 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6585 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 6586 | wait_event(mddev->sb_wait, |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 6587 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6588 | } |
| 6589 | |
| 6590 | void md_write_end(mddev_t *mddev) |
| 6591 | { |
| 6592 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 6593 | if (mddev->safemode == 2) |
| 6594 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 6595 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6596 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 6597 | } |
| 6598 | } |
| 6599 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6600 | /* md_allow_write(mddev) |
| 6601 | * Calling this ensures that the array is marked 'active' so that writes |
| 6602 | * may proceed without blocking. It is important to call this before |
| 6603 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 6604 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6605 | * |
| 6606 | * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock |
| 6607 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6608 | */ |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6609 | int md_allow_write(mddev_t *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6610 | { |
| 6611 | if (!mddev->pers) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6612 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6613 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6614 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 6615 | if (!mddev->pers->sync_request) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6616 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6617 | |
| 6618 | spin_lock_irq(&mddev->write_lock); |
| 6619 | if (mddev->in_sync) { |
| 6620 | mddev->in_sync = 0; |
| 6621 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 6622 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6623 | if (mddev->safemode_delay && |
| 6624 | mddev->safemode == 0) |
| 6625 | mddev->safemode = 1; |
| 6626 | spin_unlock_irq(&mddev->write_lock); |
| 6627 | md_update_sb(mddev, 0); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6628 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6629 | } else |
| 6630 | spin_unlock_irq(&mddev->write_lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6631 | |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 6632 | if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6633 | return -EAGAIN; |
| 6634 | else |
| 6635 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6636 | } |
| 6637 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 6638 | |
NeilBrown | b63d7c2 | 2010-06-01 19:37:33 +1000 | [diff] [blame] | 6639 | void md_unplug(mddev_t *mddev) |
NeilBrown | 252ac52 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 6640 | { |
| 6641 | if (mddev->queue) |
| 6642 | blk_unplug(mddev->queue); |
| 6643 | if (mddev->plug) |
| 6644 | mddev->plug->unplug_fn(mddev->plug); |
| 6645 | } |
| 6646 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6647 | #define SYNC_MARKS 10 |
| 6648 | #define SYNC_MARK_STEP (3*HZ) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6649 | void md_do_sync(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6650 | { |
| 6651 | mddev_t *mddev2; |
| 6652 | unsigned int currspeed = 0, |
| 6653 | window; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6654 | sector_t max_sectors,j, io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6655 | unsigned long mark[SYNC_MARKS]; |
| 6656 | sector_t mark_cnt[SYNC_MARKS]; |
| 6657 | int last_mark,m; |
| 6658 | struct list_head *tmp; |
| 6659 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6660 | int skipped = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6661 | mdk_rdev_t *rdev; |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6662 | char *desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6663 | |
| 6664 | /* just incase thread restarts... */ |
| 6665 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 6666 | return; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6667 | if (mddev->ro) /* never try to sync a read-only array */ |
| 6668 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6669 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6670 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6671 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 6672 | desc = "data-check"; |
| 6673 | else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6674 | desc = "requested-resync"; |
| 6675 | else |
| 6676 | desc = "resync"; |
| 6677 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 6678 | desc = "reshape"; |
| 6679 | else |
| 6680 | desc = "recovery"; |
| 6681 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6682 | /* we overload curr_resync somewhat here. |
| 6683 | * 0 == not engaged in resync at all |
| 6684 | * 2 == checking that there is no conflict with another sync |
| 6685 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 6686 | * commense |
| 6687 | * other == active in resync - this many blocks |
| 6688 | * |
| 6689 | * Before starting a resync we must have set curr_resync to |
| 6690 | * 2, and then checked that every "conflicting" array has curr_resync |
| 6691 | * less than ours. When we find one that is the same or higher |
| 6692 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 6693 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 6694 | * This will mean we have to start checking from the beginning again. |
| 6695 | * |
| 6696 | */ |
| 6697 | |
| 6698 | do { |
| 6699 | mddev->curr_resync = 2; |
| 6700 | |
| 6701 | try_again: |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 6702 | if (kthread_should_stop()) |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 6703 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 6704 | |
| 6705 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6706 | goto skip; |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6707 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6708 | if (mddev2 == mddev) |
| 6709 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 6710 | if (!mddev->parallel_resync |
| 6711 | && mddev2->curr_resync |
| 6712 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6713 | DEFINE_WAIT(wq); |
| 6714 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 6715 | /* arbitrarily yield */ |
| 6716 | mddev->curr_resync = 1; |
| 6717 | wake_up(&resync_wait); |
| 6718 | } |
| 6719 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 6720 | /* no need to wait here, we can wait the next |
| 6721 | * time 'round when curr_resync == 2 |
| 6722 | */ |
| 6723 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 6724 | /* We need to wait 'interruptible' so as not to |
| 6725 | * contribute to the load average, and not to |
| 6726 | * be caught by 'softlockup' |
| 6727 | */ |
| 6728 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 6729 | if (!kthread_should_stop() && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 6730 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6731 | printk(KERN_INFO "md: delaying %s of %s" |
| 6732 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6733 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6734 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6735 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 6736 | if (signal_pending(current)) |
| 6737 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6738 | schedule(); |
| 6739 | finish_wait(&resync_wait, &wq); |
| 6740 | goto try_again; |
| 6741 | } |
| 6742 | finish_wait(&resync_wait, &wq); |
| 6743 | } |
| 6744 | } |
| 6745 | } while (mddev->curr_resync < 2); |
| 6746 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6747 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6748 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6749 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6750 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6751 | */ |
| 6752 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6753 | mddev->resync_mismatches = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6754 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6755 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6756 | j = mddev->resync_min; |
| 6757 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6758 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6759 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6760 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6761 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6762 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6763 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6764 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6765 | j = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6766 | rcu_read_lock(); |
| 6767 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6768 | if (rdev->raid_disk >= 0 && |
| 6769 | !test_bit(Faulty, &rdev->flags) && |
| 6770 | !test_bit(In_sync, &rdev->flags) && |
| 6771 | rdev->recovery_offset < j) |
| 6772 | j = rdev->recovery_offset; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6773 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6774 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6775 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6776 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 6777 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 6778 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 6779 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6780 | "(but not more than %d KB/sec) for %s.\n", |
| 6781 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6782 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6783 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6784 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6785 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6786 | for (m = 0; m < SYNC_MARKS; m++) { |
| 6787 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6788 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6789 | } |
| 6790 | last_mark = 0; |
| 6791 | mddev->resync_mark = mark[last_mark]; |
| 6792 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 6793 | |
| 6794 | /* |
| 6795 | * Tune reconstruction: |
| 6796 | */ |
| 6797 | window = 32*(PAGE_SIZE/512); |
| 6798 | printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n", |
| 6799 | window/2,(unsigned long long) max_sectors/2); |
| 6800 | |
| 6801 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6802 | last_check = 0; |
| 6803 | |
| 6804 | if (j>2) { |
| 6805 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6806 | "md: resuming %s of %s from checkpoint.\n", |
| 6807 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6808 | mddev->curr_resync = j; |
| 6809 | } |
NeilBrown | efa5933 | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 6810 | mddev->curr_resync_completed = mddev->curr_resync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6811 | |
| 6812 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6813 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6814 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6815 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6816 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 6817 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 6818 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 6819 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 6820 | > (max_sectors >> 4)) || |
| 6821 | (j - mddev->curr_resync_completed)*2 |
| 6822 | >= mddev->resync_max - mddev->curr_resync_completed |
| 6823 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6824 | /* time to update curr_resync_completed */ |
NeilBrown | 252ac52 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 6825 | md_unplug(mddev); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6826 | wait_event(mddev->recovery_wait, |
| 6827 | atomic_read(&mddev->recovery_active) == 0); |
| 6828 | mddev->curr_resync_completed = |
| 6829 | mddev->curr_resync; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 6830 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6831 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6832 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6833 | |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 6834 | while (j >= mddev->resync_max && !kthread_should_stop()) { |
| 6835 | /* As this condition is controlled by user-space, |
| 6836 | * we can block indefinitely, so use '_interruptible' |
| 6837 | * to avoid triggering warnings. |
| 6838 | */ |
| 6839 | flush_signals(current); /* just in case */ |
| 6840 | wait_event_interruptible(mddev->recovery_wait, |
| 6841 | mddev->resync_max > j |
| 6842 | || kthread_should_stop()); |
| 6843 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6844 | |
| 6845 | if (kthread_should_stop()) |
| 6846 | goto interrupted; |
| 6847 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6848 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6849 | currspeed < speed_min(mddev)); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6850 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6851 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6852 | goto out; |
| 6853 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6854 | |
| 6855 | if (!skipped) { /* actual IO requested */ |
| 6856 | io_sectors += sectors; |
| 6857 | atomic_add(sectors, &mddev->recovery_active); |
| 6858 | } |
| 6859 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6860 | j += sectors; |
| 6861 | if (j>1) mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6862 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6863 | if (last_check == 0) |
| 6864 | /* this is the earliers that rebuilt will be |
| 6865 | * visible in /proc/mdstat |
| 6866 | */ |
| 6867 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6868 | |
| 6869 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6870 | continue; |
| 6871 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6872 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6873 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6874 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6875 | break; |
| 6876 | |
| 6877 | repeat: |
| 6878 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 6879 | /* step marks */ |
| 6880 | int next = (last_mark+1) % SYNC_MARKS; |
| 6881 | |
| 6882 | mddev->resync_mark = mark[next]; |
| 6883 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 6884 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6885 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6886 | last_mark = next; |
| 6887 | } |
| 6888 | |
| 6889 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6890 | if (kthread_should_stop()) |
| 6891 | goto interrupted; |
| 6892 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6893 | |
| 6894 | /* |
| 6895 | * this loop exits only if either when we are slower than |
| 6896 | * the 'hard' speed limit, or the system was IO-idle for |
| 6897 | * a jiffy. |
| 6898 | * the system might be non-idle CPU-wise, but we only care |
| 6899 | * about not overloading the IO subsystem. (things like an |
| 6900 | * e2fsck being done on the RAID array should execute fast) |
| 6901 | */ |
NeilBrown | 252ac52 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 6902 | md_unplug(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6903 | cond_resched(); |
| 6904 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6905 | currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2 |
| 6906 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6907 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 6908 | if (currspeed > speed_min(mddev)) { |
| 6909 | if ((currspeed > speed_max(mddev)) || |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6910 | !is_mddev_idle(mddev, 0)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6911 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6912 | goto repeat; |
| 6913 | } |
| 6914 | } |
| 6915 | } |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6916 | printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6917 | /* |
| 6918 | * this also signals 'finished resyncing' to md_stop |
| 6919 | */ |
| 6920 | out: |
NeilBrown | 252ac52 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 6921 | md_unplug(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6922 | |
| 6923 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 6924 | |
| 6925 | /* tell personality that we are finished */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6926 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6927 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6928 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6929 | mddev->curr_resync > 2) { |
| 6930 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6931 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 6932 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 6933 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6934 | "md: checkpointing %s of %s.\n", |
| 6935 | desc, mdname(mddev)); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6936 | mddev->recovery_cp = mddev->curr_resync; |
| 6937 | } |
| 6938 | } else |
| 6939 | mddev->recovery_cp = MaxSector; |
| 6940 | } else { |
| 6941 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 6942 | mddev->curr_resync = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6943 | rcu_read_lock(); |
| 6944 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6945 | if (rdev->raid_disk >= 0 && |
NeilBrown | 70fffd0 | 2010-06-16 17:01:25 +1000 | [diff] [blame] | 6946 | mddev->delta_disks >= 0 && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6947 | !test_bit(Faulty, &rdev->flags) && |
| 6948 | !test_bit(In_sync, &rdev->flags) && |
| 6949 | rdev->recovery_offset < mddev->curr_resync) |
| 6950 | rdev->recovery_offset = mddev->curr_resync; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6951 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6952 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6953 | } |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 6954 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6955 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6956 | skip: |
NeilBrown | c07b70a | 2009-12-14 12:49:48 +1100 | [diff] [blame] | 6957 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 6958 | /* We completed so min/max setting can be forgotten if used. */ |
| 6959 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6960 | mddev->resync_min = 0; |
| 6961 | mddev->resync_max = MaxSector; |
| 6962 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6963 | mddev->resync_min = mddev->curr_resync_completed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6964 | mddev->curr_resync = 0; |
NeilBrown | efa5933 | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 6965 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 6966 | mddev->curr_resync_completed = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6967 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6968 | wake_up(&resync_wait); |
| 6969 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
| 6970 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6971 | return; |
| 6972 | |
| 6973 | interrupted: |
| 6974 | /* |
| 6975 | * got a signal, exit. |
| 6976 | */ |
| 6977 | printk(KERN_INFO |
| 6978 | "md: md_do_sync() got signal ... exiting\n"); |
| 6979 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6980 | goto out; |
| 6981 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6982 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6983 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6984 | |
| 6985 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6986 | static int remove_and_add_spares(mddev_t *mddev) |
| 6987 | { |
| 6988 | mdk_rdev_t *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6989 | int spares = 0; |
| 6990 | |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6991 | mddev->curr_resync_completed = 0; |
| 6992 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6993 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6994 | if (rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6995 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6996 | (test_bit(Faulty, &rdev->flags) || |
| 6997 | ! test_bit(In_sync, &rdev->flags)) && |
| 6998 | atomic_read(&rdev->nr_pending)==0) { |
| 6999 | if (mddev->pers->hot_remove_disk( |
| 7000 | mddev, rdev->raid_disk)==0) { |
| 7001 | char nm[20]; |
| 7002 | sprintf(nm,"rd%d", rdev->raid_disk); |
| 7003 | sysfs_remove_link(&mddev->kobj, nm); |
| 7004 | rdev->raid_disk = -1; |
| 7005 | } |
| 7006 | } |
| 7007 | |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 7008 | if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 7009 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7010 | if (rdev->raid_disk >= 0 && |
Dan Williams | e542713 | 2008-07-28 23:28:06 -0700 | [diff] [blame] | 7011 | !test_bit(In_sync, &rdev->flags) && |
| 7012 | !test_bit(Blocked, &rdev->flags)) |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7013 | spares++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7014 | if (rdev->raid_disk < 0 |
| 7015 | && !test_bit(Faulty, &rdev->flags)) { |
| 7016 | rdev->recovery_offset = 0; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 7017 | if (mddev->pers-> |
| 7018 | hot_add_disk(mddev, rdev) == 0) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7019 | char nm[20]; |
| 7020 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 7021 | if (sysfs_create_link(&mddev->kobj, |
| 7022 | &rdev->kobj, nm)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7023 | /* failure here is OK */; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7024 | spares++; |
| 7025 | md_new_event(mddev); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 7026 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7027 | } else |
| 7028 | break; |
| 7029 | } |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7030 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7031 | } |
| 7032 | return spares; |
| 7033 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7034 | /* |
| 7035 | * This routine is regularly called by all per-raid-array threads to |
| 7036 | * deal with generic issues like resync and super-block update. |
| 7037 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 7038 | * need this as they never do any recovery or update the superblock. |
| 7039 | * |
| 7040 | * It does not do any resync itself, but rather "forks" off other threads |
| 7041 | * to do that as needed. |
| 7042 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 7043 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7044 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7045 | * and wakeups up this thread which will reap the thread and finish up. |
| 7046 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 7047 | * |
| 7048 | * The overall approach is: |
| 7049 | * 1/ if the superblock needs updating, update it. |
| 7050 | * 2/ If a recovery thread is running, don't do anything else. |
| 7051 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 7052 | * 4/ If there are any faulty devices, remove them. |
| 7053 | * 5/ If array is degraded, try to add spares devices |
| 7054 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 7055 | */ |
| 7056 | void md_check_recovery(mddev_t *mddev) |
| 7057 | { |
| 7058 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7059 | |
| 7060 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 7061 | if (mddev->bitmap) |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 7062 | bitmap_daemon_work(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7063 | |
| 7064 | if (mddev->ro) |
| 7065 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7066 | |
| 7067 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7068 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7069 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 7070 | mdname(mddev)); |
| 7071 | mddev->safemode = 2; |
| 7072 | } |
| 7073 | flush_signals(current); |
| 7074 | } |
| 7075 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7076 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 7077 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7078 | if ( ! ( |
NeilBrown | 126925c | 2010-09-07 17:02:47 +1000 | [diff] [blame] | 7079 | (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7080 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7081 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7082 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7083 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 7084 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7085 | )) |
| 7086 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7087 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 7088 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7089 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7090 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7091 | if (mddev->ro) { |
| 7092 | /* Only thing we do on a ro array is remove |
| 7093 | * failed devices. |
| 7094 | */ |
| 7095 | remove_and_add_spares(mddev); |
| 7096 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7097 | goto unlock; |
| 7098 | } |
| 7099 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7100 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7101 | int did_change = 0; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7102 | spin_lock_irq(&mddev->write_lock); |
| 7103 | if (mddev->safemode && |
| 7104 | !atomic_read(&mddev->writes_pending) && |
| 7105 | !mddev->in_sync && |
| 7106 | mddev->recovery_cp == MaxSector) { |
| 7107 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7108 | did_change = 1; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7109 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7110 | } |
| 7111 | if (mddev->safemode == 1) |
| 7112 | mddev->safemode = 0; |
| 7113 | spin_unlock_irq(&mddev->write_lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7114 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7115 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7116 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7117 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7118 | if (mddev->flags) |
| 7119 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7120 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7121 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 7122 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 7123 | /* resync/recovery still happening */ |
| 7124 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7125 | goto unlock; |
| 7126 | } |
| 7127 | if (mddev->sync_thread) { |
| 7128 | /* resync has finished, collect result */ |
| 7129 | md_unregister_thread(mddev->sync_thread); |
| 7130 | mddev->sync_thread = NULL; |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 7131 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 7132 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7133 | /* success...*/ |
| 7134 | /* activate any spares */ |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 7135 | if (mddev->pers->spare_active(mddev)) |
| 7136 | sysfs_notify(&mddev->kobj, NULL, |
| 7137 | "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7138 | } |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 7139 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 7140 | mddev->pers->finish_reshape) |
| 7141 | mddev->pers->finish_reshape(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7142 | md_update_sb(mddev, 1); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 7143 | |
| 7144 | /* if array is no-longer degraded, then any saved_raid_disk |
| 7145 | * information must be scrapped |
| 7146 | */ |
| 7147 | if (!mddev->degraded) |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 7148 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 7149 | rdev->saved_raid_disk = -1; |
| 7150 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7151 | mddev->recovery = 0; |
| 7152 | /* flag recovery needed just to double check */ |
| 7153 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7154 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7155 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7156 | goto unlock; |
| 7157 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7158 | /* Set RUNNING before clearing NEEDED to avoid |
| 7159 | * any transients in the value of "sync_action". |
| 7160 | */ |
| 7161 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7162 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7163 | /* Clear some bits that don't mean anything, but |
| 7164 | * might be left set |
| 7165 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7166 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7167 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7168 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7169 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 7170 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7171 | /* no recovery is running. |
| 7172 | * remove any failed drives, then |
| 7173 | * add spares if possible. |
| 7174 | * Spare are also removed and re-added, to allow |
| 7175 | * the personality to fail the re-add. |
| 7176 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7177 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7178 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 7179 | if (mddev->pers->check_reshape == NULL || |
| 7180 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7181 | /* Cannot proceed */ |
| 7182 | goto unlock; |
| 7183 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7184 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7185 | } else if ((spares = remove_and_add_spares(mddev))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7186 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7187 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 7188 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7189 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7190 | } else if (mddev->recovery_cp < MaxSector) { |
| 7191 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7192 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7193 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 7194 | /* nothing to be done ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7195 | goto unlock; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7196 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7197 | if (mddev->pers->sync_request) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 7198 | if (spares && mddev->bitmap && ! mddev->bitmap->file) { |
| 7199 | /* We are adding a device or devices to an array |
| 7200 | * which has the bitmap stored on all devices. |
| 7201 | * So make sure all bitmap pages get written |
| 7202 | */ |
| 7203 | bitmap_write_all(mddev->bitmap); |
| 7204 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7205 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 7206 | mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 7207 | "resync"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7208 | if (!mddev->sync_thread) { |
| 7209 | printk(KERN_ERR "%s: could not start resync" |
| 7210 | " thread...\n", |
| 7211 | mdname(mddev)); |
| 7212 | /* leave the spares where they are, it shouldn't hurt */ |
| 7213 | mddev->recovery = 0; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7214 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7215 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7216 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7217 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7218 | } |
| 7219 | unlock: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7220 | if (!mddev->sync_thread) { |
| 7221 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7222 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 7223 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 7224 | if (mddev->sysfs_action) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7225 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7226 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7227 | mddev_unlock(mddev); |
| 7228 | } |
| 7229 | } |
| 7230 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7231 | void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 7232 | { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7233 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7234 | wait_event_timeout(rdev->blocked_wait, |
| 7235 | !test_bit(Blocked, &rdev->flags), |
| 7236 | msecs_to_jiffies(5000)); |
| 7237 | rdev_dec_pending(rdev, mddev); |
| 7238 | } |
| 7239 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 7240 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7241 | static int md_notify_reboot(struct notifier_block *this, |
| 7242 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7243 | { |
| 7244 | struct list_head *tmp; |
| 7245 | mddev_t *mddev; |
| 7246 | |
| 7247 | if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) { |
| 7248 | |
| 7249 | printk(KERN_INFO "md: stopping all md devices.\n"); |
| 7250 | |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7251 | for_each_mddev(mddev, tmp) |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 7252 | if (mddev_trylock(mddev)) { |
NeilBrown | 2b25000 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7253 | /* Force a switch to readonly even array |
| 7254 | * appears to still be in use. Hence |
| 7255 | * the '100'. |
| 7256 | */ |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 7257 | md_set_readonly(mddev, 100); |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 7258 | mddev_unlock(mddev); |
| 7259 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7260 | /* |
| 7261 | * certain more exotic SCSI devices are known to be |
| 7262 | * volatile wrt too early system reboots. While the |
| 7263 | * right place to handle this issue is the given |
| 7264 | * driver, we do want to have a safe RAID driver ... |
| 7265 | */ |
| 7266 | mdelay(1000*1); |
| 7267 | } |
| 7268 | return NOTIFY_DONE; |
| 7269 | } |
| 7270 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7271 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7272 | .notifier_call = md_notify_reboot, |
| 7273 | .next = NULL, |
| 7274 | .priority = INT_MAX, /* before any real devices */ |
| 7275 | }; |
| 7276 | |
| 7277 | static void md_geninit(void) |
| 7278 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7279 | dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t)); |
| 7280 | |
Denis V. Lunev | c7705f3 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 7281 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7282 | } |
| 7283 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7284 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7285 | { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 7286 | int ret = -ENOMEM; |
| 7287 | |
| 7288 | md_wq = alloc_workqueue("md", WQ_RESCUER, 0); |
| 7289 | if (!md_wq) |
| 7290 | goto err_wq; |
| 7291 | |
| 7292 | md_misc_wq = alloc_workqueue("md_misc", 0, 0); |
| 7293 | if (!md_misc_wq) |
| 7294 | goto err_misc_wq; |
| 7295 | |
| 7296 | if ((ret = register_blkdev(MD_MAJOR, "md")) < 0) |
| 7297 | goto err_md; |
| 7298 | |
| 7299 | if ((ret = register_blkdev(0, "mdp")) < 0) |
| 7300 | goto err_mdp; |
| 7301 | mdp_major = ret; |
| 7302 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7303 | blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE, |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 7304 | md_probe, NULL, NULL); |
| 7305 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7306 | md_probe, NULL, NULL); |
| 7307 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7308 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 7309 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7310 | |
| 7311 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 7312 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7313 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 7314 | err_mdp: |
| 7315 | unregister_blkdev(MD_MAJOR, "md"); |
| 7316 | err_md: |
| 7317 | destroy_workqueue(md_misc_wq); |
| 7318 | err_misc_wq: |
| 7319 | destroy_workqueue(md_wq); |
| 7320 | err_wq: |
| 7321 | return ret; |
| 7322 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7323 | |
| 7324 | #ifndef MODULE |
| 7325 | |
| 7326 | /* |
| 7327 | * Searches all registered partitions for autorun RAID arrays |
| 7328 | * at boot time. |
| 7329 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7330 | |
| 7331 | static LIST_HEAD(all_detected_devices); |
| 7332 | struct detected_devices_node { |
| 7333 | struct list_head list; |
| 7334 | dev_t dev; |
| 7335 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7336 | |
| 7337 | void md_autodetect_dev(dev_t dev) |
| 7338 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7339 | struct detected_devices_node *node_detected_dev; |
| 7340 | |
| 7341 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 7342 | if (node_detected_dev) { |
| 7343 | node_detected_dev->dev = dev; |
| 7344 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 7345 | } else { |
| 7346 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 7347 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 7348 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7349 | } |
| 7350 | |
| 7351 | |
| 7352 | static void autostart_arrays(int part) |
| 7353 | { |
| 7354 | mdk_rdev_t *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7355 | struct detected_devices_node *node_detected_dev; |
| 7356 | dev_t dev; |
| 7357 | int i_scanned, i_passed; |
| 7358 | |
| 7359 | i_scanned = 0; |
| 7360 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7361 | |
| 7362 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 7363 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7364 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 7365 | i_scanned++; |
| 7366 | node_detected_dev = list_entry(all_detected_devices.next, |
| 7367 | struct detected_devices_node, list); |
| 7368 | list_del(&node_detected_dev->list); |
| 7369 | dev = node_detected_dev->dev; |
| 7370 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 7371 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7372 | if (IS_ERR(rdev)) |
| 7373 | continue; |
| 7374 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7375 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7376 | MD_BUG(); |
| 7377 | continue; |
| 7378 | } |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 7379 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7380 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7381 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7382 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7383 | |
| 7384 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 7385 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7386 | |
| 7387 | autorun_devices(part); |
| 7388 | } |
| 7389 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 7390 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7391 | |
| 7392 | static __exit void md_exit(void) |
| 7393 | { |
| 7394 | mddev_t *mddev; |
| 7395 | struct list_head *tmp; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 7396 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7397 | blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS); |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 7398 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7399 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7400 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7401 | unregister_blkdev(mdp_major, "mdp"); |
| 7402 | unregister_reboot_notifier(&md_notifier); |
| 7403 | unregister_sysctl_table(raid_table_header); |
| 7404 | remove_proc_entry("mdstat", NULL); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7405 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7406 | export_array(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7407 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7408 | } |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 7409 | destroy_workqueue(md_misc_wq); |
| 7410 | destroy_workqueue(md_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7411 | } |
| 7412 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 7413 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7414 | module_exit(md_exit) |
| 7415 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7416 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 7417 | { |
| 7418 | return sprintf(buffer, "%d", start_readonly); |
| 7419 | } |
| 7420 | static int set_ro(const char *val, struct kernel_param *kp) |
| 7421 | { |
| 7422 | char *e; |
| 7423 | int num = simple_strtoul(val, &e, 10); |
| 7424 | if (*val && (*e == '\0' || *e == '\n')) { |
| 7425 | start_readonly = num; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 7426 | return 0; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7427 | } |
| 7428 | return -EINVAL; |
| 7429 | } |
| 7430 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 7431 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 7432 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | 6ff8d8e | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 7433 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7434 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7435 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7436 | EXPORT_SYMBOL(register_md_personality); |
| 7437 | EXPORT_SYMBOL(unregister_md_personality); |
| 7438 | EXPORT_SYMBOL(md_error); |
| 7439 | EXPORT_SYMBOL(md_done_sync); |
| 7440 | EXPORT_SYMBOL(md_write_start); |
| 7441 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7442 | EXPORT_SYMBOL(md_register_thread); |
| 7443 | EXPORT_SYMBOL(md_unregister_thread); |
| 7444 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7445 | EXPORT_SYMBOL(md_check_recovery); |
| 7446 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 7447 | MODULE_DESCRIPTION("MD RAID framework"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 7448 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 7449 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |