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 | 6e9624b | 2010-08-07 18:25:34 +0200 | [diff] [blame^] | 39 | #include <linux/smp_lock.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 | |
| 60 | |
| 61 | #ifndef MODULE |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 62 | static void autostart_arrays(int part); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | #endif |
| 64 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 65 | static LIST_HEAD(pers_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | static DEFINE_SPINLOCK(pers_lock); |
| 67 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 68 | static void md_print_devices(void); |
| 69 | |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 70 | static DECLARE_WAIT_QUEUE_HEAD(resync_wait); |
| 71 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 72 | #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); } |
| 73 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | /* |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 75 | * Default number of read corrections we'll attempt on an rdev |
| 76 | * before ejecting it from the array. We divide the read error |
| 77 | * count by 2 for every hour elapsed between read errors. |
| 78 | */ |
| 79 | #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20 |
| 80 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit' |
| 82 | * is 1000 KB/sec, so the extra system load does not show up that much. |
| 83 | * Increase it if you want to have more _guaranteed_ speed. Note that |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 84 | * the RAID driver will use the maximum available bandwidth if the IO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | * subsystem is idle. There is also an 'absolute maximum' reconstruction |
| 86 | * speed limit - in case reconstruction slows down your system despite |
| 87 | * idle IO detection. |
| 88 | * |
| 89 | * 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] | 90 | * or /sys/block/mdX/md/sync_speed_{min,max} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | */ |
| 92 | |
| 93 | static int sysctl_speed_limit_min = 1000; |
| 94 | static int sysctl_speed_limit_max = 200000; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 95 | static inline int speed_min(mddev_t *mddev) |
| 96 | { |
| 97 | return mddev->sync_speed_min ? |
| 98 | mddev->sync_speed_min : sysctl_speed_limit_min; |
| 99 | } |
| 100 | |
| 101 | static inline int speed_max(mddev_t *mddev) |
| 102 | { |
| 103 | return mddev->sync_speed_max ? |
| 104 | mddev->sync_speed_max : sysctl_speed_limit_max; |
| 105 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
| 107 | static struct ctl_table_header *raid_table_header; |
| 108 | |
| 109 | static ctl_table raid_table[] = { |
| 110 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | .procname = "speed_limit_min", |
| 112 | .data = &sysctl_speed_limit_min, |
| 113 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 114 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 115 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | }, |
| 117 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | .procname = "speed_limit_max", |
| 119 | .data = &sysctl_speed_limit_max, |
| 120 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 121 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 122 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 124 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | static ctl_table raid_dir_table[] = { |
| 128 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | .procname = "raid", |
| 130 | .maxlen = 0, |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 131 | .mode = S_IRUGO|S_IXUGO, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | .child = raid_table, |
| 133 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 134 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | }; |
| 136 | |
| 137 | static ctl_table raid_root_table[] = { |
| 138 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | .procname = "dev", |
| 140 | .maxlen = 0, |
| 141 | .mode = 0555, |
| 142 | .child = raid_dir_table, |
| 143 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 144 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | }; |
| 146 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 147 | static const struct block_device_operations md_fops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 149 | static int start_readonly; |
| 150 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | /* |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 152 | * We have a system wide 'event count' that is incremented |
| 153 | * on any 'interesting' event, and readers of /proc/mdstat |
| 154 | * can use 'poll' or 'select' to find out when the event |
| 155 | * count increases. |
| 156 | * |
| 157 | * Events are: |
| 158 | * start array, stop array, error, add device, remove device, |
| 159 | * start build, activate spare |
| 160 | */ |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 161 | static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 162 | static atomic_t md_event_count; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 163 | void md_new_event(mddev_t *mddev) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 164 | { |
| 165 | atomic_inc(&md_event_count); |
| 166 | wake_up(&md_event_waiters); |
| 167 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 168 | EXPORT_SYMBOL_GPL(md_new_event); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 169 | |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 170 | /* Alternate version that can be called from interrupts |
| 171 | * when calling sysfs_notify isn't needed. |
| 172 | */ |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 173 | static void md_new_event_inintr(mddev_t *mddev) |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 174 | { |
| 175 | atomic_inc(&md_event_count); |
| 176 | wake_up(&md_event_waiters); |
| 177 | } |
| 178 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 179 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | * Enables to iterate over all existing md arrays |
| 181 | * all_mddevs_lock protects this list. |
| 182 | */ |
| 183 | static LIST_HEAD(all_mddevs); |
| 184 | static DEFINE_SPINLOCK(all_mddevs_lock); |
| 185 | |
| 186 | |
| 187 | /* |
| 188 | * iterates through all used mddevs in the system. |
| 189 | * We take care to grab the all_mddevs_lock whenever navigating |
| 190 | * the list, and to always hold a refcount when unlocked. |
| 191 | * Any code which breaks out of this loop while own |
| 192 | * a reference to the current mddev and must mddev_put it. |
| 193 | */ |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 194 | #define for_each_mddev(mddev,tmp) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | \ |
| 196 | for (({ spin_lock(&all_mddevs_lock); \ |
| 197 | tmp = all_mddevs.next; \ |
| 198 | mddev = NULL;}); \ |
| 199 | ({ if (tmp != &all_mddevs) \ |
| 200 | mddev_get(list_entry(tmp, mddev_t, all_mddevs));\ |
| 201 | spin_unlock(&all_mddevs_lock); \ |
| 202 | if (mddev) mddev_put(mddev); \ |
| 203 | mddev = list_entry(tmp, mddev_t, all_mddevs); \ |
| 204 | tmp != &all_mddevs;}); \ |
| 205 | ({ spin_lock(&all_mddevs_lock); \ |
| 206 | tmp = tmp->next;}) \ |
| 207 | ) |
| 208 | |
| 209 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 210 | /* Rather than calling directly into the personality make_request function, |
| 211 | * IO requests come here first so that we can check if the device is |
| 212 | * being suspended pending a reconfiguration. |
| 213 | * We hold a refcount over the call to ->make_request. By the time that |
| 214 | * call has finished, the bio has been linked into some internal structure |
| 215 | * and so is visible to ->quiesce(), so we don't need the refcount any more. |
| 216 | */ |
| 217 | static int md_make_request(struct request_queue *q, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | { |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 219 | const int rw = bio_data_dir(bio); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 220 | mddev_t *mddev = q->queuedata; |
| 221 | int rv; |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 222 | int cpu; |
| 223 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 224 | if (mddev == NULL || mddev->pers == NULL) { |
| 225 | bio_io_error(bio); |
| 226 | return 0; |
| 227 | } |
| 228 | rcu_read_lock(); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 229 | if (mddev->suspended || mddev->barrier) { |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 230 | DEFINE_WAIT(__wait); |
| 231 | for (;;) { |
| 232 | prepare_to_wait(&mddev->sb_wait, &__wait, |
| 233 | TASK_UNINTERRUPTIBLE); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 234 | if (!mddev->suspended && !mddev->barrier) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 235 | break; |
| 236 | rcu_read_unlock(); |
| 237 | schedule(); |
| 238 | rcu_read_lock(); |
| 239 | } |
| 240 | finish_wait(&mddev->sb_wait, &__wait); |
| 241 | } |
| 242 | atomic_inc(&mddev->active_io); |
| 243 | rcu_read_unlock(); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 244 | |
NeilBrown | 21a52c6 | 2010-04-01 15:02:13 +1100 | [diff] [blame] | 245 | rv = mddev->pers->make_request(mddev, bio); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 246 | |
| 247 | cpu = part_stat_lock(); |
| 248 | part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]); |
| 249 | part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], |
| 250 | bio_sectors(bio)); |
| 251 | part_stat_unlock(); |
| 252 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 253 | if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended) |
| 254 | wake_up(&mddev->sb_wait); |
| 255 | |
| 256 | return rv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | } |
| 258 | |
NeilBrown | 9e35b99 | 2010-04-06 14:23:02 +1000 | [diff] [blame] | 259 | /* mddev_suspend makes sure no new requests are submitted |
| 260 | * to the device, and that any requests that have been submitted |
| 261 | * are completely handled. |
| 262 | * Once ->stop is called and completes, the module will be completely |
| 263 | * unused. |
| 264 | */ |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 265 | static void mddev_suspend(mddev_t *mddev) |
| 266 | { |
| 267 | BUG_ON(mddev->suspended); |
| 268 | mddev->suspended = 1; |
| 269 | synchronize_rcu(); |
| 270 | wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0); |
| 271 | mddev->pers->quiesce(mddev, 1); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | static void mddev_resume(mddev_t *mddev) |
| 275 | { |
| 276 | mddev->suspended = 0; |
| 277 | wake_up(&mddev->sb_wait); |
| 278 | mddev->pers->quiesce(mddev, 0); |
| 279 | } |
| 280 | |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 281 | int mddev_congested(mddev_t *mddev, int bits) |
| 282 | { |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 283 | if (mddev->barrier) |
| 284 | return 1; |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 285 | return mddev->suspended; |
| 286 | } |
| 287 | EXPORT_SYMBOL(mddev_congested); |
| 288 | |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 289 | /* |
| 290 | * Generic barrier handling for md |
| 291 | */ |
| 292 | |
| 293 | #define POST_REQUEST_BARRIER ((void*)1) |
| 294 | |
| 295 | static void md_end_barrier(struct bio *bio, int err) |
| 296 | { |
| 297 | mdk_rdev_t *rdev = bio->bi_private; |
| 298 | mddev_t *mddev = rdev->mddev; |
| 299 | if (err == -EOPNOTSUPP && mddev->barrier != POST_REQUEST_BARRIER) |
| 300 | set_bit(BIO_EOPNOTSUPP, &mddev->barrier->bi_flags); |
| 301 | |
| 302 | rdev_dec_pending(rdev, mddev); |
| 303 | |
| 304 | if (atomic_dec_and_test(&mddev->flush_pending)) { |
| 305 | if (mddev->barrier == POST_REQUEST_BARRIER) { |
| 306 | /* This was a post-request barrier */ |
| 307 | mddev->barrier = NULL; |
| 308 | wake_up(&mddev->sb_wait); |
| 309 | } else |
| 310 | /* The pre-request barrier has finished */ |
| 311 | schedule_work(&mddev->barrier_work); |
| 312 | } |
| 313 | bio_put(bio); |
| 314 | } |
| 315 | |
| 316 | static void submit_barriers(mddev_t *mddev) |
| 317 | { |
| 318 | mdk_rdev_t *rdev; |
| 319 | |
| 320 | rcu_read_lock(); |
| 321 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
| 322 | if (rdev->raid_disk >= 0 && |
| 323 | !test_bit(Faulty, &rdev->flags)) { |
| 324 | /* Take two references, one is dropped |
| 325 | * when request finishes, one after |
| 326 | * we reclaim rcu_read_lock |
| 327 | */ |
| 328 | struct bio *bi; |
| 329 | atomic_inc(&rdev->nr_pending); |
| 330 | atomic_inc(&rdev->nr_pending); |
| 331 | rcu_read_unlock(); |
| 332 | bi = bio_alloc(GFP_KERNEL, 0); |
| 333 | bi->bi_end_io = md_end_barrier; |
| 334 | bi->bi_private = rdev; |
| 335 | bi->bi_bdev = rdev->bdev; |
| 336 | atomic_inc(&mddev->flush_pending); |
| 337 | submit_bio(WRITE_BARRIER, bi); |
| 338 | rcu_read_lock(); |
| 339 | rdev_dec_pending(rdev, mddev); |
| 340 | } |
| 341 | rcu_read_unlock(); |
| 342 | } |
| 343 | |
| 344 | static void md_submit_barrier(struct work_struct *ws) |
| 345 | { |
| 346 | mddev_t *mddev = container_of(ws, mddev_t, barrier_work); |
| 347 | struct bio *bio = mddev->barrier; |
| 348 | |
| 349 | atomic_set(&mddev->flush_pending, 1); |
| 350 | |
| 351 | if (test_bit(BIO_EOPNOTSUPP, &bio->bi_flags)) |
| 352 | bio_endio(bio, -EOPNOTSUPP); |
| 353 | else if (bio->bi_size == 0) |
| 354 | /* an empty barrier - all done */ |
| 355 | bio_endio(bio, 0); |
| 356 | else { |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 357 | bio->bi_rw &= ~REQ_HARDBARRIER; |
NeilBrown | 21a52c6 | 2010-04-01 15:02:13 +1100 | [diff] [blame] | 358 | if (mddev->pers->make_request(mddev, bio)) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 359 | generic_make_request(bio); |
| 360 | mddev->barrier = POST_REQUEST_BARRIER; |
| 361 | submit_barriers(mddev); |
| 362 | } |
| 363 | if (atomic_dec_and_test(&mddev->flush_pending)) { |
| 364 | mddev->barrier = NULL; |
| 365 | wake_up(&mddev->sb_wait); |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | void md_barrier_request(mddev_t *mddev, struct bio *bio) |
| 370 | { |
| 371 | spin_lock_irq(&mddev->write_lock); |
| 372 | wait_event_lock_irq(mddev->sb_wait, |
| 373 | !mddev->barrier, |
| 374 | mddev->write_lock, /*nothing*/); |
| 375 | mddev->barrier = bio; |
| 376 | spin_unlock_irq(&mddev->write_lock); |
| 377 | |
| 378 | atomic_set(&mddev->flush_pending, 1); |
| 379 | INIT_WORK(&mddev->barrier_work, md_submit_barrier); |
| 380 | |
| 381 | submit_barriers(mddev); |
| 382 | |
| 383 | if (atomic_dec_and_test(&mddev->flush_pending)) |
| 384 | schedule_work(&mddev->barrier_work); |
| 385 | } |
| 386 | EXPORT_SYMBOL(md_barrier_request); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 387 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | static inline mddev_t *mddev_get(mddev_t *mddev) |
| 389 | { |
| 390 | atomic_inc(&mddev->active); |
| 391 | return mddev; |
| 392 | } |
| 393 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 394 | static void mddev_delayed_delete(struct work_struct *ws); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 395 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | static void mddev_put(mddev_t *mddev) |
| 397 | { |
| 398 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
| 399 | return; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 400 | if (!mddev->raid_disks && list_empty(&mddev->disks) && |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 401 | mddev->ctime == 0 && !mddev->hold_active) { |
| 402 | /* Array is not configured at all, and not held active, |
| 403 | * so destroy it */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | list_del(&mddev->all_mddevs); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 405 | if (mddev->gendisk) { |
| 406 | /* we did a probe so need to clean up. |
| 407 | * Call schedule_work inside the spinlock |
| 408 | * so that flush_scheduled_work() after |
| 409 | * mddev_find will succeed in waiting for the |
| 410 | * work to be done. |
| 411 | */ |
| 412 | INIT_WORK(&mddev->del_work, mddev_delayed_delete); |
| 413 | schedule_work(&mddev->del_work); |
| 414 | } else |
| 415 | kfree(mddev); |
| 416 | } |
| 417 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | } |
| 419 | |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 420 | static void mddev_init(mddev_t *mddev) |
| 421 | { |
| 422 | mutex_init(&mddev->open_mutex); |
| 423 | mutex_init(&mddev->reconfig_mutex); |
| 424 | mutex_init(&mddev->bitmap_info.mutex); |
| 425 | INIT_LIST_HEAD(&mddev->disks); |
| 426 | INIT_LIST_HEAD(&mddev->all_mddevs); |
| 427 | init_timer(&mddev->safemode_timer); |
| 428 | atomic_set(&mddev->active, 1); |
| 429 | atomic_set(&mddev->openers, 0); |
| 430 | atomic_set(&mddev->active_io, 0); |
| 431 | spin_lock_init(&mddev->write_lock); |
| 432 | atomic_set(&mddev->flush_pending, 0); |
| 433 | init_waitqueue_head(&mddev->sb_wait); |
| 434 | init_waitqueue_head(&mddev->recovery_wait); |
| 435 | mddev->reshape_position = MaxSector; |
| 436 | mddev->resync_min = 0; |
| 437 | mddev->resync_max = MaxSector; |
| 438 | mddev->level = LEVEL_NONE; |
| 439 | } |
| 440 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | static mddev_t * mddev_find(dev_t unit) |
| 442 | { |
| 443 | mddev_t *mddev, *new = NULL; |
| 444 | |
| 445 | retry: |
| 446 | spin_lock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 448 | if (unit) { |
| 449 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 450 | if (mddev->unit == unit) { |
| 451 | mddev_get(mddev); |
| 452 | spin_unlock(&all_mddevs_lock); |
| 453 | kfree(new); |
| 454 | return mddev; |
| 455 | } |
| 456 | |
| 457 | if (new) { |
| 458 | list_add(&new->all_mddevs, &all_mddevs); |
| 459 | spin_unlock(&all_mddevs_lock); |
| 460 | new->hold_active = UNTIL_IOCTL; |
| 461 | return new; |
| 462 | } |
| 463 | } else if (new) { |
| 464 | /* find an unused unit number */ |
| 465 | static int next_minor = 512; |
| 466 | int start = next_minor; |
| 467 | int is_free = 0; |
| 468 | int dev = 0; |
| 469 | while (!is_free) { |
| 470 | dev = MKDEV(MD_MAJOR, next_minor); |
| 471 | next_minor++; |
| 472 | if (next_minor > MINORMASK) |
| 473 | next_minor = 0; |
| 474 | if (next_minor == start) { |
| 475 | /* Oh dear, all in use. */ |
| 476 | spin_unlock(&all_mddevs_lock); |
| 477 | kfree(new); |
| 478 | return NULL; |
| 479 | } |
| 480 | |
| 481 | is_free = 1; |
| 482 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 483 | if (mddev->unit == dev) { |
| 484 | is_free = 0; |
| 485 | break; |
| 486 | } |
| 487 | } |
| 488 | new->unit = dev; |
| 489 | new->md_minor = MINOR(dev); |
| 490 | new->hold_active = UNTIL_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | list_add(&new->all_mddevs, &all_mddevs); |
| 492 | spin_unlock(&all_mddevs_lock); |
| 493 | return new; |
| 494 | } |
| 495 | spin_unlock(&all_mddevs_lock); |
| 496 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 497 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | if (!new) |
| 499 | return NULL; |
| 500 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | new->unit = unit; |
| 502 | if (MAJOR(unit) == MD_MAJOR) |
| 503 | new->md_minor = MINOR(unit); |
| 504 | else |
| 505 | new->md_minor = MINOR(unit) >> MdpMinorShift; |
| 506 | |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 507 | mddev_init(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | goto retry; |
| 510 | } |
| 511 | |
| 512 | static inline int mddev_lock(mddev_t * mddev) |
| 513 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 514 | return mutex_lock_interruptible(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | } |
| 516 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 517 | static inline int mddev_is_locked(mddev_t *mddev) |
| 518 | { |
| 519 | return mutex_is_locked(&mddev->reconfig_mutex); |
| 520 | } |
| 521 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | static inline int mddev_trylock(mddev_t * mddev) |
| 523 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 524 | return mutex_trylock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | } |
| 526 | |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 527 | static struct attribute_group md_redundancy_group; |
| 528 | |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 529 | static void mddev_unlock(mddev_t * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | { |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 531 | if (mddev->to_remove) { |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 532 | /* These cannot be removed under reconfig_mutex as |
| 533 | * an access to the files will try to take reconfig_mutex |
| 534 | * while holding the file unremovable, which leads to |
| 535 | * a deadlock. |
| 536 | * So hold open_mutex instead - we are allowed to take |
| 537 | * it while holding reconfig_mutex, and md_run can |
| 538 | * use it to wait for the remove to complete. |
| 539 | */ |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 540 | struct attribute_group *to_remove = mddev->to_remove; |
| 541 | mddev->to_remove = NULL; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 542 | mutex_lock(&mddev->open_mutex); |
| 543 | mutex_unlock(&mddev->reconfig_mutex); |
| 544 | |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 545 | if (to_remove != &md_redundancy_group) |
| 546 | sysfs_remove_group(&mddev->kobj, to_remove); |
| 547 | if (mddev->pers == NULL || |
| 548 | mddev->pers->sync_request == NULL) { |
| 549 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 550 | if (mddev->sysfs_action) |
| 551 | sysfs_put(mddev->sysfs_action); |
| 552 | mddev->sysfs_action = NULL; |
| 553 | } |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 554 | mutex_unlock(&mddev->open_mutex); |
| 555 | } else |
| 556 | mutex_unlock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 558 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | } |
| 560 | |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 561 | static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 563 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 565 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | if (rdev->desc_nr == nr) |
| 567 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 568 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | return NULL; |
| 570 | } |
| 571 | |
| 572 | static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev) |
| 573 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | mdk_rdev_t *rdev; |
| 575 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 576 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | if (rdev->bdev->bd_dev == dev) |
| 578 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 579 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | return NULL; |
| 581 | } |
| 582 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 583 | static struct mdk_personality *find_pers(int level, char *clevel) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 584 | { |
| 585 | struct mdk_personality *pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 586 | list_for_each_entry(pers, &pers_list, list) { |
| 587 | if (level != LEVEL_NONE && pers->level == level) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 588 | return pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 589 | if (strcmp(pers->name, clevel)==0) |
| 590 | return pers; |
| 591 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 592 | return NULL; |
| 593 | } |
| 594 | |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 595 | /* return the offset of the super block in 512byte sectors */ |
Jesper Juhl | 77933d7 | 2005-07-27 11:46:09 -0700 | [diff] [blame] | 596 | static inline sector_t calc_dev_sboffset(struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | { |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 598 | sector_t num_sectors = bdev->bd_inode->i_size / 512; |
| 599 | return MD_NEW_SIZE_SECTORS(num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | } |
| 601 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | static int alloc_disk_sb(mdk_rdev_t * rdev) |
| 603 | { |
| 604 | if (rdev->sb_page) |
| 605 | MD_BUG(); |
| 606 | |
| 607 | rdev->sb_page = alloc_page(GFP_KERNEL); |
| 608 | if (!rdev->sb_page) { |
| 609 | printk(KERN_ALERT "md: out of memory.\n"); |
Andre Noll | ebc2433 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 610 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | return 0; |
| 614 | } |
| 615 | |
| 616 | static void free_disk_sb(mdk_rdev_t * rdev) |
| 617 | { |
| 618 | if (rdev->sb_page) { |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 619 | put_page(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | rdev->sb_loaded = 0; |
| 621 | rdev->sb_page = NULL; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 622 | rdev->sb_start = 0; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 623 | rdev->sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | } |
| 625 | } |
| 626 | |
| 627 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 628 | static void super_written(struct bio *bio, int error) |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 629 | { |
| 630 | mdk_rdev_t *rdev = bio->bi_private; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 631 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 632 | |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 633 | if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) { |
| 634 | printk("md: super_written gets error=%d, uptodate=%d\n", |
| 635 | error, test_bit(BIO_UPTODATE, &bio->bi_flags)); |
| 636 | WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags)); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 637 | md_error(mddev, rdev); |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 638 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 639 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 640 | if (atomic_dec_and_test(&mddev->pending_writes)) |
| 641 | wake_up(&mddev->sb_wait); |
Neil Brown | f8b58ed | 2005-06-27 22:29:34 -0700 | [diff] [blame] | 642 | bio_put(bio); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 643 | } |
| 644 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 645 | static void super_written_barrier(struct bio *bio, int error) |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 646 | { |
| 647 | struct bio *bio2 = bio->bi_private; |
| 648 | mdk_rdev_t *rdev = bio2->bi_private; |
| 649 | mddev_t *mddev = rdev->mddev; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 650 | |
| 651 | if (!test_bit(BIO_UPTODATE, &bio->bi_flags) && |
| 652 | error == -EOPNOTSUPP) { |
| 653 | unsigned long flags; |
| 654 | /* barriers don't appear to be supported :-( */ |
| 655 | set_bit(BarriersNotsupp, &rdev->flags); |
| 656 | mddev->barriers_work = 0; |
| 657 | spin_lock_irqsave(&mddev->write_lock, flags); |
| 658 | bio2->bi_next = mddev->biolist; |
| 659 | mddev->biolist = bio2; |
| 660 | spin_unlock_irqrestore(&mddev->write_lock, flags); |
| 661 | wake_up(&mddev->sb_wait); |
| 662 | bio_put(bio); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 663 | } else { |
| 664 | bio_put(bio2); |
| 665 | bio->bi_private = rdev; |
| 666 | super_written(bio, error); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 667 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 668 | } |
| 669 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 670 | void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev, |
| 671 | sector_t sector, int size, struct page *page) |
| 672 | { |
| 673 | /* write first size bytes of page to sector of rdev |
| 674 | * Increment mddev->pending_writes before returning |
| 675 | * and decrement it on completion, waking up sb_wait |
| 676 | * if zero is reached. |
| 677 | * If an error occurred, call md_error |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 678 | * |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 679 | * As we might need to resubmit the request if REQ_HARDBARRIER |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 680 | * causes ENOTSUPP, we allocate a spare bio... |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 681 | */ |
| 682 | struct bio *bio = bio_alloc(GFP_NOIO, 1); |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 683 | int rw = REQ_WRITE | REQ_SYNC | REQ_UNPLUG; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 684 | |
| 685 | bio->bi_bdev = rdev->bdev; |
| 686 | bio->bi_sector = sector; |
| 687 | bio_add_page(bio, page, size, 0); |
| 688 | bio->bi_private = rdev; |
| 689 | bio->bi_end_io = super_written; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 690 | bio->bi_rw = rw; |
| 691 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 692 | atomic_inc(&mddev->pending_writes); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 693 | if (!test_bit(BarriersNotsupp, &rdev->flags)) { |
| 694 | struct bio *rbio; |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 695 | rw |= REQ_HARDBARRIER; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 696 | rbio = bio_clone(bio, GFP_NOIO); |
| 697 | rbio->bi_private = bio; |
| 698 | rbio->bi_end_io = super_written_barrier; |
| 699 | submit_bio(rw, rbio); |
| 700 | } else |
| 701 | submit_bio(rw, bio); |
| 702 | } |
| 703 | |
| 704 | void md_super_wait(mddev_t *mddev) |
| 705 | { |
| 706 | /* wait for all superblock writes that were scheduled to complete. |
| 707 | * if any had to be retried (due to BARRIER problems), retry them |
| 708 | */ |
| 709 | DEFINE_WAIT(wq); |
| 710 | for(;;) { |
| 711 | prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE); |
| 712 | if (atomic_read(&mddev->pending_writes)==0) |
| 713 | break; |
| 714 | while (mddev->biolist) { |
| 715 | struct bio *bio; |
| 716 | spin_lock_irq(&mddev->write_lock); |
| 717 | bio = mddev->biolist; |
| 718 | mddev->biolist = bio->bi_next ; |
| 719 | bio->bi_next = NULL; |
| 720 | spin_unlock_irq(&mddev->write_lock); |
| 721 | submit_bio(bio->bi_rw, bio); |
| 722 | } |
| 723 | schedule(); |
| 724 | } |
| 725 | finish_wait(&mddev->sb_wait, &wq); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 726 | } |
| 727 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 728 | static void bi_complete(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | complete((struct completion*)bio->bi_private); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | } |
| 732 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 733 | int sync_page_io(struct block_device *bdev, sector_t sector, int size, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | struct page *page, int rw) |
| 735 | { |
Neil Brown | baaa2c5 | 2005-04-16 15:23:54 -0700 | [diff] [blame] | 736 | struct bio *bio = bio_alloc(GFP_NOIO, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | struct completion event; |
| 738 | int ret; |
| 739 | |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 740 | rw |= REQ_SYNC | REQ_UNPLUG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | |
| 742 | bio->bi_bdev = bdev; |
| 743 | bio->bi_sector = sector; |
| 744 | bio_add_page(bio, page, size, 0); |
| 745 | init_completion(&event); |
| 746 | bio->bi_private = &event; |
| 747 | bio->bi_end_io = bi_complete; |
| 748 | submit_bio(rw, bio); |
| 749 | wait_for_completion(&event); |
| 750 | |
| 751 | ret = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 752 | bio_put(bio); |
| 753 | return ret; |
| 754 | } |
NeilBrown | a8745db | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 755 | EXPORT_SYMBOL_GPL(sync_page_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 757 | static int read_disk_sb(mdk_rdev_t * rdev, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | { |
| 759 | char b[BDEVNAME_SIZE]; |
| 760 | if (!rdev->sb_page) { |
| 761 | MD_BUG(); |
| 762 | return -EINVAL; |
| 763 | } |
| 764 | if (rdev->sb_loaded) |
| 765 | return 0; |
| 766 | |
| 767 | |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 768 | if (!sync_page_io(rdev->bdev, rdev->sb_start, size, rdev->sb_page, READ)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | goto fail; |
| 770 | rdev->sb_loaded = 1; |
| 771 | return 0; |
| 772 | |
| 773 | fail: |
| 774 | printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n", |
| 775 | bdevname(rdev->bdev,b)); |
| 776 | return -EINVAL; |
| 777 | } |
| 778 | |
| 779 | static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 780 | { |
Andre Noll | 0571046 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 781 | return sb1->set_uuid0 == sb2->set_uuid0 && |
| 782 | sb1->set_uuid1 == sb2->set_uuid1 && |
| 783 | sb1->set_uuid2 == sb2->set_uuid2 && |
| 784 | sb1->set_uuid3 == sb2->set_uuid3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | } |
| 786 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 788 | { |
| 789 | int ret; |
| 790 | mdp_super_t *tmp1, *tmp2; |
| 791 | |
| 792 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); |
| 793 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); |
| 794 | |
| 795 | if (!tmp1 || !tmp2) { |
| 796 | ret = 0; |
Andre Noll | 35020f1 | 2008-03-23 15:10:33 +0100 | [diff] [blame] | 797 | printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | goto abort; |
| 799 | } |
| 800 | |
| 801 | *tmp1 = *sb1; |
| 802 | *tmp2 = *sb2; |
| 803 | |
| 804 | /* |
| 805 | * nr_disks is not constant |
| 806 | */ |
| 807 | tmp1->nr_disks = 0; |
| 808 | tmp2->nr_disks = 0; |
| 809 | |
Andre Noll | ce0c8e0 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 810 | ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | abort: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 812 | kfree(tmp1); |
| 813 | kfree(tmp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | return ret; |
| 815 | } |
| 816 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 817 | |
| 818 | static u32 md_csum_fold(u32 csum) |
| 819 | { |
| 820 | csum = (csum & 0xffff) + (csum >> 16); |
| 821 | return (csum & 0xffff) + (csum >> 16); |
| 822 | } |
| 823 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | static unsigned int calc_sb_csum(mdp_super_t * sb) |
| 825 | { |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 826 | u64 newcsum = 0; |
| 827 | u32 *sb32 = (u32*)sb; |
| 828 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | unsigned int disk_csum, csum; |
| 830 | |
| 831 | disk_csum = sb->sb_csum; |
| 832 | sb->sb_csum = 0; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 833 | |
| 834 | for (i = 0; i < MD_SB_BYTES/4 ; i++) |
| 835 | newcsum += sb32[i]; |
| 836 | csum = (newcsum & 0xffffffff) + (newcsum>>32); |
| 837 | |
| 838 | |
| 839 | #ifdef CONFIG_ALPHA |
| 840 | /* This used to use csum_partial, which was wrong for several |
| 841 | * reasons including that different results are returned on |
| 842 | * different architectures. It isn't critical that we get exactly |
| 843 | * the same return value as before (we always csum_fold before |
| 844 | * testing, and that removes any differences). However as we |
| 845 | * know that csum_partial always returned a 16bit value on |
| 846 | * alphas, do a fold to maximise conformity to previous behaviour. |
| 847 | */ |
| 848 | sb->sb_csum = md_csum_fold(disk_csum); |
| 849 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | sb->sb_csum = disk_csum; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 851 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | return csum; |
| 853 | } |
| 854 | |
| 855 | |
| 856 | /* |
| 857 | * Handle superblock details. |
| 858 | * We want to be able to handle multiple superblock formats |
| 859 | * so we have a common interface to them all, and an array of |
| 860 | * different handlers. |
| 861 | * We rely on user-space to write the initial superblock, and support |
| 862 | * reading and updating of superblocks. |
| 863 | * Interface methods are: |
| 864 | * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version) |
| 865 | * loads and validates a superblock on dev. |
| 866 | * if refdev != NULL, compare superblocks on both devices |
| 867 | * Return: |
| 868 | * 0 - dev has a superblock that is compatible with refdev |
| 869 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 870 | * so dev should be used as the refdev in future |
| 871 | * -EINVAL superblock incompatible or invalid |
| 872 | * -othererror e.g. -EIO |
| 873 | * |
| 874 | * int validate_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 875 | * Verify that dev is acceptable into mddev. |
| 876 | * The first time, mddev->raid_disks will be 0, and data from |
| 877 | * dev should be merged in. Subsequent calls check that dev |
| 878 | * is new enough. Return 0 or -EINVAL |
| 879 | * |
| 880 | * void sync_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 881 | * Update the superblock for rdev with data in mddev |
| 882 | * This does not write to disc. |
| 883 | * |
| 884 | */ |
| 885 | |
| 886 | struct super_type { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 887 | char *name; |
| 888 | struct module *owner; |
| 889 | int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, |
| 890 | int minor_version); |
| 891 | int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 892 | void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 893 | unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev, |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 894 | sector_t num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | }; |
| 896 | |
| 897 | /* |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 898 | * Check that the given mddev has no bitmap. |
| 899 | * |
| 900 | * This function is called from the run method of all personalities that do not |
| 901 | * support bitmaps. It prints an error message and returns non-zero if mddev |
| 902 | * has a bitmap. Otherwise, it returns 0. |
| 903 | * |
| 904 | */ |
| 905 | int md_check_no_bitmap(mddev_t *mddev) |
| 906 | { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 907 | if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 908 | return 0; |
| 909 | printk(KERN_ERR "%s: bitmaps are not supported for %s\n", |
| 910 | mdname(mddev), mddev->pers->name); |
| 911 | return 1; |
| 912 | } |
| 913 | EXPORT_SYMBOL(md_check_no_bitmap); |
| 914 | |
| 915 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | * load_super for 0.90.0 |
| 917 | */ |
| 918 | static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 919 | { |
| 920 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 921 | mdp_super_t *sb; |
| 922 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | |
| 924 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 925 | * Calculate the position of the superblock (512byte sectors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | * it's at the end of the disk. |
| 927 | * |
| 928 | * It also happens to be a multiple of 4Kb. |
| 929 | */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 930 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 932 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | if (ret) return ret; |
| 934 | |
| 935 | ret = -EINVAL; |
| 936 | |
| 937 | bdevname(rdev->bdev, b); |
| 938 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 939 | |
| 940 | if (sb->md_magic != MD_SB_MAGIC) { |
| 941 | printk(KERN_ERR "md: invalid raid superblock magic on %s\n", |
| 942 | b); |
| 943 | goto abort; |
| 944 | } |
| 945 | |
| 946 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 947 | sb->minor_version < 90 || |
| 948 | sb->minor_version > 91) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | printk(KERN_WARNING "Bad version number %d.%d on %s\n", |
| 950 | sb->major_version, sb->minor_version, |
| 951 | b); |
| 952 | goto abort; |
| 953 | } |
| 954 | |
| 955 | if (sb->raid_disks <= 0) |
| 956 | goto abort; |
| 957 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 958 | 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] | 959 | printk(KERN_WARNING "md: invalid superblock checksum on %s\n", |
| 960 | b); |
| 961 | goto abort; |
| 962 | } |
| 963 | |
| 964 | rdev->preferred_minor = sb->md_minor; |
| 965 | rdev->data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 966 | rdev->sb_size = MD_SB_BYTES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | |
| 968 | if (sb->level == LEVEL_MULTIPATH) |
| 969 | rdev->desc_nr = -1; |
| 970 | else |
| 971 | rdev->desc_nr = sb->this_disk.number; |
| 972 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 973 | if (!refdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 975 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | __u64 ev1, ev2; |
| 977 | mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page); |
| 978 | if (!uuid_equal(refsb, sb)) { |
| 979 | printk(KERN_WARNING "md: %s has different UUID to %s\n", |
| 980 | b, bdevname(refdev->bdev,b2)); |
| 981 | goto abort; |
| 982 | } |
| 983 | if (!sb_equal(refsb, sb)) { |
| 984 | printk(KERN_WARNING "md: %s has same UUID" |
| 985 | " but different superblock to %s\n", |
| 986 | b, bdevname(refdev->bdev, b2)); |
| 987 | goto abort; |
| 988 | } |
| 989 | ev1 = md_event(sb); |
| 990 | ev2 = md_event(refsb); |
| 991 | if (ev1 > ev2) |
| 992 | ret = 1; |
| 993 | else |
| 994 | ret = 0; |
| 995 | } |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 996 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 998 | if (rdev->sectors < sb->size * 2 && sb->level > 1) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 999 | /* "this cannot possibly happen" ... */ |
| 1000 | ret = -EINVAL; |
| 1001 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | abort: |
| 1003 | return ret; |
| 1004 | } |
| 1005 | |
| 1006 | /* |
| 1007 | * validate_super for 0.90.0 |
| 1008 | */ |
| 1009 | static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1010 | { |
| 1011 | mdp_disk_t *desc; |
| 1012 | mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1013 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1014 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1015 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1016 | clear_bit(Faulty, &rdev->flags); |
| 1017 | clear_bit(In_sync, &rdev->flags); |
| 1018 | clear_bit(WriteMostly, &rdev->flags); |
| 1019 | clear_bit(BarriersNotsupp, &rdev->flags); |
| 1020 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | if (mddev->raid_disks == 0) { |
| 1022 | mddev->major_version = 0; |
| 1023 | mddev->minor_version = sb->minor_version; |
| 1024 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1025 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1026 | mddev->chunk_sectors = sb->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | mddev->ctime = sb->ctime; |
| 1028 | mddev->utime = sb->utime; |
| 1029 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1030 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | mddev->layout = sb->layout; |
| 1032 | mddev->raid_disks = sb->raid_disks; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1033 | mddev->dev_sectors = sb->size * 2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1034 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1035 | mddev->bitmap_info.offset = 0; |
| 1036 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1038 | if (mddev->minor_version >= 91) { |
| 1039 | mddev->reshape_position = sb->reshape_position; |
| 1040 | mddev->delta_disks = sb->delta_disks; |
| 1041 | mddev->new_level = sb->new_level; |
| 1042 | mddev->new_layout = sb->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1043 | mddev->new_chunk_sectors = sb->new_chunk >> 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1044 | } else { |
| 1045 | mddev->reshape_position = MaxSector; |
| 1046 | mddev->delta_disks = 0; |
| 1047 | mddev->new_level = mddev->level; |
| 1048 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1049 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1050 | } |
| 1051 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 1053 | mddev->recovery_cp = MaxSector; |
| 1054 | else { |
| 1055 | if (sb->events_hi == sb->cp_events_hi && |
| 1056 | sb->events_lo == sb->cp_events_lo) { |
| 1057 | mddev->recovery_cp = sb->recovery_cp; |
| 1058 | } else |
| 1059 | mddev->recovery_cp = 0; |
| 1060 | } |
| 1061 | |
| 1062 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 1063 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 1064 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 1065 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 1066 | |
| 1067 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1068 | |
| 1069 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1070 | mddev->bitmap_info.file == NULL) |
| 1071 | mddev->bitmap_info.offset = |
| 1072 | mddev->bitmap_info.default_offset; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1073 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1074 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1075 | /* Insist on good event counter while assembling, except |
| 1076 | * for spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1078 | if (sb->disks[rdev->desc_nr].state & ( |
| 1079 | (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE))) |
| 1080 | if (ev1 < mddev->events) |
| 1081 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1082 | } else if (mddev->bitmap) { |
| 1083 | /* if adding to array with a bitmap, then we can accept an |
| 1084 | * older device ... but not too old. |
| 1085 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1086 | if (ev1 < mddev->bitmap->events_cleared) |
| 1087 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1088 | } else { |
| 1089 | if (ev1 < mddev->events) |
| 1090 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1091 | return 0; |
| 1092 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1093 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1094 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | desc = sb->disks + rdev->desc_nr; |
| 1096 | |
| 1097 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1098 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 1099 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 1100 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1101 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | rdev->raid_disk = desc->raid_disk; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1103 | } else if (desc->state & (1<<MD_DISK_ACTIVE)) { |
| 1104 | /* active but not in sync implies recovery up to |
| 1105 | * reshape position. We don't know exactly where |
| 1106 | * that is, so set to zero for now */ |
| 1107 | if (mddev->minor_version >= 91) { |
| 1108 | rdev->recovery_offset = 0; |
| 1109 | rdev->raid_disk = desc->raid_disk; |
| 1110 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1112 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 1113 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1114 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1115 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | return 0; |
| 1117 | } |
| 1118 | |
| 1119 | /* |
| 1120 | * sync_super for 0.90.0 |
| 1121 | */ |
| 1122 | static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1123 | { |
| 1124 | mdp_super_t *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1125 | mdk_rdev_t *rdev2; |
| 1126 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1127 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1128 | |
| 1129 | /* make rdev->sb match mddev data.. |
| 1130 | * |
| 1131 | * 1/ zero out disks |
| 1132 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 1133 | * 3/ any empty disks < next_spare become removed |
| 1134 | * |
| 1135 | * disks[0] gets initialised to REMOVED because |
| 1136 | * we cannot be sure from other fields if it has |
| 1137 | * been initialised or not. |
| 1138 | */ |
| 1139 | int i; |
| 1140 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 1141 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 1142 | rdev->sb_size = MD_SB_BYTES; |
| 1143 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 1145 | |
| 1146 | memset(sb, 0, sizeof(*sb)); |
| 1147 | |
| 1148 | sb->md_magic = MD_SB_MAGIC; |
| 1149 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | sb->patch_version = mddev->patch_version; |
| 1151 | sb->gvalid_words = 0; /* ignored */ |
| 1152 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 1153 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 1154 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 1155 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 1156 | |
| 1157 | sb->ctime = mddev->ctime; |
| 1158 | sb->level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1159 | sb->size = mddev->dev_sectors / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1160 | sb->raid_disks = mddev->raid_disks; |
| 1161 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1162 | sb->not_persistent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1163 | sb->utime = mddev->utime; |
| 1164 | sb->state = 0; |
| 1165 | sb->events_hi = (mddev->events>>32); |
| 1166 | sb->events_lo = (u32)mddev->events; |
| 1167 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1168 | if (mddev->reshape_position == MaxSector) |
| 1169 | sb->minor_version = 90; |
| 1170 | else { |
| 1171 | sb->minor_version = 91; |
| 1172 | sb->reshape_position = mddev->reshape_position; |
| 1173 | sb->new_level = mddev->new_level; |
| 1174 | sb->delta_disks = mddev->delta_disks; |
| 1175 | sb->new_layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1176 | sb->new_chunk = mddev->new_chunk_sectors << 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1177 | } |
| 1178 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1179 | if (mddev->in_sync) |
| 1180 | { |
| 1181 | sb->recovery_cp = mddev->recovery_cp; |
| 1182 | sb->cp_events_hi = (mddev->events>>32); |
| 1183 | sb->cp_events_lo = (u32)mddev->events; |
| 1184 | if (mddev->recovery_cp == MaxSector) |
| 1185 | sb->state = (1<< MD_SB_CLEAN); |
| 1186 | } else |
| 1187 | sb->recovery_cp = 0; |
| 1188 | |
| 1189 | sb->layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1190 | sb->chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1191 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1192 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1193 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 1194 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1196 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1197 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1198 | int desc_nr; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1199 | int is_active = test_bit(In_sync, &rdev2->flags); |
| 1200 | |
| 1201 | if (rdev2->raid_disk >= 0 && |
| 1202 | sb->minor_version >= 91) |
| 1203 | /* we have nowhere to store the recovery_offset, |
| 1204 | * but if it is not below the reshape_position, |
| 1205 | * we can piggy-back on that. |
| 1206 | */ |
| 1207 | is_active = 1; |
| 1208 | if (rdev2->raid_disk < 0 || |
| 1209 | test_bit(Faulty, &rdev2->flags)) |
| 1210 | is_active = 0; |
| 1211 | if (is_active) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1212 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1213 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1214 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1215 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1216 | d = &sb->disks[rdev2->desc_nr]; |
| 1217 | nr_disks++; |
| 1218 | d->number = rdev2->desc_nr; |
| 1219 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 1220 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1221 | if (is_active) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1222 | d->raid_disk = rdev2->raid_disk; |
| 1223 | else |
| 1224 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 1225 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1226 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1227 | else if (is_active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | d->state = (1<<MD_DISK_ACTIVE); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1229 | if (test_bit(In_sync, &rdev2->flags)) |
| 1230 | d->state |= (1<<MD_DISK_SYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | active++; |
| 1232 | working++; |
| 1233 | } else { |
| 1234 | d->state = 0; |
| 1235 | spare++; |
| 1236 | working++; |
| 1237 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1238 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 1239 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 1242 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 1243 | mdp_disk_t *d = &sb->disks[i]; |
| 1244 | if (d->state == 0 && d->number == 0) { |
| 1245 | d->number = i; |
| 1246 | d->raid_disk = i; |
| 1247 | d->state = (1<<MD_DISK_REMOVED); |
| 1248 | d->state |= (1<<MD_DISK_FAULTY); |
| 1249 | failed++; |
| 1250 | } |
| 1251 | } |
| 1252 | sb->nr_disks = nr_disks; |
| 1253 | sb->active_disks = active; |
| 1254 | sb->working_disks = working; |
| 1255 | sb->failed_disks = failed; |
| 1256 | sb->spare_disks = spare; |
| 1257 | |
| 1258 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 1259 | sb->sb_csum = calc_sb_csum(sb); |
| 1260 | } |
| 1261 | |
| 1262 | /* |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1263 | * rdev_size_change for 0.90.0 |
| 1264 | */ |
| 1265 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1266 | 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] | 1267 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1268 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1269 | return 0; /* component must fit device */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1270 | if (rdev->mddev->bitmap_info.offset) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1271 | return 0; /* can't move bitmap */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1272 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1273 | if (!num_sectors || num_sectors > rdev->sb_start) |
| 1274 | num_sectors = rdev->sb_start; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1275 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1276 | rdev->sb_page); |
| 1277 | md_super_wait(rdev->mddev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1278 | return num_sectors / 2; /* kB for sysfs */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1279 | } |
| 1280 | |
| 1281 | |
| 1282 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | * version 1 superblock |
| 1284 | */ |
| 1285 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1286 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1288 | __le32 disk_csum; |
| 1289 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1290 | unsigned long long newcsum; |
| 1291 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1292 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1293 | int i; |
| 1294 | |
| 1295 | disk_csum = sb->sb_csum; |
| 1296 | sb->sb_csum = 0; |
| 1297 | newcsum = 0; |
| 1298 | for (i=0; size>=4; size -= 4 ) |
| 1299 | newcsum += le32_to_cpu(*isuper++); |
| 1300 | |
| 1301 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1302 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1303 | |
| 1304 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1305 | sb->sb_csum = disk_csum; |
| 1306 | return cpu_to_le32(csum); |
| 1307 | } |
| 1308 | |
| 1309 | static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 1310 | { |
| 1311 | struct mdp_superblock_1 *sb; |
| 1312 | int ret; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1313 | sector_t sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1315 | int bmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1316 | |
| 1317 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1318 | * Calculate the position of the superblock in 512byte sectors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1319 | * It is always aligned to a 4K boundary and |
| 1320 | * depeding on minor_version, it can be: |
| 1321 | * 0: At least 8K, but less than 12K, from end of device |
| 1322 | * 1: At start of device |
| 1323 | * 2: 4K from start of device. |
| 1324 | */ |
| 1325 | switch(minor_version) { |
| 1326 | case 0: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1327 | sb_start = rdev->bdev->bd_inode->i_size >> 9; |
| 1328 | sb_start -= 8*2; |
| 1329 | sb_start &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1330 | break; |
| 1331 | case 1: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1332 | sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1333 | break; |
| 1334 | case 2: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1335 | sb_start = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1336 | break; |
| 1337 | default: |
| 1338 | return -EINVAL; |
| 1339 | } |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1340 | rdev->sb_start = sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1341 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1342 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1343 | * and it is safe to read 4k, so we do that |
| 1344 | */ |
| 1345 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 | if (ret) return ret; |
| 1347 | |
| 1348 | |
| 1349 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1350 | |
| 1351 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1352 | sb->major_version != cpu_to_le32(1) || |
| 1353 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1354 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1355 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | return -EINVAL; |
| 1357 | |
| 1358 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
| 1359 | printk("md: invalid superblock checksum on %s\n", |
| 1360 | bdevname(rdev->bdev,b)); |
| 1361 | return -EINVAL; |
| 1362 | } |
| 1363 | if (le64_to_cpu(sb->data_size) < 10) { |
| 1364 | printk("md: data_size too small on %s\n", |
| 1365 | bdevname(rdev->bdev,b)); |
| 1366 | return -EINVAL; |
| 1367 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1368 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 | rdev->preferred_minor = 0xffff; |
| 1370 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1371 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1372 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1373 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1374 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1375 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1376 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1377 | |
| 1378 | if (minor_version |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1379 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1380 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1381 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1382 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1383 | rdev->desc_nr = -1; |
| 1384 | else |
| 1385 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1386 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1387 | if (!refdev) { |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1388 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1389 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1390 | __u64 ev1, ev2; |
| 1391 | struct mdp_superblock_1 *refsb = |
| 1392 | (struct mdp_superblock_1*)page_address(refdev->sb_page); |
| 1393 | |
| 1394 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1395 | sb->level != refsb->level || |
| 1396 | sb->layout != refsb->layout || |
| 1397 | sb->chunksize != refsb->chunksize) { |
| 1398 | printk(KERN_WARNING "md: %s has strangely different" |
| 1399 | " superblock to %s\n", |
| 1400 | bdevname(rdev->bdev,b), |
| 1401 | bdevname(refdev->bdev,b2)); |
| 1402 | return -EINVAL; |
| 1403 | } |
| 1404 | ev1 = le64_to_cpu(sb->events); |
| 1405 | ev2 = le64_to_cpu(refsb->events); |
| 1406 | |
| 1407 | if (ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1408 | ret = 1; |
| 1409 | else |
| 1410 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1411 | } |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1412 | if (minor_version) |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1413 | rdev->sectors = (rdev->bdev->bd_inode->i_size >> 9) - |
| 1414 | le64_to_cpu(sb->data_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1415 | else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1416 | rdev->sectors = rdev->sb_start; |
| 1417 | if (rdev->sectors < le64_to_cpu(sb->data_size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1418 | return -EINVAL; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1419 | rdev->sectors = le64_to_cpu(sb->data_size); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1420 | if (le64_to_cpu(sb->size) > rdev->sectors) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1421 | return -EINVAL; |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1422 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1423 | } |
| 1424 | |
| 1425 | static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1426 | { |
| 1427 | 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] | 1428 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1429 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1430 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1431 | clear_bit(Faulty, &rdev->flags); |
| 1432 | clear_bit(In_sync, &rdev->flags); |
| 1433 | clear_bit(WriteMostly, &rdev->flags); |
| 1434 | clear_bit(BarriersNotsupp, &rdev->flags); |
| 1435 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1436 | if (mddev->raid_disks == 0) { |
| 1437 | mddev->major_version = 1; |
| 1438 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1439 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1440 | mddev->chunk_sectors = le32_to_cpu(sb->chunksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1441 | mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1); |
| 1442 | mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1); |
| 1443 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1444 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1445 | mddev->layout = le32_to_cpu(sb->layout); |
| 1446 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1447 | mddev->dev_sectors = le64_to_cpu(sb->size); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1448 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1449 | mddev->bitmap_info.offset = 0; |
| 1450 | mddev->bitmap_info.default_offset = 1024 >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | |
| 1452 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1453 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1454 | |
| 1455 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1456 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1457 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1458 | mddev->bitmap_info.file == NULL ) |
| 1459 | mddev->bitmap_info.offset = |
| 1460 | (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1461 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1462 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1463 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1464 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1465 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1466 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1467 | mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1468 | } else { |
| 1469 | mddev->reshape_position = MaxSector; |
| 1470 | mddev->delta_disks = 0; |
| 1471 | mddev->new_level = mddev->level; |
| 1472 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1473 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1474 | } |
| 1475 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1476 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1477 | /* Insist of good event counter while assembling, except for |
| 1478 | * spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1479 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1480 | if (rdev->desc_nr >= 0 && |
| 1481 | rdev->desc_nr < le32_to_cpu(sb->max_dev) && |
| 1482 | le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe) |
| 1483 | if (ev1 < mddev->events) |
| 1484 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1485 | } else if (mddev->bitmap) { |
| 1486 | /* If adding to array with a bitmap, then we can accept an |
| 1487 | * older device, but not too old. |
| 1488 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1489 | if (ev1 < mddev->bitmap->events_cleared) |
| 1490 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1491 | } else { |
| 1492 | if (ev1 < mddev->events) |
| 1493 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1494 | return 0; |
| 1495 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1496 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1497 | int role; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1498 | if (rdev->desc_nr < 0 || |
| 1499 | rdev->desc_nr >= le32_to_cpu(sb->max_dev)) { |
| 1500 | role = 0xffff; |
| 1501 | rdev->desc_nr = -1; |
| 1502 | } else |
| 1503 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | switch(role) { |
| 1505 | case 0xffff: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1506 | break; |
| 1507 | case 0xfffe: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1508 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1509 | break; |
| 1510 | default: |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1511 | if ((le32_to_cpu(sb->feature_map) & |
| 1512 | MD_FEATURE_RECOVERY_OFFSET)) |
| 1513 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 1514 | else |
| 1515 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1516 | rdev->raid_disk = role; |
| 1517 | break; |
| 1518 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1519 | if (sb->devflags & WriteMostly1) |
| 1520 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1521 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1522 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1523 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1524 | return 0; |
| 1525 | } |
| 1526 | |
| 1527 | static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1528 | { |
| 1529 | struct mdp_superblock_1 *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1530 | mdk_rdev_t *rdev2; |
| 1531 | int max_dev, i; |
| 1532 | /* make rdev->sb match mddev and rdev data. */ |
| 1533 | |
| 1534 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1535 | |
| 1536 | sb->feature_map = 0; |
| 1537 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1538 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1539 | memset(sb->pad1, 0, sizeof(sb->pad1)); |
| 1540 | memset(sb->pad2, 0, sizeof(sb->pad2)); |
| 1541 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1542 | |
| 1543 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1544 | sb->events = cpu_to_le64(mddev->events); |
| 1545 | if (mddev->in_sync) |
| 1546 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
| 1547 | else |
| 1548 | sb->resync_offset = cpu_to_le64(0); |
| 1549 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1550 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1551 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1552 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1553 | sb->size = cpu_to_le64(mddev->dev_sectors); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1554 | sb->chunksize = cpu_to_le32(mddev->chunk_sectors); |
NeilBrown | 62e1e38 | 2009-05-26 09:40:59 +1000 | [diff] [blame] | 1555 | sb->level = cpu_to_le32(mddev->level); |
| 1556 | sb->layout = cpu_to_le32(mddev->layout); |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1557 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1558 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) { |
| 1559 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1560 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1561 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1562 | |
| 1563 | if (rdev->raid_disk >= 0 && |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1564 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1565 | sb->feature_map |= |
| 1566 | cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 1567 | sb->recovery_offset = |
| 1568 | cpu_to_le64(rdev->recovery_offset); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1569 | } |
| 1570 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1571 | if (mddev->reshape_position != MaxSector) { |
| 1572 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 1573 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1574 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1575 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1576 | sb->new_level = cpu_to_le32(mddev->new_level); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1577 | sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1578 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1579 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1580 | max_dev = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1581 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1582 | if (rdev2->desc_nr+1 > max_dev) |
| 1583 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1584 | |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1585 | if (max_dev > le32_to_cpu(sb->max_dev)) { |
| 1586 | int bmask; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1587 | sb->max_dev = cpu_to_le32(max_dev); |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1588 | rdev->sb_size = max_dev * 2 + 256; |
| 1589 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
| 1590 | if (rdev->sb_size & bmask) |
| 1591 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1592 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1593 | for (i=0; i<max_dev;i++) |
| 1594 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
| 1595 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1596 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1597 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1598 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1599 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1600 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1601 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1602 | else if (rdev2->raid_disk >= 0) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1603 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1604 | else |
| 1605 | sb->dev_roles[i] = cpu_to_le16(0xffff); |
| 1606 | } |
| 1607 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1608 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1609 | } |
| 1610 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1611 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1612 | 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] | 1613 | { |
| 1614 | struct mdp_superblock_1 *sb; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1615 | sector_t max_sectors; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1616 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1617 | return 0; /* component must fit device */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1618 | if (rdev->sb_start < rdev->data_offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1619 | /* minor versions 1 and 2; superblock before data */ |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1620 | max_sectors = rdev->bdev->bd_inode->i_size >> 9; |
| 1621 | max_sectors -= rdev->data_offset; |
| 1622 | if (!num_sectors || num_sectors > max_sectors) |
| 1623 | num_sectors = max_sectors; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1624 | } else if (rdev->mddev->bitmap_info.offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1625 | /* minor version 0 with bitmap we can't move */ |
| 1626 | return 0; |
| 1627 | } else { |
| 1628 | /* minor version 0; superblock after data */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1629 | sector_t sb_start; |
| 1630 | sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2; |
| 1631 | sb_start &= ~(sector_t)(4*2 - 1); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1632 | max_sectors = rdev->sectors + sb_start - rdev->sb_start; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1633 | if (!num_sectors || num_sectors > max_sectors) |
| 1634 | num_sectors = max_sectors; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1635 | rdev->sb_start = sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1636 | } |
| 1637 | sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1638 | sb->data_size = cpu_to_le64(num_sectors); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1639 | sb->super_offset = rdev->sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1640 | sb->sb_csum = calc_sb_1_csum(sb); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1641 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1642 | rdev->sb_page); |
| 1643 | md_super_wait(rdev->mddev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1644 | return num_sectors / 2; /* kB for sysfs */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1645 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1646 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 1647 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1648 | [0] = { |
| 1649 | .name = "0.90.0", |
| 1650 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1651 | .load_super = super_90_load, |
| 1652 | .validate_super = super_90_validate, |
| 1653 | .sync_super = super_90_sync, |
| 1654 | .rdev_size_change = super_90_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1655 | }, |
| 1656 | [1] = { |
| 1657 | .name = "md-1", |
| 1658 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1659 | .load_super = super_1_load, |
| 1660 | .validate_super = super_1_validate, |
| 1661 | .sync_super = super_1_sync, |
| 1662 | .rdev_size_change = super_1_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1663 | }, |
| 1664 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1665 | |
| 1666 | static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2) |
| 1667 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1668 | mdk_rdev_t *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1669 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1670 | rcu_read_lock(); |
| 1671 | rdev_for_each_rcu(rdev, mddev1) |
| 1672 | rdev_for_each_rcu(rdev2, mddev2) |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1673 | if (rdev->bdev->bd_contains == |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1674 | rdev2->bdev->bd_contains) { |
| 1675 | rcu_read_unlock(); |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1676 | return 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1677 | } |
| 1678 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1679 | return 0; |
| 1680 | } |
| 1681 | |
| 1682 | static LIST_HEAD(pending_raid_disks); |
| 1683 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1684 | /* |
| 1685 | * Try to register data integrity profile for an mddev |
| 1686 | * |
| 1687 | * This is called when an array is started and after a disk has been kicked |
| 1688 | * from the array. It only succeeds if all working and active component devices |
| 1689 | * are integrity capable with matching profiles. |
| 1690 | */ |
| 1691 | int md_integrity_register(mddev_t *mddev) |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1692 | { |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1693 | mdk_rdev_t *rdev, *reference = NULL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1694 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1695 | if (list_empty(&mddev->disks)) |
| 1696 | return 0; /* nothing to do */ |
| 1697 | if (blk_get_integrity(mddev->gendisk)) |
| 1698 | return 0; /* already registered */ |
| 1699 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 1700 | /* skip spares and non-functional disks */ |
| 1701 | if (test_bit(Faulty, &rdev->flags)) |
| 1702 | continue; |
| 1703 | if (rdev->raid_disk < 0) |
| 1704 | continue; |
| 1705 | /* |
| 1706 | * If at least one rdev is not integrity capable, we can not |
| 1707 | * enable data integrity for the md device. |
| 1708 | */ |
| 1709 | if (!bdev_get_integrity(rdev->bdev)) |
| 1710 | return -EINVAL; |
| 1711 | if (!reference) { |
| 1712 | /* Use the first rdev as the reference */ |
| 1713 | reference = rdev; |
| 1714 | continue; |
| 1715 | } |
| 1716 | /* does this rdev's profile match the reference profile? */ |
| 1717 | if (blk_integrity_compare(reference->bdev->bd_disk, |
| 1718 | rdev->bdev->bd_disk) < 0) |
| 1719 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1720 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1721 | /* |
| 1722 | * All component devices are integrity capable and have matching |
| 1723 | * profiles, register the common profile for the md device. |
| 1724 | */ |
| 1725 | if (blk_integrity_register(mddev->gendisk, |
| 1726 | bdev_get_integrity(reference->bdev)) != 0) { |
| 1727 | printk(KERN_ERR "md: failed to register integrity for %s\n", |
| 1728 | mdname(mddev)); |
| 1729 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1730 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1731 | printk(KERN_NOTICE "md: data integrity on %s enabled\n", |
| 1732 | mdname(mddev)); |
| 1733 | return 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1734 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1735 | EXPORT_SYMBOL(md_integrity_register); |
| 1736 | |
| 1737 | /* Disable data integrity if non-capable/non-matching disk is being added */ |
| 1738 | void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 1739 | { |
| 1740 | struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev); |
| 1741 | struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk); |
| 1742 | |
| 1743 | if (!bi_mddev) /* nothing to do */ |
| 1744 | return; |
| 1745 | if (rdev->raid_disk < 0) /* skip spares */ |
| 1746 | return; |
| 1747 | if (bi_rdev && blk_integrity_compare(mddev->gendisk, |
| 1748 | rdev->bdev->bd_disk) >= 0) |
| 1749 | return; |
| 1750 | printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev)); |
| 1751 | blk_integrity_unregister(mddev->gendisk); |
| 1752 | } |
| 1753 | EXPORT_SYMBOL(md_integrity_add_rdev); |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1754 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1755 | static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev) |
| 1756 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1757 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 1758 | struct kobject *ko; |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1759 | char *s; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1760 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1761 | |
| 1762 | if (rdev->mddev) { |
| 1763 | MD_BUG(); |
| 1764 | return -EINVAL; |
| 1765 | } |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1766 | |
| 1767 | /* prevent duplicates */ |
| 1768 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 1769 | return -EEXIST; |
| 1770 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1771 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
| 1772 | if (rdev->sectors && (mddev->dev_sectors == 0 || |
| 1773 | rdev->sectors < mddev->dev_sectors)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1774 | if (mddev->pers) { |
| 1775 | /* Cannot change size, so fail |
| 1776 | * If mddev->level <= 0, then we don't care |
| 1777 | * about aligning sizes (e.g. linear) |
| 1778 | */ |
| 1779 | if (mddev->level > 0) |
| 1780 | return -ENOSPC; |
| 1781 | } else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1782 | mddev->dev_sectors = rdev->sectors; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1783 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1784 | |
| 1785 | /* Verify rdev->desc_nr is unique. |
| 1786 | * If it is -1, assign a free number, else |
| 1787 | * check number is not in use |
| 1788 | */ |
| 1789 | if (rdev->desc_nr < 0) { |
| 1790 | int choice = 0; |
| 1791 | if (mddev->pers) choice = mddev->raid_disks; |
| 1792 | while (find_rdev_nr(mddev, choice)) |
| 1793 | choice++; |
| 1794 | rdev->desc_nr = choice; |
| 1795 | } else { |
| 1796 | if (find_rdev_nr(mddev, rdev->desc_nr)) |
| 1797 | return -EBUSY; |
| 1798 | } |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 1799 | if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
| 1800 | printk(KERN_WARNING "md: %s: array is limited to %d devices\n", |
| 1801 | mdname(mddev), mddev->max_disks); |
| 1802 | return -EBUSY; |
| 1803 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1804 | bdevname(rdev->bdev,b); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1805 | while ( (s=strchr(b, '/')) != NULL) |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1806 | *s = '!'; |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1807 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1808 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1809 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1810 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1811 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1812 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1813 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1814 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1815 | if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) { |
| 1816 | kobject_del(&rdev->kobj); |
| 1817 | goto fail; |
| 1818 | } |
Eric W. Biederman | 3ff195b | 2010-03-30 11:31:26 -0700 | [diff] [blame] | 1819 | rdev->sysfs_state = sysfs_get_dirent(rdev->kobj.sd, NULL, "state"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 1820 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1821 | list_add_rcu(&rdev->same_set, &mddev->disks); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1822 | bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk); |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 1823 | |
| 1824 | /* May as well allow recovery to be retried once */ |
| 1825 | mddev->recovery_disabled = 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1826 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1827 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1828 | |
| 1829 | fail: |
| 1830 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", |
| 1831 | b, mdname(mddev)); |
| 1832 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1833 | } |
| 1834 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1835 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1836 | { |
| 1837 | mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work); |
| 1838 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1839 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1840 | } |
| 1841 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1842 | static void unbind_rdev_from_array(mdk_rdev_t * rdev) |
| 1843 | { |
| 1844 | char b[BDEVNAME_SIZE]; |
| 1845 | if (!rdev->mddev) { |
| 1846 | MD_BUG(); |
| 1847 | return; |
| 1848 | } |
Jun'ichi Nomura | 5463c79 | 2006-03-27 01:17:58 -0800 | [diff] [blame] | 1849 | bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1850 | list_del_rcu(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1851 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 1852 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1853 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 1854 | sysfs_put(rdev->sysfs_state); |
| 1855 | rdev->sysfs_state = NULL; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1856 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1857 | * writing to 'remove' to "dev/state". We also need |
| 1858 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1859 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1860 | synchronize_rcu(); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1861 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 1862 | kobject_get(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1863 | schedule_work(&rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1864 | } |
| 1865 | |
| 1866 | /* |
| 1867 | * prevent the device from being mounted, repartitioned or |
| 1868 | * otherwise reused by a RAID array (or any other kernel |
| 1869 | * subsystem), by bd_claiming the device. |
| 1870 | */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1871 | 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] | 1872 | { |
| 1873 | int err = 0; |
| 1874 | struct block_device *bdev; |
| 1875 | char b[BDEVNAME_SIZE]; |
| 1876 | |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1877 | bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1878 | if (IS_ERR(bdev)) { |
| 1879 | printk(KERN_ERR "md: could not open %s.\n", |
| 1880 | __bdevname(dev, b)); |
| 1881 | return PTR_ERR(bdev); |
| 1882 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1883 | err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1884 | if (err) { |
| 1885 | printk(KERN_ERR "md: could not bd_claim %s.\n", |
| 1886 | bdevname(bdev, b)); |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1887 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1888 | return err; |
| 1889 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1890 | if (!shared) |
| 1891 | set_bit(AllReserved, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1892 | rdev->bdev = bdev; |
| 1893 | return err; |
| 1894 | } |
| 1895 | |
| 1896 | static void unlock_rdev(mdk_rdev_t *rdev) |
| 1897 | { |
| 1898 | struct block_device *bdev = rdev->bdev; |
| 1899 | rdev->bdev = NULL; |
| 1900 | if (!bdev) |
| 1901 | MD_BUG(); |
| 1902 | bd_release(bdev); |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1903 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1904 | } |
| 1905 | |
| 1906 | void md_autodetect_dev(dev_t dev); |
| 1907 | |
| 1908 | static void export_rdev(mdk_rdev_t * rdev) |
| 1909 | { |
| 1910 | char b[BDEVNAME_SIZE]; |
| 1911 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 1912 | bdevname(rdev->bdev,b)); |
| 1913 | if (rdev->mddev) |
| 1914 | MD_BUG(); |
| 1915 | free_disk_sb(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1916 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1917 | if (test_bit(AutoDetected, &rdev->flags)) |
| 1918 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1919 | #endif |
| 1920 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1921 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1922 | } |
| 1923 | |
| 1924 | static void kick_rdev_from_array(mdk_rdev_t * rdev) |
| 1925 | { |
| 1926 | unbind_rdev_from_array(rdev); |
| 1927 | export_rdev(rdev); |
| 1928 | } |
| 1929 | |
| 1930 | static void export_array(mddev_t *mddev) |
| 1931 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1932 | mdk_rdev_t *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1933 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1934 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1935 | if (!rdev->mddev) { |
| 1936 | MD_BUG(); |
| 1937 | continue; |
| 1938 | } |
| 1939 | kick_rdev_from_array(rdev); |
| 1940 | } |
| 1941 | if (!list_empty(&mddev->disks)) |
| 1942 | MD_BUG(); |
| 1943 | mddev->raid_disks = 0; |
| 1944 | mddev->major_version = 0; |
| 1945 | } |
| 1946 | |
| 1947 | static void print_desc(mdp_disk_t *desc) |
| 1948 | { |
| 1949 | printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number, |
| 1950 | desc->major,desc->minor,desc->raid_disk,desc->state); |
| 1951 | } |
| 1952 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1953 | static void print_sb_90(mdp_super_t *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1954 | { |
| 1955 | int i; |
| 1956 | |
| 1957 | printk(KERN_INFO |
| 1958 | "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n", |
| 1959 | sb->major_version, sb->minor_version, sb->patch_version, |
| 1960 | sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3, |
| 1961 | sb->ctime); |
| 1962 | printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n", |
| 1963 | sb->level, sb->size, sb->nr_disks, sb->raid_disks, |
| 1964 | sb->md_minor, sb->layout, sb->chunk_size); |
| 1965 | printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d" |
| 1966 | " FD:%d SD:%d CSUM:%08x E:%08lx\n", |
| 1967 | sb->utime, sb->state, sb->active_disks, sb->working_disks, |
| 1968 | sb->failed_disks, sb->spare_disks, |
| 1969 | sb->sb_csum, (unsigned long)sb->events_lo); |
| 1970 | |
| 1971 | printk(KERN_INFO); |
| 1972 | for (i = 0; i < MD_SB_DISKS; i++) { |
| 1973 | mdp_disk_t *desc; |
| 1974 | |
| 1975 | desc = sb->disks + i; |
| 1976 | if (desc->number || desc->major || desc->minor || |
| 1977 | desc->raid_disk || (desc->state && (desc->state != 4))) { |
| 1978 | printk(" D %2d: ", i); |
| 1979 | print_desc(desc); |
| 1980 | } |
| 1981 | } |
| 1982 | printk(KERN_INFO "md: THIS: "); |
| 1983 | print_desc(&sb->this_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1984 | } |
| 1985 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1986 | static void print_sb_1(struct mdp_superblock_1 *sb) |
| 1987 | { |
| 1988 | __u8 *uuid; |
| 1989 | |
| 1990 | uuid = sb->set_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 1991 | printk(KERN_INFO |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 1992 | "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 1993 | "md: Name: \"%s\" CT:%llu\n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1994 | le32_to_cpu(sb->major_version), |
| 1995 | le32_to_cpu(sb->feature_map), |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 1996 | uuid, |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1997 | sb->set_name, |
| 1998 | (unsigned long long)le64_to_cpu(sb->ctime) |
| 1999 | & MD_SUPERBLOCK_1_TIME_SEC_MASK); |
| 2000 | |
| 2001 | uuid = sb->device_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2002 | printk(KERN_INFO |
| 2003 | "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] | 2004 | " RO:%llu\n" |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2005 | "md: Dev:%08x UUID: %pU\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2006 | "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n" |
| 2007 | "md: (MaxDev:%u) \n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2008 | le32_to_cpu(sb->level), |
| 2009 | (unsigned long long)le64_to_cpu(sb->size), |
| 2010 | le32_to_cpu(sb->raid_disks), |
| 2011 | le32_to_cpu(sb->layout), |
| 2012 | le32_to_cpu(sb->chunksize), |
| 2013 | (unsigned long long)le64_to_cpu(sb->data_offset), |
| 2014 | (unsigned long long)le64_to_cpu(sb->data_size), |
| 2015 | (unsigned long long)le64_to_cpu(sb->super_offset), |
| 2016 | (unsigned long long)le64_to_cpu(sb->recovery_offset), |
| 2017 | le32_to_cpu(sb->dev_number), |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2018 | uuid, |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2019 | sb->devflags, |
| 2020 | (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK, |
| 2021 | (unsigned long long)le64_to_cpu(sb->events), |
| 2022 | (unsigned long long)le64_to_cpu(sb->resync_offset), |
| 2023 | le32_to_cpu(sb->sb_csum), |
| 2024 | le32_to_cpu(sb->max_dev) |
| 2025 | ); |
| 2026 | } |
| 2027 | |
| 2028 | static void print_rdev(mdk_rdev_t *rdev, int major_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2029 | { |
| 2030 | char b[BDEVNAME_SIZE]; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2031 | printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n", |
| 2032 | bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors, |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2033 | test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags), |
| 2034 | rdev->desc_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2035 | if (rdev->sb_loaded) { |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2036 | printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version); |
| 2037 | switch (major_version) { |
| 2038 | case 0: |
| 2039 | print_sb_90((mdp_super_t*)page_address(rdev->sb_page)); |
| 2040 | break; |
| 2041 | case 1: |
| 2042 | print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page)); |
| 2043 | break; |
| 2044 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2045 | } else |
| 2046 | printk(KERN_INFO "md: no rdev superblock!\n"); |
| 2047 | } |
| 2048 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 2049 | static void md_print_devices(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2050 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2051 | struct list_head *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2052 | mdk_rdev_t *rdev; |
| 2053 | mddev_t *mddev; |
| 2054 | char b[BDEVNAME_SIZE]; |
| 2055 | |
| 2056 | printk("\n"); |
| 2057 | printk("md: **********************************\n"); |
| 2058 | printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n"); |
| 2059 | printk("md: **********************************\n"); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2060 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2061 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 2062 | if (mddev->bitmap) |
| 2063 | bitmap_print_sb(mddev->bitmap); |
| 2064 | else |
| 2065 | printk("%s: ", mdname(mddev)); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2066 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2067 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 2068 | printk("\n"); |
| 2069 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2070 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2071 | print_rdev(rdev, mddev->major_version); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2072 | } |
| 2073 | printk("md: **********************************\n"); |
| 2074 | printk("\n"); |
| 2075 | } |
| 2076 | |
| 2077 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2078 | static void sync_sbs(mddev_t * mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2079 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2080 | /* Update each superblock (in-memory image), but |
| 2081 | * if we are allowed to, skip spares which already |
| 2082 | * have the right event counter, or have one earlier |
| 2083 | * (which would mean they aren't being marked as dirty |
| 2084 | * with the rest of the array) |
| 2085 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2086 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2087 | |
NeilBrown | 5e86510 | 2009-11-12 12:08:04 +1100 | [diff] [blame] | 2088 | /* First make sure individual recovery_offsets are correct */ |
| 2089 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 2090 | if (rdev->raid_disk >= 0 && |
NeilBrown | 70fffd0 | 2010-06-16 17:01:25 +1000 | [diff] [blame] | 2091 | mddev->delta_disks >= 0 && |
NeilBrown | 5e86510 | 2009-11-12 12:08:04 +1100 | [diff] [blame] | 2092 | !test_bit(In_sync, &rdev->flags) && |
| 2093 | mddev->curr_resync_completed > rdev->recovery_offset) |
| 2094 | rdev->recovery_offset = mddev->curr_resync_completed; |
| 2095 | |
| 2096 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2097 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2098 | if (rdev->sb_events == mddev->events || |
| 2099 | (nospares && |
| 2100 | rdev->raid_disk < 0 && |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2101 | rdev->sb_events+1 == mddev->events)) { |
| 2102 | /* Don't update this superblock */ |
| 2103 | rdev->sb_loaded = 2; |
| 2104 | } else { |
| 2105 | super_types[mddev->major_version]. |
| 2106 | sync_super(mddev, rdev); |
| 2107 | rdev->sb_loaded = 1; |
| 2108 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2109 | } |
| 2110 | } |
| 2111 | |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2112 | static void md_update_sb(mddev_t * mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2113 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2114 | mdk_rdev_t *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2115 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2116 | int nospares = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2117 | |
NeilBrown | 1b57f13 | 2009-06-18 08:48:19 +1000 | [diff] [blame] | 2118 | mddev->utime = get_seconds(); |
NeilBrown | 8377bc8 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 2119 | if (mddev->external) |
| 2120 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2121 | repeat: |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2122 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2123 | |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2124 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 2125 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 2126 | force_change = 1; |
| 2127 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 2128 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 2129 | * though if events isn't the right even/odd, we will have to do |
| 2130 | * spares after all |
| 2131 | */ |
| 2132 | nospares = 1; |
| 2133 | if (force_change) |
| 2134 | nospares = 0; |
| 2135 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2136 | /* If the array is degraded, then skipping spares is both |
| 2137 | * dangerous and fairly pointless. |
| 2138 | * Dangerous because a device that was removed from the array |
| 2139 | * might have a event_count that still looks up-to-date, |
| 2140 | * so it can be re-added without a resync. |
| 2141 | * Pointless because if there are any spares to skip, |
| 2142 | * then a recovery will happen and soon that array won't |
| 2143 | * be degraded any more and the spare can go back to sleep then. |
| 2144 | */ |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2145 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2146 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2147 | sync_req = mddev->in_sync; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2148 | |
| 2149 | /* If this is just a dirty<->clean transition, and the array is clean |
| 2150 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2151 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2152 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2153 | && mddev->can_decrease_events |
| 2154 | && mddev->events != 1) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2155 | mddev->events--; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2156 | mddev->can_decrease_events = 0; |
| 2157 | } else { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2158 | /* otherwise we have to go forward and ... */ |
| 2159 | mddev->events ++; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2160 | mddev->can_decrease_events = nospares; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2161 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2162 | |
| 2163 | if (!mddev->events) { |
| 2164 | /* |
| 2165 | * oops, this 64-bit counter should never wrap. |
| 2166 | * Either we are in around ~1 trillion A.C., assuming |
| 2167 | * 1 reboot per second, or we have a bug: |
| 2168 | */ |
| 2169 | MD_BUG(); |
| 2170 | mddev->events --; |
| 2171 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2172 | |
| 2173 | /* |
| 2174 | * do not write anything to disk if using |
| 2175 | * nonpersistent superblocks |
| 2176 | */ |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2177 | if (!mddev->persistent) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2178 | if (!mddev->external) |
| 2179 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 2180 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2181 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2182 | wake_up(&mddev->sb_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2183 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2184 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2185 | sync_sbs(mddev, nospares); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2186 | spin_unlock_irq(&mddev->write_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2187 | |
| 2188 | dprintk(KERN_INFO |
| 2189 | "md: updating %s RAID superblock on device (in sync %d)\n", |
| 2190 | mdname(mddev),mddev->in_sync); |
| 2191 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 2192 | bitmap_update_sb(mddev->bitmap); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2193 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2194 | char b[BDEVNAME_SIZE]; |
| 2195 | dprintk(KERN_INFO "md: "); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2196 | if (rdev->sb_loaded != 1) |
| 2197 | continue; /* no noise on spare devices */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2198 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2199 | dprintk("(skipping faulty "); |
| 2200 | |
| 2201 | dprintk("%s ", bdevname(rdev->bdev,b)); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2202 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2203 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2204 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2205 | rdev->sb_page); |
| 2206 | dprintk(KERN_INFO "(write) %s's sb offset: %llu\n", |
| 2207 | bdevname(rdev->bdev,b), |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2208 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2209 | rdev->sb_events = mddev->events; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2210 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2211 | } else |
| 2212 | dprintk(")\n"); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2213 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2214 | /* only need to write one superblock... */ |
| 2215 | break; |
| 2216 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2217 | md_super_wait(mddev); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2218 | /* 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] | 2219 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2220 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2221 | if (mddev->in_sync != sync_req || |
| 2222 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2223 | /* have to write it out again */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2224 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2225 | goto repeat; |
| 2226 | } |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2227 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2228 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2229 | wake_up(&mddev->sb_wait); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 2230 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 2231 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2232 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2233 | } |
| 2234 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 2235 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2236 | * We want to accept with case. For this we use cmd_match. |
| 2237 | */ |
| 2238 | static int cmd_match(const char *cmd, const char *str) |
| 2239 | { |
| 2240 | /* See if cmd, written into a sysfs file, matches |
| 2241 | * str. They must either be the same, or cmd can |
| 2242 | * have a trailing newline |
| 2243 | */ |
| 2244 | while (*cmd && *str && *cmd == *str) { |
| 2245 | cmd++; |
| 2246 | str++; |
| 2247 | } |
| 2248 | if (*cmd == '\n') |
| 2249 | cmd++; |
| 2250 | if (*str || *cmd) |
| 2251 | return 0; |
| 2252 | return 1; |
| 2253 | } |
| 2254 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2255 | struct rdev_sysfs_entry { |
| 2256 | struct attribute attr; |
| 2257 | ssize_t (*show)(mdk_rdev_t *, char *); |
| 2258 | ssize_t (*store)(mdk_rdev_t *, const char *, size_t); |
| 2259 | }; |
| 2260 | |
| 2261 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2262 | state_show(mdk_rdev_t *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2263 | { |
| 2264 | char *sep = ""; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2265 | size_t len = 0; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2266 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2267 | if (test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2268 | len+= sprintf(page+len, "%sfaulty",sep); |
| 2269 | sep = ","; |
| 2270 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2271 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2272 | len += sprintf(page+len, "%sin_sync",sep); |
| 2273 | sep = ","; |
| 2274 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2275 | if (test_bit(WriteMostly, &rdev->flags)) { |
| 2276 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 2277 | sep = ","; |
| 2278 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2279 | if (test_bit(Blocked, &rdev->flags)) { |
| 2280 | len += sprintf(page+len, "%sblocked", sep); |
| 2281 | sep = ","; |
| 2282 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2283 | if (!test_bit(Faulty, &rdev->flags) && |
| 2284 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2285 | len += sprintf(page+len, "%sspare", sep); |
| 2286 | sep = ","; |
| 2287 | } |
| 2288 | return len+sprintf(page+len, "\n"); |
| 2289 | } |
| 2290 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2291 | static ssize_t |
| 2292 | state_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2293 | { |
| 2294 | /* can write |
| 2295 | * faulty - simulates and error |
| 2296 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2297 | * writemostly - sets write_mostly |
| 2298 | * -writemostly - clears write_mostly |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2299 | * blocked - sets the Blocked flag |
| 2300 | * -blocked - clears the Blocked flag |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2301 | * insync - sets Insync providing device isn't active |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2302 | */ |
| 2303 | int err = -EINVAL; |
| 2304 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 2305 | md_error(rdev->mddev, rdev); |
| 2306 | err = 0; |
| 2307 | } else if (cmd_match(buf, "remove")) { |
| 2308 | if (rdev->raid_disk >= 0) |
| 2309 | err = -EBUSY; |
| 2310 | else { |
| 2311 | mddev_t *mddev = rdev->mddev; |
| 2312 | kick_rdev_from_array(rdev); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2313 | if (mddev->pers) |
| 2314 | md_update_sb(mddev, 1); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2315 | md_new_event(mddev); |
| 2316 | err = 0; |
| 2317 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2318 | } else if (cmd_match(buf, "writemostly")) { |
| 2319 | set_bit(WriteMostly, &rdev->flags); |
| 2320 | err = 0; |
| 2321 | } else if (cmd_match(buf, "-writemostly")) { |
| 2322 | clear_bit(WriteMostly, &rdev->flags); |
| 2323 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2324 | } else if (cmd_match(buf, "blocked")) { |
| 2325 | set_bit(Blocked, &rdev->flags); |
| 2326 | err = 0; |
| 2327 | } else if (cmd_match(buf, "-blocked")) { |
| 2328 | clear_bit(Blocked, &rdev->flags); |
| 2329 | wake_up(&rdev->blocked_wait); |
| 2330 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2331 | md_wakeup_thread(rdev->mddev->thread); |
| 2332 | |
| 2333 | err = 0; |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2334 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { |
| 2335 | set_bit(In_sync, &rdev->flags); |
| 2336 | err = 0; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2337 | } |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2338 | if (!err && rdev->sysfs_state) |
| 2339 | sysfs_notify_dirent(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2340 | return err ? err : len; |
| 2341 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2342 | static struct rdev_sysfs_entry rdev_state = |
| 2343 | __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2344 | |
| 2345 | static ssize_t |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2346 | errors_show(mdk_rdev_t *rdev, char *page) |
| 2347 | { |
| 2348 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 2349 | } |
| 2350 | |
| 2351 | static ssize_t |
| 2352 | errors_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2353 | { |
| 2354 | char *e; |
| 2355 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2356 | if (*buf && (*e == 0 || *e == '\n')) { |
| 2357 | atomic_set(&rdev->corrected_errors, n); |
| 2358 | return len; |
| 2359 | } |
| 2360 | return -EINVAL; |
| 2361 | } |
| 2362 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2363 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2364 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2365 | static ssize_t |
| 2366 | slot_show(mdk_rdev_t *rdev, char *page) |
| 2367 | { |
| 2368 | if (rdev->raid_disk < 0) |
| 2369 | return sprintf(page, "none\n"); |
| 2370 | else |
| 2371 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 2372 | } |
| 2373 | |
| 2374 | static ssize_t |
| 2375 | slot_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2376 | { |
| 2377 | char *e; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2378 | int err; |
| 2379 | char nm[20]; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2380 | int slot = simple_strtoul(buf, &e, 10); |
| 2381 | if (strncmp(buf, "none", 4)==0) |
| 2382 | slot = -1; |
| 2383 | else if (e==buf || (*e && *e!= '\n')) |
| 2384 | return -EINVAL; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2385 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2386 | /* Setting 'slot' on an active array requires also |
| 2387 | * updating the 'rd%d' link, and communicating |
| 2388 | * with the personality with ->hot_*_disk. |
| 2389 | * For now we only support removing |
| 2390 | * failed/spare devices. This normally happens automatically, |
| 2391 | * but not when the metadata is externally managed. |
| 2392 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2393 | if (rdev->raid_disk == -1) |
| 2394 | return -EEXIST; |
| 2395 | /* personality does all needed checks */ |
| 2396 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2397 | return -EINVAL; |
| 2398 | err = rdev->mddev->pers-> |
| 2399 | hot_remove_disk(rdev->mddev, rdev->raid_disk); |
| 2400 | if (err) |
| 2401 | return err; |
| 2402 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2403 | sysfs_remove_link(&rdev->mddev->kobj, nm); |
Maciej Trela | b710310 | 2010-04-14 16:58:16 +1000 | [diff] [blame] | 2404 | rdev->raid_disk = -1; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2405 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2406 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2407 | } else if (rdev->mddev->pers) { |
| 2408 | mdk_rdev_t *rdev2; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2409 | /* Activating a spare .. or possibly reactivating |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2410 | * if we ever get bitmaps working here. |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2411 | */ |
| 2412 | |
| 2413 | if (rdev->raid_disk != -1) |
| 2414 | return -EBUSY; |
| 2415 | |
| 2416 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2417 | return -EINVAL; |
| 2418 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2419 | list_for_each_entry(rdev2, &rdev->mddev->disks, same_set) |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2420 | if (rdev2->raid_disk == slot) |
| 2421 | return -EEXIST; |
| 2422 | |
| 2423 | rdev->raid_disk = slot; |
| 2424 | if (test_bit(In_sync, &rdev->flags)) |
| 2425 | rdev->saved_raid_disk = slot; |
| 2426 | else |
| 2427 | rdev->saved_raid_disk = -1; |
| 2428 | err = rdev->mddev->pers-> |
| 2429 | hot_add_disk(rdev->mddev, rdev); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 2430 | if (err) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2431 | rdev->raid_disk = -1; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2432 | return err; |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 2433 | } else |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2434 | sysfs_notify_dirent(rdev->sysfs_state); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2435 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2436 | if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm)) |
| 2437 | printk(KERN_WARNING |
| 2438 | "md: cannot register " |
| 2439 | "%s for %s\n", |
| 2440 | nm, mdname(rdev->mddev)); |
| 2441 | |
| 2442 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2443 | } else { |
| 2444 | if (slot >= rdev->mddev->raid_disks) |
| 2445 | return -ENOSPC; |
| 2446 | rdev->raid_disk = slot; |
| 2447 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2448 | clear_bit(Faulty, &rdev->flags); |
| 2449 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2450 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2451 | sysfs_notify_dirent(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2452 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2453 | return len; |
| 2454 | } |
| 2455 | |
| 2456 | |
| 2457 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2458 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2459 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2460 | static ssize_t |
| 2461 | offset_show(mdk_rdev_t *rdev, char *page) |
| 2462 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2463 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2464 | } |
| 2465 | |
| 2466 | static ssize_t |
| 2467 | offset_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2468 | { |
| 2469 | char *e; |
| 2470 | unsigned long long offset = simple_strtoull(buf, &e, 10); |
| 2471 | if (e==buf || (*e && *e != '\n')) |
| 2472 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2473 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2474 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2475 | if (rdev->sectors && rdev->mddev->external) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2476 | /* Must set offset before size, so overlap checks |
| 2477 | * can be sane */ |
| 2478 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2479 | rdev->data_offset = offset; |
| 2480 | return len; |
| 2481 | } |
| 2482 | |
| 2483 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2484 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2485 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2486 | static ssize_t |
| 2487 | rdev_size_show(mdk_rdev_t *rdev, char *page) |
| 2488 | { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2489 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2490 | } |
| 2491 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2492 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 2493 | { |
| 2494 | /* check if two start/length pairs overlap */ |
| 2495 | if (s1+l1 <= s2) |
| 2496 | return 0; |
| 2497 | if (s2+l2 <= s1) |
| 2498 | return 0; |
| 2499 | return 1; |
| 2500 | } |
| 2501 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2502 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
| 2503 | { |
| 2504 | unsigned long long blocks; |
| 2505 | sector_t new; |
| 2506 | |
| 2507 | if (strict_strtoull(buf, 10, &blocks) < 0) |
| 2508 | return -EINVAL; |
| 2509 | |
| 2510 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) |
| 2511 | return -EINVAL; /* sector conversion overflow */ |
| 2512 | |
| 2513 | new = blocks * 2; |
| 2514 | if (new != blocks * 2) |
| 2515 | return -EINVAL; /* unsigned long long to sector_t overflow */ |
| 2516 | |
| 2517 | *sectors = new; |
| 2518 | return 0; |
| 2519 | } |
| 2520 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2521 | static ssize_t |
| 2522 | rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2523 | { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2524 | mddev_t *my_mddev = rdev->mddev; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2525 | sector_t oldsectors = rdev->sectors; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2526 | sector_t sectors; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2527 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2528 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2529 | return -EINVAL; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2530 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2531 | if (my_mddev->persistent) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2532 | sectors = super_types[my_mddev->major_version]. |
| 2533 | rdev_size_change(rdev, sectors); |
| 2534 | if (!sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2535 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2536 | } else if (!sectors) |
| 2537 | sectors = (rdev->bdev->bd_inode->i_size >> 9) - |
| 2538 | rdev->data_offset; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2539 | } |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2540 | if (sectors < my_mddev->dev_sectors) |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 2541 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2542 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2543 | rdev->sectors = sectors; |
| 2544 | if (sectors > oldsectors && my_mddev->external) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2545 | /* need to check that all other rdevs with the same ->bdev |
| 2546 | * do not overlap. We need to unlock the mddev to avoid |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2547 | * a deadlock. We have already changed rdev->sectors, and if |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2548 | * we have to change it back, we will have the lock again. |
| 2549 | */ |
| 2550 | mddev_t *mddev; |
| 2551 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2552 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2553 | |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2554 | mddev_unlock(my_mddev); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2555 | for_each_mddev(mddev, tmp) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2556 | mdk_rdev_t *rdev2; |
| 2557 | |
| 2558 | mddev_lock(mddev); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2559 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2560 | if (test_bit(AllReserved, &rdev2->flags) || |
| 2561 | (rdev->bdev == rdev2->bdev && |
| 2562 | rdev != rdev2 && |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2563 | overlaps(rdev->data_offset, rdev->sectors, |
Andre Noll | d07bd3b | 2008-07-21 14:42:07 +1000 | [diff] [blame] | 2564 | rdev2->data_offset, |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2565 | rdev2->sectors))) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2566 | overlap = 1; |
| 2567 | break; |
| 2568 | } |
| 2569 | mddev_unlock(mddev); |
| 2570 | if (overlap) { |
| 2571 | mddev_put(mddev); |
| 2572 | break; |
| 2573 | } |
| 2574 | } |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2575 | mddev_lock(my_mddev); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2576 | if (overlap) { |
| 2577 | /* Someone else could have slipped in a size |
| 2578 | * change here, but doing so is just silly. |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2579 | * We put oldsectors back because we *know* it is |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2580 | * safe, and trust userspace not to race with |
| 2581 | * itself |
| 2582 | */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2583 | rdev->sectors = oldsectors; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2584 | return -EBUSY; |
| 2585 | } |
| 2586 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2587 | return len; |
| 2588 | } |
| 2589 | |
| 2590 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2591 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2592 | |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2593 | |
| 2594 | static ssize_t recovery_start_show(mdk_rdev_t *rdev, char *page) |
| 2595 | { |
| 2596 | unsigned long long recovery_start = rdev->recovery_offset; |
| 2597 | |
| 2598 | if (test_bit(In_sync, &rdev->flags) || |
| 2599 | recovery_start == MaxSector) |
| 2600 | return sprintf(page, "none\n"); |
| 2601 | |
| 2602 | return sprintf(page, "%llu\n", recovery_start); |
| 2603 | } |
| 2604 | |
| 2605 | static ssize_t recovery_start_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2606 | { |
| 2607 | unsigned long long recovery_start; |
| 2608 | |
| 2609 | if (cmd_match(buf, "none")) |
| 2610 | recovery_start = MaxSector; |
| 2611 | else if (strict_strtoull(buf, 10, &recovery_start)) |
| 2612 | return -EINVAL; |
| 2613 | |
| 2614 | if (rdev->mddev->pers && |
| 2615 | rdev->raid_disk >= 0) |
| 2616 | return -EBUSY; |
| 2617 | |
| 2618 | rdev->recovery_offset = recovery_start; |
| 2619 | if (recovery_start == MaxSector) |
| 2620 | set_bit(In_sync, &rdev->flags); |
| 2621 | else |
| 2622 | clear_bit(In_sync, &rdev->flags); |
| 2623 | return len; |
| 2624 | } |
| 2625 | |
| 2626 | static struct rdev_sysfs_entry rdev_recovery_start = |
| 2627 | __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store); |
| 2628 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2629 | static struct attribute *rdev_default_attrs[] = { |
| 2630 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2631 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2632 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2633 | &rdev_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2634 | &rdev_size.attr, |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2635 | &rdev_recovery_start.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2636 | NULL, |
| 2637 | }; |
| 2638 | static ssize_t |
| 2639 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 2640 | { |
| 2641 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2642 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2643 | mddev_t *mddev = rdev->mddev; |
| 2644 | ssize_t rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2645 | |
| 2646 | if (!entry->show) |
| 2647 | return -EIO; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2648 | |
| 2649 | rv = mddev ? mddev_lock(mddev) : -EBUSY; |
| 2650 | if (!rv) { |
| 2651 | if (rdev->mddev == NULL) |
| 2652 | rv = -EBUSY; |
| 2653 | else |
| 2654 | rv = entry->show(rdev, page); |
| 2655 | mddev_unlock(mddev); |
| 2656 | } |
| 2657 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2658 | } |
| 2659 | |
| 2660 | static ssize_t |
| 2661 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 2662 | const char *page, size_t length) |
| 2663 | { |
| 2664 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2665 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2666 | ssize_t rv; |
| 2667 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2668 | |
| 2669 | if (!entry->store) |
| 2670 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 2671 | if (!capable(CAP_SYS_ADMIN)) |
| 2672 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2673 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2674 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2675 | if (rdev->mddev == NULL) |
| 2676 | rv = -EBUSY; |
| 2677 | else |
| 2678 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 2679 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2680 | } |
| 2681 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2682 | } |
| 2683 | |
| 2684 | static void rdev_free(struct kobject *ko) |
| 2685 | { |
| 2686 | mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj); |
| 2687 | kfree(rdev); |
| 2688 | } |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 2689 | static const struct sysfs_ops rdev_sysfs_ops = { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2690 | .show = rdev_attr_show, |
| 2691 | .store = rdev_attr_store, |
| 2692 | }; |
| 2693 | static struct kobj_type rdev_ktype = { |
| 2694 | .release = rdev_free, |
| 2695 | .sysfs_ops = &rdev_sysfs_ops, |
| 2696 | .default_attrs = rdev_default_attrs, |
| 2697 | }; |
| 2698 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2699 | /* |
| 2700 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 2701 | * |
| 2702 | * mark the device faulty if: |
| 2703 | * |
| 2704 | * - the device is nonexistent (zero size) |
| 2705 | * - the device has no valid superblock |
| 2706 | * |
| 2707 | * a faulty rdev _never_ has rdev->sb set. |
| 2708 | */ |
| 2709 | static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor) |
| 2710 | { |
| 2711 | char b[BDEVNAME_SIZE]; |
| 2712 | int err; |
| 2713 | mdk_rdev_t *rdev; |
| 2714 | sector_t size; |
| 2715 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 2716 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2717 | if (!rdev) { |
| 2718 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 2719 | return ERR_PTR(-ENOMEM); |
| 2720 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2721 | |
| 2722 | if ((err = alloc_disk_sb(rdev))) |
| 2723 | goto abort_free; |
| 2724 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2725 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2726 | if (err) |
| 2727 | goto abort_free; |
| 2728 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2729 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2730 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2731 | rdev->desc_nr = -1; |
NeilBrown | 2b6e845 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 2732 | rdev->saved_raid_disk = -1; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2733 | rdev->raid_disk = -1; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2734 | rdev->flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2735 | rdev->data_offset = 0; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2736 | rdev->sb_events = 0; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 2737 | rdev->last_read_error.tv_sec = 0; |
| 2738 | rdev->last_read_error.tv_nsec = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2739 | atomic_set(&rdev->nr_pending, 0); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2740 | atomic_set(&rdev->read_errors, 0); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2741 | atomic_set(&rdev->corrected_errors, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2742 | |
| 2743 | size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS; |
| 2744 | if (!size) { |
| 2745 | printk(KERN_WARNING |
| 2746 | "md: %s has zero or unknown size, marking faulty!\n", |
| 2747 | bdevname(rdev->bdev,b)); |
| 2748 | err = -EINVAL; |
| 2749 | goto abort_free; |
| 2750 | } |
| 2751 | |
| 2752 | if (super_format >= 0) { |
| 2753 | err = super_types[super_format]. |
| 2754 | load_super(rdev, NULL, super_minor); |
| 2755 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 2756 | printk(KERN_WARNING |
| 2757 | "md: %s does not have a valid v%d.%d " |
| 2758 | "superblock, not importing!\n", |
| 2759 | bdevname(rdev->bdev,b), |
| 2760 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2761 | goto abort_free; |
| 2762 | } |
| 2763 | if (err < 0) { |
| 2764 | printk(KERN_WARNING |
| 2765 | "md: could not read %s's sb, not importing!\n", |
| 2766 | bdevname(rdev->bdev,b)); |
| 2767 | goto abort_free; |
| 2768 | } |
| 2769 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2770 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2771 | INIT_LIST_HEAD(&rdev->same_set); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2772 | init_waitqueue_head(&rdev->blocked_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2773 | |
| 2774 | return rdev; |
| 2775 | |
| 2776 | abort_free: |
| 2777 | if (rdev->sb_page) { |
| 2778 | if (rdev->bdev) |
| 2779 | unlock_rdev(rdev); |
| 2780 | free_disk_sb(rdev); |
| 2781 | } |
| 2782 | kfree(rdev); |
| 2783 | return ERR_PTR(err); |
| 2784 | } |
| 2785 | |
| 2786 | /* |
| 2787 | * Check a full RAID array for plausibility |
| 2788 | */ |
| 2789 | |
| 2790 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 2791 | static void analyze_sbs(mddev_t * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2792 | { |
| 2793 | int i; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2794 | mdk_rdev_t *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2795 | char b[BDEVNAME_SIZE]; |
| 2796 | |
| 2797 | freshest = NULL; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2798 | rdev_for_each(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2799 | switch (super_types[mddev->major_version]. |
| 2800 | load_super(rdev, freshest, mddev->minor_version)) { |
| 2801 | case 1: |
| 2802 | freshest = rdev; |
| 2803 | break; |
| 2804 | case 0: |
| 2805 | break; |
| 2806 | default: |
| 2807 | printk( KERN_ERR \ |
| 2808 | "md: fatal superblock inconsistency in %s" |
| 2809 | " -- removing from array\n", |
| 2810 | bdevname(rdev->bdev,b)); |
| 2811 | kick_rdev_from_array(rdev); |
| 2812 | } |
| 2813 | |
| 2814 | |
| 2815 | super_types[mddev->major_version]. |
| 2816 | validate_super(mddev, freshest); |
| 2817 | |
| 2818 | i = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2819 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 2820 | if (mddev->max_disks && |
| 2821 | (rdev->desc_nr >= mddev->max_disks || |
| 2822 | i > mddev->max_disks)) { |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 2823 | printk(KERN_WARNING |
| 2824 | "md: %s: %s: only %d devices permitted\n", |
| 2825 | mdname(mddev), bdevname(rdev->bdev, b), |
| 2826 | mddev->max_disks); |
| 2827 | kick_rdev_from_array(rdev); |
| 2828 | continue; |
| 2829 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2830 | if (rdev != freshest) |
| 2831 | if (super_types[mddev->major_version]. |
| 2832 | validate_super(mddev, rdev)) { |
| 2833 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 2834 | " from array!\n", |
| 2835 | bdevname(rdev->bdev,b)); |
| 2836 | kick_rdev_from_array(rdev); |
| 2837 | continue; |
| 2838 | } |
| 2839 | if (mddev->level == LEVEL_MULTIPATH) { |
| 2840 | rdev->desc_nr = i++; |
| 2841 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2842 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 2843 | } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2844 | rdev->raid_disk = -1; |
| 2845 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2846 | } |
| 2847 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2848 | } |
| 2849 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2850 | /* Read a fixed-point number. |
| 2851 | * Numbers in sysfs attributes should be in "standard" units where |
| 2852 | * possible, so time should be in seconds. |
| 2853 | * However we internally use a a much smaller unit such as |
| 2854 | * milliseconds or jiffies. |
| 2855 | * This function takes a decimal number with a possible fractional |
| 2856 | * component, and produces an integer which is the result of |
| 2857 | * multiplying that number by 10^'scale'. |
| 2858 | * all without any floating-point arithmetic. |
| 2859 | */ |
| 2860 | int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale) |
| 2861 | { |
| 2862 | unsigned long result = 0; |
| 2863 | long decimals = -1; |
| 2864 | while (isdigit(*cp) || (*cp == '.' && decimals < 0)) { |
| 2865 | if (*cp == '.') |
| 2866 | decimals = 0; |
| 2867 | else if (decimals < scale) { |
| 2868 | unsigned int value; |
| 2869 | value = *cp - '0'; |
| 2870 | result = result * 10 + value; |
| 2871 | if (decimals >= 0) |
| 2872 | decimals++; |
| 2873 | } |
| 2874 | cp++; |
| 2875 | } |
| 2876 | if (*cp == '\n') |
| 2877 | cp++; |
| 2878 | if (*cp) |
| 2879 | return -EINVAL; |
| 2880 | if (decimals < 0) |
| 2881 | decimals = 0; |
| 2882 | while (decimals < scale) { |
| 2883 | result *= 10; |
| 2884 | decimals ++; |
| 2885 | } |
| 2886 | *res = result; |
| 2887 | return 0; |
| 2888 | } |
| 2889 | |
| 2890 | |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2891 | static void md_safemode_timeout(unsigned long data); |
| 2892 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2893 | static ssize_t |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2894 | safe_delay_show(mddev_t *mddev, char *page) |
| 2895 | { |
| 2896 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 2897 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 2898 | } |
| 2899 | static ssize_t |
| 2900 | safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len) |
| 2901 | { |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2902 | unsigned long msec; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 2903 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2904 | if (strict_strtoul_scaled(cbuf, &msec, 3) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2905 | return -EINVAL; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2906 | if (msec == 0) |
| 2907 | mddev->safemode_delay = 0; |
| 2908 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2909 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2910 | mddev->safemode_delay = (msec*HZ)/1000; |
| 2911 | if (mddev->safemode_delay == 0) |
| 2912 | mddev->safemode_delay = 1; |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2913 | if (mddev->safemode_delay < old_delay) |
| 2914 | md_safemode_timeout((unsigned long)mddev); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2915 | } |
| 2916 | return len; |
| 2917 | } |
| 2918 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2919 | __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] | 2920 | |
| 2921 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2922 | level_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2923 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 2924 | struct mdk_personality *p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2925 | if (p) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2926 | return sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2927 | else if (mddev->clevel[0]) |
| 2928 | return sprintf(page, "%s\n", mddev->clevel); |
| 2929 | else if (mddev->level != LEVEL_NONE) |
| 2930 | return sprintf(page, "%d\n", mddev->level); |
| 2931 | else |
| 2932 | return 0; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2933 | } |
| 2934 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2935 | static ssize_t |
| 2936 | level_store(mddev_t *mddev, const char *buf, size_t len) |
| 2937 | { |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 2938 | char clevel[16]; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2939 | ssize_t rv = len; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2940 | struct mdk_personality *pers; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 2941 | long level; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2942 | void *priv; |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 2943 | mdk_rdev_t *rdev; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2944 | |
| 2945 | if (mddev->pers == NULL) { |
| 2946 | if (len == 0) |
| 2947 | return 0; |
| 2948 | if (len >= sizeof(mddev->clevel)) |
| 2949 | return -ENOSPC; |
| 2950 | strncpy(mddev->clevel, buf, len); |
| 2951 | if (mddev->clevel[len-1] == '\n') |
| 2952 | len--; |
| 2953 | mddev->clevel[len] = 0; |
| 2954 | mddev->level = LEVEL_NONE; |
| 2955 | return rv; |
| 2956 | } |
| 2957 | |
| 2958 | /* request to change the personality. Need to ensure: |
| 2959 | * - array is not engaged in resync/recovery/reshape |
| 2960 | * - old personality can be suspended |
| 2961 | * - new personality will access other array. |
| 2962 | */ |
| 2963 | |
| 2964 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2965 | return -EBUSY; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2966 | |
| 2967 | if (!mddev->pers->quiesce) { |
| 2968 | printk(KERN_WARNING "md: %s: %s does not support online personality change\n", |
| 2969 | mdname(mddev), mddev->pers->name); |
| 2970 | return -EINVAL; |
| 2971 | } |
| 2972 | |
| 2973 | /* Now find the new personality */ |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 2974 | if (len == 0 || len >= sizeof(clevel)) |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2975 | return -EINVAL; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 2976 | strncpy(clevel, buf, len); |
| 2977 | if (clevel[len-1] == '\n') |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2978 | len--; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 2979 | clevel[len] = 0; |
| 2980 | if (strict_strtol(clevel, 10, &level)) |
| 2981 | level = LEVEL_NONE; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2982 | |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 2983 | if (request_module("md-%s", clevel) != 0) |
| 2984 | request_module("md-level-%s", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2985 | spin_lock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 2986 | pers = find_pers(level, clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2987 | if (!pers || !try_module_get(pers->owner)) { |
| 2988 | spin_unlock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 2989 | printk(KERN_WARNING "md: personality %s not loaded\n", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2990 | return -EINVAL; |
| 2991 | } |
| 2992 | spin_unlock(&pers_lock); |
| 2993 | |
| 2994 | if (pers == mddev->pers) { |
| 2995 | /* Nothing to do! */ |
| 2996 | module_put(pers->owner); |
| 2997 | return rv; |
| 2998 | } |
| 2999 | if (!pers->takeover) { |
| 3000 | module_put(pers->owner); |
| 3001 | printk(KERN_WARNING "md: %s: %s does not support personality takeover\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3002 | mdname(mddev), clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3003 | return -EINVAL; |
| 3004 | } |
| 3005 | |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3006 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 3007 | rdev->new_raid_disk = rdev->raid_disk; |
| 3008 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3009 | /* ->takeover must set new_* and/or delta_disks |
| 3010 | * if it succeeds, and may set them when it fails. |
| 3011 | */ |
| 3012 | priv = pers->takeover(mddev); |
| 3013 | if (IS_ERR(priv)) { |
| 3014 | mddev->new_level = mddev->level; |
| 3015 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3016 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3017 | mddev->raid_disks -= mddev->delta_disks; |
| 3018 | mddev->delta_disks = 0; |
| 3019 | module_put(pers->owner); |
| 3020 | printk(KERN_WARNING "md: %s: %s would not accept array\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3021 | mdname(mddev), clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3022 | return PTR_ERR(priv); |
| 3023 | } |
| 3024 | |
| 3025 | /* Looks like we have a winner */ |
| 3026 | mddev_suspend(mddev); |
| 3027 | mddev->pers->stop(mddev); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 3028 | |
| 3029 | if (mddev->pers->sync_request == NULL && |
| 3030 | pers->sync_request != NULL) { |
| 3031 | /* need to add the md_redundancy_group */ |
| 3032 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 3033 | printk(KERN_WARNING |
| 3034 | "md: cannot register extra attributes for %s\n", |
| 3035 | mdname(mddev)); |
NeilBrown | 19fdb9e | 2010-05-22 08:31:36 +1000 | [diff] [blame] | 3036 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action"); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 3037 | } |
| 3038 | if (mddev->pers->sync_request != NULL && |
| 3039 | pers->sync_request == NULL) { |
| 3040 | /* need to remove the md_redundancy_group */ |
| 3041 | if (mddev->to_remove == NULL) |
| 3042 | mddev->to_remove = &md_redundancy_group; |
| 3043 | } |
| 3044 | |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 3045 | if (mddev->pers->sync_request == NULL && |
| 3046 | mddev->external) { |
| 3047 | /* We are converting from a no-redundancy array |
| 3048 | * to a redundancy array and metadata is managed |
| 3049 | * externally so we need to be sure that writes |
| 3050 | * won't block due to a need to transition |
| 3051 | * clean->dirty |
| 3052 | * until external management is started. |
| 3053 | */ |
| 3054 | mddev->in_sync = 0; |
| 3055 | mddev->safemode_delay = 0; |
| 3056 | mddev->safemode = 0; |
| 3057 | } |
| 3058 | |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3059 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 3060 | char nm[20]; |
| 3061 | if (rdev->raid_disk < 0) |
| 3062 | continue; |
| 3063 | if (rdev->new_raid_disk > mddev->raid_disks) |
| 3064 | rdev->new_raid_disk = -1; |
| 3065 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3066 | continue; |
| 3067 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 3068 | sysfs_remove_link(&mddev->kobj, nm); |
| 3069 | } |
| 3070 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 3071 | if (rdev->raid_disk < 0) |
| 3072 | continue; |
| 3073 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3074 | continue; |
| 3075 | rdev->raid_disk = rdev->new_raid_disk; |
| 3076 | if (rdev->raid_disk < 0) |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3077 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3078 | else { |
| 3079 | char nm[20]; |
| 3080 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 3081 | if(sysfs_create_link(&mddev->kobj, &rdev->kobj, nm)) |
| 3082 | printk("md: cannot register %s for %s after level change\n", |
| 3083 | nm, mdname(mddev)); |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3084 | } |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3085 | } |
| 3086 | |
| 3087 | module_put(mddev->pers->owner); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3088 | mddev->pers = pers; |
| 3089 | mddev->private = priv; |
| 3090 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
| 3091 | mddev->level = mddev->new_level; |
| 3092 | mddev->layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3093 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3094 | mddev->delta_disks = 0; |
Trela, Maciej | 9af204c | 2010-03-08 16:02:44 +1100 | [diff] [blame] | 3095 | if (mddev->pers->sync_request == NULL) { |
| 3096 | /* this is now an array without redundancy, so |
| 3097 | * it must always be in_sync |
| 3098 | */ |
| 3099 | mddev->in_sync = 1; |
| 3100 | del_timer_sync(&mddev->safemode_timer); |
| 3101 | } |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3102 | pers->run(mddev); |
| 3103 | mddev_resume(mddev); |
| 3104 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 3105 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3106 | md_wakeup_thread(mddev->thread); |
Maciej Trela | 5cac7861 | 2010-04-14 17:17:39 +1000 | [diff] [blame] | 3107 | sysfs_notify(&mddev->kobj, NULL, "level"); |
Dan Williams | bb7f8d2 | 2010-05-01 18:14:57 -0700 | [diff] [blame] | 3108 | md_new_event(mddev); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3109 | return rv; |
| 3110 | } |
| 3111 | |
| 3112 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3113 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3114 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3115 | |
| 3116 | static ssize_t |
| 3117 | layout_show(mddev_t *mddev, char *page) |
| 3118 | { |
| 3119 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3120 | if (mddev->reshape_position != MaxSector && |
| 3121 | mddev->layout != mddev->new_layout) |
| 3122 | return sprintf(page, "%d (%d)\n", |
| 3123 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3124 | return sprintf(page, "%d\n", mddev->layout); |
| 3125 | } |
| 3126 | |
| 3127 | static ssize_t |
| 3128 | layout_store(mddev_t *mddev, const char *buf, size_t len) |
| 3129 | { |
| 3130 | char *e; |
| 3131 | unsigned long n = simple_strtoul(buf, &e, 10); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3132 | |
| 3133 | if (!*buf || (*e && *e != '\n')) |
| 3134 | return -EINVAL; |
| 3135 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3136 | if (mddev->pers) { |
| 3137 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3138 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3139 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3140 | mddev->new_layout = n; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3141 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3142 | if (err) { |
| 3143 | mddev->new_layout = mddev->layout; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3144 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3145 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3146 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3147 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3148 | if (mddev->reshape_position == MaxSector) |
| 3149 | mddev->layout = n; |
| 3150 | } |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3151 | return len; |
| 3152 | } |
| 3153 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3154 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3155 | |
| 3156 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3157 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3158 | raid_disks_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3159 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 3160 | if (mddev->raid_disks == 0) |
| 3161 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3162 | if (mddev->reshape_position != MaxSector && |
| 3163 | mddev->delta_disks != 0) |
| 3164 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 3165 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3166 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 3167 | } |
| 3168 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3169 | static int update_raid_disks(mddev_t *mddev, int raid_disks); |
| 3170 | |
| 3171 | static ssize_t |
| 3172 | raid_disks_store(mddev_t *mddev, const char *buf, size_t len) |
| 3173 | { |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3174 | char *e; |
| 3175 | int rv = 0; |
| 3176 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3177 | |
| 3178 | if (!*buf || (*e && *e != '\n')) |
| 3179 | return -EINVAL; |
| 3180 | |
| 3181 | if (mddev->pers) |
| 3182 | rv = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3183 | else if (mddev->reshape_position != MaxSector) { |
| 3184 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
| 3185 | mddev->delta_disks = n - olddisks; |
| 3186 | mddev->raid_disks = n; |
| 3187 | } else |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3188 | mddev->raid_disks = n; |
| 3189 | return rv ? rv : len; |
| 3190 | } |
| 3191 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3192 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3193 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3194 | static ssize_t |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3195 | chunk_size_show(mddev_t *mddev, char *page) |
| 3196 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3197 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3198 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 3199 | return sprintf(page, "%d (%d)\n", |
| 3200 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3201 | mddev->chunk_sectors << 9); |
| 3202 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3203 | } |
| 3204 | |
| 3205 | static ssize_t |
| 3206 | chunk_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3207 | { |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3208 | char *e; |
| 3209 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3210 | |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 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_chunk_sectors = n >> 9; |
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_chunk_sectors = mddev->chunk_sectors; |
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 { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3225 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3226 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3227 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3228 | } |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3229 | return len; |
| 3230 | } |
| 3231 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3232 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3233 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3234 | static ssize_t |
| 3235 | resync_start_show(mddev_t *mddev, char *page) |
| 3236 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3237 | if (mddev->recovery_cp == MaxSector) |
| 3238 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3239 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 3240 | } |
| 3241 | |
| 3242 | static ssize_t |
| 3243 | resync_start_store(mddev_t *mddev, const char *buf, size_t len) |
| 3244 | { |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3245 | char *e; |
| 3246 | unsigned long long n = simple_strtoull(buf, &e, 10); |
| 3247 | |
| 3248 | if (mddev->pers) |
| 3249 | return -EBUSY; |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3250 | if (cmd_match(buf, "none")) |
| 3251 | n = MaxSector; |
| 3252 | else if (!*buf || (*e && *e != '\n')) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3253 | return -EINVAL; |
| 3254 | |
| 3255 | mddev->recovery_cp = n; |
| 3256 | return len; |
| 3257 | } |
| 3258 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3259 | __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3260 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3261 | /* |
| 3262 | * The array state can be: |
| 3263 | * |
| 3264 | * clear |
| 3265 | * No devices, no size, no level |
| 3266 | * Equivalent to STOP_ARRAY ioctl |
| 3267 | * inactive |
| 3268 | * May have some settings, but array is not active |
| 3269 | * all IO results in error |
| 3270 | * When written, doesn't tear down array, but just stops it |
| 3271 | * suspended (not supported yet) |
| 3272 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 3273 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3274 | * readonly |
| 3275 | * no resync can happen. no superblocks get written. |
| 3276 | * write requests fail |
| 3277 | * read-auto |
| 3278 | * like readonly, but behaves like 'clean' on a write request. |
| 3279 | * |
| 3280 | * clean - no pending writes, but otherwise active. |
| 3281 | * When written to inactive array, starts without resync |
| 3282 | * If a write request arrives then |
| 3283 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 3284 | * if not known, block and switch to write-pending |
| 3285 | * If written to an active array that has pending writes, then fails. |
| 3286 | * active |
| 3287 | * fully active: IO and resync can be happening. |
| 3288 | * When written to inactive array, starts with resync |
| 3289 | * |
| 3290 | * write-pending |
| 3291 | * clean, but writes are blocked waiting for 'active' to be written. |
| 3292 | * |
| 3293 | * active-idle |
| 3294 | * like active, but no writes have been seen for a while (100msec). |
| 3295 | * |
| 3296 | */ |
| 3297 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 3298 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3299 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3300 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 3301 | "write-pending", "active-idle", NULL }; |
| 3302 | |
| 3303 | static int match_word(const char *word, char **list) |
| 3304 | { |
| 3305 | int n; |
| 3306 | for (n=0; list[n]; n++) |
| 3307 | if (cmd_match(word, list[n])) |
| 3308 | break; |
| 3309 | return n; |
| 3310 | } |
| 3311 | |
| 3312 | static ssize_t |
| 3313 | array_state_show(mddev_t *mddev, char *page) |
| 3314 | { |
| 3315 | enum array_state st = inactive; |
| 3316 | |
| 3317 | if (mddev->pers) |
| 3318 | switch(mddev->ro) { |
| 3319 | case 1: |
| 3320 | st = readonly; |
| 3321 | break; |
| 3322 | case 2: |
| 3323 | st = read_auto; |
| 3324 | break; |
| 3325 | case 0: |
| 3326 | if (mddev->in_sync) |
| 3327 | st = clean; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3328 | else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 3329 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3330 | else if (mddev->safemode) |
| 3331 | st = active_idle; |
| 3332 | else |
| 3333 | st = active; |
| 3334 | } |
| 3335 | else { |
| 3336 | if (list_empty(&mddev->disks) && |
| 3337 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3338 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3339 | st = clear; |
| 3340 | else |
| 3341 | st = inactive; |
| 3342 | } |
| 3343 | return sprintf(page, "%s\n", array_states[st]); |
| 3344 | } |
| 3345 | |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3346 | static int do_md_stop(mddev_t * mddev, int ro, int is_open); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3347 | static int md_set_readonly(mddev_t * mddev, int is_open); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3348 | static int do_md_run(mddev_t * mddev); |
| 3349 | static int restart_array(mddev_t *mddev); |
| 3350 | |
| 3351 | static ssize_t |
| 3352 | array_state_store(mddev_t *mddev, const char *buf, size_t len) |
| 3353 | { |
| 3354 | int err = -EINVAL; |
| 3355 | enum array_state st = match_word(buf, array_states); |
| 3356 | switch(st) { |
| 3357 | case bad_word: |
| 3358 | break; |
| 3359 | case clear: |
| 3360 | /* stopping an active array */ |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3361 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3362 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3363 | err = do_md_stop(mddev, 0, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3364 | break; |
| 3365 | case inactive: |
| 3366 | /* stopping an active array */ |
| 3367 | if (mddev->pers) { |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3368 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3369 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3370 | err = do_md_stop(mddev, 2, 0); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3371 | } else |
| 3372 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3373 | break; |
| 3374 | case suspended: |
| 3375 | break; /* not supported yet */ |
| 3376 | case readonly: |
| 3377 | if (mddev->pers) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3378 | err = md_set_readonly(mddev, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3379 | else { |
| 3380 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3381 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3382 | err = do_md_run(mddev); |
| 3383 | } |
| 3384 | break; |
| 3385 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3386 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3387 | if (mddev->ro == 0) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3388 | err = md_set_readonly(mddev, 0); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3389 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3390 | err = restart_array(mddev); |
| 3391 | if (err == 0) { |
| 3392 | mddev->ro = 2; |
| 3393 | set_disk_ro(mddev->gendisk, 0); |
| 3394 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3395 | } else { |
| 3396 | mddev->ro = 2; |
| 3397 | err = do_md_run(mddev); |
| 3398 | } |
| 3399 | break; |
| 3400 | case clean: |
| 3401 | if (mddev->pers) { |
| 3402 | restart_array(mddev); |
| 3403 | spin_lock_irq(&mddev->write_lock); |
| 3404 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3405 | if (mddev->in_sync == 0) { |
| 3406 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3407 | if (mddev->safemode == 1) |
| 3408 | mddev->safemode = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3409 | if (mddev->persistent) |
| 3410 | set_bit(MD_CHANGE_CLEAN, |
| 3411 | &mddev->flags); |
| 3412 | } |
| 3413 | err = 0; |
| 3414 | } else |
| 3415 | err = -EBUSY; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3416 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 3417 | } else |
| 3418 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3419 | break; |
| 3420 | case active: |
| 3421 | if (mddev->pers) { |
| 3422 | restart_array(mddev); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3423 | if (mddev->external) |
| 3424 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3425 | wake_up(&mddev->sb_wait); |
| 3426 | err = 0; |
| 3427 | } else { |
| 3428 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3429 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3430 | err = do_md_run(mddev); |
| 3431 | } |
| 3432 | break; |
| 3433 | case write_pending: |
| 3434 | case active_idle: |
| 3435 | /* these cannot be set */ |
| 3436 | break; |
| 3437 | } |
| 3438 | if (err) |
| 3439 | return err; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3440 | else { |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 3441 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3442 | return len; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3443 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3444 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3445 | static struct md_sysfs_entry md_array_state = |
| 3446 | __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3447 | |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3448 | static ssize_t |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 3449 | max_corrected_read_errors_show(mddev_t *mddev, char *page) { |
| 3450 | return sprintf(page, "%d\n", |
| 3451 | atomic_read(&mddev->max_corr_read_errors)); |
| 3452 | } |
| 3453 | |
| 3454 | static ssize_t |
| 3455 | max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len) |
| 3456 | { |
| 3457 | char *e; |
| 3458 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3459 | |
| 3460 | if (*buf && (*e == 0 || *e == '\n')) { |
| 3461 | atomic_set(&mddev->max_corr_read_errors, n); |
| 3462 | return len; |
| 3463 | } |
| 3464 | return -EINVAL; |
| 3465 | } |
| 3466 | |
| 3467 | static struct md_sysfs_entry max_corr_read_errors = |
| 3468 | __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show, |
| 3469 | max_corrected_read_errors_store); |
| 3470 | |
| 3471 | static ssize_t |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3472 | null_show(mddev_t *mddev, char *page) |
| 3473 | { |
| 3474 | return -EINVAL; |
| 3475 | } |
| 3476 | |
| 3477 | static ssize_t |
| 3478 | new_dev_store(mddev_t *mddev, const char *buf, size_t len) |
| 3479 | { |
| 3480 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 3481 | /* The new device is added to the array. |
| 3482 | * If the array has a persistent superblock, we read the |
| 3483 | * superblock to initialise info and check validity. |
| 3484 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 3485 | * which mainly checks size. |
| 3486 | */ |
| 3487 | char *e; |
| 3488 | int major = simple_strtoul(buf, &e, 10); |
| 3489 | int minor; |
| 3490 | dev_t dev; |
| 3491 | mdk_rdev_t *rdev; |
| 3492 | int err; |
| 3493 | |
| 3494 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 3495 | return -EINVAL; |
| 3496 | minor = simple_strtoul(e+1, &e, 10); |
| 3497 | if (*e && *e != '\n') |
| 3498 | return -EINVAL; |
| 3499 | dev = MKDEV(major, minor); |
| 3500 | if (major != MAJOR(dev) || |
| 3501 | minor != MINOR(dev)) |
| 3502 | return -EOVERFLOW; |
| 3503 | |
| 3504 | |
| 3505 | if (mddev->persistent) { |
| 3506 | rdev = md_import_device(dev, mddev->major_version, |
| 3507 | mddev->minor_version); |
| 3508 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
| 3509 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 3510 | mdk_rdev_t, same_set); |
| 3511 | err = super_types[mddev->major_version] |
| 3512 | .load_super(rdev, rdev0, mddev->minor_version); |
| 3513 | if (err < 0) |
| 3514 | goto out; |
| 3515 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3516 | } else if (mddev->external) |
| 3517 | rdev = md_import_device(dev, -2, -1); |
| 3518 | else |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3519 | rdev = md_import_device(dev, -1, -1); |
| 3520 | |
| 3521 | if (IS_ERR(rdev)) |
| 3522 | return PTR_ERR(rdev); |
| 3523 | err = bind_rdev_to_array(rdev, mddev); |
| 3524 | out: |
| 3525 | if (err) |
| 3526 | export_rdev(rdev); |
| 3527 | return err ? err : len; |
| 3528 | } |
| 3529 | |
| 3530 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3531 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3532 | |
| 3533 | static ssize_t |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3534 | bitmap_store(mddev_t *mddev, const char *buf, size_t len) |
| 3535 | { |
| 3536 | char *end; |
| 3537 | unsigned long chunk, end_chunk; |
| 3538 | |
| 3539 | if (!mddev->bitmap) |
| 3540 | goto out; |
| 3541 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 3542 | while (*buf) { |
| 3543 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 3544 | if (buf == end) break; |
| 3545 | if (*end == '-') { /* range */ |
| 3546 | buf = end + 1; |
| 3547 | end_chunk = simple_strtoul(buf, &end, 0); |
| 3548 | if (buf == end) break; |
| 3549 | } |
| 3550 | if (*end && !isspace(*end)) break; |
| 3551 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 3552 | buf = skip_spaces(end); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3553 | } |
| 3554 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 3555 | out: |
| 3556 | return len; |
| 3557 | } |
| 3558 | |
| 3559 | static struct md_sysfs_entry md_bitmap = |
| 3560 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 3561 | |
| 3562 | static ssize_t |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3563 | size_show(mddev_t *mddev, char *page) |
| 3564 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3565 | return sprintf(page, "%llu\n", |
| 3566 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3567 | } |
| 3568 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3569 | static int update_size(mddev_t *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3570 | |
| 3571 | static ssize_t |
| 3572 | size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3573 | { |
| 3574 | /* If array is inactive, we can reduce the component size, but |
| 3575 | * not increase it (except from 0). |
| 3576 | * If array is active, we can try an on-line resize |
| 3577 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3578 | sector_t sectors; |
| 3579 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3580 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3581 | if (err < 0) |
| 3582 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3583 | if (mddev->pers) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3584 | err = update_size(mddev, sectors); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3585 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3586 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3587 | if (mddev->dev_sectors == 0 || |
| 3588 | mddev->dev_sectors > sectors) |
| 3589 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3590 | else |
| 3591 | err = -ENOSPC; |
| 3592 | } |
| 3593 | return err ? err : len; |
| 3594 | } |
| 3595 | |
| 3596 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3597 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3598 | |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3599 | |
| 3600 | /* Metdata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3601 | * This is one of |
| 3602 | * 'none' for arrays with no metadata (good luck...) |
| 3603 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3604 | * or N.M for internally known formats |
| 3605 | */ |
| 3606 | static ssize_t |
| 3607 | metadata_show(mddev_t *mddev, char *page) |
| 3608 | { |
| 3609 | if (mddev->persistent) |
| 3610 | return sprintf(page, "%d.%d\n", |
| 3611 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3612 | else if (mddev->external) |
| 3613 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3614 | else |
| 3615 | return sprintf(page, "none\n"); |
| 3616 | } |
| 3617 | |
| 3618 | static ssize_t |
| 3619 | metadata_store(mddev_t *mddev, const char *buf, size_t len) |
| 3620 | { |
| 3621 | int major, minor; |
| 3622 | char *e; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 3623 | /* Changing the details of 'external' metadata is |
| 3624 | * always permitted. Otherwise there must be |
| 3625 | * no devices attached to the array. |
| 3626 | */ |
| 3627 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 3628 | ; |
| 3629 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3630 | return -EBUSY; |
| 3631 | |
| 3632 | if (cmd_match(buf, "none")) { |
| 3633 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3634 | mddev->external = 0; |
| 3635 | mddev->major_version = 0; |
| 3636 | mddev->minor_version = 90; |
| 3637 | return len; |
| 3638 | } |
| 3639 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 3640 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3641 | if (namelen >= sizeof(mddev->metadata_type)) |
| 3642 | namelen = sizeof(mddev->metadata_type)-1; |
| 3643 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 3644 | mddev->metadata_type[namelen] = 0; |
| 3645 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 3646 | mddev->metadata_type[--namelen] = 0; |
| 3647 | mddev->persistent = 0; |
| 3648 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3649 | mddev->major_version = 0; |
| 3650 | mddev->minor_version = 90; |
| 3651 | return len; |
| 3652 | } |
| 3653 | major = simple_strtoul(buf, &e, 10); |
| 3654 | if (e==buf || *e != '.') |
| 3655 | return -EINVAL; |
| 3656 | buf = e+1; |
| 3657 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 3658 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3659 | return -EINVAL; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 3660 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3661 | return -ENOENT; |
| 3662 | mddev->major_version = major; |
| 3663 | mddev->minor_version = minor; |
| 3664 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3665 | mddev->external = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3666 | return len; |
| 3667 | } |
| 3668 | |
| 3669 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3670 | __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3671 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3672 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3673 | action_show(mddev_t *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3674 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3675 | char *type = "idle"; |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3676 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 3677 | type = "frozen"; |
| 3678 | else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 2b12ab6 | 2007-10-16 23:30:53 -0700 | [diff] [blame] | 3679 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3680 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 3681 | type = "reshape"; |
| 3682 | else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3683 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 3684 | type = "resync"; |
| 3685 | else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 3686 | type = "check"; |
| 3687 | else |
| 3688 | type = "repair"; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3689 | } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3690 | type = "recover"; |
| 3691 | } |
| 3692 | return sprintf(page, "%s\n", type); |
| 3693 | } |
| 3694 | |
| 3695 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3696 | action_store(mddev_t *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3697 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3698 | if (!mddev->pers || !mddev->pers->sync_request) |
| 3699 | return -EINVAL; |
| 3700 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3701 | if (cmd_match(page, "frozen")) |
| 3702 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3703 | else |
| 3704 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3705 | |
| 3706 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3707 | if (mddev->sync_thread) { |
| 3708 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 3709 | md_unregister_thread(mddev->sync_thread); |
| 3710 | mddev->sync_thread = NULL; |
| 3711 | mddev->recovery = 0; |
| 3712 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3713 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 3714 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3715 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3716 | else if (cmd_match(page, "resync")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3717 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3718 | else if (cmd_match(page, "recover")) { |
| 3719 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 3720 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3721 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3722 | int err; |
| 3723 | if (mddev->pers->start_reshape == NULL) |
| 3724 | return -EINVAL; |
| 3725 | err = mddev->pers->start_reshape(mddev); |
| 3726 | if (err) |
| 3727 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 3728 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3729 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 3730 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3731 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 3732 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3733 | return -EINVAL; |
| 3734 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 3735 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3736 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3737 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3738 | md_wakeup_thread(mddev->thread); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 3739 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3740 | return len; |
| 3741 | } |
| 3742 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3743 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3744 | mismatch_cnt_show(mddev_t *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3745 | { |
| 3746 | return sprintf(page, "%llu\n", |
| 3747 | (unsigned long long) mddev->resync_mismatches); |
| 3748 | } |
| 3749 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3750 | static struct md_sysfs_entry md_scan_mode = |
| 3751 | __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3752 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3753 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3754 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3755 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3756 | static ssize_t |
| 3757 | sync_min_show(mddev_t *mddev, char *page) |
| 3758 | { |
| 3759 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 3760 | mddev->sync_speed_min ? "local": "system"); |
| 3761 | } |
| 3762 | |
| 3763 | static ssize_t |
| 3764 | sync_min_store(mddev_t *mddev, const char *buf, size_t len) |
| 3765 | { |
| 3766 | int min; |
| 3767 | char *e; |
| 3768 | if (strncmp(buf, "system", 6)==0) { |
| 3769 | mddev->sync_speed_min = 0; |
| 3770 | return len; |
| 3771 | } |
| 3772 | min = simple_strtoul(buf, &e, 10); |
| 3773 | if (buf == e || (*e && *e != '\n') || min <= 0) |
| 3774 | return -EINVAL; |
| 3775 | mddev->sync_speed_min = min; |
| 3776 | return len; |
| 3777 | } |
| 3778 | |
| 3779 | static struct md_sysfs_entry md_sync_min = |
| 3780 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 3781 | |
| 3782 | static ssize_t |
| 3783 | sync_max_show(mddev_t *mddev, char *page) |
| 3784 | { |
| 3785 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 3786 | mddev->sync_speed_max ? "local": "system"); |
| 3787 | } |
| 3788 | |
| 3789 | static ssize_t |
| 3790 | sync_max_store(mddev_t *mddev, const char *buf, size_t len) |
| 3791 | { |
| 3792 | int max; |
| 3793 | char *e; |
| 3794 | if (strncmp(buf, "system", 6)==0) { |
| 3795 | mddev->sync_speed_max = 0; |
| 3796 | return len; |
| 3797 | } |
| 3798 | max = simple_strtoul(buf, &e, 10); |
| 3799 | if (buf == e || (*e && *e != '\n') || max <= 0) |
| 3800 | return -EINVAL; |
| 3801 | mddev->sync_speed_max = max; |
| 3802 | return len; |
| 3803 | } |
| 3804 | |
| 3805 | static struct md_sysfs_entry md_sync_max = |
| 3806 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 3807 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 3808 | static ssize_t |
| 3809 | degraded_show(mddev_t *mddev, char *page) |
| 3810 | { |
| 3811 | return sprintf(page, "%d\n", mddev->degraded); |
| 3812 | } |
| 3813 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3814 | |
| 3815 | static ssize_t |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 3816 | sync_force_parallel_show(mddev_t *mddev, char *page) |
| 3817 | { |
| 3818 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 3819 | } |
| 3820 | |
| 3821 | static ssize_t |
| 3822 | sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len) |
| 3823 | { |
| 3824 | long n; |
| 3825 | |
| 3826 | if (strict_strtol(buf, 10, &n)) |
| 3827 | return -EINVAL; |
| 3828 | |
| 3829 | if (n != 0 && n != 1) |
| 3830 | return -EINVAL; |
| 3831 | |
| 3832 | mddev->parallel_resync = n; |
| 3833 | |
| 3834 | if (mddev->sync_thread) |
| 3835 | wake_up(&resync_wait); |
| 3836 | |
| 3837 | return len; |
| 3838 | } |
| 3839 | |
| 3840 | /* force parallel resync, even with shared block devices */ |
| 3841 | static struct md_sysfs_entry md_sync_force_parallel = |
| 3842 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 3843 | sync_force_parallel_show, sync_force_parallel_store); |
| 3844 | |
| 3845 | static ssize_t |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3846 | sync_speed_show(mddev_t *mddev, char *page) |
| 3847 | { |
| 3848 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3849 | if (mddev->curr_resync == 0) |
| 3850 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3851 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 3852 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3853 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3854 | db = resync - mddev->resync_mark_cnt; |
| 3855 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3856 | } |
| 3857 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3858 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3859 | |
| 3860 | static ssize_t |
| 3861 | sync_completed_show(mddev_t *mddev, char *page) |
| 3862 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3863 | unsigned long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3864 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 3865 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3866 | return sprintf(page, "none\n"); |
| 3867 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3868 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3869 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3870 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3871 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3872 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 3873 | resync = mddev->curr_resync_completed; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3874 | return sprintf(page, "%lu / %lu\n", resync, max_sectors); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3875 | } |
| 3876 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3877 | static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3878 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3879 | static ssize_t |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3880 | min_sync_show(mddev_t *mddev, char *page) |
| 3881 | { |
| 3882 | return sprintf(page, "%llu\n", |
| 3883 | (unsigned long long)mddev->resync_min); |
| 3884 | } |
| 3885 | static ssize_t |
| 3886 | min_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3887 | { |
| 3888 | unsigned long long min; |
| 3889 | if (strict_strtoull(buf, 10, &min)) |
| 3890 | return -EINVAL; |
| 3891 | if (min > mddev->resync_max) |
| 3892 | return -EINVAL; |
| 3893 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3894 | return -EBUSY; |
| 3895 | |
| 3896 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3897 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 3898 | sector_t temp = min; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3899 | if (sector_div(temp, mddev->chunk_sectors)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3900 | return -EINVAL; |
| 3901 | } |
| 3902 | mddev->resync_min = min; |
| 3903 | |
| 3904 | return len; |
| 3905 | } |
| 3906 | |
| 3907 | static struct md_sysfs_entry md_min_sync = |
| 3908 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 3909 | |
| 3910 | static ssize_t |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3911 | max_sync_show(mddev_t *mddev, char *page) |
| 3912 | { |
| 3913 | if (mddev->resync_max == MaxSector) |
| 3914 | return sprintf(page, "max\n"); |
| 3915 | else |
| 3916 | return sprintf(page, "%llu\n", |
| 3917 | (unsigned long long)mddev->resync_max); |
| 3918 | } |
| 3919 | static ssize_t |
| 3920 | max_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3921 | { |
| 3922 | if (strncmp(buf, "max", 3) == 0) |
| 3923 | mddev->resync_max = MaxSector; |
| 3924 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3925 | unsigned long long max; |
| 3926 | if (strict_strtoull(buf, 10, &max)) |
| 3927 | return -EINVAL; |
| 3928 | if (max < mddev->resync_min) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3929 | return -EINVAL; |
| 3930 | if (max < mddev->resync_max && |
NeilBrown | 4d484a4 | 2009-08-13 10:41:50 +1000 | [diff] [blame] | 3931 | mddev->ro == 0 && |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3932 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3933 | return -EBUSY; |
| 3934 | |
| 3935 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3936 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 3937 | sector_t temp = max; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3938 | if (sector_div(temp, mddev->chunk_sectors)) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3939 | return -EINVAL; |
| 3940 | } |
| 3941 | mddev->resync_max = max; |
| 3942 | } |
| 3943 | wake_up(&mddev->recovery_wait); |
| 3944 | return len; |
| 3945 | } |
| 3946 | |
| 3947 | static struct md_sysfs_entry md_max_sync = |
| 3948 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 3949 | |
| 3950 | static ssize_t |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3951 | suspend_lo_show(mddev_t *mddev, char *page) |
| 3952 | { |
| 3953 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 3954 | } |
| 3955 | |
| 3956 | static ssize_t |
| 3957 | suspend_lo_store(mddev_t *mddev, const char *buf, size_t len) |
| 3958 | { |
| 3959 | char *e; |
| 3960 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3961 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 3962 | if (mddev->pers == NULL || |
| 3963 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3964 | return -EINVAL; |
| 3965 | if (buf == e || (*e && *e != '\n')) |
| 3966 | return -EINVAL; |
| 3967 | if (new >= mddev->suspend_hi || |
| 3968 | (new > mddev->suspend_lo && new < mddev->suspend_hi)) { |
| 3969 | mddev->suspend_lo = new; |
| 3970 | mddev->pers->quiesce(mddev, 2); |
| 3971 | return len; |
| 3972 | } else |
| 3973 | return -EINVAL; |
| 3974 | } |
| 3975 | static struct md_sysfs_entry md_suspend_lo = |
| 3976 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 3977 | |
| 3978 | |
| 3979 | static ssize_t |
| 3980 | suspend_hi_show(mddev_t *mddev, char *page) |
| 3981 | { |
| 3982 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 3983 | } |
| 3984 | |
| 3985 | static ssize_t |
| 3986 | suspend_hi_store(mddev_t *mddev, const char *buf, size_t len) |
| 3987 | { |
| 3988 | char *e; |
| 3989 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3990 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 3991 | if (mddev->pers == NULL || |
| 3992 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3993 | return -EINVAL; |
| 3994 | if (buf == e || (*e && *e != '\n')) |
| 3995 | return -EINVAL; |
| 3996 | if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) || |
| 3997 | (new > mddev->suspend_lo && new > mddev->suspend_hi)) { |
| 3998 | mddev->suspend_hi = new; |
| 3999 | mddev->pers->quiesce(mddev, 1); |
| 4000 | mddev->pers->quiesce(mddev, 0); |
| 4001 | return len; |
| 4002 | } else |
| 4003 | return -EINVAL; |
| 4004 | } |
| 4005 | static struct md_sysfs_entry md_suspend_hi = |
| 4006 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 4007 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4008 | static ssize_t |
| 4009 | reshape_position_show(mddev_t *mddev, char *page) |
| 4010 | { |
| 4011 | if (mddev->reshape_position != MaxSector) |
| 4012 | return sprintf(page, "%llu\n", |
| 4013 | (unsigned long long)mddev->reshape_position); |
| 4014 | strcpy(page, "none\n"); |
| 4015 | return 5; |
| 4016 | } |
| 4017 | |
| 4018 | static ssize_t |
| 4019 | reshape_position_store(mddev_t *mddev, const char *buf, size_t len) |
| 4020 | { |
| 4021 | char *e; |
| 4022 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 4023 | if (mddev->pers) |
| 4024 | return -EBUSY; |
| 4025 | if (buf == e || (*e && *e != '\n')) |
| 4026 | return -EINVAL; |
| 4027 | mddev->reshape_position = new; |
| 4028 | mddev->delta_disks = 0; |
| 4029 | mddev->new_level = mddev->level; |
| 4030 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4031 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4032 | return len; |
| 4033 | } |
| 4034 | |
| 4035 | static struct md_sysfs_entry md_reshape_position = |
| 4036 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 4037 | reshape_position_store); |
| 4038 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4039 | static ssize_t |
| 4040 | array_size_show(mddev_t *mddev, char *page) |
| 4041 | { |
| 4042 | if (mddev->external_size) |
| 4043 | return sprintf(page, "%llu\n", |
| 4044 | (unsigned long long)mddev->array_sectors/2); |
| 4045 | else |
| 4046 | return sprintf(page, "default\n"); |
| 4047 | } |
| 4048 | |
| 4049 | static ssize_t |
| 4050 | array_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 4051 | { |
| 4052 | sector_t sectors; |
| 4053 | |
| 4054 | if (strncmp(buf, "default", 7) == 0) { |
| 4055 | if (mddev->pers) |
| 4056 | sectors = mddev->pers->size(mddev, 0, 0); |
| 4057 | else |
| 4058 | sectors = mddev->array_sectors; |
| 4059 | |
| 4060 | mddev->external_size = 0; |
| 4061 | } else { |
| 4062 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
| 4063 | return -EINVAL; |
| 4064 | if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
NeilBrown | 2b69c83 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4065 | return -E2BIG; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4066 | |
| 4067 | mddev->external_size = 1; |
| 4068 | } |
| 4069 | |
| 4070 | mddev->array_sectors = sectors; |
| 4071 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 4072 | if (mddev->pers) |
| 4073 | revalidate_disk(mddev->gendisk); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4074 | |
| 4075 | return len; |
| 4076 | } |
| 4077 | |
| 4078 | static struct md_sysfs_entry md_array_size = |
| 4079 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 4080 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4081 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4082 | static struct attribute *md_default_attrs[] = { |
| 4083 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4084 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4085 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4086 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4087 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4088 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4089 | &md_metadata.attr, |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4090 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4091 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4092 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4093 | &md_reshape_position.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4094 | &md_array_size.attr, |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4095 | &max_corr_read_errors.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4096 | NULL, |
| 4097 | }; |
| 4098 | |
| 4099 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4100 | &md_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4101 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4102 | &md_sync_min.attr, |
| 4103 | &md_sync_max.attr, |
| 4104 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4105 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4106 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4107 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4108 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4109 | &md_suspend_lo.attr, |
| 4110 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4111 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4112 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4113 | NULL, |
| 4114 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4115 | static struct attribute_group md_redundancy_group = { |
| 4116 | .name = NULL, |
| 4117 | .attrs = md_redundancy_attrs, |
| 4118 | }; |
| 4119 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4120 | |
| 4121 | static ssize_t |
| 4122 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 4123 | { |
| 4124 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 4125 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4126 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4127 | |
| 4128 | if (!entry->show) |
| 4129 | return -EIO; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4130 | rv = mddev_lock(mddev); |
| 4131 | if (!rv) { |
| 4132 | rv = entry->show(mddev, page); |
| 4133 | mddev_unlock(mddev); |
| 4134 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4135 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4136 | } |
| 4137 | |
| 4138 | static ssize_t |
| 4139 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 4140 | const char *page, size_t length) |
| 4141 | { |
| 4142 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 4143 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4144 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4145 | |
| 4146 | if (!entry->store) |
| 4147 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 4148 | if (!capable(CAP_SYS_ADMIN)) |
| 4149 | return -EACCES; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4150 | rv = mddev_lock(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4151 | if (mddev->hold_active == UNTIL_IOCTL) |
| 4152 | mddev->hold_active = 0; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4153 | if (!rv) { |
| 4154 | rv = entry->store(mddev, page, length); |
| 4155 | mddev_unlock(mddev); |
| 4156 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4157 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4158 | } |
| 4159 | |
| 4160 | static void md_free(struct kobject *ko) |
| 4161 | { |
| 4162 | mddev_t *mddev = container_of(ko, mddev_t, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4163 | |
| 4164 | if (mddev->sysfs_state) |
| 4165 | sysfs_put(mddev->sysfs_state); |
| 4166 | |
| 4167 | if (mddev->gendisk) { |
| 4168 | del_gendisk(mddev->gendisk); |
| 4169 | put_disk(mddev->gendisk); |
| 4170 | } |
| 4171 | if (mddev->queue) |
| 4172 | blk_cleanup_queue(mddev->queue); |
| 4173 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4174 | kfree(mddev); |
| 4175 | } |
| 4176 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 4177 | static const struct sysfs_ops md_sysfs_ops = { |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4178 | .show = md_attr_show, |
| 4179 | .store = md_attr_store, |
| 4180 | }; |
| 4181 | static struct kobj_type md_ktype = { |
| 4182 | .release = md_free, |
| 4183 | .sysfs_ops = &md_sysfs_ops, |
| 4184 | .default_attrs = md_default_attrs, |
| 4185 | }; |
| 4186 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4187 | int mdp_major = 0; |
| 4188 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4189 | static void mddev_delayed_delete(struct work_struct *ws) |
| 4190 | { |
| 4191 | mddev_t *mddev = container_of(ws, mddev_t, del_work); |
| 4192 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4193 | sysfs_remove_group(&mddev->kobj, &md_bitmap_group); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4194 | kobject_del(&mddev->kobj); |
| 4195 | kobject_put(&mddev->kobj); |
| 4196 | } |
| 4197 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4198 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4199 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4200 | static DEFINE_MUTEX(disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4201 | mddev_t *mddev = mddev_find(dev); |
| 4202 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4203 | int partitioned; |
| 4204 | int shift; |
| 4205 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4206 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4207 | |
| 4208 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4209 | return -ENODEV; |
| 4210 | |
| 4211 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 4212 | shift = partitioned ? MdpMinorShift : 0; |
| 4213 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4214 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4215 | /* wait for any previous instance if this device |
| 4216 | * to be completed removed (mddev_delayed_delete). |
| 4217 | */ |
| 4218 | flush_scheduled_work(); |
| 4219 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4220 | mutex_lock(&disks_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4221 | error = -EEXIST; |
| 4222 | if (mddev->gendisk) |
| 4223 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4224 | |
| 4225 | if (name) { |
| 4226 | /* Need to ensure that 'name' is not a duplicate. |
| 4227 | */ |
| 4228 | mddev_t *mddev2; |
| 4229 | spin_lock(&all_mddevs_lock); |
| 4230 | |
| 4231 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 4232 | if (mddev2->gendisk && |
| 4233 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 4234 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4235 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4236 | } |
| 4237 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4238 | } |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4239 | |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4240 | error = -ENOMEM; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4241 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4242 | if (!mddev->queue) |
| 4243 | goto abort; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4244 | mddev->queue->queuedata = mddev; |
| 4245 | |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4246 | /* Can be unlocked because the queue is new: no concurrency */ |
| 4247 | queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue); |
| 4248 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4249 | blk_queue_make_request(mddev->queue, md_make_request); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4250 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4251 | disk = alloc_disk(1 << shift); |
| 4252 | if (!disk) { |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4253 | blk_cleanup_queue(mddev->queue); |
| 4254 | mddev->queue = NULL; |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4255 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4256 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4257 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4258 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4259 | if (name) |
| 4260 | strcpy(disk->disk_name, name); |
| 4261 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4262 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f46 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 4263 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4264 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4265 | disk->fops = &md_fops; |
| 4266 | disk->private_data = mddev; |
| 4267 | disk->queue = mddev->queue; |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4268 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4269 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4270 | * remove it now. |
| 4271 | */ |
| 4272 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4273 | add_disk(disk); |
| 4274 | mddev->gendisk = disk; |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 4275 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
| 4276 | &disk_to_dev(disk)->kobj, "%s", "md"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4277 | if (error) { |
| 4278 | /* This isn't possible, but as kobject_init_and_add is marked |
| 4279 | * __must_check, we must do something with the result |
| 4280 | */ |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4281 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 4282 | disk->disk_name); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4283 | error = 0; |
| 4284 | } |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4285 | if (sysfs_create_group(&mddev->kobj, &md_bitmap_group)) |
| 4286 | printk(KERN_DEBUG "pointless warning\n"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4287 | abort: |
| 4288 | mutex_unlock(&disks_mutex); |
| 4289 | if (!error) { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4290 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
Eric W. Biederman | 3ff195b | 2010-03-30 11:31:26 -0700 | [diff] [blame] | 4291 | mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, NULL, "array_state"); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4292 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4293 | mddev_put(mddev); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4294 | return error; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4295 | } |
| 4296 | |
| 4297 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 4298 | { |
| 4299 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4300 | return NULL; |
| 4301 | } |
| 4302 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4303 | static int add_named_array(const char *val, struct kernel_param *kp) |
| 4304 | { |
| 4305 | /* val must be "md_*" where * is not all digits. |
| 4306 | * We allocate an array with a large free minor number, and |
| 4307 | * set the name to val. val must not already be an active name. |
| 4308 | */ |
| 4309 | int len = strlen(val); |
| 4310 | char buf[DISK_NAME_LEN]; |
| 4311 | |
| 4312 | while (len && val[len-1] == '\n') |
| 4313 | len--; |
| 4314 | if (len >= DISK_NAME_LEN) |
| 4315 | return -E2BIG; |
| 4316 | strlcpy(buf, val, len+1); |
| 4317 | if (strncmp(buf, "md_", 3) != 0) |
| 4318 | return -EINVAL; |
| 4319 | return md_alloc(0, buf); |
| 4320 | } |
| 4321 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4322 | static void md_safemode_timeout(unsigned long data) |
| 4323 | { |
| 4324 | mddev_t *mddev = (mddev_t *) data; |
| 4325 | |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4326 | if (!atomic_read(&mddev->writes_pending)) { |
| 4327 | mddev->safemode = 1; |
| 4328 | if (mddev->external) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4329 | sysfs_notify_dirent(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4330 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4331 | md_wakeup_thread(mddev->thread); |
| 4332 | } |
| 4333 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4334 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4335 | |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 4336 | static int md_run(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4337 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4338 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4339 | mdk_rdev_t *rdev; |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4340 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4341 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4342 | if (list_empty(&mddev->disks)) |
| 4343 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4344 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4345 | |
| 4346 | if (mddev->pers) |
| 4347 | return -EBUSY; |
| 4348 | |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 4349 | /* These two calls synchronise us with the |
| 4350 | * sysfs_remove_group calls in mddev_unlock, |
| 4351 | * so they must have completed. |
| 4352 | */ |
| 4353 | mutex_lock(&mddev->open_mutex); |
| 4354 | mutex_unlock(&mddev->open_mutex); |
| 4355 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4356 | /* |
| 4357 | * Analyze all RAID superblock(s) |
| 4358 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4359 | if (!mddev->raid_disks) { |
| 4360 | if (!mddev->persistent) |
| 4361 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4362 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4363 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4364 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4365 | if (mddev->level != LEVEL_NONE) |
| 4366 | request_module("md-level-%d", mddev->level); |
| 4367 | else if (mddev->clevel[0]) |
| 4368 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4369 | |
| 4370 | /* |
| 4371 | * Drop all container device buffers, from now on |
| 4372 | * the only valid external interface is through the md |
| 4373 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4374 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4375 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4376 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4377 | continue; |
| 4378 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 4379 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4380 | |
| 4381 | /* perform some consistency tests on the device. |
| 4382 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4383 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4384 | */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4385 | if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4386 | if (mddev->dev_sectors && |
| 4387 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4388 | > rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4389 | printk("md: %s: data overlaps metadata\n", |
| 4390 | mdname(mddev)); |
| 4391 | return -EINVAL; |
| 4392 | } |
| 4393 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4394 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4395 | > rdev->data_offset) { |
| 4396 | printk("md: %s: metadata overlaps data\n", |
| 4397 | mdname(mddev)); |
| 4398 | return -EINVAL; |
| 4399 | } |
| 4400 | } |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 4401 | sysfs_notify_dirent(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4402 | } |
| 4403 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4404 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4405 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4406 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4407 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4408 | if (mddev->level != LEVEL_NONE) |
| 4409 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 4410 | mddev->level); |
| 4411 | else |
| 4412 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 4413 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4414 | return -EINVAL; |
| 4415 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4416 | mddev->pers = pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4417 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4418 | if (mddev->level != pers->level) { |
| 4419 | mddev->level = pers->level; |
| 4420 | mddev->new_level = pers->level; |
| 4421 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4422 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4423 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4424 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4425 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4426 | /* This personality cannot handle reshaping... */ |
| 4427 | mddev->pers = NULL; |
| 4428 | module_put(pers->owner); |
| 4429 | return -EINVAL; |
| 4430 | } |
| 4431 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4432 | if (pers->sync_request) { |
| 4433 | /* Warn if this is a potentially silly |
| 4434 | * configuration. |
| 4435 | */ |
| 4436 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 4437 | mdk_rdev_t *rdev2; |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4438 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4439 | |
| 4440 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4441 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4442 | if (rdev < rdev2 && |
| 4443 | rdev->bdev->bd_contains == |
| 4444 | rdev2->bdev->bd_contains) { |
| 4445 | printk(KERN_WARNING |
| 4446 | "%s: WARNING: %s appears to be" |
| 4447 | " on the same physical disk as" |
| 4448 | " %s.\n", |
| 4449 | mdname(mddev), |
| 4450 | bdevname(rdev->bdev,b), |
| 4451 | bdevname(rdev2->bdev,b2)); |
| 4452 | warned = 1; |
| 4453 | } |
| 4454 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4455 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4456 | if (warned) |
| 4457 | printk(KERN_WARNING |
| 4458 | "True protection against single-disk" |
| 4459 | " failure might be compromised.\n"); |
| 4460 | } |
| 4461 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 4462 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4463 | /* may be over-ridden by personality */ |
| 4464 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 4465 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 4466 | mddev->barriers_work = 1; |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4467 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4468 | |
NeilBrown | 0f9552b5 | 2009-12-30 12:08:50 +1100 | [diff] [blame] | 4469 | if (start_readonly && mddev->ro == 0) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4470 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 4471 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4472 | err = mddev->pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 4473 | if (err) |
| 4474 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4475 | else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) { |
| 4476 | WARN_ONCE(!mddev->external_size, "%s: default size too small," |
| 4477 | " but 'external_size' not in effect?\n", __func__); |
| 4478 | printk(KERN_ERR |
| 4479 | "md: invalid array_size %llu > default size %llu\n", |
| 4480 | (unsigned long long)mddev->array_sectors / 2, |
| 4481 | (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2); |
| 4482 | err = -EINVAL; |
| 4483 | mddev->pers->stop(mddev); |
| 4484 | } |
| 4485 | if (err == 0 && mddev->pers->sync_request) { |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4486 | err = bitmap_create(mddev); |
| 4487 | if (err) { |
| 4488 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 4489 | mdname(mddev), err); |
| 4490 | mddev->pers->stop(mddev); |
| 4491 | } |
| 4492 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4493 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4494 | module_put(mddev->pers->owner); |
| 4495 | mddev->pers = NULL; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4496 | bitmap_destroy(mddev); |
| 4497 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4498 | } |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4499 | if (mddev->pers->sync_request) { |
| 4500 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 4501 | printk(KERN_WARNING |
| 4502 | "md: cannot register extra attributes for %s\n", |
| 4503 | mdname(mddev)); |
Eric W. Biederman | 3ff195b | 2010-03-30 11:31:26 -0700 | [diff] [blame] | 4504 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4505 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 4506 | mddev->ro = 0; |
| 4507 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4508 | atomic_set(&mddev->writes_pending,0); |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4509 | atomic_set(&mddev->max_corr_read_errors, |
| 4510 | MD_DEFAULT_MAX_CORRECTED_READ_ERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4511 | mddev->safemode = 0; |
| 4512 | mddev->safemode_timer.function = md_safemode_timeout; |
| 4513 | mddev->safemode_timer.data = (unsigned long) mddev; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4514 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4515 | mddev->in_sync = 1; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4516 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4517 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4518 | if (rdev->raid_disk >= 0) { |
| 4519 | char nm[20]; |
| 4520 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4521 | if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm)) |
| 4522 | printk("md: cannot register %s for %s\n", |
| 4523 | nm, mdname(mddev)); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4524 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4525 | |
| 4526 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4527 | |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4528 | if (mddev->flags) |
| 4529 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4530 | |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 4531 | md_wakeup_thread(mddev->thread); |
| 4532 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4533 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4534 | md_new_event(mddev); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4535 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 4536 | if (mddev->sysfs_action) |
| 4537 | sysfs_notify_dirent(mddev->sysfs_action); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4538 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4539 | return 0; |
| 4540 | } |
| 4541 | |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 4542 | static int do_md_run(mddev_t *mddev) |
| 4543 | { |
| 4544 | int err; |
| 4545 | |
| 4546 | err = md_run(mddev); |
| 4547 | if (err) |
| 4548 | goto out; |
| 4549 | |
| 4550 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 4551 | revalidate_disk(mddev->gendisk); |
| 4552 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
| 4553 | out: |
| 4554 | return err; |
| 4555 | } |
| 4556 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4557 | static int restart_array(mddev_t *mddev) |
| 4558 | { |
| 4559 | struct gendisk *disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4560 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4561 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4562 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4563 | return -ENXIO; |
| 4564 | if (!mddev->pers) |
| 4565 | return -EINVAL; |
| 4566 | if (!mddev->ro) |
| 4567 | return -EBUSY; |
| 4568 | mddev->safemode = 0; |
| 4569 | mddev->ro = 0; |
| 4570 | set_disk_ro(disk, 0); |
| 4571 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 4572 | mdname(mddev)); |
| 4573 | /* Kick recovery or resync if necessary */ |
| 4574 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4575 | md_wakeup_thread(mddev->thread); |
| 4576 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4577 | sysfs_notify_dirent(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4578 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4579 | } |
| 4580 | |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4581 | /* similar to deny_write_access, but accounts for our holding a reference |
| 4582 | * to the file ourselves */ |
| 4583 | static int deny_bitmap_write_access(struct file * file) |
| 4584 | { |
| 4585 | struct inode *inode = file->f_mapping->host; |
| 4586 | |
| 4587 | spin_lock(&inode->i_lock); |
| 4588 | if (atomic_read(&inode->i_writecount) > 1) { |
| 4589 | spin_unlock(&inode->i_lock); |
| 4590 | return -ETXTBSY; |
| 4591 | } |
| 4592 | atomic_set(&inode->i_writecount, -1); |
| 4593 | spin_unlock(&inode->i_lock); |
| 4594 | |
| 4595 | return 0; |
| 4596 | } |
| 4597 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4598 | void restore_bitmap_write_access(struct file *file) |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4599 | { |
| 4600 | struct inode *inode = file->f_mapping->host; |
| 4601 | |
| 4602 | spin_lock(&inode->i_lock); |
| 4603 | atomic_set(&inode->i_writecount, 1); |
| 4604 | spin_unlock(&inode->i_lock); |
| 4605 | } |
| 4606 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4607 | static void md_clean(mddev_t *mddev) |
| 4608 | { |
| 4609 | mddev->array_sectors = 0; |
| 4610 | mddev->external_size = 0; |
| 4611 | mddev->dev_sectors = 0; |
| 4612 | mddev->raid_disks = 0; |
| 4613 | mddev->recovery_cp = 0; |
| 4614 | mddev->resync_min = 0; |
| 4615 | mddev->resync_max = MaxSector; |
| 4616 | mddev->reshape_position = MaxSector; |
| 4617 | mddev->external = 0; |
| 4618 | mddev->persistent = 0; |
| 4619 | mddev->level = LEVEL_NONE; |
| 4620 | mddev->clevel[0] = 0; |
| 4621 | mddev->flags = 0; |
| 4622 | mddev->ro = 0; |
| 4623 | mddev->metadata_type[0] = 0; |
| 4624 | mddev->chunk_sectors = 0; |
| 4625 | mddev->ctime = mddev->utime = 0; |
| 4626 | mddev->layout = 0; |
| 4627 | mddev->max_disks = 0; |
| 4628 | mddev->events = 0; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 4629 | mddev->can_decrease_events = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4630 | mddev->delta_disks = 0; |
| 4631 | mddev->new_level = LEVEL_NONE; |
| 4632 | mddev->new_layout = 0; |
| 4633 | mddev->new_chunk_sectors = 0; |
| 4634 | mddev->curr_resync = 0; |
| 4635 | mddev->resync_mismatches = 0; |
| 4636 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 4637 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 4638 | mddev->recovery = 0; |
| 4639 | mddev->in_sync = 0; |
| 4640 | mddev->degraded = 0; |
| 4641 | mddev->barriers_work = 0; |
| 4642 | mddev->safemode = 0; |
| 4643 | mddev->bitmap_info.offset = 0; |
| 4644 | mddev->bitmap_info.default_offset = 0; |
| 4645 | mddev->bitmap_info.chunksize = 0; |
| 4646 | mddev->bitmap_info.daemon_sleep = 0; |
| 4647 | mddev->bitmap_info.max_write_behind = 0; |
| 4648 | } |
| 4649 | |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 4650 | static void md_stop_writes(mddev_t *mddev) |
| 4651 | { |
| 4652 | if (mddev->sync_thread) { |
| 4653 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4654 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 4655 | md_unregister_thread(mddev->sync_thread); |
| 4656 | mddev->sync_thread = NULL; |
| 4657 | } |
| 4658 | |
| 4659 | del_timer_sync(&mddev->safemode_timer); |
| 4660 | |
| 4661 | bitmap_flush(mddev); |
| 4662 | md_super_wait(mddev); |
| 4663 | |
| 4664 | if (!mddev->in_sync || mddev->flags) { |
| 4665 | /* mark array as shutdown cleanly */ |
| 4666 | mddev->in_sync = 1; |
| 4667 | md_update_sb(mddev, 1); |
| 4668 | } |
| 4669 | } |
| 4670 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4671 | static void md_stop(mddev_t *mddev) |
| 4672 | { |
NeilBrown | cca9cf9 | 2010-04-01 12:08:16 +1100 | [diff] [blame] | 4673 | md_stop_writes(mddev); |
| 4674 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4675 | mddev->pers->stop(mddev); |
| 4676 | if (mddev->pers->sync_request && mddev->to_remove == NULL) |
| 4677 | mddev->to_remove = &md_redundancy_group; |
| 4678 | module_put(mddev->pers->owner); |
| 4679 | mddev->pers = NULL; |
NeilBrown | cca9cf9 | 2010-04-01 12:08:16 +1100 | [diff] [blame] | 4680 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4681 | } |
| 4682 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4683 | static int md_set_readonly(mddev_t *mddev, int is_open) |
| 4684 | { |
| 4685 | int err = 0; |
| 4686 | mutex_lock(&mddev->open_mutex); |
| 4687 | if (atomic_read(&mddev->openers) > is_open) { |
| 4688 | printk("md: %s still in use.\n",mdname(mddev)); |
| 4689 | err = -EBUSY; |
| 4690 | goto out; |
| 4691 | } |
| 4692 | if (mddev->pers) { |
| 4693 | md_stop_writes(mddev); |
| 4694 | |
| 4695 | err = -ENXIO; |
| 4696 | if (mddev->ro==1) |
| 4697 | goto out; |
| 4698 | mddev->ro = 1; |
| 4699 | set_disk_ro(mddev->gendisk, 1); |
| 4700 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4701 | sysfs_notify_dirent(mddev->sysfs_state); |
| 4702 | err = 0; |
| 4703 | } |
| 4704 | out: |
| 4705 | mutex_unlock(&mddev->open_mutex); |
| 4706 | return err; |
| 4707 | } |
| 4708 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4709 | /* mode: |
| 4710 | * 0 - completely stop and dis-assemble array |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4711 | * 2 - stop but do not disassemble array |
| 4712 | */ |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4713 | 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] | 4714 | { |
| 4715 | int err = 0; |
| 4716 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 4717 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4718 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 4719 | mutex_lock(&mddev->open_mutex); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4720 | if (atomic_read(&mddev->openers) > is_open) { |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4721 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 4722 | err = -EBUSY; |
| 4723 | } else if (mddev->pers) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4724 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4725 | if (mddev->ro) |
| 4726 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4727 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4728 | md_stop(mddev); |
| 4729 | mddev->queue->merge_bvec_fn = NULL; |
| 4730 | mddev->queue->unplug_fn = NULL; |
| 4731 | mddev->queue->backing_dev_info.congested_fn = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4732 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4733 | /* tell userspace to handle 'inactive' */ |
| 4734 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4735 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4736 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4737 | if (rdev->raid_disk >= 0) { |
| 4738 | char nm[20]; |
| 4739 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 4740 | sysfs_remove_link(&mddev->kobj, nm); |
| 4741 | } |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 4742 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4743 | set_capacity(disk, 0); |
| 4744 | revalidate_disk(disk); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4745 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4746 | if (mddev->ro) |
| 4747 | mddev->ro = 0; |
| 4748 | |
NeilBrown | 80ffb3c | 2009-08-18 10:35:26 +1000 | [diff] [blame] | 4749 | err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4750 | } |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 4751 | mutex_unlock(&mddev->open_mutex); |
| 4752 | if (err) |
| 4753 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4754 | /* |
| 4755 | * Free resources if final stop |
| 4756 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4757 | if (mode == 0) { |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4758 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4759 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 4760 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4761 | bitmap_destroy(mddev); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4762 | if (mddev->bitmap_info.file) { |
| 4763 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 4764 | fput(mddev->bitmap_info.file); |
| 4765 | mddev->bitmap_info.file = NULL; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4766 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4767 | mddev->bitmap_info.offset = 0; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4768 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4769 | export_array(mddev); |
| 4770 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4771 | md_clean(mddev); |
NeilBrown | 934d9c2 | 2008-10-28 17:01:23 +1100 | [diff] [blame] | 4772 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4773 | if (mddev->hold_active == UNTIL_STOP) |
| 4774 | mddev->hold_active = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4775 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4776 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4777 | err = 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 4778 | blk_integrity_unregister(disk); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4779 | md_new_event(mddev); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4780 | sysfs_notify_dirent(mddev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4781 | return err; |
| 4782 | } |
| 4783 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4784 | #ifndef MODULE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4785 | static void autorun_array(mddev_t *mddev) |
| 4786 | { |
| 4787 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4788 | int err; |
| 4789 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4790 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4791 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4792 | |
| 4793 | printk(KERN_INFO "md: running: "); |
| 4794 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4795 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4796 | char b[BDEVNAME_SIZE]; |
| 4797 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 4798 | } |
| 4799 | printk("\n"); |
| 4800 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4801 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4802 | if (err) { |
| 4803 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4804 | do_md_stop(mddev, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4805 | } |
| 4806 | } |
| 4807 | |
| 4808 | /* |
| 4809 | * lets try to run arrays based on all disks that have arrived |
| 4810 | * until now. (those are in pending_raid_disks) |
| 4811 | * |
| 4812 | * the method: pick the first pending disk, collect all disks with |
| 4813 | * the same UUID, remove all from the pending list and put them into |
| 4814 | * the 'same_array' list. Then order this list based on superblock |
| 4815 | * update time (freshest comes first), kick out 'old' disks and |
| 4816 | * compare superblocks. If everything's fine then run it. |
| 4817 | * |
| 4818 | * If "unit" is allocated, then bump its reference count |
| 4819 | */ |
| 4820 | static void autorun_devices(int part) |
| 4821 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4822 | mdk_rdev_t *rdev0, *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4823 | mddev_t *mddev; |
| 4824 | char b[BDEVNAME_SIZE]; |
| 4825 | |
| 4826 | printk(KERN_INFO "md: autorun ...\n"); |
| 4827 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4828 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4829 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 4830 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4831 | rdev0 = list_entry(pending_raid_disks.next, |
| 4832 | mdk_rdev_t, same_set); |
| 4833 | |
| 4834 | printk(KERN_INFO "md: considering %s ...\n", |
| 4835 | bdevname(rdev0->bdev,b)); |
| 4836 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4837 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4838 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 4839 | printk(KERN_INFO "md: adding %s ...\n", |
| 4840 | bdevname(rdev->bdev,b)); |
| 4841 | list_move(&rdev->same_set, &candidates); |
| 4842 | } |
| 4843 | /* |
| 4844 | * now we have a set of devices, with all of them having |
| 4845 | * mostly sane superblocks. It's time to allocate the |
| 4846 | * mddev. |
| 4847 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4848 | if (part) { |
| 4849 | dev = MKDEV(mdp_major, |
| 4850 | rdev0->preferred_minor << MdpMinorShift); |
| 4851 | unit = MINOR(dev) >> MdpMinorShift; |
| 4852 | } else { |
| 4853 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 4854 | unit = MINOR(dev); |
| 4855 | } |
| 4856 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4857 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 4858 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 4859 | break; |
| 4860 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4861 | |
| 4862 | md_probe(dev, NULL, NULL); |
| 4863 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 4864 | if (!mddev || !mddev->gendisk) { |
| 4865 | if (mddev) |
| 4866 | mddev_put(mddev); |
| 4867 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4868 | "md: cannot allocate memory for md drive.\n"); |
| 4869 | break; |
| 4870 | } |
| 4871 | if (mddev_lock(mddev)) |
| 4872 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 4873 | mdname(mddev)); |
| 4874 | else if (mddev->raid_disks || mddev->major_version |
| 4875 | || !list_empty(&mddev->disks)) { |
| 4876 | printk(KERN_WARNING |
| 4877 | "md: %s already running, cannot run %s\n", |
| 4878 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 4879 | mddev_unlock(mddev); |
| 4880 | } else { |
| 4881 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4882 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4883 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4884 | list_del_init(&rdev->same_set); |
| 4885 | if (bind_rdev_to_array(rdev, mddev)) |
| 4886 | export_rdev(rdev); |
| 4887 | } |
| 4888 | autorun_array(mddev); |
| 4889 | mddev_unlock(mddev); |
| 4890 | } |
| 4891 | /* on success, candidates will be empty, on error |
| 4892 | * it won't... |
| 4893 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4894 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4895 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4896 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4897 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4898 | mddev_put(mddev); |
| 4899 | } |
| 4900 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 4901 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4902 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4903 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4904 | static int get_version(void __user * arg) |
| 4905 | { |
| 4906 | mdu_version_t ver; |
| 4907 | |
| 4908 | ver.major = MD_MAJOR_VERSION; |
| 4909 | ver.minor = MD_MINOR_VERSION; |
| 4910 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 4911 | |
| 4912 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 4913 | return -EFAULT; |
| 4914 | |
| 4915 | return 0; |
| 4916 | } |
| 4917 | |
| 4918 | static int get_array_info(mddev_t * mddev, void __user * arg) |
| 4919 | { |
| 4920 | mdu_array_info_t info; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4921 | int nr,working,insync,failed,spare; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4922 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4923 | |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4924 | nr=working=insync=failed=spare=0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4925 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4926 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4927 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4928 | failed++; |
| 4929 | else { |
| 4930 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4931 | if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4932 | insync++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4933 | else |
| 4934 | spare++; |
| 4935 | } |
| 4936 | } |
| 4937 | |
| 4938 | info.major_version = mddev->major_version; |
| 4939 | info.minor_version = mddev->minor_version; |
| 4940 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 4941 | info.ctime = mddev->ctime; |
| 4942 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4943 | info.size = mddev->dev_sectors / 2; |
| 4944 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 4945 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4946 | info.nr_disks = nr; |
| 4947 | info.raid_disks = mddev->raid_disks; |
| 4948 | info.md_minor = mddev->md_minor; |
| 4949 | info.not_persistent= !mddev->persistent; |
| 4950 | |
| 4951 | info.utime = mddev->utime; |
| 4952 | info.state = 0; |
| 4953 | if (mddev->in_sync) |
| 4954 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4955 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4956 | info.state = (1<<MD_SB_BITMAP_PRESENT); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4957 | info.active_disks = insync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4958 | info.working_disks = working; |
| 4959 | info.failed_disks = failed; |
| 4960 | info.spare_disks = spare; |
| 4961 | |
| 4962 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4963 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4964 | |
| 4965 | if (copy_to_user(arg, &info, sizeof(info))) |
| 4966 | return -EFAULT; |
| 4967 | |
| 4968 | return 0; |
| 4969 | } |
| 4970 | |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 4971 | static int get_bitmap_file(mddev_t * mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4972 | { |
| 4973 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
| 4974 | char *ptr, *buf = NULL; |
| 4975 | int err = -ENOMEM; |
| 4976 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 4977 | if (md_allow_write(mddev)) |
| 4978 | file = kmalloc(sizeof(*file), GFP_NOIO); |
| 4979 | else |
| 4980 | file = kmalloc(sizeof(*file), GFP_KERNEL); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 4981 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4982 | if (!file) |
| 4983 | goto out; |
| 4984 | |
| 4985 | /* bitmap disabled, zero the first byte and copy out */ |
| 4986 | if (!mddev->bitmap || !mddev->bitmap->file) { |
| 4987 | file->pathname[0] = '\0'; |
| 4988 | goto copy_out; |
| 4989 | } |
| 4990 | |
| 4991 | buf = kmalloc(sizeof(file->pathname), GFP_KERNEL); |
| 4992 | if (!buf) |
| 4993 | goto out; |
| 4994 | |
Christoph Hellwig | 6bcfd60 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 4995 | ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname)); |
| 4996 | if (IS_ERR(ptr)) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4997 | goto out; |
| 4998 | |
| 4999 | strcpy(file->pathname, ptr); |
| 5000 | |
| 5001 | copy_out: |
| 5002 | err = 0; |
| 5003 | if (copy_to_user(arg, file, sizeof(*file))) |
| 5004 | err = -EFAULT; |
| 5005 | out: |
| 5006 | kfree(buf); |
| 5007 | kfree(file); |
| 5008 | return err; |
| 5009 | } |
| 5010 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5011 | static int get_disk_info(mddev_t * mddev, void __user * arg) |
| 5012 | { |
| 5013 | mdu_disk_info_t info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5014 | mdk_rdev_t *rdev; |
| 5015 | |
| 5016 | if (copy_from_user(&info, arg, sizeof(info))) |
| 5017 | return -EFAULT; |
| 5018 | |
Andre Noll | 26ef379 | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5019 | rdev = find_rdev_nr(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5020 | if (rdev) { |
| 5021 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 5022 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 5023 | info.raid_disk = rdev->raid_disk; |
| 5024 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5025 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5026 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5027 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5028 | info.state |= (1<<MD_DISK_ACTIVE); |
| 5029 | info.state |= (1<<MD_DISK_SYNC); |
| 5030 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5031 | if (test_bit(WriteMostly, &rdev->flags)) |
| 5032 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5033 | } else { |
| 5034 | info.major = info.minor = 0; |
| 5035 | info.raid_disk = -1; |
| 5036 | info.state = (1<<MD_DISK_REMOVED); |
| 5037 | } |
| 5038 | |
| 5039 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5040 | return -EFAULT; |
| 5041 | |
| 5042 | return 0; |
| 5043 | } |
| 5044 | |
| 5045 | static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info) |
| 5046 | { |
| 5047 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 5048 | mdk_rdev_t *rdev; |
| 5049 | dev_t dev = MKDEV(info->major,info->minor); |
| 5050 | |
| 5051 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 5052 | return -EOVERFLOW; |
| 5053 | |
| 5054 | if (!mddev->raid_disks) { |
| 5055 | int err; |
| 5056 | /* expecting a device which has a superblock */ |
| 5057 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 5058 | if (IS_ERR(rdev)) { |
| 5059 | printk(KERN_WARNING |
| 5060 | "md: md_import_device returned %ld\n", |
| 5061 | PTR_ERR(rdev)); |
| 5062 | return PTR_ERR(rdev); |
| 5063 | } |
| 5064 | if (!list_empty(&mddev->disks)) { |
| 5065 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 5066 | mdk_rdev_t, same_set); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5067 | err = super_types[mddev->major_version] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5068 | .load_super(rdev, rdev0, mddev->minor_version); |
| 5069 | if (err < 0) { |
| 5070 | printk(KERN_WARNING |
| 5071 | "md: %s has different UUID to %s\n", |
| 5072 | bdevname(rdev->bdev,b), |
| 5073 | bdevname(rdev0->bdev,b2)); |
| 5074 | export_rdev(rdev); |
| 5075 | return -EINVAL; |
| 5076 | } |
| 5077 | } |
| 5078 | err = bind_rdev_to_array(rdev, mddev); |
| 5079 | if (err) |
| 5080 | export_rdev(rdev); |
| 5081 | return err; |
| 5082 | } |
| 5083 | |
| 5084 | /* |
| 5085 | * add_new_disk can be used once the array is assembled |
| 5086 | * to add "hot spares". They must already have a superblock |
| 5087 | * written |
| 5088 | */ |
| 5089 | if (mddev->pers) { |
| 5090 | int err; |
| 5091 | if (!mddev->pers->hot_add_disk) { |
| 5092 | printk(KERN_WARNING |
| 5093 | "%s: personality does not support diskops!\n", |
| 5094 | mdname(mddev)); |
| 5095 | return -EINVAL; |
| 5096 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 5097 | if (mddev->persistent) |
| 5098 | rdev = md_import_device(dev, mddev->major_version, |
| 5099 | mddev->minor_version); |
| 5100 | else |
| 5101 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5102 | if (IS_ERR(rdev)) { |
| 5103 | printk(KERN_WARNING |
| 5104 | "md: md_import_device returned %ld\n", |
| 5105 | PTR_ERR(rdev)); |
| 5106 | return PTR_ERR(rdev); |
| 5107 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5108 | /* set save_raid_disk if appropriate */ |
| 5109 | if (!mddev->persistent) { |
| 5110 | if (info->state & (1<<MD_DISK_SYNC) && |
| 5111 | info->raid_disk < mddev->raid_disks) |
| 5112 | rdev->raid_disk = info->raid_disk; |
| 5113 | else |
| 5114 | rdev->raid_disk = -1; |
| 5115 | } else |
| 5116 | super_types[mddev->major_version]. |
| 5117 | validate_super(mddev, rdev); |
| 5118 | rdev->saved_raid_disk = rdev->raid_disk; |
| 5119 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5120 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5121 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5122 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5123 | else |
| 5124 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5125 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5126 | rdev->raid_disk = -1; |
| 5127 | err = bind_rdev_to_array(rdev, mddev); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 5128 | if (!err && !mddev->pers->hot_remove_disk) { |
| 5129 | /* If there is hot_add_disk but no hot_remove_disk |
| 5130 | * then added disks for geometry changes, |
| 5131 | * and should be added immediately. |
| 5132 | */ |
| 5133 | super_types[mddev->major_version]. |
| 5134 | validate_super(mddev, rdev); |
| 5135 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 5136 | if (err) |
| 5137 | unbind_rdev_from_array(rdev); |
| 5138 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5139 | if (err) |
| 5140 | export_rdev(rdev); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 5141 | else |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 5142 | sysfs_notify_dirent(rdev->sysfs_state); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5143 | |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 5144 | md_update_sb(mddev, 1); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 5145 | if (mddev->degraded) |
| 5146 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5147 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 5148 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5149 | return err; |
| 5150 | } |
| 5151 | |
| 5152 | /* otherwise, add_new_disk is only allowed |
| 5153 | * for major_version==0 superblocks |
| 5154 | */ |
| 5155 | if (mddev->major_version != 0) { |
| 5156 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 5157 | mdname(mddev)); |
| 5158 | return -EINVAL; |
| 5159 | } |
| 5160 | |
| 5161 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 5162 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5163 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5164 | if (IS_ERR(rdev)) { |
| 5165 | printk(KERN_WARNING |
| 5166 | "md: error, md_import_device() returned %ld\n", |
| 5167 | PTR_ERR(rdev)); |
| 5168 | return PTR_ERR(rdev); |
| 5169 | } |
| 5170 | rdev->desc_nr = info->number; |
| 5171 | if (info->raid_disk < mddev->raid_disks) |
| 5172 | rdev->raid_disk = info->raid_disk; |
| 5173 | else |
| 5174 | rdev->raid_disk = -1; |
| 5175 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5176 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5177 | if (info->state & (1<<MD_DISK_SYNC)) |
| 5178 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5179 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5180 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5181 | set_bit(WriteMostly, &rdev->flags); |
| 5182 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5183 | if (!mddev->persistent) { |
| 5184 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5185 | rdev->sb_start = rdev->bdev->bd_inode->i_size / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5186 | } else |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5187 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5188 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5189 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5190 | err = bind_rdev_to_array(rdev, mddev); |
| 5191 | if (err) { |
| 5192 | export_rdev(rdev); |
| 5193 | return err; |
| 5194 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5195 | } |
| 5196 | |
| 5197 | return 0; |
| 5198 | } |
| 5199 | |
| 5200 | static int hot_remove_disk(mddev_t * mddev, dev_t dev) |
| 5201 | { |
| 5202 | char b[BDEVNAME_SIZE]; |
| 5203 | mdk_rdev_t *rdev; |
| 5204 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5205 | rdev = find_rdev(mddev, dev); |
| 5206 | if (!rdev) |
| 5207 | return -ENXIO; |
| 5208 | |
| 5209 | if (rdev->raid_disk >= 0) |
| 5210 | goto busy; |
| 5211 | |
| 5212 | kick_rdev_from_array(rdev); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5213 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5214 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5215 | |
| 5216 | return 0; |
| 5217 | busy: |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 5218 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5219 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5220 | return -EBUSY; |
| 5221 | } |
| 5222 | |
| 5223 | static int hot_add_disk(mddev_t * mddev, dev_t dev) |
| 5224 | { |
| 5225 | char b[BDEVNAME_SIZE]; |
| 5226 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5227 | mdk_rdev_t *rdev; |
| 5228 | |
| 5229 | if (!mddev->pers) |
| 5230 | return -ENODEV; |
| 5231 | |
| 5232 | if (mddev->major_version != 0) { |
| 5233 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 5234 | " version-0 superblocks.\n", |
| 5235 | mdname(mddev)); |
| 5236 | return -EINVAL; |
| 5237 | } |
| 5238 | if (!mddev->pers->hot_add_disk) { |
| 5239 | printk(KERN_WARNING |
| 5240 | "%s: personality does not support diskops!\n", |
| 5241 | mdname(mddev)); |
| 5242 | return -EINVAL; |
| 5243 | } |
| 5244 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5245 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5246 | if (IS_ERR(rdev)) { |
| 5247 | printk(KERN_WARNING |
| 5248 | "md: error, md_import_device() returned %ld\n", |
| 5249 | PTR_ERR(rdev)); |
| 5250 | return -EINVAL; |
| 5251 | } |
| 5252 | |
| 5253 | if (mddev->persistent) |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5254 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5255 | else |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5256 | rdev->sb_start = rdev->bdev->bd_inode->i_size / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5257 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5258 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5259 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5260 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5261 | printk(KERN_WARNING |
| 5262 | "md: can not hot-add faulty %s disk to %s!\n", |
| 5263 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5264 | err = -EINVAL; |
| 5265 | goto abort_export; |
| 5266 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5267 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5268 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 5269 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5270 | err = bind_rdev_to_array(rdev, mddev); |
| 5271 | if (err) |
| 5272 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5273 | |
| 5274 | /* |
| 5275 | * The rest should better be atomic, we can have disk failures |
| 5276 | * noticed in interrupt contexts ... |
| 5277 | */ |
| 5278 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5279 | rdev->raid_disk = -1; |
| 5280 | |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5281 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5282 | |
| 5283 | /* |
| 5284 | * Kick recovery, maybe this spare has to be added to the |
| 5285 | * array immediately. |
| 5286 | */ |
| 5287 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5288 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5289 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5290 | return 0; |
| 5291 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5292 | abort_export: |
| 5293 | export_rdev(rdev); |
| 5294 | return err; |
| 5295 | } |
| 5296 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5297 | static int set_bitmap_file(mddev_t *mddev, int fd) |
| 5298 | { |
| 5299 | int err; |
| 5300 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5301 | if (mddev->pers) { |
| 5302 | if (!mddev->pers->quiesce) |
| 5303 | return -EBUSY; |
| 5304 | if (mddev->recovery || mddev->sync_thread) |
| 5305 | return -EBUSY; |
| 5306 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5307 | } |
| 5308 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5309 | |
| 5310 | if (fd >= 0) { |
| 5311 | if (mddev->bitmap) |
| 5312 | return -EEXIST; /* cannot add when bitmap is present */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5313 | mddev->bitmap_info.file = fget(fd); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5314 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5315 | if (mddev->bitmap_info.file == NULL) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5316 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 5317 | mdname(mddev)); |
| 5318 | return -EBADF; |
| 5319 | } |
| 5320 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5321 | err = deny_bitmap_write_access(mddev->bitmap_info.file); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5322 | if (err) { |
| 5323 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 5324 | mdname(mddev)); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5325 | fput(mddev->bitmap_info.file); |
| 5326 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5327 | return err; |
| 5328 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5329 | mddev->bitmap_info.offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5330 | } else if (mddev->bitmap == NULL) |
| 5331 | return -ENOENT; /* cannot remove what isn't there */ |
| 5332 | err = 0; |
| 5333 | if (mddev->pers) { |
| 5334 | mddev->pers->quiesce(mddev, 1); |
| 5335 | if (fd >= 0) |
| 5336 | err = bitmap_create(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5337 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5338 | bitmap_destroy(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5339 | fd = -1; /* make sure to put the file */ |
| 5340 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5341 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5342 | } |
| 5343 | if (fd < 0) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5344 | if (mddev->bitmap_info.file) { |
| 5345 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 5346 | fput(mddev->bitmap_info.file); |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 5347 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5348 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5349 | } |
| 5350 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5351 | return err; |
| 5352 | } |
| 5353 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5354 | /* |
| 5355 | * set_array_info is used two different ways |
| 5356 | * The original usage is when creating a new array. |
| 5357 | * In this usage, raid_disks is > 0 and it together with |
| 5358 | * level, size, not_persistent,layout,chunksize determine the |
| 5359 | * shape of the array. |
| 5360 | * This will always create an array with a type-0.90.0 superblock. |
| 5361 | * The newer usage is when assembling an array. |
| 5362 | * In this case raid_disks will be 0, and the major_version field is |
| 5363 | * use to determine which style super-blocks are to be found on the devices. |
| 5364 | * The minor and patch _version numbers are also kept incase the |
| 5365 | * super_block handler wishes to interpret them. |
| 5366 | */ |
| 5367 | static int set_array_info(mddev_t * mddev, mdu_array_info_t *info) |
| 5368 | { |
| 5369 | |
| 5370 | if (info->raid_disks == 0) { |
| 5371 | /* just setting version number for superblock loading */ |
| 5372 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 5373 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5374 | super_types[info->major_version].name == NULL) { |
| 5375 | /* maybe try to auto-load a module? */ |
| 5376 | printk(KERN_INFO |
| 5377 | "md: superblock version %d not known\n", |
| 5378 | info->major_version); |
| 5379 | return -EINVAL; |
| 5380 | } |
| 5381 | mddev->major_version = info->major_version; |
| 5382 | mddev->minor_version = info->minor_version; |
| 5383 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5384 | mddev->persistent = !info->not_persistent; |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 5385 | /* ensure mddev_put doesn't delete this now that there |
| 5386 | * is some minimal configuration. |
| 5387 | */ |
| 5388 | mddev->ctime = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5389 | return 0; |
| 5390 | } |
| 5391 | mddev->major_version = MD_MAJOR_VERSION; |
| 5392 | mddev->minor_version = MD_MINOR_VERSION; |
| 5393 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 5394 | mddev->ctime = get_seconds(); |
| 5395 | |
| 5396 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 5397 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5398 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5399 | mddev->raid_disks = info->raid_disks; |
| 5400 | /* don't set md_minor, it is determined by which /dev/md* was |
| 5401 | * openned |
| 5402 | */ |
| 5403 | if (info->state & (1<<MD_SB_CLEAN)) |
| 5404 | mddev->recovery_cp = MaxSector; |
| 5405 | else |
| 5406 | mddev->recovery_cp = 0; |
| 5407 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5408 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5409 | |
| 5410 | mddev->layout = info->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5411 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5412 | |
| 5413 | mddev->max_disks = MD_SB_DISKS; |
| 5414 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5415 | if (mddev->persistent) |
| 5416 | mddev->flags = 0; |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5417 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5418 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5419 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
| 5420 | mddev->bitmap_info.offset = 0; |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 5421 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5422 | mddev->reshape_position = MaxSector; |
| 5423 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5424 | /* |
| 5425 | * Generate a 128 bit UUID |
| 5426 | */ |
| 5427 | get_random_bytes(mddev->uuid, 16); |
| 5428 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5429 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5430 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5431 | mddev->new_layout = mddev->layout; |
| 5432 | mddev->delta_disks = 0; |
| 5433 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5434 | return 0; |
| 5435 | } |
| 5436 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5437 | void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors) |
| 5438 | { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5439 | WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__); |
| 5440 | |
| 5441 | if (mddev->external_size) |
| 5442 | return; |
| 5443 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5444 | mddev->array_sectors = array_sectors; |
| 5445 | } |
| 5446 | EXPORT_SYMBOL(md_set_array_sectors); |
| 5447 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5448 | static int update_size(mddev_t *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5449 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5450 | mdk_rdev_t *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5451 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5452 | int fit = (num_sectors == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5453 | |
| 5454 | if (mddev->pers->resize == NULL) |
| 5455 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5456 | /* The "num_sectors" is the number of sectors of each device that |
| 5457 | * is used. This can only make sense for arrays with redundancy. |
| 5458 | * linear and raid0 always use whatever space is available. We can only |
| 5459 | * consider changing this number if no resync or reconstruction is |
| 5460 | * 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] | 5461 | * 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] | 5462 | * of each device. If num_sectors is zero, we find the largest size |
| 5463 | * that fits. |
| 5464 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5465 | */ |
| 5466 | if (mddev->sync_thread) |
| 5467 | return -EBUSY; |
NeilBrown | dba034e | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 5468 | if (mddev->bitmap) |
| 5469 | /* Sorry, cannot grow a bitmap yet, just remove it, |
| 5470 | * grow, and re-add. |
| 5471 | */ |
| 5472 | return -EBUSY; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5473 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5474 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 5475 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5476 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 5477 | num_sectors = avail; |
| 5478 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5479 | return -ENOSPC; |
| 5480 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5481 | rv = mddev->pers->resize(mddev, num_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5482 | if (!rv) |
| 5483 | revalidate_disk(mddev->gendisk); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5484 | return rv; |
| 5485 | } |
| 5486 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5487 | static int update_raid_disks(mddev_t *mddev, int raid_disks) |
| 5488 | { |
| 5489 | int rv; |
| 5490 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5491 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5492 | return -EINVAL; |
| 5493 | if (raid_disks <= 0 || |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 5494 | (mddev->max_disks && raid_disks >= mddev->max_disks)) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5495 | return -EINVAL; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5496 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5497 | return -EBUSY; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5498 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
| 5499 | |
| 5500 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5501 | return rv; |
| 5502 | } |
| 5503 | |
| 5504 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5505 | /* |
| 5506 | * update_array_info is used to change the configuration of an |
| 5507 | * on-line array. |
| 5508 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 5509 | * fields in the info are checked against the array. |
| 5510 | * Any differences that cannot be handled will cause an error. |
| 5511 | * Normally, only one change can be managed at a time. |
| 5512 | */ |
| 5513 | static int update_array_info(mddev_t *mddev, mdu_array_info_t *info) |
| 5514 | { |
| 5515 | int rv = 0; |
| 5516 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5517 | int state = 0; |
| 5518 | |
| 5519 | /* calculate expected state,ignoring low bits */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5520 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5521 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5522 | |
| 5523 | if (mddev->major_version != info->major_version || |
| 5524 | mddev->minor_version != info->minor_version || |
| 5525 | /* mddev->patch_version != info->patch_version || */ |
| 5526 | mddev->ctime != info->ctime || |
| 5527 | mddev->level != info->level || |
| 5528 | /* mddev->layout != info->layout || */ |
| 5529 | !mddev->persistent != info->not_persistent|| |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5530 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5531 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 5532 | ((state^info->state) & 0xfffffe00) |
| 5533 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5534 | return -EINVAL; |
| 5535 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5536 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 5537 | cnt++; |
| 5538 | if (mddev->raid_disks != info->raid_disks) |
| 5539 | cnt++; |
| 5540 | if (mddev->layout != info->layout) |
| 5541 | cnt++; |
| 5542 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 5543 | cnt++; |
| 5544 | if (cnt == 0) |
| 5545 | return 0; |
| 5546 | if (cnt > 1) |
| 5547 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5548 | |
| 5549 | if (mddev->layout != info->layout) { |
| 5550 | /* Change layout |
| 5551 | * we don't need to do anything at the md level, the |
| 5552 | * personality will take care of it all. |
| 5553 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5554 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5555 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5556 | else { |
| 5557 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5558 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5559 | if (rv) |
| 5560 | mddev->new_layout = mddev->layout; |
| 5561 | return rv; |
| 5562 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5563 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5564 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5565 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5566 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5567 | if (mddev->raid_disks != info->raid_disks) |
| 5568 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5569 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5570 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5571 | if (mddev->pers->quiesce == NULL) |
| 5572 | return -EINVAL; |
| 5573 | if (mddev->recovery || mddev->sync_thread) |
| 5574 | return -EBUSY; |
| 5575 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5576 | /* add the bitmap */ |
| 5577 | if (mddev->bitmap) |
| 5578 | return -EEXIST; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5579 | if (mddev->bitmap_info.default_offset == 0) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5580 | return -EINVAL; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5581 | mddev->bitmap_info.offset = |
| 5582 | mddev->bitmap_info.default_offset; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5583 | mddev->pers->quiesce(mddev, 1); |
| 5584 | rv = bitmap_create(mddev); |
| 5585 | if (rv) |
| 5586 | bitmap_destroy(mddev); |
| 5587 | mddev->pers->quiesce(mddev, 0); |
| 5588 | } else { |
| 5589 | /* remove the bitmap */ |
| 5590 | if (!mddev->bitmap) |
| 5591 | return -ENOENT; |
| 5592 | if (mddev->bitmap->file) |
| 5593 | return -EINVAL; |
| 5594 | mddev->pers->quiesce(mddev, 1); |
| 5595 | bitmap_destroy(mddev); |
| 5596 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5597 | mddev->bitmap_info.offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5598 | } |
| 5599 | } |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5600 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5601 | return rv; |
| 5602 | } |
| 5603 | |
| 5604 | static int set_disk_faulty(mddev_t *mddev, dev_t dev) |
| 5605 | { |
| 5606 | mdk_rdev_t *rdev; |
| 5607 | |
| 5608 | if (mddev->pers == NULL) |
| 5609 | return -ENODEV; |
| 5610 | |
| 5611 | rdev = find_rdev(mddev, dev); |
| 5612 | if (!rdev) |
| 5613 | return -ENODEV; |
| 5614 | |
| 5615 | md_error(mddev, rdev); |
| 5616 | return 0; |
| 5617 | } |
| 5618 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 5619 | /* |
| 5620 | * We have a problem here : there is no easy way to give a CHS |
| 5621 | * virtual geometry. We currently pretend that we have a 2 heads |
| 5622 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 5623 | * dosfs just mad... ;-) |
| 5624 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5625 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 5626 | { |
| 5627 | mddev_t *mddev = bdev->bd_disk->private_data; |
| 5628 | |
| 5629 | geo->heads = 2; |
| 5630 | geo->sectors = 4; |
NeilBrown | 49ce6ce | 2010-03-29 10:51:42 +1100 | [diff] [blame] | 5631 | geo->cylinders = mddev->array_sectors / 8; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5632 | return 0; |
| 5633 | } |
| 5634 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5635 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5636 | unsigned int cmd, unsigned long arg) |
| 5637 | { |
| 5638 | int err = 0; |
| 5639 | void __user *argp = (void __user *)arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5640 | mddev_t *mddev = NULL; |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 5641 | int ro; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5642 | |
| 5643 | if (!capable(CAP_SYS_ADMIN)) |
| 5644 | return -EACCES; |
| 5645 | |
| 5646 | /* |
| 5647 | * Commands dealing with the RAID driver but not any |
| 5648 | * particular array: |
| 5649 | */ |
| 5650 | switch (cmd) |
| 5651 | { |
| 5652 | case RAID_VERSION: |
| 5653 | err = get_version(argp); |
| 5654 | goto done; |
| 5655 | |
| 5656 | case PRINT_RAID_DEBUG: |
| 5657 | err = 0; |
| 5658 | md_print_devices(); |
| 5659 | goto done; |
| 5660 | |
| 5661 | #ifndef MODULE |
| 5662 | case RAID_AUTORUN: |
| 5663 | err = 0; |
| 5664 | autostart_arrays(arg); |
| 5665 | goto done; |
| 5666 | #endif |
| 5667 | default:; |
| 5668 | } |
| 5669 | |
| 5670 | /* |
| 5671 | * Commands creating/starting a new array: |
| 5672 | */ |
| 5673 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5674 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5675 | |
| 5676 | if (!mddev) { |
| 5677 | BUG(); |
| 5678 | goto abort; |
| 5679 | } |
| 5680 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5681 | err = mddev_lock(mddev); |
| 5682 | if (err) { |
| 5683 | printk(KERN_INFO |
| 5684 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 5685 | err, cmd); |
| 5686 | goto abort; |
| 5687 | } |
| 5688 | |
| 5689 | switch (cmd) |
| 5690 | { |
| 5691 | case SET_ARRAY_INFO: |
| 5692 | { |
| 5693 | mdu_array_info_t info; |
| 5694 | if (!arg) |
| 5695 | memset(&info, 0, sizeof(info)); |
| 5696 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 5697 | err = -EFAULT; |
| 5698 | goto abort_unlock; |
| 5699 | } |
| 5700 | if (mddev->pers) { |
| 5701 | err = update_array_info(mddev, &info); |
| 5702 | if (err) { |
| 5703 | printk(KERN_WARNING "md: couldn't update" |
| 5704 | " array info. %d\n", err); |
| 5705 | goto abort_unlock; |
| 5706 | } |
| 5707 | goto done_unlock; |
| 5708 | } |
| 5709 | if (!list_empty(&mddev->disks)) { |
| 5710 | printk(KERN_WARNING |
| 5711 | "md: array %s already has disks!\n", |
| 5712 | mdname(mddev)); |
| 5713 | err = -EBUSY; |
| 5714 | goto abort_unlock; |
| 5715 | } |
| 5716 | if (mddev->raid_disks) { |
| 5717 | printk(KERN_WARNING |
| 5718 | "md: array %s already initialised!\n", |
| 5719 | mdname(mddev)); |
| 5720 | err = -EBUSY; |
| 5721 | goto abort_unlock; |
| 5722 | } |
| 5723 | err = set_array_info(mddev, &info); |
| 5724 | if (err) { |
| 5725 | printk(KERN_WARNING "md: couldn't set" |
| 5726 | " array info. %d\n", err); |
| 5727 | goto abort_unlock; |
| 5728 | } |
| 5729 | } |
| 5730 | goto done_unlock; |
| 5731 | |
| 5732 | default:; |
| 5733 | } |
| 5734 | |
| 5735 | /* |
| 5736 | * Commands querying/configuring an existing array: |
| 5737 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5738 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5739 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 5740 | if ((!mddev->raid_disks && !mddev->external) |
| 5741 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 5742 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 5743 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5744 | err = -ENODEV; |
| 5745 | goto abort_unlock; |
| 5746 | } |
| 5747 | |
| 5748 | /* |
| 5749 | * Commands even a read-only array can execute: |
| 5750 | */ |
| 5751 | switch (cmd) |
| 5752 | { |
| 5753 | case GET_ARRAY_INFO: |
| 5754 | err = get_array_info(mddev, argp); |
| 5755 | goto done_unlock; |
| 5756 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5757 | case GET_BITMAP_FILE: |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 5758 | err = get_bitmap_file(mddev, argp); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5759 | goto done_unlock; |
| 5760 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5761 | case GET_DISK_INFO: |
| 5762 | err = get_disk_info(mddev, argp); |
| 5763 | goto done_unlock; |
| 5764 | |
| 5765 | case RESTART_ARRAY_RW: |
| 5766 | err = restart_array(mddev); |
| 5767 | goto done_unlock; |
| 5768 | |
| 5769 | case STOP_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5770 | err = do_md_stop(mddev, 0, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5771 | goto done_unlock; |
| 5772 | |
| 5773 | case STOP_ARRAY_RO: |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5774 | err = md_set_readonly(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5775 | goto done_unlock; |
| 5776 | |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 5777 | case BLKROSET: |
| 5778 | if (get_user(ro, (int __user *)(arg))) { |
| 5779 | err = -EFAULT; |
| 5780 | goto done_unlock; |
| 5781 | } |
| 5782 | err = -EINVAL; |
| 5783 | |
| 5784 | /* if the bdev is going readonly the value of mddev->ro |
| 5785 | * does not matter, no writes are coming |
| 5786 | */ |
| 5787 | if (ro) |
| 5788 | goto done_unlock; |
| 5789 | |
| 5790 | /* are we are already prepared for writes? */ |
| 5791 | if (mddev->ro != 1) |
| 5792 | goto done_unlock; |
| 5793 | |
| 5794 | /* transitioning to readauto need only happen for |
| 5795 | * arrays that call md_write_start |
| 5796 | */ |
| 5797 | if (mddev->pers) { |
| 5798 | err = restart_array(mddev); |
| 5799 | if (err == 0) { |
| 5800 | mddev->ro = 2; |
| 5801 | set_disk_ro(mddev->gendisk, 0); |
| 5802 | } |
| 5803 | } |
| 5804 | goto done_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5805 | } |
| 5806 | |
| 5807 | /* |
| 5808 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5809 | * superblock, so we do not allow them on read-only arrays. |
| 5810 | * However non-MD ioctls (e.g. get-size) will still come through |
| 5811 | * here and hit the 'default' below, so only disallow |
| 5812 | * 'md' ioctls, and switch to rw mode if started auto-readonly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5813 | */ |
Andre Noll | bb57fc6 | 2008-04-25 19:06:35 +0200 | [diff] [blame] | 5814 | if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5815 | if (mddev->ro == 2) { |
| 5816 | mddev->ro = 0; |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 5817 | sysfs_notify_dirent(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5818 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5819 | md_wakeup_thread(mddev->thread); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5820 | } else { |
| 5821 | err = -EROFS; |
| 5822 | goto abort_unlock; |
| 5823 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5824 | } |
| 5825 | |
| 5826 | switch (cmd) |
| 5827 | { |
| 5828 | case ADD_NEW_DISK: |
| 5829 | { |
| 5830 | mdu_disk_info_t info; |
| 5831 | if (copy_from_user(&info, argp, sizeof(info))) |
| 5832 | err = -EFAULT; |
| 5833 | else |
| 5834 | err = add_new_disk(mddev, &info); |
| 5835 | goto done_unlock; |
| 5836 | } |
| 5837 | |
| 5838 | case HOT_REMOVE_DISK: |
| 5839 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
| 5840 | goto done_unlock; |
| 5841 | |
| 5842 | case HOT_ADD_DISK: |
| 5843 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
| 5844 | goto done_unlock; |
| 5845 | |
| 5846 | case SET_DISK_FAULTY: |
| 5847 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
| 5848 | goto done_unlock; |
| 5849 | |
| 5850 | case RUN_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5851 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5852 | goto done_unlock; |
| 5853 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5854 | case SET_BITMAP_FILE: |
| 5855 | err = set_bitmap_file(mddev, (int)arg); |
| 5856 | goto done_unlock; |
| 5857 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5858 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5859 | err = -EINVAL; |
| 5860 | goto abort_unlock; |
| 5861 | } |
| 5862 | |
| 5863 | done_unlock: |
| 5864 | abort_unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5865 | if (mddev->hold_active == UNTIL_IOCTL && |
| 5866 | err != -EINVAL) |
| 5867 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5868 | mddev_unlock(mddev); |
| 5869 | |
| 5870 | return err; |
| 5871 | done: |
| 5872 | if (err) |
| 5873 | MD_BUG(); |
| 5874 | abort: |
| 5875 | return err; |
| 5876 | } |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 5877 | #ifdef CONFIG_COMPAT |
| 5878 | static int md_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 5879 | unsigned int cmd, unsigned long arg) |
| 5880 | { |
| 5881 | switch (cmd) { |
| 5882 | case HOT_REMOVE_DISK: |
| 5883 | case HOT_ADD_DISK: |
| 5884 | case SET_DISK_FAULTY: |
| 5885 | case SET_BITMAP_FILE: |
| 5886 | /* These take in integer arg, do not convert */ |
| 5887 | break; |
| 5888 | default: |
| 5889 | arg = (unsigned long)compat_ptr(arg); |
| 5890 | break; |
| 5891 | } |
| 5892 | |
| 5893 | return md_ioctl(bdev, mode, cmd, arg); |
| 5894 | } |
| 5895 | #endif /* CONFIG_COMPAT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5896 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5897 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5898 | { |
| 5899 | /* |
| 5900 | * Succeed if we can lock the mddev, which confirms that |
| 5901 | * it isn't being stopped right now. |
| 5902 | */ |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5903 | mddev_t *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5904 | int err; |
| 5905 | |
Arnd Bergmann | 6e9624b | 2010-08-07 18:25:34 +0200 | [diff] [blame^] | 5906 | lock_kernel(); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5907 | if (mddev->gendisk != bdev->bd_disk) { |
| 5908 | /* we are racing with mddev_put which is discarding this |
| 5909 | * bd_disk. |
| 5910 | */ |
| 5911 | mddev_put(mddev); |
| 5912 | /* Wait until bdev->bd_disk is definitely gone */ |
| 5913 | flush_scheduled_work(); |
| 5914 | /* Then retry the open from the top */ |
Arnd Bergmann | 6e9624b | 2010-08-07 18:25:34 +0200 | [diff] [blame^] | 5915 | unlock_kernel(); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5916 | return -ERESTARTSYS; |
| 5917 | } |
| 5918 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 5919 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5920 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5921 | goto out; |
| 5922 | |
| 5923 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5924 | atomic_inc(&mddev->openers); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5925 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5926 | |
NeilBrown | f3b99be | 2010-06-24 13:31:03 +1000 | [diff] [blame] | 5927 | check_disk_size_change(mddev->gendisk, bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5928 | out: |
Arnd Bergmann | 6e9624b | 2010-08-07 18:25:34 +0200 | [diff] [blame^] | 5929 | unlock_kernel(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5930 | return err; |
| 5931 | } |
| 5932 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5933 | static int md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5934 | { |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5935 | mddev_t *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5936 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 5937 | BUG_ON(!mddev); |
Arnd Bergmann | 6e9624b | 2010-08-07 18:25:34 +0200 | [diff] [blame^] | 5938 | lock_kernel(); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5939 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5940 | mddev_put(mddev); |
Arnd Bergmann | 6e9624b | 2010-08-07 18:25:34 +0200 | [diff] [blame^] | 5941 | unlock_kernel(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5942 | |
| 5943 | return 0; |
| 5944 | } |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 5945 | static const struct block_device_operations md_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5946 | { |
| 5947 | .owner = THIS_MODULE, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5948 | .open = md_open, |
| 5949 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 5950 | .ioctl = md_ioctl, |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 5951 | #ifdef CONFIG_COMPAT |
| 5952 | .compat_ioctl = md_compat_ioctl, |
| 5953 | #endif |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5954 | .getgeo = md_getgeo, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5955 | }; |
| 5956 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 5957 | static int md_thread(void * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5958 | { |
| 5959 | mdk_thread_t *thread = arg; |
| 5960 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5961 | /* |
| 5962 | * md_thread is a 'system-thread', it's priority should be very |
| 5963 | * high. We avoid resource deadlocks individually in each |
| 5964 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 5965 | * the very same RT priority as kswapd, thus we will never get |
| 5966 | * into a priority inversion deadlock. |
| 5967 | * |
| 5968 | * we definitely have to have equal or higher priority than |
| 5969 | * bdflush, otherwise bdflush will deadlock if there are too |
| 5970 | * many dirty RAID5 blocks. |
| 5971 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5972 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 5973 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5974 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5975 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 5976 | /* We need to wait INTERRUPTIBLE so that |
| 5977 | * we don't add to the load-average. |
| 5978 | * That means we need to be sure no signals are |
| 5979 | * pending |
| 5980 | */ |
| 5981 | if (signal_pending(current)) |
| 5982 | flush_signals(current); |
| 5983 | |
| 5984 | wait_event_interruptible_timeout |
| 5985 | (thread->wqueue, |
| 5986 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 5987 | || kthread_should_stop(), |
| 5988 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5989 | |
| 5990 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 5991 | |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 5992 | thread->run(thread->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5993 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5994 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5995 | return 0; |
| 5996 | } |
| 5997 | |
| 5998 | void md_wakeup_thread(mdk_thread_t *thread) |
| 5999 | { |
| 6000 | if (thread) { |
| 6001 | dprintk("md: waking up MD thread %s.\n", thread->tsk->comm); |
| 6002 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 6003 | wake_up(&thread->wqueue); |
| 6004 | } |
| 6005 | } |
| 6006 | |
| 6007 | mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev, |
| 6008 | const char *name) |
| 6009 | { |
| 6010 | mdk_thread_t *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6011 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 6012 | thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6013 | if (!thread) |
| 6014 | return NULL; |
| 6015 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6016 | init_waitqueue_head(&thread->wqueue); |
| 6017 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6018 | thread->run = run; |
| 6019 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6020 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 6021 | thread->tsk = kthread_run(md_thread, thread, |
| 6022 | "%s_%s", |
| 6023 | mdname(thread->mddev), |
| 6024 | name ?: mddev->pers->name); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6025 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6026 | kfree(thread); |
| 6027 | return NULL; |
| 6028 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6029 | return thread; |
| 6030 | } |
| 6031 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6032 | void md_unregister_thread(mdk_thread_t *thread) |
| 6033 | { |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6034 | if (!thread) |
| 6035 | return; |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 6036 | dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6037 | |
| 6038 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6039 | kfree(thread); |
| 6040 | } |
| 6041 | |
| 6042 | void md_error(mddev_t *mddev, mdk_rdev_t *rdev) |
| 6043 | { |
| 6044 | if (!mddev) { |
| 6045 | MD_BUG(); |
| 6046 | return; |
| 6047 | } |
| 6048 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6049 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6050 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6051 | |
| 6052 | if (mddev->external) |
| 6053 | set_bit(Blocked, &rdev->flags); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6054 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6055 | dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n", |
| 6056 | mdname(mddev), |
| 6057 | MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev), |
| 6058 | __builtin_return_address(0),__builtin_return_address(1), |
| 6059 | __builtin_return_address(2),__builtin_return_address(3)); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6060 | */ |
Andrew Morton | d0a0a5e | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 6061 | if (!mddev->pers) |
| 6062 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6063 | if (!mddev->pers->error_handler) |
| 6064 | return; |
| 6065 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6066 | if (mddev->degraded) |
| 6067 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | c0cc75f | 2010-03-22 10:28:51 +1100 | [diff] [blame] | 6068 | sysfs_notify_dirent(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6069 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6070 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6071 | md_wakeup_thread(mddev->thread); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 6072 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6073 | } |
| 6074 | |
| 6075 | /* seq_file implementation /proc/mdstat */ |
| 6076 | |
| 6077 | static void status_unused(struct seq_file *seq) |
| 6078 | { |
| 6079 | int i = 0; |
| 6080 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6081 | |
| 6082 | seq_printf(seq, "unused devices: "); |
| 6083 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6084 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6085 | char b[BDEVNAME_SIZE]; |
| 6086 | i++; |
| 6087 | seq_printf(seq, "%s ", |
| 6088 | bdevname(rdev->bdev,b)); |
| 6089 | } |
| 6090 | if (!i) |
| 6091 | seq_printf(seq, "<none>"); |
| 6092 | |
| 6093 | seq_printf(seq, "\n"); |
| 6094 | } |
| 6095 | |
| 6096 | |
| 6097 | static void status_resync(struct seq_file *seq, mddev_t * mddev) |
| 6098 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6099 | sector_t max_sectors, resync, res; |
| 6100 | unsigned long dt, db; |
| 6101 | sector_t rt; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6102 | int scale; |
| 6103 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6104 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6105 | resync = mddev->curr_resync - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6106 | |
| 6107 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6108 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6109 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6110 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6111 | |
| 6112 | /* |
| 6113 | * Should not happen. |
| 6114 | */ |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6115 | if (!max_sectors) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6116 | MD_BUG(); |
| 6117 | return; |
| 6118 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6119 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6120 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6121 | * u32, as those are the requirements for sector_div. |
| 6122 | * Thus 'scale' must be at least 10 |
| 6123 | */ |
| 6124 | scale = 10; |
| 6125 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6126 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6127 | scale++; |
| 6128 | } |
| 6129 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6130 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6131 | |
| 6132 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6133 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6134 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6135 | seq_printf(seq, "["); |
| 6136 | for (i = 0; i < x; i++) |
| 6137 | seq_printf(seq, "="); |
| 6138 | seq_printf(seq, ">"); |
| 6139 | for (i = 0; i < y; i++) |
| 6140 | seq_printf(seq, "."); |
| 6141 | seq_printf(seq, "] "); |
| 6142 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6143 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6144 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 6145 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6146 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 6147 | "check" : |
| 6148 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 6149 | "resync" : "recovery"))), |
| 6150 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6151 | (unsigned long long) resync/2, |
| 6152 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6153 | |
| 6154 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6155 | * dt: time from mark until now |
| 6156 | * db: blocks written from mark until now |
| 6157 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6158 | * |
| 6159 | * rt is a sector_t, so could be 32bit or 64bit. |
| 6160 | * So we divide before multiply in case it is 32bit and close |
| 6161 | * to the limit. |
| 6162 | * We scale the divisor (db) by 32 to avoid loosing precision |
| 6163 | * near the end of resync when the number of remaining sectors |
| 6164 | * is close to 'db'. |
| 6165 | * We then divide rt by 32 after multiplying by db to compensate. |
| 6166 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6167 | */ |
| 6168 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 6169 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6170 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 6171 | - mddev->resync_mark_cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6172 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6173 | rt = max_sectors - resync; /* number of remaining sectors */ |
| 6174 | sector_div(rt, db/32+1); |
| 6175 | rt *= dt; |
| 6176 | rt >>= 5; |
| 6177 | |
| 6178 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 6179 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6180 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6181 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6182 | } |
| 6183 | |
| 6184 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 6185 | { |
| 6186 | struct list_head *tmp; |
| 6187 | loff_t l = *pos; |
| 6188 | mddev_t *mddev; |
| 6189 | |
| 6190 | if (l >= 0x10000) |
| 6191 | return NULL; |
| 6192 | if (!l--) |
| 6193 | /* header */ |
| 6194 | return (void*)1; |
| 6195 | |
| 6196 | spin_lock(&all_mddevs_lock); |
| 6197 | list_for_each(tmp,&all_mddevs) |
| 6198 | if (!l--) { |
| 6199 | mddev = list_entry(tmp, mddev_t, all_mddevs); |
| 6200 | mddev_get(mddev); |
| 6201 | spin_unlock(&all_mddevs_lock); |
| 6202 | return mddev; |
| 6203 | } |
| 6204 | spin_unlock(&all_mddevs_lock); |
| 6205 | if (!l--) |
| 6206 | return (void*)2;/* tail */ |
| 6207 | return NULL; |
| 6208 | } |
| 6209 | |
| 6210 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 6211 | { |
| 6212 | struct list_head *tmp; |
| 6213 | mddev_t *next_mddev, *mddev = v; |
| 6214 | |
| 6215 | ++*pos; |
| 6216 | if (v == (void*)2) |
| 6217 | return NULL; |
| 6218 | |
| 6219 | spin_lock(&all_mddevs_lock); |
| 6220 | if (v == (void*)1) |
| 6221 | tmp = all_mddevs.next; |
| 6222 | else |
| 6223 | tmp = mddev->all_mddevs.next; |
| 6224 | if (tmp != &all_mddevs) |
| 6225 | next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs)); |
| 6226 | else { |
| 6227 | next_mddev = (void*)2; |
| 6228 | *pos = 0x10000; |
| 6229 | } |
| 6230 | spin_unlock(&all_mddevs_lock); |
| 6231 | |
| 6232 | if (v != (void*)1) |
| 6233 | mddev_put(mddev); |
| 6234 | return next_mddev; |
| 6235 | |
| 6236 | } |
| 6237 | |
| 6238 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 6239 | { |
| 6240 | mddev_t *mddev = v; |
| 6241 | |
| 6242 | if (mddev && v != (void*)1 && v != (void*)2) |
| 6243 | mddev_put(mddev); |
| 6244 | } |
| 6245 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6246 | struct mdstat_info { |
| 6247 | int event; |
| 6248 | }; |
| 6249 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6250 | static int md_seq_show(struct seq_file *seq, void *v) |
| 6251 | { |
| 6252 | mddev_t *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6253 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6254 | mdk_rdev_t *rdev; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6255 | struct mdstat_info *mi = seq->private; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6256 | struct bitmap *bitmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6257 | |
| 6258 | if (v == (void*)1) { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6259 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6260 | seq_printf(seq, "Personalities : "); |
| 6261 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6262 | list_for_each_entry(pers, &pers_list, list) |
| 6263 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6264 | |
| 6265 | spin_unlock(&pers_lock); |
| 6266 | seq_printf(seq, "\n"); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6267 | mi->event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6268 | return 0; |
| 6269 | } |
| 6270 | if (v == (void*)2) { |
| 6271 | status_unused(seq); |
| 6272 | return 0; |
| 6273 | } |
| 6274 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6275 | if (mddev_lock(mddev) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6276 | return -EINTR; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6277 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6278 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 6279 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 6280 | mddev->pers ? "" : "in"); |
| 6281 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6282 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6283 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6284 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 6285 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6286 | seq_printf(seq, " %s", mddev->pers->name); |
| 6287 | } |
| 6288 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6289 | sectors = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6290 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6291 | char b[BDEVNAME_SIZE]; |
| 6292 | seq_printf(seq, " %s[%d]", |
| 6293 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6294 | if (test_bit(WriteMostly, &rdev->flags)) |
| 6295 | seq_printf(seq, "(W)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6296 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6297 | seq_printf(seq, "(F)"); |
| 6298 | continue; |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6299 | } else if (rdev->raid_disk < 0) |
| 6300 | seq_printf(seq, "(S)"); /* spare */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6301 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6302 | } |
| 6303 | |
| 6304 | if (!list_empty(&mddev->disks)) { |
| 6305 | if (mddev->pers) |
| 6306 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 6307 | (unsigned long long) |
| 6308 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6309 | else |
| 6310 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6311 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6312 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6313 | if (mddev->persistent) { |
| 6314 | if (mddev->major_version != 0 || |
| 6315 | mddev->minor_version != 90) { |
| 6316 | seq_printf(seq," super %d.%d", |
| 6317 | mddev->major_version, |
| 6318 | mddev->minor_version); |
| 6319 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6320 | } else if (mddev->external) |
| 6321 | seq_printf(seq, " super external:%s", |
| 6322 | mddev->metadata_type); |
| 6323 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6324 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6325 | |
| 6326 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6327 | mddev->pers->status(seq, mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6328 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6329 | if (mddev->pers->sync_request) { |
| 6330 | if (mddev->curr_resync > 2) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6331 | status_resync(seq, mddev); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6332 | seq_printf(seq, "\n "); |
| 6333 | } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2) |
| 6334 | seq_printf(seq, "\tresync=DELAYED\n "); |
| 6335 | else if (mddev->recovery_cp < MaxSector) |
| 6336 | seq_printf(seq, "\tresync=PENDING\n "); |
| 6337 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6338 | } else |
| 6339 | seq_printf(seq, "\n "); |
| 6340 | |
| 6341 | if ((bitmap = mddev->bitmap)) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6342 | unsigned long chunk_kb; |
| 6343 | unsigned long flags; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6344 | spin_lock_irqsave(&bitmap->lock, flags); |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 6345 | chunk_kb = mddev->bitmap_info.chunksize >> 10; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6346 | seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], " |
| 6347 | "%lu%s chunk", |
| 6348 | bitmap->pages - bitmap->missing_pages, |
| 6349 | bitmap->pages, |
| 6350 | (bitmap->pages - bitmap->missing_pages) |
| 6351 | << (PAGE_SHIFT - 10), |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 6352 | chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize, |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6353 | chunk_kb ? "KB" : "B"); |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 6354 | if (bitmap->file) { |
| 6355 | seq_printf(seq, ", file: "); |
Jan Blunck | c32c2f6 | 2008-02-14 19:38:43 -0800 | [diff] [blame] | 6356 | seq_path(seq, &bitmap->file->f_path, " \t\n"); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6357 | } |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 6358 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6359 | seq_printf(seq, "\n"); |
| 6360 | spin_unlock_irqrestore(&bitmap->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6361 | } |
| 6362 | |
| 6363 | seq_printf(seq, "\n"); |
| 6364 | } |
| 6365 | mddev_unlock(mddev); |
| 6366 | |
| 6367 | return 0; |
| 6368 | } |
| 6369 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 6370 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6371 | .start = md_seq_start, |
| 6372 | .next = md_seq_next, |
| 6373 | .stop = md_seq_stop, |
| 6374 | .show = md_seq_show, |
| 6375 | }; |
| 6376 | |
| 6377 | static int md_seq_open(struct inode *inode, struct file *file) |
| 6378 | { |
| 6379 | int error; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6380 | struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL); |
| 6381 | if (mi == NULL) |
| 6382 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6383 | |
| 6384 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6385 | if (error) |
| 6386 | kfree(mi); |
| 6387 | else { |
| 6388 | struct seq_file *p = file->private_data; |
| 6389 | p->private = mi; |
| 6390 | mi->event = atomic_read(&md_event_count); |
| 6391 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6392 | return error; |
| 6393 | } |
| 6394 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6395 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 6396 | { |
| 6397 | struct seq_file *m = filp->private_data; |
| 6398 | struct mdstat_info *mi = m->private; |
| 6399 | int mask; |
| 6400 | |
| 6401 | poll_wait(filp, &md_event_waiters, wait); |
| 6402 | |
| 6403 | /* always allow read */ |
| 6404 | mask = POLLIN | POLLRDNORM; |
| 6405 | |
| 6406 | if (mi->event != atomic_read(&md_event_count)) |
| 6407 | mask |= POLLERR | POLLPRI; |
| 6408 | return mask; |
| 6409 | } |
| 6410 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 6411 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 6412 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6413 | .open = md_seq_open, |
| 6414 | .read = seq_read, |
| 6415 | .llseek = seq_lseek, |
Martin Peschke | c3f94b40 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 6416 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6417 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6418 | }; |
| 6419 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6420 | int register_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6421 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6422 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6423 | list_add_tail(&p->list, &pers_list); |
| 6424 | 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] | 6425 | spin_unlock(&pers_lock); |
| 6426 | return 0; |
| 6427 | } |
| 6428 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6429 | int unregister_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6430 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6431 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6432 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6433 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6434 | spin_unlock(&pers_lock); |
| 6435 | return 0; |
| 6436 | } |
| 6437 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6438 | static int is_mddev_idle(mddev_t *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6439 | { |
| 6440 | mdk_rdev_t * rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6441 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6442 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6443 | |
| 6444 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6445 | rcu_read_lock(); |
| 6446 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6447 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6448 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
| 6449 | (int)part_stat_read(&disk->part0, sectors[1]) - |
| 6450 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 6451 | /* sync IO will cause sync_io to increase before the disk_stats |
| 6452 | * as sync_io is counted when a request starts, and |
| 6453 | * disk_stats is counted when it completes. |
| 6454 | * So resync activity will cause curr_events to be smaller than |
| 6455 | * when there was no such activity. |
| 6456 | * non-sync IO will cause disk_stat to increase without |
| 6457 | * increasing sync_io so curr_events will (eventually) |
| 6458 | * be larger than it was before. Once it becomes |
| 6459 | * substantially larger, the test below will cause |
| 6460 | * the array to appear non-idle, and resync will slow |
| 6461 | * down. |
| 6462 | * If there is a lot of outstanding resync activity when |
| 6463 | * we set last_event to curr_events, then all that activity |
| 6464 | * completing might cause the array to appear non-idle |
| 6465 | * and resync will be slowed down even though there might |
| 6466 | * not have been non-resync activity. This will only |
| 6467 | * happen once though. 'last_events' will soon reflect |
| 6468 | * the state where there is little or no outstanding |
| 6469 | * resync requests, and further resync activity will |
| 6470 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6471 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6472 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6473 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6474 | rdev->last_events = curr_events; |
| 6475 | idle = 0; |
| 6476 | } |
| 6477 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6478 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6479 | return idle; |
| 6480 | } |
| 6481 | |
| 6482 | void md_done_sync(mddev_t *mddev, int blocks, int ok) |
| 6483 | { |
| 6484 | /* another "blocks" (512byte) blocks have been synced */ |
| 6485 | atomic_sub(blocks, &mddev->recovery_active); |
| 6486 | wake_up(&mddev->recovery_wait); |
| 6487 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6488 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6489 | md_wakeup_thread(mddev->thread); |
| 6490 | // stop recovery, signal do_sync .... |
| 6491 | } |
| 6492 | } |
| 6493 | |
| 6494 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6495 | /* md_write_start(mddev, bi) |
| 6496 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6497 | * in superblock) before writing, schedule a superblock update |
| 6498 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6499 | */ |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6500 | void md_write_start(mddev_t *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6501 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6502 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6503 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6504 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6505 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6506 | BUG_ON(mddev->ro == 1); |
| 6507 | if (mddev->ro == 2) { |
| 6508 | /* need to switch to read/write */ |
| 6509 | mddev->ro = 0; |
| 6510 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6511 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 6512 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6513 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6514 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6515 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6516 | if (mddev->safemode == 1) |
| 6517 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6518 | if (mddev->in_sync) { |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6519 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6520 | if (mddev->in_sync) { |
| 6521 | mddev->in_sync = 0; |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6522 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6523 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6524 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6525 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6526 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6527 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6528 | if (did_change) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 6529 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 6530 | wait_event(mddev->sb_wait, |
| 6531 | !test_bit(MD_CHANGE_CLEAN, &mddev->flags) && |
| 6532 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6533 | } |
| 6534 | |
| 6535 | void md_write_end(mddev_t *mddev) |
| 6536 | { |
| 6537 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 6538 | if (mddev->safemode == 2) |
| 6539 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 6540 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6541 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 6542 | } |
| 6543 | } |
| 6544 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6545 | /* md_allow_write(mddev) |
| 6546 | * Calling this ensures that the array is marked 'active' so that writes |
| 6547 | * may proceed without blocking. It is important to call this before |
| 6548 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 6549 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6550 | * |
| 6551 | * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock |
| 6552 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6553 | */ |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6554 | int md_allow_write(mddev_t *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6555 | { |
| 6556 | if (!mddev->pers) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6557 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6558 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6559 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 6560 | if (!mddev->pers->sync_request) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6561 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6562 | |
| 6563 | spin_lock_irq(&mddev->write_lock); |
| 6564 | if (mddev->in_sync) { |
| 6565 | mddev->in_sync = 0; |
| 6566 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 6567 | if (mddev->safemode_delay && |
| 6568 | mddev->safemode == 0) |
| 6569 | mddev->safemode = 1; |
| 6570 | spin_unlock_irq(&mddev->write_lock); |
| 6571 | md_update_sb(mddev, 0); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 6572 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6573 | } else |
| 6574 | spin_unlock_irq(&mddev->write_lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6575 | |
| 6576 | if (test_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 6577 | return -EAGAIN; |
| 6578 | else |
| 6579 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6580 | } |
| 6581 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 6582 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6583 | #define SYNC_MARKS 10 |
| 6584 | #define SYNC_MARK_STEP (3*HZ) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6585 | void md_do_sync(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6586 | { |
| 6587 | mddev_t *mddev2; |
| 6588 | unsigned int currspeed = 0, |
| 6589 | window; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6590 | sector_t max_sectors,j, io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6591 | unsigned long mark[SYNC_MARKS]; |
| 6592 | sector_t mark_cnt[SYNC_MARKS]; |
| 6593 | int last_mark,m; |
| 6594 | struct list_head *tmp; |
| 6595 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6596 | int skipped = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6597 | mdk_rdev_t *rdev; |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6598 | char *desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6599 | |
| 6600 | /* just incase thread restarts... */ |
| 6601 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 6602 | return; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6603 | if (mddev->ro) /* never try to sync a read-only array */ |
| 6604 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6605 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6606 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6607 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 6608 | desc = "data-check"; |
| 6609 | else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6610 | desc = "requested-resync"; |
| 6611 | else |
| 6612 | desc = "resync"; |
| 6613 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 6614 | desc = "reshape"; |
| 6615 | else |
| 6616 | desc = "recovery"; |
| 6617 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6618 | /* we overload curr_resync somewhat here. |
| 6619 | * 0 == not engaged in resync at all |
| 6620 | * 2 == checking that there is no conflict with another sync |
| 6621 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 6622 | * commense |
| 6623 | * other == active in resync - this many blocks |
| 6624 | * |
| 6625 | * Before starting a resync we must have set curr_resync to |
| 6626 | * 2, and then checked that every "conflicting" array has curr_resync |
| 6627 | * less than ours. When we find one that is the same or higher |
| 6628 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 6629 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 6630 | * This will mean we have to start checking from the beginning again. |
| 6631 | * |
| 6632 | */ |
| 6633 | |
| 6634 | do { |
| 6635 | mddev->curr_resync = 2; |
| 6636 | |
| 6637 | try_again: |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 6638 | if (kthread_should_stop()) |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 6639 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 6640 | |
| 6641 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6642 | goto skip; |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6643 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6644 | if (mddev2 == mddev) |
| 6645 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 6646 | if (!mddev->parallel_resync |
| 6647 | && mddev2->curr_resync |
| 6648 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6649 | DEFINE_WAIT(wq); |
| 6650 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 6651 | /* arbitrarily yield */ |
| 6652 | mddev->curr_resync = 1; |
| 6653 | wake_up(&resync_wait); |
| 6654 | } |
| 6655 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 6656 | /* no need to wait here, we can wait the next |
| 6657 | * time 'round when curr_resync == 2 |
| 6658 | */ |
| 6659 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 6660 | /* We need to wait 'interruptible' so as not to |
| 6661 | * contribute to the load average, and not to |
| 6662 | * be caught by 'softlockup' |
| 6663 | */ |
| 6664 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 6665 | if (!kthread_should_stop() && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 6666 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6667 | printk(KERN_INFO "md: delaying %s of %s" |
| 6668 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6669 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6670 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6671 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 6672 | if (signal_pending(current)) |
| 6673 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6674 | schedule(); |
| 6675 | finish_wait(&resync_wait, &wq); |
| 6676 | goto try_again; |
| 6677 | } |
| 6678 | finish_wait(&resync_wait, &wq); |
| 6679 | } |
| 6680 | } |
| 6681 | } while (mddev->curr_resync < 2); |
| 6682 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6683 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6684 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6685 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6686 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6687 | */ |
| 6688 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6689 | mddev->resync_mismatches = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6690 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6691 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6692 | j = mddev->resync_min; |
| 6693 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6694 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6695 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6696 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6697 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6698 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6699 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6700 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6701 | j = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6702 | rcu_read_lock(); |
| 6703 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6704 | if (rdev->raid_disk >= 0 && |
| 6705 | !test_bit(Faulty, &rdev->flags) && |
| 6706 | !test_bit(In_sync, &rdev->flags) && |
| 6707 | rdev->recovery_offset < j) |
| 6708 | j = rdev->recovery_offset; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6709 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6710 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6711 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6712 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 6713 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 6714 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 6715 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6716 | "(but not more than %d KB/sec) for %s.\n", |
| 6717 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6718 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6719 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6720 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6721 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6722 | for (m = 0; m < SYNC_MARKS; m++) { |
| 6723 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6724 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6725 | } |
| 6726 | last_mark = 0; |
| 6727 | mddev->resync_mark = mark[last_mark]; |
| 6728 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 6729 | |
| 6730 | /* |
| 6731 | * Tune reconstruction: |
| 6732 | */ |
| 6733 | window = 32*(PAGE_SIZE/512); |
| 6734 | printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n", |
| 6735 | window/2,(unsigned long long) max_sectors/2); |
| 6736 | |
| 6737 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6738 | last_check = 0; |
| 6739 | |
| 6740 | if (j>2) { |
| 6741 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6742 | "md: resuming %s of %s from checkpoint.\n", |
| 6743 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6744 | mddev->curr_resync = j; |
| 6745 | } |
NeilBrown | efa5933 | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 6746 | mddev->curr_resync_completed = mddev->curr_resync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6747 | |
| 6748 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6749 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6750 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6751 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6752 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 6753 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 6754 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 6755 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 6756 | > (max_sectors >> 4)) || |
| 6757 | (j - mddev->curr_resync_completed)*2 |
| 6758 | >= mddev->resync_max - mddev->curr_resync_completed |
| 6759 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6760 | /* time to update curr_resync_completed */ |
| 6761 | blk_unplug(mddev->queue); |
| 6762 | wait_event(mddev->recovery_wait, |
| 6763 | atomic_read(&mddev->recovery_active) == 0); |
| 6764 | mddev->curr_resync_completed = |
| 6765 | mddev->curr_resync; |
| 6766 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6767 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6768 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6769 | |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 6770 | while (j >= mddev->resync_max && !kthread_should_stop()) { |
| 6771 | /* As this condition is controlled by user-space, |
| 6772 | * we can block indefinitely, so use '_interruptible' |
| 6773 | * to avoid triggering warnings. |
| 6774 | */ |
| 6775 | flush_signals(current); /* just in case */ |
| 6776 | wait_event_interruptible(mddev->recovery_wait, |
| 6777 | mddev->resync_max > j |
| 6778 | || kthread_should_stop()); |
| 6779 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6780 | |
| 6781 | if (kthread_should_stop()) |
| 6782 | goto interrupted; |
| 6783 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6784 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6785 | currspeed < speed_min(mddev)); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6786 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6787 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6788 | goto out; |
| 6789 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6790 | |
| 6791 | if (!skipped) { /* actual IO requested */ |
| 6792 | io_sectors += sectors; |
| 6793 | atomic_add(sectors, &mddev->recovery_active); |
| 6794 | } |
| 6795 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6796 | j += sectors; |
| 6797 | if (j>1) mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6798 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6799 | if (last_check == 0) |
| 6800 | /* this is the earliers that rebuilt will be |
| 6801 | * visible in /proc/mdstat |
| 6802 | */ |
| 6803 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6804 | |
| 6805 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6806 | continue; |
| 6807 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6808 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6809 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6810 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6811 | break; |
| 6812 | |
| 6813 | repeat: |
| 6814 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 6815 | /* step marks */ |
| 6816 | int next = (last_mark+1) % SYNC_MARKS; |
| 6817 | |
| 6818 | mddev->resync_mark = mark[next]; |
| 6819 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 6820 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6821 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6822 | last_mark = next; |
| 6823 | } |
| 6824 | |
| 6825 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6826 | if (kthread_should_stop()) |
| 6827 | goto interrupted; |
| 6828 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6829 | |
| 6830 | /* |
| 6831 | * this loop exits only if either when we are slower than |
| 6832 | * the 'hard' speed limit, or the system was IO-idle for |
| 6833 | * a jiffy. |
| 6834 | * the system might be non-idle CPU-wise, but we only care |
| 6835 | * about not overloading the IO subsystem. (things like an |
| 6836 | * e2fsck being done on the RAID array should execute fast) |
| 6837 | */ |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 6838 | blk_unplug(mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6839 | cond_resched(); |
| 6840 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6841 | currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2 |
| 6842 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6843 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 6844 | if (currspeed > speed_min(mddev)) { |
| 6845 | if ((currspeed > speed_max(mddev)) || |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6846 | !is_mddev_idle(mddev, 0)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6847 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6848 | goto repeat; |
| 6849 | } |
| 6850 | } |
| 6851 | } |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6852 | printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6853 | /* |
| 6854 | * this also signals 'finished resyncing' to md_stop |
| 6855 | */ |
| 6856 | out: |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 6857 | blk_unplug(mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6858 | |
| 6859 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 6860 | |
| 6861 | /* tell personality that we are finished */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6862 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6863 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6864 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6865 | mddev->curr_resync > 2) { |
| 6866 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6867 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 6868 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 6869 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6870 | "md: checkpointing %s of %s.\n", |
| 6871 | desc, mdname(mddev)); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6872 | mddev->recovery_cp = mddev->curr_resync; |
| 6873 | } |
| 6874 | } else |
| 6875 | mddev->recovery_cp = MaxSector; |
| 6876 | } else { |
| 6877 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 6878 | mddev->curr_resync = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6879 | rcu_read_lock(); |
| 6880 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6881 | if (rdev->raid_disk >= 0 && |
NeilBrown | 70fffd0 | 2010-06-16 17:01:25 +1000 | [diff] [blame] | 6882 | mddev->delta_disks >= 0 && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6883 | !test_bit(Faulty, &rdev->flags) && |
| 6884 | !test_bit(In_sync, &rdev->flags) && |
| 6885 | rdev->recovery_offset < mddev->curr_resync) |
| 6886 | rdev->recovery_offset = mddev->curr_resync; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6887 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6888 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6889 | } |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 6890 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6891 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6892 | skip: |
NeilBrown | c07b70a | 2009-12-14 12:49:48 +1100 | [diff] [blame] | 6893 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 6894 | /* We completed so min/max setting can be forgotten if used. */ |
| 6895 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6896 | mddev->resync_min = 0; |
| 6897 | mddev->resync_max = MaxSector; |
| 6898 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6899 | mddev->resync_min = mddev->curr_resync_completed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6900 | mddev->curr_resync = 0; |
NeilBrown | efa5933 | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 6901 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 6902 | mddev->curr_resync_completed = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6903 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6904 | wake_up(&resync_wait); |
| 6905 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
| 6906 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6907 | return; |
| 6908 | |
| 6909 | interrupted: |
| 6910 | /* |
| 6911 | * got a signal, exit. |
| 6912 | */ |
| 6913 | printk(KERN_INFO |
| 6914 | "md: md_do_sync() got signal ... exiting\n"); |
| 6915 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6916 | goto out; |
| 6917 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6918 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6919 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6920 | |
| 6921 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6922 | static int remove_and_add_spares(mddev_t *mddev) |
| 6923 | { |
| 6924 | mdk_rdev_t *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6925 | int spares = 0; |
| 6926 | |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6927 | mddev->curr_resync_completed = 0; |
| 6928 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6929 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6930 | if (rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6931 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6932 | (test_bit(Faulty, &rdev->flags) || |
| 6933 | ! test_bit(In_sync, &rdev->flags)) && |
| 6934 | atomic_read(&rdev->nr_pending)==0) { |
| 6935 | if (mddev->pers->hot_remove_disk( |
| 6936 | mddev, rdev->raid_disk)==0) { |
| 6937 | char nm[20]; |
| 6938 | sprintf(nm,"rd%d", rdev->raid_disk); |
| 6939 | sysfs_remove_link(&mddev->kobj, nm); |
| 6940 | rdev->raid_disk = -1; |
| 6941 | } |
| 6942 | } |
| 6943 | |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 6944 | if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6945 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6946 | if (rdev->raid_disk >= 0 && |
Dan Williams | e542713 | 2008-07-28 23:28:06 -0700 | [diff] [blame] | 6947 | !test_bit(In_sync, &rdev->flags) && |
| 6948 | !test_bit(Blocked, &rdev->flags)) |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6949 | spares++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6950 | if (rdev->raid_disk < 0 |
| 6951 | && !test_bit(Faulty, &rdev->flags)) { |
| 6952 | rdev->recovery_offset = 0; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 6953 | if (mddev->pers-> |
| 6954 | hot_add_disk(mddev, rdev) == 0) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6955 | char nm[20]; |
| 6956 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 6957 | if (sysfs_create_link(&mddev->kobj, |
| 6958 | &rdev->kobj, nm)) |
| 6959 | printk(KERN_WARNING |
| 6960 | "md: cannot register " |
| 6961 | "%s for %s\n", |
| 6962 | nm, mdname(mddev)); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6963 | spares++; |
| 6964 | md_new_event(mddev); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 6965 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6966 | } else |
| 6967 | break; |
| 6968 | } |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6969 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6970 | } |
| 6971 | return spares; |
| 6972 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6973 | /* |
| 6974 | * This routine is regularly called by all per-raid-array threads to |
| 6975 | * deal with generic issues like resync and super-block update. |
| 6976 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 6977 | * need this as they never do any recovery or update the superblock. |
| 6978 | * |
| 6979 | * It does not do any resync itself, but rather "forks" off other threads |
| 6980 | * to do that as needed. |
| 6981 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 6982 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6983 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6984 | * and wakeups up this thread which will reap the thread and finish up. |
| 6985 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 6986 | * |
| 6987 | * The overall approach is: |
| 6988 | * 1/ if the superblock needs updating, update it. |
| 6989 | * 2/ If a recovery thread is running, don't do anything else. |
| 6990 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 6991 | * 4/ If there are any faulty devices, remove them. |
| 6992 | * 5/ If array is degraded, try to add spares devices |
| 6993 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 6994 | */ |
| 6995 | void md_check_recovery(mddev_t *mddev) |
| 6996 | { |
| 6997 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6998 | |
| 6999 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 7000 | if (mddev->bitmap) |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 7001 | bitmap_daemon_work(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7002 | |
| 7003 | if (mddev->ro) |
| 7004 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7005 | |
| 7006 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7007 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7008 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 7009 | mdname(mddev)); |
| 7010 | mddev->safemode = 2; |
| 7011 | } |
| 7012 | flush_signals(current); |
| 7013 | } |
| 7014 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7015 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 7016 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7017 | if ( ! ( |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 7018 | (mddev->flags && !mddev->external) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7019 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7020 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7021 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7022 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 7023 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7024 | )) |
| 7025 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7026 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 7027 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7028 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7029 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7030 | if (mddev->ro) { |
| 7031 | /* Only thing we do on a ro array is remove |
| 7032 | * failed devices. |
| 7033 | */ |
| 7034 | remove_and_add_spares(mddev); |
| 7035 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7036 | goto unlock; |
| 7037 | } |
| 7038 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7039 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7040 | int did_change = 0; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7041 | spin_lock_irq(&mddev->write_lock); |
| 7042 | if (mddev->safemode && |
| 7043 | !atomic_read(&mddev->writes_pending) && |
| 7044 | !mddev->in_sync && |
| 7045 | mddev->recovery_cp == MaxSector) { |
| 7046 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7047 | did_change = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7048 | if (mddev->persistent) |
| 7049 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 7050 | } |
| 7051 | if (mddev->safemode == 1) |
| 7052 | mddev->safemode = 0; |
| 7053 | spin_unlock_irq(&mddev->write_lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7054 | if (did_change) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 7055 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7056 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7057 | |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7058 | if (mddev->flags) |
| 7059 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7060 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7061 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 7062 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 7063 | /* resync/recovery still happening */ |
| 7064 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7065 | goto unlock; |
| 7066 | } |
| 7067 | if (mddev->sync_thread) { |
| 7068 | /* resync has finished, collect result */ |
| 7069 | md_unregister_thread(mddev->sync_thread); |
| 7070 | mddev->sync_thread = NULL; |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 7071 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 7072 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7073 | /* success...*/ |
| 7074 | /* activate any spares */ |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 7075 | if (mddev->pers->spare_active(mddev)) |
| 7076 | sysfs_notify(&mddev->kobj, NULL, |
| 7077 | "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7078 | } |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 7079 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 7080 | mddev->pers->finish_reshape) |
| 7081 | mddev->pers->finish_reshape(mddev); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7082 | md_update_sb(mddev, 1); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 7083 | |
| 7084 | /* if array is no-longer degraded, then any saved_raid_disk |
| 7085 | * information must be scrapped |
| 7086 | */ |
| 7087 | if (!mddev->degraded) |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 7088 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 7089 | rdev->saved_raid_disk = -1; |
| 7090 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7091 | mddev->recovery = 0; |
| 7092 | /* flag recovery needed just to double check */ |
| 7093 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 7094 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7095 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7096 | goto unlock; |
| 7097 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7098 | /* Set RUNNING before clearing NEEDED to avoid |
| 7099 | * any transients in the value of "sync_action". |
| 7100 | */ |
| 7101 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7102 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7103 | /* Clear some bits that don't mean anything, but |
| 7104 | * might be left set |
| 7105 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7106 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7107 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7108 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7109 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 7110 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7111 | /* no recovery is running. |
| 7112 | * remove any failed drives, then |
| 7113 | * add spares if possible. |
| 7114 | * Spare are also removed and re-added, to allow |
| 7115 | * the personality to fail the re-add. |
| 7116 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7117 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7118 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 7119 | if (mddev->pers->check_reshape == NULL || |
| 7120 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7121 | /* Cannot proceed */ |
| 7122 | goto unlock; |
| 7123 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7124 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7125 | } else if ((spares = remove_and_add_spares(mddev))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7126 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7127 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 7128 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7129 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7130 | } else if (mddev->recovery_cp < MaxSector) { |
| 7131 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7132 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7133 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 7134 | /* nothing to be done ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7135 | goto unlock; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7136 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7137 | if (mddev->pers->sync_request) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 7138 | if (spares && mddev->bitmap && ! mddev->bitmap->file) { |
| 7139 | /* We are adding a device or devices to an array |
| 7140 | * which has the bitmap stored on all devices. |
| 7141 | * So make sure all bitmap pages get written |
| 7142 | */ |
| 7143 | bitmap_write_all(mddev->bitmap); |
| 7144 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7145 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 7146 | mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 7147 | "resync"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7148 | if (!mddev->sync_thread) { |
| 7149 | printk(KERN_ERR "%s: could not start resync" |
| 7150 | " thread...\n", |
| 7151 | mdname(mddev)); |
| 7152 | /* leave the spares where they are, it shouldn't hurt */ |
| 7153 | mddev->recovery = 0; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7154 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7155 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 7156 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7157 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7158 | } |
| 7159 | unlock: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7160 | if (!mddev->sync_thread) { |
| 7161 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7162 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 7163 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 7164 | if (mddev->sysfs_action) |
| 7165 | sysfs_notify_dirent(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7166 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7167 | mddev_unlock(mddev); |
| 7168 | } |
| 7169 | } |
| 7170 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7171 | void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 7172 | { |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 7173 | sysfs_notify_dirent(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7174 | wait_event_timeout(rdev->blocked_wait, |
| 7175 | !test_bit(Blocked, &rdev->flags), |
| 7176 | msecs_to_jiffies(5000)); |
| 7177 | rdev_dec_pending(rdev, mddev); |
| 7178 | } |
| 7179 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 7180 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7181 | static int md_notify_reboot(struct notifier_block *this, |
| 7182 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7183 | { |
| 7184 | struct list_head *tmp; |
| 7185 | mddev_t *mddev; |
| 7186 | |
| 7187 | if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) { |
| 7188 | |
| 7189 | printk(KERN_INFO "md: stopping all md devices.\n"); |
| 7190 | |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7191 | for_each_mddev(mddev, tmp) |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 7192 | if (mddev_trylock(mddev)) { |
NeilBrown | 2b25000 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7193 | /* Force a switch to readonly even array |
| 7194 | * appears to still be in use. Hence |
| 7195 | * the '100'. |
| 7196 | */ |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 7197 | md_set_readonly(mddev, 100); |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 7198 | mddev_unlock(mddev); |
| 7199 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7200 | /* |
| 7201 | * certain more exotic SCSI devices are known to be |
| 7202 | * volatile wrt too early system reboots. While the |
| 7203 | * right place to handle this issue is the given |
| 7204 | * driver, we do want to have a safe RAID driver ... |
| 7205 | */ |
| 7206 | mdelay(1000*1); |
| 7207 | } |
| 7208 | return NOTIFY_DONE; |
| 7209 | } |
| 7210 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7211 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7212 | .notifier_call = md_notify_reboot, |
| 7213 | .next = NULL, |
| 7214 | .priority = INT_MAX, /* before any real devices */ |
| 7215 | }; |
| 7216 | |
| 7217 | static void md_geninit(void) |
| 7218 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7219 | dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t)); |
| 7220 | |
Denis V. Lunev | c7705f344 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 7221 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7222 | } |
| 7223 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7224 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7225 | { |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7226 | if (register_blkdev(MD_MAJOR, "md")) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7227 | return -1; |
| 7228 | if ((mdp_major=register_blkdev(0, "mdp"))<=0) { |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7229 | unregister_blkdev(MD_MAJOR, "md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7230 | return -1; |
| 7231 | } |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7232 | blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE, |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 7233 | md_probe, NULL, NULL); |
| 7234 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7235 | md_probe, NULL, NULL); |
| 7236 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7237 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 7238 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7239 | |
| 7240 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 7241 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7242 | } |
| 7243 | |
| 7244 | |
| 7245 | #ifndef MODULE |
| 7246 | |
| 7247 | /* |
| 7248 | * Searches all registered partitions for autorun RAID arrays |
| 7249 | * at boot time. |
| 7250 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7251 | |
| 7252 | static LIST_HEAD(all_detected_devices); |
| 7253 | struct detected_devices_node { |
| 7254 | struct list_head list; |
| 7255 | dev_t dev; |
| 7256 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7257 | |
| 7258 | void md_autodetect_dev(dev_t dev) |
| 7259 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7260 | struct detected_devices_node *node_detected_dev; |
| 7261 | |
| 7262 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 7263 | if (node_detected_dev) { |
| 7264 | node_detected_dev->dev = dev; |
| 7265 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 7266 | } else { |
| 7267 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 7268 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 7269 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7270 | } |
| 7271 | |
| 7272 | |
| 7273 | static void autostart_arrays(int part) |
| 7274 | { |
| 7275 | mdk_rdev_t *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7276 | struct detected_devices_node *node_detected_dev; |
| 7277 | dev_t dev; |
| 7278 | int i_scanned, i_passed; |
| 7279 | |
| 7280 | i_scanned = 0; |
| 7281 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7282 | |
| 7283 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 7284 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7285 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 7286 | i_scanned++; |
| 7287 | node_detected_dev = list_entry(all_detected_devices.next, |
| 7288 | struct detected_devices_node, list); |
| 7289 | list_del(&node_detected_dev->list); |
| 7290 | dev = node_detected_dev->dev; |
| 7291 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 7292 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7293 | if (IS_ERR(rdev)) |
| 7294 | continue; |
| 7295 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7296 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7297 | MD_BUG(); |
| 7298 | continue; |
| 7299 | } |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 7300 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7301 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7302 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7303 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7304 | |
| 7305 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 7306 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7307 | |
| 7308 | autorun_devices(part); |
| 7309 | } |
| 7310 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 7311 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7312 | |
| 7313 | static __exit void md_exit(void) |
| 7314 | { |
| 7315 | mddev_t *mddev; |
| 7316 | struct list_head *tmp; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 7317 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7318 | blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS); |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 7319 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7320 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7321 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7322 | unregister_blkdev(mdp_major, "mdp"); |
| 7323 | unregister_reboot_notifier(&md_notifier); |
| 7324 | unregister_sysctl_table(raid_table_header); |
| 7325 | remove_proc_entry("mdstat", NULL); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7326 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7327 | export_array(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7328 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7329 | } |
| 7330 | } |
| 7331 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 7332 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7333 | module_exit(md_exit) |
| 7334 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7335 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 7336 | { |
| 7337 | return sprintf(buffer, "%d", start_readonly); |
| 7338 | } |
| 7339 | static int set_ro(const char *val, struct kernel_param *kp) |
| 7340 | { |
| 7341 | char *e; |
| 7342 | int num = simple_strtoul(val, &e, 10); |
| 7343 | if (*val && (*e == '\0' || *e == '\n')) { |
| 7344 | start_readonly = num; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 7345 | return 0; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7346 | } |
| 7347 | return -EINVAL; |
| 7348 | } |
| 7349 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 7350 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 7351 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 7352 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7353 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7354 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7355 | EXPORT_SYMBOL(register_md_personality); |
| 7356 | EXPORT_SYMBOL(unregister_md_personality); |
| 7357 | EXPORT_SYMBOL(md_error); |
| 7358 | EXPORT_SYMBOL(md_done_sync); |
| 7359 | EXPORT_SYMBOL(md_write_start); |
| 7360 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7361 | EXPORT_SYMBOL(md_register_thread); |
| 7362 | EXPORT_SYMBOL(md_unregister_thread); |
| 7363 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7364 | EXPORT_SYMBOL(md_check_recovery); |
| 7365 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 7366 | MODULE_DESCRIPTION("MD RAID framework"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 7367 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 7368 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |