Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | md.c : Multiple Devices driver for Linux |
| 3 | Copyright (C) 1998, 1999, 2000 Ingo Molnar |
| 4 | |
| 5 | completely rewritten, based on the MD driver code from Marc Zyngier |
| 6 | |
| 7 | Changes: |
| 8 | |
| 9 | - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar |
| 10 | - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com> |
| 11 | - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net> |
| 12 | - kerneld support by Boris Tobotras <boris@xtalk.msk.su> |
| 13 | - kmod support by: Cyrus Durgin |
| 14 | - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com> |
| 15 | - Devfs support by Richard Gooch <rgooch@atnf.csiro.au> |
| 16 | |
| 17 | - lots of fixes and improvements to the RAID1/RAID5 and generic |
| 18 | RAID code (such as request based resynchronization): |
| 19 | |
| 20 | Neil Brown <neilb@cse.unsw.edu.au>. |
| 21 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 22 | - persistent bitmap code |
| 23 | Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc. |
| 24 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | This program is free software; you can redistribute it and/or modify |
| 26 | it under the terms of the GNU General Public License as published by |
| 27 | the Free Software Foundation; either version 2, or (at your option) |
| 28 | any later version. |
| 29 | |
| 30 | You should have received a copy of the GNU General Public License |
| 31 | (for example /usr/src/linux/COPYING); if not, write to the Free |
| 32 | Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 33 | */ |
| 34 | |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 35 | #include <linux/kthread.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 36 | #include <linux/blkdev.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <linux/sysctl.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 38 | #include <linux/seq_file.h> |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 39 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include <linux/buffer_head.h> /* for invalidate_bdev */ |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 41 | #include <linux/poll.h> |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 42 | #include <linux/ctype.h> |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 43 | #include <linux/string.h> |
NeilBrown | fb4d8c7 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 44 | #include <linux/hdreg.h> |
| 45 | #include <linux/proc_fs.h> |
| 46 | #include <linux/random.h> |
| 47 | #include <linux/reboot.h> |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 48 | #include <linux/file.h> |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 49 | #include <linux/compat.h> |
Stephen Rothwell | 2557072 | 2008-10-15 09:09:21 +1100 | [diff] [blame] | 50 | #include <linux/delay.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 51 | #include <linux/raid/md_p.h> |
| 52 | #include <linux/raid/md_u.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 53 | #include <linux/slab.h> |
NeilBrown | 43b2e5d | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 54 | #include "md.h" |
Christoph Hellwig | ef740c3 | 2009-03-31 14:27:03 +1100 | [diff] [blame] | 55 | #include "bitmap.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
| 57 | #define DEBUG 0 |
| 58 | #define dprintk(x...) ((void)(DEBUG && printk(x))) |
| 59 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | #ifndef MODULE |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 61 | static void autostart_arrays(int part); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | #endif |
| 63 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 64 | static LIST_HEAD(pers_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | static DEFINE_SPINLOCK(pers_lock); |
| 66 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 67 | static void md_print_devices(void); |
| 68 | |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 69 | static DECLARE_WAIT_QUEUE_HEAD(resync_wait); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 70 | static struct workqueue_struct *md_wq; |
| 71 | static struct workqueue_struct *md_misc_wq; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 72 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 73 | #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); } |
| 74 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | /* |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 76 | * Default number of read corrections we'll attempt on an rdev |
| 77 | * before ejecting it from the array. We divide the read error |
| 78 | * count by 2 for every hour elapsed between read errors. |
| 79 | */ |
| 80 | #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20 |
| 81 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit' |
| 83 | * is 1000 KB/sec, so the extra system load does not show up that much. |
| 84 | * Increase it if you want to have more _guaranteed_ speed. Note that |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 85 | * the RAID driver will use the maximum available bandwidth if the IO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | * subsystem is idle. There is also an 'absolute maximum' reconstruction |
| 87 | * speed limit - in case reconstruction slows down your system despite |
| 88 | * idle IO detection. |
| 89 | * |
| 90 | * you can change it via /proc/sys/dev/raid/speed_limit_min and _max. |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 91 | * or /sys/block/mdX/md/sync_speed_{min,max} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | */ |
| 93 | |
| 94 | static int sysctl_speed_limit_min = 1000; |
| 95 | static int sysctl_speed_limit_max = 200000; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 96 | static inline int speed_min(mddev_t *mddev) |
| 97 | { |
| 98 | return mddev->sync_speed_min ? |
| 99 | mddev->sync_speed_min : sysctl_speed_limit_min; |
| 100 | } |
| 101 | |
| 102 | static inline int speed_max(mddev_t *mddev) |
| 103 | { |
| 104 | return mddev->sync_speed_max ? |
| 105 | mddev->sync_speed_max : sysctl_speed_limit_max; |
| 106 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | |
| 108 | static struct ctl_table_header *raid_table_header; |
| 109 | |
| 110 | static ctl_table raid_table[] = { |
| 111 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | .procname = "speed_limit_min", |
| 113 | .data = &sysctl_speed_limit_min, |
| 114 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 115 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 116 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | }, |
| 118 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | .procname = "speed_limit_max", |
| 120 | .data = &sysctl_speed_limit_max, |
| 121 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 122 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 123 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 125 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | static ctl_table raid_dir_table[] = { |
| 129 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | .procname = "raid", |
| 131 | .maxlen = 0, |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 132 | .mode = S_IRUGO|S_IXUGO, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | .child = raid_table, |
| 134 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 135 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | }; |
| 137 | |
| 138 | static ctl_table raid_root_table[] = { |
| 139 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | .procname = "dev", |
| 141 | .maxlen = 0, |
| 142 | .mode = 0555, |
| 143 | .child = raid_dir_table, |
| 144 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 145 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | }; |
| 147 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 148 | static const struct block_device_operations md_fops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 150 | static int start_readonly; |
| 151 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 152 | /* bio_clone_mddev |
| 153 | * like bio_clone, but with a local bio set |
| 154 | */ |
| 155 | |
| 156 | static void mddev_bio_destructor(struct bio *bio) |
| 157 | { |
| 158 | mddev_t *mddev, **mddevp; |
| 159 | |
| 160 | mddevp = (void*)bio; |
| 161 | mddev = mddevp[-1]; |
| 162 | |
| 163 | bio_free(bio, mddev->bio_set); |
| 164 | } |
| 165 | |
| 166 | struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs, |
| 167 | mddev_t *mddev) |
| 168 | { |
| 169 | struct bio *b; |
| 170 | mddev_t **mddevp; |
| 171 | |
| 172 | if (!mddev || !mddev->bio_set) |
| 173 | return bio_alloc(gfp_mask, nr_iovecs); |
| 174 | |
| 175 | b = bio_alloc_bioset(gfp_mask, nr_iovecs, |
| 176 | mddev->bio_set); |
| 177 | if (!b) |
| 178 | return NULL; |
| 179 | mddevp = (void*)b; |
| 180 | mddevp[-1] = mddev; |
| 181 | b->bi_destructor = mddev_bio_destructor; |
| 182 | return b; |
| 183 | } |
| 184 | EXPORT_SYMBOL_GPL(bio_alloc_mddev); |
| 185 | |
| 186 | struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask, |
| 187 | mddev_t *mddev) |
| 188 | { |
| 189 | struct bio *b; |
| 190 | mddev_t **mddevp; |
| 191 | |
| 192 | if (!mddev || !mddev->bio_set) |
| 193 | return bio_clone(bio, gfp_mask); |
| 194 | |
| 195 | b = bio_alloc_bioset(gfp_mask, bio->bi_max_vecs, |
| 196 | mddev->bio_set); |
| 197 | if (!b) |
| 198 | return NULL; |
| 199 | mddevp = (void*)b; |
| 200 | mddevp[-1] = mddev; |
| 201 | b->bi_destructor = mddev_bio_destructor; |
| 202 | __bio_clone(b, bio); |
| 203 | if (bio_integrity(bio)) { |
| 204 | int ret; |
| 205 | |
| 206 | ret = bio_integrity_clone(b, bio, gfp_mask, mddev->bio_set); |
| 207 | |
| 208 | if (ret < 0) { |
| 209 | bio_put(b); |
| 210 | return NULL; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | return b; |
| 215 | } |
| 216 | EXPORT_SYMBOL_GPL(bio_clone_mddev); |
| 217 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | /* |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 219 | * We have a system wide 'event count' that is incremented |
| 220 | * on any 'interesting' event, and readers of /proc/mdstat |
| 221 | * can use 'poll' or 'select' to find out when the event |
| 222 | * count increases. |
| 223 | * |
| 224 | * Events are: |
| 225 | * start array, stop array, error, add device, remove device, |
| 226 | * start build, activate spare |
| 227 | */ |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 228 | static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 229 | static atomic_t md_event_count; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 230 | void md_new_event(mddev_t *mddev) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 231 | { |
| 232 | atomic_inc(&md_event_count); |
| 233 | wake_up(&md_event_waiters); |
| 234 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 235 | EXPORT_SYMBOL_GPL(md_new_event); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 236 | |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 237 | /* Alternate version that can be called from interrupts |
| 238 | * when calling sysfs_notify isn't needed. |
| 239 | */ |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 240 | static void md_new_event_inintr(mddev_t *mddev) |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 241 | { |
| 242 | atomic_inc(&md_event_count); |
| 243 | wake_up(&md_event_waiters); |
| 244 | } |
| 245 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 246 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | * Enables to iterate over all existing md arrays |
| 248 | * all_mddevs_lock protects this list. |
| 249 | */ |
| 250 | static LIST_HEAD(all_mddevs); |
| 251 | static DEFINE_SPINLOCK(all_mddevs_lock); |
| 252 | |
| 253 | |
| 254 | /* |
| 255 | * iterates through all used mddevs in the system. |
| 256 | * We take care to grab the all_mddevs_lock whenever navigating |
| 257 | * the list, and to always hold a refcount when unlocked. |
| 258 | * Any code which breaks out of this loop while own |
| 259 | * a reference to the current mddev and must mddev_put it. |
| 260 | */ |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 261 | #define for_each_mddev(mddev,tmp) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | \ |
| 263 | for (({ spin_lock(&all_mddevs_lock); \ |
| 264 | tmp = all_mddevs.next; \ |
| 265 | mddev = NULL;}); \ |
| 266 | ({ if (tmp != &all_mddevs) \ |
| 267 | mddev_get(list_entry(tmp, mddev_t, all_mddevs));\ |
| 268 | spin_unlock(&all_mddevs_lock); \ |
| 269 | if (mddev) mddev_put(mddev); \ |
| 270 | mddev = list_entry(tmp, mddev_t, all_mddevs); \ |
| 271 | tmp != &all_mddevs;}); \ |
| 272 | ({ spin_lock(&all_mddevs_lock); \ |
| 273 | tmp = tmp->next;}) \ |
| 274 | ) |
| 275 | |
| 276 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 277 | /* Rather than calling directly into the personality make_request function, |
| 278 | * IO requests come here first so that we can check if the device is |
| 279 | * being suspended pending a reconfiguration. |
| 280 | * We hold a refcount over the call to ->make_request. By the time that |
| 281 | * call has finished, the bio has been linked into some internal structure |
| 282 | * and so is visible to ->quiesce(), so we don't need the refcount any more. |
| 283 | */ |
| 284 | static int md_make_request(struct request_queue *q, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | { |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 286 | const int rw = bio_data_dir(bio); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 287 | mddev_t *mddev = q->queuedata; |
| 288 | int rv; |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 289 | int cpu; |
Chris Mason | e91ece5 | 2011-02-07 19:21:48 -0500 | [diff] [blame] | 290 | unsigned int sectors; |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 291 | |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 292 | if (mddev == NULL || mddev->pers == NULL |
| 293 | || !mddev->ready) { |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 294 | bio_io_error(bio); |
| 295 | return 0; |
| 296 | } |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 297 | smp_rmb(); /* Ensure implications of 'active' are visible */ |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 298 | rcu_read_lock(); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 299 | if (mddev->suspended) { |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 300 | DEFINE_WAIT(__wait); |
| 301 | for (;;) { |
| 302 | prepare_to_wait(&mddev->sb_wait, &__wait, |
| 303 | TASK_UNINTERRUPTIBLE); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 304 | if (!mddev->suspended) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 305 | break; |
| 306 | rcu_read_unlock(); |
| 307 | schedule(); |
| 308 | rcu_read_lock(); |
| 309 | } |
| 310 | finish_wait(&mddev->sb_wait, &__wait); |
| 311 | } |
| 312 | atomic_inc(&mddev->active_io); |
| 313 | rcu_read_unlock(); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 314 | |
Chris Mason | e91ece5 | 2011-02-07 19:21:48 -0500 | [diff] [blame] | 315 | /* |
| 316 | * save the sectors now since our bio can |
| 317 | * go away inside make_request |
| 318 | */ |
| 319 | sectors = bio_sectors(bio); |
NeilBrown | 21a52c6 | 2010-04-01 15:02:13 +1100 | [diff] [blame] | 320 | rv = mddev->pers->make_request(mddev, bio); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 321 | |
| 322 | cpu = part_stat_lock(); |
| 323 | part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]); |
Chris Mason | e91ece5 | 2011-02-07 19:21:48 -0500 | [diff] [blame] | 324 | part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 325 | part_stat_unlock(); |
| 326 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 327 | if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended) |
| 328 | wake_up(&mddev->sb_wait); |
| 329 | |
| 330 | return rv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | } |
| 332 | |
NeilBrown | 9e35b99 | 2010-04-06 14:23:02 +1000 | [diff] [blame] | 333 | /* mddev_suspend makes sure no new requests are submitted |
| 334 | * to the device, and that any requests that have been submitted |
| 335 | * are completely handled. |
| 336 | * Once ->stop is called and completes, the module will be completely |
| 337 | * unused. |
| 338 | */ |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 339 | void mddev_suspend(mddev_t *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 340 | { |
| 341 | BUG_ON(mddev->suspended); |
| 342 | mddev->suspended = 1; |
| 343 | synchronize_rcu(); |
| 344 | wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0); |
| 345 | mddev->pers->quiesce(mddev, 1); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 346 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 347 | EXPORT_SYMBOL_GPL(mddev_suspend); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 348 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 349 | void mddev_resume(mddev_t *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 350 | { |
| 351 | mddev->suspended = 0; |
| 352 | wake_up(&mddev->sb_wait); |
| 353 | mddev->pers->quiesce(mddev, 0); |
| 354 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 355 | EXPORT_SYMBOL_GPL(mddev_resume); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 356 | |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 357 | int mddev_congested(mddev_t *mddev, int bits) |
| 358 | { |
| 359 | return mddev->suspended; |
| 360 | } |
| 361 | EXPORT_SYMBOL(mddev_congested); |
| 362 | |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 363 | /* |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 364 | * Generic flush handling for md |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 365 | */ |
| 366 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 367 | static void md_end_flush(struct bio *bio, int err) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 368 | { |
| 369 | mdk_rdev_t *rdev = bio->bi_private; |
| 370 | mddev_t *mddev = rdev->mddev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 371 | |
| 372 | rdev_dec_pending(rdev, mddev); |
| 373 | |
| 374 | if (atomic_dec_and_test(&mddev->flush_pending)) { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 375 | /* The pre-request flush has finished */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 376 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 377 | } |
| 378 | bio_put(bio); |
| 379 | } |
| 380 | |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 381 | static void md_submit_flush_data(struct work_struct *ws); |
| 382 | |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 383 | static void submit_flushes(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 384 | { |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 385 | mddev_t *mddev = container_of(ws, mddev_t, flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 386 | mdk_rdev_t *rdev; |
| 387 | |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 388 | INIT_WORK(&mddev->flush_work, md_submit_flush_data); |
| 389 | atomic_set(&mddev->flush_pending, 1); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 390 | rcu_read_lock(); |
| 391 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
| 392 | if (rdev->raid_disk >= 0 && |
| 393 | !test_bit(Faulty, &rdev->flags)) { |
| 394 | /* Take two references, one is dropped |
| 395 | * when request finishes, one after |
| 396 | * we reclaim rcu_read_lock |
| 397 | */ |
| 398 | struct bio *bi; |
| 399 | atomic_inc(&rdev->nr_pending); |
| 400 | atomic_inc(&rdev->nr_pending); |
| 401 | rcu_read_unlock(); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 402 | bi = bio_alloc_mddev(GFP_KERNEL, 0, mddev); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 403 | bi->bi_end_io = md_end_flush; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 404 | bi->bi_private = rdev; |
| 405 | bi->bi_bdev = rdev->bdev; |
| 406 | atomic_inc(&mddev->flush_pending); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 407 | submit_bio(WRITE_FLUSH, bi); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 408 | rcu_read_lock(); |
| 409 | rdev_dec_pending(rdev, mddev); |
| 410 | } |
| 411 | rcu_read_unlock(); |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 412 | if (atomic_dec_and_test(&mddev->flush_pending)) |
| 413 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 414 | } |
| 415 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 416 | static void md_submit_flush_data(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 417 | { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 418 | mddev_t *mddev = container_of(ws, mddev_t, flush_work); |
| 419 | struct bio *bio = mddev->flush_bio; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 420 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 421 | if (bio->bi_size == 0) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 422 | /* an empty barrier - all done */ |
| 423 | bio_endio(bio, 0); |
| 424 | else { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 425 | bio->bi_rw &= ~REQ_FLUSH; |
NeilBrown | 21a52c6 | 2010-04-01 15:02:13 +1100 | [diff] [blame] | 426 | if (mddev->pers->make_request(mddev, bio)) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 427 | generic_make_request(bio); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 428 | } |
NeilBrown | 2b74e12 | 2010-12-09 15:59:01 +1100 | [diff] [blame] | 429 | |
| 430 | mddev->flush_bio = NULL; |
| 431 | wake_up(&mddev->sb_wait); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 432 | } |
| 433 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 434 | void md_flush_request(mddev_t *mddev, struct bio *bio) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 435 | { |
| 436 | spin_lock_irq(&mddev->write_lock); |
| 437 | wait_event_lock_irq(mddev->sb_wait, |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 438 | !mddev->flush_bio, |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 439 | mddev->write_lock, /*nothing*/); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 440 | mddev->flush_bio = bio; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 441 | spin_unlock_irq(&mddev->write_lock); |
| 442 | |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 443 | INIT_WORK(&mddev->flush_work, submit_flushes); |
| 444 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 445 | } |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 446 | EXPORT_SYMBOL(md_flush_request); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 447 | |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame^] | 448 | /* Support for plugging. |
| 449 | * This mirrors the plugging support in request_queue, but does not |
| 450 | * require having a whole queue or request structures. |
| 451 | * We allocate an md_plug_cb for each md device and each thread it gets |
| 452 | * plugged on. This links tot the private plug_handle structure in the |
| 453 | * personality data where we keep a count of the number of outstanding |
| 454 | * plugs so other code can see if a plug is active. |
| 455 | */ |
| 456 | struct md_plug_cb { |
| 457 | struct blk_plug_cb cb; |
| 458 | mddev_t *mddev; |
| 459 | }; |
| 460 | |
| 461 | static void plugger_unplug(struct blk_plug_cb *cb) |
| 462 | { |
| 463 | struct md_plug_cb *mdcb = container_of(cb, struct md_plug_cb, cb); |
| 464 | if (atomic_dec_and_test(&mdcb->mddev->plug_cnt)) |
| 465 | md_wakeup_thread(mdcb->mddev->thread); |
| 466 | kfree(mdcb); |
| 467 | } |
| 468 | |
| 469 | /* Check that an unplug wakeup will come shortly. |
| 470 | * If not, wakeup the md thread immediately |
| 471 | */ |
| 472 | int mddev_check_plugged(mddev_t *mddev) |
| 473 | { |
| 474 | struct blk_plug *plug = current->plug; |
| 475 | struct md_plug_cb *mdcb; |
| 476 | |
| 477 | if (!plug) |
| 478 | return 0; |
| 479 | |
| 480 | list_for_each_entry(mdcb, &plug->cb_list, cb.list) { |
| 481 | if (mdcb->cb.callback == plugger_unplug && |
| 482 | mdcb->mddev == mddev) { |
| 483 | /* Already on the list, move to top */ |
| 484 | if (mdcb != list_first_entry(&plug->cb_list, |
| 485 | struct md_plug_cb, |
| 486 | cb.list)) |
| 487 | list_move(&mdcb->cb.list, &plug->cb_list); |
| 488 | return 1; |
| 489 | } |
| 490 | } |
| 491 | /* Not currently on the callback list */ |
| 492 | mdcb = kmalloc(sizeof(*mdcb), GFP_ATOMIC); |
| 493 | if (!mdcb) |
| 494 | return 0; |
| 495 | |
| 496 | mdcb->mddev = mddev; |
| 497 | mdcb->cb.callback = plugger_unplug; |
| 498 | atomic_inc(&mddev->plug_cnt); |
| 499 | list_add(&mdcb->cb.list, &plug->cb_list); |
| 500 | return 1; |
| 501 | } |
| 502 | EXPORT_SYMBOL_GPL(mddev_check_plugged); |
NeilBrown | 2ac8740 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 503 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | static inline mddev_t *mddev_get(mddev_t *mddev) |
| 505 | { |
| 506 | atomic_inc(&mddev->active); |
| 507 | return mddev; |
| 508 | } |
| 509 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 510 | static void mddev_delayed_delete(struct work_struct *ws); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 511 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | static void mddev_put(mddev_t *mddev) |
| 513 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 514 | struct bio_set *bs = NULL; |
| 515 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
| 517 | return; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 518 | if (!mddev->raid_disks && list_empty(&mddev->disks) && |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 519 | mddev->ctime == 0 && !mddev->hold_active) { |
| 520 | /* Array is not configured at all, and not held active, |
| 521 | * so destroy it */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | list_del(&mddev->all_mddevs); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 523 | bs = mddev->bio_set; |
| 524 | mddev->bio_set = NULL; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 525 | if (mddev->gendisk) { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 526 | /* We did a probe so need to clean up. Call |
| 527 | * queue_work inside the spinlock so that |
| 528 | * flush_workqueue() after mddev_find will |
| 529 | * succeed in waiting for the work to be done. |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 530 | */ |
| 531 | INIT_WORK(&mddev->del_work, mddev_delayed_delete); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 532 | queue_work(md_misc_wq, &mddev->del_work); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 533 | } else |
| 534 | kfree(mddev); |
| 535 | } |
| 536 | spin_unlock(&all_mddevs_lock); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 537 | if (bs) |
| 538 | bioset_free(bs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | } |
| 540 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 541 | void mddev_init(mddev_t *mddev) |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 542 | { |
| 543 | mutex_init(&mddev->open_mutex); |
| 544 | mutex_init(&mddev->reconfig_mutex); |
| 545 | mutex_init(&mddev->bitmap_info.mutex); |
| 546 | INIT_LIST_HEAD(&mddev->disks); |
| 547 | INIT_LIST_HEAD(&mddev->all_mddevs); |
| 548 | init_timer(&mddev->safemode_timer); |
| 549 | atomic_set(&mddev->active, 1); |
| 550 | atomic_set(&mddev->openers, 0); |
| 551 | atomic_set(&mddev->active_io, 0); |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame^] | 552 | atomic_set(&mddev->plug_cnt, 0); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 553 | spin_lock_init(&mddev->write_lock); |
| 554 | atomic_set(&mddev->flush_pending, 0); |
| 555 | init_waitqueue_head(&mddev->sb_wait); |
| 556 | init_waitqueue_head(&mddev->recovery_wait); |
| 557 | mddev->reshape_position = MaxSector; |
| 558 | mddev->resync_min = 0; |
| 559 | mddev->resync_max = MaxSector; |
| 560 | mddev->level = LEVEL_NONE; |
| 561 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 562 | EXPORT_SYMBOL_GPL(mddev_init); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 563 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | static mddev_t * mddev_find(dev_t unit) |
| 565 | { |
| 566 | mddev_t *mddev, *new = NULL; |
| 567 | |
NeilBrown | 8f5f02c | 2011-02-16 13:58:51 +1100 | [diff] [blame] | 568 | if (unit && MAJOR(unit) != MD_MAJOR) |
| 569 | unit &= ~((1<<MdpMinorShift)-1); |
| 570 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | retry: |
| 572 | spin_lock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 574 | if (unit) { |
| 575 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 576 | if (mddev->unit == unit) { |
| 577 | mddev_get(mddev); |
| 578 | spin_unlock(&all_mddevs_lock); |
| 579 | kfree(new); |
| 580 | return mddev; |
| 581 | } |
| 582 | |
| 583 | if (new) { |
| 584 | list_add(&new->all_mddevs, &all_mddevs); |
| 585 | spin_unlock(&all_mddevs_lock); |
| 586 | new->hold_active = UNTIL_IOCTL; |
| 587 | return new; |
| 588 | } |
| 589 | } else if (new) { |
| 590 | /* find an unused unit number */ |
| 591 | static int next_minor = 512; |
| 592 | int start = next_minor; |
| 593 | int is_free = 0; |
| 594 | int dev = 0; |
| 595 | while (!is_free) { |
| 596 | dev = MKDEV(MD_MAJOR, next_minor); |
| 597 | next_minor++; |
| 598 | if (next_minor > MINORMASK) |
| 599 | next_minor = 0; |
| 600 | if (next_minor == start) { |
| 601 | /* Oh dear, all in use. */ |
| 602 | spin_unlock(&all_mddevs_lock); |
| 603 | kfree(new); |
| 604 | return NULL; |
| 605 | } |
| 606 | |
| 607 | is_free = 1; |
| 608 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 609 | if (mddev->unit == dev) { |
| 610 | is_free = 0; |
| 611 | break; |
| 612 | } |
| 613 | } |
| 614 | new->unit = dev; |
| 615 | new->md_minor = MINOR(dev); |
| 616 | new->hold_active = UNTIL_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | list_add(&new->all_mddevs, &all_mddevs); |
| 618 | spin_unlock(&all_mddevs_lock); |
| 619 | return new; |
| 620 | } |
| 621 | spin_unlock(&all_mddevs_lock); |
| 622 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 623 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | if (!new) |
| 625 | return NULL; |
| 626 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | new->unit = unit; |
| 628 | if (MAJOR(unit) == MD_MAJOR) |
| 629 | new->md_minor = MINOR(unit); |
| 630 | else |
| 631 | new->md_minor = MINOR(unit) >> MdpMinorShift; |
| 632 | |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 633 | mddev_init(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | goto retry; |
| 636 | } |
| 637 | |
| 638 | static inline int mddev_lock(mddev_t * mddev) |
| 639 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 640 | return mutex_lock_interruptible(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | } |
| 642 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 643 | static inline int mddev_is_locked(mddev_t *mddev) |
| 644 | { |
| 645 | return mutex_is_locked(&mddev->reconfig_mutex); |
| 646 | } |
| 647 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | static inline int mddev_trylock(mddev_t * mddev) |
| 649 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 650 | return mutex_trylock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | } |
| 652 | |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 653 | static struct attribute_group md_redundancy_group; |
| 654 | |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 655 | static void mddev_unlock(mddev_t * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | { |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 657 | if (mddev->to_remove) { |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 658 | /* These cannot be removed under reconfig_mutex as |
| 659 | * an access to the files will try to take reconfig_mutex |
| 660 | * while holding the file unremovable, which leads to |
| 661 | * a deadlock. |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 662 | * So hold set sysfs_active while the remove in happeing, |
| 663 | * and anything else which might set ->to_remove or my |
| 664 | * otherwise change the sysfs namespace will fail with |
| 665 | * -EBUSY if sysfs_active is still set. |
| 666 | * We set sysfs_active under reconfig_mutex and elsewhere |
| 667 | * test it under the same mutex to ensure its correct value |
| 668 | * is seen. |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 669 | */ |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 670 | struct attribute_group *to_remove = mddev->to_remove; |
| 671 | mddev->to_remove = NULL; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 672 | mddev->sysfs_active = 1; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 673 | mutex_unlock(&mddev->reconfig_mutex); |
| 674 | |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 675 | if (mddev->kobj.sd) { |
| 676 | if (to_remove != &md_redundancy_group) |
| 677 | sysfs_remove_group(&mddev->kobj, to_remove); |
| 678 | if (mddev->pers == NULL || |
| 679 | mddev->pers->sync_request == NULL) { |
| 680 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 681 | if (mddev->sysfs_action) |
| 682 | sysfs_put(mddev->sysfs_action); |
| 683 | mddev->sysfs_action = NULL; |
| 684 | } |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 685 | } |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 686 | mddev->sysfs_active = 0; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 687 | } else |
| 688 | mutex_unlock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 690 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | } |
| 692 | |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 693 | static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 695 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 697 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | if (rdev->desc_nr == nr) |
| 699 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 700 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | return NULL; |
| 702 | } |
| 703 | |
| 704 | static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev) |
| 705 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | mdk_rdev_t *rdev; |
| 707 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 708 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | if (rdev->bdev->bd_dev == dev) |
| 710 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 711 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | return NULL; |
| 713 | } |
| 714 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 715 | static struct mdk_personality *find_pers(int level, char *clevel) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 716 | { |
| 717 | struct mdk_personality *pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 718 | list_for_each_entry(pers, &pers_list, list) { |
| 719 | if (level != LEVEL_NONE && pers->level == level) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 720 | return pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 721 | if (strcmp(pers->name, clevel)==0) |
| 722 | return pers; |
| 723 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 724 | return NULL; |
| 725 | } |
| 726 | |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 727 | /* return the offset of the super block in 512byte sectors */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 728 | static inline sector_t calc_dev_sboffset(mdk_rdev_t *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | { |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 730 | sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512; |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 731 | return MD_NEW_SIZE_SECTORS(num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | } |
| 733 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | static int alloc_disk_sb(mdk_rdev_t * rdev) |
| 735 | { |
| 736 | if (rdev->sb_page) |
| 737 | MD_BUG(); |
| 738 | |
| 739 | rdev->sb_page = alloc_page(GFP_KERNEL); |
| 740 | if (!rdev->sb_page) { |
| 741 | printk(KERN_ALERT "md: out of memory.\n"); |
Andre Noll | ebc2433 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 742 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | return 0; |
| 746 | } |
| 747 | |
| 748 | static void free_disk_sb(mdk_rdev_t * rdev) |
| 749 | { |
| 750 | if (rdev->sb_page) { |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 751 | put_page(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | rdev->sb_loaded = 0; |
| 753 | rdev->sb_page = NULL; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 754 | rdev->sb_start = 0; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 755 | rdev->sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | } |
| 757 | } |
| 758 | |
| 759 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 760 | static void super_written(struct bio *bio, int error) |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 761 | { |
| 762 | mdk_rdev_t *rdev = bio->bi_private; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 763 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 764 | |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 765 | if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) { |
| 766 | printk("md: super_written gets error=%d, uptodate=%d\n", |
| 767 | error, test_bit(BIO_UPTODATE, &bio->bi_flags)); |
| 768 | WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags)); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 769 | md_error(mddev, rdev); |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 770 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 771 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 772 | if (atomic_dec_and_test(&mddev->pending_writes)) |
| 773 | wake_up(&mddev->sb_wait); |
Neil Brown | f8b58ed | 2005-06-27 22:29:34 -0700 | [diff] [blame] | 774 | bio_put(bio); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev, |
| 778 | sector_t sector, int size, struct page *page) |
| 779 | { |
| 780 | /* write first size bytes of page to sector of rdev |
| 781 | * Increment mddev->pending_writes before returning |
| 782 | * and decrement it on completion, waking up sb_wait |
| 783 | * if zero is reached. |
| 784 | * If an error occurred, call md_error |
| 785 | */ |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 786 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 787 | |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 788 | bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 789 | bio->bi_sector = sector; |
| 790 | bio_add_page(bio, page, size, 0); |
| 791 | bio->bi_private = rdev; |
| 792 | bio->bi_end_io = super_written; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 793 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 794 | atomic_inc(&mddev->pending_writes); |
Jens Axboe | 721a960 | 2011-03-09 11:56:30 +0100 | [diff] [blame] | 795 | submit_bio(REQ_WRITE | REQ_SYNC | REQ_FLUSH | REQ_FUA, bio); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | void md_super_wait(mddev_t *mddev) |
| 799 | { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 800 | /* wait for all superblock writes that were scheduled to complete */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 801 | DEFINE_WAIT(wq); |
| 802 | for(;;) { |
| 803 | prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE); |
| 804 | if (atomic_read(&mddev->pending_writes)==0) |
| 805 | break; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 806 | schedule(); |
| 807 | } |
| 808 | finish_wait(&mddev->sb_wait, &wq); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 809 | } |
| 810 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 811 | static void bi_complete(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | complete((struct completion*)bio->bi_private); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | } |
| 815 | |
NeilBrown | 2b19336 | 2010-10-27 15:16:40 +1100 | [diff] [blame] | 816 | int sync_page_io(mdk_rdev_t *rdev, sector_t sector, int size, |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 817 | struct page *page, int rw, bool metadata_op) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 819 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | struct completion event; |
| 821 | int ret; |
| 822 | |
Jens Axboe | 721a960 | 2011-03-09 11:56:30 +0100 | [diff] [blame] | 823 | rw |= REQ_SYNC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 825 | bio->bi_bdev = (metadata_op && rdev->meta_bdev) ? |
| 826 | rdev->meta_bdev : rdev->bdev; |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 827 | if (metadata_op) |
| 828 | bio->bi_sector = sector + rdev->sb_start; |
| 829 | else |
| 830 | bio->bi_sector = sector + rdev->data_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | bio_add_page(bio, page, size, 0); |
| 832 | init_completion(&event); |
| 833 | bio->bi_private = &event; |
| 834 | bio->bi_end_io = bi_complete; |
| 835 | submit_bio(rw, bio); |
| 836 | wait_for_completion(&event); |
| 837 | |
| 838 | ret = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 839 | bio_put(bio); |
| 840 | return ret; |
| 841 | } |
NeilBrown | a8745db | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 842 | EXPORT_SYMBOL_GPL(sync_page_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 844 | static int read_disk_sb(mdk_rdev_t * rdev, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | { |
| 846 | char b[BDEVNAME_SIZE]; |
| 847 | if (!rdev->sb_page) { |
| 848 | MD_BUG(); |
| 849 | return -EINVAL; |
| 850 | } |
| 851 | if (rdev->sb_loaded) |
| 852 | return 0; |
| 853 | |
| 854 | |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 855 | if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | goto fail; |
| 857 | rdev->sb_loaded = 1; |
| 858 | return 0; |
| 859 | |
| 860 | fail: |
| 861 | printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n", |
| 862 | bdevname(rdev->bdev,b)); |
| 863 | return -EINVAL; |
| 864 | } |
| 865 | |
| 866 | static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 867 | { |
Andre Noll | 0571046 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 868 | return sb1->set_uuid0 == sb2->set_uuid0 && |
| 869 | sb1->set_uuid1 == sb2->set_uuid1 && |
| 870 | sb1->set_uuid2 == sb2->set_uuid2 && |
| 871 | sb1->set_uuid3 == sb2->set_uuid3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | } |
| 873 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 875 | { |
| 876 | int ret; |
| 877 | mdp_super_t *tmp1, *tmp2; |
| 878 | |
| 879 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); |
| 880 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); |
| 881 | |
| 882 | if (!tmp1 || !tmp2) { |
| 883 | ret = 0; |
Andre Noll | 35020f1 | 2008-03-23 15:10:33 +0100 | [diff] [blame] | 884 | printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | goto abort; |
| 886 | } |
| 887 | |
| 888 | *tmp1 = *sb1; |
| 889 | *tmp2 = *sb2; |
| 890 | |
| 891 | /* |
| 892 | * nr_disks is not constant |
| 893 | */ |
| 894 | tmp1->nr_disks = 0; |
| 895 | tmp2->nr_disks = 0; |
| 896 | |
Andre Noll | ce0c8e0 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 897 | ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | abort: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 899 | kfree(tmp1); |
| 900 | kfree(tmp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | return ret; |
| 902 | } |
| 903 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 904 | |
| 905 | static u32 md_csum_fold(u32 csum) |
| 906 | { |
| 907 | csum = (csum & 0xffff) + (csum >> 16); |
| 908 | return (csum & 0xffff) + (csum >> 16); |
| 909 | } |
| 910 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | static unsigned int calc_sb_csum(mdp_super_t * sb) |
| 912 | { |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 913 | u64 newcsum = 0; |
| 914 | u32 *sb32 = (u32*)sb; |
| 915 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | unsigned int disk_csum, csum; |
| 917 | |
| 918 | disk_csum = sb->sb_csum; |
| 919 | sb->sb_csum = 0; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 920 | |
| 921 | for (i = 0; i < MD_SB_BYTES/4 ; i++) |
| 922 | newcsum += sb32[i]; |
| 923 | csum = (newcsum & 0xffffffff) + (newcsum>>32); |
| 924 | |
| 925 | |
| 926 | #ifdef CONFIG_ALPHA |
| 927 | /* This used to use csum_partial, which was wrong for several |
| 928 | * reasons including that different results are returned on |
| 929 | * different architectures. It isn't critical that we get exactly |
| 930 | * the same return value as before (we always csum_fold before |
| 931 | * testing, and that removes any differences). However as we |
| 932 | * know that csum_partial always returned a 16bit value on |
| 933 | * alphas, do a fold to maximise conformity to previous behaviour. |
| 934 | */ |
| 935 | sb->sb_csum = md_csum_fold(disk_csum); |
| 936 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | sb->sb_csum = disk_csum; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 938 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | return csum; |
| 940 | } |
| 941 | |
| 942 | |
| 943 | /* |
| 944 | * Handle superblock details. |
| 945 | * We want to be able to handle multiple superblock formats |
| 946 | * so we have a common interface to them all, and an array of |
| 947 | * different handlers. |
| 948 | * We rely on user-space to write the initial superblock, and support |
| 949 | * reading and updating of superblocks. |
| 950 | * Interface methods are: |
| 951 | * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version) |
| 952 | * loads and validates a superblock on dev. |
| 953 | * if refdev != NULL, compare superblocks on both devices |
| 954 | * Return: |
| 955 | * 0 - dev has a superblock that is compatible with refdev |
| 956 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 957 | * so dev should be used as the refdev in future |
| 958 | * -EINVAL superblock incompatible or invalid |
| 959 | * -othererror e.g. -EIO |
| 960 | * |
| 961 | * int validate_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 962 | * Verify that dev is acceptable into mddev. |
| 963 | * The first time, mddev->raid_disks will be 0, and data from |
| 964 | * dev should be merged in. Subsequent calls check that dev |
| 965 | * is new enough. Return 0 or -EINVAL |
| 966 | * |
| 967 | * void sync_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 968 | * Update the superblock for rdev with data in mddev |
| 969 | * This does not write to disc. |
| 970 | * |
| 971 | */ |
| 972 | |
| 973 | struct super_type { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 974 | char *name; |
| 975 | struct module *owner; |
| 976 | int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, |
| 977 | int minor_version); |
| 978 | int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 979 | void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 980 | unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev, |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 981 | sector_t num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | }; |
| 983 | |
| 984 | /* |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 985 | * Check that the given mddev has no bitmap. |
| 986 | * |
| 987 | * This function is called from the run method of all personalities that do not |
| 988 | * support bitmaps. It prints an error message and returns non-zero if mddev |
| 989 | * has a bitmap. Otherwise, it returns 0. |
| 990 | * |
| 991 | */ |
| 992 | int md_check_no_bitmap(mddev_t *mddev) |
| 993 | { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 994 | if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 995 | return 0; |
| 996 | printk(KERN_ERR "%s: bitmaps are not supported for %s\n", |
| 997 | mdname(mddev), mddev->pers->name); |
| 998 | return 1; |
| 999 | } |
| 1000 | EXPORT_SYMBOL(md_check_no_bitmap); |
| 1001 | |
| 1002 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | * load_super for 0.90.0 |
| 1004 | */ |
| 1005 | static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 1006 | { |
| 1007 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 1008 | mdp_super_t *sb; |
| 1009 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | |
| 1011 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1012 | * Calculate the position of the superblock (512byte sectors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | * it's at the end of the disk. |
| 1014 | * |
| 1015 | * It also happens to be a multiple of 4Kb. |
| 1016 | */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1017 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1019 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | if (ret) return ret; |
| 1021 | |
| 1022 | ret = -EINVAL; |
| 1023 | |
| 1024 | bdevname(rdev->bdev, b); |
| 1025 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 1026 | |
| 1027 | if (sb->md_magic != MD_SB_MAGIC) { |
| 1028 | printk(KERN_ERR "md: invalid raid superblock magic on %s\n", |
| 1029 | b); |
| 1030 | goto abort; |
| 1031 | } |
| 1032 | |
| 1033 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1034 | sb->minor_version < 90 || |
| 1035 | sb->minor_version > 91) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | printk(KERN_WARNING "Bad version number %d.%d on %s\n", |
| 1037 | sb->major_version, sb->minor_version, |
| 1038 | b); |
| 1039 | goto abort; |
| 1040 | } |
| 1041 | |
| 1042 | if (sb->raid_disks <= 0) |
| 1043 | goto abort; |
| 1044 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 1045 | 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] | 1046 | printk(KERN_WARNING "md: invalid superblock checksum on %s\n", |
| 1047 | b); |
| 1048 | goto abort; |
| 1049 | } |
| 1050 | |
| 1051 | rdev->preferred_minor = sb->md_minor; |
| 1052 | rdev->data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1053 | rdev->sb_size = MD_SB_BYTES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | |
| 1055 | if (sb->level == LEVEL_MULTIPATH) |
| 1056 | rdev->desc_nr = -1; |
| 1057 | else |
| 1058 | rdev->desc_nr = sb->this_disk.number; |
| 1059 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1060 | if (!refdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1062 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | __u64 ev1, ev2; |
| 1064 | mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page); |
| 1065 | if (!uuid_equal(refsb, sb)) { |
| 1066 | printk(KERN_WARNING "md: %s has different UUID to %s\n", |
| 1067 | b, bdevname(refdev->bdev,b2)); |
| 1068 | goto abort; |
| 1069 | } |
| 1070 | if (!sb_equal(refsb, sb)) { |
| 1071 | printk(KERN_WARNING "md: %s has same UUID" |
| 1072 | " but different superblock to %s\n", |
| 1073 | b, bdevname(refdev->bdev, b2)); |
| 1074 | goto abort; |
| 1075 | } |
| 1076 | ev1 = md_event(sb); |
| 1077 | ev2 = md_event(refsb); |
| 1078 | if (ev1 > ev2) |
| 1079 | ret = 1; |
| 1080 | else |
| 1081 | ret = 0; |
| 1082 | } |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 1083 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1085 | if (rdev->sectors < sb->size * 2 && sb->level > 1) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1086 | /* "this cannot possibly happen" ... */ |
| 1087 | ret = -EINVAL; |
| 1088 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | abort: |
| 1090 | return ret; |
| 1091 | } |
| 1092 | |
| 1093 | /* |
| 1094 | * validate_super for 0.90.0 |
| 1095 | */ |
| 1096 | static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1097 | { |
| 1098 | mdp_disk_t *desc; |
| 1099 | mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1100 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1101 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1102 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1103 | clear_bit(Faulty, &rdev->flags); |
| 1104 | clear_bit(In_sync, &rdev->flags); |
| 1105 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1106 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 | if (mddev->raid_disks == 0) { |
| 1108 | mddev->major_version = 0; |
| 1109 | mddev->minor_version = sb->minor_version; |
| 1110 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1111 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1112 | mddev->chunk_sectors = sb->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | mddev->ctime = sb->ctime; |
| 1114 | mddev->utime = sb->utime; |
| 1115 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1116 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1117 | mddev->layout = sb->layout; |
| 1118 | mddev->raid_disks = sb->raid_disks; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1119 | mddev->dev_sectors = sb->size * 2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1120 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1121 | mddev->bitmap_info.offset = 0; |
| 1122 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1123 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1124 | if (mddev->minor_version >= 91) { |
| 1125 | mddev->reshape_position = sb->reshape_position; |
| 1126 | mddev->delta_disks = sb->delta_disks; |
| 1127 | mddev->new_level = sb->new_level; |
| 1128 | mddev->new_layout = sb->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1129 | mddev->new_chunk_sectors = sb->new_chunk >> 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1130 | } else { |
| 1131 | mddev->reshape_position = MaxSector; |
| 1132 | mddev->delta_disks = 0; |
| 1133 | mddev->new_level = mddev->level; |
| 1134 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1135 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1136 | } |
| 1137 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 1139 | mddev->recovery_cp = MaxSector; |
| 1140 | else { |
| 1141 | if (sb->events_hi == sb->cp_events_hi && |
| 1142 | sb->events_lo == sb->cp_events_lo) { |
| 1143 | mddev->recovery_cp = sb->recovery_cp; |
| 1144 | } else |
| 1145 | mddev->recovery_cp = 0; |
| 1146 | } |
| 1147 | |
| 1148 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 1149 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 1150 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 1151 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 1152 | |
| 1153 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1154 | |
| 1155 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1156 | mddev->bitmap_info.file == NULL) |
| 1157 | mddev->bitmap_info.offset = |
| 1158 | mddev->bitmap_info.default_offset; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1159 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1160 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1161 | /* Insist on good event counter while assembling, except |
| 1162 | * for spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1163 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1164 | if (sb->disks[rdev->desc_nr].state & ( |
| 1165 | (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE))) |
| 1166 | if (ev1 < mddev->events) |
| 1167 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1168 | } else if (mddev->bitmap) { |
| 1169 | /* if adding to array with a bitmap, then we can accept an |
| 1170 | * older device ... but not too old. |
| 1171 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1172 | if (ev1 < mddev->bitmap->events_cleared) |
| 1173 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1174 | } else { |
| 1175 | if (ev1 < mddev->events) |
| 1176 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1177 | return 0; |
| 1178 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1179 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1180 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1181 | desc = sb->disks + rdev->desc_nr; |
| 1182 | |
| 1183 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1184 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 1185 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 1186 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1187 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1188 | rdev->raid_disk = desc->raid_disk; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1189 | } else if (desc->state & (1<<MD_DISK_ACTIVE)) { |
| 1190 | /* active but not in sync implies recovery up to |
| 1191 | * reshape position. We don't know exactly where |
| 1192 | * that is, so set to zero for now */ |
| 1193 | if (mddev->minor_version >= 91) { |
| 1194 | rdev->recovery_offset = 0; |
| 1195 | rdev->raid_disk = desc->raid_disk; |
| 1196 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1197 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1198 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 1199 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1200 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1201 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | return 0; |
| 1203 | } |
| 1204 | |
| 1205 | /* |
| 1206 | * sync_super for 0.90.0 |
| 1207 | */ |
| 1208 | static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1209 | { |
| 1210 | mdp_super_t *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1211 | mdk_rdev_t *rdev2; |
| 1212 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1213 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1214 | |
| 1215 | /* make rdev->sb match mddev data.. |
| 1216 | * |
| 1217 | * 1/ zero out disks |
| 1218 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 1219 | * 3/ any empty disks < next_spare become removed |
| 1220 | * |
| 1221 | * disks[0] gets initialised to REMOVED because |
| 1222 | * we cannot be sure from other fields if it has |
| 1223 | * been initialised or not. |
| 1224 | */ |
| 1225 | int i; |
| 1226 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 1227 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 1228 | rdev->sb_size = MD_SB_BYTES; |
| 1229 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1230 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 1231 | |
| 1232 | memset(sb, 0, sizeof(*sb)); |
| 1233 | |
| 1234 | sb->md_magic = MD_SB_MAGIC; |
| 1235 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | sb->patch_version = mddev->patch_version; |
| 1237 | sb->gvalid_words = 0; /* ignored */ |
| 1238 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 1239 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 1240 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 1241 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 1242 | |
| 1243 | sb->ctime = mddev->ctime; |
| 1244 | sb->level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1245 | sb->size = mddev->dev_sectors / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | sb->raid_disks = mddev->raid_disks; |
| 1247 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1248 | sb->not_persistent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1249 | sb->utime = mddev->utime; |
| 1250 | sb->state = 0; |
| 1251 | sb->events_hi = (mddev->events>>32); |
| 1252 | sb->events_lo = (u32)mddev->events; |
| 1253 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1254 | if (mddev->reshape_position == MaxSector) |
| 1255 | sb->minor_version = 90; |
| 1256 | else { |
| 1257 | sb->minor_version = 91; |
| 1258 | sb->reshape_position = mddev->reshape_position; |
| 1259 | sb->new_level = mddev->new_level; |
| 1260 | sb->delta_disks = mddev->delta_disks; |
| 1261 | sb->new_layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1262 | sb->new_chunk = mddev->new_chunk_sectors << 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1263 | } |
| 1264 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | if (mddev->in_sync) |
| 1266 | { |
| 1267 | sb->recovery_cp = mddev->recovery_cp; |
| 1268 | sb->cp_events_hi = (mddev->events>>32); |
| 1269 | sb->cp_events_lo = (u32)mddev->events; |
| 1270 | if (mddev->recovery_cp == MaxSector) |
| 1271 | sb->state = (1<< MD_SB_CLEAN); |
| 1272 | } else |
| 1273 | sb->recovery_cp = 0; |
| 1274 | |
| 1275 | sb->layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1276 | sb->chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1277 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1278 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1279 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 1280 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1281 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1282 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1284 | int desc_nr; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1285 | int is_active = test_bit(In_sync, &rdev2->flags); |
| 1286 | |
| 1287 | if (rdev2->raid_disk >= 0 && |
| 1288 | sb->minor_version >= 91) |
| 1289 | /* we have nowhere to store the recovery_offset, |
| 1290 | * but if it is not below the reshape_position, |
| 1291 | * we can piggy-back on that. |
| 1292 | */ |
| 1293 | is_active = 1; |
| 1294 | if (rdev2->raid_disk < 0 || |
| 1295 | test_bit(Faulty, &rdev2->flags)) |
| 1296 | is_active = 0; |
| 1297 | if (is_active) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1298 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1299 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1300 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1301 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | d = &sb->disks[rdev2->desc_nr]; |
| 1303 | nr_disks++; |
| 1304 | d->number = rdev2->desc_nr; |
| 1305 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 1306 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1307 | if (is_active) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1308 | d->raid_disk = rdev2->raid_disk; |
| 1309 | else |
| 1310 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 1311 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1312 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1313 | else if (is_active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | d->state = (1<<MD_DISK_ACTIVE); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1315 | if (test_bit(In_sync, &rdev2->flags)) |
| 1316 | d->state |= (1<<MD_DISK_SYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1317 | active++; |
| 1318 | working++; |
| 1319 | } else { |
| 1320 | d->state = 0; |
| 1321 | spare++; |
| 1322 | working++; |
| 1323 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1324 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 1325 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1326 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 1328 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 1329 | mdp_disk_t *d = &sb->disks[i]; |
| 1330 | if (d->state == 0 && d->number == 0) { |
| 1331 | d->number = i; |
| 1332 | d->raid_disk = i; |
| 1333 | d->state = (1<<MD_DISK_REMOVED); |
| 1334 | d->state |= (1<<MD_DISK_FAULTY); |
| 1335 | failed++; |
| 1336 | } |
| 1337 | } |
| 1338 | sb->nr_disks = nr_disks; |
| 1339 | sb->active_disks = active; |
| 1340 | sb->working_disks = working; |
| 1341 | sb->failed_disks = failed; |
| 1342 | sb->spare_disks = spare; |
| 1343 | |
| 1344 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 1345 | sb->sb_csum = calc_sb_csum(sb); |
| 1346 | } |
| 1347 | |
| 1348 | /* |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1349 | * rdev_size_change for 0.90.0 |
| 1350 | */ |
| 1351 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1352 | 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] | 1353 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1354 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1355 | return 0; /* component must fit device */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1356 | if (rdev->mddev->bitmap_info.offset) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1357 | return 0; /* can't move bitmap */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1358 | rdev->sb_start = calc_dev_sboffset(rdev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1359 | if (!num_sectors || num_sectors > rdev->sb_start) |
| 1360 | num_sectors = rdev->sb_start; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1361 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1362 | rdev->sb_page); |
| 1363 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1364 | return num_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1365 | } |
| 1366 | |
| 1367 | |
| 1368 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 | * version 1 superblock |
| 1370 | */ |
| 1371 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1372 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1373 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1374 | __le32 disk_csum; |
| 1375 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1376 | unsigned long long newcsum; |
| 1377 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1378 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | int i; |
| 1380 | |
| 1381 | disk_csum = sb->sb_csum; |
| 1382 | sb->sb_csum = 0; |
| 1383 | newcsum = 0; |
| 1384 | for (i=0; size>=4; size -= 4 ) |
| 1385 | newcsum += le32_to_cpu(*isuper++); |
| 1386 | |
| 1387 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1388 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | |
| 1390 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1391 | sb->sb_csum = disk_csum; |
| 1392 | return cpu_to_le32(csum); |
| 1393 | } |
| 1394 | |
| 1395 | static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 1396 | { |
| 1397 | struct mdp_superblock_1 *sb; |
| 1398 | int ret; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1399 | sector_t sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1400 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1401 | int bmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | |
| 1403 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1404 | * Calculate the position of the superblock in 512byte sectors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1405 | * It is always aligned to a 4K boundary and |
| 1406 | * depeding on minor_version, it can be: |
| 1407 | * 0: At least 8K, but less than 12K, from end of device |
| 1408 | * 1: At start of device |
| 1409 | * 2: 4K from start of device. |
| 1410 | */ |
| 1411 | switch(minor_version) { |
| 1412 | case 0: |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1413 | sb_start = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1414 | sb_start -= 8*2; |
| 1415 | sb_start &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1416 | break; |
| 1417 | case 1: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1418 | sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1419 | break; |
| 1420 | case 2: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1421 | sb_start = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1422 | break; |
| 1423 | default: |
| 1424 | return -EINVAL; |
| 1425 | } |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1426 | rdev->sb_start = sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1427 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1428 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1429 | * and it is safe to read 4k, so we do that |
| 1430 | */ |
| 1431 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1432 | if (ret) return ret; |
| 1433 | |
| 1434 | |
| 1435 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1436 | |
| 1437 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1438 | sb->major_version != cpu_to_le32(1) || |
| 1439 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1440 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1441 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1442 | return -EINVAL; |
| 1443 | |
| 1444 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
| 1445 | printk("md: invalid superblock checksum on %s\n", |
| 1446 | bdevname(rdev->bdev,b)); |
| 1447 | return -EINVAL; |
| 1448 | } |
| 1449 | if (le64_to_cpu(sb->data_size) < 10) { |
| 1450 | printk("md: data_size too small on %s\n", |
| 1451 | bdevname(rdev->bdev,b)); |
| 1452 | return -EINVAL; |
| 1453 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1454 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1455 | rdev->preferred_minor = 0xffff; |
| 1456 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1457 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1458 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1459 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1460 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1461 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1462 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1463 | |
| 1464 | if (minor_version |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1465 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1466 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1467 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1468 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1469 | rdev->desc_nr = -1; |
| 1470 | else |
| 1471 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1472 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1473 | if (!refdev) { |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1474 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1475 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1476 | __u64 ev1, ev2; |
| 1477 | struct mdp_superblock_1 *refsb = |
| 1478 | (struct mdp_superblock_1*)page_address(refdev->sb_page); |
| 1479 | |
| 1480 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1481 | sb->level != refsb->level || |
| 1482 | sb->layout != refsb->layout || |
| 1483 | sb->chunksize != refsb->chunksize) { |
| 1484 | printk(KERN_WARNING "md: %s has strangely different" |
| 1485 | " superblock to %s\n", |
| 1486 | bdevname(rdev->bdev,b), |
| 1487 | bdevname(refdev->bdev,b2)); |
| 1488 | return -EINVAL; |
| 1489 | } |
| 1490 | ev1 = le64_to_cpu(sb->events); |
| 1491 | ev2 = le64_to_cpu(refsb->events); |
| 1492 | |
| 1493 | if (ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1494 | ret = 1; |
| 1495 | else |
| 1496 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 | } |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1498 | if (minor_version) |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1499 | rdev->sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) - |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1500 | le64_to_cpu(sb->data_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1501 | else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1502 | rdev->sectors = rdev->sb_start; |
| 1503 | if (rdev->sectors < le64_to_cpu(sb->data_size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | return -EINVAL; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1505 | rdev->sectors = le64_to_cpu(sb->data_size); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1506 | if (le64_to_cpu(sb->size) > rdev->sectors) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1507 | return -EINVAL; |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1508 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1509 | } |
| 1510 | |
| 1511 | static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1512 | { |
| 1513 | 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] | 1514 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1515 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1516 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1517 | clear_bit(Faulty, &rdev->flags); |
| 1518 | clear_bit(In_sync, &rdev->flags); |
| 1519 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1520 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1521 | if (mddev->raid_disks == 0) { |
| 1522 | mddev->major_version = 1; |
| 1523 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1524 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1525 | mddev->chunk_sectors = le32_to_cpu(sb->chunksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1526 | mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1); |
| 1527 | mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1); |
| 1528 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1529 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1530 | mddev->layout = le32_to_cpu(sb->layout); |
| 1531 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1532 | mddev->dev_sectors = le64_to_cpu(sb->size); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1533 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1534 | mddev->bitmap_info.offset = 0; |
| 1535 | mddev->bitmap_info.default_offset = 1024 >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1536 | |
| 1537 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1538 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1539 | |
| 1540 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1541 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1542 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1543 | mddev->bitmap_info.file == NULL ) |
| 1544 | mddev->bitmap_info.offset = |
| 1545 | (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1546 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1547 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1548 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1549 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1550 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1551 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1552 | mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1553 | } else { |
| 1554 | mddev->reshape_position = MaxSector; |
| 1555 | mddev->delta_disks = 0; |
| 1556 | mddev->new_level = mddev->level; |
| 1557 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1558 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1559 | } |
| 1560 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1561 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1562 | /* Insist of good event counter while assembling, except for |
| 1563 | * spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1564 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1565 | if (rdev->desc_nr >= 0 && |
| 1566 | rdev->desc_nr < le32_to_cpu(sb->max_dev) && |
| 1567 | le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe) |
| 1568 | if (ev1 < mddev->events) |
| 1569 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1570 | } else if (mddev->bitmap) { |
| 1571 | /* If adding to array with a bitmap, then we can accept an |
| 1572 | * older device, but not too old. |
| 1573 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1574 | if (ev1 < mddev->bitmap->events_cleared) |
| 1575 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1576 | } else { |
| 1577 | if (ev1 < mddev->events) |
| 1578 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1579 | return 0; |
| 1580 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1581 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1582 | int role; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1583 | if (rdev->desc_nr < 0 || |
| 1584 | rdev->desc_nr >= le32_to_cpu(sb->max_dev)) { |
| 1585 | role = 0xffff; |
| 1586 | rdev->desc_nr = -1; |
| 1587 | } else |
| 1588 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1589 | switch(role) { |
| 1590 | case 0xffff: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1591 | break; |
| 1592 | case 0xfffe: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1593 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1594 | break; |
| 1595 | default: |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1596 | if ((le32_to_cpu(sb->feature_map) & |
| 1597 | MD_FEATURE_RECOVERY_OFFSET)) |
| 1598 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 1599 | else |
| 1600 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1601 | rdev->raid_disk = role; |
| 1602 | break; |
| 1603 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1604 | if (sb->devflags & WriteMostly1) |
| 1605 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1606 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1607 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1608 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1609 | return 0; |
| 1610 | } |
| 1611 | |
| 1612 | static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1613 | { |
| 1614 | struct mdp_superblock_1 *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1615 | mdk_rdev_t *rdev2; |
| 1616 | int max_dev, i; |
| 1617 | /* make rdev->sb match mddev and rdev data. */ |
| 1618 | |
| 1619 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1620 | |
| 1621 | sb->feature_map = 0; |
| 1622 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1623 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1624 | memset(sb->pad1, 0, sizeof(sb->pad1)); |
| 1625 | memset(sb->pad2, 0, sizeof(sb->pad2)); |
| 1626 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1627 | |
| 1628 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1629 | sb->events = cpu_to_le64(mddev->events); |
| 1630 | if (mddev->in_sync) |
| 1631 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
| 1632 | else |
| 1633 | sb->resync_offset = cpu_to_le64(0); |
| 1634 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1635 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1636 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1637 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1638 | sb->size = cpu_to_le64(mddev->dev_sectors); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1639 | sb->chunksize = cpu_to_le32(mddev->chunk_sectors); |
NeilBrown | 62e1e38 | 2009-05-26 09:40:59 +1000 | [diff] [blame] | 1640 | sb->level = cpu_to_le32(mddev->level); |
| 1641 | sb->layout = cpu_to_le32(mddev->layout); |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1642 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1643 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) { |
| 1644 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1645 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1646 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1647 | |
| 1648 | if (rdev->raid_disk >= 0 && |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1649 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1650 | sb->feature_map |= |
| 1651 | cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 1652 | sb->recovery_offset = |
| 1653 | cpu_to_le64(rdev->recovery_offset); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1654 | } |
| 1655 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1656 | if (mddev->reshape_position != MaxSector) { |
| 1657 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 1658 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1659 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1660 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1661 | sb->new_level = cpu_to_le32(mddev->new_level); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1662 | sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1663 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1664 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1665 | max_dev = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1666 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1667 | if (rdev2->desc_nr+1 > max_dev) |
| 1668 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1669 | |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1670 | if (max_dev > le32_to_cpu(sb->max_dev)) { |
| 1671 | int bmask; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1672 | sb->max_dev = cpu_to_le32(max_dev); |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1673 | rdev->sb_size = max_dev * 2 + 256; |
| 1674 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
| 1675 | if (rdev->sb_size & bmask) |
| 1676 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
NeilBrown | ddcf352 | 2010-09-08 16:48:17 +1000 | [diff] [blame] | 1677 | } else |
| 1678 | max_dev = le32_to_cpu(sb->max_dev); |
| 1679 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1680 | for (i=0; i<max_dev;i++) |
| 1681 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
| 1682 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1683 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1684 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1685 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1686 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1687 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1688 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1689 | else if (rdev2->raid_disk >= 0) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1690 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1691 | else |
| 1692 | sb->dev_roles[i] = cpu_to_le16(0xffff); |
| 1693 | } |
| 1694 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1695 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1696 | } |
| 1697 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1698 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1699 | 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] | 1700 | { |
| 1701 | struct mdp_superblock_1 *sb; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1702 | sector_t max_sectors; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1703 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1704 | return 0; /* component must fit device */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1705 | if (rdev->sb_start < rdev->data_offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1706 | /* minor versions 1 and 2; superblock before data */ |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1707 | max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1708 | max_sectors -= rdev->data_offset; |
| 1709 | if (!num_sectors || num_sectors > max_sectors) |
| 1710 | num_sectors = max_sectors; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1711 | } else if (rdev->mddev->bitmap_info.offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1712 | /* minor version 0 with bitmap we can't move */ |
| 1713 | return 0; |
| 1714 | } else { |
| 1715 | /* minor version 0; superblock after data */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1716 | sector_t sb_start; |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1717 | sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1718 | sb_start &= ~(sector_t)(4*2 - 1); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1719 | max_sectors = rdev->sectors + sb_start - rdev->sb_start; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1720 | if (!num_sectors || num_sectors > max_sectors) |
| 1721 | num_sectors = max_sectors; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1722 | rdev->sb_start = sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1723 | } |
| 1724 | sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1725 | sb->data_size = cpu_to_le64(num_sectors); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1726 | sb->super_offset = rdev->sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1727 | sb->sb_csum = calc_sb_1_csum(sb); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1728 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1729 | rdev->sb_page); |
| 1730 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1731 | return num_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1732 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1733 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 1734 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1735 | [0] = { |
| 1736 | .name = "0.90.0", |
| 1737 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1738 | .load_super = super_90_load, |
| 1739 | .validate_super = super_90_validate, |
| 1740 | .sync_super = super_90_sync, |
| 1741 | .rdev_size_change = super_90_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1742 | }, |
| 1743 | [1] = { |
| 1744 | .name = "md-1", |
| 1745 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1746 | .load_super = super_1_load, |
| 1747 | .validate_super = super_1_validate, |
| 1748 | .sync_super = super_1_sync, |
| 1749 | .rdev_size_change = super_1_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1750 | }, |
| 1751 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1752 | |
| 1753 | static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2) |
| 1754 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1755 | mdk_rdev_t *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1756 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1757 | rcu_read_lock(); |
| 1758 | rdev_for_each_rcu(rdev, mddev1) |
| 1759 | rdev_for_each_rcu(rdev2, mddev2) |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1760 | if (rdev->bdev->bd_contains == |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1761 | rdev2->bdev->bd_contains) { |
| 1762 | rcu_read_unlock(); |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1763 | return 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1764 | } |
| 1765 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1766 | return 0; |
| 1767 | } |
| 1768 | |
| 1769 | static LIST_HEAD(pending_raid_disks); |
| 1770 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1771 | /* |
| 1772 | * Try to register data integrity profile for an mddev |
| 1773 | * |
| 1774 | * This is called when an array is started and after a disk has been kicked |
| 1775 | * from the array. It only succeeds if all working and active component devices |
| 1776 | * are integrity capable with matching profiles. |
| 1777 | */ |
| 1778 | int md_integrity_register(mddev_t *mddev) |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1779 | { |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1780 | mdk_rdev_t *rdev, *reference = NULL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1781 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1782 | if (list_empty(&mddev->disks)) |
| 1783 | return 0; /* nothing to do */ |
| 1784 | if (blk_get_integrity(mddev->gendisk)) |
| 1785 | return 0; /* already registered */ |
| 1786 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 1787 | /* skip spares and non-functional disks */ |
| 1788 | if (test_bit(Faulty, &rdev->flags)) |
| 1789 | continue; |
| 1790 | if (rdev->raid_disk < 0) |
| 1791 | continue; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1792 | if (!reference) { |
| 1793 | /* Use the first rdev as the reference */ |
| 1794 | reference = rdev; |
| 1795 | continue; |
| 1796 | } |
| 1797 | /* does this rdev's profile match the reference profile? */ |
| 1798 | if (blk_integrity_compare(reference->bdev->bd_disk, |
| 1799 | rdev->bdev->bd_disk) < 0) |
| 1800 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1801 | } |
Martin K. Petersen | 89078d5 | 2011-03-28 20:09:12 -0400 | [diff] [blame] | 1802 | if (!reference || !bdev_get_integrity(reference->bdev)) |
| 1803 | return 0; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1804 | /* |
| 1805 | * All component devices are integrity capable and have matching |
| 1806 | * profiles, register the common profile for the md device. |
| 1807 | */ |
| 1808 | if (blk_integrity_register(mddev->gendisk, |
| 1809 | bdev_get_integrity(reference->bdev)) != 0) { |
| 1810 | printk(KERN_ERR "md: failed to register integrity for %s\n", |
| 1811 | mdname(mddev)); |
| 1812 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1813 | } |
Martin K. Petersen | a91a278 | 2011-03-17 11:11:05 +0100 | [diff] [blame] | 1814 | printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev)); |
| 1815 | if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) { |
| 1816 | printk(KERN_ERR "md: failed to create integrity pool for %s\n", |
| 1817 | mdname(mddev)); |
| 1818 | return -EINVAL; |
| 1819 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1820 | return 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1821 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1822 | EXPORT_SYMBOL(md_integrity_register); |
| 1823 | |
| 1824 | /* Disable data integrity if non-capable/non-matching disk is being added */ |
| 1825 | void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 1826 | { |
| 1827 | struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev); |
| 1828 | struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk); |
| 1829 | |
| 1830 | if (!bi_mddev) /* nothing to do */ |
| 1831 | return; |
| 1832 | if (rdev->raid_disk < 0) /* skip spares */ |
| 1833 | return; |
| 1834 | if (bi_rdev && blk_integrity_compare(mddev->gendisk, |
| 1835 | rdev->bdev->bd_disk) >= 0) |
| 1836 | return; |
| 1837 | printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev)); |
| 1838 | blk_integrity_unregister(mddev->gendisk); |
| 1839 | } |
| 1840 | EXPORT_SYMBOL(md_integrity_add_rdev); |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1841 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1842 | static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev) |
| 1843 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1844 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 1845 | struct kobject *ko; |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1846 | char *s; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1847 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1848 | |
| 1849 | if (rdev->mddev) { |
| 1850 | MD_BUG(); |
| 1851 | return -EINVAL; |
| 1852 | } |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1853 | |
| 1854 | /* prevent duplicates */ |
| 1855 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 1856 | return -EEXIST; |
| 1857 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1858 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
| 1859 | if (rdev->sectors && (mddev->dev_sectors == 0 || |
| 1860 | rdev->sectors < mddev->dev_sectors)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1861 | if (mddev->pers) { |
| 1862 | /* Cannot change size, so fail |
| 1863 | * If mddev->level <= 0, then we don't care |
| 1864 | * about aligning sizes (e.g. linear) |
| 1865 | */ |
| 1866 | if (mddev->level > 0) |
| 1867 | return -ENOSPC; |
| 1868 | } else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1869 | mddev->dev_sectors = rdev->sectors; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1870 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1871 | |
| 1872 | /* Verify rdev->desc_nr is unique. |
| 1873 | * If it is -1, assign a free number, else |
| 1874 | * check number is not in use |
| 1875 | */ |
| 1876 | if (rdev->desc_nr < 0) { |
| 1877 | int choice = 0; |
| 1878 | if (mddev->pers) choice = mddev->raid_disks; |
| 1879 | while (find_rdev_nr(mddev, choice)) |
| 1880 | choice++; |
| 1881 | rdev->desc_nr = choice; |
| 1882 | } else { |
| 1883 | if (find_rdev_nr(mddev, rdev->desc_nr)) |
| 1884 | return -EBUSY; |
| 1885 | } |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 1886 | if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
| 1887 | printk(KERN_WARNING "md: %s: array is limited to %d devices\n", |
| 1888 | mdname(mddev), mddev->max_disks); |
| 1889 | return -EBUSY; |
| 1890 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1891 | bdevname(rdev->bdev,b); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1892 | while ( (s=strchr(b, '/')) != NULL) |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1893 | *s = '!'; |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1894 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1895 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1896 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1897 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1898 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1899 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1900 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1901 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 1902 | if (sysfs_create_link(&rdev->kobj, ko, "block")) |
| 1903 | /* failure here is OK */; |
| 1904 | rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 1905 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1906 | list_add_rcu(&rdev->same_set, &mddev->disks); |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1907 | bd_link_disk_holder(rdev->bdev, mddev->gendisk); |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 1908 | |
| 1909 | /* May as well allow recovery to be retried once */ |
| 1910 | mddev->recovery_disabled = 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1911 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1912 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1913 | |
| 1914 | fail: |
| 1915 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", |
| 1916 | b, mdname(mddev)); |
| 1917 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1918 | } |
| 1919 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1920 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1921 | { |
| 1922 | mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work); |
| 1923 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1924 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1925 | } |
| 1926 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1927 | static void unbind_rdev_from_array(mdk_rdev_t * rdev) |
| 1928 | { |
| 1929 | char b[BDEVNAME_SIZE]; |
| 1930 | if (!rdev->mddev) { |
| 1931 | MD_BUG(); |
| 1932 | return; |
| 1933 | } |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 1934 | bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1935 | list_del_rcu(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1936 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 1937 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1938 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 1939 | sysfs_put(rdev->sysfs_state); |
| 1940 | rdev->sysfs_state = NULL; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1941 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1942 | * writing to 'remove' to "dev/state". We also need |
| 1943 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1944 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1945 | synchronize_rcu(); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1946 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 1947 | kobject_get(&rdev->kobj); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 1948 | queue_work(md_misc_wq, &rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1949 | } |
| 1950 | |
| 1951 | /* |
| 1952 | * prevent the device from being mounted, repartitioned or |
| 1953 | * otherwise reused by a RAID array (or any other kernel |
| 1954 | * subsystem), by bd_claiming the device. |
| 1955 | */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1956 | 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] | 1957 | { |
| 1958 | int err = 0; |
| 1959 | struct block_device *bdev; |
| 1960 | char b[BDEVNAME_SIZE]; |
| 1961 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1962 | bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, |
| 1963 | shared ? (mdk_rdev_t *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1964 | if (IS_ERR(bdev)) { |
| 1965 | printk(KERN_ERR "md: could not open %s.\n", |
| 1966 | __bdevname(dev, b)); |
| 1967 | return PTR_ERR(bdev); |
| 1968 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1969 | rdev->bdev = bdev; |
| 1970 | return err; |
| 1971 | } |
| 1972 | |
| 1973 | static void unlock_rdev(mdk_rdev_t *rdev) |
| 1974 | { |
| 1975 | struct block_device *bdev = rdev->bdev; |
| 1976 | rdev->bdev = NULL; |
| 1977 | if (!bdev) |
| 1978 | MD_BUG(); |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1979 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1980 | } |
| 1981 | |
| 1982 | void md_autodetect_dev(dev_t dev); |
| 1983 | |
| 1984 | static void export_rdev(mdk_rdev_t * rdev) |
| 1985 | { |
| 1986 | char b[BDEVNAME_SIZE]; |
| 1987 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 1988 | bdevname(rdev->bdev,b)); |
| 1989 | if (rdev->mddev) |
| 1990 | MD_BUG(); |
| 1991 | free_disk_sb(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1992 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1993 | if (test_bit(AutoDetected, &rdev->flags)) |
| 1994 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1995 | #endif |
| 1996 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1997 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1998 | } |
| 1999 | |
| 2000 | static void kick_rdev_from_array(mdk_rdev_t * rdev) |
| 2001 | { |
| 2002 | unbind_rdev_from_array(rdev); |
| 2003 | export_rdev(rdev); |
| 2004 | } |
| 2005 | |
| 2006 | static void export_array(mddev_t *mddev) |
| 2007 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2008 | mdk_rdev_t *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2009 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2010 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2011 | if (!rdev->mddev) { |
| 2012 | MD_BUG(); |
| 2013 | continue; |
| 2014 | } |
| 2015 | kick_rdev_from_array(rdev); |
| 2016 | } |
| 2017 | if (!list_empty(&mddev->disks)) |
| 2018 | MD_BUG(); |
| 2019 | mddev->raid_disks = 0; |
| 2020 | mddev->major_version = 0; |
| 2021 | } |
| 2022 | |
| 2023 | static void print_desc(mdp_disk_t *desc) |
| 2024 | { |
| 2025 | printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number, |
| 2026 | desc->major,desc->minor,desc->raid_disk,desc->state); |
| 2027 | } |
| 2028 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2029 | static void print_sb_90(mdp_super_t *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2030 | { |
| 2031 | int i; |
| 2032 | |
| 2033 | printk(KERN_INFO |
| 2034 | "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n", |
| 2035 | sb->major_version, sb->minor_version, sb->patch_version, |
| 2036 | sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3, |
| 2037 | sb->ctime); |
| 2038 | printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n", |
| 2039 | sb->level, sb->size, sb->nr_disks, sb->raid_disks, |
| 2040 | sb->md_minor, sb->layout, sb->chunk_size); |
| 2041 | printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d" |
| 2042 | " FD:%d SD:%d CSUM:%08x E:%08lx\n", |
| 2043 | sb->utime, sb->state, sb->active_disks, sb->working_disks, |
| 2044 | sb->failed_disks, sb->spare_disks, |
| 2045 | sb->sb_csum, (unsigned long)sb->events_lo); |
| 2046 | |
| 2047 | printk(KERN_INFO); |
| 2048 | for (i = 0; i < MD_SB_DISKS; i++) { |
| 2049 | mdp_disk_t *desc; |
| 2050 | |
| 2051 | desc = sb->disks + i; |
| 2052 | if (desc->number || desc->major || desc->minor || |
| 2053 | desc->raid_disk || (desc->state && (desc->state != 4))) { |
| 2054 | printk(" D %2d: ", i); |
| 2055 | print_desc(desc); |
| 2056 | } |
| 2057 | } |
| 2058 | printk(KERN_INFO "md: THIS: "); |
| 2059 | print_desc(&sb->this_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2060 | } |
| 2061 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2062 | static void print_sb_1(struct mdp_superblock_1 *sb) |
| 2063 | { |
| 2064 | __u8 *uuid; |
| 2065 | |
| 2066 | uuid = sb->set_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2067 | printk(KERN_INFO |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2068 | "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2069 | "md: Name: \"%s\" CT:%llu\n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2070 | le32_to_cpu(sb->major_version), |
| 2071 | le32_to_cpu(sb->feature_map), |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2072 | uuid, |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2073 | sb->set_name, |
| 2074 | (unsigned long long)le64_to_cpu(sb->ctime) |
| 2075 | & MD_SUPERBLOCK_1_TIME_SEC_MASK); |
| 2076 | |
| 2077 | uuid = sb->device_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2078 | printk(KERN_INFO |
| 2079 | "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] | 2080 | " RO:%llu\n" |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2081 | "md: Dev:%08x UUID: %pU\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2082 | "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n" |
| 2083 | "md: (MaxDev:%u) \n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2084 | le32_to_cpu(sb->level), |
| 2085 | (unsigned long long)le64_to_cpu(sb->size), |
| 2086 | le32_to_cpu(sb->raid_disks), |
| 2087 | le32_to_cpu(sb->layout), |
| 2088 | le32_to_cpu(sb->chunksize), |
| 2089 | (unsigned long long)le64_to_cpu(sb->data_offset), |
| 2090 | (unsigned long long)le64_to_cpu(sb->data_size), |
| 2091 | (unsigned long long)le64_to_cpu(sb->super_offset), |
| 2092 | (unsigned long long)le64_to_cpu(sb->recovery_offset), |
| 2093 | le32_to_cpu(sb->dev_number), |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2094 | uuid, |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2095 | sb->devflags, |
| 2096 | (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK, |
| 2097 | (unsigned long long)le64_to_cpu(sb->events), |
| 2098 | (unsigned long long)le64_to_cpu(sb->resync_offset), |
| 2099 | le32_to_cpu(sb->sb_csum), |
| 2100 | le32_to_cpu(sb->max_dev) |
| 2101 | ); |
| 2102 | } |
| 2103 | |
| 2104 | static void print_rdev(mdk_rdev_t *rdev, int major_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2105 | { |
| 2106 | char b[BDEVNAME_SIZE]; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2107 | printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n", |
| 2108 | bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors, |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2109 | test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags), |
| 2110 | rdev->desc_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2111 | if (rdev->sb_loaded) { |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2112 | printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version); |
| 2113 | switch (major_version) { |
| 2114 | case 0: |
| 2115 | print_sb_90((mdp_super_t*)page_address(rdev->sb_page)); |
| 2116 | break; |
| 2117 | case 1: |
| 2118 | print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page)); |
| 2119 | break; |
| 2120 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2121 | } else |
| 2122 | printk(KERN_INFO "md: no rdev superblock!\n"); |
| 2123 | } |
| 2124 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 2125 | static void md_print_devices(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2126 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2127 | struct list_head *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2128 | mdk_rdev_t *rdev; |
| 2129 | mddev_t *mddev; |
| 2130 | char b[BDEVNAME_SIZE]; |
| 2131 | |
| 2132 | printk("\n"); |
| 2133 | printk("md: **********************************\n"); |
| 2134 | printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n"); |
| 2135 | printk("md: **********************************\n"); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2136 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2137 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 2138 | if (mddev->bitmap) |
| 2139 | bitmap_print_sb(mddev->bitmap); |
| 2140 | else |
| 2141 | printk("%s: ", mdname(mddev)); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2142 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2143 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 2144 | printk("\n"); |
| 2145 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2146 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2147 | print_rdev(rdev, mddev->major_version); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2148 | } |
| 2149 | printk("md: **********************************\n"); |
| 2150 | printk("\n"); |
| 2151 | } |
| 2152 | |
| 2153 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2154 | static void sync_sbs(mddev_t * mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2155 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2156 | /* Update each superblock (in-memory image), but |
| 2157 | * if we are allowed to, skip spares which already |
| 2158 | * have the right event counter, or have one earlier |
| 2159 | * (which would mean they aren't being marked as dirty |
| 2160 | * with the rest of the array) |
| 2161 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2162 | mdk_rdev_t *rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2163 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2164 | if (rdev->sb_events == mddev->events || |
| 2165 | (nospares && |
| 2166 | rdev->raid_disk < 0 && |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2167 | rdev->sb_events+1 == mddev->events)) { |
| 2168 | /* Don't update this superblock */ |
| 2169 | rdev->sb_loaded = 2; |
| 2170 | } else { |
| 2171 | super_types[mddev->major_version]. |
| 2172 | sync_super(mddev, rdev); |
| 2173 | rdev->sb_loaded = 1; |
| 2174 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2175 | } |
| 2176 | } |
| 2177 | |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2178 | static void md_update_sb(mddev_t * mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2179 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2180 | mdk_rdev_t *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2181 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2182 | int nospares = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2183 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2184 | repeat: |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2185 | /* First make sure individual recovery_offsets are correct */ |
| 2186 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 2187 | if (rdev->raid_disk >= 0 && |
| 2188 | mddev->delta_disks >= 0 && |
| 2189 | !test_bit(In_sync, &rdev->flags) && |
| 2190 | mddev->curr_resync_completed > rdev->recovery_offset) |
| 2191 | rdev->recovery_offset = mddev->curr_resync_completed; |
| 2192 | |
| 2193 | } |
Dan Williams | bd52b74 | 2010-08-30 17:33:33 +1000 | [diff] [blame] | 2194 | if (!mddev->persistent) { |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 2195 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2196 | clear_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | d97a41d | 2010-10-28 17:30:20 +1100 | [diff] [blame] | 2197 | if (!mddev->external) |
| 2198 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2199 | wake_up(&mddev->sb_wait); |
| 2200 | return; |
| 2201 | } |
| 2202 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2203 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2204 | |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2205 | mddev->utime = get_seconds(); |
| 2206 | |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2207 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 2208 | force_change = 1; |
| 2209 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 2210 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 2211 | * though if events isn't the right even/odd, we will have to do |
| 2212 | * spares after all |
| 2213 | */ |
| 2214 | nospares = 1; |
| 2215 | if (force_change) |
| 2216 | nospares = 0; |
| 2217 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2218 | /* If the array is degraded, then skipping spares is both |
| 2219 | * dangerous and fairly pointless. |
| 2220 | * Dangerous because a device that was removed from the array |
| 2221 | * might have a event_count that still looks up-to-date, |
| 2222 | * so it can be re-added without a resync. |
| 2223 | * Pointless because if there are any spares to skip, |
| 2224 | * then a recovery will happen and soon that array won't |
| 2225 | * be degraded any more and the spare can go back to sleep then. |
| 2226 | */ |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2227 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2228 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2229 | sync_req = mddev->in_sync; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2230 | |
| 2231 | /* If this is just a dirty<->clean transition, and the array is clean |
| 2232 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2233 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2234 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2235 | && mddev->can_decrease_events |
| 2236 | && mddev->events != 1) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2237 | mddev->events--; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2238 | mddev->can_decrease_events = 0; |
| 2239 | } else { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2240 | /* otherwise we have to go forward and ... */ |
| 2241 | mddev->events ++; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2242 | mddev->can_decrease_events = nospares; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2243 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2244 | |
| 2245 | if (!mddev->events) { |
| 2246 | /* |
| 2247 | * oops, this 64-bit counter should never wrap. |
| 2248 | * Either we are in around ~1 trillion A.C., assuming |
| 2249 | * 1 reboot per second, or we have a bug: |
| 2250 | */ |
| 2251 | MD_BUG(); |
| 2252 | mddev->events --; |
| 2253 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2254 | sync_sbs(mddev, nospares); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2255 | spin_unlock_irq(&mddev->write_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2256 | |
| 2257 | dprintk(KERN_INFO |
| 2258 | "md: updating %s RAID superblock on device (in sync %d)\n", |
| 2259 | mdname(mddev),mddev->in_sync); |
| 2260 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 2261 | bitmap_update_sb(mddev->bitmap); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2262 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2263 | char b[BDEVNAME_SIZE]; |
| 2264 | dprintk(KERN_INFO "md: "); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2265 | if (rdev->sb_loaded != 1) |
| 2266 | continue; /* no noise on spare devices */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2267 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2268 | dprintk("(skipping faulty "); |
| 2269 | |
| 2270 | dprintk("%s ", bdevname(rdev->bdev,b)); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2271 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2272 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2273 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2274 | rdev->sb_page); |
| 2275 | dprintk(KERN_INFO "(write) %s's sb offset: %llu\n", |
| 2276 | bdevname(rdev->bdev,b), |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2277 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2278 | rdev->sb_events = mddev->events; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2279 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2280 | } else |
| 2281 | dprintk(")\n"); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2282 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2283 | /* only need to write one superblock... */ |
| 2284 | break; |
| 2285 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2286 | md_super_wait(mddev); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2287 | /* 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] | 2288 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2289 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2290 | if (mddev->in_sync != sync_req || |
| 2291 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2292 | /* have to write it out again */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2293 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2294 | goto repeat; |
| 2295 | } |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2296 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2297 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2298 | wake_up(&mddev->sb_wait); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 2299 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 2300 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2301 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2302 | } |
| 2303 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 2304 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2305 | * We want to accept with case. For this we use cmd_match. |
| 2306 | */ |
| 2307 | static int cmd_match(const char *cmd, const char *str) |
| 2308 | { |
| 2309 | /* See if cmd, written into a sysfs file, matches |
| 2310 | * str. They must either be the same, or cmd can |
| 2311 | * have a trailing newline |
| 2312 | */ |
| 2313 | while (*cmd && *str && *cmd == *str) { |
| 2314 | cmd++; |
| 2315 | str++; |
| 2316 | } |
| 2317 | if (*cmd == '\n') |
| 2318 | cmd++; |
| 2319 | if (*str || *cmd) |
| 2320 | return 0; |
| 2321 | return 1; |
| 2322 | } |
| 2323 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2324 | struct rdev_sysfs_entry { |
| 2325 | struct attribute attr; |
| 2326 | ssize_t (*show)(mdk_rdev_t *, char *); |
| 2327 | ssize_t (*store)(mdk_rdev_t *, const char *, size_t); |
| 2328 | }; |
| 2329 | |
| 2330 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2331 | state_show(mdk_rdev_t *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2332 | { |
| 2333 | char *sep = ""; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2334 | size_t len = 0; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2335 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2336 | if (test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2337 | len+= sprintf(page+len, "%sfaulty",sep); |
| 2338 | sep = ","; |
| 2339 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2340 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2341 | len += sprintf(page+len, "%sin_sync",sep); |
| 2342 | sep = ","; |
| 2343 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2344 | if (test_bit(WriteMostly, &rdev->flags)) { |
| 2345 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 2346 | sep = ","; |
| 2347 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2348 | if (test_bit(Blocked, &rdev->flags)) { |
| 2349 | len += sprintf(page+len, "%sblocked", sep); |
| 2350 | sep = ","; |
| 2351 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2352 | if (!test_bit(Faulty, &rdev->flags) && |
| 2353 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2354 | len += sprintf(page+len, "%sspare", sep); |
| 2355 | sep = ","; |
| 2356 | } |
| 2357 | return len+sprintf(page+len, "\n"); |
| 2358 | } |
| 2359 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2360 | static ssize_t |
| 2361 | state_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2362 | { |
| 2363 | /* can write |
| 2364 | * faulty - simulates and error |
| 2365 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2366 | * writemostly - sets write_mostly |
| 2367 | * -writemostly - clears write_mostly |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2368 | * blocked - sets the Blocked flag |
| 2369 | * -blocked - clears the Blocked flag |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2370 | * insync - sets Insync providing device isn't active |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2371 | */ |
| 2372 | int err = -EINVAL; |
| 2373 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 2374 | md_error(rdev->mddev, rdev); |
| 2375 | err = 0; |
| 2376 | } else if (cmd_match(buf, "remove")) { |
| 2377 | if (rdev->raid_disk >= 0) |
| 2378 | err = -EBUSY; |
| 2379 | else { |
| 2380 | mddev_t *mddev = rdev->mddev; |
| 2381 | kick_rdev_from_array(rdev); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2382 | if (mddev->pers) |
| 2383 | md_update_sb(mddev, 1); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2384 | md_new_event(mddev); |
| 2385 | err = 0; |
| 2386 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2387 | } else if (cmd_match(buf, "writemostly")) { |
| 2388 | set_bit(WriteMostly, &rdev->flags); |
| 2389 | err = 0; |
| 2390 | } else if (cmd_match(buf, "-writemostly")) { |
| 2391 | clear_bit(WriteMostly, &rdev->flags); |
| 2392 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2393 | } else if (cmd_match(buf, "blocked")) { |
| 2394 | set_bit(Blocked, &rdev->flags); |
| 2395 | err = 0; |
| 2396 | } else if (cmd_match(buf, "-blocked")) { |
| 2397 | clear_bit(Blocked, &rdev->flags); |
| 2398 | wake_up(&rdev->blocked_wait); |
| 2399 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2400 | md_wakeup_thread(rdev->mddev->thread); |
| 2401 | |
| 2402 | err = 0; |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2403 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { |
| 2404 | set_bit(In_sync, &rdev->flags); |
| 2405 | err = 0; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2406 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2407 | if (!err) |
| 2408 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2409 | return err ? err : len; |
| 2410 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2411 | static struct rdev_sysfs_entry rdev_state = |
| 2412 | __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2413 | |
| 2414 | static ssize_t |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2415 | errors_show(mdk_rdev_t *rdev, char *page) |
| 2416 | { |
| 2417 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 2418 | } |
| 2419 | |
| 2420 | static ssize_t |
| 2421 | errors_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2422 | { |
| 2423 | char *e; |
| 2424 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2425 | if (*buf && (*e == 0 || *e == '\n')) { |
| 2426 | atomic_set(&rdev->corrected_errors, n); |
| 2427 | return len; |
| 2428 | } |
| 2429 | return -EINVAL; |
| 2430 | } |
| 2431 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2432 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2433 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2434 | static ssize_t |
| 2435 | slot_show(mdk_rdev_t *rdev, char *page) |
| 2436 | { |
| 2437 | if (rdev->raid_disk < 0) |
| 2438 | return sprintf(page, "none\n"); |
| 2439 | else |
| 2440 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 2441 | } |
| 2442 | |
| 2443 | static ssize_t |
| 2444 | slot_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2445 | { |
| 2446 | char *e; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2447 | int err; |
| 2448 | char nm[20]; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2449 | int slot = simple_strtoul(buf, &e, 10); |
| 2450 | if (strncmp(buf, "none", 4)==0) |
| 2451 | slot = -1; |
| 2452 | else if (e==buf || (*e && *e!= '\n')) |
| 2453 | return -EINVAL; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2454 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2455 | /* Setting 'slot' on an active array requires also |
| 2456 | * updating the 'rd%d' link, and communicating |
| 2457 | * with the personality with ->hot_*_disk. |
| 2458 | * For now we only support removing |
| 2459 | * failed/spare devices. This normally happens automatically, |
| 2460 | * but not when the metadata is externally managed. |
| 2461 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2462 | if (rdev->raid_disk == -1) |
| 2463 | return -EEXIST; |
| 2464 | /* personality does all needed checks */ |
| 2465 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2466 | return -EINVAL; |
| 2467 | err = rdev->mddev->pers-> |
| 2468 | hot_remove_disk(rdev->mddev, rdev->raid_disk); |
| 2469 | if (err) |
| 2470 | return err; |
| 2471 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2472 | sysfs_remove_link(&rdev->mddev->kobj, nm); |
Maciej Trela | b710310 | 2010-04-14 16:58:16 +1000 | [diff] [blame] | 2473 | rdev->raid_disk = -1; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2474 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2475 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2476 | } else if (rdev->mddev->pers) { |
| 2477 | mdk_rdev_t *rdev2; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2478 | /* Activating a spare .. or possibly reactivating |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2479 | * if we ever get bitmaps working here. |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2480 | */ |
| 2481 | |
| 2482 | if (rdev->raid_disk != -1) |
| 2483 | return -EBUSY; |
| 2484 | |
NeilBrown | c6751b2 | 2011-02-02 11:57:13 +1100 | [diff] [blame] | 2485 | if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery)) |
| 2486 | return -EBUSY; |
| 2487 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2488 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2489 | return -EINVAL; |
| 2490 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2491 | list_for_each_entry(rdev2, &rdev->mddev->disks, same_set) |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2492 | if (rdev2->raid_disk == slot) |
| 2493 | return -EEXIST; |
| 2494 | |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2495 | if (slot >= rdev->mddev->raid_disks && |
| 2496 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
| 2497 | return -ENOSPC; |
| 2498 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2499 | rdev->raid_disk = slot; |
| 2500 | if (test_bit(In_sync, &rdev->flags)) |
| 2501 | rdev->saved_raid_disk = slot; |
| 2502 | else |
| 2503 | rdev->saved_raid_disk = -1; |
| 2504 | err = rdev->mddev->pers-> |
| 2505 | hot_add_disk(rdev->mddev, rdev); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 2506 | if (err) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2507 | rdev->raid_disk = -1; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2508 | return err; |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 2509 | } else |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2510 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2511 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2512 | if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2513 | /* failure here is OK */; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2514 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2515 | } else { |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2516 | if (slot >= rdev->mddev->raid_disks && |
| 2517 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2518 | return -ENOSPC; |
| 2519 | rdev->raid_disk = slot; |
| 2520 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2521 | clear_bit(Faulty, &rdev->flags); |
| 2522 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2523 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2524 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2525 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2526 | return len; |
| 2527 | } |
| 2528 | |
| 2529 | |
| 2530 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2531 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2532 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2533 | static ssize_t |
| 2534 | offset_show(mdk_rdev_t *rdev, char *page) |
| 2535 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2536 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2537 | } |
| 2538 | |
| 2539 | static ssize_t |
| 2540 | offset_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2541 | { |
| 2542 | char *e; |
| 2543 | unsigned long long offset = simple_strtoull(buf, &e, 10); |
| 2544 | if (e==buf || (*e && *e != '\n')) |
| 2545 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2546 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2547 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2548 | if (rdev->sectors && rdev->mddev->external) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2549 | /* Must set offset before size, so overlap checks |
| 2550 | * can be sane */ |
| 2551 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2552 | rdev->data_offset = offset; |
| 2553 | return len; |
| 2554 | } |
| 2555 | |
| 2556 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2557 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2558 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2559 | static ssize_t |
| 2560 | rdev_size_show(mdk_rdev_t *rdev, char *page) |
| 2561 | { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2562 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2563 | } |
| 2564 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2565 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 2566 | { |
| 2567 | /* check if two start/length pairs overlap */ |
| 2568 | if (s1+l1 <= s2) |
| 2569 | return 0; |
| 2570 | if (s2+l2 <= s1) |
| 2571 | return 0; |
| 2572 | return 1; |
| 2573 | } |
| 2574 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2575 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
| 2576 | { |
| 2577 | unsigned long long blocks; |
| 2578 | sector_t new; |
| 2579 | |
| 2580 | if (strict_strtoull(buf, 10, &blocks) < 0) |
| 2581 | return -EINVAL; |
| 2582 | |
| 2583 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) |
| 2584 | return -EINVAL; /* sector conversion overflow */ |
| 2585 | |
| 2586 | new = blocks * 2; |
| 2587 | if (new != blocks * 2) |
| 2588 | return -EINVAL; /* unsigned long long to sector_t overflow */ |
| 2589 | |
| 2590 | *sectors = new; |
| 2591 | return 0; |
| 2592 | } |
| 2593 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2594 | static ssize_t |
| 2595 | rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2596 | { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2597 | mddev_t *my_mddev = rdev->mddev; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2598 | sector_t oldsectors = rdev->sectors; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2599 | sector_t sectors; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2600 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2601 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2602 | return -EINVAL; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2603 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2604 | if (my_mddev->persistent) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2605 | sectors = super_types[my_mddev->major_version]. |
| 2606 | rdev_size_change(rdev, sectors); |
| 2607 | if (!sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2608 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2609 | } else if (!sectors) |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 2610 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) - |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2611 | rdev->data_offset; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2612 | } |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2613 | if (sectors < my_mddev->dev_sectors) |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 2614 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2615 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2616 | rdev->sectors = sectors; |
| 2617 | if (sectors > oldsectors && my_mddev->external) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2618 | /* need to check that all other rdevs with the same ->bdev |
| 2619 | * do not overlap. We need to unlock the mddev to avoid |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2620 | * a deadlock. We have already changed rdev->sectors, and if |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2621 | * we have to change it back, we will have the lock again. |
| 2622 | */ |
| 2623 | mddev_t *mddev; |
| 2624 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2625 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2626 | |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2627 | mddev_unlock(my_mddev); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2628 | for_each_mddev(mddev, tmp) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2629 | mdk_rdev_t *rdev2; |
| 2630 | |
| 2631 | mddev_lock(mddev); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2632 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
NeilBrown | f21e9ff | 2011-01-31 12:10:09 +1100 | [diff] [blame] | 2633 | if (rdev->bdev == rdev2->bdev && |
| 2634 | rdev != rdev2 && |
| 2635 | overlaps(rdev->data_offset, rdev->sectors, |
| 2636 | rdev2->data_offset, |
| 2637 | rdev2->sectors)) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2638 | overlap = 1; |
| 2639 | break; |
| 2640 | } |
| 2641 | mddev_unlock(mddev); |
| 2642 | if (overlap) { |
| 2643 | mddev_put(mddev); |
| 2644 | break; |
| 2645 | } |
| 2646 | } |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2647 | mddev_lock(my_mddev); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2648 | if (overlap) { |
| 2649 | /* Someone else could have slipped in a size |
| 2650 | * change here, but doing so is just silly. |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2651 | * We put oldsectors back because we *know* it is |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2652 | * safe, and trust userspace not to race with |
| 2653 | * itself |
| 2654 | */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2655 | rdev->sectors = oldsectors; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2656 | return -EBUSY; |
| 2657 | } |
| 2658 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2659 | return len; |
| 2660 | } |
| 2661 | |
| 2662 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2663 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2664 | |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2665 | |
| 2666 | static ssize_t recovery_start_show(mdk_rdev_t *rdev, char *page) |
| 2667 | { |
| 2668 | unsigned long long recovery_start = rdev->recovery_offset; |
| 2669 | |
| 2670 | if (test_bit(In_sync, &rdev->flags) || |
| 2671 | recovery_start == MaxSector) |
| 2672 | return sprintf(page, "none\n"); |
| 2673 | |
| 2674 | return sprintf(page, "%llu\n", recovery_start); |
| 2675 | } |
| 2676 | |
| 2677 | static ssize_t recovery_start_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2678 | { |
| 2679 | unsigned long long recovery_start; |
| 2680 | |
| 2681 | if (cmd_match(buf, "none")) |
| 2682 | recovery_start = MaxSector; |
| 2683 | else if (strict_strtoull(buf, 10, &recovery_start)) |
| 2684 | return -EINVAL; |
| 2685 | |
| 2686 | if (rdev->mddev->pers && |
| 2687 | rdev->raid_disk >= 0) |
| 2688 | return -EBUSY; |
| 2689 | |
| 2690 | rdev->recovery_offset = recovery_start; |
| 2691 | if (recovery_start == MaxSector) |
| 2692 | set_bit(In_sync, &rdev->flags); |
| 2693 | else |
| 2694 | clear_bit(In_sync, &rdev->flags); |
| 2695 | return len; |
| 2696 | } |
| 2697 | |
| 2698 | static struct rdev_sysfs_entry rdev_recovery_start = |
| 2699 | __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store); |
| 2700 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2701 | static struct attribute *rdev_default_attrs[] = { |
| 2702 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2703 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2704 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2705 | &rdev_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2706 | &rdev_size.attr, |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2707 | &rdev_recovery_start.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2708 | NULL, |
| 2709 | }; |
| 2710 | static ssize_t |
| 2711 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 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 | mddev_t *mddev = rdev->mddev; |
| 2716 | ssize_t rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2717 | |
| 2718 | if (!entry->show) |
| 2719 | return -EIO; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2720 | |
| 2721 | rv = mddev ? mddev_lock(mddev) : -EBUSY; |
| 2722 | if (!rv) { |
| 2723 | if (rdev->mddev == NULL) |
| 2724 | rv = -EBUSY; |
| 2725 | else |
| 2726 | rv = entry->show(rdev, page); |
| 2727 | mddev_unlock(mddev); |
| 2728 | } |
| 2729 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2730 | } |
| 2731 | |
| 2732 | static ssize_t |
| 2733 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 2734 | const char *page, size_t length) |
| 2735 | { |
| 2736 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2737 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2738 | ssize_t rv; |
| 2739 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2740 | |
| 2741 | if (!entry->store) |
| 2742 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 2743 | if (!capable(CAP_SYS_ADMIN)) |
| 2744 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2745 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2746 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2747 | if (rdev->mddev == NULL) |
| 2748 | rv = -EBUSY; |
| 2749 | else |
| 2750 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 2751 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2752 | } |
| 2753 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2754 | } |
| 2755 | |
| 2756 | static void rdev_free(struct kobject *ko) |
| 2757 | { |
| 2758 | mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj); |
| 2759 | kfree(rdev); |
| 2760 | } |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 2761 | static const struct sysfs_ops rdev_sysfs_ops = { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2762 | .show = rdev_attr_show, |
| 2763 | .store = rdev_attr_store, |
| 2764 | }; |
| 2765 | static struct kobj_type rdev_ktype = { |
| 2766 | .release = rdev_free, |
| 2767 | .sysfs_ops = &rdev_sysfs_ops, |
| 2768 | .default_attrs = rdev_default_attrs, |
| 2769 | }; |
| 2770 | |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 2771 | void md_rdev_init(mdk_rdev_t *rdev) |
| 2772 | { |
| 2773 | rdev->desc_nr = -1; |
| 2774 | rdev->saved_raid_disk = -1; |
| 2775 | rdev->raid_disk = -1; |
| 2776 | rdev->flags = 0; |
| 2777 | rdev->data_offset = 0; |
| 2778 | rdev->sb_events = 0; |
| 2779 | rdev->last_read_error.tv_sec = 0; |
| 2780 | rdev->last_read_error.tv_nsec = 0; |
| 2781 | atomic_set(&rdev->nr_pending, 0); |
| 2782 | atomic_set(&rdev->read_errors, 0); |
| 2783 | atomic_set(&rdev->corrected_errors, 0); |
| 2784 | |
| 2785 | INIT_LIST_HEAD(&rdev->same_set); |
| 2786 | init_waitqueue_head(&rdev->blocked_wait); |
| 2787 | } |
| 2788 | EXPORT_SYMBOL_GPL(md_rdev_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2789 | /* |
| 2790 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 2791 | * |
| 2792 | * mark the device faulty if: |
| 2793 | * |
| 2794 | * - the device is nonexistent (zero size) |
| 2795 | * - the device has no valid superblock |
| 2796 | * |
| 2797 | * a faulty rdev _never_ has rdev->sb set. |
| 2798 | */ |
| 2799 | static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor) |
| 2800 | { |
| 2801 | char b[BDEVNAME_SIZE]; |
| 2802 | int err; |
| 2803 | mdk_rdev_t *rdev; |
| 2804 | sector_t size; |
| 2805 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 2806 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2807 | if (!rdev) { |
| 2808 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 2809 | return ERR_PTR(-ENOMEM); |
| 2810 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2811 | |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 2812 | md_rdev_init(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2813 | if ((err = alloc_disk_sb(rdev))) |
| 2814 | goto abort_free; |
| 2815 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2816 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2817 | if (err) |
| 2818 | goto abort_free; |
| 2819 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2820 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2821 | |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 2822 | size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2823 | if (!size) { |
| 2824 | printk(KERN_WARNING |
| 2825 | "md: %s has zero or unknown size, marking faulty!\n", |
| 2826 | bdevname(rdev->bdev,b)); |
| 2827 | err = -EINVAL; |
| 2828 | goto abort_free; |
| 2829 | } |
| 2830 | |
| 2831 | if (super_format >= 0) { |
| 2832 | err = super_types[super_format]. |
| 2833 | load_super(rdev, NULL, super_minor); |
| 2834 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 2835 | printk(KERN_WARNING |
| 2836 | "md: %s does not have a valid v%d.%d " |
| 2837 | "superblock, not importing!\n", |
| 2838 | bdevname(rdev->bdev,b), |
| 2839 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2840 | goto abort_free; |
| 2841 | } |
| 2842 | if (err < 0) { |
| 2843 | printk(KERN_WARNING |
| 2844 | "md: could not read %s's sb, not importing!\n", |
| 2845 | bdevname(rdev->bdev,b)); |
| 2846 | goto abort_free; |
| 2847 | } |
| 2848 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2849 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2850 | return rdev; |
| 2851 | |
| 2852 | abort_free: |
| 2853 | if (rdev->sb_page) { |
| 2854 | if (rdev->bdev) |
| 2855 | unlock_rdev(rdev); |
| 2856 | free_disk_sb(rdev); |
| 2857 | } |
| 2858 | kfree(rdev); |
| 2859 | return ERR_PTR(err); |
| 2860 | } |
| 2861 | |
| 2862 | /* |
| 2863 | * Check a full RAID array for plausibility |
| 2864 | */ |
| 2865 | |
| 2866 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 2867 | static void analyze_sbs(mddev_t * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2868 | { |
| 2869 | int i; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2870 | mdk_rdev_t *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2871 | char b[BDEVNAME_SIZE]; |
| 2872 | |
| 2873 | freshest = NULL; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2874 | rdev_for_each(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2875 | switch (super_types[mddev->major_version]. |
| 2876 | load_super(rdev, freshest, mddev->minor_version)) { |
| 2877 | case 1: |
| 2878 | freshest = rdev; |
| 2879 | break; |
| 2880 | case 0: |
| 2881 | break; |
| 2882 | default: |
| 2883 | printk( KERN_ERR \ |
| 2884 | "md: fatal superblock inconsistency in %s" |
| 2885 | " -- removing from array\n", |
| 2886 | bdevname(rdev->bdev,b)); |
| 2887 | kick_rdev_from_array(rdev); |
| 2888 | } |
| 2889 | |
| 2890 | |
| 2891 | super_types[mddev->major_version]. |
| 2892 | validate_super(mddev, freshest); |
| 2893 | |
| 2894 | i = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2895 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 2896 | if (mddev->max_disks && |
| 2897 | (rdev->desc_nr >= mddev->max_disks || |
| 2898 | i > mddev->max_disks)) { |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 2899 | printk(KERN_WARNING |
| 2900 | "md: %s: %s: only %d devices permitted\n", |
| 2901 | mdname(mddev), bdevname(rdev->bdev, b), |
| 2902 | mddev->max_disks); |
| 2903 | kick_rdev_from_array(rdev); |
| 2904 | continue; |
| 2905 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2906 | if (rdev != freshest) |
| 2907 | if (super_types[mddev->major_version]. |
| 2908 | validate_super(mddev, rdev)) { |
| 2909 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 2910 | " from array!\n", |
| 2911 | bdevname(rdev->bdev,b)); |
| 2912 | kick_rdev_from_array(rdev); |
| 2913 | continue; |
| 2914 | } |
| 2915 | if (mddev->level == LEVEL_MULTIPATH) { |
| 2916 | rdev->desc_nr = i++; |
| 2917 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2918 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 2919 | } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2920 | rdev->raid_disk = -1; |
| 2921 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2922 | } |
| 2923 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2924 | } |
| 2925 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2926 | /* Read a fixed-point number. |
| 2927 | * Numbers in sysfs attributes should be in "standard" units where |
| 2928 | * possible, so time should be in seconds. |
| 2929 | * However we internally use a a much smaller unit such as |
| 2930 | * milliseconds or jiffies. |
| 2931 | * This function takes a decimal number with a possible fractional |
| 2932 | * component, and produces an integer which is the result of |
| 2933 | * multiplying that number by 10^'scale'. |
| 2934 | * all without any floating-point arithmetic. |
| 2935 | */ |
| 2936 | int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale) |
| 2937 | { |
| 2938 | unsigned long result = 0; |
| 2939 | long decimals = -1; |
| 2940 | while (isdigit(*cp) || (*cp == '.' && decimals < 0)) { |
| 2941 | if (*cp == '.') |
| 2942 | decimals = 0; |
| 2943 | else if (decimals < scale) { |
| 2944 | unsigned int value; |
| 2945 | value = *cp - '0'; |
| 2946 | result = result * 10 + value; |
| 2947 | if (decimals >= 0) |
| 2948 | decimals++; |
| 2949 | } |
| 2950 | cp++; |
| 2951 | } |
| 2952 | if (*cp == '\n') |
| 2953 | cp++; |
| 2954 | if (*cp) |
| 2955 | return -EINVAL; |
| 2956 | if (decimals < 0) |
| 2957 | decimals = 0; |
| 2958 | while (decimals < scale) { |
| 2959 | result *= 10; |
| 2960 | decimals ++; |
| 2961 | } |
| 2962 | *res = result; |
| 2963 | return 0; |
| 2964 | } |
| 2965 | |
| 2966 | |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2967 | static void md_safemode_timeout(unsigned long data); |
| 2968 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2969 | static ssize_t |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2970 | safe_delay_show(mddev_t *mddev, char *page) |
| 2971 | { |
| 2972 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 2973 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 2974 | } |
| 2975 | static ssize_t |
| 2976 | safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len) |
| 2977 | { |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2978 | unsigned long msec; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 2979 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2980 | if (strict_strtoul_scaled(cbuf, &msec, 3) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2981 | return -EINVAL; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2982 | if (msec == 0) |
| 2983 | mddev->safemode_delay = 0; |
| 2984 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2985 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2986 | mddev->safemode_delay = (msec*HZ)/1000; |
| 2987 | if (mddev->safemode_delay == 0) |
| 2988 | mddev->safemode_delay = 1; |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2989 | if (mddev->safemode_delay < old_delay) |
| 2990 | md_safemode_timeout((unsigned long)mddev); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2991 | } |
| 2992 | return len; |
| 2993 | } |
| 2994 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2995 | __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] | 2996 | |
| 2997 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2998 | level_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2999 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3000 | struct mdk_personality *p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3001 | if (p) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3002 | return sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3003 | else if (mddev->clevel[0]) |
| 3004 | return sprintf(page, "%s\n", mddev->clevel); |
| 3005 | else if (mddev->level != LEVEL_NONE) |
| 3006 | return sprintf(page, "%d\n", mddev->level); |
| 3007 | else |
| 3008 | return 0; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3009 | } |
| 3010 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3011 | static ssize_t |
| 3012 | level_store(mddev_t *mddev, const char *buf, size_t len) |
| 3013 | { |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3014 | char clevel[16]; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 3015 | ssize_t rv = len; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3016 | struct mdk_personality *pers; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3017 | long level; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3018 | void *priv; |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3019 | mdk_rdev_t *rdev; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3020 | |
| 3021 | if (mddev->pers == NULL) { |
| 3022 | if (len == 0) |
| 3023 | return 0; |
| 3024 | if (len >= sizeof(mddev->clevel)) |
| 3025 | return -ENOSPC; |
| 3026 | strncpy(mddev->clevel, buf, len); |
| 3027 | if (mddev->clevel[len-1] == '\n') |
| 3028 | len--; |
| 3029 | mddev->clevel[len] = 0; |
| 3030 | mddev->level = LEVEL_NONE; |
| 3031 | return rv; |
| 3032 | } |
| 3033 | |
| 3034 | /* request to change the personality. Need to ensure: |
| 3035 | * - array is not engaged in resync/recovery/reshape |
| 3036 | * - old personality can be suspended |
| 3037 | * - new personality will access other array. |
| 3038 | */ |
| 3039 | |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 3040 | if (mddev->sync_thread || |
| 3041 | mddev->reshape_position != MaxSector || |
| 3042 | mddev->sysfs_active) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3043 | return -EBUSY; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3044 | |
| 3045 | if (!mddev->pers->quiesce) { |
| 3046 | printk(KERN_WARNING "md: %s: %s does not support online personality change\n", |
| 3047 | mdname(mddev), mddev->pers->name); |
| 3048 | return -EINVAL; |
| 3049 | } |
| 3050 | |
| 3051 | /* Now find the new personality */ |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3052 | if (len == 0 || len >= sizeof(clevel)) |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3053 | return -EINVAL; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3054 | strncpy(clevel, buf, len); |
| 3055 | if (clevel[len-1] == '\n') |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3056 | len--; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3057 | clevel[len] = 0; |
| 3058 | if (strict_strtol(clevel, 10, &level)) |
| 3059 | level = LEVEL_NONE; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3060 | |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3061 | if (request_module("md-%s", clevel) != 0) |
| 3062 | request_module("md-level-%s", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3063 | spin_lock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3064 | pers = find_pers(level, clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3065 | if (!pers || !try_module_get(pers->owner)) { |
| 3066 | spin_unlock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3067 | printk(KERN_WARNING "md: personality %s not loaded\n", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3068 | return -EINVAL; |
| 3069 | } |
| 3070 | spin_unlock(&pers_lock); |
| 3071 | |
| 3072 | if (pers == mddev->pers) { |
| 3073 | /* Nothing to do! */ |
| 3074 | module_put(pers->owner); |
| 3075 | return rv; |
| 3076 | } |
| 3077 | if (!pers->takeover) { |
| 3078 | module_put(pers->owner); |
| 3079 | printk(KERN_WARNING "md: %s: %s does not support personality takeover\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3080 | mdname(mddev), clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3081 | return -EINVAL; |
| 3082 | } |
| 3083 | |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3084 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 3085 | rdev->new_raid_disk = rdev->raid_disk; |
| 3086 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3087 | /* ->takeover must set new_* and/or delta_disks |
| 3088 | * if it succeeds, and may set them when it fails. |
| 3089 | */ |
| 3090 | priv = pers->takeover(mddev); |
| 3091 | if (IS_ERR(priv)) { |
| 3092 | mddev->new_level = mddev->level; |
| 3093 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3094 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3095 | mddev->raid_disks -= mddev->delta_disks; |
| 3096 | mddev->delta_disks = 0; |
| 3097 | module_put(pers->owner); |
| 3098 | printk(KERN_WARNING "md: %s: %s would not accept array\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3099 | mdname(mddev), clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3100 | return PTR_ERR(priv); |
| 3101 | } |
| 3102 | |
| 3103 | /* Looks like we have a winner */ |
| 3104 | mddev_suspend(mddev); |
| 3105 | mddev->pers->stop(mddev); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 3106 | |
| 3107 | if (mddev->pers->sync_request == NULL && |
| 3108 | pers->sync_request != NULL) { |
| 3109 | /* need to add the md_redundancy_group */ |
| 3110 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 3111 | printk(KERN_WARNING |
| 3112 | "md: cannot register extra attributes for %s\n", |
| 3113 | mdname(mddev)); |
NeilBrown | 19fdb9e | 2010-05-22 08:31:36 +1000 | [diff] [blame] | 3114 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action"); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 3115 | } |
| 3116 | if (mddev->pers->sync_request != NULL && |
| 3117 | pers->sync_request == NULL) { |
| 3118 | /* need to remove the md_redundancy_group */ |
| 3119 | if (mddev->to_remove == NULL) |
| 3120 | mddev->to_remove = &md_redundancy_group; |
| 3121 | } |
| 3122 | |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 3123 | if (mddev->pers->sync_request == NULL && |
| 3124 | mddev->external) { |
| 3125 | /* We are converting from a no-redundancy array |
| 3126 | * to a redundancy array and metadata is managed |
| 3127 | * externally so we need to be sure that writes |
| 3128 | * won't block due to a need to transition |
| 3129 | * clean->dirty |
| 3130 | * until external management is started. |
| 3131 | */ |
| 3132 | mddev->in_sync = 0; |
| 3133 | mddev->safemode_delay = 0; |
| 3134 | mddev->safemode = 0; |
| 3135 | } |
| 3136 | |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3137 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 3138 | char nm[20]; |
| 3139 | if (rdev->raid_disk < 0) |
| 3140 | continue; |
NeilBrown | bf2cb0d | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 3141 | if (rdev->new_raid_disk >= mddev->raid_disks) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3142 | rdev->new_raid_disk = -1; |
| 3143 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3144 | continue; |
| 3145 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 3146 | sysfs_remove_link(&mddev->kobj, nm); |
| 3147 | } |
| 3148 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 3149 | if (rdev->raid_disk < 0) |
| 3150 | continue; |
| 3151 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3152 | continue; |
| 3153 | rdev->raid_disk = rdev->new_raid_disk; |
| 3154 | if (rdev->raid_disk < 0) |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3155 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3156 | else { |
| 3157 | char nm[20]; |
| 3158 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 3159 | if(sysfs_create_link(&mddev->kobj, &rdev->kobj, nm)) |
| 3160 | printk("md: cannot register %s for %s after level change\n", |
| 3161 | nm, mdname(mddev)); |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3162 | } |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3163 | } |
| 3164 | |
| 3165 | module_put(mddev->pers->owner); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3166 | mddev->pers = pers; |
| 3167 | mddev->private = priv; |
| 3168 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
| 3169 | mddev->level = mddev->new_level; |
| 3170 | mddev->layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3171 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3172 | mddev->delta_disks = 0; |
Trela, Maciej | 9af204c | 2010-03-08 16:02:44 +1100 | [diff] [blame] | 3173 | if (mddev->pers->sync_request == NULL) { |
| 3174 | /* this is now an array without redundancy, so |
| 3175 | * it must always be in_sync |
| 3176 | */ |
| 3177 | mddev->in_sync = 1; |
| 3178 | del_timer_sync(&mddev->safemode_timer); |
| 3179 | } |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3180 | pers->run(mddev); |
| 3181 | mddev_resume(mddev); |
| 3182 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 3183 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3184 | md_wakeup_thread(mddev->thread); |
Maciej Trela | 5cac7861 | 2010-04-14 17:17:39 +1000 | [diff] [blame] | 3185 | sysfs_notify(&mddev->kobj, NULL, "level"); |
Dan Williams | bb7f8d2 | 2010-05-01 18:14:57 -0700 | [diff] [blame] | 3186 | md_new_event(mddev); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3187 | return rv; |
| 3188 | } |
| 3189 | |
| 3190 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3191 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3192 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3193 | |
| 3194 | static ssize_t |
| 3195 | layout_show(mddev_t *mddev, char *page) |
| 3196 | { |
| 3197 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3198 | if (mddev->reshape_position != MaxSector && |
| 3199 | mddev->layout != mddev->new_layout) |
| 3200 | return sprintf(page, "%d (%d)\n", |
| 3201 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3202 | return sprintf(page, "%d\n", mddev->layout); |
| 3203 | } |
| 3204 | |
| 3205 | static ssize_t |
| 3206 | layout_store(mddev_t *mddev, const char *buf, size_t len) |
| 3207 | { |
| 3208 | char *e; |
| 3209 | unsigned long n = simple_strtoul(buf, &e, 10); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3210 | |
| 3211 | if (!*buf || (*e && *e != '\n')) |
| 3212 | return -EINVAL; |
| 3213 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3214 | if (mddev->pers) { |
| 3215 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3216 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3217 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3218 | mddev->new_layout = n; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3219 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3220 | if (err) { |
| 3221 | mddev->new_layout = mddev->layout; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3222 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3223 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3224 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3225 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3226 | if (mddev->reshape_position == MaxSector) |
| 3227 | mddev->layout = n; |
| 3228 | } |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3229 | return len; |
| 3230 | } |
| 3231 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3232 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3233 | |
| 3234 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3235 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3236 | raid_disks_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3237 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 3238 | if (mddev->raid_disks == 0) |
| 3239 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3240 | if (mddev->reshape_position != MaxSector && |
| 3241 | mddev->delta_disks != 0) |
| 3242 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 3243 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3244 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 3245 | } |
| 3246 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3247 | static int update_raid_disks(mddev_t *mddev, int raid_disks); |
| 3248 | |
| 3249 | static ssize_t |
| 3250 | raid_disks_store(mddev_t *mddev, const char *buf, size_t len) |
| 3251 | { |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3252 | char *e; |
| 3253 | int rv = 0; |
| 3254 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3255 | |
| 3256 | if (!*buf || (*e && *e != '\n')) |
| 3257 | return -EINVAL; |
| 3258 | |
| 3259 | if (mddev->pers) |
| 3260 | rv = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3261 | else if (mddev->reshape_position != MaxSector) { |
| 3262 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
| 3263 | mddev->delta_disks = n - olddisks; |
| 3264 | mddev->raid_disks = n; |
| 3265 | } else |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3266 | mddev->raid_disks = n; |
| 3267 | return rv ? rv : len; |
| 3268 | } |
| 3269 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3270 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3271 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3272 | static ssize_t |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3273 | chunk_size_show(mddev_t *mddev, char *page) |
| 3274 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3275 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3276 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 3277 | return sprintf(page, "%d (%d)\n", |
| 3278 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3279 | mddev->chunk_sectors << 9); |
| 3280 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3281 | } |
| 3282 | |
| 3283 | static ssize_t |
| 3284 | chunk_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3285 | { |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3286 | char *e; |
| 3287 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3288 | |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3289 | if (!*buf || (*e && *e != '\n')) |
| 3290 | return -EINVAL; |
| 3291 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3292 | if (mddev->pers) { |
| 3293 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3294 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3295 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3296 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3297 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3298 | if (err) { |
| 3299 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3300 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3301 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3302 | } else { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3303 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3304 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3305 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3306 | } |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3307 | return len; |
| 3308 | } |
| 3309 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3310 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3311 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3312 | static ssize_t |
| 3313 | resync_start_show(mddev_t *mddev, char *page) |
| 3314 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3315 | if (mddev->recovery_cp == MaxSector) |
| 3316 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3317 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 3318 | } |
| 3319 | |
| 3320 | static ssize_t |
| 3321 | resync_start_store(mddev_t *mddev, const char *buf, size_t len) |
| 3322 | { |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3323 | char *e; |
| 3324 | unsigned long long n = simple_strtoull(buf, &e, 10); |
| 3325 | |
| 3326 | if (mddev->pers) |
| 3327 | return -EBUSY; |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3328 | if (cmd_match(buf, "none")) |
| 3329 | n = MaxSector; |
| 3330 | else if (!*buf || (*e && *e != '\n')) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3331 | return -EINVAL; |
| 3332 | |
| 3333 | mddev->recovery_cp = n; |
| 3334 | return len; |
| 3335 | } |
| 3336 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3337 | __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3338 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3339 | /* |
| 3340 | * The array state can be: |
| 3341 | * |
| 3342 | * clear |
| 3343 | * No devices, no size, no level |
| 3344 | * Equivalent to STOP_ARRAY ioctl |
| 3345 | * inactive |
| 3346 | * May have some settings, but array is not active |
| 3347 | * all IO results in error |
| 3348 | * When written, doesn't tear down array, but just stops it |
| 3349 | * suspended (not supported yet) |
| 3350 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 3351 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3352 | * readonly |
| 3353 | * no resync can happen. no superblocks get written. |
| 3354 | * write requests fail |
| 3355 | * read-auto |
| 3356 | * like readonly, but behaves like 'clean' on a write request. |
| 3357 | * |
| 3358 | * clean - no pending writes, but otherwise active. |
| 3359 | * When written to inactive array, starts without resync |
| 3360 | * If a write request arrives then |
| 3361 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 3362 | * if not known, block and switch to write-pending |
| 3363 | * If written to an active array that has pending writes, then fails. |
| 3364 | * active |
| 3365 | * fully active: IO and resync can be happening. |
| 3366 | * When written to inactive array, starts with resync |
| 3367 | * |
| 3368 | * write-pending |
| 3369 | * clean, but writes are blocked waiting for 'active' to be written. |
| 3370 | * |
| 3371 | * active-idle |
| 3372 | * like active, but no writes have been seen for a while (100msec). |
| 3373 | * |
| 3374 | */ |
| 3375 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 3376 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3377 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3378 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 3379 | "write-pending", "active-idle", NULL }; |
| 3380 | |
| 3381 | static int match_word(const char *word, char **list) |
| 3382 | { |
| 3383 | int n; |
| 3384 | for (n=0; list[n]; n++) |
| 3385 | if (cmd_match(word, list[n])) |
| 3386 | break; |
| 3387 | return n; |
| 3388 | } |
| 3389 | |
| 3390 | static ssize_t |
| 3391 | array_state_show(mddev_t *mddev, char *page) |
| 3392 | { |
| 3393 | enum array_state st = inactive; |
| 3394 | |
| 3395 | if (mddev->pers) |
| 3396 | switch(mddev->ro) { |
| 3397 | case 1: |
| 3398 | st = readonly; |
| 3399 | break; |
| 3400 | case 2: |
| 3401 | st = read_auto; |
| 3402 | break; |
| 3403 | case 0: |
| 3404 | if (mddev->in_sync) |
| 3405 | st = clean; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3406 | else if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3407 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3408 | else if (mddev->safemode) |
| 3409 | st = active_idle; |
| 3410 | else |
| 3411 | st = active; |
| 3412 | } |
| 3413 | else { |
| 3414 | if (list_empty(&mddev->disks) && |
| 3415 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3416 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3417 | st = clear; |
| 3418 | else |
| 3419 | st = inactive; |
| 3420 | } |
| 3421 | return sprintf(page, "%s\n", array_states[st]); |
| 3422 | } |
| 3423 | |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3424 | static int do_md_stop(mddev_t * mddev, int ro, int is_open); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3425 | static int md_set_readonly(mddev_t * mddev, int is_open); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3426 | static int do_md_run(mddev_t * mddev); |
| 3427 | static int restart_array(mddev_t *mddev); |
| 3428 | |
| 3429 | static ssize_t |
| 3430 | array_state_store(mddev_t *mddev, const char *buf, size_t len) |
| 3431 | { |
| 3432 | int err = -EINVAL; |
| 3433 | enum array_state st = match_word(buf, array_states); |
| 3434 | switch(st) { |
| 3435 | case bad_word: |
| 3436 | break; |
| 3437 | case clear: |
| 3438 | /* stopping an active array */ |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3439 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3440 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3441 | err = do_md_stop(mddev, 0, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3442 | break; |
| 3443 | case inactive: |
| 3444 | /* stopping an active array */ |
| 3445 | if (mddev->pers) { |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3446 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3447 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3448 | err = do_md_stop(mddev, 2, 0); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3449 | } else |
| 3450 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3451 | break; |
| 3452 | case suspended: |
| 3453 | break; /* not supported yet */ |
| 3454 | case readonly: |
| 3455 | if (mddev->pers) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3456 | err = md_set_readonly(mddev, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3457 | else { |
| 3458 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3459 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3460 | err = do_md_run(mddev); |
| 3461 | } |
| 3462 | break; |
| 3463 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3464 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3465 | if (mddev->ro == 0) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3466 | err = md_set_readonly(mddev, 0); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3467 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3468 | err = restart_array(mddev); |
| 3469 | if (err == 0) { |
| 3470 | mddev->ro = 2; |
| 3471 | set_disk_ro(mddev->gendisk, 0); |
| 3472 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3473 | } else { |
| 3474 | mddev->ro = 2; |
| 3475 | err = do_md_run(mddev); |
| 3476 | } |
| 3477 | break; |
| 3478 | case clean: |
| 3479 | if (mddev->pers) { |
| 3480 | restart_array(mddev); |
| 3481 | spin_lock_irq(&mddev->write_lock); |
| 3482 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3483 | if (mddev->in_sync == 0) { |
| 3484 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3485 | if (mddev->safemode == 1) |
| 3486 | mddev->safemode = 0; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3487 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3488 | } |
| 3489 | err = 0; |
| 3490 | } else |
| 3491 | err = -EBUSY; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3492 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 3493 | } else |
| 3494 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3495 | break; |
| 3496 | case active: |
| 3497 | if (mddev->pers) { |
| 3498 | restart_array(mddev); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3499 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3500 | wake_up(&mddev->sb_wait); |
| 3501 | err = 0; |
| 3502 | } else { |
| 3503 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3504 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3505 | err = do_md_run(mddev); |
| 3506 | } |
| 3507 | break; |
| 3508 | case write_pending: |
| 3509 | case active_idle: |
| 3510 | /* these cannot be set */ |
| 3511 | break; |
| 3512 | } |
| 3513 | if (err) |
| 3514 | return err; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3515 | else { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 3516 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3517 | return len; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3518 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3519 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3520 | static struct md_sysfs_entry md_array_state = |
| 3521 | __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3522 | |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3523 | static ssize_t |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 3524 | max_corrected_read_errors_show(mddev_t *mddev, char *page) { |
| 3525 | return sprintf(page, "%d\n", |
| 3526 | atomic_read(&mddev->max_corr_read_errors)); |
| 3527 | } |
| 3528 | |
| 3529 | static ssize_t |
| 3530 | max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len) |
| 3531 | { |
| 3532 | char *e; |
| 3533 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3534 | |
| 3535 | if (*buf && (*e == 0 || *e == '\n')) { |
| 3536 | atomic_set(&mddev->max_corr_read_errors, n); |
| 3537 | return len; |
| 3538 | } |
| 3539 | return -EINVAL; |
| 3540 | } |
| 3541 | |
| 3542 | static struct md_sysfs_entry max_corr_read_errors = |
| 3543 | __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show, |
| 3544 | max_corrected_read_errors_store); |
| 3545 | |
| 3546 | static ssize_t |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3547 | null_show(mddev_t *mddev, char *page) |
| 3548 | { |
| 3549 | return -EINVAL; |
| 3550 | } |
| 3551 | |
| 3552 | static ssize_t |
| 3553 | new_dev_store(mddev_t *mddev, const char *buf, size_t len) |
| 3554 | { |
| 3555 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 3556 | /* The new device is added to the array. |
| 3557 | * If the array has a persistent superblock, we read the |
| 3558 | * superblock to initialise info and check validity. |
| 3559 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 3560 | * which mainly checks size. |
| 3561 | */ |
| 3562 | char *e; |
| 3563 | int major = simple_strtoul(buf, &e, 10); |
| 3564 | int minor; |
| 3565 | dev_t dev; |
| 3566 | mdk_rdev_t *rdev; |
| 3567 | int err; |
| 3568 | |
| 3569 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 3570 | return -EINVAL; |
| 3571 | minor = simple_strtoul(e+1, &e, 10); |
| 3572 | if (*e && *e != '\n') |
| 3573 | return -EINVAL; |
| 3574 | dev = MKDEV(major, minor); |
| 3575 | if (major != MAJOR(dev) || |
| 3576 | minor != MINOR(dev)) |
| 3577 | return -EOVERFLOW; |
| 3578 | |
| 3579 | |
| 3580 | if (mddev->persistent) { |
| 3581 | rdev = md_import_device(dev, mddev->major_version, |
| 3582 | mddev->minor_version); |
| 3583 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
| 3584 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 3585 | mdk_rdev_t, same_set); |
| 3586 | err = super_types[mddev->major_version] |
| 3587 | .load_super(rdev, rdev0, mddev->minor_version); |
| 3588 | if (err < 0) |
| 3589 | goto out; |
| 3590 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3591 | } else if (mddev->external) |
| 3592 | rdev = md_import_device(dev, -2, -1); |
| 3593 | else |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3594 | rdev = md_import_device(dev, -1, -1); |
| 3595 | |
| 3596 | if (IS_ERR(rdev)) |
| 3597 | return PTR_ERR(rdev); |
| 3598 | err = bind_rdev_to_array(rdev, mddev); |
| 3599 | out: |
| 3600 | if (err) |
| 3601 | export_rdev(rdev); |
| 3602 | return err ? err : len; |
| 3603 | } |
| 3604 | |
| 3605 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3606 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3607 | |
| 3608 | static ssize_t |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3609 | bitmap_store(mddev_t *mddev, const char *buf, size_t len) |
| 3610 | { |
| 3611 | char *end; |
| 3612 | unsigned long chunk, end_chunk; |
| 3613 | |
| 3614 | if (!mddev->bitmap) |
| 3615 | goto out; |
| 3616 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 3617 | while (*buf) { |
| 3618 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 3619 | if (buf == end) break; |
| 3620 | if (*end == '-') { /* range */ |
| 3621 | buf = end + 1; |
| 3622 | end_chunk = simple_strtoul(buf, &end, 0); |
| 3623 | if (buf == end) break; |
| 3624 | } |
| 3625 | if (*end && !isspace(*end)) break; |
| 3626 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 3627 | buf = skip_spaces(end); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3628 | } |
| 3629 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 3630 | out: |
| 3631 | return len; |
| 3632 | } |
| 3633 | |
| 3634 | static struct md_sysfs_entry md_bitmap = |
| 3635 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 3636 | |
| 3637 | static ssize_t |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3638 | size_show(mddev_t *mddev, char *page) |
| 3639 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3640 | return sprintf(page, "%llu\n", |
| 3641 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3642 | } |
| 3643 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3644 | static int update_size(mddev_t *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3645 | |
| 3646 | static ssize_t |
| 3647 | size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3648 | { |
| 3649 | /* If array is inactive, we can reduce the component size, but |
| 3650 | * not increase it (except from 0). |
| 3651 | * If array is active, we can try an on-line resize |
| 3652 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3653 | sector_t sectors; |
| 3654 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3655 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3656 | if (err < 0) |
| 3657 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3658 | if (mddev->pers) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3659 | err = update_size(mddev, sectors); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3660 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3661 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3662 | if (mddev->dev_sectors == 0 || |
| 3663 | mddev->dev_sectors > sectors) |
| 3664 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3665 | else |
| 3666 | err = -ENOSPC; |
| 3667 | } |
| 3668 | return err ? err : len; |
| 3669 | } |
| 3670 | |
| 3671 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3672 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3673 | |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3674 | |
| 3675 | /* Metdata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3676 | * This is one of |
| 3677 | * 'none' for arrays with no metadata (good luck...) |
| 3678 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3679 | * or N.M for internally known formats |
| 3680 | */ |
| 3681 | static ssize_t |
| 3682 | metadata_show(mddev_t *mddev, char *page) |
| 3683 | { |
| 3684 | if (mddev->persistent) |
| 3685 | return sprintf(page, "%d.%d\n", |
| 3686 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3687 | else if (mddev->external) |
| 3688 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3689 | else |
| 3690 | return sprintf(page, "none\n"); |
| 3691 | } |
| 3692 | |
| 3693 | static ssize_t |
| 3694 | metadata_store(mddev_t *mddev, const char *buf, size_t len) |
| 3695 | { |
| 3696 | int major, minor; |
| 3697 | char *e; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 3698 | /* Changing the details of 'external' metadata is |
| 3699 | * always permitted. Otherwise there must be |
| 3700 | * no devices attached to the array. |
| 3701 | */ |
| 3702 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 3703 | ; |
| 3704 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3705 | return -EBUSY; |
| 3706 | |
| 3707 | if (cmd_match(buf, "none")) { |
| 3708 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3709 | mddev->external = 0; |
| 3710 | mddev->major_version = 0; |
| 3711 | mddev->minor_version = 90; |
| 3712 | return len; |
| 3713 | } |
| 3714 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 3715 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3716 | if (namelen >= sizeof(mddev->metadata_type)) |
| 3717 | namelen = sizeof(mddev->metadata_type)-1; |
| 3718 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 3719 | mddev->metadata_type[namelen] = 0; |
| 3720 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 3721 | mddev->metadata_type[--namelen] = 0; |
| 3722 | mddev->persistent = 0; |
| 3723 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3724 | mddev->major_version = 0; |
| 3725 | mddev->minor_version = 90; |
| 3726 | return len; |
| 3727 | } |
| 3728 | major = simple_strtoul(buf, &e, 10); |
| 3729 | if (e==buf || *e != '.') |
| 3730 | return -EINVAL; |
| 3731 | buf = e+1; |
| 3732 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 3733 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3734 | return -EINVAL; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 3735 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3736 | return -ENOENT; |
| 3737 | mddev->major_version = major; |
| 3738 | mddev->minor_version = minor; |
| 3739 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3740 | mddev->external = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3741 | return len; |
| 3742 | } |
| 3743 | |
| 3744 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3745 | __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3746 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3747 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3748 | action_show(mddev_t *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3749 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3750 | char *type = "idle"; |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3751 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 3752 | type = "frozen"; |
| 3753 | else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 2b12ab6 | 2007-10-16 23:30:53 -0700 | [diff] [blame] | 3754 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3755 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 3756 | type = "reshape"; |
| 3757 | else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3758 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 3759 | type = "resync"; |
| 3760 | else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 3761 | type = "check"; |
| 3762 | else |
| 3763 | type = "repair"; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3764 | } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3765 | type = "recover"; |
| 3766 | } |
| 3767 | return sprintf(page, "%s\n", type); |
| 3768 | } |
| 3769 | |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 3770 | static void reap_sync_thread(mddev_t *mddev); |
| 3771 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3772 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3773 | action_store(mddev_t *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3774 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3775 | if (!mddev->pers || !mddev->pers->sync_request) |
| 3776 | return -EINVAL; |
| 3777 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3778 | if (cmd_match(page, "frozen")) |
| 3779 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3780 | else |
| 3781 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3782 | |
| 3783 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3784 | if (mddev->sync_thread) { |
| 3785 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 3786 | reap_sync_thread(mddev); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3787 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3788 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 3789 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3790 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3791 | else if (cmd_match(page, "resync")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3792 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3793 | else if (cmd_match(page, "recover")) { |
| 3794 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 3795 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3796 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3797 | int err; |
| 3798 | if (mddev->pers->start_reshape == NULL) |
| 3799 | return -EINVAL; |
| 3800 | err = mddev->pers->start_reshape(mddev); |
| 3801 | if (err) |
| 3802 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 3803 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3804 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 3805 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3806 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 3807 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3808 | return -EINVAL; |
| 3809 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 3810 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3811 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3812 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3813 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 3814 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3815 | return len; |
| 3816 | } |
| 3817 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3818 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3819 | mismatch_cnt_show(mddev_t *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3820 | { |
| 3821 | return sprintf(page, "%llu\n", |
| 3822 | (unsigned long long) mddev->resync_mismatches); |
| 3823 | } |
| 3824 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3825 | static struct md_sysfs_entry md_scan_mode = |
| 3826 | __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3827 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3828 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3829 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3830 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3831 | static ssize_t |
| 3832 | sync_min_show(mddev_t *mddev, char *page) |
| 3833 | { |
| 3834 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 3835 | mddev->sync_speed_min ? "local": "system"); |
| 3836 | } |
| 3837 | |
| 3838 | static ssize_t |
| 3839 | sync_min_store(mddev_t *mddev, const char *buf, size_t len) |
| 3840 | { |
| 3841 | int min; |
| 3842 | char *e; |
| 3843 | if (strncmp(buf, "system", 6)==0) { |
| 3844 | mddev->sync_speed_min = 0; |
| 3845 | return len; |
| 3846 | } |
| 3847 | min = simple_strtoul(buf, &e, 10); |
| 3848 | if (buf == e || (*e && *e != '\n') || min <= 0) |
| 3849 | return -EINVAL; |
| 3850 | mddev->sync_speed_min = min; |
| 3851 | return len; |
| 3852 | } |
| 3853 | |
| 3854 | static struct md_sysfs_entry md_sync_min = |
| 3855 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 3856 | |
| 3857 | static ssize_t |
| 3858 | sync_max_show(mddev_t *mddev, char *page) |
| 3859 | { |
| 3860 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 3861 | mddev->sync_speed_max ? "local": "system"); |
| 3862 | } |
| 3863 | |
| 3864 | static ssize_t |
| 3865 | sync_max_store(mddev_t *mddev, const char *buf, size_t len) |
| 3866 | { |
| 3867 | int max; |
| 3868 | char *e; |
| 3869 | if (strncmp(buf, "system", 6)==0) { |
| 3870 | mddev->sync_speed_max = 0; |
| 3871 | return len; |
| 3872 | } |
| 3873 | max = simple_strtoul(buf, &e, 10); |
| 3874 | if (buf == e || (*e && *e != '\n') || max <= 0) |
| 3875 | return -EINVAL; |
| 3876 | mddev->sync_speed_max = max; |
| 3877 | return len; |
| 3878 | } |
| 3879 | |
| 3880 | static struct md_sysfs_entry md_sync_max = |
| 3881 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 3882 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 3883 | static ssize_t |
| 3884 | degraded_show(mddev_t *mddev, char *page) |
| 3885 | { |
| 3886 | return sprintf(page, "%d\n", mddev->degraded); |
| 3887 | } |
| 3888 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3889 | |
| 3890 | static ssize_t |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 3891 | sync_force_parallel_show(mddev_t *mddev, char *page) |
| 3892 | { |
| 3893 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 3894 | } |
| 3895 | |
| 3896 | static ssize_t |
| 3897 | sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len) |
| 3898 | { |
| 3899 | long n; |
| 3900 | |
| 3901 | if (strict_strtol(buf, 10, &n)) |
| 3902 | return -EINVAL; |
| 3903 | |
| 3904 | if (n != 0 && n != 1) |
| 3905 | return -EINVAL; |
| 3906 | |
| 3907 | mddev->parallel_resync = n; |
| 3908 | |
| 3909 | if (mddev->sync_thread) |
| 3910 | wake_up(&resync_wait); |
| 3911 | |
| 3912 | return len; |
| 3913 | } |
| 3914 | |
| 3915 | /* force parallel resync, even with shared block devices */ |
| 3916 | static struct md_sysfs_entry md_sync_force_parallel = |
| 3917 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 3918 | sync_force_parallel_show, sync_force_parallel_store); |
| 3919 | |
| 3920 | static ssize_t |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3921 | sync_speed_show(mddev_t *mddev, char *page) |
| 3922 | { |
| 3923 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3924 | if (mddev->curr_resync == 0) |
| 3925 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3926 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 3927 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3928 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3929 | db = resync - mddev->resync_mark_cnt; |
| 3930 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3931 | } |
| 3932 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3933 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3934 | |
| 3935 | static ssize_t |
| 3936 | sync_completed_show(mddev_t *mddev, char *page) |
| 3937 | { |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 3938 | unsigned long long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3939 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 3940 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3941 | return sprintf(page, "none\n"); |
| 3942 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3943 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3944 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3945 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3946 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3947 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 3948 | resync = mddev->curr_resync_completed; |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 3949 | return sprintf(page, "%llu / %llu\n", resync, max_sectors); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3950 | } |
| 3951 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3952 | static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3953 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3954 | static ssize_t |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3955 | min_sync_show(mddev_t *mddev, char *page) |
| 3956 | { |
| 3957 | return sprintf(page, "%llu\n", |
| 3958 | (unsigned long long)mddev->resync_min); |
| 3959 | } |
| 3960 | static ssize_t |
| 3961 | min_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3962 | { |
| 3963 | unsigned long long min; |
| 3964 | if (strict_strtoull(buf, 10, &min)) |
| 3965 | return -EINVAL; |
| 3966 | if (min > mddev->resync_max) |
| 3967 | return -EINVAL; |
| 3968 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3969 | return -EBUSY; |
| 3970 | |
| 3971 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3972 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 3973 | sector_t temp = min; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3974 | if (sector_div(temp, mddev->chunk_sectors)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3975 | return -EINVAL; |
| 3976 | } |
| 3977 | mddev->resync_min = min; |
| 3978 | |
| 3979 | return len; |
| 3980 | } |
| 3981 | |
| 3982 | static struct md_sysfs_entry md_min_sync = |
| 3983 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 3984 | |
| 3985 | static ssize_t |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3986 | max_sync_show(mddev_t *mddev, char *page) |
| 3987 | { |
| 3988 | if (mddev->resync_max == MaxSector) |
| 3989 | return sprintf(page, "max\n"); |
| 3990 | else |
| 3991 | return sprintf(page, "%llu\n", |
| 3992 | (unsigned long long)mddev->resync_max); |
| 3993 | } |
| 3994 | static ssize_t |
| 3995 | max_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3996 | { |
| 3997 | if (strncmp(buf, "max", 3) == 0) |
| 3998 | mddev->resync_max = MaxSector; |
| 3999 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4000 | unsigned long long max; |
| 4001 | if (strict_strtoull(buf, 10, &max)) |
| 4002 | return -EINVAL; |
| 4003 | if (max < mddev->resync_min) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4004 | return -EINVAL; |
| 4005 | if (max < mddev->resync_max && |
NeilBrown | 4d484a4 | 2009-08-13 10:41:50 +1000 | [diff] [blame] | 4006 | mddev->ro == 0 && |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4007 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4008 | return -EBUSY; |
| 4009 | |
| 4010 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4011 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 4012 | sector_t temp = max; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4013 | if (sector_div(temp, mddev->chunk_sectors)) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4014 | return -EINVAL; |
| 4015 | } |
| 4016 | mddev->resync_max = max; |
| 4017 | } |
| 4018 | wake_up(&mddev->recovery_wait); |
| 4019 | return len; |
| 4020 | } |
| 4021 | |
| 4022 | static struct md_sysfs_entry md_max_sync = |
| 4023 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 4024 | |
| 4025 | static ssize_t |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4026 | suspend_lo_show(mddev_t *mddev, char *page) |
| 4027 | { |
| 4028 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 4029 | } |
| 4030 | |
| 4031 | static ssize_t |
| 4032 | suspend_lo_store(mddev_t *mddev, const char *buf, size_t len) |
| 4033 | { |
| 4034 | char *e; |
| 4035 | unsigned long long new = simple_strtoull(buf, &e, 10); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4036 | unsigned long long old = mddev->suspend_lo; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4037 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 4038 | if (mddev->pers == NULL || |
| 4039 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4040 | return -EINVAL; |
| 4041 | if (buf == e || (*e && *e != '\n')) |
| 4042 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4043 | |
| 4044 | mddev->suspend_lo = new; |
| 4045 | if (new >= old) |
| 4046 | /* Shrinking suspended region */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4047 | mddev->pers->quiesce(mddev, 2); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4048 | else { |
| 4049 | /* Expanding suspended region - need to wait */ |
| 4050 | mddev->pers->quiesce(mddev, 1); |
| 4051 | mddev->pers->quiesce(mddev, 0); |
| 4052 | } |
| 4053 | return len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4054 | } |
| 4055 | static struct md_sysfs_entry md_suspend_lo = |
| 4056 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 4057 | |
| 4058 | |
| 4059 | static ssize_t |
| 4060 | suspend_hi_show(mddev_t *mddev, char *page) |
| 4061 | { |
| 4062 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 4063 | } |
| 4064 | |
| 4065 | static ssize_t |
| 4066 | suspend_hi_store(mddev_t *mddev, const char *buf, size_t len) |
| 4067 | { |
| 4068 | char *e; |
| 4069 | unsigned long long new = simple_strtoull(buf, &e, 10); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4070 | unsigned long long old = mddev->suspend_hi; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4071 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 4072 | if (mddev->pers == NULL || |
| 4073 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4074 | return -EINVAL; |
| 4075 | if (buf == e || (*e && *e != '\n')) |
| 4076 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4077 | |
| 4078 | mddev->suspend_hi = new; |
| 4079 | if (new <= old) |
| 4080 | /* Shrinking suspended region */ |
| 4081 | mddev->pers->quiesce(mddev, 2); |
| 4082 | else { |
| 4083 | /* Expanding suspended region - need to wait */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4084 | mddev->pers->quiesce(mddev, 1); |
| 4085 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4086 | } |
| 4087 | return len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4088 | } |
| 4089 | static struct md_sysfs_entry md_suspend_hi = |
| 4090 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 4091 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4092 | static ssize_t |
| 4093 | reshape_position_show(mddev_t *mddev, char *page) |
| 4094 | { |
| 4095 | if (mddev->reshape_position != MaxSector) |
| 4096 | return sprintf(page, "%llu\n", |
| 4097 | (unsigned long long)mddev->reshape_position); |
| 4098 | strcpy(page, "none\n"); |
| 4099 | return 5; |
| 4100 | } |
| 4101 | |
| 4102 | static ssize_t |
| 4103 | reshape_position_store(mddev_t *mddev, const char *buf, size_t len) |
| 4104 | { |
| 4105 | char *e; |
| 4106 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 4107 | if (mddev->pers) |
| 4108 | return -EBUSY; |
| 4109 | if (buf == e || (*e && *e != '\n')) |
| 4110 | return -EINVAL; |
| 4111 | mddev->reshape_position = new; |
| 4112 | mddev->delta_disks = 0; |
| 4113 | mddev->new_level = mddev->level; |
| 4114 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4115 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4116 | return len; |
| 4117 | } |
| 4118 | |
| 4119 | static struct md_sysfs_entry md_reshape_position = |
| 4120 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 4121 | reshape_position_store); |
| 4122 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4123 | static ssize_t |
| 4124 | array_size_show(mddev_t *mddev, char *page) |
| 4125 | { |
| 4126 | if (mddev->external_size) |
| 4127 | return sprintf(page, "%llu\n", |
| 4128 | (unsigned long long)mddev->array_sectors/2); |
| 4129 | else |
| 4130 | return sprintf(page, "default\n"); |
| 4131 | } |
| 4132 | |
| 4133 | static ssize_t |
| 4134 | array_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 4135 | { |
| 4136 | sector_t sectors; |
| 4137 | |
| 4138 | if (strncmp(buf, "default", 7) == 0) { |
| 4139 | if (mddev->pers) |
| 4140 | sectors = mddev->pers->size(mddev, 0, 0); |
| 4141 | else |
| 4142 | sectors = mddev->array_sectors; |
| 4143 | |
| 4144 | mddev->external_size = 0; |
| 4145 | } else { |
| 4146 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
| 4147 | return -EINVAL; |
| 4148 | if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
NeilBrown | 2b69c83 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4149 | return -E2BIG; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4150 | |
| 4151 | mddev->external_size = 1; |
| 4152 | } |
| 4153 | |
| 4154 | mddev->array_sectors = sectors; |
NeilBrown | cbe6ef1 | 2011-02-16 13:58:38 +1100 | [diff] [blame] | 4155 | if (mddev->pers) { |
| 4156 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 4157 | revalidate_disk(mddev->gendisk); |
NeilBrown | cbe6ef1 | 2011-02-16 13:58:38 +1100 | [diff] [blame] | 4158 | } |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4159 | return len; |
| 4160 | } |
| 4161 | |
| 4162 | static struct md_sysfs_entry md_array_size = |
| 4163 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 4164 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4165 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4166 | static struct attribute *md_default_attrs[] = { |
| 4167 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4168 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4169 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4170 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4171 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4172 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4173 | &md_metadata.attr, |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4174 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4175 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4176 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4177 | &md_reshape_position.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4178 | &md_array_size.attr, |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4179 | &max_corr_read_errors.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4180 | NULL, |
| 4181 | }; |
| 4182 | |
| 4183 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4184 | &md_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4185 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4186 | &md_sync_min.attr, |
| 4187 | &md_sync_max.attr, |
| 4188 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4189 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4190 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4191 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4192 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4193 | &md_suspend_lo.attr, |
| 4194 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4195 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4196 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4197 | NULL, |
| 4198 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4199 | static struct attribute_group md_redundancy_group = { |
| 4200 | .name = NULL, |
| 4201 | .attrs = md_redundancy_attrs, |
| 4202 | }; |
| 4203 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4204 | |
| 4205 | static ssize_t |
| 4206 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 4207 | { |
| 4208 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 4209 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4210 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4211 | |
| 4212 | if (!entry->show) |
| 4213 | return -EIO; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4214 | rv = mddev_lock(mddev); |
| 4215 | if (!rv) { |
| 4216 | rv = entry->show(mddev, page); |
| 4217 | mddev_unlock(mddev); |
| 4218 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4219 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4220 | } |
| 4221 | |
| 4222 | static ssize_t |
| 4223 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 4224 | const char *page, size_t length) |
| 4225 | { |
| 4226 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 4227 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4228 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4229 | |
| 4230 | if (!entry->store) |
| 4231 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 4232 | if (!capable(CAP_SYS_ADMIN)) |
| 4233 | return -EACCES; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4234 | rv = mddev_lock(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4235 | if (mddev->hold_active == UNTIL_IOCTL) |
| 4236 | mddev->hold_active = 0; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4237 | if (!rv) { |
| 4238 | rv = entry->store(mddev, page, length); |
| 4239 | mddev_unlock(mddev); |
| 4240 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4241 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4242 | } |
| 4243 | |
| 4244 | static void md_free(struct kobject *ko) |
| 4245 | { |
| 4246 | mddev_t *mddev = container_of(ko, mddev_t, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4247 | |
| 4248 | if (mddev->sysfs_state) |
| 4249 | sysfs_put(mddev->sysfs_state); |
| 4250 | |
| 4251 | if (mddev->gendisk) { |
| 4252 | del_gendisk(mddev->gendisk); |
| 4253 | put_disk(mddev->gendisk); |
| 4254 | } |
| 4255 | if (mddev->queue) |
| 4256 | blk_cleanup_queue(mddev->queue); |
| 4257 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4258 | kfree(mddev); |
| 4259 | } |
| 4260 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 4261 | static const struct sysfs_ops md_sysfs_ops = { |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4262 | .show = md_attr_show, |
| 4263 | .store = md_attr_store, |
| 4264 | }; |
| 4265 | static struct kobj_type md_ktype = { |
| 4266 | .release = md_free, |
| 4267 | .sysfs_ops = &md_sysfs_ops, |
| 4268 | .default_attrs = md_default_attrs, |
| 4269 | }; |
| 4270 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4271 | int mdp_major = 0; |
| 4272 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4273 | static void mddev_delayed_delete(struct work_struct *ws) |
| 4274 | { |
| 4275 | mddev_t *mddev = container_of(ws, mddev_t, del_work); |
| 4276 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4277 | sysfs_remove_group(&mddev->kobj, &md_bitmap_group); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4278 | kobject_del(&mddev->kobj); |
| 4279 | kobject_put(&mddev->kobj); |
| 4280 | } |
| 4281 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4282 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4283 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4284 | static DEFINE_MUTEX(disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4285 | mddev_t *mddev = mddev_find(dev); |
| 4286 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4287 | int partitioned; |
| 4288 | int shift; |
| 4289 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4290 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4291 | |
| 4292 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4293 | return -ENODEV; |
| 4294 | |
| 4295 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 4296 | shift = partitioned ? MdpMinorShift : 0; |
| 4297 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4298 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4299 | /* wait for any previous instance of this device to be |
| 4300 | * completely removed (mddev_delayed_delete). |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4301 | */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4302 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4303 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4304 | mutex_lock(&disks_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4305 | error = -EEXIST; |
| 4306 | if (mddev->gendisk) |
| 4307 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4308 | |
| 4309 | if (name) { |
| 4310 | /* Need to ensure that 'name' is not a duplicate. |
| 4311 | */ |
| 4312 | mddev_t *mddev2; |
| 4313 | spin_lock(&all_mddevs_lock); |
| 4314 | |
| 4315 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 4316 | if (mddev2->gendisk && |
| 4317 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 4318 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4319 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4320 | } |
| 4321 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4322 | } |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4323 | |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4324 | error = -ENOMEM; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4325 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4326 | if (!mddev->queue) |
| 4327 | goto abort; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4328 | mddev->queue->queuedata = mddev; |
| 4329 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4330 | blk_queue_make_request(mddev->queue, md_make_request); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4331 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4332 | disk = alloc_disk(1 << shift); |
| 4333 | if (!disk) { |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4334 | blk_cleanup_queue(mddev->queue); |
| 4335 | mddev->queue = NULL; |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4336 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4337 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4338 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4339 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4340 | if (name) |
| 4341 | strcpy(disk->disk_name, name); |
| 4342 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4343 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f46 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 4344 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4345 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4346 | disk->fops = &md_fops; |
| 4347 | disk->private_data = mddev; |
| 4348 | disk->queue = mddev->queue; |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4349 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4350 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4351 | * remove it now. |
| 4352 | */ |
| 4353 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4354 | add_disk(disk); |
| 4355 | mddev->gendisk = disk; |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 4356 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
| 4357 | &disk_to_dev(disk)->kobj, "%s", "md"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4358 | if (error) { |
| 4359 | /* This isn't possible, but as kobject_init_and_add is marked |
| 4360 | * __must_check, we must do something with the result |
| 4361 | */ |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4362 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 4363 | disk->disk_name); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4364 | error = 0; |
| 4365 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4366 | if (mddev->kobj.sd && |
| 4367 | sysfs_create_group(&mddev->kobj, &md_bitmap_group)) |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4368 | printk(KERN_DEBUG "pointless warning\n"); |
Darrick J. Wong | be20e6c | 2010-11-24 16:40:33 +1100 | [diff] [blame] | 4369 | |
| 4370 | blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4371 | abort: |
| 4372 | mutex_unlock(&disks_mutex); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4373 | if (!error && mddev->kobj.sd) { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4374 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4375 | mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state"); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4376 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4377 | mddev_put(mddev); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4378 | return error; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4379 | } |
| 4380 | |
| 4381 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 4382 | { |
| 4383 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4384 | return NULL; |
| 4385 | } |
| 4386 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4387 | static int add_named_array(const char *val, struct kernel_param *kp) |
| 4388 | { |
| 4389 | /* val must be "md_*" where * is not all digits. |
| 4390 | * We allocate an array with a large free minor number, and |
| 4391 | * set the name to val. val must not already be an active name. |
| 4392 | */ |
| 4393 | int len = strlen(val); |
| 4394 | char buf[DISK_NAME_LEN]; |
| 4395 | |
| 4396 | while (len && val[len-1] == '\n') |
| 4397 | len--; |
| 4398 | if (len >= DISK_NAME_LEN) |
| 4399 | return -E2BIG; |
| 4400 | strlcpy(buf, val, len+1); |
| 4401 | if (strncmp(buf, "md_", 3) != 0) |
| 4402 | return -EINVAL; |
| 4403 | return md_alloc(0, buf); |
| 4404 | } |
| 4405 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4406 | static void md_safemode_timeout(unsigned long data) |
| 4407 | { |
| 4408 | mddev_t *mddev = (mddev_t *) data; |
| 4409 | |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4410 | if (!atomic_read(&mddev->writes_pending)) { |
| 4411 | mddev->safemode = 1; |
| 4412 | if (mddev->external) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4413 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4414 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4415 | md_wakeup_thread(mddev->thread); |
| 4416 | } |
| 4417 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4418 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4419 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4420 | int md_run(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4421 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4422 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4423 | mdk_rdev_t *rdev; |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4424 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4425 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4426 | if (list_empty(&mddev->disks)) |
| 4427 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4428 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4429 | |
| 4430 | if (mddev->pers) |
| 4431 | return -EBUSY; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 4432 | /* Cannot run until previous stop completes properly */ |
| 4433 | if (mddev->sysfs_active) |
| 4434 | return -EBUSY; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 4435 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4436 | /* |
| 4437 | * Analyze all RAID superblock(s) |
| 4438 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4439 | if (!mddev->raid_disks) { |
| 4440 | if (!mddev->persistent) |
| 4441 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4442 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4443 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4444 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4445 | if (mddev->level != LEVEL_NONE) |
| 4446 | request_module("md-level-%d", mddev->level); |
| 4447 | else if (mddev->clevel[0]) |
| 4448 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4449 | |
| 4450 | /* |
| 4451 | * Drop all container device buffers, from now on |
| 4452 | * the only valid external interface is through the md |
| 4453 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4454 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4455 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4456 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4457 | continue; |
| 4458 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 4459 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4460 | |
| 4461 | /* perform some consistency tests on the device. |
| 4462 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4463 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4464 | */ |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4465 | if (rdev->meta_bdev) { |
| 4466 | /* Nothing to check */; |
| 4467 | } else if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4468 | if (mddev->dev_sectors && |
| 4469 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4470 | > rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4471 | printk("md: %s: data overlaps metadata\n", |
| 4472 | mdname(mddev)); |
| 4473 | return -EINVAL; |
| 4474 | } |
| 4475 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4476 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4477 | > rdev->data_offset) { |
| 4478 | printk("md: %s: metadata overlaps data\n", |
| 4479 | mdname(mddev)); |
| 4480 | return -EINVAL; |
| 4481 | } |
| 4482 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4483 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4484 | } |
| 4485 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 4486 | if (mddev->bio_set == NULL) |
| 4487 | mddev->bio_set = bioset_create(BIO_POOL_SIZE, sizeof(mddev)); |
| 4488 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4489 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4490 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4491 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4492 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4493 | if (mddev->level != LEVEL_NONE) |
| 4494 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 4495 | mddev->level); |
| 4496 | else |
| 4497 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 4498 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4499 | return -EINVAL; |
| 4500 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4501 | mddev->pers = pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4502 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4503 | if (mddev->level != pers->level) { |
| 4504 | mddev->level = pers->level; |
| 4505 | mddev->new_level = pers->level; |
| 4506 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4507 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4508 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4509 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4510 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4511 | /* This personality cannot handle reshaping... */ |
| 4512 | mddev->pers = NULL; |
| 4513 | module_put(pers->owner); |
| 4514 | return -EINVAL; |
| 4515 | } |
| 4516 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4517 | if (pers->sync_request) { |
| 4518 | /* Warn if this is a potentially silly |
| 4519 | * configuration. |
| 4520 | */ |
| 4521 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 4522 | mdk_rdev_t *rdev2; |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4523 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4524 | |
| 4525 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4526 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4527 | if (rdev < rdev2 && |
| 4528 | rdev->bdev->bd_contains == |
| 4529 | rdev2->bdev->bd_contains) { |
| 4530 | printk(KERN_WARNING |
| 4531 | "%s: WARNING: %s appears to be" |
| 4532 | " on the same physical disk as" |
| 4533 | " %s.\n", |
| 4534 | mdname(mddev), |
| 4535 | bdevname(rdev->bdev,b), |
| 4536 | bdevname(rdev2->bdev,b2)); |
| 4537 | warned = 1; |
| 4538 | } |
| 4539 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4540 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4541 | if (warned) |
| 4542 | printk(KERN_WARNING |
| 4543 | "True protection against single-disk" |
| 4544 | " failure might be compromised.\n"); |
| 4545 | } |
| 4546 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 4547 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4548 | /* may be over-ridden by personality */ |
| 4549 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 4550 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4551 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4552 | |
NeilBrown | 0f9552b5 | 2009-12-30 12:08:50 +1100 | [diff] [blame] | 4553 | if (start_readonly && mddev->ro == 0) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4554 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 4555 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4556 | err = mddev->pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 4557 | if (err) |
| 4558 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4559 | else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) { |
| 4560 | WARN_ONCE(!mddev->external_size, "%s: default size too small," |
| 4561 | " but 'external_size' not in effect?\n", __func__); |
| 4562 | printk(KERN_ERR |
| 4563 | "md: invalid array_size %llu > default size %llu\n", |
| 4564 | (unsigned long long)mddev->array_sectors / 2, |
| 4565 | (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2); |
| 4566 | err = -EINVAL; |
| 4567 | mddev->pers->stop(mddev); |
| 4568 | } |
| 4569 | if (err == 0 && mddev->pers->sync_request) { |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4570 | err = bitmap_create(mddev); |
| 4571 | if (err) { |
| 4572 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 4573 | mdname(mddev), err); |
| 4574 | mddev->pers->stop(mddev); |
| 4575 | } |
| 4576 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4577 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4578 | module_put(mddev->pers->owner); |
| 4579 | mddev->pers = NULL; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4580 | bitmap_destroy(mddev); |
| 4581 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4582 | } |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4583 | if (mddev->pers->sync_request) { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4584 | if (mddev->kobj.sd && |
| 4585 | sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4586 | printk(KERN_WARNING |
| 4587 | "md: cannot register extra attributes for %s\n", |
| 4588 | mdname(mddev)); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4589 | mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4590 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 4591 | mddev->ro = 0; |
| 4592 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4593 | atomic_set(&mddev->writes_pending,0); |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4594 | atomic_set(&mddev->max_corr_read_errors, |
| 4595 | MD_DEFAULT_MAX_CORRECTED_READ_ERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4596 | mddev->safemode = 0; |
| 4597 | mddev->safemode_timer.function = md_safemode_timeout; |
| 4598 | mddev->safemode_timer.data = (unsigned long) mddev; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4599 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4600 | mddev->in_sync = 1; |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4601 | smp_wmb(); |
| 4602 | mddev->ready = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4603 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4604 | if (rdev->raid_disk >= 0) { |
| 4605 | char nm[20]; |
| 4606 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4607 | if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4608 | /* failure here is OK */; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4609 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4610 | |
| 4611 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4612 | |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4613 | if (mddev->flags) |
| 4614 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4615 | |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 4616 | md_wakeup_thread(mddev->thread); |
| 4617 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4618 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4619 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4620 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
| 4621 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4622 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4623 | return 0; |
| 4624 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4625 | EXPORT_SYMBOL_GPL(md_run); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4626 | |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 4627 | static int do_md_run(mddev_t *mddev) |
| 4628 | { |
| 4629 | int err; |
| 4630 | |
| 4631 | err = md_run(mddev); |
| 4632 | if (err) |
| 4633 | goto out; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 4634 | err = bitmap_load(mddev); |
| 4635 | if (err) { |
| 4636 | bitmap_destroy(mddev); |
| 4637 | goto out; |
| 4638 | } |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 4639 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 4640 | revalidate_disk(mddev->gendisk); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 4641 | mddev->changed = 1; |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 4642 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
| 4643 | out: |
| 4644 | return err; |
| 4645 | } |
| 4646 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4647 | static int restart_array(mddev_t *mddev) |
| 4648 | { |
| 4649 | struct gendisk *disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4650 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4651 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4652 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4653 | return -ENXIO; |
| 4654 | if (!mddev->pers) |
| 4655 | return -EINVAL; |
| 4656 | if (!mddev->ro) |
| 4657 | return -EBUSY; |
| 4658 | mddev->safemode = 0; |
| 4659 | mddev->ro = 0; |
| 4660 | set_disk_ro(disk, 0); |
| 4661 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 4662 | mdname(mddev)); |
| 4663 | /* Kick recovery or resync if necessary */ |
| 4664 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4665 | md_wakeup_thread(mddev->thread); |
| 4666 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4667 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4668 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4669 | } |
| 4670 | |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4671 | /* similar to deny_write_access, but accounts for our holding a reference |
| 4672 | * to the file ourselves */ |
| 4673 | static int deny_bitmap_write_access(struct file * file) |
| 4674 | { |
| 4675 | struct inode *inode = file->f_mapping->host; |
| 4676 | |
| 4677 | spin_lock(&inode->i_lock); |
| 4678 | if (atomic_read(&inode->i_writecount) > 1) { |
| 4679 | spin_unlock(&inode->i_lock); |
| 4680 | return -ETXTBSY; |
| 4681 | } |
| 4682 | atomic_set(&inode->i_writecount, -1); |
| 4683 | spin_unlock(&inode->i_lock); |
| 4684 | |
| 4685 | return 0; |
| 4686 | } |
| 4687 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4688 | void restore_bitmap_write_access(struct file *file) |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4689 | { |
| 4690 | struct inode *inode = file->f_mapping->host; |
| 4691 | |
| 4692 | spin_lock(&inode->i_lock); |
| 4693 | atomic_set(&inode->i_writecount, 1); |
| 4694 | spin_unlock(&inode->i_lock); |
| 4695 | } |
| 4696 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4697 | static void md_clean(mddev_t *mddev) |
| 4698 | { |
| 4699 | mddev->array_sectors = 0; |
| 4700 | mddev->external_size = 0; |
| 4701 | mddev->dev_sectors = 0; |
| 4702 | mddev->raid_disks = 0; |
| 4703 | mddev->recovery_cp = 0; |
| 4704 | mddev->resync_min = 0; |
| 4705 | mddev->resync_max = MaxSector; |
| 4706 | mddev->reshape_position = MaxSector; |
| 4707 | mddev->external = 0; |
| 4708 | mddev->persistent = 0; |
| 4709 | mddev->level = LEVEL_NONE; |
| 4710 | mddev->clevel[0] = 0; |
| 4711 | mddev->flags = 0; |
| 4712 | mddev->ro = 0; |
| 4713 | mddev->metadata_type[0] = 0; |
| 4714 | mddev->chunk_sectors = 0; |
| 4715 | mddev->ctime = mddev->utime = 0; |
| 4716 | mddev->layout = 0; |
| 4717 | mddev->max_disks = 0; |
| 4718 | mddev->events = 0; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 4719 | mddev->can_decrease_events = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4720 | mddev->delta_disks = 0; |
| 4721 | mddev->new_level = LEVEL_NONE; |
| 4722 | mddev->new_layout = 0; |
| 4723 | mddev->new_chunk_sectors = 0; |
| 4724 | mddev->curr_resync = 0; |
| 4725 | mddev->resync_mismatches = 0; |
| 4726 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 4727 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 4728 | mddev->recovery = 0; |
| 4729 | mddev->in_sync = 0; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 4730 | mddev->changed = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4731 | mddev->degraded = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4732 | mddev->safemode = 0; |
| 4733 | mddev->bitmap_info.offset = 0; |
| 4734 | mddev->bitmap_info.default_offset = 0; |
| 4735 | mddev->bitmap_info.chunksize = 0; |
| 4736 | mddev->bitmap_info.daemon_sleep = 0; |
| 4737 | mddev->bitmap_info.max_write_behind = 0; |
| 4738 | } |
| 4739 | |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4740 | static void __md_stop_writes(mddev_t *mddev) |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 4741 | { |
| 4742 | if (mddev->sync_thread) { |
| 4743 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4744 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4745 | reap_sync_thread(mddev); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 4746 | } |
| 4747 | |
| 4748 | del_timer_sync(&mddev->safemode_timer); |
| 4749 | |
| 4750 | bitmap_flush(mddev); |
| 4751 | md_super_wait(mddev); |
| 4752 | |
| 4753 | if (!mddev->in_sync || mddev->flags) { |
| 4754 | /* mark array as shutdown cleanly */ |
| 4755 | mddev->in_sync = 1; |
| 4756 | md_update_sb(mddev, 1); |
| 4757 | } |
| 4758 | } |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4759 | |
| 4760 | void md_stop_writes(mddev_t *mddev) |
| 4761 | { |
| 4762 | mddev_lock(mddev); |
| 4763 | __md_stop_writes(mddev); |
| 4764 | mddev_unlock(mddev); |
| 4765 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4766 | EXPORT_SYMBOL_GPL(md_stop_writes); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 4767 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4768 | void md_stop(mddev_t *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4769 | { |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4770 | mddev->ready = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4771 | mddev->pers->stop(mddev); |
| 4772 | if (mddev->pers->sync_request && mddev->to_remove == NULL) |
| 4773 | mddev->to_remove = &md_redundancy_group; |
| 4774 | module_put(mddev->pers->owner); |
| 4775 | mddev->pers = NULL; |
NeilBrown | cca9cf9 | 2010-04-01 12:08:16 +1100 | [diff] [blame] | 4776 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4777 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4778 | EXPORT_SYMBOL_GPL(md_stop); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4779 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4780 | static int md_set_readonly(mddev_t *mddev, int is_open) |
| 4781 | { |
| 4782 | int err = 0; |
| 4783 | mutex_lock(&mddev->open_mutex); |
| 4784 | if (atomic_read(&mddev->openers) > is_open) { |
| 4785 | printk("md: %s still in use.\n",mdname(mddev)); |
| 4786 | err = -EBUSY; |
| 4787 | goto out; |
| 4788 | } |
| 4789 | if (mddev->pers) { |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4790 | __md_stop_writes(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4791 | |
| 4792 | err = -ENXIO; |
| 4793 | if (mddev->ro==1) |
| 4794 | goto out; |
| 4795 | mddev->ro = 1; |
| 4796 | set_disk_ro(mddev->gendisk, 1); |
| 4797 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4798 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4799 | err = 0; |
| 4800 | } |
| 4801 | out: |
| 4802 | mutex_unlock(&mddev->open_mutex); |
| 4803 | return err; |
| 4804 | } |
| 4805 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4806 | /* mode: |
| 4807 | * 0 - completely stop and dis-assemble array |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4808 | * 2 - stop but do not disassemble array |
| 4809 | */ |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4810 | 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] | 4811 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4812 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 4813 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4814 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 4815 | mutex_lock(&mddev->open_mutex); |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 4816 | if (atomic_read(&mddev->openers) > is_open || |
| 4817 | mddev->sysfs_active) { |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4818 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 4819 | mutex_unlock(&mddev->open_mutex); |
| 4820 | return -EBUSY; |
| 4821 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4822 | |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 4823 | if (mddev->pers) { |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4824 | if (mddev->ro) |
| 4825 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4826 | |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4827 | __md_stop_writes(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4828 | md_stop(mddev); |
| 4829 | mddev->queue->merge_bvec_fn = NULL; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4830 | mddev->queue->backing_dev_info.congested_fn = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4831 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4832 | /* tell userspace to handle 'inactive' */ |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4833 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4834 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4835 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4836 | if (rdev->raid_disk >= 0) { |
| 4837 | char nm[20]; |
| 4838 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 4839 | sysfs_remove_link(&mddev->kobj, nm); |
| 4840 | } |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 4841 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4842 | set_capacity(disk, 0); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 4843 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 4844 | mddev->changed = 1; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4845 | revalidate_disk(disk); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4846 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4847 | if (mddev->ro) |
| 4848 | mddev->ro = 0; |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 4849 | } else |
| 4850 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4851 | /* |
| 4852 | * Free resources if final stop |
| 4853 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4854 | if (mode == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4855 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 4856 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4857 | bitmap_destroy(mddev); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4858 | if (mddev->bitmap_info.file) { |
| 4859 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 4860 | fput(mddev->bitmap_info.file); |
| 4861 | mddev->bitmap_info.file = NULL; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4862 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4863 | mddev->bitmap_info.offset = 0; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4864 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4865 | export_array(mddev); |
| 4866 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4867 | md_clean(mddev); |
NeilBrown | 934d9c2 | 2008-10-28 17:01:23 +1100 | [diff] [blame] | 4868 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4869 | if (mddev->hold_active == UNTIL_STOP) |
| 4870 | mddev->hold_active = 0; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4871 | } |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 4872 | blk_integrity_unregister(disk); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4873 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4874 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 4875 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4876 | } |
| 4877 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4878 | #ifndef MODULE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4879 | static void autorun_array(mddev_t *mddev) |
| 4880 | { |
| 4881 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4882 | int err; |
| 4883 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4884 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4885 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4886 | |
| 4887 | printk(KERN_INFO "md: running: "); |
| 4888 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4889 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4890 | char b[BDEVNAME_SIZE]; |
| 4891 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 4892 | } |
| 4893 | printk("\n"); |
| 4894 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4895 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4896 | if (err) { |
| 4897 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4898 | do_md_stop(mddev, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4899 | } |
| 4900 | } |
| 4901 | |
| 4902 | /* |
| 4903 | * lets try to run arrays based on all disks that have arrived |
| 4904 | * until now. (those are in pending_raid_disks) |
| 4905 | * |
| 4906 | * the method: pick the first pending disk, collect all disks with |
| 4907 | * the same UUID, remove all from the pending list and put them into |
| 4908 | * the 'same_array' list. Then order this list based on superblock |
| 4909 | * update time (freshest comes first), kick out 'old' disks and |
| 4910 | * compare superblocks. If everything's fine then run it. |
| 4911 | * |
| 4912 | * If "unit" is allocated, then bump its reference count |
| 4913 | */ |
| 4914 | static void autorun_devices(int part) |
| 4915 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4916 | mdk_rdev_t *rdev0, *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4917 | mddev_t *mddev; |
| 4918 | char b[BDEVNAME_SIZE]; |
| 4919 | |
| 4920 | printk(KERN_INFO "md: autorun ...\n"); |
| 4921 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4922 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4923 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 4924 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4925 | rdev0 = list_entry(pending_raid_disks.next, |
| 4926 | mdk_rdev_t, same_set); |
| 4927 | |
| 4928 | printk(KERN_INFO "md: considering %s ...\n", |
| 4929 | bdevname(rdev0->bdev,b)); |
| 4930 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4931 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4932 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 4933 | printk(KERN_INFO "md: adding %s ...\n", |
| 4934 | bdevname(rdev->bdev,b)); |
| 4935 | list_move(&rdev->same_set, &candidates); |
| 4936 | } |
| 4937 | /* |
| 4938 | * now we have a set of devices, with all of them having |
| 4939 | * mostly sane superblocks. It's time to allocate the |
| 4940 | * mddev. |
| 4941 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4942 | if (part) { |
| 4943 | dev = MKDEV(mdp_major, |
| 4944 | rdev0->preferred_minor << MdpMinorShift); |
| 4945 | unit = MINOR(dev) >> MdpMinorShift; |
| 4946 | } else { |
| 4947 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 4948 | unit = MINOR(dev); |
| 4949 | } |
| 4950 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4951 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 4952 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 4953 | break; |
| 4954 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4955 | |
| 4956 | md_probe(dev, NULL, NULL); |
| 4957 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 4958 | if (!mddev || !mddev->gendisk) { |
| 4959 | if (mddev) |
| 4960 | mddev_put(mddev); |
| 4961 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4962 | "md: cannot allocate memory for md drive.\n"); |
| 4963 | break; |
| 4964 | } |
| 4965 | if (mddev_lock(mddev)) |
| 4966 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 4967 | mdname(mddev)); |
| 4968 | else if (mddev->raid_disks || mddev->major_version |
| 4969 | || !list_empty(&mddev->disks)) { |
| 4970 | printk(KERN_WARNING |
| 4971 | "md: %s already running, cannot run %s\n", |
| 4972 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 4973 | mddev_unlock(mddev); |
| 4974 | } else { |
| 4975 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4976 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4977 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4978 | list_del_init(&rdev->same_set); |
| 4979 | if (bind_rdev_to_array(rdev, mddev)) |
| 4980 | export_rdev(rdev); |
| 4981 | } |
| 4982 | autorun_array(mddev); |
| 4983 | mddev_unlock(mddev); |
| 4984 | } |
| 4985 | /* on success, candidates will be empty, on error |
| 4986 | * it won't... |
| 4987 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4988 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4989 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4990 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4991 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4992 | mddev_put(mddev); |
| 4993 | } |
| 4994 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 4995 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4996 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4997 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4998 | static int get_version(void __user * arg) |
| 4999 | { |
| 5000 | mdu_version_t ver; |
| 5001 | |
| 5002 | ver.major = MD_MAJOR_VERSION; |
| 5003 | ver.minor = MD_MINOR_VERSION; |
| 5004 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 5005 | |
| 5006 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 5007 | return -EFAULT; |
| 5008 | |
| 5009 | return 0; |
| 5010 | } |
| 5011 | |
| 5012 | static int get_array_info(mddev_t * mddev, void __user * arg) |
| 5013 | { |
| 5014 | mdu_array_info_t info; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5015 | int nr,working,insync,failed,spare; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5016 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5017 | |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5018 | nr=working=insync=failed=spare=0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5019 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5020 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5021 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5022 | failed++; |
| 5023 | else { |
| 5024 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5025 | if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5026 | insync++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5027 | else |
| 5028 | spare++; |
| 5029 | } |
| 5030 | } |
| 5031 | |
| 5032 | info.major_version = mddev->major_version; |
| 5033 | info.minor_version = mddev->minor_version; |
| 5034 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 5035 | info.ctime = mddev->ctime; |
| 5036 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5037 | info.size = mddev->dev_sectors / 2; |
| 5038 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 5039 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5040 | info.nr_disks = nr; |
| 5041 | info.raid_disks = mddev->raid_disks; |
| 5042 | info.md_minor = mddev->md_minor; |
| 5043 | info.not_persistent= !mddev->persistent; |
| 5044 | |
| 5045 | info.utime = mddev->utime; |
| 5046 | info.state = 0; |
| 5047 | if (mddev->in_sync) |
| 5048 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5049 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5050 | info.state = (1<<MD_SB_BITMAP_PRESENT); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5051 | info.active_disks = insync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5052 | info.working_disks = working; |
| 5053 | info.failed_disks = failed; |
| 5054 | info.spare_disks = spare; |
| 5055 | |
| 5056 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5057 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5058 | |
| 5059 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5060 | return -EFAULT; |
| 5061 | |
| 5062 | return 0; |
| 5063 | } |
| 5064 | |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 5065 | static int get_bitmap_file(mddev_t * mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5066 | { |
| 5067 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
| 5068 | char *ptr, *buf = NULL; |
| 5069 | int err = -ENOMEM; |
| 5070 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5071 | if (md_allow_write(mddev)) |
| 5072 | file = kmalloc(sizeof(*file), GFP_NOIO); |
| 5073 | else |
| 5074 | file = kmalloc(sizeof(*file), GFP_KERNEL); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5075 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5076 | if (!file) |
| 5077 | goto out; |
| 5078 | |
| 5079 | /* bitmap disabled, zero the first byte and copy out */ |
| 5080 | if (!mddev->bitmap || !mddev->bitmap->file) { |
| 5081 | file->pathname[0] = '\0'; |
| 5082 | goto copy_out; |
| 5083 | } |
| 5084 | |
| 5085 | buf = kmalloc(sizeof(file->pathname), GFP_KERNEL); |
| 5086 | if (!buf) |
| 5087 | goto out; |
| 5088 | |
Christoph Hellwig | 6bcfd60 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 5089 | ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname)); |
| 5090 | if (IS_ERR(ptr)) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5091 | goto out; |
| 5092 | |
| 5093 | strcpy(file->pathname, ptr); |
| 5094 | |
| 5095 | copy_out: |
| 5096 | err = 0; |
| 5097 | if (copy_to_user(arg, file, sizeof(*file))) |
| 5098 | err = -EFAULT; |
| 5099 | out: |
| 5100 | kfree(buf); |
| 5101 | kfree(file); |
| 5102 | return err; |
| 5103 | } |
| 5104 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5105 | static int get_disk_info(mddev_t * mddev, void __user * arg) |
| 5106 | { |
| 5107 | mdu_disk_info_t info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5108 | mdk_rdev_t *rdev; |
| 5109 | |
| 5110 | if (copy_from_user(&info, arg, sizeof(info))) |
| 5111 | return -EFAULT; |
| 5112 | |
Andre Noll | 26ef379 | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5113 | rdev = find_rdev_nr(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5114 | if (rdev) { |
| 5115 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 5116 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 5117 | info.raid_disk = rdev->raid_disk; |
| 5118 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5119 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5120 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5121 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5122 | info.state |= (1<<MD_DISK_ACTIVE); |
| 5123 | info.state |= (1<<MD_DISK_SYNC); |
| 5124 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5125 | if (test_bit(WriteMostly, &rdev->flags)) |
| 5126 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5127 | } else { |
| 5128 | info.major = info.minor = 0; |
| 5129 | info.raid_disk = -1; |
| 5130 | info.state = (1<<MD_DISK_REMOVED); |
| 5131 | } |
| 5132 | |
| 5133 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5134 | return -EFAULT; |
| 5135 | |
| 5136 | return 0; |
| 5137 | } |
| 5138 | |
| 5139 | static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info) |
| 5140 | { |
| 5141 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 5142 | mdk_rdev_t *rdev; |
| 5143 | dev_t dev = MKDEV(info->major,info->minor); |
| 5144 | |
| 5145 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 5146 | return -EOVERFLOW; |
| 5147 | |
| 5148 | if (!mddev->raid_disks) { |
| 5149 | int err; |
| 5150 | /* expecting a device which has a superblock */ |
| 5151 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 5152 | if (IS_ERR(rdev)) { |
| 5153 | printk(KERN_WARNING |
| 5154 | "md: md_import_device returned %ld\n", |
| 5155 | PTR_ERR(rdev)); |
| 5156 | return PTR_ERR(rdev); |
| 5157 | } |
| 5158 | if (!list_empty(&mddev->disks)) { |
| 5159 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 5160 | mdk_rdev_t, same_set); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5161 | err = super_types[mddev->major_version] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5162 | .load_super(rdev, rdev0, mddev->minor_version); |
| 5163 | if (err < 0) { |
| 5164 | printk(KERN_WARNING |
| 5165 | "md: %s has different UUID to %s\n", |
| 5166 | bdevname(rdev->bdev,b), |
| 5167 | bdevname(rdev0->bdev,b2)); |
| 5168 | export_rdev(rdev); |
| 5169 | return -EINVAL; |
| 5170 | } |
| 5171 | } |
| 5172 | err = bind_rdev_to_array(rdev, mddev); |
| 5173 | if (err) |
| 5174 | export_rdev(rdev); |
| 5175 | return err; |
| 5176 | } |
| 5177 | |
| 5178 | /* |
| 5179 | * add_new_disk can be used once the array is assembled |
| 5180 | * to add "hot spares". They must already have a superblock |
| 5181 | * written |
| 5182 | */ |
| 5183 | if (mddev->pers) { |
| 5184 | int err; |
| 5185 | if (!mddev->pers->hot_add_disk) { |
| 5186 | printk(KERN_WARNING |
| 5187 | "%s: personality does not support diskops!\n", |
| 5188 | mdname(mddev)); |
| 5189 | return -EINVAL; |
| 5190 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 5191 | if (mddev->persistent) |
| 5192 | rdev = md_import_device(dev, mddev->major_version, |
| 5193 | mddev->minor_version); |
| 5194 | else |
| 5195 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5196 | if (IS_ERR(rdev)) { |
| 5197 | printk(KERN_WARNING |
| 5198 | "md: md_import_device returned %ld\n", |
| 5199 | PTR_ERR(rdev)); |
| 5200 | return PTR_ERR(rdev); |
| 5201 | } |
NeilBrown | 1a855a0 | 2010-12-09 16:36:28 +1100 | [diff] [blame] | 5202 | /* set saved_raid_disk if appropriate */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5203 | if (!mddev->persistent) { |
| 5204 | if (info->state & (1<<MD_DISK_SYNC) && |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 5205 | info->raid_disk < mddev->raid_disks) { |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5206 | rdev->raid_disk = info->raid_disk; |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 5207 | set_bit(In_sync, &rdev->flags); |
| 5208 | } else |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5209 | rdev->raid_disk = -1; |
| 5210 | } else |
| 5211 | super_types[mddev->major_version]. |
| 5212 | validate_super(mddev, rdev); |
NeilBrown | 1a855a0 | 2010-12-09 16:36:28 +1100 | [diff] [blame] | 5213 | if (test_bit(In_sync, &rdev->flags)) |
| 5214 | rdev->saved_raid_disk = rdev->raid_disk; |
| 5215 | else |
| 5216 | rdev->saved_raid_disk = -1; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5217 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5218 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5219 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5220 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5221 | else |
| 5222 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5223 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5224 | rdev->raid_disk = -1; |
| 5225 | err = bind_rdev_to_array(rdev, mddev); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 5226 | if (!err && !mddev->pers->hot_remove_disk) { |
| 5227 | /* If there is hot_add_disk but no hot_remove_disk |
| 5228 | * then added disks for geometry changes, |
| 5229 | * and should be added immediately. |
| 5230 | */ |
| 5231 | super_types[mddev->major_version]. |
| 5232 | validate_super(mddev, rdev); |
| 5233 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 5234 | if (err) |
| 5235 | unbind_rdev_from_array(rdev); |
| 5236 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5237 | if (err) |
| 5238 | export_rdev(rdev); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 5239 | else |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5240 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5241 | |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 5242 | md_update_sb(mddev, 1); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 5243 | if (mddev->degraded) |
| 5244 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5245 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 5246 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5247 | return err; |
| 5248 | } |
| 5249 | |
| 5250 | /* otherwise, add_new_disk is only allowed |
| 5251 | * for major_version==0 superblocks |
| 5252 | */ |
| 5253 | if (mddev->major_version != 0) { |
| 5254 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 5255 | mdname(mddev)); |
| 5256 | return -EINVAL; |
| 5257 | } |
| 5258 | |
| 5259 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 5260 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5261 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5262 | if (IS_ERR(rdev)) { |
| 5263 | printk(KERN_WARNING |
| 5264 | "md: error, md_import_device() returned %ld\n", |
| 5265 | PTR_ERR(rdev)); |
| 5266 | return PTR_ERR(rdev); |
| 5267 | } |
| 5268 | rdev->desc_nr = info->number; |
| 5269 | if (info->raid_disk < mddev->raid_disks) |
| 5270 | rdev->raid_disk = info->raid_disk; |
| 5271 | else |
| 5272 | rdev->raid_disk = -1; |
| 5273 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5274 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5275 | if (info->state & (1<<MD_DISK_SYNC)) |
| 5276 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5277 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5278 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5279 | set_bit(WriteMostly, &rdev->flags); |
| 5280 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5281 | if (!mddev->persistent) { |
| 5282 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 5283 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
| 5284 | } else |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5285 | rdev->sb_start = calc_dev_sboffset(rdev); |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5286 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5287 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5288 | err = bind_rdev_to_array(rdev, mddev); |
| 5289 | if (err) { |
| 5290 | export_rdev(rdev); |
| 5291 | return err; |
| 5292 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5293 | } |
| 5294 | |
| 5295 | return 0; |
| 5296 | } |
| 5297 | |
| 5298 | static int hot_remove_disk(mddev_t * mddev, dev_t dev) |
| 5299 | { |
| 5300 | char b[BDEVNAME_SIZE]; |
| 5301 | mdk_rdev_t *rdev; |
| 5302 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5303 | rdev = find_rdev(mddev, dev); |
| 5304 | if (!rdev) |
| 5305 | return -ENXIO; |
| 5306 | |
| 5307 | if (rdev->raid_disk >= 0) |
| 5308 | goto busy; |
| 5309 | |
| 5310 | kick_rdev_from_array(rdev); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5311 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5312 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5313 | |
| 5314 | return 0; |
| 5315 | busy: |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 5316 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5317 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5318 | return -EBUSY; |
| 5319 | } |
| 5320 | |
| 5321 | static int hot_add_disk(mddev_t * mddev, dev_t dev) |
| 5322 | { |
| 5323 | char b[BDEVNAME_SIZE]; |
| 5324 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5325 | mdk_rdev_t *rdev; |
| 5326 | |
| 5327 | if (!mddev->pers) |
| 5328 | return -ENODEV; |
| 5329 | |
| 5330 | if (mddev->major_version != 0) { |
| 5331 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 5332 | " version-0 superblocks.\n", |
| 5333 | mdname(mddev)); |
| 5334 | return -EINVAL; |
| 5335 | } |
| 5336 | if (!mddev->pers->hot_add_disk) { |
| 5337 | printk(KERN_WARNING |
| 5338 | "%s: personality does not support diskops!\n", |
| 5339 | mdname(mddev)); |
| 5340 | return -EINVAL; |
| 5341 | } |
| 5342 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5343 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5344 | if (IS_ERR(rdev)) { |
| 5345 | printk(KERN_WARNING |
| 5346 | "md: error, md_import_device() returned %ld\n", |
| 5347 | PTR_ERR(rdev)); |
| 5348 | return -EINVAL; |
| 5349 | } |
| 5350 | |
| 5351 | if (mddev->persistent) |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5352 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5353 | else |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 5354 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5355 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5356 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5357 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5358 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5359 | printk(KERN_WARNING |
| 5360 | "md: can not hot-add faulty %s disk to %s!\n", |
| 5361 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5362 | err = -EINVAL; |
| 5363 | goto abort_export; |
| 5364 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5365 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5366 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 5367 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5368 | err = bind_rdev_to_array(rdev, mddev); |
| 5369 | if (err) |
| 5370 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5371 | |
| 5372 | /* |
| 5373 | * The rest should better be atomic, we can have disk failures |
| 5374 | * noticed in interrupt contexts ... |
| 5375 | */ |
| 5376 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5377 | rdev->raid_disk = -1; |
| 5378 | |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5379 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5380 | |
| 5381 | /* |
| 5382 | * Kick recovery, maybe this spare has to be added to the |
| 5383 | * array immediately. |
| 5384 | */ |
| 5385 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5386 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5387 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5388 | return 0; |
| 5389 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5390 | abort_export: |
| 5391 | export_rdev(rdev); |
| 5392 | return err; |
| 5393 | } |
| 5394 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5395 | static int set_bitmap_file(mddev_t *mddev, int fd) |
| 5396 | { |
| 5397 | int err; |
| 5398 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5399 | if (mddev->pers) { |
| 5400 | if (!mddev->pers->quiesce) |
| 5401 | return -EBUSY; |
| 5402 | if (mddev->recovery || mddev->sync_thread) |
| 5403 | return -EBUSY; |
| 5404 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5405 | } |
| 5406 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5407 | |
| 5408 | if (fd >= 0) { |
| 5409 | if (mddev->bitmap) |
| 5410 | return -EEXIST; /* cannot add when bitmap is present */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5411 | mddev->bitmap_info.file = fget(fd); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5412 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5413 | if (mddev->bitmap_info.file == NULL) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5414 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 5415 | mdname(mddev)); |
| 5416 | return -EBADF; |
| 5417 | } |
| 5418 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5419 | err = deny_bitmap_write_access(mddev->bitmap_info.file); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5420 | if (err) { |
| 5421 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 5422 | mdname(mddev)); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5423 | fput(mddev->bitmap_info.file); |
| 5424 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5425 | return err; |
| 5426 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5427 | mddev->bitmap_info.offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5428 | } else if (mddev->bitmap == NULL) |
| 5429 | return -ENOENT; /* cannot remove what isn't there */ |
| 5430 | err = 0; |
| 5431 | if (mddev->pers) { |
| 5432 | mddev->pers->quiesce(mddev, 1); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5433 | if (fd >= 0) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5434 | err = bitmap_create(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5435 | if (!err) |
| 5436 | err = bitmap_load(mddev); |
| 5437 | } |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5438 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5439 | bitmap_destroy(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5440 | fd = -1; /* make sure to put the file */ |
| 5441 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5442 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5443 | } |
| 5444 | if (fd < 0) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5445 | if (mddev->bitmap_info.file) { |
| 5446 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 5447 | fput(mddev->bitmap_info.file); |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 5448 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5449 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5450 | } |
| 5451 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5452 | return err; |
| 5453 | } |
| 5454 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5455 | /* |
| 5456 | * set_array_info is used two different ways |
| 5457 | * The original usage is when creating a new array. |
| 5458 | * In this usage, raid_disks is > 0 and it together with |
| 5459 | * level, size, not_persistent,layout,chunksize determine the |
| 5460 | * shape of the array. |
| 5461 | * This will always create an array with a type-0.90.0 superblock. |
| 5462 | * The newer usage is when assembling an array. |
| 5463 | * In this case raid_disks will be 0, and the major_version field is |
| 5464 | * use to determine which style super-blocks are to be found on the devices. |
| 5465 | * The minor and patch _version numbers are also kept incase the |
| 5466 | * super_block handler wishes to interpret them. |
| 5467 | */ |
| 5468 | static int set_array_info(mddev_t * mddev, mdu_array_info_t *info) |
| 5469 | { |
| 5470 | |
| 5471 | if (info->raid_disks == 0) { |
| 5472 | /* just setting version number for superblock loading */ |
| 5473 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 5474 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5475 | super_types[info->major_version].name == NULL) { |
| 5476 | /* maybe try to auto-load a module? */ |
| 5477 | printk(KERN_INFO |
| 5478 | "md: superblock version %d not known\n", |
| 5479 | info->major_version); |
| 5480 | return -EINVAL; |
| 5481 | } |
| 5482 | mddev->major_version = info->major_version; |
| 5483 | mddev->minor_version = info->minor_version; |
| 5484 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5485 | mddev->persistent = !info->not_persistent; |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 5486 | /* ensure mddev_put doesn't delete this now that there |
| 5487 | * is some minimal configuration. |
| 5488 | */ |
| 5489 | mddev->ctime = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5490 | return 0; |
| 5491 | } |
| 5492 | mddev->major_version = MD_MAJOR_VERSION; |
| 5493 | mddev->minor_version = MD_MINOR_VERSION; |
| 5494 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 5495 | mddev->ctime = get_seconds(); |
| 5496 | |
| 5497 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 5498 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5499 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5500 | mddev->raid_disks = info->raid_disks; |
| 5501 | /* don't set md_minor, it is determined by which /dev/md* was |
| 5502 | * openned |
| 5503 | */ |
| 5504 | if (info->state & (1<<MD_SB_CLEAN)) |
| 5505 | mddev->recovery_cp = MaxSector; |
| 5506 | else |
| 5507 | mddev->recovery_cp = 0; |
| 5508 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5509 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5510 | |
| 5511 | mddev->layout = info->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5512 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5513 | |
| 5514 | mddev->max_disks = MD_SB_DISKS; |
| 5515 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5516 | if (mddev->persistent) |
| 5517 | mddev->flags = 0; |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5518 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5519 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5520 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
| 5521 | mddev->bitmap_info.offset = 0; |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 5522 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5523 | mddev->reshape_position = MaxSector; |
| 5524 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5525 | /* |
| 5526 | * Generate a 128 bit UUID |
| 5527 | */ |
| 5528 | get_random_bytes(mddev->uuid, 16); |
| 5529 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5530 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5531 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5532 | mddev->new_layout = mddev->layout; |
| 5533 | mddev->delta_disks = 0; |
| 5534 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5535 | return 0; |
| 5536 | } |
| 5537 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5538 | void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors) |
| 5539 | { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5540 | WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__); |
| 5541 | |
| 5542 | if (mddev->external_size) |
| 5543 | return; |
| 5544 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5545 | mddev->array_sectors = array_sectors; |
| 5546 | } |
| 5547 | EXPORT_SYMBOL(md_set_array_sectors); |
| 5548 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5549 | static int update_size(mddev_t *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5550 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5551 | mdk_rdev_t *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5552 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5553 | int fit = (num_sectors == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5554 | |
| 5555 | if (mddev->pers->resize == NULL) |
| 5556 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5557 | /* The "num_sectors" is the number of sectors of each device that |
| 5558 | * is used. This can only make sense for arrays with redundancy. |
| 5559 | * linear and raid0 always use whatever space is available. We can only |
| 5560 | * consider changing this number if no resync or reconstruction is |
| 5561 | * 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] | 5562 | * 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] | 5563 | * of each device. If num_sectors is zero, we find the largest size |
| 5564 | * that fits. |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5565 | */ |
| 5566 | if (mddev->sync_thread) |
| 5567 | return -EBUSY; |
NeilBrown | dba034e | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 5568 | if (mddev->bitmap) |
| 5569 | /* Sorry, cannot grow a bitmap yet, just remove it, |
| 5570 | * grow, and re-add. |
| 5571 | */ |
| 5572 | return -EBUSY; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5573 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5574 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 5575 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5576 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 5577 | num_sectors = avail; |
| 5578 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5579 | return -ENOSPC; |
| 5580 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5581 | rv = mddev->pers->resize(mddev, num_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5582 | if (!rv) |
| 5583 | revalidate_disk(mddev->gendisk); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5584 | return rv; |
| 5585 | } |
| 5586 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5587 | static int update_raid_disks(mddev_t *mddev, int raid_disks) |
| 5588 | { |
| 5589 | int rv; |
| 5590 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5591 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5592 | return -EINVAL; |
| 5593 | if (raid_disks <= 0 || |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 5594 | (mddev->max_disks && raid_disks >= mddev->max_disks)) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5595 | return -EINVAL; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5596 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5597 | return -EBUSY; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5598 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
| 5599 | |
| 5600 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | de171cb | 2011-01-31 11:57:42 +1100 | [diff] [blame] | 5601 | if (rv < 0) |
| 5602 | mddev->delta_disks = 0; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5603 | return rv; |
| 5604 | } |
| 5605 | |
| 5606 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5607 | /* |
| 5608 | * update_array_info is used to change the configuration of an |
| 5609 | * on-line array. |
| 5610 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 5611 | * fields in the info are checked against the array. |
| 5612 | * Any differences that cannot be handled will cause an error. |
| 5613 | * Normally, only one change can be managed at a time. |
| 5614 | */ |
| 5615 | static int update_array_info(mddev_t *mddev, mdu_array_info_t *info) |
| 5616 | { |
| 5617 | int rv = 0; |
| 5618 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5619 | int state = 0; |
| 5620 | |
| 5621 | /* calculate expected state,ignoring low bits */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5622 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5623 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5624 | |
| 5625 | if (mddev->major_version != info->major_version || |
| 5626 | mddev->minor_version != info->minor_version || |
| 5627 | /* mddev->patch_version != info->patch_version || */ |
| 5628 | mddev->ctime != info->ctime || |
| 5629 | mddev->level != info->level || |
| 5630 | /* mddev->layout != info->layout || */ |
| 5631 | !mddev->persistent != info->not_persistent|| |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5632 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5633 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 5634 | ((state^info->state) & 0xfffffe00) |
| 5635 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5636 | return -EINVAL; |
| 5637 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5638 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 5639 | cnt++; |
| 5640 | if (mddev->raid_disks != info->raid_disks) |
| 5641 | cnt++; |
| 5642 | if (mddev->layout != info->layout) |
| 5643 | cnt++; |
| 5644 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 5645 | cnt++; |
| 5646 | if (cnt == 0) |
| 5647 | return 0; |
| 5648 | if (cnt > 1) |
| 5649 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5650 | |
| 5651 | if (mddev->layout != info->layout) { |
| 5652 | /* Change layout |
| 5653 | * we don't need to do anything at the md level, the |
| 5654 | * personality will take care of it all. |
| 5655 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5656 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5657 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5658 | else { |
| 5659 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5660 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5661 | if (rv) |
| 5662 | mddev->new_layout = mddev->layout; |
| 5663 | return rv; |
| 5664 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5665 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5666 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5667 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5668 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5669 | if (mddev->raid_disks != info->raid_disks) |
| 5670 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5671 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5672 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5673 | if (mddev->pers->quiesce == NULL) |
| 5674 | return -EINVAL; |
| 5675 | if (mddev->recovery || mddev->sync_thread) |
| 5676 | return -EBUSY; |
| 5677 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5678 | /* add the bitmap */ |
| 5679 | if (mddev->bitmap) |
| 5680 | return -EEXIST; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5681 | if (mddev->bitmap_info.default_offset == 0) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5682 | return -EINVAL; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5683 | mddev->bitmap_info.offset = |
| 5684 | mddev->bitmap_info.default_offset; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5685 | mddev->pers->quiesce(mddev, 1); |
| 5686 | rv = bitmap_create(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5687 | if (!rv) |
| 5688 | rv = bitmap_load(mddev); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5689 | if (rv) |
| 5690 | bitmap_destroy(mddev); |
| 5691 | mddev->pers->quiesce(mddev, 0); |
| 5692 | } else { |
| 5693 | /* remove the bitmap */ |
| 5694 | if (!mddev->bitmap) |
| 5695 | return -ENOENT; |
| 5696 | if (mddev->bitmap->file) |
| 5697 | return -EINVAL; |
| 5698 | mddev->pers->quiesce(mddev, 1); |
| 5699 | bitmap_destroy(mddev); |
| 5700 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5701 | mddev->bitmap_info.offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5702 | } |
| 5703 | } |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5704 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5705 | return rv; |
| 5706 | } |
| 5707 | |
| 5708 | static int set_disk_faulty(mddev_t *mddev, dev_t dev) |
| 5709 | { |
| 5710 | mdk_rdev_t *rdev; |
| 5711 | |
| 5712 | if (mddev->pers == NULL) |
| 5713 | return -ENODEV; |
| 5714 | |
| 5715 | rdev = find_rdev(mddev, dev); |
| 5716 | if (!rdev) |
| 5717 | return -ENODEV; |
| 5718 | |
| 5719 | md_error(mddev, rdev); |
| 5720 | return 0; |
| 5721 | } |
| 5722 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 5723 | /* |
| 5724 | * We have a problem here : there is no easy way to give a CHS |
| 5725 | * virtual geometry. We currently pretend that we have a 2 heads |
| 5726 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 5727 | * dosfs just mad... ;-) |
| 5728 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5729 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 5730 | { |
| 5731 | mddev_t *mddev = bdev->bd_disk->private_data; |
| 5732 | |
| 5733 | geo->heads = 2; |
| 5734 | geo->sectors = 4; |
NeilBrown | 49ce6ce | 2010-03-29 10:51:42 +1100 | [diff] [blame] | 5735 | geo->cylinders = mddev->array_sectors / 8; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5736 | return 0; |
| 5737 | } |
| 5738 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5739 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5740 | unsigned int cmd, unsigned long arg) |
| 5741 | { |
| 5742 | int err = 0; |
| 5743 | void __user *argp = (void __user *)arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5744 | mddev_t *mddev = NULL; |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 5745 | int ro; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5746 | |
| 5747 | if (!capable(CAP_SYS_ADMIN)) |
| 5748 | return -EACCES; |
| 5749 | |
| 5750 | /* |
| 5751 | * Commands dealing with the RAID driver but not any |
| 5752 | * particular array: |
| 5753 | */ |
| 5754 | switch (cmd) |
| 5755 | { |
| 5756 | case RAID_VERSION: |
| 5757 | err = get_version(argp); |
| 5758 | goto done; |
| 5759 | |
| 5760 | case PRINT_RAID_DEBUG: |
| 5761 | err = 0; |
| 5762 | md_print_devices(); |
| 5763 | goto done; |
| 5764 | |
| 5765 | #ifndef MODULE |
| 5766 | case RAID_AUTORUN: |
| 5767 | err = 0; |
| 5768 | autostart_arrays(arg); |
| 5769 | goto done; |
| 5770 | #endif |
| 5771 | default:; |
| 5772 | } |
| 5773 | |
| 5774 | /* |
| 5775 | * Commands creating/starting a new array: |
| 5776 | */ |
| 5777 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5778 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5779 | |
| 5780 | if (!mddev) { |
| 5781 | BUG(); |
| 5782 | goto abort; |
| 5783 | } |
| 5784 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5785 | err = mddev_lock(mddev); |
| 5786 | if (err) { |
| 5787 | printk(KERN_INFO |
| 5788 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 5789 | err, cmd); |
| 5790 | goto abort; |
| 5791 | } |
| 5792 | |
| 5793 | switch (cmd) |
| 5794 | { |
| 5795 | case SET_ARRAY_INFO: |
| 5796 | { |
| 5797 | mdu_array_info_t info; |
| 5798 | if (!arg) |
| 5799 | memset(&info, 0, sizeof(info)); |
| 5800 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 5801 | err = -EFAULT; |
| 5802 | goto abort_unlock; |
| 5803 | } |
| 5804 | if (mddev->pers) { |
| 5805 | err = update_array_info(mddev, &info); |
| 5806 | if (err) { |
| 5807 | printk(KERN_WARNING "md: couldn't update" |
| 5808 | " array info. %d\n", err); |
| 5809 | goto abort_unlock; |
| 5810 | } |
| 5811 | goto done_unlock; |
| 5812 | } |
| 5813 | if (!list_empty(&mddev->disks)) { |
| 5814 | printk(KERN_WARNING |
| 5815 | "md: array %s already has disks!\n", |
| 5816 | mdname(mddev)); |
| 5817 | err = -EBUSY; |
| 5818 | goto abort_unlock; |
| 5819 | } |
| 5820 | if (mddev->raid_disks) { |
| 5821 | printk(KERN_WARNING |
| 5822 | "md: array %s already initialised!\n", |
| 5823 | mdname(mddev)); |
| 5824 | err = -EBUSY; |
| 5825 | goto abort_unlock; |
| 5826 | } |
| 5827 | err = set_array_info(mddev, &info); |
| 5828 | if (err) { |
| 5829 | printk(KERN_WARNING "md: couldn't set" |
| 5830 | " array info. %d\n", err); |
| 5831 | goto abort_unlock; |
| 5832 | } |
| 5833 | } |
| 5834 | goto done_unlock; |
| 5835 | |
| 5836 | default:; |
| 5837 | } |
| 5838 | |
| 5839 | /* |
| 5840 | * Commands querying/configuring an existing array: |
| 5841 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5842 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5843 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 5844 | if ((!mddev->raid_disks && !mddev->external) |
| 5845 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 5846 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 5847 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5848 | err = -ENODEV; |
| 5849 | goto abort_unlock; |
| 5850 | } |
| 5851 | |
| 5852 | /* |
| 5853 | * Commands even a read-only array can execute: |
| 5854 | */ |
| 5855 | switch (cmd) |
| 5856 | { |
| 5857 | case GET_ARRAY_INFO: |
| 5858 | err = get_array_info(mddev, argp); |
| 5859 | goto done_unlock; |
| 5860 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5861 | case GET_BITMAP_FILE: |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 5862 | err = get_bitmap_file(mddev, argp); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5863 | goto done_unlock; |
| 5864 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5865 | case GET_DISK_INFO: |
| 5866 | err = get_disk_info(mddev, argp); |
| 5867 | goto done_unlock; |
| 5868 | |
| 5869 | case RESTART_ARRAY_RW: |
| 5870 | err = restart_array(mddev); |
| 5871 | goto done_unlock; |
| 5872 | |
| 5873 | case STOP_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5874 | err = do_md_stop(mddev, 0, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5875 | goto done_unlock; |
| 5876 | |
| 5877 | case STOP_ARRAY_RO: |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5878 | err = md_set_readonly(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5879 | goto done_unlock; |
| 5880 | |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 5881 | case BLKROSET: |
| 5882 | if (get_user(ro, (int __user *)(arg))) { |
| 5883 | err = -EFAULT; |
| 5884 | goto done_unlock; |
| 5885 | } |
| 5886 | err = -EINVAL; |
| 5887 | |
| 5888 | /* if the bdev is going readonly the value of mddev->ro |
| 5889 | * does not matter, no writes are coming |
| 5890 | */ |
| 5891 | if (ro) |
| 5892 | goto done_unlock; |
| 5893 | |
| 5894 | /* are we are already prepared for writes? */ |
| 5895 | if (mddev->ro != 1) |
| 5896 | goto done_unlock; |
| 5897 | |
| 5898 | /* transitioning to readauto need only happen for |
| 5899 | * arrays that call md_write_start |
| 5900 | */ |
| 5901 | if (mddev->pers) { |
| 5902 | err = restart_array(mddev); |
| 5903 | if (err == 0) { |
| 5904 | mddev->ro = 2; |
| 5905 | set_disk_ro(mddev->gendisk, 0); |
| 5906 | } |
| 5907 | } |
| 5908 | goto done_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5909 | } |
| 5910 | |
| 5911 | /* |
| 5912 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5913 | * superblock, so we do not allow them on read-only arrays. |
| 5914 | * However non-MD ioctls (e.g. get-size) will still come through |
| 5915 | * here and hit the 'default' below, so only disallow |
| 5916 | * 'md' ioctls, and switch to rw mode if started auto-readonly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5917 | */ |
Andre Noll | bb57fc6 | 2008-04-25 19:06:35 +0200 | [diff] [blame] | 5918 | if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5919 | if (mddev->ro == 2) { |
| 5920 | mddev->ro = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5921 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5922 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5923 | md_wakeup_thread(mddev->thread); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5924 | } else { |
| 5925 | err = -EROFS; |
| 5926 | goto abort_unlock; |
| 5927 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5928 | } |
| 5929 | |
| 5930 | switch (cmd) |
| 5931 | { |
| 5932 | case ADD_NEW_DISK: |
| 5933 | { |
| 5934 | mdu_disk_info_t info; |
| 5935 | if (copy_from_user(&info, argp, sizeof(info))) |
| 5936 | err = -EFAULT; |
| 5937 | else |
| 5938 | err = add_new_disk(mddev, &info); |
| 5939 | goto done_unlock; |
| 5940 | } |
| 5941 | |
| 5942 | case HOT_REMOVE_DISK: |
| 5943 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
| 5944 | goto done_unlock; |
| 5945 | |
| 5946 | case HOT_ADD_DISK: |
| 5947 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
| 5948 | goto done_unlock; |
| 5949 | |
| 5950 | case SET_DISK_FAULTY: |
| 5951 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
| 5952 | goto done_unlock; |
| 5953 | |
| 5954 | case RUN_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5955 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5956 | goto done_unlock; |
| 5957 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5958 | case SET_BITMAP_FILE: |
| 5959 | err = set_bitmap_file(mddev, (int)arg); |
| 5960 | goto done_unlock; |
| 5961 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5962 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5963 | err = -EINVAL; |
| 5964 | goto abort_unlock; |
| 5965 | } |
| 5966 | |
| 5967 | done_unlock: |
| 5968 | abort_unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5969 | if (mddev->hold_active == UNTIL_IOCTL && |
| 5970 | err != -EINVAL) |
| 5971 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5972 | mddev_unlock(mddev); |
| 5973 | |
| 5974 | return err; |
| 5975 | done: |
| 5976 | if (err) |
| 5977 | MD_BUG(); |
| 5978 | abort: |
| 5979 | return err; |
| 5980 | } |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 5981 | #ifdef CONFIG_COMPAT |
| 5982 | static int md_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 5983 | unsigned int cmd, unsigned long arg) |
| 5984 | { |
| 5985 | switch (cmd) { |
| 5986 | case HOT_REMOVE_DISK: |
| 5987 | case HOT_ADD_DISK: |
| 5988 | case SET_DISK_FAULTY: |
| 5989 | case SET_BITMAP_FILE: |
| 5990 | /* These take in integer arg, do not convert */ |
| 5991 | break; |
| 5992 | default: |
| 5993 | arg = (unsigned long)compat_ptr(arg); |
| 5994 | break; |
| 5995 | } |
| 5996 | |
| 5997 | return md_ioctl(bdev, mode, cmd, arg); |
| 5998 | } |
| 5999 | #endif /* CONFIG_COMPAT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6000 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6001 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6002 | { |
| 6003 | /* |
| 6004 | * Succeed if we can lock the mddev, which confirms that |
| 6005 | * it isn't being stopped right now. |
| 6006 | */ |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6007 | mddev_t *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6008 | int err; |
| 6009 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6010 | if (mddev->gendisk != bdev->bd_disk) { |
| 6011 | /* we are racing with mddev_put which is discarding this |
| 6012 | * bd_disk. |
| 6013 | */ |
| 6014 | mddev_put(mddev); |
| 6015 | /* Wait until bdev->bd_disk is definitely gone */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 6016 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6017 | /* Then retry the open from the top */ |
| 6018 | return -ERESTARTSYS; |
| 6019 | } |
| 6020 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 6021 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 6022 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6023 | goto out; |
| 6024 | |
| 6025 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6026 | atomic_inc(&mddev->openers); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 6027 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6028 | |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6029 | check_disk_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6030 | out: |
| 6031 | return err; |
| 6032 | } |
| 6033 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6034 | static int md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6035 | { |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6036 | mddev_t *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6037 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 6038 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6039 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6040 | mddev_put(mddev); |
| 6041 | |
| 6042 | return 0; |
| 6043 | } |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6044 | |
| 6045 | static int md_media_changed(struct gendisk *disk) |
| 6046 | { |
| 6047 | mddev_t *mddev = disk->private_data; |
| 6048 | |
| 6049 | return mddev->changed; |
| 6050 | } |
| 6051 | |
| 6052 | static int md_revalidate(struct gendisk *disk) |
| 6053 | { |
| 6054 | mddev_t *mddev = disk->private_data; |
| 6055 | |
| 6056 | mddev->changed = 0; |
| 6057 | return 0; |
| 6058 | } |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 6059 | static const struct block_device_operations md_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6060 | { |
| 6061 | .owner = THIS_MODULE, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6062 | .open = md_open, |
| 6063 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 6064 | .ioctl = md_ioctl, |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 6065 | #ifdef CONFIG_COMPAT |
| 6066 | .compat_ioctl = md_compat_ioctl, |
| 6067 | #endif |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6068 | .getgeo = md_getgeo, |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6069 | .media_changed = md_media_changed, |
| 6070 | .revalidate_disk= md_revalidate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6071 | }; |
| 6072 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6073 | static int md_thread(void * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6074 | { |
| 6075 | mdk_thread_t *thread = arg; |
| 6076 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6077 | /* |
| 6078 | * md_thread is a 'system-thread', it's priority should be very |
| 6079 | * high. We avoid resource deadlocks individually in each |
| 6080 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 6081 | * the very same RT priority as kswapd, thus we will never get |
| 6082 | * into a priority inversion deadlock. |
| 6083 | * |
| 6084 | * we definitely have to have equal or higher priority than |
| 6085 | * bdflush, otherwise bdflush will deadlock if there are too |
| 6086 | * many dirty RAID5 blocks. |
| 6087 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6088 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 6089 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6090 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6091 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 6092 | /* We need to wait INTERRUPTIBLE so that |
| 6093 | * we don't add to the load-average. |
| 6094 | * That means we need to be sure no signals are |
| 6095 | * pending |
| 6096 | */ |
| 6097 | if (signal_pending(current)) |
| 6098 | flush_signals(current); |
| 6099 | |
| 6100 | wait_event_interruptible_timeout |
| 6101 | (thread->wqueue, |
| 6102 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 6103 | || kthread_should_stop(), |
| 6104 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6105 | |
NeilBrown | 6c98791 | 2011-01-14 09:13:53 +1100 | [diff] [blame] | 6106 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 6107 | if (!kthread_should_stop()) |
NeilBrown | 589a594 | 2010-12-09 17:02:14 +1100 | [diff] [blame] | 6108 | thread->run(thread->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6109 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6110 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6111 | return 0; |
| 6112 | } |
| 6113 | |
| 6114 | void md_wakeup_thread(mdk_thread_t *thread) |
| 6115 | { |
| 6116 | if (thread) { |
| 6117 | dprintk("md: waking up MD thread %s.\n", thread->tsk->comm); |
| 6118 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 6119 | wake_up(&thread->wqueue); |
| 6120 | } |
| 6121 | } |
| 6122 | |
| 6123 | mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev, |
| 6124 | const char *name) |
| 6125 | { |
| 6126 | mdk_thread_t *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6127 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 6128 | thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6129 | if (!thread) |
| 6130 | return NULL; |
| 6131 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6132 | init_waitqueue_head(&thread->wqueue); |
| 6133 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6134 | thread->run = run; |
| 6135 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6136 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 6137 | thread->tsk = kthread_run(md_thread, thread, |
| 6138 | "%s_%s", |
| 6139 | mdname(thread->mddev), |
| 6140 | name ?: mddev->pers->name); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6141 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6142 | kfree(thread); |
| 6143 | return NULL; |
| 6144 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6145 | return thread; |
| 6146 | } |
| 6147 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6148 | void md_unregister_thread(mdk_thread_t *thread) |
| 6149 | { |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6150 | if (!thread) |
| 6151 | return; |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 6152 | dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6153 | |
| 6154 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6155 | kfree(thread); |
| 6156 | } |
| 6157 | |
| 6158 | void md_error(mddev_t *mddev, mdk_rdev_t *rdev) |
| 6159 | { |
| 6160 | if (!mddev) { |
| 6161 | MD_BUG(); |
| 6162 | return; |
| 6163 | } |
| 6164 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6165 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6166 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6167 | |
| 6168 | if (mddev->external) |
| 6169 | set_bit(Blocked, &rdev->flags); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6170 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6171 | dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n", |
| 6172 | mdname(mddev), |
| 6173 | MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev), |
| 6174 | __builtin_return_address(0),__builtin_return_address(1), |
| 6175 | __builtin_return_address(2),__builtin_return_address(3)); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6176 | */ |
Andrew Morton | d0a0a5e | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 6177 | if (!mddev->pers) |
| 6178 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6179 | if (!mddev->pers->error_handler) |
| 6180 | return; |
| 6181 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6182 | if (mddev->degraded) |
| 6183 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6184 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6185 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6186 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6187 | md_wakeup_thread(mddev->thread); |
NeilBrown | 768a418 | 2010-07-26 11:49:55 +1000 | [diff] [blame] | 6188 | if (mddev->event_work.func) |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 6189 | queue_work(md_misc_wq, &mddev->event_work); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 6190 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6191 | } |
| 6192 | |
| 6193 | /* seq_file implementation /proc/mdstat */ |
| 6194 | |
| 6195 | static void status_unused(struct seq_file *seq) |
| 6196 | { |
| 6197 | int i = 0; |
| 6198 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6199 | |
| 6200 | seq_printf(seq, "unused devices: "); |
| 6201 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6202 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6203 | char b[BDEVNAME_SIZE]; |
| 6204 | i++; |
| 6205 | seq_printf(seq, "%s ", |
| 6206 | bdevname(rdev->bdev,b)); |
| 6207 | } |
| 6208 | if (!i) |
| 6209 | seq_printf(seq, "<none>"); |
| 6210 | |
| 6211 | seq_printf(seq, "\n"); |
| 6212 | } |
| 6213 | |
| 6214 | |
| 6215 | static void status_resync(struct seq_file *seq, mddev_t * mddev) |
| 6216 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6217 | sector_t max_sectors, resync, res; |
| 6218 | unsigned long dt, db; |
| 6219 | sector_t rt; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6220 | int scale; |
| 6221 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6222 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6223 | resync = mddev->curr_resync - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6224 | |
| 6225 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6226 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6227 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6228 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6229 | |
| 6230 | /* |
| 6231 | * Should not happen. |
| 6232 | */ |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6233 | if (!max_sectors) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6234 | MD_BUG(); |
| 6235 | return; |
| 6236 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6237 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6238 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6239 | * u32, as those are the requirements for sector_div. |
| 6240 | * Thus 'scale' must be at least 10 |
| 6241 | */ |
| 6242 | scale = 10; |
| 6243 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6244 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6245 | scale++; |
| 6246 | } |
| 6247 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6248 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6249 | |
| 6250 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6251 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6252 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6253 | seq_printf(seq, "["); |
| 6254 | for (i = 0; i < x; i++) |
| 6255 | seq_printf(seq, "="); |
| 6256 | seq_printf(seq, ">"); |
| 6257 | for (i = 0; i < y; i++) |
| 6258 | seq_printf(seq, "."); |
| 6259 | seq_printf(seq, "] "); |
| 6260 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6261 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6262 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 6263 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6264 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 6265 | "check" : |
| 6266 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 6267 | "resync" : "recovery"))), |
| 6268 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6269 | (unsigned long long) resync/2, |
| 6270 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6271 | |
| 6272 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6273 | * dt: time from mark until now |
| 6274 | * db: blocks written from mark until now |
| 6275 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6276 | * |
| 6277 | * rt is a sector_t, so could be 32bit or 64bit. |
| 6278 | * So we divide before multiply in case it is 32bit and close |
| 6279 | * to the limit. |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 6280 | * We scale the divisor (db) by 32 to avoid losing precision |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6281 | * near the end of resync when the number of remaining sectors |
| 6282 | * is close to 'db'. |
| 6283 | * We then divide rt by 32 after multiplying by db to compensate. |
| 6284 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6285 | */ |
| 6286 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 6287 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6288 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 6289 | - mddev->resync_mark_cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6290 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6291 | rt = max_sectors - resync; /* number of remaining sectors */ |
| 6292 | sector_div(rt, db/32+1); |
| 6293 | rt *= dt; |
| 6294 | rt >>= 5; |
| 6295 | |
| 6296 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 6297 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6298 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6299 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6300 | } |
| 6301 | |
| 6302 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 6303 | { |
| 6304 | struct list_head *tmp; |
| 6305 | loff_t l = *pos; |
| 6306 | mddev_t *mddev; |
| 6307 | |
| 6308 | if (l >= 0x10000) |
| 6309 | return NULL; |
| 6310 | if (!l--) |
| 6311 | /* header */ |
| 6312 | return (void*)1; |
| 6313 | |
| 6314 | spin_lock(&all_mddevs_lock); |
| 6315 | list_for_each(tmp,&all_mddevs) |
| 6316 | if (!l--) { |
| 6317 | mddev = list_entry(tmp, mddev_t, all_mddevs); |
| 6318 | mddev_get(mddev); |
| 6319 | spin_unlock(&all_mddevs_lock); |
| 6320 | return mddev; |
| 6321 | } |
| 6322 | spin_unlock(&all_mddevs_lock); |
| 6323 | if (!l--) |
| 6324 | return (void*)2;/* tail */ |
| 6325 | return NULL; |
| 6326 | } |
| 6327 | |
| 6328 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 6329 | { |
| 6330 | struct list_head *tmp; |
| 6331 | mddev_t *next_mddev, *mddev = v; |
| 6332 | |
| 6333 | ++*pos; |
| 6334 | if (v == (void*)2) |
| 6335 | return NULL; |
| 6336 | |
| 6337 | spin_lock(&all_mddevs_lock); |
| 6338 | if (v == (void*)1) |
| 6339 | tmp = all_mddevs.next; |
| 6340 | else |
| 6341 | tmp = mddev->all_mddevs.next; |
| 6342 | if (tmp != &all_mddevs) |
| 6343 | next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs)); |
| 6344 | else { |
| 6345 | next_mddev = (void*)2; |
| 6346 | *pos = 0x10000; |
| 6347 | } |
| 6348 | spin_unlock(&all_mddevs_lock); |
| 6349 | |
| 6350 | if (v != (void*)1) |
| 6351 | mddev_put(mddev); |
| 6352 | return next_mddev; |
| 6353 | |
| 6354 | } |
| 6355 | |
| 6356 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 6357 | { |
| 6358 | mddev_t *mddev = v; |
| 6359 | |
| 6360 | if (mddev && v != (void*)1 && v != (void*)2) |
| 6361 | mddev_put(mddev); |
| 6362 | } |
| 6363 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6364 | struct mdstat_info { |
| 6365 | int event; |
| 6366 | }; |
| 6367 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6368 | static int md_seq_show(struct seq_file *seq, void *v) |
| 6369 | { |
| 6370 | mddev_t *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6371 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6372 | mdk_rdev_t *rdev; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6373 | struct mdstat_info *mi = seq->private; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6374 | struct bitmap *bitmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6375 | |
| 6376 | if (v == (void*)1) { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6377 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6378 | seq_printf(seq, "Personalities : "); |
| 6379 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6380 | list_for_each_entry(pers, &pers_list, list) |
| 6381 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6382 | |
| 6383 | spin_unlock(&pers_lock); |
| 6384 | seq_printf(seq, "\n"); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6385 | mi->event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6386 | return 0; |
| 6387 | } |
| 6388 | if (v == (void*)2) { |
| 6389 | status_unused(seq); |
| 6390 | return 0; |
| 6391 | } |
| 6392 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6393 | if (mddev_lock(mddev) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6394 | return -EINTR; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6395 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6396 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 6397 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 6398 | mddev->pers ? "" : "in"); |
| 6399 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6400 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6401 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6402 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 6403 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6404 | seq_printf(seq, " %s", mddev->pers->name); |
| 6405 | } |
| 6406 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6407 | sectors = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6408 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6409 | char b[BDEVNAME_SIZE]; |
| 6410 | seq_printf(seq, " %s[%d]", |
| 6411 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6412 | if (test_bit(WriteMostly, &rdev->flags)) |
| 6413 | seq_printf(seq, "(W)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6414 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6415 | seq_printf(seq, "(F)"); |
| 6416 | continue; |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6417 | } else if (rdev->raid_disk < 0) |
| 6418 | seq_printf(seq, "(S)"); /* spare */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6419 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6420 | } |
| 6421 | |
| 6422 | if (!list_empty(&mddev->disks)) { |
| 6423 | if (mddev->pers) |
| 6424 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 6425 | (unsigned long long) |
| 6426 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6427 | else |
| 6428 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6429 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6430 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6431 | if (mddev->persistent) { |
| 6432 | if (mddev->major_version != 0 || |
| 6433 | mddev->minor_version != 90) { |
| 6434 | seq_printf(seq," super %d.%d", |
| 6435 | mddev->major_version, |
| 6436 | mddev->minor_version); |
| 6437 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6438 | } else if (mddev->external) |
| 6439 | seq_printf(seq, " super external:%s", |
| 6440 | mddev->metadata_type); |
| 6441 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6442 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6443 | |
| 6444 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6445 | mddev->pers->status(seq, mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6446 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6447 | if (mddev->pers->sync_request) { |
| 6448 | if (mddev->curr_resync > 2) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6449 | status_resync(seq, mddev); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6450 | seq_printf(seq, "\n "); |
| 6451 | } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2) |
| 6452 | seq_printf(seq, "\tresync=DELAYED\n "); |
| 6453 | else if (mddev->recovery_cp < MaxSector) |
| 6454 | seq_printf(seq, "\tresync=PENDING\n "); |
| 6455 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6456 | } else |
| 6457 | seq_printf(seq, "\n "); |
| 6458 | |
| 6459 | if ((bitmap = mddev->bitmap)) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6460 | unsigned long chunk_kb; |
| 6461 | unsigned long flags; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6462 | spin_lock_irqsave(&bitmap->lock, flags); |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 6463 | chunk_kb = mddev->bitmap_info.chunksize >> 10; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6464 | seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], " |
| 6465 | "%lu%s chunk", |
| 6466 | bitmap->pages - bitmap->missing_pages, |
| 6467 | bitmap->pages, |
| 6468 | (bitmap->pages - bitmap->missing_pages) |
| 6469 | << (PAGE_SHIFT - 10), |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 6470 | chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize, |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6471 | chunk_kb ? "KB" : "B"); |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 6472 | if (bitmap->file) { |
| 6473 | seq_printf(seq, ", file: "); |
Jan Blunck | c32c2f6 | 2008-02-14 19:38:43 -0800 | [diff] [blame] | 6474 | seq_path(seq, &bitmap->file->f_path, " \t\n"); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6475 | } |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 6476 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6477 | seq_printf(seq, "\n"); |
| 6478 | spin_unlock_irqrestore(&bitmap->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6479 | } |
| 6480 | |
| 6481 | seq_printf(seq, "\n"); |
| 6482 | } |
| 6483 | mddev_unlock(mddev); |
| 6484 | |
| 6485 | return 0; |
| 6486 | } |
| 6487 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 6488 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6489 | .start = md_seq_start, |
| 6490 | .next = md_seq_next, |
| 6491 | .stop = md_seq_stop, |
| 6492 | .show = md_seq_show, |
| 6493 | }; |
| 6494 | |
| 6495 | static int md_seq_open(struct inode *inode, struct file *file) |
| 6496 | { |
| 6497 | int error; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6498 | struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL); |
| 6499 | if (mi == NULL) |
| 6500 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6501 | |
| 6502 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6503 | if (error) |
| 6504 | kfree(mi); |
| 6505 | else { |
| 6506 | struct seq_file *p = file->private_data; |
| 6507 | p->private = mi; |
| 6508 | mi->event = atomic_read(&md_event_count); |
| 6509 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6510 | return error; |
| 6511 | } |
| 6512 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6513 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 6514 | { |
| 6515 | struct seq_file *m = filp->private_data; |
| 6516 | struct mdstat_info *mi = m->private; |
| 6517 | int mask; |
| 6518 | |
| 6519 | poll_wait(filp, &md_event_waiters, wait); |
| 6520 | |
| 6521 | /* always allow read */ |
| 6522 | mask = POLLIN | POLLRDNORM; |
| 6523 | |
| 6524 | if (mi->event != atomic_read(&md_event_count)) |
| 6525 | mask |= POLLERR | POLLPRI; |
| 6526 | return mask; |
| 6527 | } |
| 6528 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 6529 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 6530 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6531 | .open = md_seq_open, |
| 6532 | .read = seq_read, |
| 6533 | .llseek = seq_lseek, |
Martin Peschke | c3f94b40 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 6534 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6535 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6536 | }; |
| 6537 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6538 | int register_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6539 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6540 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6541 | list_add_tail(&p->list, &pers_list); |
| 6542 | 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] | 6543 | spin_unlock(&pers_lock); |
| 6544 | return 0; |
| 6545 | } |
| 6546 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6547 | int unregister_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6548 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6549 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6550 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6551 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6552 | spin_unlock(&pers_lock); |
| 6553 | return 0; |
| 6554 | } |
| 6555 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6556 | static int is_mddev_idle(mddev_t *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6557 | { |
| 6558 | mdk_rdev_t * rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6559 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6560 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6561 | |
| 6562 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6563 | rcu_read_lock(); |
| 6564 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6565 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6566 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
| 6567 | (int)part_stat_read(&disk->part0, sectors[1]) - |
| 6568 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 6569 | /* sync IO will cause sync_io to increase before the disk_stats |
| 6570 | * as sync_io is counted when a request starts, and |
| 6571 | * disk_stats is counted when it completes. |
| 6572 | * So resync activity will cause curr_events to be smaller than |
| 6573 | * when there was no such activity. |
| 6574 | * non-sync IO will cause disk_stat to increase without |
| 6575 | * increasing sync_io so curr_events will (eventually) |
| 6576 | * be larger than it was before. Once it becomes |
| 6577 | * substantially larger, the test below will cause |
| 6578 | * the array to appear non-idle, and resync will slow |
| 6579 | * down. |
| 6580 | * If there is a lot of outstanding resync activity when |
| 6581 | * we set last_event to curr_events, then all that activity |
| 6582 | * completing might cause the array to appear non-idle |
| 6583 | * and resync will be slowed down even though there might |
| 6584 | * not have been non-resync activity. This will only |
| 6585 | * happen once though. 'last_events' will soon reflect |
| 6586 | * the state where there is little or no outstanding |
| 6587 | * resync requests, and further resync activity will |
| 6588 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6589 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6590 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6591 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6592 | rdev->last_events = curr_events; |
| 6593 | idle = 0; |
| 6594 | } |
| 6595 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6596 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6597 | return idle; |
| 6598 | } |
| 6599 | |
| 6600 | void md_done_sync(mddev_t *mddev, int blocks, int ok) |
| 6601 | { |
| 6602 | /* another "blocks" (512byte) blocks have been synced */ |
| 6603 | atomic_sub(blocks, &mddev->recovery_active); |
| 6604 | wake_up(&mddev->recovery_wait); |
| 6605 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6606 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6607 | md_wakeup_thread(mddev->thread); |
| 6608 | // stop recovery, signal do_sync .... |
| 6609 | } |
| 6610 | } |
| 6611 | |
| 6612 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6613 | /* md_write_start(mddev, bi) |
| 6614 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6615 | * in superblock) before writing, schedule a superblock update |
| 6616 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6617 | */ |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6618 | void md_write_start(mddev_t *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6619 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6620 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6621 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6622 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6623 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6624 | BUG_ON(mddev->ro == 1); |
| 6625 | if (mddev->ro == 2) { |
| 6626 | /* need to switch to read/write */ |
| 6627 | mddev->ro = 0; |
| 6628 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6629 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 6630 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6631 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6632 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6633 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6634 | if (mddev->safemode == 1) |
| 6635 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6636 | if (mddev->in_sync) { |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6637 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6638 | if (mddev->in_sync) { |
| 6639 | mddev->in_sync = 0; |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6640 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 6641 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6642 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6643 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6644 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6645 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6646 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6647 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6648 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 6649 | wait_event(mddev->sb_wait, |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 6650 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6651 | } |
| 6652 | |
| 6653 | void md_write_end(mddev_t *mddev) |
| 6654 | { |
| 6655 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 6656 | if (mddev->safemode == 2) |
| 6657 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 6658 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6659 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 6660 | } |
| 6661 | } |
| 6662 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6663 | /* md_allow_write(mddev) |
| 6664 | * Calling this ensures that the array is marked 'active' so that writes |
| 6665 | * may proceed without blocking. It is important to call this before |
| 6666 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 6667 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6668 | * |
| 6669 | * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock |
| 6670 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6671 | */ |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6672 | int md_allow_write(mddev_t *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6673 | { |
| 6674 | if (!mddev->pers) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6675 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6676 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6677 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 6678 | if (!mddev->pers->sync_request) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6679 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6680 | |
| 6681 | spin_lock_irq(&mddev->write_lock); |
| 6682 | if (mddev->in_sync) { |
| 6683 | mddev->in_sync = 0; |
| 6684 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 6685 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6686 | if (mddev->safemode_delay && |
| 6687 | mddev->safemode == 0) |
| 6688 | mddev->safemode = 1; |
| 6689 | spin_unlock_irq(&mddev->write_lock); |
| 6690 | md_update_sb(mddev, 0); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6691 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6692 | } else |
| 6693 | spin_unlock_irq(&mddev->write_lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6694 | |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 6695 | if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6696 | return -EAGAIN; |
| 6697 | else |
| 6698 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6699 | } |
| 6700 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 6701 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6702 | #define SYNC_MARKS 10 |
| 6703 | #define SYNC_MARK_STEP (3*HZ) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6704 | void md_do_sync(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6705 | { |
| 6706 | mddev_t *mddev2; |
| 6707 | unsigned int currspeed = 0, |
| 6708 | window; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6709 | sector_t max_sectors,j, io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6710 | unsigned long mark[SYNC_MARKS]; |
| 6711 | sector_t mark_cnt[SYNC_MARKS]; |
| 6712 | int last_mark,m; |
| 6713 | struct list_head *tmp; |
| 6714 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6715 | int skipped = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6716 | mdk_rdev_t *rdev; |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6717 | char *desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6718 | |
| 6719 | /* just incase thread restarts... */ |
| 6720 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 6721 | return; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6722 | if (mddev->ro) /* never try to sync a read-only array */ |
| 6723 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6724 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6725 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6726 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 6727 | desc = "data-check"; |
| 6728 | else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6729 | desc = "requested-resync"; |
| 6730 | else |
| 6731 | desc = "resync"; |
| 6732 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 6733 | desc = "reshape"; |
| 6734 | else |
| 6735 | desc = "recovery"; |
| 6736 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6737 | /* we overload curr_resync somewhat here. |
| 6738 | * 0 == not engaged in resync at all |
| 6739 | * 2 == checking that there is no conflict with another sync |
| 6740 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 6741 | * commense |
| 6742 | * other == active in resync - this many blocks |
| 6743 | * |
| 6744 | * Before starting a resync we must have set curr_resync to |
| 6745 | * 2, and then checked that every "conflicting" array has curr_resync |
| 6746 | * less than ours. When we find one that is the same or higher |
| 6747 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 6748 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 6749 | * This will mean we have to start checking from the beginning again. |
| 6750 | * |
| 6751 | */ |
| 6752 | |
| 6753 | do { |
| 6754 | mddev->curr_resync = 2; |
| 6755 | |
| 6756 | try_again: |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 6757 | if (kthread_should_stop()) |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 6758 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 6759 | |
| 6760 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6761 | goto skip; |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6762 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6763 | if (mddev2 == mddev) |
| 6764 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 6765 | if (!mddev->parallel_resync |
| 6766 | && mddev2->curr_resync |
| 6767 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6768 | DEFINE_WAIT(wq); |
| 6769 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 6770 | /* arbitrarily yield */ |
| 6771 | mddev->curr_resync = 1; |
| 6772 | wake_up(&resync_wait); |
| 6773 | } |
| 6774 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 6775 | /* no need to wait here, we can wait the next |
| 6776 | * time 'round when curr_resync == 2 |
| 6777 | */ |
| 6778 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 6779 | /* We need to wait 'interruptible' so as not to |
| 6780 | * contribute to the load average, and not to |
| 6781 | * be caught by 'softlockup' |
| 6782 | */ |
| 6783 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 6784 | if (!kthread_should_stop() && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 6785 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6786 | printk(KERN_INFO "md: delaying %s of %s" |
| 6787 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6788 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6789 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6790 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 6791 | if (signal_pending(current)) |
| 6792 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6793 | schedule(); |
| 6794 | finish_wait(&resync_wait, &wq); |
| 6795 | goto try_again; |
| 6796 | } |
| 6797 | finish_wait(&resync_wait, &wq); |
| 6798 | } |
| 6799 | } |
| 6800 | } while (mddev->curr_resync < 2); |
| 6801 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6802 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6803 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6804 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6805 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6806 | */ |
| 6807 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6808 | mddev->resync_mismatches = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6809 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6810 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6811 | j = mddev->resync_min; |
| 6812 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6813 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6814 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6815 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6816 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6817 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6818 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6819 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6820 | j = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6821 | rcu_read_lock(); |
| 6822 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6823 | if (rdev->raid_disk >= 0 && |
| 6824 | !test_bit(Faulty, &rdev->flags) && |
| 6825 | !test_bit(In_sync, &rdev->flags) && |
| 6826 | rdev->recovery_offset < j) |
| 6827 | j = rdev->recovery_offset; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6828 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6829 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6830 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6831 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 6832 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 6833 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 6834 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6835 | "(but not more than %d KB/sec) for %s.\n", |
| 6836 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6837 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6838 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6839 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6840 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6841 | for (m = 0; m < SYNC_MARKS; m++) { |
| 6842 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6843 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6844 | } |
| 6845 | last_mark = 0; |
| 6846 | mddev->resync_mark = mark[last_mark]; |
| 6847 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 6848 | |
| 6849 | /* |
| 6850 | * Tune reconstruction: |
| 6851 | */ |
| 6852 | window = 32*(PAGE_SIZE/512); |
| 6853 | printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n", |
| 6854 | window/2,(unsigned long long) max_sectors/2); |
| 6855 | |
| 6856 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6857 | last_check = 0; |
| 6858 | |
| 6859 | if (j>2) { |
| 6860 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6861 | "md: resuming %s of %s from checkpoint.\n", |
| 6862 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6863 | mddev->curr_resync = j; |
| 6864 | } |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 6865 | mddev->curr_resync_completed = j; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6866 | |
| 6867 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6868 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6869 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6870 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6871 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 6872 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 6873 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 6874 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 6875 | > (max_sectors >> 4)) || |
| 6876 | (j - mddev->curr_resync_completed)*2 |
| 6877 | >= mddev->resync_max - mddev->curr_resync_completed |
| 6878 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6879 | /* time to update curr_resync_completed */ |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6880 | wait_event(mddev->recovery_wait, |
| 6881 | atomic_read(&mddev->recovery_active) == 0); |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 6882 | mddev->curr_resync_completed = j; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 6883 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6884 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6885 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6886 | |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 6887 | while (j >= mddev->resync_max && !kthread_should_stop()) { |
| 6888 | /* As this condition is controlled by user-space, |
| 6889 | * we can block indefinitely, so use '_interruptible' |
| 6890 | * to avoid triggering warnings. |
| 6891 | */ |
| 6892 | flush_signals(current); /* just in case */ |
| 6893 | wait_event_interruptible(mddev->recovery_wait, |
| 6894 | mddev->resync_max > j |
| 6895 | || kthread_should_stop()); |
| 6896 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6897 | |
| 6898 | if (kthread_should_stop()) |
| 6899 | goto interrupted; |
| 6900 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6901 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6902 | currspeed < speed_min(mddev)); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6903 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6904 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6905 | goto out; |
| 6906 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6907 | |
| 6908 | if (!skipped) { /* actual IO requested */ |
| 6909 | io_sectors += sectors; |
| 6910 | atomic_add(sectors, &mddev->recovery_active); |
| 6911 | } |
| 6912 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6913 | j += sectors; |
| 6914 | if (j>1) mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6915 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6916 | if (last_check == 0) |
| 6917 | /* this is the earliers that rebuilt will be |
| 6918 | * visible in /proc/mdstat |
| 6919 | */ |
| 6920 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6921 | |
| 6922 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6923 | continue; |
| 6924 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6925 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6926 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6927 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6928 | break; |
| 6929 | |
| 6930 | repeat: |
| 6931 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 6932 | /* step marks */ |
| 6933 | int next = (last_mark+1) % SYNC_MARKS; |
| 6934 | |
| 6935 | mddev->resync_mark = mark[next]; |
| 6936 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 6937 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6938 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6939 | last_mark = next; |
| 6940 | } |
| 6941 | |
| 6942 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6943 | if (kthread_should_stop()) |
| 6944 | goto interrupted; |
| 6945 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6946 | |
| 6947 | /* |
| 6948 | * this loop exits only if either when we are slower than |
| 6949 | * the 'hard' speed limit, or the system was IO-idle for |
| 6950 | * a jiffy. |
| 6951 | * the system might be non-idle CPU-wise, but we only care |
| 6952 | * about not overloading the IO subsystem. (things like an |
| 6953 | * e2fsck being done on the RAID array should execute fast) |
| 6954 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6955 | cond_resched(); |
| 6956 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6957 | currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2 |
| 6958 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6959 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 6960 | if (currspeed > speed_min(mddev)) { |
| 6961 | if ((currspeed > speed_max(mddev)) || |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6962 | !is_mddev_idle(mddev, 0)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6963 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6964 | goto repeat; |
| 6965 | } |
| 6966 | } |
| 6967 | } |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6968 | printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6969 | /* |
| 6970 | * this also signals 'finished resyncing' to md_stop |
| 6971 | */ |
| 6972 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6973 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 6974 | |
| 6975 | /* tell personality that we are finished */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6976 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6977 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6978 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6979 | mddev->curr_resync > 2) { |
| 6980 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6981 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 6982 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 6983 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6984 | "md: checkpointing %s of %s.\n", |
| 6985 | desc, mdname(mddev)); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6986 | mddev->recovery_cp = mddev->curr_resync; |
| 6987 | } |
| 6988 | } else |
| 6989 | mddev->recovery_cp = MaxSector; |
| 6990 | } else { |
| 6991 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 6992 | mddev->curr_resync = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6993 | rcu_read_lock(); |
| 6994 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6995 | if (rdev->raid_disk >= 0 && |
NeilBrown | 70fffd0 | 2010-06-16 17:01:25 +1000 | [diff] [blame] | 6996 | mddev->delta_disks >= 0 && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6997 | !test_bit(Faulty, &rdev->flags) && |
| 6998 | !test_bit(In_sync, &rdev->flags) && |
| 6999 | rdev->recovery_offset < mddev->curr_resync) |
| 7000 | rdev->recovery_offset = mddev->curr_resync; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7001 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7002 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7003 | } |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 7004 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7005 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7006 | skip: |
NeilBrown | c07b70a | 2009-12-14 12:49:48 +1100 | [diff] [blame] | 7007 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 7008 | /* We completed so min/max setting can be forgotten if used. */ |
| 7009 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7010 | mddev->resync_min = 0; |
| 7011 | mddev->resync_max = MaxSector; |
| 7012 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7013 | mddev->resync_min = mddev->curr_resync_completed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7014 | mddev->curr_resync = 0; |
| 7015 | wake_up(&resync_wait); |
| 7016 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
| 7017 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 7018 | return; |
| 7019 | |
| 7020 | interrupted: |
| 7021 | /* |
| 7022 | * got a signal, exit. |
| 7023 | */ |
| 7024 | printk(KERN_INFO |
| 7025 | "md: md_do_sync() got signal ... exiting\n"); |
| 7026 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7027 | goto out; |
| 7028 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7029 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7030 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7031 | |
| 7032 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7033 | static int remove_and_add_spares(mddev_t *mddev) |
| 7034 | { |
| 7035 | mdk_rdev_t *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7036 | int spares = 0; |
| 7037 | |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7038 | mddev->curr_resync_completed = 0; |
| 7039 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 7040 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7041 | if (rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7042 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7043 | (test_bit(Faulty, &rdev->flags) || |
| 7044 | ! test_bit(In_sync, &rdev->flags)) && |
| 7045 | atomic_read(&rdev->nr_pending)==0) { |
| 7046 | if (mddev->pers->hot_remove_disk( |
| 7047 | mddev, rdev->raid_disk)==0) { |
| 7048 | char nm[20]; |
| 7049 | sprintf(nm,"rd%d", rdev->raid_disk); |
| 7050 | sysfs_remove_link(&mddev->kobj, nm); |
| 7051 | rdev->raid_disk = -1; |
| 7052 | } |
| 7053 | } |
| 7054 | |
NeilBrown | a8c42c7 | 2011-01-31 13:47:13 +1100 | [diff] [blame] | 7055 | if (mddev->degraded && !mddev->recovery_disabled) { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 7056 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7057 | if (rdev->raid_disk >= 0 && |
Dan Williams | e542713 | 2008-07-28 23:28:06 -0700 | [diff] [blame] | 7058 | !test_bit(In_sync, &rdev->flags) && |
| 7059 | !test_bit(Blocked, &rdev->flags)) |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7060 | spares++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7061 | if (rdev->raid_disk < 0 |
| 7062 | && !test_bit(Faulty, &rdev->flags)) { |
| 7063 | rdev->recovery_offset = 0; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 7064 | if (mddev->pers-> |
| 7065 | hot_add_disk(mddev, rdev) == 0) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7066 | char nm[20]; |
| 7067 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 7068 | if (sysfs_create_link(&mddev->kobj, |
| 7069 | &rdev->kobj, nm)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7070 | /* failure here is OK */; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7071 | spares++; |
| 7072 | md_new_event(mddev); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 7073 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7074 | } else |
| 7075 | break; |
| 7076 | } |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7077 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7078 | } |
| 7079 | return spares; |
| 7080 | } |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7081 | |
| 7082 | static void reap_sync_thread(mddev_t *mddev) |
| 7083 | { |
| 7084 | mdk_rdev_t *rdev; |
| 7085 | |
| 7086 | /* resync has finished, collect result */ |
| 7087 | md_unregister_thread(mddev->sync_thread); |
| 7088 | mddev->sync_thread = NULL; |
| 7089 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 7090 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
| 7091 | /* success...*/ |
| 7092 | /* activate any spares */ |
| 7093 | if (mddev->pers->spare_active(mddev)) |
| 7094 | sysfs_notify(&mddev->kobj, NULL, |
| 7095 | "degraded"); |
| 7096 | } |
| 7097 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 7098 | mddev->pers->finish_reshape) |
| 7099 | mddev->pers->finish_reshape(mddev); |
| 7100 | md_update_sb(mddev, 1); |
| 7101 | |
| 7102 | /* if array is no-longer degraded, then any saved_raid_disk |
| 7103 | * information must be scrapped |
| 7104 | */ |
| 7105 | if (!mddev->degraded) |
| 7106 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 7107 | rdev->saved_raid_disk = -1; |
| 7108 | |
| 7109 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7110 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7111 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 7112 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 7113 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 7114 | /* flag recovery needed just to double check */ |
| 7115 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7116 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 7117 | md_new_event(mddev); |
| 7118 | } |
| 7119 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7120 | /* |
| 7121 | * This routine is regularly called by all per-raid-array threads to |
| 7122 | * deal with generic issues like resync and super-block update. |
| 7123 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 7124 | * need this as they never do any recovery or update the superblock. |
| 7125 | * |
| 7126 | * It does not do any resync itself, but rather "forks" off other threads |
| 7127 | * to do that as needed. |
| 7128 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 7129 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7130 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7131 | * and wakeups up this thread which will reap the thread and finish up. |
| 7132 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 7133 | * |
| 7134 | * The overall approach is: |
| 7135 | * 1/ if the superblock needs updating, update it. |
| 7136 | * 2/ If a recovery thread is running, don't do anything else. |
| 7137 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 7138 | * 4/ If there are any faulty devices, remove them. |
| 7139 | * 5/ If array is degraded, try to add spares devices |
| 7140 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 7141 | */ |
| 7142 | void md_check_recovery(mddev_t *mddev) |
| 7143 | { |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 7144 | if (mddev->bitmap) |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 7145 | bitmap_daemon_work(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7146 | |
| 7147 | if (mddev->ro) |
| 7148 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7149 | |
| 7150 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7151 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7152 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 7153 | mdname(mddev)); |
| 7154 | mddev->safemode = 2; |
| 7155 | } |
| 7156 | flush_signals(current); |
| 7157 | } |
| 7158 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7159 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 7160 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7161 | if ( ! ( |
NeilBrown | 126925c | 2010-09-07 17:02:47 +1000 | [diff] [blame] | 7162 | (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7163 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7164 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7165 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7166 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 7167 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7168 | )) |
| 7169 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7170 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 7171 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7172 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7173 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7174 | if (mddev->ro) { |
| 7175 | /* Only thing we do on a ro array is remove |
| 7176 | * failed devices. |
| 7177 | */ |
NeilBrown | a8c42c7 | 2011-01-31 13:47:13 +1100 | [diff] [blame] | 7178 | mdk_rdev_t *rdev; |
| 7179 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 7180 | if (rdev->raid_disk >= 0 && |
| 7181 | !test_bit(Blocked, &rdev->flags) && |
| 7182 | test_bit(Faulty, &rdev->flags) && |
| 7183 | atomic_read(&rdev->nr_pending)==0) { |
| 7184 | if (mddev->pers->hot_remove_disk( |
| 7185 | mddev, rdev->raid_disk)==0) { |
| 7186 | char nm[20]; |
| 7187 | sprintf(nm,"rd%d", rdev->raid_disk); |
| 7188 | sysfs_remove_link(&mddev->kobj, nm); |
| 7189 | rdev->raid_disk = -1; |
| 7190 | } |
| 7191 | } |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7192 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7193 | goto unlock; |
| 7194 | } |
| 7195 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7196 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7197 | int did_change = 0; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7198 | spin_lock_irq(&mddev->write_lock); |
| 7199 | if (mddev->safemode && |
| 7200 | !atomic_read(&mddev->writes_pending) && |
| 7201 | !mddev->in_sync && |
| 7202 | mddev->recovery_cp == MaxSector) { |
| 7203 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7204 | did_change = 1; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7205 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7206 | } |
| 7207 | if (mddev->safemode == 1) |
| 7208 | mddev->safemode = 0; |
| 7209 | spin_unlock_irq(&mddev->write_lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7210 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7211 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7212 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7213 | |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7214 | if (mddev->flags) |
| 7215 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7216 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7217 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 7218 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 7219 | /* resync/recovery still happening */ |
| 7220 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7221 | goto unlock; |
| 7222 | } |
| 7223 | if (mddev->sync_thread) { |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7224 | reap_sync_thread(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7225 | goto unlock; |
| 7226 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7227 | /* Set RUNNING before clearing NEEDED to avoid |
| 7228 | * any transients in the value of "sync_action". |
| 7229 | */ |
| 7230 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7231 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7232 | /* Clear some bits that don't mean anything, but |
| 7233 | * might be left set |
| 7234 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7235 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7236 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7237 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7238 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 7239 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7240 | /* no recovery is running. |
| 7241 | * remove any failed drives, then |
| 7242 | * add spares if possible. |
| 7243 | * Spare are also removed and re-added, to allow |
| 7244 | * the personality to fail the re-add. |
| 7245 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7246 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7247 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 7248 | if (mddev->pers->check_reshape == NULL || |
| 7249 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7250 | /* Cannot proceed */ |
| 7251 | goto unlock; |
| 7252 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7253 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7254 | } else if ((spares = remove_and_add_spares(mddev))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7255 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7256 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 7257 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7258 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7259 | } else if (mddev->recovery_cp < MaxSector) { |
| 7260 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7261 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7262 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 7263 | /* nothing to be done ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7264 | goto unlock; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7265 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7266 | if (mddev->pers->sync_request) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 7267 | if (spares && mddev->bitmap && ! mddev->bitmap->file) { |
| 7268 | /* We are adding a device or devices to an array |
| 7269 | * which has the bitmap stored on all devices. |
| 7270 | * So make sure all bitmap pages get written |
| 7271 | */ |
| 7272 | bitmap_write_all(mddev->bitmap); |
| 7273 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7274 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 7275 | mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 7276 | "resync"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7277 | if (!mddev->sync_thread) { |
| 7278 | printk(KERN_ERR "%s: could not start resync" |
| 7279 | " thread...\n", |
| 7280 | mdname(mddev)); |
| 7281 | /* leave the spares where they are, it shouldn't hurt */ |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7282 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7283 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7284 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 7285 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 7286 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7287 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7288 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7289 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7290 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7291 | } |
| 7292 | unlock: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7293 | if (!mddev->sync_thread) { |
| 7294 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7295 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 7296 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 7297 | if (mddev->sysfs_action) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7298 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7299 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7300 | mddev_unlock(mddev); |
| 7301 | } |
| 7302 | } |
| 7303 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7304 | void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 7305 | { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7306 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7307 | wait_event_timeout(rdev->blocked_wait, |
| 7308 | !test_bit(Blocked, &rdev->flags), |
| 7309 | msecs_to_jiffies(5000)); |
| 7310 | rdev_dec_pending(rdev, mddev); |
| 7311 | } |
| 7312 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 7313 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7314 | static int md_notify_reboot(struct notifier_block *this, |
| 7315 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7316 | { |
| 7317 | struct list_head *tmp; |
| 7318 | mddev_t *mddev; |
| 7319 | |
| 7320 | if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) { |
| 7321 | |
| 7322 | printk(KERN_INFO "md: stopping all md devices.\n"); |
| 7323 | |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7324 | for_each_mddev(mddev, tmp) |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 7325 | if (mddev_trylock(mddev)) { |
NeilBrown | 2b25000 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7326 | /* Force a switch to readonly even array |
| 7327 | * appears to still be in use. Hence |
| 7328 | * the '100'. |
| 7329 | */ |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 7330 | md_set_readonly(mddev, 100); |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 7331 | mddev_unlock(mddev); |
| 7332 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7333 | /* |
| 7334 | * certain more exotic SCSI devices are known to be |
| 7335 | * volatile wrt too early system reboots. While the |
| 7336 | * right place to handle this issue is the given |
| 7337 | * driver, we do want to have a safe RAID driver ... |
| 7338 | */ |
| 7339 | mdelay(1000*1); |
| 7340 | } |
| 7341 | return NOTIFY_DONE; |
| 7342 | } |
| 7343 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7344 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7345 | .notifier_call = md_notify_reboot, |
| 7346 | .next = NULL, |
| 7347 | .priority = INT_MAX, /* before any real devices */ |
| 7348 | }; |
| 7349 | |
| 7350 | static void md_geninit(void) |
| 7351 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7352 | dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t)); |
| 7353 | |
Denis V. Lunev | c7705f344 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 7354 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7355 | } |
| 7356 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7357 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7358 | { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 7359 | int ret = -ENOMEM; |
| 7360 | |
Tejun Heo | ada609e | 2011-01-25 14:35:54 +0100 | [diff] [blame] | 7361 | md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 7362 | if (!md_wq) |
| 7363 | goto err_wq; |
| 7364 | |
| 7365 | md_misc_wq = alloc_workqueue("md_misc", 0, 0); |
| 7366 | if (!md_misc_wq) |
| 7367 | goto err_misc_wq; |
| 7368 | |
| 7369 | if ((ret = register_blkdev(MD_MAJOR, "md")) < 0) |
| 7370 | goto err_md; |
| 7371 | |
| 7372 | if ((ret = register_blkdev(0, "mdp")) < 0) |
| 7373 | goto err_mdp; |
| 7374 | mdp_major = ret; |
| 7375 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7376 | blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE, |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 7377 | md_probe, NULL, NULL); |
| 7378 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7379 | md_probe, NULL, NULL); |
| 7380 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7381 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 7382 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7383 | |
| 7384 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 7385 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7386 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 7387 | err_mdp: |
| 7388 | unregister_blkdev(MD_MAJOR, "md"); |
| 7389 | err_md: |
| 7390 | destroy_workqueue(md_misc_wq); |
| 7391 | err_misc_wq: |
| 7392 | destroy_workqueue(md_wq); |
| 7393 | err_wq: |
| 7394 | return ret; |
| 7395 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7396 | |
| 7397 | #ifndef MODULE |
| 7398 | |
| 7399 | /* |
| 7400 | * Searches all registered partitions for autorun RAID arrays |
| 7401 | * at boot time. |
| 7402 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7403 | |
| 7404 | static LIST_HEAD(all_detected_devices); |
| 7405 | struct detected_devices_node { |
| 7406 | struct list_head list; |
| 7407 | dev_t dev; |
| 7408 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7409 | |
| 7410 | void md_autodetect_dev(dev_t dev) |
| 7411 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7412 | struct detected_devices_node *node_detected_dev; |
| 7413 | |
| 7414 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 7415 | if (node_detected_dev) { |
| 7416 | node_detected_dev->dev = dev; |
| 7417 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 7418 | } else { |
| 7419 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 7420 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 7421 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7422 | } |
| 7423 | |
| 7424 | |
| 7425 | static void autostart_arrays(int part) |
| 7426 | { |
| 7427 | mdk_rdev_t *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7428 | struct detected_devices_node *node_detected_dev; |
| 7429 | dev_t dev; |
| 7430 | int i_scanned, i_passed; |
| 7431 | |
| 7432 | i_scanned = 0; |
| 7433 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7434 | |
| 7435 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 7436 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7437 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 7438 | i_scanned++; |
| 7439 | node_detected_dev = list_entry(all_detected_devices.next, |
| 7440 | struct detected_devices_node, list); |
| 7441 | list_del(&node_detected_dev->list); |
| 7442 | dev = node_detected_dev->dev; |
| 7443 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 7444 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7445 | if (IS_ERR(rdev)) |
| 7446 | continue; |
| 7447 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7448 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7449 | MD_BUG(); |
| 7450 | continue; |
| 7451 | } |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 7452 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7453 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7454 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7455 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7456 | |
| 7457 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 7458 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7459 | |
| 7460 | autorun_devices(part); |
| 7461 | } |
| 7462 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 7463 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7464 | |
| 7465 | static __exit void md_exit(void) |
| 7466 | { |
| 7467 | mddev_t *mddev; |
| 7468 | struct list_head *tmp; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 7469 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7470 | blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS); |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 7471 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7472 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7473 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7474 | unregister_blkdev(mdp_major, "mdp"); |
| 7475 | unregister_reboot_notifier(&md_notifier); |
| 7476 | unregister_sysctl_table(raid_table_header); |
| 7477 | remove_proc_entry("mdstat", NULL); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7478 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7479 | export_array(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7480 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7481 | } |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 7482 | destroy_workqueue(md_misc_wq); |
| 7483 | destroy_workqueue(md_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7484 | } |
| 7485 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 7486 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7487 | module_exit(md_exit) |
| 7488 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7489 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 7490 | { |
| 7491 | return sprintf(buffer, "%d", start_readonly); |
| 7492 | } |
| 7493 | static int set_ro(const char *val, struct kernel_param *kp) |
| 7494 | { |
| 7495 | char *e; |
| 7496 | int num = simple_strtoul(val, &e, 10); |
| 7497 | if (*val && (*e == '\0' || *e == '\n')) { |
| 7498 | start_readonly = num; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 7499 | return 0; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7500 | } |
| 7501 | return -EINVAL; |
| 7502 | } |
| 7503 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 7504 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 7505 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 7506 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7507 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7508 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7509 | EXPORT_SYMBOL(register_md_personality); |
| 7510 | EXPORT_SYMBOL(unregister_md_personality); |
| 7511 | EXPORT_SYMBOL(md_error); |
| 7512 | EXPORT_SYMBOL(md_done_sync); |
| 7513 | EXPORT_SYMBOL(md_write_start); |
| 7514 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7515 | EXPORT_SYMBOL(md_register_thread); |
| 7516 | EXPORT_SYMBOL(md_unregister_thread); |
| 7517 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7518 | EXPORT_SYMBOL(md_check_recovery); |
| 7519 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 7520 | MODULE_DESCRIPTION("MD RAID framework"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 7521 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 7522 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |