Thomas Gleixner | af1a889 | 2019-05-20 19:08:12 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | md.c : Multiple Devices driver for Linux |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 4 | Copyright (C) 1998, 1999, 2000 Ingo Molnar |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | |
| 6 | completely rewritten, based on the MD driver code from Marc Zyngier |
| 7 | |
| 8 | Changes: |
| 9 | |
| 10 | - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar |
| 11 | - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com> |
| 12 | - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net> |
| 13 | - kerneld support by Boris Tobotras <boris@xtalk.msk.su> |
| 14 | - kmod support by: Cyrus Durgin |
| 15 | - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com> |
| 16 | - Devfs support by Richard Gooch <rgooch@atnf.csiro.au> |
| 17 | |
| 18 | - lots of fixes and improvements to the RAID1/RAID5 and generic |
| 19 | RAID code (such as request based resynchronization): |
| 20 | |
| 21 | Neil Brown <neilb@cse.unsw.edu.au>. |
| 22 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 23 | - persistent bitmap code |
| 24 | Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc. |
| 25 | |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 26 | |
| 27 | Errors, Warnings, etc. |
| 28 | Please use: |
| 29 | pr_crit() for error conditions that risk data loss |
| 30 | pr_err() for error conditions that are unexpected, like an IO error |
| 31 | or internal inconsistency |
| 32 | pr_warn() for error conditions that could have been predicated, like |
| 33 | adding a device to an array when it has incompatible metadata |
| 34 | pr_info() for every interesting, very rare events, like an array starting |
| 35 | or stopping, or resync starting or stopping |
| 36 | pr_debug() for everything else. |
| 37 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | */ |
| 39 | |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 40 | #include <linux/sched/mm.h> |
Ingo Molnar | 3f07c01 | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 41 | #include <linux/sched/signal.h> |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 42 | #include <linux/kthread.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 43 | #include <linux/blkdev.h> |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 44 | #include <linux/badblocks.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include <linux/sysctl.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 46 | #include <linux/seq_file.h> |
Al Viro | ff01bb4 | 2011-09-16 02:31:11 -0400 | [diff] [blame] | 47 | #include <linux/fs.h> |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 48 | #include <linux/poll.h> |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 49 | #include <linux/ctype.h> |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 50 | #include <linux/string.h> |
NeilBrown | fb4d8c7 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 51 | #include <linux/hdreg.h> |
| 52 | #include <linux/proc_fs.h> |
| 53 | #include <linux/random.h> |
Paul Gortmaker | 056075c | 2011-07-03 13:58:33 -0400 | [diff] [blame] | 54 | #include <linux/module.h> |
NeilBrown | fb4d8c7 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 55 | #include <linux/reboot.h> |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 56 | #include <linux/file.h> |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 57 | #include <linux/compat.h> |
Stephen Rothwell | 2557072 | 2008-10-15 09:09:21 +1100 | [diff] [blame] | 58 | #include <linux/delay.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 59 | #include <linux/raid/md_p.h> |
| 60 | #include <linux/raid/md_u.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 61 | #include <linux/slab.h> |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 62 | #include <linux/percpu-refcount.h> |
| 63 | |
Shaohua Li | 504634f | 2016-11-18 09:44:08 -0800 | [diff] [blame] | 64 | #include <trace/events/block.h> |
NeilBrown | 43b2e5d | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 65 | #include "md.h" |
Mike Snitzer | 935fe09 | 2017-10-10 17:02:41 -0400 | [diff] [blame] | 66 | #include "md-bitmap.h" |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 67 | #include "md-cluster.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | #ifndef MODULE |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 70 | static void autostart_arrays(int part); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | #endif |
| 72 | |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 73 | /* pers_list is a list of registered personalities protected |
| 74 | * by pers_lock. |
| 75 | * pers_lock does extra service to protect accesses to |
| 76 | * mddev->thread when the mutex cannot be held. |
| 77 | */ |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 78 | static LIST_HEAD(pers_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | static DEFINE_SPINLOCK(pers_lock); |
| 80 | |
Kent Overstreet | 28dec87 | 2018-06-07 20:52:54 -0400 | [diff] [blame] | 81 | static struct kobj_type md_ktype; |
| 82 | |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 83 | struct md_cluster_operations *md_cluster_ops; |
Goldwyn Rodrigues | 589a1c4 | 2014-06-07 02:39:37 -0500 | [diff] [blame] | 84 | EXPORT_SYMBOL(md_cluster_ops); |
Christoph Hellwig | 2b598ee | 2019-04-04 18:56:14 +0200 | [diff] [blame] | 85 | static struct module *md_cluster_mod; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 86 | |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 87 | static DECLARE_WAIT_QUEUE_HEAD(resync_wait); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 88 | static struct workqueue_struct *md_wq; |
| 89 | static struct workqueue_struct *md_misc_wq; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 90 | |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 91 | static int remove_and_add_spares(struct mddev *mddev, |
| 92 | struct md_rdev *this); |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 93 | static void mddev_detach(struct mddev *mddev); |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 94 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | /* |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 96 | * Default number of read corrections we'll attempt on an rdev |
| 97 | * before ejecting it from the array. We divide the read error |
| 98 | * count by 2 for every hour elapsed between read errors. |
| 99 | */ |
| 100 | #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20 |
| 101 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit' |
| 103 | * is 1000 KB/sec, so the extra system load does not show up that much. |
| 104 | * Increase it if you want to have more _guaranteed_ speed. Note that |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 105 | * the RAID driver will use the maximum available bandwidth if the IO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | * subsystem is idle. There is also an 'absolute maximum' reconstruction |
| 107 | * speed limit - in case reconstruction slows down your system despite |
| 108 | * idle IO detection. |
| 109 | * |
| 110 | * 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] | 111 | * or /sys/block/mdX/md/sync_speed_{min,max} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | */ |
| 113 | |
| 114 | static int sysctl_speed_limit_min = 1000; |
| 115 | static int sysctl_speed_limit_max = 200000; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 116 | static inline int speed_min(struct mddev *mddev) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 117 | { |
| 118 | return mddev->sync_speed_min ? |
| 119 | mddev->sync_speed_min : sysctl_speed_limit_min; |
| 120 | } |
| 121 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 122 | static inline int speed_max(struct mddev *mddev) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 123 | { |
| 124 | return mddev->sync_speed_max ? |
| 125 | mddev->sync_speed_max : sysctl_speed_limit_max; |
| 126 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 128 | static void rdev_uninit_serial(struct md_rdev *rdev) |
Guoqing Jiang | 3e148a3 | 2019-06-19 17:30:46 +0800 | [diff] [blame] | 129 | { |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 130 | if (!test_and_clear_bit(CollisionCheck, &rdev->flags)) |
| 131 | return; |
Guoqing Jiang | 3e148a3 | 2019-06-19 17:30:46 +0800 | [diff] [blame] | 132 | |
Guoqing Jiang | 025471f | 2019-12-23 10:49:01 +0100 | [diff] [blame] | 133 | kvfree(rdev->serial); |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 134 | rdev->serial = NULL; |
Guoqing Jiang | 3e148a3 | 2019-06-19 17:30:46 +0800 | [diff] [blame] | 135 | } |
| 136 | |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 137 | static void rdevs_uninit_serial(struct mddev *mddev) |
Guoqing Jiang | 11d3a9f | 2019-12-23 10:48:55 +0100 | [diff] [blame] | 138 | { |
| 139 | struct md_rdev *rdev; |
| 140 | |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 141 | rdev_for_each(rdev, mddev) |
| 142 | rdev_uninit_serial(rdev); |
| 143 | } |
| 144 | |
| 145 | static int rdev_init_serial(struct md_rdev *rdev) |
| 146 | { |
Guoqing Jiang | 025471f | 2019-12-23 10:49:01 +0100 | [diff] [blame] | 147 | /* serial_nums equals with BARRIER_BUCKETS_NR */ |
| 148 | int i, serial_nums = 1 << ((PAGE_SHIFT - ilog2(sizeof(atomic_t)))); |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 149 | struct serial_in_rdev *serial = NULL; |
| 150 | |
| 151 | if (test_bit(CollisionCheck, &rdev->flags)) |
| 152 | return 0; |
| 153 | |
Guoqing Jiang | 025471f | 2019-12-23 10:49:01 +0100 | [diff] [blame] | 154 | serial = kvmalloc(sizeof(struct serial_in_rdev) * serial_nums, |
| 155 | GFP_KERNEL); |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 156 | if (!serial) |
| 157 | return -ENOMEM; |
| 158 | |
Guoqing Jiang | 025471f | 2019-12-23 10:49:01 +0100 | [diff] [blame] | 159 | for (i = 0; i < serial_nums; i++) { |
| 160 | struct serial_in_rdev *serial_tmp = &serial[i]; |
| 161 | |
| 162 | spin_lock_init(&serial_tmp->serial_lock); |
| 163 | serial_tmp->serial_rb = RB_ROOT_CACHED; |
| 164 | init_waitqueue_head(&serial_tmp->serial_io_wait); |
| 165 | } |
| 166 | |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 167 | rdev->serial = serial; |
| 168 | set_bit(CollisionCheck, &rdev->flags); |
| 169 | |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | static int rdevs_init_serial(struct mddev *mddev) |
| 174 | { |
| 175 | struct md_rdev *rdev; |
| 176 | int ret = 0; |
| 177 | |
Guoqing Jiang | 11d3a9f | 2019-12-23 10:48:55 +0100 | [diff] [blame] | 178 | rdev_for_each(rdev, mddev) { |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 179 | ret = rdev_init_serial(rdev); |
| 180 | if (ret) |
| 181 | break; |
Guoqing Jiang | 11d3a9f | 2019-12-23 10:48:55 +0100 | [diff] [blame] | 182 | } |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 183 | |
| 184 | /* Free all resources if pool is not existed */ |
| 185 | if (ret && !mddev->serial_info_pool) |
| 186 | rdevs_uninit_serial(mddev); |
| 187 | |
| 188 | return ret; |
Guoqing Jiang | 11d3a9f | 2019-12-23 10:48:55 +0100 | [diff] [blame] | 189 | } |
| 190 | |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 191 | /* |
Guoqing Jiang | de31ee9 | 2019-12-23 10:48:57 +0100 | [diff] [blame] | 192 | * rdev needs to enable serial stuffs if it meets the conditions: |
| 193 | * 1. it is multi-queue device flaged with writemostly. |
| 194 | * 2. the write-behind mode is enabled. |
| 195 | */ |
| 196 | static int rdev_need_serial(struct md_rdev *rdev) |
| 197 | { |
| 198 | return (rdev && rdev->mddev->bitmap_info.max_write_behind > 0 && |
| 199 | rdev->bdev->bd_queue->nr_hw_queues != 1 && |
| 200 | test_bit(WriteMostly, &rdev->flags)); |
| 201 | } |
| 202 | |
| 203 | /* |
| 204 | * Init resource for rdev(s), then create serial_info_pool if: |
| 205 | * 1. rdev is the first device which return true from rdev_enable_serial. |
| 206 | * 2. rdev is NULL, means we want to enable serialization for all rdevs. |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 207 | */ |
Guoqing Jiang | 404659c | 2019-12-23 10:48:53 +0100 | [diff] [blame] | 208 | void mddev_create_serial_pool(struct mddev *mddev, struct md_rdev *rdev, |
Guoqing Jiang | 11d3a9f | 2019-12-23 10:48:55 +0100 | [diff] [blame] | 209 | bool is_suspend) |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 210 | { |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 211 | int ret = 0; |
| 212 | |
Guoqing Jiang | de31ee9 | 2019-12-23 10:48:57 +0100 | [diff] [blame] | 213 | if (rdev && !rdev_need_serial(rdev) && |
| 214 | !test_bit(CollisionCheck, &rdev->flags)) |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 215 | return; |
| 216 | |
Guoqing Jiang | de31ee9 | 2019-12-23 10:48:57 +0100 | [diff] [blame] | 217 | if (!is_suspend) |
| 218 | mddev_suspend(mddev); |
| 219 | |
| 220 | if (!rdev) |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 221 | ret = rdevs_init_serial(mddev); |
Guoqing Jiang | de31ee9 | 2019-12-23 10:48:57 +0100 | [diff] [blame] | 222 | else |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 223 | ret = rdev_init_serial(rdev); |
| 224 | if (ret) |
| 225 | goto abort; |
Guoqing Jiang | de31ee9 | 2019-12-23 10:48:57 +0100 | [diff] [blame] | 226 | |
Guoqing Jiang | 404659c | 2019-12-23 10:48:53 +0100 | [diff] [blame] | 227 | if (mddev->serial_info_pool == NULL) { |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 228 | unsigned int noio_flag; |
| 229 | |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 230 | noio_flag = memalloc_noio_save(); |
Guoqing Jiang | 404659c | 2019-12-23 10:48:53 +0100 | [diff] [blame] | 231 | mddev->serial_info_pool = |
| 232 | mempool_create_kmalloc_pool(NR_SERIAL_INFOS, |
| 233 | sizeof(struct serial_info)); |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 234 | memalloc_noio_restore(noio_flag); |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 235 | if (!mddev->serial_info_pool) { |
| 236 | rdevs_uninit_serial(mddev); |
Guoqing Jiang | 404659c | 2019-12-23 10:48:53 +0100 | [diff] [blame] | 237 | pr_err("can't alloc memory pool for serialization\n"); |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 238 | } |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 239 | } |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 240 | |
| 241 | abort: |
Guoqing Jiang | de31ee9 | 2019-12-23 10:48:57 +0100 | [diff] [blame] | 242 | if (!is_suspend) |
| 243 | mddev_resume(mddev); |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 244 | } |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 245 | |
| 246 | /* |
Guoqing Jiang | de31ee9 | 2019-12-23 10:48:57 +0100 | [diff] [blame] | 247 | * Free resource from rdev(s), and destroy serial_info_pool under conditions: |
| 248 | * 1. rdev is the last device flaged with CollisionCheck. |
| 249 | * 2. when bitmap is destroyed while policy is not enabled. |
| 250 | * 3. for disable policy, the pool is destroyed only when no rdev needs it. |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 251 | */ |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 252 | void mddev_destroy_serial_pool(struct mddev *mddev, struct md_rdev *rdev, |
| 253 | bool is_suspend) |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 254 | { |
Guoqing Jiang | 11d3a9f | 2019-12-23 10:48:55 +0100 | [diff] [blame] | 255 | if (rdev && !test_bit(CollisionCheck, &rdev->flags)) |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 256 | return; |
| 257 | |
Guoqing Jiang | 404659c | 2019-12-23 10:48:53 +0100 | [diff] [blame] | 258 | if (mddev->serial_info_pool) { |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 259 | struct md_rdev *temp; |
Guoqing Jiang | de31ee9 | 2019-12-23 10:48:57 +0100 | [diff] [blame] | 260 | int num = 0; /* used to track if other rdevs need the pool */ |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 261 | |
Guoqing Jiang | 11d3a9f | 2019-12-23 10:48:55 +0100 | [diff] [blame] | 262 | if (!is_suspend) |
| 263 | mddev_suspend(mddev); |
| 264 | rdev_for_each(temp, mddev) { |
| 265 | if (!rdev) { |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 266 | if (!mddev->serialize_policy || |
| 267 | !rdev_need_serial(temp)) |
| 268 | rdev_uninit_serial(temp); |
Guoqing Jiang | de31ee9 | 2019-12-23 10:48:57 +0100 | [diff] [blame] | 269 | else |
| 270 | num++; |
| 271 | } else if (temp != rdev && |
| 272 | test_bit(CollisionCheck, &temp->flags)) |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 273 | num++; |
Guoqing Jiang | 11d3a9f | 2019-12-23 10:48:55 +0100 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | if (rdev) |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 277 | rdev_uninit_serial(rdev); |
Guoqing Jiang | de31ee9 | 2019-12-23 10:48:57 +0100 | [diff] [blame] | 278 | |
| 279 | if (num) |
| 280 | pr_info("The mempool could be used by other devices\n"); |
| 281 | else { |
Guoqing Jiang | 404659c | 2019-12-23 10:48:53 +0100 | [diff] [blame] | 282 | mempool_destroy(mddev->serial_info_pool); |
| 283 | mddev->serial_info_pool = NULL; |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 284 | } |
Guoqing Jiang | 11d3a9f | 2019-12-23 10:48:55 +0100 | [diff] [blame] | 285 | if (!is_suspend) |
| 286 | mddev_resume(mddev); |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | static struct ctl_table_header *raid_table_header; |
| 291 | |
Joe Perches | 82592c3 | 2013-11-14 15:16:18 +1100 | [diff] [blame] | 292 | static struct ctl_table raid_table[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | .procname = "speed_limit_min", |
| 295 | .data = &sysctl_speed_limit_min, |
| 296 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 297 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 298 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | }, |
| 300 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | .procname = "speed_limit_max", |
| 302 | .data = &sysctl_speed_limit_max, |
| 303 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 304 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 305 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 307 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | }; |
| 309 | |
Joe Perches | 82592c3 | 2013-11-14 15:16:18 +1100 | [diff] [blame] | 310 | static struct ctl_table raid_dir_table[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | .procname = "raid", |
| 313 | .maxlen = 0, |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 314 | .mode = S_IRUGO|S_IXUGO, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | .child = raid_table, |
| 316 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 317 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | }; |
| 319 | |
Joe Perches | 82592c3 | 2013-11-14 15:16:18 +1100 | [diff] [blame] | 320 | static struct ctl_table raid_root_table[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | .procname = "dev", |
| 323 | .maxlen = 0, |
| 324 | .mode = 0555, |
| 325 | .child = raid_dir_table, |
| 326 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 327 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | }; |
| 329 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 330 | static const struct block_device_operations md_fops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 332 | static int start_readonly; |
| 333 | |
NeilBrown | 78b6350 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 334 | /* |
| 335 | * The original mechanism for creating an md device is to create |
| 336 | * a device node in /dev and to open it. This causes races with device-close. |
| 337 | * The preferred method is to write to the "new_array" module parameter. |
| 338 | * This can avoid races. |
| 339 | * Setting create_on_open to false disables the original mechanism |
| 340 | * so all the races disappear. |
| 341 | */ |
| 342 | static bool create_on_open = true; |
| 343 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 344 | struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs, |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 345 | struct mddev *mddev) |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 346 | { |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 347 | if (!mddev || !bioset_initialized(&mddev->bio_set)) |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 348 | return bio_alloc(gfp_mask, nr_iovecs); |
| 349 | |
Marcos Paulo de Souza | 6251691 | 2019-01-14 06:31:56 -0700 | [diff] [blame] | 350 | return bio_alloc_bioset(gfp_mask, nr_iovecs, &mddev->bio_set); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 351 | } |
| 352 | EXPORT_SYMBOL_GPL(bio_alloc_mddev); |
| 353 | |
NeilBrown | 5a85071 | 2017-06-21 09:12:21 +1000 | [diff] [blame] | 354 | static struct bio *md_bio_alloc_sync(struct mddev *mddev) |
| 355 | { |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 356 | if (!mddev || !bioset_initialized(&mddev->sync_set)) |
NeilBrown | 5a85071 | 2017-06-21 09:12:21 +1000 | [diff] [blame] | 357 | return bio_alloc(GFP_NOIO, 1); |
| 358 | |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 359 | return bio_alloc_bioset(GFP_NOIO, 1, &mddev->sync_set); |
NeilBrown | 5a85071 | 2017-06-21 09:12:21 +1000 | [diff] [blame] | 360 | } |
| 361 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | /* |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 363 | * We have a system wide 'event count' that is incremented |
| 364 | * on any 'interesting' event, and readers of /proc/mdstat |
| 365 | * can use 'poll' or 'select' to find out when the event |
| 366 | * count increases. |
| 367 | * |
| 368 | * Events are: |
| 369 | * start array, stop array, error, add device, remove device, |
| 370 | * start build, activate spare |
| 371 | */ |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 372 | static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 373 | static atomic_t md_event_count; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 374 | void md_new_event(struct mddev *mddev) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 375 | { |
| 376 | atomic_inc(&md_event_count); |
| 377 | wake_up(&md_event_waiters); |
| 378 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 379 | EXPORT_SYMBOL_GPL(md_new_event); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 380 | |
| 381 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | * Enables to iterate over all existing md arrays |
| 383 | * all_mddevs_lock protects this list. |
| 384 | */ |
| 385 | static LIST_HEAD(all_mddevs); |
| 386 | static DEFINE_SPINLOCK(all_mddevs_lock); |
| 387 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | /* |
| 389 | * iterates through all used mddevs in the system. |
| 390 | * We take care to grab the all_mddevs_lock whenever navigating |
| 391 | * the list, and to always hold a refcount when unlocked. |
| 392 | * Any code which breaks out of this loop while own |
| 393 | * a reference to the current mddev and must mddev_put it. |
| 394 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 395 | #define for_each_mddev(_mddev,_tmp) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | \ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 397 | for (({ spin_lock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 398 | _tmp = all_mddevs.next; \ |
| 399 | _mddev = NULL;}); \ |
| 400 | ({ if (_tmp != &all_mddevs) \ |
| 401 | mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | spin_unlock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 403 | if (_mddev) mddev_put(_mddev); \ |
| 404 | _mddev = list_entry(_tmp, struct mddev, all_mddevs); \ |
| 405 | _tmp != &all_mddevs;}); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | ({ spin_lock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 407 | _tmp = _tmp->next;}) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | ) |
| 409 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 410 | /* Rather than calling directly into the personality make_request function, |
| 411 | * IO requests come here first so that we can check if the device is |
| 412 | * being suspended pending a reconfiguration. |
| 413 | * We hold a refcount over the call to ->make_request. By the time that |
| 414 | * call has finished, the bio has been linked into some internal structure |
| 415 | * and so is visible to ->quiesce(), so we don't need the refcount any more. |
| 416 | */ |
NeilBrown | b3143b9 | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 417 | static bool is_suspended(struct mddev *mddev, struct bio *bio) |
| 418 | { |
| 419 | if (mddev->suspended) |
| 420 | return true; |
| 421 | if (bio_data_dir(bio) != WRITE) |
| 422 | return false; |
| 423 | if (mddev->suspend_lo >= mddev->suspend_hi) |
| 424 | return false; |
| 425 | if (bio->bi_iter.bi_sector >= mddev->suspend_hi) |
| 426 | return false; |
| 427 | if (bio_end_sector(bio) < mddev->suspend_lo) |
| 428 | return false; |
| 429 | return true; |
| 430 | } |
| 431 | |
Shaohua Li | 393debc | 2017-09-21 10:23:35 -0700 | [diff] [blame] | 432 | void md_handle_request(struct mddev *mddev, struct bio *bio) |
| 433 | { |
| 434 | check_suspended: |
| 435 | rcu_read_lock(); |
NeilBrown | b3143b9 | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 436 | if (is_suspended(mddev, bio)) { |
Shaohua Li | 393debc | 2017-09-21 10:23:35 -0700 | [diff] [blame] | 437 | DEFINE_WAIT(__wait); |
| 438 | for (;;) { |
| 439 | prepare_to_wait(&mddev->sb_wait, &__wait, |
| 440 | TASK_UNINTERRUPTIBLE); |
NeilBrown | b3143b9 | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 441 | if (!is_suspended(mddev, bio)) |
Shaohua Li | 393debc | 2017-09-21 10:23:35 -0700 | [diff] [blame] | 442 | break; |
| 443 | rcu_read_unlock(); |
| 444 | schedule(); |
| 445 | rcu_read_lock(); |
| 446 | } |
| 447 | finish_wait(&mddev->sb_wait, &__wait); |
| 448 | } |
| 449 | atomic_inc(&mddev->active_io); |
| 450 | rcu_read_unlock(); |
| 451 | |
| 452 | if (!mddev->pers->make_request(mddev, bio)) { |
| 453 | atomic_dec(&mddev->active_io); |
| 454 | wake_up(&mddev->sb_wait); |
| 455 | goto check_suspended; |
| 456 | } |
| 457 | |
| 458 | if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended) |
| 459 | wake_up(&mddev->sb_wait); |
| 460 | } |
| 461 | EXPORT_SYMBOL(md_handle_request); |
| 462 | |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 463 | static blk_qc_t md_make_request(struct request_queue *q, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | { |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 465 | const int rw = bio_data_dir(bio); |
Michael Callahan | ddcf35d | 2018-07-18 04:47:39 -0700 | [diff] [blame] | 466 | const int sgrp = op_stat_group(bio_op(bio)); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 467 | struct mddev *mddev = q->queuedata; |
Chris Mason | e91ece5 | 2011-02-07 19:21:48 -0500 | [diff] [blame] | 468 | unsigned int sectors; |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 469 | |
Guilherme G. Piccoli | 62f7b19 | 2019-09-03 16:49:00 -0300 | [diff] [blame] | 470 | if (unlikely(test_bit(MD_BROKEN, &mddev->flags)) && (rw == WRITE)) { |
| 471 | bio_io_error(bio); |
| 472 | return BLK_QC_T_NONE; |
| 473 | } |
| 474 | |
NeilBrown | af67c31 | 2017-06-18 14:38:57 +1000 | [diff] [blame] | 475 | blk_queue_split(q, &bio); |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 476 | |
NeilBrown | 274d8cb | 2016-01-04 16:16:58 +1100 | [diff] [blame] | 477 | if (mddev == NULL || mddev->pers == NULL) { |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 478 | bio_io_error(bio); |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 479 | return BLK_QC_T_NONE; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 480 | } |
Sebastian Riemer | bbfa57c | 2013-02-21 13:28:09 +1100 | [diff] [blame] | 481 | if (mddev->ro == 1 && unlikely(rw == WRITE)) { |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 482 | if (bio_sectors(bio) != 0) |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 483 | bio->bi_status = BLK_STS_IOERR; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 484 | bio_endio(bio); |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 485 | return BLK_QC_T_NONE; |
Sebastian Riemer | bbfa57c | 2013-02-21 13:28:09 +1100 | [diff] [blame] | 486 | } |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 487 | |
Chris Mason | e91ece5 | 2011-02-07 19:21:48 -0500 | [diff] [blame] | 488 | /* |
| 489 | * save the sectors now since our bio can |
| 490 | * go away inside make_request |
| 491 | */ |
| 492 | sectors = bio_sectors(bio); |
Shaohua Li | 9c573de | 2016-04-25 16:52:38 -0700 | [diff] [blame] | 493 | /* bio could be mergeable after passing to underlayer */ |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 494 | bio->bi_opf &= ~REQ_NOMERGE; |
Shaohua Li | 393debc | 2017-09-21 10:23:35 -0700 | [diff] [blame] | 495 | |
| 496 | md_handle_request(mddev, bio); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 497 | |
Mike Snitzer | 112f158 | 2018-12-06 11:41:18 -0500 | [diff] [blame] | 498 | part_stat_lock(); |
| 499 | part_stat_inc(&mddev->gendisk->part0, ios[sgrp]); |
| 500 | part_stat_add(&mddev->gendisk->part0, sectors[sgrp], sectors); |
Gu Zheng | 74672d0 | 2015-04-03 08:44:47 +0800 | [diff] [blame] | 501 | part_stat_unlock(); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 502 | |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 503 | return BLK_QC_T_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | } |
| 505 | |
NeilBrown | 9e35b99 | 2010-04-06 14:23:02 +1000 | [diff] [blame] | 506 | /* mddev_suspend makes sure no new requests are submitted |
| 507 | * to the device, and that any requests that have been submitted |
| 508 | * are completely handled. |
NeilBrown | afa0f55 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 509 | * Once mddev_detach() is called and completes, the module will be |
| 510 | * completely unused. |
NeilBrown | 9e35b99 | 2010-04-06 14:23:02 +1000 | [diff] [blame] | 511 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 512 | void mddev_suspend(struct mddev *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 513 | { |
Heinz Mauelshagen | 092398d | 2016-05-03 19:43:57 +0200 | [diff] [blame] | 514 | WARN_ON_ONCE(mddev->thread && current == mddev->thread->tsk); |
NeilBrown | 4d5324f | 2017-10-19 12:17:16 +1100 | [diff] [blame] | 515 | lockdep_assert_held(&mddev->reconfig_mutex); |
Mikulas Patocka | 0dc10e5 | 2015-12-18 15:19:16 +1100 | [diff] [blame] | 516 | if (mddev->suspended++) |
| 517 | return; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 518 | synchronize_rcu(); |
NeilBrown | cc27b0c | 2017-06-05 16:49:39 +1000 | [diff] [blame] | 519 | wake_up(&mddev->sb_wait); |
NeilBrown | 35bfc52 | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 520 | set_bit(MD_ALLOW_SB_UPDATE, &mddev->flags); |
| 521 | smp_mb__after_atomic(); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 522 | wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0); |
| 523 | mddev->pers->quiesce(mddev, 1); |
NeilBrown | 35bfc52 | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 524 | clear_bit_unlock(MD_ALLOW_SB_UPDATE, &mddev->flags); |
| 525 | wait_event(mddev->sb_wait, !test_bit(MD_UPDATING_SB, &mddev->flags)); |
Jonathan Brassow | 0d9f4f1 | 2012-05-16 04:06:14 -0500 | [diff] [blame] | 526 | |
| 527 | del_timer_sync(&mddev->safemode_timer); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 528 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 529 | EXPORT_SYMBOL_GPL(mddev_suspend); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 530 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 531 | void mddev_resume(struct mddev *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 532 | { |
NeilBrown | 4d5324f | 2017-10-19 12:17:16 +1100 | [diff] [blame] | 533 | lockdep_assert_held(&mddev->reconfig_mutex); |
Mikulas Patocka | 0dc10e5 | 2015-12-18 15:19:16 +1100 | [diff] [blame] | 534 | if (--mddev->suspended) |
| 535 | return; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 536 | wake_up(&mddev->sb_wait); |
| 537 | mddev->pers->quiesce(mddev, 0); |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 538 | |
Jonathan Brassow | 47525e5 | 2012-05-22 13:55:29 +1000 | [diff] [blame] | 539 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 540 | md_wakeup_thread(mddev->thread); |
| 541 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 542 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 543 | EXPORT_SYMBOL_GPL(mddev_resume); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 544 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 545 | int mddev_congested(struct mddev *mddev, int bits) |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 546 | { |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 547 | struct md_personality *pers = mddev->pers; |
| 548 | int ret = 0; |
| 549 | |
| 550 | rcu_read_lock(); |
| 551 | if (mddev->suspended) |
| 552 | ret = 1; |
| 553 | else if (pers && pers->congested) |
| 554 | ret = pers->congested(mddev, bits); |
| 555 | rcu_read_unlock(); |
| 556 | return ret; |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 557 | } |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 558 | EXPORT_SYMBOL_GPL(mddev_congested); |
| 559 | static int md_congested(void *data, int bits) |
| 560 | { |
| 561 | struct mddev *mddev = data; |
| 562 | return mddev_congested(mddev, bits); |
| 563 | } |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 564 | |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 565 | /* |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 566 | * Generic flush handling for md |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 567 | */ |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 568 | |
| 569 | static void md_end_flush(struct bio *bio) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 570 | { |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 571 | struct md_rdev *rdev = bio->bi_private; |
| 572 | struct mddev *mddev = rdev->mddev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 573 | |
| 574 | rdev_dec_pending(rdev, mddev); |
| 575 | |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 576 | if (atomic_dec_and_test(&mddev->flush_pending)) { |
| 577 | /* The pre-request flush has finished */ |
| 578 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 579 | } |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 580 | bio_put(bio); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 581 | } |
| 582 | |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 583 | static void md_submit_flush_data(struct work_struct *ws); |
| 584 | |
| 585 | static void submit_flushes(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 586 | { |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 587 | struct mddev *mddev = container_of(ws, struct mddev, flush_work); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 588 | struct md_rdev *rdev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 589 | |
NeilBrown | 2bc13b8 | 2019-03-29 10:46:17 -0700 | [diff] [blame] | 590 | mddev->start_flush = ktime_get_boottime(); |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 591 | INIT_WORK(&mddev->flush_work, md_submit_flush_data); |
| 592 | atomic_set(&mddev->flush_pending, 1); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 593 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 594 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 595 | if (rdev->raid_disk >= 0 && |
| 596 | !test_bit(Faulty, &rdev->flags)) { |
| 597 | /* Take two references, one is dropped |
| 598 | * when request finishes, one after |
| 599 | * we reclaim rcu_read_lock |
| 600 | */ |
| 601 | struct bio *bi; |
| 602 | atomic_inc(&rdev->nr_pending); |
| 603 | atomic_inc(&rdev->nr_pending); |
| 604 | rcu_read_unlock(); |
Shaohua Li | b5e1b8c | 2012-05-21 09:26:59 +1000 | [diff] [blame] | 605 | bi = bio_alloc_mddev(GFP_NOIO, 0, mddev); |
Xiao Ni | 5a409b4 | 2018-05-21 11:49:54 +0800 | [diff] [blame] | 606 | bi->bi_end_io = md_end_flush; |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 607 | bi->bi_private = rdev; |
| 608 | bio_set_dev(bi, rdev->bdev); |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 609 | bi->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH; |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 610 | atomic_inc(&mddev->flush_pending); |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 611 | submit_bio(bi); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 612 | rcu_read_lock(); |
| 613 | rdev_dec_pending(rdev, mddev); |
| 614 | } |
| 615 | rcu_read_unlock(); |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 616 | if (atomic_dec_and_test(&mddev->flush_pending)) |
| 617 | queue_work(md_wq, &mddev->flush_work); |
| 618 | } |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 619 | |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 620 | static void md_submit_flush_data(struct work_struct *ws) |
| 621 | { |
| 622 | struct mddev *mddev = container_of(ws, struct mddev, flush_work); |
| 623 | struct bio *bio = mddev->flush_bio; |
| 624 | |
| 625 | /* |
| 626 | * must reset flush_bio before calling into md_handle_request to avoid a |
| 627 | * deadlock, because other bios passed md_handle_request suspend check |
| 628 | * could wait for this and below md_handle_request could wait for those |
| 629 | * bios because of suspend check |
| 630 | */ |
NeilBrown | 2bc13b8 | 2019-03-29 10:46:17 -0700 | [diff] [blame] | 631 | mddev->last_flush = mddev->start_flush; |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 632 | mddev->flush_bio = NULL; |
| 633 | wake_up(&mddev->sb_wait); |
| 634 | |
| 635 | if (bio->bi_iter.bi_size == 0) { |
| 636 | /* an empty barrier - all done */ |
| 637 | bio_endio(bio); |
| 638 | } else { |
| 639 | bio->bi_opf &= ~REQ_PREFLUSH; |
| 640 | md_handle_request(mddev, bio); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 641 | } |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 642 | } |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 643 | |
David Jeffery | 775d783 | 2019-09-16 13:15:14 -0400 | [diff] [blame] | 644 | /* |
| 645 | * Manages consolidation of flushes and submitting any flushes needed for |
| 646 | * a bio with REQ_PREFLUSH. Returns true if the bio is finished or is |
| 647 | * being finished in another context. Returns false if the flushing is |
| 648 | * complete but still needs the I/O portion of the bio to be processed. |
| 649 | */ |
| 650 | bool md_flush_request(struct mddev *mddev, struct bio *bio) |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 651 | { |
NeilBrown | 2bc13b8 | 2019-03-29 10:46:17 -0700 | [diff] [blame] | 652 | ktime_t start = ktime_get_boottime(); |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 653 | spin_lock_irq(&mddev->lock); |
| 654 | wait_event_lock_irq(mddev->sb_wait, |
NeilBrown | 2bc13b8 | 2019-03-29 10:46:17 -0700 | [diff] [blame] | 655 | !mddev->flush_bio || |
| 656 | ktime_after(mddev->last_flush, start), |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 657 | mddev->lock); |
NeilBrown | 2bc13b8 | 2019-03-29 10:46:17 -0700 | [diff] [blame] | 658 | if (!ktime_after(mddev->last_flush, start)) { |
| 659 | WARN_ON(mddev->flush_bio); |
| 660 | mddev->flush_bio = bio; |
| 661 | bio = NULL; |
| 662 | } |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 663 | spin_unlock_irq(&mddev->lock); |
| 664 | |
NeilBrown | 2bc13b8 | 2019-03-29 10:46:17 -0700 | [diff] [blame] | 665 | if (!bio) { |
| 666 | INIT_WORK(&mddev->flush_work, submit_flushes); |
| 667 | queue_work(md_wq, &mddev->flush_work); |
| 668 | } else { |
| 669 | /* flush was performed for some other bio while we waited. */ |
| 670 | if (bio->bi_iter.bi_size == 0) |
| 671 | /* an empty barrier - all done */ |
| 672 | bio_endio(bio); |
| 673 | else { |
| 674 | bio->bi_opf &= ~REQ_PREFLUSH; |
David Jeffery | 775d783 | 2019-09-16 13:15:14 -0400 | [diff] [blame] | 675 | return false; |
NeilBrown | 2bc13b8 | 2019-03-29 10:46:17 -0700 | [diff] [blame] | 676 | } |
| 677 | } |
David Jeffery | 775d783 | 2019-09-16 13:15:14 -0400 | [diff] [blame] | 678 | return true; |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 679 | } |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 680 | EXPORT_SYMBOL(md_flush_request); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 681 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 682 | static inline struct mddev *mddev_get(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | { |
| 684 | atomic_inc(&mddev->active); |
| 685 | return mddev; |
| 686 | } |
| 687 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 688 | static void mddev_delayed_delete(struct work_struct *ws); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 689 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 690 | static void mddev_put(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | { |
| 692 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
| 693 | return; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 694 | if (!mddev->raid_disks && list_empty(&mddev->disks) && |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 695 | mddev->ctime == 0 && !mddev->hold_active) { |
| 696 | /* Array is not configured at all, and not held active, |
| 697 | * so destroy it */ |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 698 | list_del_init(&mddev->all_mddevs); |
Kent Overstreet | 28dec87 | 2018-06-07 20:52:54 -0400 | [diff] [blame] | 699 | |
| 700 | /* |
| 701 | * Call queue_work inside the spinlock so that |
| 702 | * flush_workqueue() after mddev_find will succeed in waiting |
| 703 | * for the work to be done. |
| 704 | */ |
| 705 | INIT_WORK(&mddev->del_work, mddev_delayed_delete); |
| 706 | queue_work(md_misc_wq, &mddev->del_work); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 707 | } |
| 708 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | } |
| 710 | |
Kees Cook | 8376d3c | 2017-10-16 17:01:48 -0700 | [diff] [blame] | 711 | static void md_safemode_timeout(struct timer_list *t); |
Sasha Levin | 25b2edf | 2015-07-24 18:19:58 -0400 | [diff] [blame] | 712 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 713 | void mddev_init(struct mddev *mddev) |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 714 | { |
Kent Overstreet | 28dec87 | 2018-06-07 20:52:54 -0400 | [diff] [blame] | 715 | kobject_init(&mddev->kobj, &md_ktype); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 716 | mutex_init(&mddev->open_mutex); |
| 717 | mutex_init(&mddev->reconfig_mutex); |
| 718 | mutex_init(&mddev->bitmap_info.mutex); |
| 719 | INIT_LIST_HEAD(&mddev->disks); |
| 720 | INIT_LIST_HEAD(&mddev->all_mddevs); |
Kees Cook | 8376d3c | 2017-10-16 17:01:48 -0700 | [diff] [blame] | 721 | timer_setup(&mddev->safemode_timer, md_safemode_timeout, 0); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 722 | atomic_set(&mddev->active, 1); |
| 723 | atomic_set(&mddev->openers, 0); |
| 724 | atomic_set(&mddev->active_io, 0); |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 725 | spin_lock_init(&mddev->lock); |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 726 | atomic_set(&mddev->flush_pending, 0); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 727 | init_waitqueue_head(&mddev->sb_wait); |
| 728 | init_waitqueue_head(&mddev->recovery_wait); |
| 729 | mddev->reshape_position = MaxSector; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 730 | mddev->reshape_backwards = 0; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 731 | mddev->last_sync_action = "none"; |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 732 | mddev->resync_min = 0; |
| 733 | mddev->resync_max = MaxSector; |
| 734 | mddev->level = LEVEL_NONE; |
| 735 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 736 | EXPORT_SYMBOL_GPL(mddev_init); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 737 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 738 | static struct mddev *mddev_find(dev_t unit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 740 | struct mddev *mddev, *new = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | |
NeilBrown | 8f5f02c | 2011-02-16 13:58:51 +1100 | [diff] [blame] | 742 | if (unit && MAJOR(unit) != MD_MAJOR) |
| 743 | unit &= ~((1<<MdpMinorShift)-1); |
| 744 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | retry: |
| 746 | spin_lock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 748 | if (unit) { |
| 749 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 750 | if (mddev->unit == unit) { |
| 751 | mddev_get(mddev); |
| 752 | spin_unlock(&all_mddevs_lock); |
| 753 | kfree(new); |
| 754 | return mddev; |
| 755 | } |
| 756 | |
| 757 | if (new) { |
| 758 | list_add(&new->all_mddevs, &all_mddevs); |
| 759 | spin_unlock(&all_mddevs_lock); |
| 760 | new->hold_active = UNTIL_IOCTL; |
| 761 | return new; |
| 762 | } |
| 763 | } else if (new) { |
| 764 | /* find an unused unit number */ |
| 765 | static int next_minor = 512; |
| 766 | int start = next_minor; |
| 767 | int is_free = 0; |
| 768 | int dev = 0; |
| 769 | while (!is_free) { |
| 770 | dev = MKDEV(MD_MAJOR, next_minor); |
| 771 | next_minor++; |
| 772 | if (next_minor > MINORMASK) |
| 773 | next_minor = 0; |
| 774 | if (next_minor == start) { |
| 775 | /* Oh dear, all in use. */ |
| 776 | spin_unlock(&all_mddevs_lock); |
| 777 | kfree(new); |
| 778 | return NULL; |
| 779 | } |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 780 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 781 | is_free = 1; |
| 782 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 783 | if (mddev->unit == dev) { |
| 784 | is_free = 0; |
| 785 | break; |
| 786 | } |
| 787 | } |
| 788 | new->unit = dev; |
| 789 | new->md_minor = MINOR(dev); |
| 790 | new->hold_active = UNTIL_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | list_add(&new->all_mddevs, &all_mddevs); |
| 792 | spin_unlock(&all_mddevs_lock); |
| 793 | return new; |
| 794 | } |
| 795 | spin_unlock(&all_mddevs_lock); |
| 796 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 797 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | if (!new) |
| 799 | return NULL; |
| 800 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | new->unit = unit; |
| 802 | if (MAJOR(unit) == MD_MAJOR) |
| 803 | new->md_minor = MINOR(unit); |
| 804 | else |
| 805 | new->md_minor = MINOR(unit) >> MdpMinorShift; |
| 806 | |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 807 | mddev_init(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | goto retry; |
| 810 | } |
| 811 | |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 812 | static struct attribute_group md_redundancy_group; |
| 813 | |
NeilBrown | 5c47daf | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 814 | void mddev_unlock(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | { |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 816 | if (mddev->to_remove) { |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 817 | /* These cannot be removed under reconfig_mutex as |
| 818 | * an access to the files will try to take reconfig_mutex |
| 819 | * while holding the file unremovable, which leads to |
| 820 | * a deadlock. |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 821 | * So hold set sysfs_active while the remove in happeing, |
| 822 | * and anything else which might set ->to_remove or my |
| 823 | * otherwise change the sysfs namespace will fail with |
| 824 | * -EBUSY if sysfs_active is still set. |
| 825 | * We set sysfs_active under reconfig_mutex and elsewhere |
| 826 | * test it under the same mutex to ensure its correct value |
| 827 | * is seen. |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 828 | */ |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 829 | struct attribute_group *to_remove = mddev->to_remove; |
| 830 | mddev->to_remove = NULL; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 831 | mddev->sysfs_active = 1; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 832 | mutex_unlock(&mddev->reconfig_mutex); |
| 833 | |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 834 | if (mddev->kobj.sd) { |
| 835 | if (to_remove != &md_redundancy_group) |
| 836 | sysfs_remove_group(&mddev->kobj, to_remove); |
| 837 | if (mddev->pers == NULL || |
| 838 | mddev->pers->sync_request == NULL) { |
| 839 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 840 | if (mddev->sysfs_action) |
| 841 | sysfs_put(mddev->sysfs_action); |
| 842 | mddev->sysfs_action = NULL; |
| 843 | } |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 844 | } |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 845 | mddev->sysfs_active = 0; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 846 | } else |
| 847 | mutex_unlock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | |
Chris Dunlop | 751e67c | 2011-10-19 16:48:26 +1100 | [diff] [blame] | 849 | /* As we've dropped the mutex we need a spinlock to |
| 850 | * make sure the thread doesn't disappear |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 851 | */ |
| 852 | spin_lock(&pers_lock); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 853 | md_wakeup_thread(mddev->thread); |
NeilBrown | 4d5324f | 2017-10-19 12:17:16 +1100 | [diff] [blame] | 854 | wake_up(&mddev->sb_wait); |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 855 | spin_unlock(&pers_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | } |
NeilBrown | 5c47daf | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 857 | EXPORT_SYMBOL_GPL(mddev_unlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 859 | struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr) |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 860 | { |
| 861 | struct md_rdev *rdev; |
| 862 | |
| 863 | rdev_for_each_rcu(rdev, mddev) |
| 864 | if (rdev->desc_nr == nr) |
| 865 | return rdev; |
| 866 | |
| 867 | return NULL; |
| 868 | } |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 869 | EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu); |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 870 | |
| 871 | static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 873 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 875 | rdev_for_each(rdev, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | if (rdev->bdev->bd_dev == dev) |
| 877 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 878 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | return NULL; |
| 880 | } |
| 881 | |
Tomasz Majchrzak | 1532d9e | 2017-12-27 10:31:40 +0100 | [diff] [blame] | 882 | struct md_rdev *md_find_rdev_rcu(struct mddev *mddev, dev_t dev) |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 883 | { |
| 884 | struct md_rdev *rdev; |
| 885 | |
| 886 | rdev_for_each_rcu(rdev, mddev) |
| 887 | if (rdev->bdev->bd_dev == dev) |
| 888 | return rdev; |
| 889 | |
| 890 | return NULL; |
| 891 | } |
Tomasz Majchrzak | 1532d9e | 2017-12-27 10:31:40 +0100 | [diff] [blame] | 892 | EXPORT_SYMBOL_GPL(md_find_rdev_rcu); |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 893 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 894 | static struct md_personality *find_pers(int level, char *clevel) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 895 | { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 896 | struct md_personality *pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 897 | list_for_each_entry(pers, &pers_list, list) { |
| 898 | if (level != LEVEL_NONE && pers->level == level) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 899 | return pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 900 | if (strcmp(pers->name, clevel)==0) |
| 901 | return pers; |
| 902 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 903 | return NULL; |
| 904 | } |
| 905 | |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 906 | /* return the offset of the super block in 512byte sectors */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 907 | static inline sector_t calc_dev_sboffset(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | { |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 909 | sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512; |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 910 | return MD_NEW_SIZE_SECTORS(num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | } |
| 912 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 913 | static int alloc_disk_sb(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | rdev->sb_page = alloc_page(GFP_KERNEL); |
NeilBrown | 7f0f0d8 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 916 | if (!rdev->sb_page) |
Andre Noll | ebc2433 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 917 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | return 0; |
| 919 | } |
| 920 | |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 921 | void md_rdev_clear(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | { |
| 923 | if (rdev->sb_page) { |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 924 | put_page(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | rdev->sb_loaded = 0; |
| 926 | rdev->sb_page = NULL; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 927 | rdev->sb_start = 0; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 928 | rdev->sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 930 | if (rdev->bb_page) { |
| 931 | put_page(rdev->bb_page); |
| 932 | rdev->bb_page = NULL; |
| 933 | } |
Dan Williams | d3b407fb | 2016-01-06 12:19:22 -0800 | [diff] [blame] | 934 | badblocks_exit(&rdev->badblocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | } |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 936 | EXPORT_SYMBOL_GPL(md_rdev_clear); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 938 | static void super_written(struct bio *bio) |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 939 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 940 | struct md_rdev *rdev = bio->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 941 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 942 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 943 | if (bio->bi_status) { |
| 944 | pr_err("md: super_written gets error=%d\n", bio->bi_status); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 945 | md_error(mddev, rdev); |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 946 | if (!test_bit(Faulty, &rdev->flags) |
| 947 | && (bio->bi_opf & MD_FAILFAST)) { |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 948 | set_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags); |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 949 | set_bit(LastDev, &rdev->flags); |
| 950 | } |
| 951 | } else |
| 952 | clear_bit(LastDev, &rdev->flags); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 953 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 954 | if (atomic_dec_and_test(&mddev->pending_writes)) |
| 955 | wake_up(&mddev->sb_wait); |
Shaohua Li | ed3b98c | 2016-03-29 14:00:19 -0700 | [diff] [blame] | 956 | rdev_dec_pending(rdev, mddev); |
Neil Brown | f8b58ed | 2005-06-27 22:29:34 -0700 | [diff] [blame] | 957 | bio_put(bio); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 958 | } |
| 959 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 960 | void md_super_write(struct mddev *mddev, struct md_rdev *rdev, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 961 | sector_t sector, int size, struct page *page) |
| 962 | { |
| 963 | /* write first size bytes of page to sector of rdev |
| 964 | * Increment mddev->pending_writes before returning |
| 965 | * and decrement it on completion, waking up sb_wait |
| 966 | * if zero is reached. |
| 967 | * If an error occurred, call md_error |
| 968 | */ |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 969 | struct bio *bio; |
| 970 | int ff = 0; |
| 971 | |
Heinz Mauelshagen | 4b6c106 | 2018-02-02 23:13:19 +0100 | [diff] [blame] | 972 | if (!page) |
| 973 | return; |
| 974 | |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 975 | if (test_bit(Faulty, &rdev->flags)) |
| 976 | return; |
| 977 | |
NeilBrown | 5a85071 | 2017-06-21 09:12:21 +1000 | [diff] [blame] | 978 | bio = md_bio_alloc_sync(mddev); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 979 | |
Shaohua Li | ed3b98c | 2016-03-29 14:00:19 -0700 | [diff] [blame] | 980 | atomic_inc(&rdev->nr_pending); |
| 981 | |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 982 | bio_set_dev(bio, rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 983 | bio->bi_iter.bi_sector = sector; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 984 | bio_add_page(bio, page, size, 0); |
| 985 | bio->bi_private = rdev; |
| 986 | bio->bi_end_io = super_written; |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 987 | |
| 988 | if (test_bit(MD_FAILFAST_SUPPORTED, &mddev->flags) && |
| 989 | test_bit(FailFast, &rdev->flags) && |
| 990 | !test_bit(LastDev, &rdev->flags)) |
| 991 | ff = MD_FAILFAST; |
Jan Kara | 5a8948f | 2017-05-31 09:44:33 +0200 | [diff] [blame] | 992 | bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH | REQ_FUA | ff; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 993 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 994 | atomic_inc(&mddev->pending_writes); |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 995 | submit_bio(bio); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 996 | } |
| 997 | |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 998 | int md_super_wait(struct mddev *mddev) |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 999 | { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1000 | /* wait for all superblock writes that were scheduled to complete */ |
NeilBrown | 1967cd5 | 2014-09-09 14:20:28 +1000 | [diff] [blame] | 1001 | wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 1002 | if (test_and_clear_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags)) |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 1003 | return -EAGAIN; |
| 1004 | return 0; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1005 | } |
| 1006 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1007 | int sync_page_io(struct md_rdev *rdev, sector_t sector, int size, |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1008 | struct page *page, int op, int op_flags, bool metadata_op) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | { |
NeilBrown | 5a85071 | 2017-06-21 09:12:21 +1000 | [diff] [blame] | 1010 | struct bio *bio = md_bio_alloc_sync(rdev->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | int ret; |
| 1012 | |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 1013 | if (metadata_op && rdev->meta_bdev) |
| 1014 | bio_set_dev(bio, rdev->meta_bdev); |
| 1015 | else |
| 1016 | bio_set_dev(bio, rdev->bdev); |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1017 | bio_set_op_attrs(bio, op, op_flags); |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1018 | if (metadata_op) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1019 | bio->bi_iter.bi_sector = sector + rdev->sb_start; |
NeilBrown | 1fdd6fc9 | 2012-05-21 09:28:32 +1000 | [diff] [blame] | 1020 | else if (rdev->mddev->reshape_position != MaxSector && |
| 1021 | (rdev->mddev->reshape_backwards == |
| 1022 | (sector >= rdev->mddev->reshape_position))) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1023 | bio->bi_iter.bi_sector = sector + rdev->new_data_offset; |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1024 | else |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1025 | bio->bi_iter.bi_sector = sector + rdev->data_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1026 | bio_add_page(bio, page, size, 0); |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 1027 | |
| 1028 | submit_bio_wait(bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 1030 | ret = !bio->bi_status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | bio_put(bio); |
| 1032 | return ret; |
| 1033 | } |
NeilBrown | a8745db | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 1034 | EXPORT_SYMBOL_GPL(sync_page_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1036 | static int read_disk_sb(struct md_rdev *rdev, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | { |
| 1038 | char b[BDEVNAME_SIZE]; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 1039 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1040 | if (rdev->sb_loaded) |
| 1041 | return 0; |
| 1042 | |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1043 | if (!sync_page_io(rdev, 0, size, rdev->sb_page, REQ_OP_READ, 0, true)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | goto fail; |
| 1045 | rdev->sb_loaded = 1; |
| 1046 | return 0; |
| 1047 | |
| 1048 | fail: |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1049 | pr_err("md: disabled device %s, could not read superblock.\n", |
| 1050 | bdevname(rdev->bdev,b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | return -EINVAL; |
| 1052 | } |
| 1053 | |
Amir Goldstein | e6fd209 | 2017-05-04 16:26:20 +0300 | [diff] [blame] | 1054 | static int md_uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1056 | return sb1->set_uuid0 == sb2->set_uuid0 && |
Andre Noll | 0571046 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 1057 | sb1->set_uuid1 == sb2->set_uuid1 && |
| 1058 | sb1->set_uuid2 == sb2->set_uuid2 && |
| 1059 | sb1->set_uuid3 == sb2->set_uuid3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | } |
| 1061 | |
Amir Goldstein | e6fd209 | 2017-05-04 16:26:20 +0300 | [diff] [blame] | 1062 | static int md_sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | { |
| 1064 | int ret; |
| 1065 | mdp_super_t *tmp1, *tmp2; |
| 1066 | |
| 1067 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); |
| 1068 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); |
| 1069 | |
| 1070 | if (!tmp1 || !tmp2) { |
| 1071 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | goto abort; |
| 1073 | } |
| 1074 | |
| 1075 | *tmp1 = *sb1; |
| 1076 | *tmp2 = *sb2; |
| 1077 | |
| 1078 | /* |
| 1079 | * nr_disks is not constant |
| 1080 | */ |
| 1081 | tmp1->nr_disks = 0; |
| 1082 | tmp2->nr_disks = 0; |
| 1083 | |
Andre Noll | ce0c8e0 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 1084 | ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | abort: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 1086 | kfree(tmp1); |
| 1087 | kfree(tmp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1088 | return ret; |
| 1089 | } |
| 1090 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 1091 | static u32 md_csum_fold(u32 csum) |
| 1092 | { |
| 1093 | csum = (csum & 0xffff) + (csum >> 16); |
| 1094 | return (csum & 0xffff) + (csum >> 16); |
| 1095 | } |
| 1096 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1097 | static unsigned int calc_sb_csum(mdp_super_t *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1098 | { |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 1099 | u64 newcsum = 0; |
| 1100 | u32 *sb32 = (u32*)sb; |
| 1101 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | unsigned int disk_csum, csum; |
| 1103 | |
| 1104 | disk_csum = sb->sb_csum; |
| 1105 | sb->sb_csum = 0; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 1106 | |
| 1107 | for (i = 0; i < MD_SB_BYTES/4 ; i++) |
| 1108 | newcsum += sb32[i]; |
| 1109 | csum = (newcsum & 0xffffffff) + (newcsum>>32); |
| 1110 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 1111 | #ifdef CONFIG_ALPHA |
| 1112 | /* This used to use csum_partial, which was wrong for several |
| 1113 | * reasons including that different results are returned on |
| 1114 | * different architectures. It isn't critical that we get exactly |
| 1115 | * the same return value as before (we always csum_fold before |
| 1116 | * testing, and that removes any differences). However as we |
| 1117 | * know that csum_partial always returned a 16bit value on |
| 1118 | * alphas, do a fold to maximise conformity to previous behaviour. |
| 1119 | */ |
| 1120 | sb->sb_csum = md_csum_fold(disk_csum); |
| 1121 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | sb->sb_csum = disk_csum; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 1123 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1124 | return csum; |
| 1125 | } |
| 1126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | /* |
| 1128 | * Handle superblock details. |
| 1129 | * We want to be able to handle multiple superblock formats |
| 1130 | * so we have a common interface to them all, and an array of |
| 1131 | * different handlers. |
| 1132 | * We rely on user-space to write the initial superblock, and support |
| 1133 | * reading and updating of superblocks. |
| 1134 | * Interface methods are: |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1135 | * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | * loads and validates a superblock on dev. |
| 1137 | * if refdev != NULL, compare superblocks on both devices |
| 1138 | * Return: |
| 1139 | * 0 - dev has a superblock that is compatible with refdev |
| 1140 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 1141 | * so dev should be used as the refdev in future |
| 1142 | * -EINVAL superblock incompatible or invalid |
| 1143 | * -othererror e.g. -EIO |
| 1144 | * |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1145 | * int validate_super(struct mddev *mddev, struct md_rdev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1146 | * Verify that dev is acceptable into mddev. |
| 1147 | * The first time, mddev->raid_disks will be 0, and data from |
| 1148 | * dev should be merged in. Subsequent calls check that dev |
| 1149 | * is new enough. Return 0 or -EINVAL |
| 1150 | * |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1151 | * void sync_super(struct mddev *mddev, struct md_rdev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | * Update the superblock for rdev with data in mddev |
| 1153 | * This does not write to disc. |
| 1154 | * |
| 1155 | */ |
| 1156 | |
| 1157 | struct super_type { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1158 | char *name; |
| 1159 | struct module *owner; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1160 | int (*load_super)(struct md_rdev *rdev, |
| 1161 | struct md_rdev *refdev, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1162 | int minor_version); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1163 | int (*validate_super)(struct mddev *mddev, |
| 1164 | struct md_rdev *rdev); |
| 1165 | void (*sync_super)(struct mddev *mddev, |
| 1166 | struct md_rdev *rdev); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1167 | unsigned long long (*rdev_size_change)(struct md_rdev *rdev, |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1168 | sector_t num_sectors); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1169 | int (*allow_new_offset)(struct md_rdev *rdev, |
| 1170 | unsigned long long new_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | }; |
| 1172 | |
| 1173 | /* |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 1174 | * Check that the given mddev has no bitmap. |
| 1175 | * |
| 1176 | * This function is called from the run method of all personalities that do not |
| 1177 | * support bitmaps. It prints an error message and returns non-zero if mddev |
| 1178 | * has a bitmap. Otherwise, it returns 0. |
| 1179 | * |
| 1180 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1181 | int md_check_no_bitmap(struct mddev *mddev) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 1182 | { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1183 | if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 1184 | return 0; |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1185 | pr_warn("%s: bitmaps are not supported for %s\n", |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 1186 | mdname(mddev), mddev->pers->name); |
| 1187 | return 1; |
| 1188 | } |
| 1189 | EXPORT_SYMBOL(md_check_no_bitmap); |
| 1190 | |
| 1191 | /* |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1192 | * load_super for 0.90.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1193 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1194 | static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | { |
| 1196 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 1197 | mdp_super_t *sb; |
| 1198 | int ret; |
Yufen Yu | 228fc7d | 2019-10-30 18:47:02 +0800 | [diff] [blame] | 1199 | bool spare_disk = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | |
| 1201 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1202 | * Calculate the position of the superblock (512byte sectors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1203 | * it's at the end of the disk. |
| 1204 | * |
| 1205 | * It also happens to be a multiple of 4Kb. |
| 1206 | */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1207 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1208 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1209 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1210 | if (ret) |
| 1211 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | |
| 1213 | ret = -EINVAL; |
| 1214 | |
| 1215 | bdevname(rdev->bdev, b); |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1216 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1217 | |
| 1218 | if (sb->md_magic != MD_SB_MAGIC) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1219 | pr_warn("md: invalid raid superblock magic on %s\n", b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1220 | goto abort; |
| 1221 | } |
| 1222 | |
| 1223 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1224 | sb->minor_version < 90 || |
| 1225 | sb->minor_version > 91) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1226 | pr_warn("Bad version number %d.%d on %s\n", |
| 1227 | sb->major_version, sb->minor_version, b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | goto abort; |
| 1229 | } |
| 1230 | |
| 1231 | if (sb->raid_disks <= 0) |
| 1232 | goto abort; |
| 1233 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 1234 | if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1235 | pr_warn("md: invalid superblock checksum on %s\n", b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | goto abort; |
| 1237 | } |
| 1238 | |
| 1239 | rdev->preferred_minor = sb->md_minor; |
| 1240 | rdev->data_offset = 0; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1241 | rdev->new_data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1242 | rdev->sb_size = MD_SB_BYTES; |
NeilBrown | 9f2f383 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1243 | rdev->badblocks.shift = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | |
| 1245 | if (sb->level == LEVEL_MULTIPATH) |
| 1246 | rdev->desc_nr = -1; |
| 1247 | else |
| 1248 | rdev->desc_nr = sb->this_disk.number; |
| 1249 | |
Yufen Yu | 228fc7d | 2019-10-30 18:47:02 +0800 | [diff] [blame] | 1250 | /* not spare disk, or LEVEL_MULTIPATH */ |
| 1251 | if (sb->level == LEVEL_MULTIPATH || |
| 1252 | (rdev->desc_nr >= 0 && |
Yufen Yu | 3b7436c | 2019-12-10 15:01:29 +0800 | [diff] [blame] | 1253 | rdev->desc_nr < MD_SB_DISKS && |
Yufen Yu | 228fc7d | 2019-10-30 18:47:02 +0800 | [diff] [blame] | 1254 | sb->disks[rdev->desc_nr].state & |
| 1255 | ((1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))) |
| 1256 | spare_disk = false; |
| 1257 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1258 | if (!refdev) { |
Yufen Yu | 228fc7d | 2019-10-30 18:47:02 +0800 | [diff] [blame] | 1259 | if (!spare_disk) |
Yufen Yu | 6a5cb53 | 2019-10-16 16:00:03 +0800 | [diff] [blame] | 1260 | ret = 1; |
| 1261 | else |
| 1262 | ret = 0; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1263 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1265 | mdp_super_t *refsb = page_address(refdev->sb_page); |
Amir Goldstein | e6fd209 | 2017-05-04 16:26:20 +0300 | [diff] [blame] | 1266 | if (!md_uuid_equal(refsb, sb)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1267 | pr_warn("md: %s has different UUID to %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | b, bdevname(refdev->bdev,b2)); |
| 1269 | goto abort; |
| 1270 | } |
Amir Goldstein | e6fd209 | 2017-05-04 16:26:20 +0300 | [diff] [blame] | 1271 | if (!md_sb_equal(refsb, sb)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1272 | pr_warn("md: %s has same UUID but different superblock to %s\n", |
| 1273 | b, bdevname(refdev->bdev, b2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1274 | goto abort; |
| 1275 | } |
| 1276 | ev1 = md_event(sb); |
| 1277 | ev2 = md_event(refsb); |
Yufen Yu | 6a5cb53 | 2019-10-16 16:00:03 +0800 | [diff] [blame] | 1278 | |
Yufen Yu | 228fc7d | 2019-10-30 18:47:02 +0800 | [diff] [blame] | 1279 | if (!spare_disk && ev1 > ev2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1280 | ret = 1; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1281 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1282 | ret = 0; |
| 1283 | } |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 1284 | rdev->sectors = rdev->sb_start; |
NeilBrown | 667a531 | 2012-08-16 16:46:12 +1000 | [diff] [blame] | 1285 | /* Limit to 4TB as metadata cannot record more than that. |
| 1286 | * (not needed for Linear and RAID0 as metadata doesn't |
| 1287 | * record this size) |
| 1288 | */ |
Christoph Hellwig | 72deb45 | 2019-04-05 18:08:59 +0200 | [diff] [blame] | 1289 | if ((u64)rdev->sectors >= (2ULL << 32) && sb->level >= 1) |
Arnd Bergmann | 3312c95 | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 1290 | rdev->sectors = (sector_t)(2ULL << 32) - 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1292 | if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1293 | /* "this cannot possibly happen" ... */ |
| 1294 | ret = -EINVAL; |
| 1295 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | abort: |
| 1297 | return ret; |
| 1298 | } |
| 1299 | |
| 1300 | /* |
| 1301 | * validate_super for 0.90.0 |
| 1302 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1303 | static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | { |
| 1305 | mdp_disk_t *desc; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1306 | mdp_super_t *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1307 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1308 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1309 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1310 | clear_bit(Faulty, &rdev->flags); |
| 1311 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1312 | clear_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1313 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1314 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1315 | if (mddev->raid_disks == 0) { |
| 1316 | mddev->major_version = 0; |
| 1317 | mddev->minor_version = sb->minor_version; |
| 1318 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1319 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1320 | mddev->chunk_sectors = sb->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1321 | mddev->ctime = sb->ctime; |
| 1322 | mddev->utime = sb->utime; |
| 1323 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1324 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | mddev->layout = sb->layout; |
| 1326 | mddev->raid_disks = sb->raid_disks; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1327 | mddev->dev_sectors = ((sector_t)sb->size) * 2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1328 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1329 | mddev->bitmap_info.offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1330 | mddev->bitmap_info.space = 0; |
| 1331 | /* bitmap can use 60 K after the 4K superblocks */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1332 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1333 | mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1334 | mddev->reshape_backwards = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1336 | if (mddev->minor_version >= 91) { |
| 1337 | mddev->reshape_position = sb->reshape_position; |
| 1338 | mddev->delta_disks = sb->delta_disks; |
| 1339 | mddev->new_level = sb->new_level; |
| 1340 | mddev->new_layout = sb->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1341 | mddev->new_chunk_sectors = sb->new_chunk >> 9; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1342 | if (mddev->delta_disks < 0) |
| 1343 | mddev->reshape_backwards = 1; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1344 | } else { |
| 1345 | mddev->reshape_position = MaxSector; |
| 1346 | mddev->delta_disks = 0; |
| 1347 | mddev->new_level = mddev->level; |
| 1348 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1349 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1350 | } |
NeilBrown | 33f2c35 | 2019-09-09 16:52:29 +1000 | [diff] [blame] | 1351 | if (mddev->level == 0) |
| 1352 | mddev->layout = -1; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1353 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1354 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 1355 | mddev->recovery_cp = MaxSector; |
| 1356 | else { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1357 | if (sb->events_hi == sb->cp_events_hi && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1358 | sb->events_lo == sb->cp_events_lo) { |
| 1359 | mddev->recovery_cp = sb->recovery_cp; |
| 1360 | } else |
| 1361 | mddev->recovery_cp = 0; |
| 1362 | } |
| 1363 | |
| 1364 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 1365 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 1366 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 1367 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 1368 | |
| 1369 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1370 | |
| 1371 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1372 | mddev->bitmap_info.file == NULL) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1373 | mddev->bitmap_info.offset = |
| 1374 | mddev->bitmap_info.default_offset; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1375 | mddev->bitmap_info.space = |
Dave Jones | c9ad020 | 2013-08-19 22:26:32 -0400 | [diff] [blame] | 1376 | mddev->bitmap_info.default_space; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1377 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1378 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1379 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1380 | /* Insist on good event counter while assembling, except |
| 1381 | * for spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1383 | if (sb->disks[rdev->desc_nr].state & ( |
| 1384 | (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE))) |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1385 | if (ev1 < mddev->events) |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1386 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1387 | } else if (mddev->bitmap) { |
| 1388 | /* if adding to array with a bitmap, then we can accept an |
| 1389 | * older device ... but not too old. |
| 1390 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1391 | if (ev1 < mddev->bitmap->events_cleared) |
| 1392 | return 0; |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1393 | if (ev1 < mddev->events) |
| 1394 | set_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1395 | } else { |
| 1396 | if (ev1 < mddev->events) |
| 1397 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1398 | return 0; |
| 1399 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1400 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | desc = sb->disks + rdev->desc_nr; |
| 1403 | |
| 1404 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1405 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 1406 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 1407 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1408 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | rdev->raid_disk = desc->raid_disk; |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1410 | rdev->saved_raid_disk = desc->raid_disk; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1411 | } else if (desc->state & (1<<MD_DISK_ACTIVE)) { |
| 1412 | /* active but not in sync implies recovery up to |
| 1413 | * reshape position. We don't know exactly where |
| 1414 | * that is, so set to zero for now */ |
| 1415 | if (mddev->minor_version >= 91) { |
| 1416 | rdev->recovery_offset = 0; |
| 1417 | rdev->raid_disk = desc->raid_disk; |
| 1418 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1419 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1420 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 1421 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 1422 | if (desc->state & (1<<MD_DISK_FAILFAST)) |
| 1423 | set_bit(FailFast, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1424 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1425 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1426 | return 0; |
| 1427 | } |
| 1428 | |
| 1429 | /* |
| 1430 | * sync_super for 0.90.0 |
| 1431 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1432 | static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1433 | { |
| 1434 | mdp_super_t *sb; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1435 | struct md_rdev *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1436 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1437 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1438 | /* make rdev->sb match mddev data.. |
| 1439 | * |
| 1440 | * 1/ zero out disks |
| 1441 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 1442 | * 3/ any empty disks < next_spare become removed |
| 1443 | * |
| 1444 | * disks[0] gets initialised to REMOVED because |
| 1445 | * we cannot be sure from other fields if it has |
| 1446 | * been initialised or not. |
| 1447 | */ |
| 1448 | int i; |
| 1449 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 1450 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 1451 | rdev->sb_size = MD_SB_BYTES; |
| 1452 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1453 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1454 | |
| 1455 | memset(sb, 0, sizeof(*sb)); |
| 1456 | |
| 1457 | sb->md_magic = MD_SB_MAGIC; |
| 1458 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1459 | sb->patch_version = mddev->patch_version; |
| 1460 | sb->gvalid_words = 0; /* ignored */ |
| 1461 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 1462 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 1463 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 1464 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 1465 | |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 1466 | sb->ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1467 | sb->level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1468 | sb->size = mddev->dev_sectors / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1469 | sb->raid_disks = mddev->raid_disks; |
| 1470 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1471 | sb->not_persistent = 0; |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 1472 | sb->utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1473 | sb->state = 0; |
| 1474 | sb->events_hi = (mddev->events>>32); |
| 1475 | sb->events_lo = (u32)mddev->events; |
| 1476 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1477 | if (mddev->reshape_position == MaxSector) |
| 1478 | sb->minor_version = 90; |
| 1479 | else { |
| 1480 | sb->minor_version = 91; |
| 1481 | sb->reshape_position = mddev->reshape_position; |
| 1482 | sb->new_level = mddev->new_level; |
| 1483 | sb->delta_disks = mddev->delta_disks; |
| 1484 | sb->new_layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1485 | sb->new_chunk = mddev->new_chunk_sectors << 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1486 | } |
| 1487 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1488 | if (mddev->in_sync) |
| 1489 | { |
| 1490 | sb->recovery_cp = mddev->recovery_cp; |
| 1491 | sb->cp_events_hi = (mddev->events>>32); |
| 1492 | sb->cp_events_lo = (u32)mddev->events; |
| 1493 | if (mddev->recovery_cp == MaxSector) |
| 1494 | sb->state = (1<< MD_SB_CLEAN); |
| 1495 | } else |
| 1496 | sb->recovery_cp = 0; |
| 1497 | |
| 1498 | sb->layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1499 | sb->chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1500 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1501 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1502 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 1503 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1505 | rdev_for_each(rdev2, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1506 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1507 | int desc_nr; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1508 | int is_active = test_bit(In_sync, &rdev2->flags); |
| 1509 | |
| 1510 | if (rdev2->raid_disk >= 0 && |
| 1511 | sb->minor_version >= 91) |
| 1512 | /* we have nowhere to store the recovery_offset, |
| 1513 | * but if it is not below the reshape_position, |
| 1514 | * we can piggy-back on that. |
| 1515 | */ |
| 1516 | is_active = 1; |
| 1517 | if (rdev2->raid_disk < 0 || |
| 1518 | test_bit(Faulty, &rdev2->flags)) |
| 1519 | is_active = 0; |
| 1520 | if (is_active) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1521 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1522 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1523 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1524 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1525 | d = &sb->disks[rdev2->desc_nr]; |
| 1526 | nr_disks++; |
| 1527 | d->number = rdev2->desc_nr; |
| 1528 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 1529 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1530 | if (is_active) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1531 | d->raid_disk = rdev2->raid_disk; |
| 1532 | else |
| 1533 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 1534 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1535 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1536 | else if (is_active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1537 | d->state = (1<<MD_DISK_ACTIVE); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1538 | if (test_bit(In_sync, &rdev2->flags)) |
| 1539 | d->state |= (1<<MD_DISK_SYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1540 | active++; |
| 1541 | working++; |
| 1542 | } else { |
| 1543 | d->state = 0; |
| 1544 | spare++; |
| 1545 | working++; |
| 1546 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1547 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 1548 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 1549 | if (test_bit(FailFast, &rdev2->flags)) |
| 1550 | d->state |= (1<<MD_DISK_FAILFAST); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1552 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 1553 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 1554 | mdp_disk_t *d = &sb->disks[i]; |
| 1555 | if (d->state == 0 && d->number == 0) { |
| 1556 | d->number = i; |
| 1557 | d->raid_disk = i; |
| 1558 | d->state = (1<<MD_DISK_REMOVED); |
| 1559 | d->state |= (1<<MD_DISK_FAULTY); |
| 1560 | failed++; |
| 1561 | } |
| 1562 | } |
| 1563 | sb->nr_disks = nr_disks; |
| 1564 | sb->active_disks = active; |
| 1565 | sb->working_disks = working; |
| 1566 | sb->failed_disks = failed; |
| 1567 | sb->spare_disks = spare; |
| 1568 | |
| 1569 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 1570 | sb->sb_csum = calc_sb_csum(sb); |
| 1571 | } |
| 1572 | |
| 1573 | /* |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1574 | * rdev_size_change for 0.90.0 |
| 1575 | */ |
| 1576 | static unsigned long long |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1577 | super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1578 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1579 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1580 | return 0; /* component must fit device */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1581 | if (rdev->mddev->bitmap_info.offset) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1582 | return 0; /* can't move bitmap */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1583 | rdev->sb_start = calc_dev_sboffset(rdev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1584 | if (!num_sectors || num_sectors > rdev->sb_start) |
| 1585 | num_sectors = rdev->sb_start; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1586 | /* Limit to 4TB as metadata cannot record more than that. |
| 1587 | * 4TB == 2^32 KB, or 2*2^32 sectors. |
| 1588 | */ |
Christoph Hellwig | 72deb45 | 2019-04-05 18:08:59 +0200 | [diff] [blame] | 1589 | if ((u64)num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1) |
Arnd Bergmann | 3312c95 | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 1590 | num_sectors = (sector_t)(2ULL << 32) - 2; |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 1591 | do { |
| 1592 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1593 | rdev->sb_page); |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 1594 | } while (md_super_wait(rdev->mddev) < 0); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1595 | return num_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1596 | } |
| 1597 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1598 | static int |
| 1599 | super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset) |
| 1600 | { |
| 1601 | /* non-zero offset changes not possible with v0.90 */ |
| 1602 | return new_offset == 0; |
| 1603 | } |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1604 | |
| 1605 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1606 | * version 1 superblock |
| 1607 | */ |
| 1608 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1609 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1610 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1611 | __le32 disk_csum; |
| 1612 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1613 | unsigned long long newcsum; |
| 1614 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1615 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1616 | |
| 1617 | disk_csum = sb->sb_csum; |
| 1618 | sb->sb_csum = 0; |
| 1619 | newcsum = 0; |
NeilBrown | 1f3c990 | 2012-12-11 13:09:00 +1100 | [diff] [blame] | 1620 | for (; size >= 4; size -= 4) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1621 | newcsum += le32_to_cpu(*isuper++); |
| 1622 | |
| 1623 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1624 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1625 | |
| 1626 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1627 | sb->sb_csum = disk_csum; |
| 1628 | return cpu_to_le32(csum); |
| 1629 | } |
| 1630 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1631 | static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1632 | { |
| 1633 | struct mdp_superblock_1 *sb; |
| 1634 | int ret; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1635 | sector_t sb_start; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1636 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1637 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1638 | int bmask; |
Yufen Yu | 228fc7d | 2019-10-30 18:47:02 +0800 | [diff] [blame] | 1639 | bool spare_disk = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1640 | |
| 1641 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1642 | * Calculate the position of the superblock in 512byte sectors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1643 | * It is always aligned to a 4K boundary and |
| 1644 | * depeding on minor_version, it can be: |
| 1645 | * 0: At least 8K, but less than 12K, from end of device |
| 1646 | * 1: At start of device |
| 1647 | * 2: 4K from start of device. |
| 1648 | */ |
| 1649 | switch(minor_version) { |
| 1650 | case 0: |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1651 | sb_start = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1652 | sb_start -= 8*2; |
| 1653 | sb_start &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1654 | break; |
| 1655 | case 1: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1656 | sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1657 | break; |
| 1658 | case 2: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1659 | sb_start = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1660 | break; |
| 1661 | default: |
| 1662 | return -EINVAL; |
| 1663 | } |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1664 | rdev->sb_start = sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1665 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1666 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1667 | * and it is safe to read 4k, so we do that |
| 1668 | */ |
| 1669 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1670 | if (ret) return ret; |
| 1671 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1672 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1673 | |
| 1674 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1675 | sb->major_version != cpu_to_le32(1) || |
| 1676 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1677 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1678 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1679 | return -EINVAL; |
| 1680 | |
| 1681 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1682 | pr_warn("md: invalid superblock checksum on %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1683 | bdevname(rdev->bdev,b)); |
| 1684 | return -EINVAL; |
| 1685 | } |
| 1686 | if (le64_to_cpu(sb->data_size) < 10) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1687 | pr_warn("md: data_size too small on %s\n", |
| 1688 | bdevname(rdev->bdev,b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1689 | return -EINVAL; |
| 1690 | } |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1691 | if (sb->pad0 || |
| 1692 | sb->pad3[0] || |
| 1693 | memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1]))) |
| 1694 | /* Some padding is non-zero, might be a new feature */ |
| 1695 | return -EINVAL; |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1696 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1697 | rdev->preferred_minor = 0xffff; |
| 1698 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1699 | rdev->new_data_offset = rdev->data_offset; |
| 1700 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) && |
| 1701 | (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET)) |
| 1702 | rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1703 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1704 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1705 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1706 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1707 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1708 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1709 | |
| 1710 | if (minor_version |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1711 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1712 | return -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1713 | if (minor_version |
| 1714 | && rdev->new_data_offset < sb_start + (rdev->sb_size/512)) |
| 1715 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1716 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1717 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1718 | rdev->desc_nr = -1; |
| 1719 | else |
| 1720 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1721 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1722 | if (!rdev->bb_page) { |
| 1723 | rdev->bb_page = alloc_page(GFP_KERNEL); |
| 1724 | if (!rdev->bb_page) |
| 1725 | return -ENOMEM; |
| 1726 | } |
| 1727 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) && |
| 1728 | rdev->badblocks.count == 0) { |
| 1729 | /* need to load the bad block list. |
| 1730 | * Currently we limit it to one page. |
| 1731 | */ |
| 1732 | s32 offset; |
| 1733 | sector_t bb_sector; |
Christoph Hellwig | 00485d0 | 2019-04-04 18:56:12 +0200 | [diff] [blame] | 1734 | __le64 *bbp; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1735 | int i; |
| 1736 | int sectors = le16_to_cpu(sb->bblog_size); |
| 1737 | if (sectors > (PAGE_SIZE / 512)) |
| 1738 | return -EINVAL; |
| 1739 | offset = le32_to_cpu(sb->bblog_offset); |
| 1740 | if (offset == 0) |
| 1741 | return -EINVAL; |
| 1742 | bb_sector = (long long)offset; |
| 1743 | if (!sync_page_io(rdev, bb_sector, sectors << 9, |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1744 | rdev->bb_page, REQ_OP_READ, 0, true)) |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1745 | return -EIO; |
Christoph Hellwig | 00485d0 | 2019-04-04 18:56:12 +0200 | [diff] [blame] | 1746 | bbp = (__le64 *)page_address(rdev->bb_page); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1747 | rdev->badblocks.shift = sb->bblog_shift; |
| 1748 | for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) { |
| 1749 | u64 bb = le64_to_cpu(*bbp); |
| 1750 | int count = bb & (0x3ff); |
| 1751 | u64 sector = bb >> 10; |
| 1752 | sector <<= sb->bblog_shift; |
| 1753 | count <<= sb->bblog_shift; |
| 1754 | if (bb + 1 == 0) |
| 1755 | break; |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 1756 | if (badblocks_set(&rdev->badblocks, sector, count, 1)) |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1757 | return -EINVAL; |
| 1758 | } |
NeilBrown | 486adf7 | 2013-04-24 11:42:44 +1000 | [diff] [blame] | 1759 | } else if (sb->bblog_offset != 0) |
| 1760 | rdev->badblocks.shift = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1761 | |
Pawel Baldysiak | ddc0882 | 2017-08-16 17:13:45 +0200 | [diff] [blame] | 1762 | if ((le32_to_cpu(sb->feature_map) & |
| 1763 | (MD_FEATURE_PPL | MD_FEATURE_MULTIPLE_PPLS))) { |
Artur Paszkiewicz | ea0213e | 2017-03-09 09:59:57 +0100 | [diff] [blame] | 1764 | rdev->ppl.offset = (__s16)le16_to_cpu(sb->ppl.offset); |
| 1765 | rdev->ppl.size = le16_to_cpu(sb->ppl.size); |
| 1766 | rdev->ppl.sector = rdev->sb_start + rdev->ppl.offset; |
| 1767 | } |
| 1768 | |
NeilBrown | 33f2c35 | 2019-09-09 16:52:29 +1000 | [diff] [blame] | 1769 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RAID0_LAYOUT) && |
| 1770 | sb->level != 0) |
| 1771 | return -EINVAL; |
| 1772 | |
Yufen Yu | 228fc7d | 2019-10-30 18:47:02 +0800 | [diff] [blame] | 1773 | /* not spare disk, or LEVEL_MULTIPATH */ |
| 1774 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH) || |
| 1775 | (rdev->desc_nr >= 0 && |
| 1776 | rdev->desc_nr < le32_to_cpu(sb->max_dev) && |
| 1777 | (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX || |
| 1778 | le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL))) |
| 1779 | spare_disk = false; |
Yufen Yu | 6a5cb53 | 2019-10-16 16:00:03 +0800 | [diff] [blame] | 1780 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1781 | if (!refdev) { |
Yufen Yu | 228fc7d | 2019-10-30 18:47:02 +0800 | [diff] [blame] | 1782 | if (!spare_disk) |
Yufen Yu | 6a5cb53 | 2019-10-16 16:00:03 +0800 | [diff] [blame] | 1783 | ret = 1; |
| 1784 | else |
| 1785 | ret = 0; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1786 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1787 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1788 | struct mdp_superblock_1 *refsb = page_address(refdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1789 | |
| 1790 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1791 | sb->level != refsb->level || |
| 1792 | sb->layout != refsb->layout || |
| 1793 | sb->chunksize != refsb->chunksize) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1794 | pr_warn("md: %s has strangely different superblock to %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1795 | bdevname(rdev->bdev,b), |
| 1796 | bdevname(refdev->bdev,b2)); |
| 1797 | return -EINVAL; |
| 1798 | } |
| 1799 | ev1 = le64_to_cpu(sb->events); |
| 1800 | ev2 = le64_to_cpu(refsb->events); |
| 1801 | |
Yufen Yu | 228fc7d | 2019-10-30 18:47:02 +0800 | [diff] [blame] | 1802 | if (!spare_disk && ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1803 | ret = 1; |
| 1804 | else |
| 1805 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1806 | } |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1807 | if (minor_version) { |
| 1808 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9); |
| 1809 | sectors -= rdev->data_offset; |
| 1810 | } else |
| 1811 | sectors = rdev->sb_start; |
| 1812 | if (sectors < le64_to_cpu(sb->data_size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1813 | return -EINVAL; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1814 | rdev->sectors = le64_to_cpu(sb->data_size); |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1815 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1816 | } |
| 1817 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1818 | static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1819 | { |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1820 | struct mdp_superblock_1 *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1821 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1822 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1823 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1824 | clear_bit(Faulty, &rdev->flags); |
| 1825 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1826 | clear_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1827 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1828 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1829 | if (mddev->raid_disks == 0) { |
| 1830 | mddev->major_version = 1; |
| 1831 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1832 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1833 | mddev->chunk_sectors = le32_to_cpu(sb->chunksize); |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 1834 | mddev->ctime = le64_to_cpu(sb->ctime); |
| 1835 | mddev->utime = le64_to_cpu(sb->utime); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1836 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1837 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1838 | mddev->layout = le32_to_cpu(sb->layout); |
| 1839 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1840 | mddev->dev_sectors = le64_to_cpu(sb->size); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1841 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1842 | mddev->bitmap_info.offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1843 | mddev->bitmap_info.space = 0; |
| 1844 | /* Default location for bitmap is 1K after superblock |
| 1845 | * using 3K - total of 4K |
| 1846 | */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1847 | mddev->bitmap_info.default_offset = 1024 >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1848 | mddev->bitmap_info.default_space = (4096-1024) >> 9; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1849 | mddev->reshape_backwards = 0; |
| 1850 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1851 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1852 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1853 | |
| 1854 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1855 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1856 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1857 | mddev->bitmap_info.file == NULL) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1858 | mddev->bitmap_info.offset = |
| 1859 | (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1860 | /* Metadata doesn't record how much space is available. |
| 1861 | * For 1.0, we assume we can use up to the superblock |
| 1862 | * if before, else to 4K beyond superblock. |
| 1863 | * For others, assume no change is possible. |
| 1864 | */ |
| 1865 | if (mddev->minor_version > 0) |
| 1866 | mddev->bitmap_info.space = 0; |
| 1867 | else if (mddev->bitmap_info.offset > 0) |
| 1868 | mddev->bitmap_info.space = |
| 1869 | 8 - mddev->bitmap_info.offset; |
| 1870 | else |
| 1871 | mddev->bitmap_info.space = |
| 1872 | -mddev->bitmap_info.offset; |
| 1873 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1874 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1875 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1876 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1877 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1878 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1879 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1880 | mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1881 | if (mddev->delta_disks < 0 || |
| 1882 | (mddev->delta_disks == 0 && |
| 1883 | (le32_to_cpu(sb->feature_map) |
| 1884 | & MD_FEATURE_RESHAPE_BACKWARDS))) |
| 1885 | mddev->reshape_backwards = 1; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1886 | } else { |
| 1887 | mddev->reshape_position = MaxSector; |
| 1888 | mddev->delta_disks = 0; |
| 1889 | mddev->new_level = mddev->level; |
| 1890 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1891 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1892 | } |
| 1893 | |
NeilBrown | 33f2c35 | 2019-09-09 16:52:29 +1000 | [diff] [blame] | 1894 | if (mddev->level == 0 && |
| 1895 | !(le32_to_cpu(sb->feature_map) & MD_FEATURE_RAID0_LAYOUT)) |
| 1896 | mddev->layout = -1; |
| 1897 | |
Song Liu | 486b0f7 | 2016-08-19 15:34:01 -0700 | [diff] [blame] | 1898 | if (le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL) |
Shaohua Li | a62ab49 | 2016-01-06 14:37:13 -0800 | [diff] [blame] | 1899 | set_bit(MD_HAS_JOURNAL, &mddev->flags); |
Artur Paszkiewicz | ea0213e | 2017-03-09 09:59:57 +0100 | [diff] [blame] | 1900 | |
Pawel Baldysiak | ddc0882 | 2017-08-16 17:13:45 +0200 | [diff] [blame] | 1901 | if (le32_to_cpu(sb->feature_map) & |
| 1902 | (MD_FEATURE_PPL | MD_FEATURE_MULTIPLE_PPLS)) { |
Artur Paszkiewicz | ea0213e | 2017-03-09 09:59:57 +0100 | [diff] [blame] | 1903 | if (le32_to_cpu(sb->feature_map) & |
| 1904 | (MD_FEATURE_BITMAP_OFFSET | MD_FEATURE_JOURNAL)) |
| 1905 | return -EINVAL; |
Pawel Baldysiak | ddc0882 | 2017-08-16 17:13:45 +0200 | [diff] [blame] | 1906 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_PPL) && |
| 1907 | (le32_to_cpu(sb->feature_map) & |
| 1908 | MD_FEATURE_MULTIPLE_PPLS)) |
| 1909 | return -EINVAL; |
Artur Paszkiewicz | ea0213e | 2017-03-09 09:59:57 +0100 | [diff] [blame] | 1910 | set_bit(MD_HAS_PPL, &mddev->flags); |
| 1911 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1912 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1913 | /* Insist of good event counter while assembling, except for |
| 1914 | * spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1915 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1916 | if (rdev->desc_nr >= 0 && |
| 1917 | rdev->desc_nr < le32_to_cpu(sb->max_dev) && |
Song Liu | a3dfbda | 2015-10-08 21:54:11 -0700 | [diff] [blame] | 1918 | (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX || |
| 1919 | le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL)) |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1920 | if (ev1 < mddev->events) |
| 1921 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1922 | } else if (mddev->bitmap) { |
| 1923 | /* If adding to array with a bitmap, then we can accept an |
| 1924 | * older device, but not too old. |
| 1925 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1926 | if (ev1 < mddev->bitmap->events_cleared) |
| 1927 | return 0; |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1928 | if (ev1 < mddev->events) |
| 1929 | set_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1930 | } else { |
| 1931 | if (ev1 < mddev->events) |
| 1932 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1933 | return 0; |
| 1934 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1935 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1936 | int role; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1937 | if (rdev->desc_nr < 0 || |
| 1938 | rdev->desc_nr >= le32_to_cpu(sb->max_dev)) { |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1939 | role = MD_DISK_ROLE_SPARE; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1940 | rdev->desc_nr = -1; |
| 1941 | } else |
| 1942 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1943 | switch(role) { |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1944 | case MD_DISK_ROLE_SPARE: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1945 | break; |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1946 | case MD_DISK_ROLE_FAULTY: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1947 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1948 | break; |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 1949 | case MD_DISK_ROLE_JOURNAL: /* journal device */ |
| 1950 | if (!(le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)) { |
| 1951 | /* journal device without journal feature */ |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1952 | pr_warn("md: journal device provided without journal feature, ignoring the device\n"); |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 1953 | return -EINVAL; |
| 1954 | } |
| 1955 | set_bit(Journal, &rdev->flags); |
Shaohua Li | 3069aa8 | 2015-08-13 14:31:56 -0700 | [diff] [blame] | 1956 | rdev->journal_tail = le64_to_cpu(sb->journal_tail); |
Shaohua Li | 9b15603 | 2015-12-18 15:19:16 +1100 | [diff] [blame] | 1957 | rdev->raid_disk = 0; |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 1958 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1959 | default: |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1960 | rdev->saved_raid_disk = role; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1961 | if ((le32_to_cpu(sb->feature_map) & |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1962 | MD_FEATURE_RECOVERY_OFFSET)) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1963 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1964 | if (!(le32_to_cpu(sb->feature_map) & |
| 1965 | MD_FEATURE_RECOVERY_BITMAP)) |
| 1966 | rdev->saved_raid_disk = -1; |
Guoqing Jiang | 062f5b2a | 2019-07-24 11:09:20 +0200 | [diff] [blame] | 1967 | } else { |
| 1968 | /* |
| 1969 | * If the array is FROZEN, then the device can't |
| 1970 | * be in_sync with rest of array. |
| 1971 | */ |
| 1972 | if (!test_bit(MD_RECOVERY_FROZEN, |
| 1973 | &mddev->recovery)) |
| 1974 | set_bit(In_sync, &rdev->flags); |
| 1975 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1976 | rdev->raid_disk = role; |
| 1977 | break; |
| 1978 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1979 | if (sb->devflags & WriteMostly1) |
| 1980 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 1981 | if (sb->devflags & FailFast1) |
| 1982 | set_bit(FailFast, &rdev->flags); |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 1983 | if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT) |
| 1984 | set_bit(Replacement, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1985 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1986 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1987 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1988 | return 0; |
| 1989 | } |
| 1990 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1991 | static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1992 | { |
| 1993 | struct mdp_superblock_1 *sb; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1994 | struct md_rdev *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1995 | int max_dev, i; |
| 1996 | /* make rdev->sb match mddev and rdev data. */ |
| 1997 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1998 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1999 | |
| 2000 | sb->feature_map = 0; |
| 2001 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 2002 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2003 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 2004 | |
| 2005 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 2006 | sb->events = cpu_to_le64(mddev->events); |
| 2007 | if (mddev->in_sync) |
| 2008 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
Shaohua Li | bd18f64 | 2015-09-02 13:49:50 -0700 | [diff] [blame] | 2009 | else if (test_bit(MD_JOURNAL_CLEAN, &mddev->flags)) |
| 2010 | sb->resync_offset = cpu_to_le64(MaxSector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2011 | else |
| 2012 | sb->resync_offset = cpu_to_le64(0); |
| 2013 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 2014 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2015 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 2016 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2017 | sb->size = cpu_to_le64(mddev->dev_sectors); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 2018 | sb->chunksize = cpu_to_le32(mddev->chunk_sectors); |
NeilBrown | 62e1e38 | 2009-05-26 09:40:59 +1000 | [diff] [blame] | 2019 | sb->level = cpu_to_le32(mddev->level); |
| 2020 | sb->layout = cpu_to_le32(mddev->layout); |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 2021 | if (test_bit(FailFast, &rdev->flags)) |
| 2022 | sb->devflags |= FailFast1; |
| 2023 | else |
| 2024 | sb->devflags &= ~FailFast1; |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 2025 | |
NeilBrown | aeb9b211 | 2011-08-25 14:43:08 +1000 | [diff] [blame] | 2026 | if (test_bit(WriteMostly, &rdev->flags)) |
| 2027 | sb->devflags |= WriteMostly1; |
| 2028 | else |
| 2029 | sb->devflags &= ~WriteMostly1; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2030 | sb->data_offset = cpu_to_le64(rdev->data_offset); |
| 2031 | sb->data_size = cpu_to_le64(rdev->sectors); |
NeilBrown | aeb9b211 | 2011-08-25 14:43:08 +1000 | [diff] [blame] | 2032 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 2033 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) { |
| 2034 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 2035 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 2036 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 2037 | |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2038 | if (rdev->raid_disk >= 0 && !test_bit(Journal, &rdev->flags) && |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2039 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 2040 | sb->feature_map |= |
| 2041 | cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 2042 | sb->recovery_offset = |
| 2043 | cpu_to_le64(rdev->recovery_offset); |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2044 | if (rdev->saved_raid_disk >= 0 && mddev->bitmap) |
| 2045 | sb->feature_map |= |
| 2046 | cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 2047 | } |
Shaohua Li | 3069aa8 | 2015-08-13 14:31:56 -0700 | [diff] [blame] | 2048 | /* Note: recovery_offset and journal_tail share space */ |
| 2049 | if (test_bit(Journal, &rdev->flags)) |
| 2050 | sb->journal_tail = cpu_to_le64(rdev->journal_tail); |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2051 | if (test_bit(Replacement, &rdev->flags)) |
| 2052 | sb->feature_map |= |
| 2053 | cpu_to_le32(MD_FEATURE_REPLACEMENT); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 2054 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 2055 | if (mddev->reshape_position != MaxSector) { |
| 2056 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 2057 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 2058 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 2059 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 2060 | sb->new_level = cpu_to_le32(mddev->new_level); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 2061 | sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2062 | if (mddev->delta_disks == 0 && |
| 2063 | mddev->reshape_backwards) |
| 2064 | sb->feature_map |
| 2065 | |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2066 | if (rdev->new_data_offset != rdev->data_offset) { |
| 2067 | sb->feature_map |
| 2068 | |= cpu_to_le32(MD_FEATURE_NEW_OFFSET); |
| 2069 | sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset |
| 2070 | - rdev->data_offset)); |
| 2071 | } |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 2072 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 2073 | |
Goldwyn Rodrigues | 3c462c8 | 2015-08-19 07:35:54 +1000 | [diff] [blame] | 2074 | if (mddev_is_clustered(mddev)) |
| 2075 | sb->feature_map |= cpu_to_le32(MD_FEATURE_CLUSTERED); |
| 2076 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2077 | if (rdev->badblocks.count == 0) |
| 2078 | /* Nothing to do for bad blocks*/ ; |
| 2079 | else if (sb->bblog_offset == 0) |
| 2080 | /* Cannot record bad blocks on this device */ |
| 2081 | md_error(mddev, rdev); |
| 2082 | else { |
| 2083 | struct badblocks *bb = &rdev->badblocks; |
Christoph Hellwig | ae50640 | 2019-04-04 18:56:13 +0200 | [diff] [blame] | 2084 | __le64 *bbp = (__le64 *)page_address(rdev->bb_page); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2085 | u64 *p = bb->page; |
| 2086 | sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS); |
| 2087 | if (bb->changed) { |
| 2088 | unsigned seq; |
| 2089 | |
| 2090 | retry: |
| 2091 | seq = read_seqbegin(&bb->lock); |
| 2092 | |
| 2093 | memset(bbp, 0xff, PAGE_SIZE); |
| 2094 | |
| 2095 | for (i = 0 ; i < bb->count ; i++) { |
majianpeng | 35f9ac2 | 2012-11-08 08:56:27 +0800 | [diff] [blame] | 2096 | u64 internal_bb = p[i]; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2097 | u64 store_bb = ((BB_OFFSET(internal_bb) << 10) |
| 2098 | | BB_LEN(internal_bb)); |
majianpeng | 35f9ac2 | 2012-11-08 08:56:27 +0800 | [diff] [blame] | 2099 | bbp[i] = cpu_to_le64(store_bb); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2100 | } |
NeilBrown | d096293 | 2012-03-19 12:46:41 +1100 | [diff] [blame] | 2101 | bb->changed = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2102 | if (read_seqretry(&bb->lock, seq)) |
| 2103 | goto retry; |
| 2104 | |
| 2105 | bb->sector = (rdev->sb_start + |
| 2106 | (int)le32_to_cpu(sb->bblog_offset)); |
| 2107 | bb->size = le16_to_cpu(sb->bblog_size); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2108 | } |
| 2109 | } |
| 2110 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2111 | max_dev = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2112 | rdev_for_each(rdev2, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2113 | if (rdev2->desc_nr+1 > max_dev) |
| 2114 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2115 | |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 2116 | if (max_dev > le32_to_cpu(sb->max_dev)) { |
| 2117 | int bmask; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2118 | sb->max_dev = cpu_to_le32(max_dev); |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 2119 | rdev->sb_size = max_dev * 2 + 256; |
| 2120 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
| 2121 | if (rdev->sb_size & bmask) |
| 2122 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
NeilBrown | ddcf352 | 2010-09-08 16:48:17 +1000 | [diff] [blame] | 2123 | } else |
| 2124 | max_dev = le32_to_cpu(sb->max_dev); |
| 2125 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2126 | for (i=0; i<max_dev;i++) |
Lidong Zhong | 8df7202 | 2017-06-12 10:45:55 +0800 | [diff] [blame] | 2127 | sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2128 | |
Song Liu | a97b789 | 2015-10-08 21:54:09 -0700 | [diff] [blame] | 2129 | if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) |
| 2130 | sb->feature_map |= cpu_to_le32(MD_FEATURE_JOURNAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2131 | |
Artur Paszkiewicz | ea0213e | 2017-03-09 09:59:57 +0100 | [diff] [blame] | 2132 | if (test_bit(MD_HAS_PPL, &mddev->flags)) { |
Pawel Baldysiak | ddc0882 | 2017-08-16 17:13:45 +0200 | [diff] [blame] | 2133 | if (test_bit(MD_HAS_MULTIPLE_PPLS, &mddev->flags)) |
| 2134 | sb->feature_map |= |
| 2135 | cpu_to_le32(MD_FEATURE_MULTIPLE_PPLS); |
| 2136 | else |
| 2137 | sb->feature_map |= cpu_to_le32(MD_FEATURE_PPL); |
Artur Paszkiewicz | ea0213e | 2017-03-09 09:59:57 +0100 | [diff] [blame] | 2138 | sb->ppl.offset = cpu_to_le16(rdev->ppl.offset); |
| 2139 | sb->ppl.size = cpu_to_le16(rdev->ppl.size); |
| 2140 | } |
| 2141 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2142 | rdev_for_each(rdev2, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2143 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2144 | if (test_bit(Faulty, &rdev2->flags)) |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 2145 | sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2146 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2147 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Song Liu | a97b789 | 2015-10-08 21:54:09 -0700 | [diff] [blame] | 2148 | else if (test_bit(Journal, &rdev2->flags)) |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 2149 | sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_JOURNAL); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 2150 | else if (rdev2->raid_disk >= 0) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 2151 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2152 | else |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 2153 | sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2154 | } |
| 2155 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2156 | sb->sb_csum = calc_sb_1_csum(sb); |
| 2157 | } |
| 2158 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2159 | static unsigned long long |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2160 | super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2161 | { |
| 2162 | struct mdp_superblock_1 *sb; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 2163 | sector_t max_sectors; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2164 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2165 | return 0; /* component must fit device */ |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2166 | if (rdev->data_offset != rdev->new_data_offset) |
| 2167 | return 0; /* too confusing */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2168 | if (rdev->sb_start < rdev->data_offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2169 | /* minor versions 1 and 2; superblock before data */ |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 2170 | max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 2171 | max_sectors -= rdev->data_offset; |
| 2172 | if (!num_sectors || num_sectors > max_sectors) |
| 2173 | num_sectors = max_sectors; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 2174 | } else if (rdev->mddev->bitmap_info.offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2175 | /* minor version 0 with bitmap we can't move */ |
| 2176 | return 0; |
| 2177 | } else { |
| 2178 | /* minor version 0; superblock after data */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2179 | sector_t sb_start; |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 2180 | sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2181 | sb_start &= ~(sector_t)(4*2 - 1); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2182 | max_sectors = rdev->sectors + sb_start - rdev->sb_start; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 2183 | if (!num_sectors || num_sectors > max_sectors) |
| 2184 | num_sectors = max_sectors; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2185 | rdev->sb_start = sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2186 | } |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2187 | sb = page_address(rdev->sb_page); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 2188 | sb->data_size = cpu_to_le64(num_sectors); |
Jason Yan | 3fb632e | 2017-03-10 11:27:23 +0800 | [diff] [blame] | 2189 | sb->super_offset = cpu_to_le64(rdev->sb_start); |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2190 | sb->sb_csum = calc_sb_1_csum(sb); |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 2191 | do { |
| 2192 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
| 2193 | rdev->sb_page); |
| 2194 | } while (md_super_wait(rdev->mddev) < 0); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 2195 | return num_sectors; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2196 | |
| 2197 | } |
| 2198 | |
| 2199 | static int |
| 2200 | super_1_allow_new_offset(struct md_rdev *rdev, |
| 2201 | unsigned long long new_offset) |
| 2202 | { |
| 2203 | /* All necessary checks on new >= old have been done */ |
| 2204 | struct bitmap *bitmap; |
| 2205 | if (new_offset >= rdev->data_offset) |
| 2206 | return 1; |
| 2207 | |
| 2208 | /* with 1.0 metadata, there is no metadata to tread on |
| 2209 | * so we can always move back */ |
| 2210 | if (rdev->mddev->minor_version == 0) |
| 2211 | return 1; |
| 2212 | |
| 2213 | /* otherwise we must be sure not to step on |
| 2214 | * any metadata, so stay: |
| 2215 | * 36K beyond start of superblock |
| 2216 | * beyond end of badblocks |
| 2217 | * beyond write-intent bitmap |
| 2218 | */ |
| 2219 | if (rdev->sb_start + (32+4)*2 > new_offset) |
| 2220 | return 0; |
| 2221 | bitmap = rdev->mddev->bitmap; |
| 2222 | if (bitmap && !rdev->mddev->bitmap_info.file && |
| 2223 | rdev->sb_start + rdev->mddev->bitmap_info.offset + |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 2224 | bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2225 | return 0; |
| 2226 | if (rdev->badblocks.sector + rdev->badblocks.size > new_offset) |
| 2227 | return 0; |
| 2228 | |
| 2229 | return 1; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2230 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2231 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 2232 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2233 | [0] = { |
| 2234 | .name = "0.90.0", |
| 2235 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2236 | .load_super = super_90_load, |
| 2237 | .validate_super = super_90_validate, |
| 2238 | .sync_super = super_90_sync, |
| 2239 | .rdev_size_change = super_90_rdev_size_change, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2240 | .allow_new_offset = super_90_allow_new_offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2241 | }, |
| 2242 | [1] = { |
| 2243 | .name = "md-1", |
| 2244 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2245 | .load_super = super_1_load, |
| 2246 | .validate_super = super_1_validate, |
| 2247 | .sync_super = super_1_sync, |
| 2248 | .rdev_size_change = super_1_rdev_size_change, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2249 | .allow_new_offset = super_1_allow_new_offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2250 | }, |
| 2251 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2252 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2253 | static void sync_super(struct mddev *mddev, struct md_rdev *rdev) |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 2254 | { |
| 2255 | if (mddev->sync_super) { |
| 2256 | mddev->sync_super(mddev, rdev); |
| 2257 | return; |
| 2258 | } |
| 2259 | |
| 2260 | BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types)); |
| 2261 | |
| 2262 | super_types[mddev->major_version].sync_super(mddev, rdev); |
| 2263 | } |
| 2264 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2265 | static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2266 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2267 | struct md_rdev *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2268 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2269 | rcu_read_lock(); |
Song Liu | 0b020e8 | 2015-09-03 23:00:35 -0700 | [diff] [blame] | 2270 | rdev_for_each_rcu(rdev, mddev1) { |
| 2271 | if (test_bit(Faulty, &rdev->flags) || |
| 2272 | test_bit(Journal, &rdev->flags) || |
| 2273 | rdev->raid_disk == -1) |
| 2274 | continue; |
| 2275 | rdev_for_each_rcu(rdev2, mddev2) { |
| 2276 | if (test_bit(Faulty, &rdev2->flags) || |
| 2277 | test_bit(Journal, &rdev2->flags) || |
| 2278 | rdev2->raid_disk == -1) |
| 2279 | continue; |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 2280 | if (rdev->bdev->bd_contains == |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2281 | rdev2->bdev->bd_contains) { |
| 2282 | rcu_read_unlock(); |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 2283 | return 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2284 | } |
Song Liu | 0b020e8 | 2015-09-03 23:00:35 -0700 | [diff] [blame] | 2285 | } |
| 2286 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2287 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2288 | return 0; |
| 2289 | } |
| 2290 | |
| 2291 | static LIST_HEAD(pending_raid_disks); |
| 2292 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2293 | /* |
| 2294 | * Try to register data integrity profile for an mddev |
| 2295 | * |
| 2296 | * This is called when an array is started and after a disk has been kicked |
| 2297 | * from the array. It only succeeds if all working and active component devices |
| 2298 | * are integrity capable with matching profiles. |
| 2299 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2300 | int md_integrity_register(struct mddev *mddev) |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2301 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2302 | struct md_rdev *rdev, *reference = NULL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2303 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2304 | if (list_empty(&mddev->disks)) |
| 2305 | return 0; /* nothing to do */ |
Jonathan Brassow | 629acb6 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 2306 | if (!mddev->gendisk || blk_get_integrity(mddev->gendisk)) |
| 2307 | return 0; /* shouldn't register, or already is */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2308 | rdev_for_each(rdev, mddev) { |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2309 | /* skip spares and non-functional disks */ |
| 2310 | if (test_bit(Faulty, &rdev->flags)) |
| 2311 | continue; |
| 2312 | if (rdev->raid_disk < 0) |
| 2313 | continue; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2314 | if (!reference) { |
| 2315 | /* Use the first rdev as the reference */ |
| 2316 | reference = rdev; |
| 2317 | continue; |
| 2318 | } |
| 2319 | /* does this rdev's profile match the reference profile? */ |
| 2320 | if (blk_integrity_compare(reference->bdev->bd_disk, |
| 2321 | rdev->bdev->bd_disk) < 0) |
| 2322 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2323 | } |
Martin K. Petersen | 89078d5 | 2011-03-28 20:09:12 -0400 | [diff] [blame] | 2324 | if (!reference || !bdev_get_integrity(reference->bdev)) |
| 2325 | return 0; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2326 | /* |
| 2327 | * All component devices are integrity capable and have matching |
| 2328 | * profiles, register the common profile for the md device. |
| 2329 | */ |
Martin K. Petersen | 25520d5 | 2015-10-21 13:19:49 -0400 | [diff] [blame] | 2330 | blk_integrity_register(mddev->gendisk, |
| 2331 | bdev_get_integrity(reference->bdev)); |
| 2332 | |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 2333 | pr_debug("md: data integrity enabled on %s\n", mdname(mddev)); |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 2334 | if (bioset_integrity_create(&mddev->bio_set, BIO_POOL_SIZE)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 2335 | pr_err("md: failed to create integrity pool for %s\n", |
Martin K. Petersen | a91a278 | 2011-03-17 11:11:05 +0100 | [diff] [blame] | 2336 | mdname(mddev)); |
| 2337 | return -EINVAL; |
| 2338 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2339 | return 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2340 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2341 | EXPORT_SYMBOL(md_integrity_register); |
| 2342 | |
Dan Williams | 1501efa | 2016-01-13 16:00:07 -0800 | [diff] [blame] | 2343 | /* |
| 2344 | * Attempt to add an rdev, but only if it is consistent with the current |
| 2345 | * integrity profile |
| 2346 | */ |
| 2347 | int md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev) |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2348 | { |
Jonathan Brassow | 2863b9e | 2012-10-11 13:38:58 +1100 | [diff] [blame] | 2349 | struct blk_integrity *bi_mddev; |
Dan Williams | 1501efa | 2016-01-13 16:00:07 -0800 | [diff] [blame] | 2350 | char name[BDEVNAME_SIZE]; |
Jonathan Brassow | 2863b9e | 2012-10-11 13:38:58 +1100 | [diff] [blame] | 2351 | |
| 2352 | if (!mddev->gendisk) |
Dan Williams | 1501efa | 2016-01-13 16:00:07 -0800 | [diff] [blame] | 2353 | return 0; |
Jonathan Brassow | 2863b9e | 2012-10-11 13:38:58 +1100 | [diff] [blame] | 2354 | |
Jonathan Brassow | 2863b9e | 2012-10-11 13:38:58 +1100 | [diff] [blame] | 2355 | bi_mddev = blk_get_integrity(mddev->gendisk); |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2356 | |
| 2357 | if (!bi_mddev) /* nothing to do */ |
Dan Williams | 1501efa | 2016-01-13 16:00:07 -0800 | [diff] [blame] | 2358 | return 0; |
| 2359 | |
| 2360 | if (blk_integrity_compare(mddev->gendisk, rdev->bdev->bd_disk) != 0) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 2361 | pr_err("%s: incompatible integrity profile for %s\n", |
| 2362 | mdname(mddev), bdevname(rdev->bdev, name)); |
Dan Williams | 1501efa | 2016-01-13 16:00:07 -0800 | [diff] [blame] | 2363 | return -ENXIO; |
| 2364 | } |
| 2365 | |
| 2366 | return 0; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2367 | } |
| 2368 | EXPORT_SYMBOL(md_integrity_add_rdev); |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2369 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2370 | static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2371 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 2372 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 2373 | struct kobject *ko; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2374 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2375 | |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2376 | /* prevent duplicates */ |
| 2377 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 2378 | return -EEXIST; |
| 2379 | |
NeilBrown | 97b20ef | 2017-04-13 08:53:48 +1000 | [diff] [blame] | 2380 | if ((bdev_read_only(rdev->bdev) || bdev_read_only(rdev->meta_bdev)) && |
| 2381 | mddev->pers) |
| 2382 | return -EROFS; |
| 2383 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2384 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 2385 | if (!test_bit(Journal, &rdev->flags) && |
| 2386 | rdev->sectors && |
| 2387 | (mddev->dev_sectors == 0 || rdev->sectors < mddev->dev_sectors)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2388 | if (mddev->pers) { |
| 2389 | /* Cannot change size, so fail |
| 2390 | * If mddev->level <= 0, then we don't care |
| 2391 | * about aligning sizes (e.g. linear) |
| 2392 | */ |
| 2393 | if (mddev->level > 0) |
| 2394 | return -ENOSPC; |
| 2395 | } else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2396 | mddev->dev_sectors = rdev->sectors; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2397 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2398 | |
| 2399 | /* Verify rdev->desc_nr is unique. |
| 2400 | * If it is -1, assign a free number, else |
| 2401 | * check number is not in use |
| 2402 | */ |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2403 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2404 | if (rdev->desc_nr < 0) { |
| 2405 | int choice = 0; |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2406 | if (mddev->pers) |
| 2407 | choice = mddev->raid_disks; |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 2408 | while (md_find_rdev_nr_rcu(mddev, choice)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2409 | choice++; |
| 2410 | rdev->desc_nr = choice; |
| 2411 | } else { |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 2412 | if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) { |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2413 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2414 | return -EBUSY; |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2415 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2416 | } |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2417 | rcu_read_unlock(); |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 2418 | if (!test_bit(Journal, &rdev->flags) && |
| 2419 | mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 2420 | pr_warn("md: %s: array is limited to %d devices\n", |
| 2421 | mdname(mddev), mddev->max_disks); |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 2422 | return -EBUSY; |
| 2423 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 2424 | bdevname(rdev->bdev,b); |
Rasmus Villemoes | 90a9bef | 2015-06-25 15:02:36 -0700 | [diff] [blame] | 2425 | strreplace(b, '/', '!'); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2426 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2427 | rdev->mddev = mddev; |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 2428 | pr_debug("md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2429 | |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 2430 | if (mddev->raid_disks) |
Guoqing Jiang | 404659c | 2019-12-23 10:48:53 +0100 | [diff] [blame] | 2431 | mddev_create_serial_pool(mddev, rdev, false); |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 2432 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2433 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2434 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2435 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 2436 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2437 | if (sysfs_create_link(&rdev->kobj, ko, "block")) |
| 2438 | /* failure here is OK */; |
| 2439 | rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2440 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2441 | list_add_rcu(&rdev->same_set, &mddev->disks); |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2442 | bd_link_disk_holder(rdev->bdev, mddev->gendisk); |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 2443 | |
| 2444 | /* May as well allow recovery to be retried once */ |
NeilBrown | 5389042 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2445 | mddev->recovery_disabled++; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2446 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2447 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2448 | |
| 2449 | fail: |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 2450 | pr_warn("md: failed to register dev-%s for %s\n", |
| 2451 | b, mdname(mddev)); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2452 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2453 | } |
| 2454 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2455 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2456 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2457 | struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2458 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2459 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2460 | } |
| 2461 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2462 | static void unbind_rdev_from_array(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2463 | { |
| 2464 | char b[BDEVNAME_SIZE]; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 2465 | |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 2466 | bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2467 | list_del_rcu(&rdev->same_set); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 2468 | pr_debug("md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
Guoqing Jiang | 11d3a9f | 2019-12-23 10:48:55 +0100 | [diff] [blame] | 2469 | mddev_destroy_serial_pool(rdev->mddev, rdev, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2470 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2471 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2472 | sysfs_put(rdev->sysfs_state); |
| 2473 | rdev->sysfs_state = NULL; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 2474 | rdev->badblocks.count = 0; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2475 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2476 | * writing to 'remove' to "dev/state". We also need |
| 2477 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2478 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2479 | synchronize_rcu(); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2480 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 2481 | kobject_get(&rdev->kobj); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 2482 | queue_work(md_misc_wq, &rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2483 | } |
| 2484 | |
| 2485 | /* |
| 2486 | * prevent the device from being mounted, repartitioned or |
| 2487 | * otherwise reused by a RAID array (or any other kernel |
| 2488 | * subsystem), by bd_claiming the device. |
| 2489 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2490 | static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2491 | { |
| 2492 | int err = 0; |
| 2493 | struct block_device *bdev; |
| 2494 | char b[BDEVNAME_SIZE]; |
| 2495 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 2496 | bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2497 | shared ? (struct md_rdev *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2498 | if (IS_ERR(bdev)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 2499 | pr_warn("md: could not open %s.\n", __bdevname(dev, b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2500 | return PTR_ERR(bdev); |
| 2501 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2502 | rdev->bdev = bdev; |
| 2503 | return err; |
| 2504 | } |
| 2505 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2506 | static void unlock_rdev(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2507 | { |
| 2508 | struct block_device *bdev = rdev->bdev; |
| 2509 | rdev->bdev = NULL; |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2510 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2511 | } |
| 2512 | |
| 2513 | void md_autodetect_dev(dev_t dev); |
| 2514 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2515 | static void export_rdev(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2516 | { |
| 2517 | char b[BDEVNAME_SIZE]; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 2518 | |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 2519 | pr_debug("md: export_rdev(%s)\n", bdevname(rdev->bdev,b)); |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 2520 | md_rdev_clear(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2521 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 2522 | if (test_bit(AutoDetected, &rdev->flags)) |
| 2523 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2524 | #endif |
| 2525 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2526 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2527 | } |
| 2528 | |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 2529 | void md_kick_rdev_from_array(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2530 | { |
| 2531 | unbind_rdev_from_array(rdev); |
| 2532 | export_rdev(rdev); |
| 2533 | } |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 2534 | EXPORT_SYMBOL_GPL(md_kick_rdev_from_array); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2535 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2536 | static void export_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2537 | { |
NeilBrown | 0638bb0 | 2014-09-25 17:43:47 +1000 | [diff] [blame] | 2538 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2539 | |
NeilBrown | 0638bb0 | 2014-09-25 17:43:47 +1000 | [diff] [blame] | 2540 | while (!list_empty(&mddev->disks)) { |
| 2541 | rdev = list_first_entry(&mddev->disks, struct md_rdev, |
| 2542 | same_set); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 2543 | md_kick_rdev_from_array(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2544 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2545 | mddev->raid_disks = 0; |
| 2546 | mddev->major_version = 0; |
| 2547 | } |
| 2548 | |
NeilBrown | 6497709b | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 2549 | static bool set_in_sync(struct mddev *mddev) |
| 2550 | { |
Shaohua Li | efa4b77 | 2017-10-18 22:08:13 -0700 | [diff] [blame] | 2551 | lockdep_assert_held(&mddev->lock); |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 2552 | if (!mddev->in_sync) { |
| 2553 | mddev->sync_checkers++; |
| 2554 | spin_unlock(&mddev->lock); |
| 2555 | percpu_ref_switch_to_atomic_sync(&mddev->writes_pending); |
| 2556 | spin_lock(&mddev->lock); |
| 2557 | if (!mddev->in_sync && |
| 2558 | percpu_ref_is_zero(&mddev->writes_pending)) { |
NeilBrown | 6497709b | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 2559 | mddev->in_sync = 1; |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 2560 | /* |
| 2561 | * Ensure ->in_sync is visible before we clear |
| 2562 | * ->sync_checkers. |
| 2563 | */ |
NeilBrown | 55cc39f | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 2564 | smp_mb(); |
NeilBrown | 6497709b | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 2565 | set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags); |
| 2566 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
| 2567 | } |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 2568 | if (--mddev->sync_checkers == 0) |
| 2569 | percpu_ref_switch_to_percpu(&mddev->writes_pending); |
NeilBrown | 6497709b | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 2570 | } |
| 2571 | if (mddev->safemode == 1) |
| 2572 | mddev->safemode = 0; |
| 2573 | return mddev->in_sync; |
| 2574 | } |
| 2575 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2576 | static void sync_sbs(struct mddev *mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2577 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2578 | /* Update each superblock (in-memory image), but |
| 2579 | * if we are allowed to, skip spares which already |
| 2580 | * have the right event counter, or have one earlier |
| 2581 | * (which would mean they aren't being marked as dirty |
| 2582 | * with the rest of the array) |
| 2583 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2584 | struct md_rdev *rdev; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2585 | rdev_for_each(rdev, mddev) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2586 | if (rdev->sb_events == mddev->events || |
| 2587 | (nospares && |
| 2588 | rdev->raid_disk < 0 && |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2589 | rdev->sb_events+1 == mddev->events)) { |
| 2590 | /* Don't update this superblock */ |
| 2591 | rdev->sb_loaded = 2; |
| 2592 | } else { |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 2593 | sync_super(mddev, rdev); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2594 | rdev->sb_loaded = 1; |
| 2595 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2596 | } |
| 2597 | } |
| 2598 | |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2599 | static bool does_sb_need_changing(struct mddev *mddev) |
| 2600 | { |
| 2601 | struct md_rdev *rdev; |
| 2602 | struct mdp_superblock_1 *sb; |
| 2603 | int role; |
| 2604 | |
| 2605 | /* Find a good rdev */ |
| 2606 | rdev_for_each(rdev, mddev) |
| 2607 | if ((rdev->raid_disk >= 0) && !test_bit(Faulty, &rdev->flags)) |
| 2608 | break; |
| 2609 | |
| 2610 | /* No good device found. */ |
| 2611 | if (!rdev) |
| 2612 | return false; |
| 2613 | |
| 2614 | sb = page_address(rdev->sb_page); |
| 2615 | /* Check if a device has become faulty or a spare become active */ |
| 2616 | rdev_for_each(rdev, mddev) { |
| 2617 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
| 2618 | /* Device activated? */ |
| 2619 | if (role == 0xffff && rdev->raid_disk >=0 && |
| 2620 | !test_bit(Faulty, &rdev->flags)) |
| 2621 | return true; |
| 2622 | /* Device turned faulty? */ |
| 2623 | if (test_bit(Faulty, &rdev->flags) && (role < 0xfffd)) |
| 2624 | return true; |
| 2625 | } |
| 2626 | |
| 2627 | /* Check if any mddev parameters have changed */ |
| 2628 | if ((mddev->dev_sectors != le64_to_cpu(sb->size)) || |
| 2629 | (mddev->reshape_position != le64_to_cpu(sb->reshape_position)) || |
Jason Yan | 1345921 | 2017-03-10 11:49:12 +0800 | [diff] [blame] | 2630 | (mddev->layout != le32_to_cpu(sb->layout)) || |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2631 | (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) || |
| 2632 | (mddev->chunk_sectors != le32_to_cpu(sb->chunksize))) |
| 2633 | return true; |
| 2634 | |
| 2635 | return false; |
| 2636 | } |
| 2637 | |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 2638 | void md_update_sb(struct mddev *mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2639 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2640 | struct md_rdev *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2641 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2642 | int nospares = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2643 | int any_badblocks_changed = 0; |
Guoqing Jiang | 23b63f9 | 2015-10-12 17:21:30 +0800 | [diff] [blame] | 2644 | int ret = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2645 | |
NeilBrown | d87f064 | 2013-04-24 11:42:40 +1000 | [diff] [blame] | 2646 | if (mddev->ro) { |
| 2647 | if (force_change) |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2648 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | d87f064 | 2013-04-24 11:42:40 +1000 | [diff] [blame] | 2649 | return; |
| 2650 | } |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2651 | |
Guoqing Jiang | 2c97cf1 | 2016-05-02 11:33:09 -0400 | [diff] [blame] | 2652 | repeat: |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2653 | if (mddev_is_clustered(mddev)) { |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2654 | if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)) |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2655 | force_change = 1; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2656 | if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags)) |
Guoqing Jiang | 85ad1d1 | 2016-05-03 22:22:13 -0400 | [diff] [blame] | 2657 | nospares = 1; |
Guoqing Jiang | 23b63f9 | 2015-10-12 17:21:30 +0800 | [diff] [blame] | 2658 | ret = md_cluster_ops->metadata_update_start(mddev); |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2659 | /* Has someone else has updated the sb */ |
| 2660 | if (!does_sb_need_changing(mddev)) { |
Guoqing Jiang | 23b63f9 | 2015-10-12 17:21:30 +0800 | [diff] [blame] | 2661 | if (ret == 0) |
| 2662 | md_cluster_ops->metadata_update_cancel(mddev); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2663 | bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING), |
| 2664 | BIT(MD_SB_CHANGE_DEVS) | |
| 2665 | BIT(MD_SB_CHANGE_CLEAN)); |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2666 | return; |
| 2667 | } |
| 2668 | } |
Guoqing Jiang | 2c97cf1 | 2016-05-02 11:33:09 -0400 | [diff] [blame] | 2669 | |
NeilBrown | db0505d | 2017-10-17 16:18:36 +1100 | [diff] [blame] | 2670 | /* |
| 2671 | * First make sure individual recovery_offsets are correct |
| 2672 | * curr_resync_completed can only be used during recovery. |
| 2673 | * During reshape/resync it might use array-addresses rather |
| 2674 | * that device addresses. |
| 2675 | */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2676 | rdev_for_each(rdev, mddev) { |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2677 | if (rdev->raid_disk >= 0 && |
| 2678 | mddev->delta_disks >= 0 && |
NeilBrown | db0505d | 2017-10-17 16:18:36 +1100 | [diff] [blame] | 2679 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 2680 | test_bit(MD_RECOVERY_RECOVER, &mddev->recovery) && |
| 2681 | !test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2682 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2683 | !test_bit(In_sync, &rdev->flags) && |
| 2684 | mddev->curr_resync_completed > rdev->recovery_offset) |
| 2685 | rdev->recovery_offset = mddev->curr_resync_completed; |
| 2686 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2687 | } |
Dan Williams | bd52b74 | 2010-08-30 17:33:33 +1000 | [diff] [blame] | 2688 | if (!mddev->persistent) { |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2689 | clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags); |
| 2690 | clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2691 | if (!mddev->external) { |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2692 | clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2693 | rdev_for_each(rdev, mddev) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2694 | if (rdev->badblocks.changed) { |
NeilBrown | d096293 | 2012-03-19 12:46:41 +1100 | [diff] [blame] | 2695 | rdev->badblocks.changed = 0; |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 2696 | ack_all_badblocks(&rdev->badblocks); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2697 | md_error(mddev, rdev); |
| 2698 | } |
| 2699 | clear_bit(Blocked, &rdev->flags); |
| 2700 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2701 | wake_up(&rdev->blocked_wait); |
| 2702 | } |
| 2703 | } |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2704 | wake_up(&mddev->sb_wait); |
| 2705 | return; |
| 2706 | } |
| 2707 | |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 2708 | spin_lock(&mddev->lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2709 | |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 2710 | mddev->utime = ktime_get_real_seconds(); |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2711 | |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2712 | if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)) |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2713 | force_change = 1; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2714 | if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags)) |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2715 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 2716 | * though if events isn't the right even/odd, we will have to do |
| 2717 | * spares after all |
| 2718 | */ |
| 2719 | nospares = 1; |
| 2720 | if (force_change) |
| 2721 | nospares = 0; |
| 2722 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2723 | /* If the array is degraded, then skipping spares is both |
| 2724 | * dangerous and fairly pointless. |
| 2725 | * Dangerous because a device that was removed from the array |
| 2726 | * might have a event_count that still looks up-to-date, |
| 2727 | * so it can be re-added without a resync. |
| 2728 | * Pointless because if there are any spares to skip, |
| 2729 | * then a recovery will happen and soon that array won't |
| 2730 | * be degraded any more and the spare can go back to sleep then. |
| 2731 | */ |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2732 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2733 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2734 | sync_req = mddev->in_sync; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2735 | |
| 2736 | /* If this is just a dirty<->clean transition, and the array is clean |
| 2737 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2738 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2739 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2740 | && mddev->can_decrease_events |
| 2741 | && mddev->events != 1) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2742 | mddev->events--; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2743 | mddev->can_decrease_events = 0; |
| 2744 | } else { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2745 | /* otherwise we have to go forward and ... */ |
| 2746 | mddev->events ++; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2747 | mddev->can_decrease_events = nospares; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2748 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2749 | |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 2750 | /* |
| 2751 | * This 64-bit counter should never wrap. |
| 2752 | * Either we are in around ~1 trillion A.C., assuming |
| 2753 | * 1 reboot per second, or we have a bug... |
| 2754 | */ |
| 2755 | WARN_ON(mddev->events == 0); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2756 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2757 | rdev_for_each(rdev, mddev) { |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2758 | if (rdev->badblocks.changed) |
| 2759 | any_badblocks_changed++; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2760 | if (test_bit(Faulty, &rdev->flags)) |
| 2761 | set_bit(FaultRecorded, &rdev->flags); |
| 2762 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2763 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2764 | sync_sbs(mddev, nospares); |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 2765 | spin_unlock(&mddev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2766 | |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2767 | pr_debug("md: updating %s RAID superblock on device (in sync %d)\n", |
| 2768 | mdname(mddev), mddev->in_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2769 | |
Shaohua Li | 504634f | 2016-11-18 09:44:08 -0800 | [diff] [blame] | 2770 | if (mddev->queue) |
| 2771 | blk_add_trace_msg(mddev->queue, "md md_update_sb"); |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 2772 | rewrite: |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 2773 | md_bitmap_update_sb(mddev->bitmap); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2774 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2775 | char b[BDEVNAME_SIZE]; |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2776 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2777 | if (rdev->sb_loaded != 1) |
| 2778 | continue; /* no noise on spare devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2779 | |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2780 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2781 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2782 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2783 | rdev->sb_page); |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2784 | pr_debug("md: (write) %s's sb offset: %llu\n", |
| 2785 | bdevname(rdev->bdev, b), |
| 2786 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2787 | rdev->sb_events = mddev->events; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2788 | if (rdev->badblocks.size) { |
| 2789 | md_super_write(mddev, rdev, |
| 2790 | rdev->badblocks.sector, |
| 2791 | rdev->badblocks.size << 9, |
| 2792 | rdev->bb_page); |
| 2793 | rdev->badblocks.size = 0; |
| 2794 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2795 | |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2796 | } else |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2797 | pr_debug("md: %s (skipping faulty)\n", |
| 2798 | bdevname(rdev->bdev, b)); |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 2799 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2800 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2801 | /* only need to write one superblock... */ |
| 2802 | break; |
| 2803 | } |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 2804 | if (md_super_wait(mddev) < 0) |
| 2805 | goto rewrite; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2806 | /* if there was a failure, MD_SB_CHANGE_DEVS was set, and we re-write super */ |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2807 | |
Guoqing Jiang | 2c97cf1 | 2016-05-02 11:33:09 -0400 | [diff] [blame] | 2808 | if (mddev_is_clustered(mddev) && ret == 0) |
| 2809 | md_cluster_ops->metadata_update_finish(mddev); |
| 2810 | |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2811 | if (mddev->in_sync != sync_req || |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2812 | !bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING), |
| 2813 | BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_CLEAN))) |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2814 | /* have to write it out again */ |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2815 | goto repeat; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2816 | wake_up(&mddev->sb_wait); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 2817 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 2818 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2819 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2820 | rdev_for_each(rdev, mddev) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2821 | if (test_and_clear_bit(FaultRecorded, &rdev->flags)) |
| 2822 | clear_bit(Blocked, &rdev->flags); |
| 2823 | |
| 2824 | if (any_badblocks_changed) |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 2825 | ack_all_badblocks(&rdev->badblocks); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2826 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2827 | wake_up(&rdev->blocked_wait); |
| 2828 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2829 | } |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 2830 | EXPORT_SYMBOL(md_update_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2831 | |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2832 | static int add_bound_rdev(struct md_rdev *rdev) |
| 2833 | { |
| 2834 | struct mddev *mddev = rdev->mddev; |
| 2835 | int err = 0; |
Shaohua Li | 87d4d91 | 2016-01-06 14:37:14 -0800 | [diff] [blame] | 2836 | bool add_journal = test_bit(Journal, &rdev->flags); |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2837 | |
Shaohua Li | 87d4d91 | 2016-01-06 14:37:14 -0800 | [diff] [blame] | 2838 | if (!mddev->pers->hot_remove_disk || add_journal) { |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2839 | /* If there is hot_add_disk but no hot_remove_disk |
| 2840 | * then added disks for geometry changes, |
| 2841 | * and should be added immediately. |
| 2842 | */ |
| 2843 | super_types[mddev->major_version]. |
| 2844 | validate_super(mddev, rdev); |
Shaohua Li | 87d4d91 | 2016-01-06 14:37:14 -0800 | [diff] [blame] | 2845 | if (add_journal) |
| 2846 | mddev_suspend(mddev); |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2847 | err = mddev->pers->hot_add_disk(mddev, rdev); |
Shaohua Li | 87d4d91 | 2016-01-06 14:37:14 -0800 | [diff] [blame] | 2848 | if (add_journal) |
| 2849 | mddev_resume(mddev); |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2850 | if (err) { |
Guoqing Jiang | db76767 | 2016-06-02 23:32:05 -0400 | [diff] [blame] | 2851 | md_kick_rdev_from_array(rdev); |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2852 | return err; |
| 2853 | } |
| 2854 | } |
| 2855 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
| 2856 | |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2857 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2858 | if (mddev->degraded) |
| 2859 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 2860 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 2861 | md_new_event(mddev); |
| 2862 | md_wakeup_thread(mddev->thread); |
| 2863 | return 0; |
| 2864 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2865 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 2866 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2867 | * We want to accept with case. For this we use cmd_match. |
| 2868 | */ |
| 2869 | static int cmd_match(const char *cmd, const char *str) |
| 2870 | { |
| 2871 | /* See if cmd, written into a sysfs file, matches |
| 2872 | * str. They must either be the same, or cmd can |
| 2873 | * have a trailing newline |
| 2874 | */ |
| 2875 | while (*cmd && *str && *cmd == *str) { |
| 2876 | cmd++; |
| 2877 | str++; |
| 2878 | } |
| 2879 | if (*cmd == '\n') |
| 2880 | cmd++; |
| 2881 | if (*str || *cmd) |
| 2882 | return 0; |
| 2883 | return 1; |
| 2884 | } |
| 2885 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2886 | struct rdev_sysfs_entry { |
| 2887 | struct attribute attr; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2888 | ssize_t (*show)(struct md_rdev *, char *); |
| 2889 | ssize_t (*store)(struct md_rdev *, const char *, size_t); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2890 | }; |
| 2891 | |
| 2892 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2893 | state_show(struct md_rdev *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2894 | { |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 2895 | char *sep = ","; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2896 | size_t len = 0; |
Mark Rutland | 6aa7de0 | 2017-10-23 14:07:29 -0700 | [diff] [blame] | 2897 | unsigned long flags = READ_ONCE(rdev->flags); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2898 | |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2899 | if (test_bit(Faulty, &flags) || |
Tomasz Majchrzak | dcbcb48 | 2016-10-21 16:27:08 +0200 | [diff] [blame] | 2900 | (!test_bit(ExternalBbl, &flags) && |
| 2901 | rdev->badblocks.unacked_exist)) |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 2902 | len += sprintf(page+len, "faulty%s", sep); |
| 2903 | if (test_bit(In_sync, &flags)) |
| 2904 | len += sprintf(page+len, "in_sync%s", sep); |
| 2905 | if (test_bit(Journal, &flags)) |
| 2906 | len += sprintf(page+len, "journal%s", sep); |
| 2907 | if (test_bit(WriteMostly, &flags)) |
| 2908 | len += sprintf(page+len, "write_mostly%s", sep); |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2909 | if (test_bit(Blocked, &flags) || |
NeilBrown | 52c6415 | 2011-12-08 16:22:48 +1100 | [diff] [blame] | 2910 | (rdev->badblocks.unacked_exist |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 2911 | && !test_bit(Faulty, &flags))) |
| 2912 | len += sprintf(page+len, "blocked%s", sep); |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2913 | if (!test_bit(Faulty, &flags) && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2914 | !test_bit(Journal, &flags) && |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 2915 | !test_bit(In_sync, &flags)) |
| 2916 | len += sprintf(page+len, "spare%s", sep); |
| 2917 | if (test_bit(WriteErrorSeen, &flags)) |
| 2918 | len += sprintf(page+len, "write_error%s", sep); |
| 2919 | if (test_bit(WantReplacement, &flags)) |
| 2920 | len += sprintf(page+len, "want_replacement%s", sep); |
| 2921 | if (test_bit(Replacement, &flags)) |
| 2922 | len += sprintf(page+len, "replacement%s", sep); |
| 2923 | if (test_bit(ExternalBbl, &flags)) |
| 2924 | len += sprintf(page+len, "external_bbl%s", sep); |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 2925 | if (test_bit(FailFast, &flags)) |
| 2926 | len += sprintf(page+len, "failfast%s", sep); |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 2927 | |
| 2928 | if (len) |
| 2929 | len -= strlen(sep); |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2930 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2931 | return len+sprintf(page+len, "\n"); |
| 2932 | } |
| 2933 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2934 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2935 | state_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2936 | { |
| 2937 | /* can write |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2938 | * faulty - simulates an error |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2939 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2940 | * writemostly - sets write_mostly |
| 2941 | * -writemostly - clears write_mostly |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2942 | * blocked - sets the Blocked flags |
| 2943 | * -blocked - clears the Blocked and possibly simulates an error |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2944 | * insync - sets Insync providing device isn't active |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2945 | * -insync - clear Insync for a device with a slot assigned, |
| 2946 | * so that it gets rebuilt based on bitmap |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2947 | * write_error - sets WriteErrorSeen |
| 2948 | * -write_error - clears WriteErrorSeen |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 2949 | * {,-}failfast - set/clear FailFast |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2950 | */ |
| 2951 | int err = -EINVAL; |
| 2952 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 2953 | md_error(rdev->mddev, rdev); |
NeilBrown | 5ef56c8 | 2011-08-25 14:42:51 +1000 | [diff] [blame] | 2954 | if (test_bit(Faulty, &rdev->flags)) |
| 2955 | err = 0; |
| 2956 | else |
| 2957 | err = -EBUSY; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2958 | } else if (cmd_match(buf, "remove")) { |
Shaohua Li | 5d88178 | 2016-07-28 09:06:34 -0700 | [diff] [blame] | 2959 | if (rdev->mddev->pers) { |
| 2960 | clear_bit(Blocked, &rdev->flags); |
| 2961 | remove_and_add_spares(rdev->mddev, rdev); |
| 2962 | } |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2963 | if (rdev->raid_disk >= 0) |
| 2964 | err = -EBUSY; |
| 2965 | else { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2966 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2967 | err = 0; |
Guoqing Jiang | a972090 | 2015-10-12 17:21:27 +0800 | [diff] [blame] | 2968 | if (mddev_is_clustered(mddev)) |
| 2969 | err = md_cluster_ops->remove_disk(mddev, rdev); |
| 2970 | |
| 2971 | if (err == 0) { |
| 2972 | md_kick_rdev_from_array(rdev); |
NeilBrown | 060b068 | 2016-11-04 16:46:03 +1100 | [diff] [blame] | 2973 | if (mddev->pers) { |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2974 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | 060b068 | 2016-11-04 16:46:03 +1100 | [diff] [blame] | 2975 | md_wakeup_thread(mddev->thread); |
| 2976 | } |
Guoqing Jiang | a972090 | 2015-10-12 17:21:27 +0800 | [diff] [blame] | 2977 | md_new_event(mddev); |
| 2978 | } |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2979 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2980 | } else if (cmd_match(buf, "writemostly")) { |
| 2981 | set_bit(WriteMostly, &rdev->flags); |
Guoqing Jiang | 404659c | 2019-12-23 10:48:53 +0100 | [diff] [blame] | 2982 | mddev_create_serial_pool(rdev->mddev, rdev, false); |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2983 | err = 0; |
| 2984 | } else if (cmd_match(buf, "-writemostly")) { |
Guoqing Jiang | 11d3a9f | 2019-12-23 10:48:55 +0100 | [diff] [blame] | 2985 | mddev_destroy_serial_pool(rdev->mddev, rdev, false); |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2986 | clear_bit(WriteMostly, &rdev->flags); |
| 2987 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2988 | } else if (cmd_match(buf, "blocked")) { |
| 2989 | set_bit(Blocked, &rdev->flags); |
| 2990 | err = 0; |
| 2991 | } else if (cmd_match(buf, "-blocked")) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2992 | if (!test_bit(Faulty, &rdev->flags) && |
Tomasz Majchrzak | dcbcb48 | 2016-10-21 16:27:08 +0200 | [diff] [blame] | 2993 | !test_bit(ExternalBbl, &rdev->flags) && |
NeilBrown | 7da64a0 | 2011-08-30 16:20:17 +1000 | [diff] [blame] | 2994 | rdev->badblocks.unacked_exist) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2995 | /* metadata handler doesn't understand badblocks, |
| 2996 | * so we need to fail the device |
| 2997 | */ |
| 2998 | md_error(rdev->mddev, rdev); |
| 2999 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3000 | clear_bit(Blocked, &rdev->flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 3001 | clear_bit(BlockedBadBlocks, &rdev->flags); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3002 | wake_up(&rdev->blocked_wait); |
| 3003 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 3004 | md_wakeup_thread(rdev->mddev->thread); |
| 3005 | |
| 3006 | err = 0; |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 3007 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { |
| 3008 | set_bit(In_sync, &rdev->flags); |
| 3009 | err = 0; |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 3010 | } else if (cmd_match(buf, "failfast")) { |
| 3011 | set_bit(FailFast, &rdev->flags); |
| 3012 | err = 0; |
| 3013 | } else if (cmd_match(buf, "-failfast")) { |
| 3014 | clear_bit(FailFast, &rdev->flags); |
| 3015 | err = 0; |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 3016 | } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0 && |
| 3017 | !test_bit(Journal, &rdev->flags)) { |
NeilBrown | e1960f8 | 2014-09-30 15:24:25 +1000 | [diff] [blame] | 3018 | if (rdev->mddev->pers == NULL) { |
| 3019 | clear_bit(In_sync, &rdev->flags); |
| 3020 | rdev->saved_raid_disk = rdev->raid_disk; |
| 3021 | rdev->raid_disk = -1; |
| 3022 | err = 0; |
| 3023 | } |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 3024 | } else if (cmd_match(buf, "write_error")) { |
| 3025 | set_bit(WriteErrorSeen, &rdev->flags); |
| 3026 | err = 0; |
| 3027 | } else if (cmd_match(buf, "-write_error")) { |
| 3028 | clear_bit(WriteErrorSeen, &rdev->flags); |
| 3029 | err = 0; |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 3030 | } else if (cmd_match(buf, "want_replacement")) { |
| 3031 | /* Any non-spare device that is not a replacement can |
| 3032 | * become want_replacement at any time, but we then need to |
| 3033 | * check if recovery is needed. |
| 3034 | */ |
| 3035 | if (rdev->raid_disk >= 0 && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 3036 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 3037 | !test_bit(Replacement, &rdev->flags)) |
| 3038 | set_bit(WantReplacement, &rdev->flags); |
| 3039 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 3040 | md_wakeup_thread(rdev->mddev->thread); |
| 3041 | err = 0; |
| 3042 | } else if (cmd_match(buf, "-want_replacement")) { |
| 3043 | /* Clearing 'want_replacement' is always allowed. |
| 3044 | * Once replacements starts it is too late though. |
| 3045 | */ |
| 3046 | err = 0; |
| 3047 | clear_bit(WantReplacement, &rdev->flags); |
| 3048 | } else if (cmd_match(buf, "replacement")) { |
| 3049 | /* Can only set a device as a replacement when array has not |
| 3050 | * yet been started. Once running, replacement is automatic |
| 3051 | * from spares, or by assigning 'slot'. |
| 3052 | */ |
| 3053 | if (rdev->mddev->pers) |
| 3054 | err = -EBUSY; |
| 3055 | else { |
| 3056 | set_bit(Replacement, &rdev->flags); |
| 3057 | err = 0; |
| 3058 | } |
| 3059 | } else if (cmd_match(buf, "-replacement")) { |
| 3060 | /* Similarly, can only clear Replacement before start */ |
| 3061 | if (rdev->mddev->pers) |
| 3062 | err = -EBUSY; |
| 3063 | else { |
| 3064 | clear_bit(Replacement, &rdev->flags); |
| 3065 | err = 0; |
| 3066 | } |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 3067 | } else if (cmd_match(buf, "re-add")) { |
Yufen Yu | ee37e62 | 2019-04-02 14:22:14 +0800 | [diff] [blame] | 3068 | if (!rdev->mddev->pers) |
| 3069 | err = -EINVAL; |
| 3070 | else if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1) && |
| 3071 | rdev->saved_raid_disk >= 0) { |
Goldwyn Rodrigues | 97f6cd3 | 2015-04-14 10:45:42 -0500 | [diff] [blame] | 3072 | /* clear_bit is performed _after_ all the devices |
| 3073 | * have their local Faulty bit cleared. If any writes |
| 3074 | * happen in the meantime in the local node, they |
| 3075 | * will land in the local bitmap, which will be synced |
| 3076 | * by this node eventually |
| 3077 | */ |
| 3078 | if (!mddev_is_clustered(rdev->mddev) || |
| 3079 | (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) { |
| 3080 | clear_bit(Faulty, &rdev->flags); |
| 3081 | err = add_bound_rdev(rdev); |
| 3082 | } |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 3083 | } else |
| 3084 | err = -EBUSY; |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 3085 | } else if (cmd_match(buf, "external_bbl") && (rdev->mddev->external)) { |
| 3086 | set_bit(ExternalBbl, &rdev->flags); |
| 3087 | rdev->badblocks.shift = 0; |
| 3088 | err = 0; |
| 3089 | } else if (cmd_match(buf, "-external_bbl") && (rdev->mddev->external)) { |
| 3090 | clear_bit(ExternalBbl, &rdev->flags); |
| 3091 | err = 0; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3092 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 3093 | if (!err) |
| 3094 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3095 | return err ? err : len; |
| 3096 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3097 | static struct rdev_sysfs_entry rdev_state = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 3098 | __ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3099 | |
| 3100 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3101 | errors_show(struct md_rdev *rdev, char *page) |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 3102 | { |
| 3103 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 3104 | } |
| 3105 | |
| 3106 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3107 | errors_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 3108 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3109 | unsigned int n; |
| 3110 | int rv; |
| 3111 | |
| 3112 | rv = kstrtouint(buf, 10, &n); |
| 3113 | if (rv < 0) |
| 3114 | return rv; |
| 3115 | atomic_set(&rdev->corrected_errors, n); |
| 3116 | return len; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 3117 | } |
| 3118 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3119 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 3120 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3121 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3122 | slot_show(struct md_rdev *rdev, char *page) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3123 | { |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 3124 | if (test_bit(Journal, &rdev->flags)) |
| 3125 | return sprintf(page, "journal\n"); |
| 3126 | else if (rdev->raid_disk < 0) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3127 | return sprintf(page, "none\n"); |
| 3128 | else |
| 3129 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 3130 | } |
| 3131 | |
| 3132 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3133 | slot_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3134 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3135 | int slot; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3136 | int err; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3137 | |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 3138 | if (test_bit(Journal, &rdev->flags)) |
| 3139 | return -EBUSY; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3140 | if (strncmp(buf, "none", 4)==0) |
| 3141 | slot = -1; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3142 | else { |
| 3143 | err = kstrtouint(buf, 10, (unsigned int *)&slot); |
| 3144 | if (err < 0) |
| 3145 | return err; |
| 3146 | } |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 3147 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3148 | /* Setting 'slot' on an active array requires also |
| 3149 | * updating the 'rd%d' link, and communicating |
| 3150 | * with the personality with ->hot_*_disk. |
| 3151 | * For now we only support removing |
| 3152 | * failed/spare devices. This normally happens automatically, |
| 3153 | * but not when the metadata is externally managed. |
| 3154 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3155 | if (rdev->raid_disk == -1) |
| 3156 | return -EEXIST; |
| 3157 | /* personality does all needed checks */ |
Namhyung Kim | 01393f3 | 2011-06-09 11:42:54 +1000 | [diff] [blame] | 3158 | if (rdev->mddev->pers->hot_remove_disk == NULL) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3159 | return -EINVAL; |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 3160 | clear_bit(Blocked, &rdev->flags); |
| 3161 | remove_and_add_spares(rdev->mddev, rdev); |
| 3162 | if (rdev->raid_disk >= 0) |
| 3163 | return -EBUSY; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3164 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 3165 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 3166 | } else if (rdev->mddev->pers) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 3167 | /* Activating a spare .. or possibly reactivating |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 3168 | * if we ever get bitmaps working here. |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 3169 | */ |
Goldwyn Rodrigues | cb01c54 | 2015-12-18 15:19:16 +1100 | [diff] [blame] | 3170 | int err; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 3171 | |
| 3172 | if (rdev->raid_disk != -1) |
| 3173 | return -EBUSY; |
| 3174 | |
NeilBrown | c6751b2 | 2011-02-02 11:57:13 +1100 | [diff] [blame] | 3175 | if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery)) |
| 3176 | return -EBUSY; |
| 3177 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 3178 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 3179 | return -EINVAL; |
| 3180 | |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 3181 | if (slot >= rdev->mddev->raid_disks && |
| 3182 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
| 3183 | return -ENOSPC; |
| 3184 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 3185 | rdev->raid_disk = slot; |
| 3186 | if (test_bit(In_sync, &rdev->flags)) |
| 3187 | rdev->saved_raid_disk = slot; |
| 3188 | else |
| 3189 | rdev->saved_raid_disk = -1; |
NeilBrown | d30519f | 2011-10-18 12:13:47 +1100 | [diff] [blame] | 3190 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 3191 | clear_bit(Bitmap_sync, &rdev->flags); |
Goldwyn Rodrigues | cb01c54 | 2015-12-18 15:19:16 +1100 | [diff] [blame] | 3192 | err = rdev->mddev->pers-> |
| 3193 | hot_add_disk(rdev->mddev, rdev); |
| 3194 | if (err) { |
| 3195 | rdev->raid_disk = -1; |
| 3196 | return err; |
| 3197 | } else |
| 3198 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
| 3199 | if (sysfs_link_rdev(rdev->mddev, rdev)) |
| 3200 | /* failure here is OK */; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 3201 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3202 | } else { |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 3203 | if (slot >= rdev->mddev->raid_disks && |
| 3204 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3205 | return -ENOSPC; |
| 3206 | rdev->raid_disk = slot; |
| 3207 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3208 | clear_bit(Faulty, &rdev->flags); |
| 3209 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3210 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 3211 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3212 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3213 | return len; |
| 3214 | } |
| 3215 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3216 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3217 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3218 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3219 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3220 | offset_show(struct md_rdev *rdev, char *page) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3221 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 3222 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3223 | } |
| 3224 | |
| 3225 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3226 | offset_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3227 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3228 | unsigned long long offset; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 3229 | if (kstrtoull(buf, 10, &offset) < 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3230 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 3231 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3232 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3233 | if (rdev->sectors && rdev->mddev->external) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3234 | /* Must set offset before size, so overlap checks |
| 3235 | * can be sane */ |
| 3236 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3237 | rdev->data_offset = offset; |
NeilBrown | 25f7fd4 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3238 | rdev->new_data_offset = offset; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3239 | return len; |
| 3240 | } |
| 3241 | |
| 3242 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3243 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3244 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3245 | static ssize_t new_offset_show(struct md_rdev *rdev, char *page) |
| 3246 | { |
| 3247 | return sprintf(page, "%llu\n", |
| 3248 | (unsigned long long)rdev->new_data_offset); |
| 3249 | } |
| 3250 | |
| 3251 | static ssize_t new_offset_store(struct md_rdev *rdev, |
| 3252 | const char *buf, size_t len) |
| 3253 | { |
| 3254 | unsigned long long new_offset; |
| 3255 | struct mddev *mddev = rdev->mddev; |
| 3256 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 3257 | if (kstrtoull(buf, 10, &new_offset) < 0) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3258 | return -EINVAL; |
| 3259 | |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 3260 | if (mddev->sync_thread || |
| 3261 | test_bit(MD_RECOVERY_RUNNING,&mddev->recovery)) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3262 | return -EBUSY; |
| 3263 | if (new_offset == rdev->data_offset) |
| 3264 | /* reset is always permitted */ |
| 3265 | ; |
| 3266 | else if (new_offset > rdev->data_offset) { |
| 3267 | /* must not push array size beyond rdev_sectors */ |
| 3268 | if (new_offset - rdev->data_offset |
| 3269 | + mddev->dev_sectors > rdev->sectors) |
| 3270 | return -E2BIG; |
| 3271 | } |
| 3272 | /* Metadata worries about other space details. */ |
| 3273 | |
| 3274 | /* decreasing the offset is inconsistent with a backwards |
| 3275 | * reshape. |
| 3276 | */ |
| 3277 | if (new_offset < rdev->data_offset && |
| 3278 | mddev->reshape_backwards) |
| 3279 | return -EINVAL; |
| 3280 | /* Increasing offset is inconsistent with forwards |
| 3281 | * reshape. reshape_direction should be set to |
| 3282 | * 'backwards' first. |
| 3283 | */ |
| 3284 | if (new_offset > rdev->data_offset && |
| 3285 | !mddev->reshape_backwards) |
| 3286 | return -EINVAL; |
| 3287 | |
| 3288 | if (mddev->pers && mddev->persistent && |
| 3289 | !super_types[mddev->major_version] |
| 3290 | .allow_new_offset(rdev, new_offset)) |
| 3291 | return -E2BIG; |
| 3292 | rdev->new_data_offset = new_offset; |
| 3293 | if (new_offset > rdev->data_offset) |
| 3294 | mddev->reshape_backwards = 1; |
| 3295 | else if (new_offset < rdev->data_offset) |
| 3296 | mddev->reshape_backwards = 0; |
| 3297 | |
| 3298 | return len; |
| 3299 | } |
| 3300 | static struct rdev_sysfs_entry rdev_new_offset = |
| 3301 | __ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store); |
| 3302 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3303 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3304 | rdev_size_show(struct md_rdev *rdev, char *page) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3305 | { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3306 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3307 | } |
| 3308 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3309 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 3310 | { |
| 3311 | /* check if two start/length pairs overlap */ |
| 3312 | if (s1+l1 <= s2) |
| 3313 | return 0; |
| 3314 | if (s2+l2 <= s1) |
| 3315 | return 0; |
| 3316 | return 1; |
| 3317 | } |
| 3318 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3319 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
| 3320 | { |
| 3321 | unsigned long long blocks; |
| 3322 | sector_t new; |
| 3323 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 3324 | if (kstrtoull(buf, 10, &blocks) < 0) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3325 | return -EINVAL; |
| 3326 | |
| 3327 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) |
| 3328 | return -EINVAL; /* sector conversion overflow */ |
| 3329 | |
| 3330 | new = blocks * 2; |
| 3331 | if (new != blocks * 2) |
| 3332 | return -EINVAL; /* unsigned long long to sector_t overflow */ |
| 3333 | |
| 3334 | *sectors = new; |
| 3335 | return 0; |
| 3336 | } |
| 3337 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3338 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3339 | rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3340 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3341 | struct mddev *my_mddev = rdev->mddev; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3342 | sector_t oldsectors = rdev->sectors; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3343 | sector_t sectors; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3344 | |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 3345 | if (test_bit(Journal, &rdev->flags)) |
| 3346 | return -EBUSY; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3347 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 3348 | return -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3349 | if (rdev->data_offset != rdev->new_data_offset) |
| 3350 | return -EINVAL; /* too confusing */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 3351 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 3352 | if (my_mddev->persistent) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3353 | sectors = super_types[my_mddev->major_version]. |
| 3354 | rdev_size_change(rdev, sectors); |
| 3355 | if (!sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 3356 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3357 | } else if (!sectors) |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 3358 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) - |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3359 | rdev->data_offset; |
NeilBrown | a646853 | 2013-02-21 14:33:17 +1100 | [diff] [blame] | 3360 | if (!my_mddev->pers->resize) |
| 3361 | /* Cannot change size for RAID0 or Linear etc */ |
| 3362 | return -EINVAL; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 3363 | } |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3364 | if (sectors < my_mddev->dev_sectors) |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 3365 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 3366 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3367 | rdev->sectors = sectors; |
| 3368 | if (sectors > oldsectors && my_mddev->external) { |
NeilBrown | 8b1afc3 | 2014-09-29 15:33:20 +1000 | [diff] [blame] | 3369 | /* Need to check that all other rdevs with the same |
| 3370 | * ->bdev do not overlap. 'rcu' is sufficient to walk |
| 3371 | * the rdev lists safely. |
| 3372 | * This check does not provide a hard guarantee, it |
| 3373 | * just helps avoid dangerous mistakes. |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3374 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3375 | struct mddev *mddev; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3376 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3377 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3378 | |
NeilBrown | 8b1afc3 | 2014-09-29 15:33:20 +1000 | [diff] [blame] | 3379 | rcu_read_lock(); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 3380 | for_each_mddev(mddev, tmp) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3381 | struct md_rdev *rdev2; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3382 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3383 | rdev_for_each(rdev2, mddev) |
NeilBrown | f21e9ff | 2011-01-31 12:10:09 +1100 | [diff] [blame] | 3384 | if (rdev->bdev == rdev2->bdev && |
| 3385 | rdev != rdev2 && |
| 3386 | overlaps(rdev->data_offset, rdev->sectors, |
| 3387 | rdev2->data_offset, |
| 3388 | rdev2->sectors)) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3389 | overlap = 1; |
| 3390 | break; |
| 3391 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3392 | if (overlap) { |
| 3393 | mddev_put(mddev); |
| 3394 | break; |
| 3395 | } |
| 3396 | } |
NeilBrown | 8b1afc3 | 2014-09-29 15:33:20 +1000 | [diff] [blame] | 3397 | rcu_read_unlock(); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3398 | if (overlap) { |
| 3399 | /* Someone else could have slipped in a size |
| 3400 | * change here, but doing so is just silly. |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3401 | * We put oldsectors back because we *know* it is |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3402 | * safe, and trust userspace not to race with |
| 3403 | * itself |
| 3404 | */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3405 | rdev->sectors = oldsectors; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3406 | return -EBUSY; |
| 3407 | } |
| 3408 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3409 | return len; |
| 3410 | } |
| 3411 | |
| 3412 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3413 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3414 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3415 | static ssize_t recovery_start_show(struct md_rdev *rdev, char *page) |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3416 | { |
| 3417 | unsigned long long recovery_start = rdev->recovery_offset; |
| 3418 | |
| 3419 | if (test_bit(In_sync, &rdev->flags) || |
| 3420 | recovery_start == MaxSector) |
| 3421 | return sprintf(page, "none\n"); |
| 3422 | |
| 3423 | return sprintf(page, "%llu\n", recovery_start); |
| 3424 | } |
| 3425 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3426 | static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len) |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3427 | { |
| 3428 | unsigned long long recovery_start; |
| 3429 | |
| 3430 | if (cmd_match(buf, "none")) |
| 3431 | recovery_start = MaxSector; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 3432 | else if (kstrtoull(buf, 10, &recovery_start)) |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3433 | return -EINVAL; |
| 3434 | |
| 3435 | if (rdev->mddev->pers && |
| 3436 | rdev->raid_disk >= 0) |
| 3437 | return -EBUSY; |
| 3438 | |
| 3439 | rdev->recovery_offset = recovery_start; |
| 3440 | if (recovery_start == MaxSector) |
| 3441 | set_bit(In_sync, &rdev->flags); |
| 3442 | else |
| 3443 | clear_bit(In_sync, &rdev->flags); |
| 3444 | return len; |
| 3445 | } |
| 3446 | |
| 3447 | static struct rdev_sysfs_entry rdev_recovery_start = |
| 3448 | __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store); |
| 3449 | |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 3450 | /* sysfs access to bad-blocks list. |
| 3451 | * We present two files. |
| 3452 | * 'bad-blocks' lists sector numbers and lengths of ranges that |
| 3453 | * are recorded as bad. The list is truncated to fit within |
| 3454 | * the one-page limit of sysfs. |
| 3455 | * Writing "sector length" to this file adds an acknowledged |
| 3456 | * bad block list. |
| 3457 | * 'unacknowledged-bad-blocks' lists bad blocks that have not yet |
| 3458 | * been acknowledged. Writing to this file adds bad blocks |
| 3459 | * without acknowledging them. This is largely for testing. |
| 3460 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3461 | static ssize_t bb_show(struct md_rdev *rdev, char *page) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3462 | { |
| 3463 | return badblocks_show(&rdev->badblocks, page, 0); |
| 3464 | } |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3465 | static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3466 | { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 3467 | int rv = badblocks_store(&rdev->badblocks, page, len, 0); |
| 3468 | /* Maybe that ack was all we needed */ |
| 3469 | if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags)) |
| 3470 | wake_up(&rdev->blocked_wait); |
| 3471 | return rv; |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3472 | } |
| 3473 | static struct rdev_sysfs_entry rdev_bad_blocks = |
| 3474 | __ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store); |
| 3475 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3476 | static ssize_t ubb_show(struct md_rdev *rdev, char *page) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3477 | { |
| 3478 | return badblocks_show(&rdev->badblocks, page, 1); |
| 3479 | } |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3480 | static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3481 | { |
| 3482 | return badblocks_store(&rdev->badblocks, page, len, 1); |
| 3483 | } |
| 3484 | static struct rdev_sysfs_entry rdev_unack_bad_blocks = |
| 3485 | __ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store); |
| 3486 | |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 3487 | static ssize_t |
| 3488 | ppl_sector_show(struct md_rdev *rdev, char *page) |
| 3489 | { |
| 3490 | return sprintf(page, "%llu\n", (unsigned long long)rdev->ppl.sector); |
| 3491 | } |
| 3492 | |
| 3493 | static ssize_t |
| 3494 | ppl_sector_store(struct md_rdev *rdev, const char *buf, size_t len) |
| 3495 | { |
| 3496 | unsigned long long sector; |
| 3497 | |
| 3498 | if (kstrtoull(buf, 10, §or) < 0) |
| 3499 | return -EINVAL; |
| 3500 | if (sector != (sector_t)sector) |
| 3501 | return -EINVAL; |
| 3502 | |
| 3503 | if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) && |
| 3504 | rdev->raid_disk >= 0) |
| 3505 | return -EBUSY; |
| 3506 | |
| 3507 | if (rdev->mddev->persistent) { |
| 3508 | if (rdev->mddev->major_version == 0) |
| 3509 | return -EINVAL; |
| 3510 | if ((sector > rdev->sb_start && |
| 3511 | sector - rdev->sb_start > S16_MAX) || |
| 3512 | (sector < rdev->sb_start && |
| 3513 | rdev->sb_start - sector > -S16_MIN)) |
| 3514 | return -EINVAL; |
| 3515 | rdev->ppl.offset = sector - rdev->sb_start; |
| 3516 | } else if (!rdev->mddev->external) { |
| 3517 | return -EBUSY; |
| 3518 | } |
| 3519 | rdev->ppl.sector = sector; |
| 3520 | return len; |
| 3521 | } |
| 3522 | |
| 3523 | static struct rdev_sysfs_entry rdev_ppl_sector = |
| 3524 | __ATTR(ppl_sector, S_IRUGO|S_IWUSR, ppl_sector_show, ppl_sector_store); |
| 3525 | |
| 3526 | static ssize_t |
| 3527 | ppl_size_show(struct md_rdev *rdev, char *page) |
| 3528 | { |
| 3529 | return sprintf(page, "%u\n", rdev->ppl.size); |
| 3530 | } |
| 3531 | |
| 3532 | static ssize_t |
| 3533 | ppl_size_store(struct md_rdev *rdev, const char *buf, size_t len) |
| 3534 | { |
| 3535 | unsigned int size; |
| 3536 | |
| 3537 | if (kstrtouint(buf, 10, &size) < 0) |
| 3538 | return -EINVAL; |
| 3539 | |
| 3540 | if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) && |
| 3541 | rdev->raid_disk >= 0) |
| 3542 | return -EBUSY; |
| 3543 | |
| 3544 | if (rdev->mddev->persistent) { |
| 3545 | if (rdev->mddev->major_version == 0) |
| 3546 | return -EINVAL; |
| 3547 | if (size > U16_MAX) |
| 3548 | return -EINVAL; |
| 3549 | } else if (!rdev->mddev->external) { |
| 3550 | return -EBUSY; |
| 3551 | } |
| 3552 | rdev->ppl.size = size; |
| 3553 | return len; |
| 3554 | } |
| 3555 | |
| 3556 | static struct rdev_sysfs_entry rdev_ppl_size = |
| 3557 | __ATTR(ppl_size, S_IRUGO|S_IWUSR, ppl_size_show, ppl_size_store); |
| 3558 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3559 | static struct attribute *rdev_default_attrs[] = { |
| 3560 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 3561 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3562 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3563 | &rdev_offset.attr, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3564 | &rdev_new_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3565 | &rdev_size.attr, |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3566 | &rdev_recovery_start.attr, |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3567 | &rdev_bad_blocks.attr, |
| 3568 | &rdev_unack_bad_blocks.attr, |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 3569 | &rdev_ppl_sector.attr, |
| 3570 | &rdev_ppl_size.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3571 | NULL, |
| 3572 | }; |
| 3573 | static ssize_t |
| 3574 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 3575 | { |
| 3576 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3577 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3578 | |
| 3579 | if (!entry->show) |
| 3580 | return -EIO; |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 3581 | if (!rdev->mddev) |
Marcos Paulo de Souza | 168b305 | 2019-06-14 15:41:06 -0700 | [diff] [blame] | 3582 | return -ENODEV; |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 3583 | return entry->show(rdev, page); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3584 | } |
| 3585 | |
| 3586 | static ssize_t |
| 3587 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 3588 | const char *page, size_t length) |
| 3589 | { |
| 3590 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3591 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3592 | ssize_t rv; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3593 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3594 | |
| 3595 | if (!entry->store) |
| 3596 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 3597 | if (!capable(CAP_SYS_ADMIN)) |
| 3598 | return -EACCES; |
Pawel Baldysiak | c42d324 | 2019-03-27 13:48:21 +0100 | [diff] [blame] | 3599 | rv = mddev ? mddev_lock(mddev) : -ENODEV; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 3600 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3601 | if (rdev->mddev == NULL) |
Pawel Baldysiak | c42d324 | 2019-03-27 13:48:21 +0100 | [diff] [blame] | 3602 | rv = -ENODEV; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3603 | else |
| 3604 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 3605 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 3606 | } |
| 3607 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3608 | } |
| 3609 | |
| 3610 | static void rdev_free(struct kobject *ko) |
| 3611 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3612 | struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3613 | kfree(rdev); |
| 3614 | } |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 3615 | static const struct sysfs_ops rdev_sysfs_ops = { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3616 | .show = rdev_attr_show, |
| 3617 | .store = rdev_attr_store, |
| 3618 | }; |
| 3619 | static struct kobj_type rdev_ktype = { |
| 3620 | .release = rdev_free, |
| 3621 | .sysfs_ops = &rdev_sysfs_ops, |
| 3622 | .default_attrs = rdev_default_attrs, |
| 3623 | }; |
| 3624 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3625 | int md_rdev_init(struct md_rdev *rdev) |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3626 | { |
| 3627 | rdev->desc_nr = -1; |
| 3628 | rdev->saved_raid_disk = -1; |
| 3629 | rdev->raid_disk = -1; |
| 3630 | rdev->flags = 0; |
| 3631 | rdev->data_offset = 0; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3632 | rdev->new_data_offset = 0; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3633 | rdev->sb_events = 0; |
Arnd Bergmann | 0e3ef49 | 2016-06-17 17:33:10 +0200 | [diff] [blame] | 3634 | rdev->last_read_error = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3635 | rdev->sb_loaded = 0; |
| 3636 | rdev->bb_page = NULL; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3637 | atomic_set(&rdev->nr_pending, 0); |
| 3638 | atomic_set(&rdev->read_errors, 0); |
| 3639 | atomic_set(&rdev->corrected_errors, 0); |
| 3640 | |
| 3641 | INIT_LIST_HEAD(&rdev->same_set); |
| 3642 | init_waitqueue_head(&rdev->blocked_wait); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3643 | |
| 3644 | /* Add space to store bad block list. |
| 3645 | * This reserves the space even on arrays where it cannot |
| 3646 | * be used - I wonder if that matters |
| 3647 | */ |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 3648 | return badblocks_init(&rdev->badblocks, 0); |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3649 | } |
| 3650 | EXPORT_SYMBOL_GPL(md_rdev_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3651 | /* |
| 3652 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 3653 | * |
| 3654 | * mark the device faulty if: |
| 3655 | * |
| 3656 | * - the device is nonexistent (zero size) |
| 3657 | * - the device has no valid superblock |
| 3658 | * |
| 3659 | * a faulty rdev _never_ has rdev->sb set. |
| 3660 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3661 | static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3662 | { |
| 3663 | char b[BDEVNAME_SIZE]; |
| 3664 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3665 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3666 | sector_t size; |
| 3667 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 3668 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3669 | if (!rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3670 | return ERR_PTR(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3671 | |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3672 | err = md_rdev_init(rdev); |
| 3673 | if (err) |
| 3674 | goto abort_free; |
| 3675 | err = alloc_disk_sb(rdev); |
| 3676 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3677 | goto abort_free; |
| 3678 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3679 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3680 | if (err) |
| 3681 | goto abort_free; |
| 3682 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 3683 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3684 | |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 3685 | size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3686 | if (!size) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3687 | pr_warn("md: %s has zero or unknown size, marking faulty!\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3688 | bdevname(rdev->bdev,b)); |
| 3689 | err = -EINVAL; |
| 3690 | goto abort_free; |
| 3691 | } |
| 3692 | |
| 3693 | if (super_format >= 0) { |
| 3694 | err = super_types[super_format]. |
| 3695 | load_super(rdev, NULL, super_minor); |
| 3696 | if (err == -EINVAL) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3697 | pr_warn("md: %s does not have a valid v%d.%d superblock, not importing!\n", |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 3698 | bdevname(rdev->bdev,b), |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3699 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3700 | goto abort_free; |
| 3701 | } |
| 3702 | if (err < 0) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3703 | pr_warn("md: could not read %s's sb, not importing!\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3704 | bdevname(rdev->bdev,b)); |
| 3705 | goto abort_free; |
| 3706 | } |
| 3707 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3708 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3709 | return rdev; |
| 3710 | |
| 3711 | abort_free: |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3712 | if (rdev->bdev) |
| 3713 | unlock_rdev(rdev); |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 3714 | md_rdev_clear(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3715 | kfree(rdev); |
| 3716 | return ERR_PTR(err); |
| 3717 | } |
| 3718 | |
| 3719 | /* |
| 3720 | * Check a full RAID array for plausibility |
| 3721 | */ |
| 3722 | |
Yufen Yu | 6a5cb53 | 2019-10-16 16:00:03 +0800 | [diff] [blame] | 3723 | static int analyze_sbs(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3724 | { |
| 3725 | int i; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3726 | struct md_rdev *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3727 | char b[BDEVNAME_SIZE]; |
| 3728 | |
| 3729 | freshest = NULL; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3730 | rdev_for_each_safe(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3731 | switch (super_types[mddev->major_version]. |
| 3732 | load_super(rdev, freshest, mddev->minor_version)) { |
| 3733 | case 1: |
| 3734 | freshest = rdev; |
| 3735 | break; |
| 3736 | case 0: |
| 3737 | break; |
| 3738 | default: |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3739 | pr_warn("md: fatal superblock inconsistency in %s -- removing from array\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3740 | bdevname(rdev->bdev,b)); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 3741 | md_kick_rdev_from_array(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3742 | } |
| 3743 | |
Yufen Yu | 6a5cb53 | 2019-10-16 16:00:03 +0800 | [diff] [blame] | 3744 | /* Cannot find a valid fresh disk */ |
| 3745 | if (!freshest) { |
| 3746 | pr_warn("md: cannot find a valid disk\n"); |
| 3747 | return -EINVAL; |
| 3748 | } |
| 3749 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3750 | super_types[mddev->major_version]. |
| 3751 | validate_super(mddev, freshest); |
| 3752 | |
| 3753 | i = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3754 | rdev_for_each_safe(rdev, tmp, mddev) { |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 3755 | if (mddev->max_disks && |
| 3756 | (rdev->desc_nr >= mddev->max_disks || |
| 3757 | i > mddev->max_disks)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3758 | pr_warn("md: %s: %s: only %d devices permitted\n", |
| 3759 | mdname(mddev), bdevname(rdev->bdev, b), |
| 3760 | mddev->max_disks); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 3761 | md_kick_rdev_from_array(rdev); |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 3762 | continue; |
| 3763 | } |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 3764 | if (rdev != freshest) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3765 | if (super_types[mddev->major_version]. |
| 3766 | validate_super(mddev, rdev)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3767 | pr_warn("md: kicking non-fresh %s from array!\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3768 | bdevname(rdev->bdev,b)); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 3769 | md_kick_rdev_from_array(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3770 | continue; |
| 3771 | } |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 3772 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3773 | if (mddev->level == LEVEL_MULTIPATH) { |
| 3774 | rdev->desc_nr = i++; |
| 3775 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3776 | set_bit(In_sync, &rdev->flags); |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 3777 | } else if (rdev->raid_disk >= |
| 3778 | (mddev->raid_disks - min(0, mddev->delta_disks)) && |
| 3779 | !test_bit(Journal, &rdev->flags)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 3780 | rdev->raid_disk = -1; |
| 3781 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3782 | } |
| 3783 | } |
Yufen Yu | 6a5cb53 | 2019-10-16 16:00:03 +0800 | [diff] [blame] | 3784 | |
| 3785 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3786 | } |
| 3787 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3788 | /* Read a fixed-point number. |
| 3789 | * Numbers in sysfs attributes should be in "standard" units where |
| 3790 | * possible, so time should be in seconds. |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3791 | * However we internally use a a much smaller unit such as |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3792 | * milliseconds or jiffies. |
| 3793 | * This function takes a decimal number with a possible fractional |
| 3794 | * component, and produces an integer which is the result of |
| 3795 | * multiplying that number by 10^'scale'. |
| 3796 | * all without any floating-point arithmetic. |
| 3797 | */ |
| 3798 | int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale) |
| 3799 | { |
| 3800 | unsigned long result = 0; |
| 3801 | long decimals = -1; |
| 3802 | while (isdigit(*cp) || (*cp == '.' && decimals < 0)) { |
| 3803 | if (*cp == '.') |
| 3804 | decimals = 0; |
| 3805 | else if (decimals < scale) { |
| 3806 | unsigned int value; |
| 3807 | value = *cp - '0'; |
| 3808 | result = result * 10 + value; |
| 3809 | if (decimals >= 0) |
| 3810 | decimals++; |
| 3811 | } |
| 3812 | cp++; |
| 3813 | } |
| 3814 | if (*cp == '\n') |
| 3815 | cp++; |
| 3816 | if (*cp) |
| 3817 | return -EINVAL; |
| 3818 | if (decimals < 0) |
| 3819 | decimals = 0; |
Andy Shevchenko | cf89160 | 2019-07-23 23:41:55 +0300 | [diff] [blame] | 3820 | *res = result * int_pow(10, scale - decimals); |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3821 | return 0; |
| 3822 | } |
| 3823 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3824 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3825 | safe_delay_show(struct mddev *mddev, char *page) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3826 | { |
| 3827 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 3828 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 3829 | } |
| 3830 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3831 | safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3832 | { |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3833 | unsigned long msec; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 3834 | |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 3835 | if (mddev_is_clustered(mddev)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3836 | pr_warn("md: Safemode is disabled for clustered mode\n"); |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 3837 | return -EINVAL; |
| 3838 | } |
| 3839 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3840 | if (strict_strtoul_scaled(cbuf, &msec, 3) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3841 | return -EINVAL; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3842 | if (msec == 0) |
| 3843 | mddev->safemode_delay = 0; |
| 3844 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3845 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 1b30e66 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 3846 | unsigned long new_delay = (msec*HZ)/1000; |
| 3847 | |
| 3848 | if (new_delay == 0) |
| 3849 | new_delay = 1; |
| 3850 | mddev->safemode_delay = new_delay; |
| 3851 | if (new_delay < old_delay || old_delay == 0) |
| 3852 | mod_timer(&mddev->safemode_timer, jiffies+1); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3853 | } |
| 3854 | return len; |
| 3855 | } |
| 3856 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3857 | __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] | 3858 | |
| 3859 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3860 | level_show(struct mddev *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3861 | { |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3862 | struct md_personality *p; |
| 3863 | int ret; |
| 3864 | spin_lock(&mddev->lock); |
| 3865 | p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3866 | if (p) |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3867 | ret = sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3868 | else if (mddev->clevel[0]) |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3869 | ret = sprintf(page, "%s\n", mddev->clevel); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3870 | else if (mddev->level != LEVEL_NONE) |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3871 | ret = sprintf(page, "%d\n", mddev->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3872 | else |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3873 | ret = 0; |
| 3874 | spin_unlock(&mddev->lock); |
| 3875 | return ret; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3876 | } |
| 3877 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3878 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3879 | level_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3880 | { |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3881 | char clevel[16]; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3882 | ssize_t rv; |
| 3883 | size_t slen = len; |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3884 | struct md_personality *pers, *oldpers; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3885 | long level; |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3886 | void *priv, *oldpriv; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3887 | struct md_rdev *rdev; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3888 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3889 | if (slen == 0 || slen >= sizeof(clevel)) |
| 3890 | return -EINVAL; |
| 3891 | |
| 3892 | rv = mddev_lock(mddev); |
| 3893 | if (rv) |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3894 | return rv; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3895 | |
| 3896 | if (mddev->pers == NULL) { |
| 3897 | strncpy(mddev->clevel, buf, slen); |
| 3898 | if (mddev->clevel[slen-1] == '\n') |
| 3899 | slen--; |
| 3900 | mddev->clevel[slen] = 0; |
| 3901 | mddev->level = LEVEL_NONE; |
| 3902 | rv = len; |
| 3903 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3904 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3905 | rv = -EROFS; |
NeilBrown | bd8839e | 2014-05-28 13:39:21 +1000 | [diff] [blame] | 3906 | if (mddev->ro) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3907 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3908 | |
| 3909 | /* request to change the personality. Need to ensure: |
| 3910 | * - array is not engaged in resync/recovery/reshape |
| 3911 | * - old personality can be suspended |
| 3912 | * - new personality will access other array. |
| 3913 | */ |
| 3914 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3915 | rv = -EBUSY; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 3916 | if (mddev->sync_thread || |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 3917 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 3918 | mddev->reshape_position != MaxSector || |
| 3919 | mddev->sysfs_active) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3920 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3921 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3922 | rv = -EINVAL; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3923 | if (!mddev->pers->quiesce) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3924 | pr_warn("md: %s: %s does not support online personality change\n", |
| 3925 | mdname(mddev), mddev->pers->name); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3926 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3927 | } |
| 3928 | |
| 3929 | /* Now find the new personality */ |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3930 | strncpy(clevel, buf, slen); |
| 3931 | if (clevel[slen-1] == '\n') |
| 3932 | slen--; |
| 3933 | clevel[slen] = 0; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 3934 | if (kstrtol(clevel, 10, &level)) |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3935 | level = LEVEL_NONE; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3936 | |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3937 | if (request_module("md-%s", clevel) != 0) |
| 3938 | request_module("md-level-%s", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3939 | spin_lock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3940 | pers = find_pers(level, clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3941 | if (!pers || !try_module_get(pers->owner)) { |
| 3942 | spin_unlock(&pers_lock); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3943 | pr_warn("md: personality %s not loaded\n", clevel); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3944 | rv = -EINVAL; |
| 3945 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3946 | } |
| 3947 | spin_unlock(&pers_lock); |
| 3948 | |
| 3949 | if (pers == mddev->pers) { |
| 3950 | /* Nothing to do! */ |
| 3951 | module_put(pers->owner); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3952 | rv = len; |
| 3953 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3954 | } |
| 3955 | if (!pers->takeover) { |
| 3956 | module_put(pers->owner); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3957 | pr_warn("md: %s: %s does not support personality takeover\n", |
| 3958 | mdname(mddev), clevel); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3959 | rv = -EINVAL; |
| 3960 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3961 | } |
| 3962 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3963 | rdev_for_each(rdev, mddev) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3964 | rdev->new_raid_disk = rdev->raid_disk; |
| 3965 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3966 | /* ->takeover must set new_* and/or delta_disks |
| 3967 | * if it succeeds, and may set them when it fails. |
| 3968 | */ |
| 3969 | priv = pers->takeover(mddev); |
| 3970 | if (IS_ERR(priv)) { |
| 3971 | mddev->new_level = mddev->level; |
| 3972 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3973 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3974 | mddev->raid_disks -= mddev->delta_disks; |
| 3975 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3976 | mddev->reshape_backwards = 0; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3977 | module_put(pers->owner); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3978 | pr_warn("md: %s: %s would not accept array\n", |
| 3979 | mdname(mddev), clevel); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3980 | rv = PTR_ERR(priv); |
| 3981 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3982 | } |
| 3983 | |
| 3984 | /* Looks like we have a winner */ |
| 3985 | mddev_suspend(mddev); |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 3986 | mddev_detach(mddev); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3987 | |
| 3988 | spin_lock(&mddev->lock); |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3989 | oldpers = mddev->pers; |
| 3990 | oldpriv = mddev->private; |
| 3991 | mddev->pers = pers; |
| 3992 | mddev->private = priv; |
| 3993 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
| 3994 | mddev->level = mddev->new_level; |
| 3995 | mddev->layout = mddev->new_layout; |
| 3996 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
| 3997 | mddev->delta_disks = 0; |
| 3998 | mddev->reshape_backwards = 0; |
| 3999 | mddev->degraded = 0; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 4000 | spin_unlock(&mddev->lock); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 4001 | |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 4002 | if (oldpers->sync_request == NULL && |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 4003 | mddev->external) { |
| 4004 | /* We are converting from a no-redundancy array |
| 4005 | * to a redundancy array and metadata is managed |
| 4006 | * externally so we need to be sure that writes |
| 4007 | * won't block due to a need to transition |
| 4008 | * clean->dirty |
| 4009 | * until external management is started. |
| 4010 | */ |
| 4011 | mddev->in_sync = 0; |
| 4012 | mddev->safemode_delay = 0; |
| 4013 | mddev->safemode = 0; |
| 4014 | } |
| 4015 | |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 4016 | oldpers->free(mddev, oldpriv); |
| 4017 | |
| 4018 | if (oldpers->sync_request == NULL && |
| 4019 | pers->sync_request != NULL) { |
| 4020 | /* need to add the md_redundancy_group */ |
| 4021 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 4022 | pr_warn("md: cannot register extra attributes for %s\n", |
| 4023 | mdname(mddev)); |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 4024 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action"); |
| 4025 | } |
| 4026 | if (oldpers->sync_request != NULL && |
| 4027 | pers->sync_request == NULL) { |
| 4028 | /* need to remove the md_redundancy_group */ |
| 4029 | if (mddev->to_remove == NULL) |
| 4030 | mddev->to_remove = &md_redundancy_group; |
| 4031 | } |
| 4032 | |
Alexey Obitotskiy | 4cb9da7 | 2016-06-23 12:11:01 +0200 | [diff] [blame] | 4033 | module_put(oldpers->owner); |
| 4034 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 4035 | rdev_for_each(rdev, mddev) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 4036 | if (rdev->raid_disk < 0) |
| 4037 | continue; |
NeilBrown | bf2cb0d | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4038 | if (rdev->new_raid_disk >= mddev->raid_disks) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 4039 | rdev->new_raid_disk = -1; |
| 4040 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 4041 | continue; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4042 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 4043 | } |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 4044 | rdev_for_each(rdev, mddev) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 4045 | if (rdev->raid_disk < 0) |
| 4046 | continue; |
| 4047 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 4048 | continue; |
| 4049 | rdev->raid_disk = rdev->new_raid_disk; |
| 4050 | if (rdev->raid_disk < 0) |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 4051 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 4052 | else { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4053 | if (sysfs_link_rdev(mddev, rdev)) |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 4054 | pr_warn("md: cannot register rd%d for %s after level change\n", |
| 4055 | rdev->raid_disk, mdname(mddev)); |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 4056 | } |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 4057 | } |
| 4058 | |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 4059 | if (pers->sync_request == NULL) { |
Trela, Maciej | 9af204c | 2010-03-08 16:02:44 +1100 | [diff] [blame] | 4060 | /* this is now an array without redundancy, so |
| 4061 | * it must always be in_sync |
| 4062 | */ |
| 4063 | mddev->in_sync = 1; |
| 4064 | del_timer_sync(&mddev->safemode_timer); |
| 4065 | } |
NeilBrown | 02e5f5c | 2013-11-14 15:16:15 +1100 | [diff] [blame] | 4066 | blk_set_stacking_limits(&mddev->queue->limits); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4067 | pers->run(mddev); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 4068 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
Jonathan Brassow | 47525e5 | 2012-05-22 13:55:29 +1000 | [diff] [blame] | 4069 | mddev_resume(mddev); |
NeilBrown | 830778a | 2014-01-14 15:17:03 +1100 | [diff] [blame] | 4070 | if (!mddev->thread) |
| 4071 | md_update_sb(mddev, 1); |
Maciej Trela | 5cac7861 | 2010-04-14 17:17:39 +1000 | [diff] [blame] | 4072 | sysfs_notify(&mddev->kobj, NULL, "level"); |
Dan Williams | bb7f8d2 | 2010-05-01 18:14:57 -0700 | [diff] [blame] | 4073 | md_new_event(mddev); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4074 | rv = len; |
| 4075 | out_unlock: |
| 4076 | mddev_unlock(mddev); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4077 | return rv; |
| 4078 | } |
| 4079 | |
| 4080 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4081 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4082 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4083 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4084 | layout_show(struct mddev *mddev, char *page) |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4085 | { |
| 4086 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4087 | if (mddev->reshape_position != MaxSector && |
| 4088 | mddev->layout != mddev->new_layout) |
| 4089 | return sprintf(page, "%d (%d)\n", |
| 4090 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4091 | return sprintf(page, "%d\n", mddev->layout); |
| 4092 | } |
| 4093 | |
| 4094 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4095 | layout_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4096 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4097 | unsigned int n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4098 | int err; |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4099 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4100 | err = kstrtouint(buf, 10, &n); |
| 4101 | if (err < 0) |
| 4102 | return err; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4103 | err = mddev_lock(mddev); |
| 4104 | if (err) |
| 4105 | return err; |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4106 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 4107 | if (mddev->pers) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 4108 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4109 | err = -EBUSY; |
| 4110 | else if (mddev->ro) |
| 4111 | err = -EROFS; |
| 4112 | else { |
| 4113 | mddev->new_layout = n; |
| 4114 | err = mddev->pers->check_reshape(mddev); |
| 4115 | if (err) |
| 4116 | mddev->new_layout = mddev->layout; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 4117 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 4118 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4119 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 4120 | if (mddev->reshape_position == MaxSector) |
| 4121 | mddev->layout = n; |
| 4122 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4123 | mddev_unlock(mddev); |
| 4124 | return err ?: len; |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4125 | } |
| 4126 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4127 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4128 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4129 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4130 | raid_disks_show(struct mddev *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4131 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 4132 | if (mddev->raid_disks == 0) |
| 4133 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4134 | if (mddev->reshape_position != MaxSector && |
| 4135 | mddev->delta_disks != 0) |
| 4136 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 4137 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4138 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 4139 | } |
| 4140 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4141 | static int update_raid_disks(struct mddev *mddev, int raid_disks); |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4142 | |
| 4143 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4144 | raid_disks_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4145 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4146 | unsigned int n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4147 | int err; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4148 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4149 | err = kstrtouint(buf, 10, &n); |
| 4150 | if (err < 0) |
| 4151 | return err; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4152 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4153 | err = mddev_lock(mddev); |
| 4154 | if (err) |
| 4155 | return err; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4156 | if (mddev->pers) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4157 | err = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4158 | else if (mddev->reshape_position != MaxSector) { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4159 | struct md_rdev *rdev; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4160 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4161 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4162 | err = -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4163 | rdev_for_each(rdev, mddev) { |
| 4164 | if (olddisks < n && |
| 4165 | rdev->data_offset < rdev->new_data_offset) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4166 | goto out_unlock; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4167 | if (olddisks > n && |
| 4168 | rdev->data_offset > rdev->new_data_offset) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4169 | goto out_unlock; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4170 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4171 | err = 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4172 | mddev->delta_disks = n - olddisks; |
| 4173 | mddev->raid_disks = n; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4174 | mddev->reshape_backwards = (mddev->delta_disks < 0); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4175 | } else |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4176 | mddev->raid_disks = n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4177 | out_unlock: |
| 4178 | mddev_unlock(mddev); |
| 4179 | return err ? err : len; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4180 | } |
| 4181 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4182 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4183 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4184 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4185 | chunk_size_show(struct mddev *mddev, char *page) |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4186 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4187 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4188 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 4189 | return sprintf(page, "%d (%d)\n", |
| 4190 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4191 | mddev->chunk_sectors << 9); |
| 4192 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4193 | } |
| 4194 | |
| 4195 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4196 | chunk_size_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4197 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4198 | unsigned long n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4199 | int err; |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4200 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4201 | err = kstrtoul(buf, 10, &n); |
| 4202 | if (err < 0) |
| 4203 | return err; |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4204 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4205 | err = mddev_lock(mddev); |
| 4206 | if (err) |
| 4207 | return err; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 4208 | if (mddev->pers) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 4209 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4210 | err = -EBUSY; |
| 4211 | else if (mddev->ro) |
| 4212 | err = -EROFS; |
| 4213 | else { |
| 4214 | mddev->new_chunk_sectors = n >> 9; |
| 4215 | err = mddev->pers->check_reshape(mddev); |
| 4216 | if (err) |
| 4217 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 4218 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 4219 | } else { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4220 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 4221 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4222 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 4223 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4224 | mddev_unlock(mddev); |
| 4225 | return err ?: len; |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4226 | } |
| 4227 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4228 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4229 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4230 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4231 | resync_start_show(struct mddev *mddev, char *page) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4232 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 4233 | if (mddev->recovery_cp == MaxSector) |
| 4234 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4235 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 4236 | } |
| 4237 | |
| 4238 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4239 | resync_start_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4240 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4241 | unsigned long long n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4242 | int err; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4243 | |
| 4244 | if (cmd_match(buf, "none")) |
| 4245 | n = MaxSector; |
| 4246 | else { |
| 4247 | err = kstrtoull(buf, 10, &n); |
| 4248 | if (err < 0) |
| 4249 | return err; |
| 4250 | if (n != (sector_t)n) |
| 4251 | return -EINVAL; |
| 4252 | } |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4253 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4254 | err = mddev_lock(mddev); |
| 4255 | if (err) |
| 4256 | return err; |
NeilBrown | b098636 | 2011-05-11 15:52:21 +1000 | [diff] [blame] | 4257 | if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4258 | err = -EBUSY; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4259 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4260 | if (!err) { |
| 4261 | mddev->recovery_cp = n; |
| 4262 | if (mddev->pers) |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 4263 | set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4264 | } |
| 4265 | mddev_unlock(mddev); |
| 4266 | return err ?: len; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4267 | } |
| 4268 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 4269 | __ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR, |
| 4270 | resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4271 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4272 | /* |
| 4273 | * The array state can be: |
| 4274 | * |
| 4275 | * clear |
| 4276 | * No devices, no size, no level |
| 4277 | * Equivalent to STOP_ARRAY ioctl |
| 4278 | * inactive |
| 4279 | * May have some settings, but array is not active |
| 4280 | * all IO results in error |
| 4281 | * When written, doesn't tear down array, but just stops it |
| 4282 | * suspended (not supported yet) |
| 4283 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 4284 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4285 | * readonly |
| 4286 | * no resync can happen. no superblocks get written. |
| 4287 | * write requests fail |
| 4288 | * read-auto |
| 4289 | * like readonly, but behaves like 'clean' on a write request. |
| 4290 | * |
| 4291 | * clean - no pending writes, but otherwise active. |
| 4292 | * When written to inactive array, starts without resync |
| 4293 | * If a write request arrives then |
| 4294 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 4295 | * if not known, block and switch to write-pending |
| 4296 | * If written to an active array that has pending writes, then fails. |
| 4297 | * active |
| 4298 | * fully active: IO and resync can be happening. |
| 4299 | * When written to inactive array, starts with resync |
| 4300 | * |
| 4301 | * write-pending |
| 4302 | * clean, but writes are blocked waiting for 'active' to be written. |
| 4303 | * |
| 4304 | * active-idle |
| 4305 | * like active, but no writes have been seen for a while (100msec). |
| 4306 | * |
Guilherme G. Piccoli | 62f7b19 | 2019-09-03 16:49:00 -0300 | [diff] [blame] | 4307 | * broken |
| 4308 | * RAID0/LINEAR-only: same as clean, but array is missing a member. |
| 4309 | * It's useful because RAID0/LINEAR mounted-arrays aren't stopped |
| 4310 | * when a member is gone, so this state will at least alert the |
| 4311 | * user that something is wrong. |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4312 | */ |
| 4313 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
Guilherme G. Piccoli | 62f7b19 | 2019-09-03 16:49:00 -0300 | [diff] [blame] | 4314 | write_pending, active_idle, broken, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 4315 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4316 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
Guilherme G. Piccoli | 62f7b19 | 2019-09-03 16:49:00 -0300 | [diff] [blame] | 4317 | "write-pending", "active-idle", "broken", NULL }; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4318 | |
| 4319 | static int match_word(const char *word, char **list) |
| 4320 | { |
| 4321 | int n; |
| 4322 | for (n=0; list[n]; n++) |
| 4323 | if (cmd_match(word, list[n])) |
| 4324 | break; |
| 4325 | return n; |
| 4326 | } |
| 4327 | |
| 4328 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4329 | array_state_show(struct mddev *mddev, char *page) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4330 | { |
| 4331 | enum array_state st = inactive; |
| 4332 | |
Guilherme G. Piccoli | 62f7b19 | 2019-09-03 16:49:00 -0300 | [diff] [blame] | 4333 | if (mddev->pers && !test_bit(MD_NOT_READY, &mddev->flags)) { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4334 | switch(mddev->ro) { |
| 4335 | case 1: |
| 4336 | st = readonly; |
| 4337 | break; |
| 4338 | case 2: |
| 4339 | st = read_auto; |
| 4340 | break; |
| 4341 | case 0: |
NeilBrown | 55cc39f | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 4342 | spin_lock(&mddev->lock); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 4343 | if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4344 | st = write_pending; |
Tomasz Majchrzak | 16f8894 | 2016-10-24 12:47:28 +0200 | [diff] [blame] | 4345 | else if (mddev->in_sync) |
| 4346 | st = clean; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4347 | else if (mddev->safemode) |
| 4348 | st = active_idle; |
| 4349 | else |
| 4350 | st = active; |
NeilBrown | 55cc39f | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 4351 | spin_unlock(&mddev->lock); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4352 | } |
Guilherme G. Piccoli | 62f7b19 | 2019-09-03 16:49:00 -0300 | [diff] [blame] | 4353 | |
| 4354 | if (test_bit(MD_BROKEN, &mddev->flags) && st == clean) |
| 4355 | st = broken; |
| 4356 | } else { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4357 | if (list_empty(&mddev->disks) && |
| 4358 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4359 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4360 | st = clear; |
| 4361 | else |
| 4362 | st = inactive; |
| 4363 | } |
| 4364 | return sprintf(page, "%s\n", array_states[st]); |
| 4365 | } |
| 4366 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 4367 | static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev); |
| 4368 | static int md_set_readonly(struct mddev *mddev, struct block_device *bdev); |
| 4369 | static int do_md_run(struct mddev *mddev); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4370 | static int restart_array(struct mddev *mddev); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4371 | |
| 4372 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4373 | array_state_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4374 | { |
NeilBrown | 6497709b | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 4375 | int err = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4376 | enum array_state st = match_word(buf, array_states); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4377 | |
| 4378 | if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) { |
| 4379 | /* don't take reconfig_mutex when toggling between |
| 4380 | * clean and active |
| 4381 | */ |
| 4382 | spin_lock(&mddev->lock); |
| 4383 | if (st == active) { |
| 4384 | restart_array(mddev); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 4385 | clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags); |
Tomasz Majchrzak | 91a6c4a | 2016-10-25 17:07:08 +0200 | [diff] [blame] | 4386 | md_wakeup_thread(mddev->thread); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4387 | wake_up(&mddev->sb_wait); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4388 | } else /* st == clean */ { |
| 4389 | restart_array(mddev); |
NeilBrown | 6497709b | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 4390 | if (!set_in_sync(mddev)) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4391 | err = -EBUSY; |
| 4392 | } |
Tomasz Majchrzak | 573275b | 2016-06-30 10:47:09 +0200 | [diff] [blame] | 4393 | if (!err) |
| 4394 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4395 | spin_unlock(&mddev->lock); |
NeilBrown | c008f1d | 2015-06-12 19:46:44 +1000 | [diff] [blame] | 4396 | return err ?: len; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4397 | } |
| 4398 | err = mddev_lock(mddev); |
| 4399 | if (err) |
| 4400 | return err; |
| 4401 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4402 | switch(st) { |
| 4403 | case bad_word: |
| 4404 | break; |
| 4405 | case clear: |
| 4406 | /* stopping an active array */ |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 4407 | err = do_md_stop(mddev, 0, NULL); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4408 | break; |
| 4409 | case inactive: |
| 4410 | /* stopping an active array */ |
NeilBrown | 90cf195 | 2012-07-31 10:04:55 +1000 | [diff] [blame] | 4411 | if (mddev->pers) |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 4412 | err = do_md_stop(mddev, 2, NULL); |
NeilBrown | 90cf195 | 2012-07-31 10:04:55 +1000 | [diff] [blame] | 4413 | else |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4414 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4415 | break; |
| 4416 | case suspended: |
| 4417 | break; /* not supported yet */ |
| 4418 | case readonly: |
| 4419 | if (mddev->pers) |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 4420 | err = md_set_readonly(mddev, NULL); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4421 | else { |
| 4422 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 4423 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4424 | err = do_md_run(mddev); |
| 4425 | } |
| 4426 | break; |
| 4427 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4428 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4429 | if (mddev->ro == 0) |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 4430 | err = md_set_readonly(mddev, NULL); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4431 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 4432 | err = restart_array(mddev); |
| 4433 | if (err == 0) { |
| 4434 | mddev->ro = 2; |
| 4435 | set_disk_ro(mddev->gendisk, 0); |
| 4436 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4437 | } else { |
| 4438 | mddev->ro = 2; |
| 4439 | err = do_md_run(mddev); |
| 4440 | } |
| 4441 | break; |
| 4442 | case clean: |
| 4443 | if (mddev->pers) { |
Song Liu | 339421d | 2015-10-08 21:54:13 -0700 | [diff] [blame] | 4444 | err = restart_array(mddev); |
| 4445 | if (err) |
| 4446 | break; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 4447 | spin_lock(&mddev->lock); |
NeilBrown | 6497709b | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 4448 | if (!set_in_sync(mddev)) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4449 | err = -EBUSY; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 4450 | spin_unlock(&mddev->lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 4451 | } else |
| 4452 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4453 | break; |
| 4454 | case active: |
| 4455 | if (mddev->pers) { |
Song Liu | 339421d | 2015-10-08 21:54:13 -0700 | [diff] [blame] | 4456 | err = restart_array(mddev); |
| 4457 | if (err) |
| 4458 | break; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 4459 | clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4460 | wake_up(&mddev->sb_wait); |
| 4461 | err = 0; |
| 4462 | } else { |
| 4463 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 4464 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4465 | err = do_md_run(mddev); |
| 4466 | } |
| 4467 | break; |
| 4468 | case write_pending: |
| 4469 | case active_idle: |
Guilherme G. Piccoli | 62f7b19 | 2019-09-03 16:49:00 -0300 | [diff] [blame] | 4470 | case broken: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4471 | /* these cannot be set */ |
| 4472 | break; |
| 4473 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4474 | |
| 4475 | if (!err) { |
NeilBrown | 1d23f17 | 2011-12-08 15:49:12 +1100 | [diff] [blame] | 4476 | if (mddev->hold_active == UNTIL_IOCTL) |
| 4477 | mddev->hold_active = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4478 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4479 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4480 | mddev_unlock(mddev); |
| 4481 | return err ?: len; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4482 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4483 | static struct md_sysfs_entry md_array_state = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 4484 | __ATTR_PREALLOC(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4485 | |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4486 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4487 | max_corrected_read_errors_show(struct mddev *mddev, char *page) { |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4488 | return sprintf(page, "%d\n", |
| 4489 | atomic_read(&mddev->max_corr_read_errors)); |
| 4490 | } |
| 4491 | |
| 4492 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4493 | max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len) |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4494 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4495 | unsigned int n; |
| 4496 | int rv; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4497 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4498 | rv = kstrtouint(buf, 10, &n); |
| 4499 | if (rv < 0) |
| 4500 | return rv; |
| 4501 | atomic_set(&mddev->max_corr_read_errors, n); |
| 4502 | return len; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4503 | } |
| 4504 | |
| 4505 | static struct md_sysfs_entry max_corr_read_errors = |
| 4506 | __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show, |
| 4507 | max_corrected_read_errors_store); |
| 4508 | |
| 4509 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4510 | null_show(struct mddev *mddev, char *page) |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4511 | { |
| 4512 | return -EINVAL; |
| 4513 | } |
| 4514 | |
| 4515 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4516 | new_dev_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4517 | { |
| 4518 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 4519 | /* The new device is added to the array. |
| 4520 | * If the array has a persistent superblock, we read the |
| 4521 | * superblock to initialise info and check validity. |
| 4522 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 4523 | * which mainly checks size. |
| 4524 | */ |
| 4525 | char *e; |
| 4526 | int major = simple_strtoul(buf, &e, 10); |
| 4527 | int minor; |
| 4528 | dev_t dev; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4529 | struct md_rdev *rdev; |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4530 | int err; |
| 4531 | |
| 4532 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 4533 | return -EINVAL; |
| 4534 | minor = simple_strtoul(e+1, &e, 10); |
| 4535 | if (*e && *e != '\n') |
| 4536 | return -EINVAL; |
| 4537 | dev = MKDEV(major, minor); |
| 4538 | if (major != MAJOR(dev) || |
| 4539 | minor != MINOR(dev)) |
| 4540 | return -EOVERFLOW; |
| 4541 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4542 | flush_workqueue(md_misc_wq); |
| 4543 | |
| 4544 | err = mddev_lock(mddev); |
| 4545 | if (err) |
| 4546 | return err; |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4547 | if (mddev->persistent) { |
| 4548 | rdev = md_import_device(dev, mddev->major_version, |
| 4549 | mddev->minor_version); |
| 4550 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4551 | struct md_rdev *rdev0 |
| 4552 | = list_entry(mddev->disks.next, |
| 4553 | struct md_rdev, same_set); |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4554 | err = super_types[mddev->major_version] |
| 4555 | .load_super(rdev, rdev0, mddev->minor_version); |
| 4556 | if (err < 0) |
| 4557 | goto out; |
| 4558 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 4559 | } else if (mddev->external) |
| 4560 | rdev = md_import_device(dev, -2, -1); |
| 4561 | else |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4562 | rdev = md_import_device(dev, -1, -1); |
| 4563 | |
NeilBrown | 9a8c0fa | 2015-06-25 17:06:40 +1000 | [diff] [blame] | 4564 | if (IS_ERR(rdev)) { |
| 4565 | mddev_unlock(mddev); |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4566 | return PTR_ERR(rdev); |
NeilBrown | 9a8c0fa | 2015-06-25 17:06:40 +1000 | [diff] [blame] | 4567 | } |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4568 | err = bind_rdev_to_array(rdev, mddev); |
| 4569 | out: |
| 4570 | if (err) |
| 4571 | export_rdev(rdev); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4572 | mddev_unlock(mddev); |
Alexey Obitotskiy | 5492c46 | 2017-07-28 15:49:25 +0200 | [diff] [blame] | 4573 | if (!err) |
| 4574 | md_new_event(mddev); |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4575 | return err ? err : len; |
| 4576 | } |
| 4577 | |
| 4578 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4579 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4580 | |
| 4581 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4582 | bitmap_store(struct mddev *mddev, const char *buf, size_t len) |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4583 | { |
| 4584 | char *end; |
| 4585 | unsigned long chunk, end_chunk; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4586 | int err; |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4587 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4588 | err = mddev_lock(mddev); |
| 4589 | if (err) |
| 4590 | return err; |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4591 | if (!mddev->bitmap) |
| 4592 | goto out; |
| 4593 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 4594 | while (*buf) { |
| 4595 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 4596 | if (buf == end) break; |
| 4597 | if (*end == '-') { /* range */ |
| 4598 | buf = end + 1; |
| 4599 | end_chunk = simple_strtoul(buf, &end, 0); |
| 4600 | if (buf == end) break; |
| 4601 | } |
| 4602 | if (*end && !isspace(*end)) break; |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 4603 | md_bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 4604 | buf = skip_spaces(end); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4605 | } |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 4606 | md_bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4607 | out: |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4608 | mddev_unlock(mddev); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4609 | return len; |
| 4610 | } |
| 4611 | |
| 4612 | static struct md_sysfs_entry md_bitmap = |
| 4613 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 4614 | |
| 4615 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4616 | size_show(struct mddev *mddev, char *page) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4617 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4618 | return sprintf(page, "%llu\n", |
| 4619 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4620 | } |
| 4621 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4622 | static int update_size(struct mddev *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4623 | |
| 4624 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4625 | size_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4626 | { |
| 4627 | /* If array is inactive, we can reduce the component size, but |
| 4628 | * not increase it (except from 0). |
| 4629 | * If array is active, we can try an on-line resize |
| 4630 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4631 | sector_t sectors; |
| 4632 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4633 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4634 | if (err < 0) |
| 4635 | return err; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4636 | err = mddev_lock(mddev); |
| 4637 | if (err) |
| 4638 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4639 | if (mddev->pers) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4640 | err = update_size(mddev, sectors); |
Xiao Ni | 4ba1e78 | 2016-06-12 17:18:00 +0800 | [diff] [blame] | 4641 | if (err == 0) |
| 4642 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4643 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4644 | if (mddev->dev_sectors == 0 || |
| 4645 | mddev->dev_sectors > sectors) |
| 4646 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4647 | else |
| 4648 | err = -ENOSPC; |
| 4649 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4650 | mddev_unlock(mddev); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4651 | return err ? err : len; |
| 4652 | } |
| 4653 | |
| 4654 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4655 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4656 | |
Masanari Iida | 83f0d77 | 2012-10-30 00:18:08 +0900 | [diff] [blame] | 4657 | /* Metadata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4658 | * This is one of |
| 4659 | * 'none' for arrays with no metadata (good luck...) |
| 4660 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4661 | * or N.M for internally known formats |
| 4662 | */ |
| 4663 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4664 | metadata_show(struct mddev *mddev, char *page) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4665 | { |
| 4666 | if (mddev->persistent) |
| 4667 | return sprintf(page, "%d.%d\n", |
| 4668 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4669 | else if (mddev->external) |
| 4670 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4671 | else |
| 4672 | return sprintf(page, "none\n"); |
| 4673 | } |
| 4674 | |
| 4675 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4676 | metadata_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4677 | { |
| 4678 | int major, minor; |
| 4679 | char *e; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4680 | int err; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 4681 | /* Changing the details of 'external' metadata is |
| 4682 | * always permitted. Otherwise there must be |
| 4683 | * no devices attached to the array. |
| 4684 | */ |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4685 | |
| 4686 | err = mddev_lock(mddev); |
| 4687 | if (err) |
| 4688 | return err; |
| 4689 | err = -EBUSY; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 4690 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 4691 | ; |
| 4692 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4693 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4694 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4695 | err = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4696 | if (cmd_match(buf, "none")) { |
| 4697 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4698 | mddev->external = 0; |
| 4699 | mddev->major_version = 0; |
| 4700 | mddev->minor_version = 90; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4701 | goto out_unlock; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4702 | } |
| 4703 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 4704 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4705 | if (namelen >= sizeof(mddev->metadata_type)) |
| 4706 | namelen = sizeof(mddev->metadata_type)-1; |
| 4707 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 4708 | mddev->metadata_type[namelen] = 0; |
| 4709 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 4710 | mddev->metadata_type[--namelen] = 0; |
| 4711 | mddev->persistent = 0; |
| 4712 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4713 | mddev->major_version = 0; |
| 4714 | mddev->minor_version = 90; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4715 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4716 | } |
| 4717 | major = simple_strtoul(buf, &e, 10); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4718 | err = -EINVAL; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4719 | if (e==buf || *e != '.') |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4720 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4721 | buf = e+1; |
| 4722 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 4723 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4724 | goto out_unlock; |
| 4725 | err = -ENOENT; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 4726 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4727 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4728 | mddev->major_version = major; |
| 4729 | mddev->minor_version = minor; |
| 4730 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4731 | mddev->external = 0; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4732 | err = 0; |
| 4733 | out_unlock: |
| 4734 | mddev_unlock(mddev); |
| 4735 | return err ?: len; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4736 | } |
| 4737 | |
| 4738 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 4739 | __ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4740 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4741 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4742 | action_show(struct mddev *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4743 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4744 | char *type = "idle"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4745 | unsigned long recovery = mddev->recovery; |
| 4746 | if (test_bit(MD_RECOVERY_FROZEN, &recovery)) |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4747 | type = "frozen"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4748 | else if (test_bit(MD_RECOVERY_RUNNING, &recovery) || |
| 4749 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) { |
| 4750 | if (test_bit(MD_RECOVERY_RESHAPE, &recovery)) |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 4751 | type = "reshape"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4752 | else if (test_bit(MD_RECOVERY_SYNC, &recovery)) { |
| 4753 | if (!test_bit(MD_RECOVERY_REQUESTED, &recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4754 | type = "resync"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4755 | else if (test_bit(MD_RECOVERY_CHECK, &recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4756 | type = "check"; |
| 4757 | else |
| 4758 | type = "repair"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4759 | } else if (test_bit(MD_RECOVERY_RECOVER, &recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4760 | type = "recover"; |
NeilBrown | 985ca97 | 2015-07-06 12:26:57 +1000 | [diff] [blame] | 4761 | else if (mddev->reshape_position != MaxSector) |
| 4762 | type = "reshape"; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4763 | } |
| 4764 | return sprintf(page, "%s\n", type); |
| 4765 | } |
| 4766 | |
| 4767 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4768 | action_store(struct mddev *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4769 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4770 | if (!mddev->pers || !mddev->pers->sync_request) |
| 4771 | return -EINVAL; |
| 4772 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4773 | |
| 4774 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4775 | if (cmd_match(page, "frozen")) |
| 4776 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4777 | else |
| 4778 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 8e8e251 | 2015-06-12 19:51:27 +1000 | [diff] [blame] | 4779 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 4780 | mddev_lock(mddev) == 0) { |
| 4781 | flush_workqueue(md_misc_wq); |
| 4782 | if (mddev->sync_thread) { |
| 4783 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4784 | md_reap_sync_thread(mddev); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4785 | } |
NeilBrown | 8e8e251 | 2015-06-12 19:51:27 +1000 | [diff] [blame] | 4786 | mddev_unlock(mddev); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4787 | } |
NeilBrown | 312045e | 2015-12-21 11:01:21 +1100 | [diff] [blame] | 4788 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4789 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4790 | else if (cmd_match(page, "resync")) |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4791 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4792 | else if (cmd_match(page, "recover")) { |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4793 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4794 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4795 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4796 | int err; |
| 4797 | if (mddev->pers->start_reshape == NULL) |
| 4798 | return -EINVAL; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4799 | err = mddev_lock(mddev); |
| 4800 | if (!err) { |
NeilBrown | 312045e | 2015-12-21 11:01:21 +1100 | [diff] [blame] | 4801 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4802 | err = -EBUSY; |
| 4803 | else { |
| 4804 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4805 | err = mddev->pers->start_reshape(mddev); |
| 4806 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4807 | mddev_unlock(mddev); |
| 4808 | } |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4809 | if (err) |
| 4810 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4811 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4812 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 4813 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4814 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 4815 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4816 | return -EINVAL; |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4817 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4818 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 4819 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4820 | } |
NeilBrown | 48c26dd | 2012-10-11 14:19:39 +1100 | [diff] [blame] | 4821 | if (mddev->ro == 2) { |
| 4822 | /* A write to sync_action is enough to justify |
| 4823 | * canceling read-auto mode |
| 4824 | */ |
| 4825 | mddev->ro = 0; |
| 4826 | md_wakeup_thread(mddev->sync_thread); |
| 4827 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 4828 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4829 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4830 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4831 | return len; |
| 4832 | } |
| 4833 | |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 4834 | static struct md_sysfs_entry md_scan_mode = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 4835 | __ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 4836 | |
| 4837 | static ssize_t |
| 4838 | last_sync_action_show(struct mddev *mddev, char *page) |
| 4839 | { |
| 4840 | return sprintf(page, "%s\n", mddev->last_sync_action); |
| 4841 | } |
| 4842 | |
| 4843 | static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action); |
| 4844 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4845 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4846 | mismatch_cnt_show(struct mddev *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4847 | { |
| 4848 | return sprintf(page, "%llu\n", |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 4849 | (unsigned long long) |
| 4850 | atomic64_read(&mddev->resync_mismatches)); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4851 | } |
| 4852 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4853 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4854 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4855 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4856 | sync_min_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4857 | { |
| 4858 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 4859 | mddev->sync_speed_min ? "local": "system"); |
| 4860 | } |
| 4861 | |
| 4862 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4863 | sync_min_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4864 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4865 | unsigned int min; |
| 4866 | int rv; |
| 4867 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4868 | if (strncmp(buf, "system", 6)==0) { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4869 | min = 0; |
| 4870 | } else { |
| 4871 | rv = kstrtouint(buf, 10, &min); |
| 4872 | if (rv < 0) |
| 4873 | return rv; |
| 4874 | if (min == 0) |
| 4875 | return -EINVAL; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4876 | } |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4877 | mddev->sync_speed_min = min; |
| 4878 | return len; |
| 4879 | } |
| 4880 | |
| 4881 | static struct md_sysfs_entry md_sync_min = |
| 4882 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 4883 | |
| 4884 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4885 | sync_max_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4886 | { |
| 4887 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 4888 | mddev->sync_speed_max ? "local": "system"); |
| 4889 | } |
| 4890 | |
| 4891 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4892 | sync_max_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4893 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4894 | unsigned int max; |
| 4895 | int rv; |
| 4896 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4897 | if (strncmp(buf, "system", 6)==0) { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4898 | max = 0; |
| 4899 | } else { |
| 4900 | rv = kstrtouint(buf, 10, &max); |
| 4901 | if (rv < 0) |
| 4902 | return rv; |
| 4903 | if (max == 0) |
| 4904 | return -EINVAL; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4905 | } |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4906 | mddev->sync_speed_max = max; |
| 4907 | return len; |
| 4908 | } |
| 4909 | |
| 4910 | static struct md_sysfs_entry md_sync_max = |
| 4911 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 4912 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4913 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4914 | degraded_show(struct mddev *mddev, char *page) |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4915 | { |
| 4916 | return sprintf(page, "%d\n", mddev->degraded); |
| 4917 | } |
| 4918 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4919 | |
| 4920 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4921 | sync_force_parallel_show(struct mddev *mddev, char *page) |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4922 | { |
| 4923 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 4924 | } |
| 4925 | |
| 4926 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4927 | sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len) |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4928 | { |
| 4929 | long n; |
| 4930 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 4931 | if (kstrtol(buf, 10, &n)) |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4932 | return -EINVAL; |
| 4933 | |
| 4934 | if (n != 0 && n != 1) |
| 4935 | return -EINVAL; |
| 4936 | |
| 4937 | mddev->parallel_resync = n; |
| 4938 | |
| 4939 | if (mddev->sync_thread) |
| 4940 | wake_up(&resync_wait); |
| 4941 | |
| 4942 | return len; |
| 4943 | } |
| 4944 | |
| 4945 | /* force parallel resync, even with shared block devices */ |
| 4946 | static struct md_sysfs_entry md_sync_force_parallel = |
| 4947 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 4948 | sync_force_parallel_show, sync_force_parallel_store); |
| 4949 | |
| 4950 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4951 | sync_speed_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4952 | { |
| 4953 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 4954 | if (mddev->curr_resync == 0) |
| 4955 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 4956 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 4957 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4958 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 4959 | db = resync - mddev->resync_mark_cnt; |
| 4960 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4961 | } |
| 4962 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4963 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4964 | |
| 4965 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4966 | sync_completed_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4967 | { |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4968 | unsigned long long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4969 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4970 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4971 | return sprintf(page, "none\n"); |
| 4972 | |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 4973 | if (mddev->curr_resync == 1 || |
| 4974 | mddev->curr_resync == 2) |
| 4975 | return sprintf(page, "delayed\n"); |
| 4976 | |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 4977 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 4978 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4979 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4980 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4981 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4982 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4983 | resync = mddev->curr_resync_completed; |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4984 | return sprintf(page, "%llu / %llu\n", resync, max_sectors); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4985 | } |
| 4986 | |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 4987 | static struct md_sysfs_entry md_sync_completed = |
| 4988 | __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4989 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4990 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4991 | min_sync_show(struct mddev *mddev, char *page) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4992 | { |
| 4993 | return sprintf(page, "%llu\n", |
| 4994 | (unsigned long long)mddev->resync_min); |
| 4995 | } |
| 4996 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4997 | min_sync_store(struct mddev *mddev, const char *buf, size_t len) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4998 | { |
| 4999 | unsigned long long min; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5000 | int err; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5001 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 5002 | if (kstrtoull(buf, 10, &min)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5003 | return -EINVAL; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5004 | |
| 5005 | spin_lock(&mddev->lock); |
| 5006 | err = -EINVAL; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5007 | if (min > mddev->resync_max) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5008 | goto out_unlock; |
| 5009 | |
| 5010 | err = -EBUSY; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5011 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5012 | goto out_unlock; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5013 | |
NeilBrown | 50c37b1 | 2015-03-23 17:36:38 +1100 | [diff] [blame] | 5014 | /* Round down to multiple of 4K for safety */ |
| 5015 | mddev->resync_min = round_down(min, 8); |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5016 | err = 0; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5017 | |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5018 | out_unlock: |
| 5019 | spin_unlock(&mddev->lock); |
| 5020 | return err ?: len; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5021 | } |
| 5022 | |
| 5023 | static struct md_sysfs_entry md_min_sync = |
| 5024 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 5025 | |
| 5026 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5027 | max_sync_show(struct mddev *mddev, char *page) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5028 | { |
| 5029 | if (mddev->resync_max == MaxSector) |
| 5030 | return sprintf(page, "max\n"); |
| 5031 | else |
| 5032 | return sprintf(page, "%llu\n", |
| 5033 | (unsigned long long)mddev->resync_max); |
| 5034 | } |
| 5035 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5036 | max_sync_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5037 | { |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5038 | int err; |
| 5039 | spin_lock(&mddev->lock); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5040 | if (strncmp(buf, "max", 3) == 0) |
| 5041 | mddev->resync_max = MaxSector; |
| 5042 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5043 | unsigned long long max; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5044 | int chunk; |
| 5045 | |
| 5046 | err = -EINVAL; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 5047 | if (kstrtoull(buf, 10, &max)) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5048 | goto out_unlock; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5049 | if (max < mddev->resync_min) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5050 | goto out_unlock; |
| 5051 | |
| 5052 | err = -EBUSY; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5053 | if (max < mddev->resync_max && |
NeilBrown | 4d484a4 | 2009-08-13 10:41:50 +1000 | [diff] [blame] | 5054 | mddev->ro == 0 && |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5055 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5056 | goto out_unlock; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5057 | |
| 5058 | /* Must be a multiple of chunk_size */ |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5059 | chunk = mddev->chunk_sectors; |
| 5060 | if (chunk) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 5061 | sector_t temp = max; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5062 | |
| 5063 | err = -EINVAL; |
| 5064 | if (sector_div(temp, chunk)) |
| 5065 | goto out_unlock; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5066 | } |
| 5067 | mddev->resync_max = max; |
| 5068 | } |
| 5069 | wake_up(&mddev->recovery_wait); |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5070 | err = 0; |
| 5071 | out_unlock: |
| 5072 | spin_unlock(&mddev->lock); |
| 5073 | return err ?: len; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5074 | } |
| 5075 | |
| 5076 | static struct md_sysfs_entry md_max_sync = |
| 5077 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 5078 | |
| 5079 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5080 | suspend_lo_show(struct mddev *mddev, char *page) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5081 | { |
| 5082 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 5083 | } |
| 5084 | |
| 5085 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5086 | suspend_lo_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5087 | { |
NeilBrown | b03e0cc | 2017-10-19 12:49:15 +1100 | [diff] [blame] | 5088 | unsigned long long new; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5089 | int err; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5090 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 5091 | err = kstrtoull(buf, 10, &new); |
| 5092 | if (err < 0) |
| 5093 | return err; |
| 5094 | if (new != (sector_t)new) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5095 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 5096 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5097 | err = mddev_lock(mddev); |
| 5098 | if (err) |
| 5099 | return err; |
| 5100 | err = -EINVAL; |
| 5101 | if (mddev->pers == NULL || |
| 5102 | mddev->pers->quiesce == NULL) |
| 5103 | goto unlock; |
NeilBrown | b03e0cc | 2017-10-19 12:49:15 +1100 | [diff] [blame] | 5104 | mddev_suspend(mddev); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 5105 | mddev->suspend_lo = new; |
NeilBrown | b03e0cc | 2017-10-19 12:49:15 +1100 | [diff] [blame] | 5106 | mddev_resume(mddev); |
| 5107 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5108 | err = 0; |
| 5109 | unlock: |
| 5110 | mddev_unlock(mddev); |
| 5111 | return err ?: len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5112 | } |
| 5113 | static struct md_sysfs_entry md_suspend_lo = |
| 5114 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 5115 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5116 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5117 | suspend_hi_show(struct mddev *mddev, char *page) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5118 | { |
| 5119 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 5120 | } |
| 5121 | |
| 5122 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5123 | suspend_hi_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5124 | { |
NeilBrown | b03e0cc | 2017-10-19 12:49:15 +1100 | [diff] [blame] | 5125 | unsigned long long new; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5126 | int err; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5127 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 5128 | err = kstrtoull(buf, 10, &new); |
| 5129 | if (err < 0) |
| 5130 | return err; |
| 5131 | if (new != (sector_t)new) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5132 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 5133 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5134 | err = mddev_lock(mddev); |
| 5135 | if (err) |
| 5136 | return err; |
| 5137 | err = -EINVAL; |
NeilBrown | b03e0cc | 2017-10-19 12:49:15 +1100 | [diff] [blame] | 5138 | if (mddev->pers == NULL) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5139 | goto unlock; |
NeilBrown | b03e0cc | 2017-10-19 12:49:15 +1100 | [diff] [blame] | 5140 | |
| 5141 | mddev_suspend(mddev); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 5142 | mddev->suspend_hi = new; |
NeilBrown | b03e0cc | 2017-10-19 12:49:15 +1100 | [diff] [blame] | 5143 | mddev_resume(mddev); |
| 5144 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5145 | err = 0; |
| 5146 | unlock: |
| 5147 | mddev_unlock(mddev); |
| 5148 | return err ?: len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5149 | } |
| 5150 | static struct md_sysfs_entry md_suspend_hi = |
| 5151 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 5152 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 5153 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5154 | reshape_position_show(struct mddev *mddev, char *page) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 5155 | { |
| 5156 | if (mddev->reshape_position != MaxSector) |
| 5157 | return sprintf(page, "%llu\n", |
| 5158 | (unsigned long long)mddev->reshape_position); |
| 5159 | strcpy(page, "none\n"); |
| 5160 | return 5; |
| 5161 | } |
| 5162 | |
| 5163 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5164 | reshape_position_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 5165 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5166 | struct md_rdev *rdev; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 5167 | unsigned long long new; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5168 | int err; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5169 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 5170 | err = kstrtoull(buf, 10, &new); |
| 5171 | if (err < 0) |
| 5172 | return err; |
| 5173 | if (new != (sector_t)new) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 5174 | return -EINVAL; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5175 | err = mddev_lock(mddev); |
| 5176 | if (err) |
| 5177 | return err; |
| 5178 | err = -EBUSY; |
| 5179 | if (mddev->pers) |
| 5180 | goto unlock; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 5181 | mddev->reshape_position = new; |
| 5182 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5183 | mddev->reshape_backwards = 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 5184 | mddev->new_level = mddev->level; |
| 5185 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5186 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5187 | rdev_for_each(rdev, mddev) |
| 5188 | rdev->new_data_offset = rdev->data_offset; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5189 | err = 0; |
| 5190 | unlock: |
| 5191 | mddev_unlock(mddev); |
| 5192 | return err ?: len; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 5193 | } |
| 5194 | |
| 5195 | static struct md_sysfs_entry md_reshape_position = |
| 5196 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 5197 | reshape_position_store); |
| 5198 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5199 | static ssize_t |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5200 | reshape_direction_show(struct mddev *mddev, char *page) |
| 5201 | { |
| 5202 | return sprintf(page, "%s\n", |
| 5203 | mddev->reshape_backwards ? "backwards" : "forwards"); |
| 5204 | } |
| 5205 | |
| 5206 | static ssize_t |
| 5207 | reshape_direction_store(struct mddev *mddev, const char *buf, size_t len) |
| 5208 | { |
| 5209 | int backwards = 0; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5210 | int err; |
| 5211 | |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5212 | if (cmd_match(buf, "forwards")) |
| 5213 | backwards = 0; |
| 5214 | else if (cmd_match(buf, "backwards")) |
| 5215 | backwards = 1; |
| 5216 | else |
| 5217 | return -EINVAL; |
| 5218 | if (mddev->reshape_backwards == backwards) |
| 5219 | return len; |
| 5220 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5221 | err = mddev_lock(mddev); |
| 5222 | if (err) |
| 5223 | return err; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5224 | /* check if we are allowed to change */ |
| 5225 | if (mddev->delta_disks) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5226 | err = -EBUSY; |
| 5227 | else if (mddev->persistent && |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5228 | mddev->major_version == 0) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5229 | err = -EINVAL; |
| 5230 | else |
| 5231 | mddev->reshape_backwards = backwards; |
| 5232 | mddev_unlock(mddev); |
| 5233 | return err ?: len; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5234 | } |
| 5235 | |
| 5236 | static struct md_sysfs_entry md_reshape_direction = |
| 5237 | __ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show, |
| 5238 | reshape_direction_store); |
| 5239 | |
| 5240 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5241 | array_size_show(struct mddev *mddev, char *page) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5242 | { |
| 5243 | if (mddev->external_size) |
| 5244 | return sprintf(page, "%llu\n", |
| 5245 | (unsigned long long)mddev->array_sectors/2); |
| 5246 | else |
| 5247 | return sprintf(page, "default\n"); |
| 5248 | } |
| 5249 | |
| 5250 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5251 | array_size_store(struct mddev *mddev, const char *buf, size_t len) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5252 | { |
| 5253 | sector_t sectors; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5254 | int err; |
| 5255 | |
| 5256 | err = mddev_lock(mddev); |
| 5257 | if (err) |
| 5258 | return err; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5259 | |
Guoqing Jiang | ab5a98b | 2016-05-02 11:33:13 -0400 | [diff] [blame] | 5260 | /* cluster raid doesn't support change array_sectors */ |
Zhilong Liu | b670883 | 2017-04-10 14:15:55 +0800 | [diff] [blame] | 5261 | if (mddev_is_clustered(mddev)) { |
| 5262 | mddev_unlock(mddev); |
Guoqing Jiang | ab5a98b | 2016-05-02 11:33:13 -0400 | [diff] [blame] | 5263 | return -EINVAL; |
Zhilong Liu | b670883 | 2017-04-10 14:15:55 +0800 | [diff] [blame] | 5264 | } |
Guoqing Jiang | ab5a98b | 2016-05-02 11:33:13 -0400 | [diff] [blame] | 5265 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5266 | if (strncmp(buf, "default", 7) == 0) { |
| 5267 | if (mddev->pers) |
| 5268 | sectors = mddev->pers->size(mddev, 0, 0); |
| 5269 | else |
| 5270 | sectors = mddev->array_sectors; |
| 5271 | |
| 5272 | mddev->external_size = 0; |
| 5273 | } else { |
| 5274 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5275 | err = -EINVAL; |
| 5276 | else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
| 5277 | err = -E2BIG; |
| 5278 | else |
| 5279 | mddev->external_size = 1; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5280 | } |
| 5281 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5282 | if (!err) { |
| 5283 | mddev->array_sectors = sectors; |
| 5284 | if (mddev->pers) { |
| 5285 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 5286 | revalidate_disk(mddev->gendisk); |
| 5287 | } |
NeilBrown | cbe6ef1 | 2011-02-16 13:58:38 +1100 | [diff] [blame] | 5288 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5289 | mddev_unlock(mddev); |
| 5290 | return err ?: len; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5291 | } |
| 5292 | |
| 5293 | static struct md_sysfs_entry md_array_size = |
| 5294 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 5295 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5296 | |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 5297 | static ssize_t |
| 5298 | consistency_policy_show(struct mddev *mddev, char *page) |
| 5299 | { |
| 5300 | int ret; |
| 5301 | |
| 5302 | if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) { |
| 5303 | ret = sprintf(page, "journal\n"); |
| 5304 | } else if (test_bit(MD_HAS_PPL, &mddev->flags)) { |
| 5305 | ret = sprintf(page, "ppl\n"); |
| 5306 | } else if (mddev->bitmap) { |
| 5307 | ret = sprintf(page, "bitmap\n"); |
| 5308 | } else if (mddev->pers) { |
| 5309 | if (mddev->pers->sync_request) |
| 5310 | ret = sprintf(page, "resync\n"); |
| 5311 | else |
| 5312 | ret = sprintf(page, "none\n"); |
| 5313 | } else { |
| 5314 | ret = sprintf(page, "unknown\n"); |
| 5315 | } |
| 5316 | |
| 5317 | return ret; |
| 5318 | } |
| 5319 | |
| 5320 | static ssize_t |
| 5321 | consistency_policy_store(struct mddev *mddev, const char *buf, size_t len) |
| 5322 | { |
Artur Paszkiewicz | ba903a3 | 2017-03-09 10:00:03 +0100 | [diff] [blame] | 5323 | int err = 0; |
| 5324 | |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 5325 | if (mddev->pers) { |
Artur Paszkiewicz | ba903a3 | 2017-03-09 10:00:03 +0100 | [diff] [blame] | 5326 | if (mddev->pers->change_consistency_policy) |
| 5327 | err = mddev->pers->change_consistency_policy(mddev, buf); |
| 5328 | else |
| 5329 | err = -EBUSY; |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 5330 | } else if (mddev->external && strncmp(buf, "ppl", 3) == 0) { |
| 5331 | set_bit(MD_HAS_PPL, &mddev->flags); |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 5332 | } else { |
Artur Paszkiewicz | ba903a3 | 2017-03-09 10:00:03 +0100 | [diff] [blame] | 5333 | err = -EINVAL; |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 5334 | } |
Artur Paszkiewicz | ba903a3 | 2017-03-09 10:00:03 +0100 | [diff] [blame] | 5335 | |
| 5336 | return err ? err : len; |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 5337 | } |
| 5338 | |
| 5339 | static struct md_sysfs_entry md_consistency_policy = |
| 5340 | __ATTR(consistency_policy, S_IRUGO | S_IWUSR, consistency_policy_show, |
| 5341 | consistency_policy_store); |
| 5342 | |
Guoqing Jiang | 9a56784 | 2019-07-24 11:09:19 +0200 | [diff] [blame] | 5343 | static ssize_t fail_last_dev_show(struct mddev *mddev, char *page) |
| 5344 | { |
| 5345 | return sprintf(page, "%d\n", mddev->fail_last_dev); |
| 5346 | } |
| 5347 | |
| 5348 | /* |
| 5349 | * Setting fail_last_dev to true to allow last device to be forcibly removed |
| 5350 | * from RAID1/RAID10. |
| 5351 | */ |
| 5352 | static ssize_t |
| 5353 | fail_last_dev_store(struct mddev *mddev, const char *buf, size_t len) |
| 5354 | { |
| 5355 | int ret; |
| 5356 | bool value; |
| 5357 | |
| 5358 | ret = kstrtobool(buf, &value); |
| 5359 | if (ret) |
| 5360 | return ret; |
| 5361 | |
| 5362 | if (value != mddev->fail_last_dev) |
| 5363 | mddev->fail_last_dev = value; |
| 5364 | |
| 5365 | return len; |
| 5366 | } |
| 5367 | static struct md_sysfs_entry md_fail_last_dev = |
| 5368 | __ATTR(fail_last_dev, S_IRUGO | S_IWUSR, fail_last_dev_show, |
| 5369 | fail_last_dev_store); |
| 5370 | |
Guoqing Jiang | 3938f5f | 2019-12-23 10:48:56 +0100 | [diff] [blame] | 5371 | static ssize_t serialize_policy_show(struct mddev *mddev, char *page) |
| 5372 | { |
| 5373 | if (mddev->pers == NULL || (mddev->pers->level != 1)) |
| 5374 | return sprintf(page, "n/a\n"); |
| 5375 | else |
| 5376 | return sprintf(page, "%d\n", mddev->serialize_policy); |
| 5377 | } |
| 5378 | |
| 5379 | /* |
| 5380 | * Setting serialize_policy to true to enforce write IO is not reordered |
| 5381 | * for raid1. |
| 5382 | */ |
| 5383 | static ssize_t |
| 5384 | serialize_policy_store(struct mddev *mddev, const char *buf, size_t len) |
| 5385 | { |
| 5386 | int err; |
| 5387 | bool value; |
| 5388 | |
| 5389 | err = kstrtobool(buf, &value); |
| 5390 | if (err) |
| 5391 | return err; |
| 5392 | |
| 5393 | if (value == mddev->serialize_policy) |
| 5394 | return len; |
| 5395 | |
| 5396 | err = mddev_lock(mddev); |
| 5397 | if (err) |
| 5398 | return err; |
| 5399 | if (mddev->pers == NULL || (mddev->pers->level != 1)) { |
| 5400 | pr_err("md: serialize_policy is only effective for raid1\n"); |
| 5401 | err = -EINVAL; |
| 5402 | goto unlock; |
| 5403 | } |
| 5404 | |
| 5405 | mddev_suspend(mddev); |
| 5406 | if (value) |
| 5407 | mddev_create_serial_pool(mddev, NULL, true); |
| 5408 | else |
| 5409 | mddev_destroy_serial_pool(mddev, NULL, true); |
| 5410 | mddev->serialize_policy = value; |
| 5411 | mddev_resume(mddev); |
| 5412 | unlock: |
| 5413 | mddev_unlock(mddev); |
| 5414 | return err ?: len; |
| 5415 | } |
| 5416 | |
| 5417 | static struct md_sysfs_entry md_serialize_policy = |
| 5418 | __ATTR(serialize_policy, S_IRUGO | S_IWUSR, serialize_policy_show, |
| 5419 | serialize_policy_store); |
| 5420 | |
| 5421 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5422 | static struct attribute *md_default_attrs[] = { |
| 5423 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 5424 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5425 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 5426 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5427 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 5428 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 5429 | &md_metadata.attr, |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 5430 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 5431 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5432 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 5433 | &md_reshape_position.attr, |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5434 | &md_reshape_direction.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5435 | &md_array_size.attr, |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 5436 | &max_corr_read_errors.attr, |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 5437 | &md_consistency_policy.attr, |
Guoqing Jiang | 9a56784 | 2019-07-24 11:09:19 +0200 | [diff] [blame] | 5438 | &md_fail_last_dev.attr, |
Guoqing Jiang | 3938f5f | 2019-12-23 10:48:56 +0100 | [diff] [blame] | 5439 | &md_serialize_policy.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 5440 | NULL, |
| 5441 | }; |
| 5442 | |
| 5443 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 5444 | &md_scan_mode.attr, |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 5445 | &md_last_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 5446 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5447 | &md_sync_min.attr, |
| 5448 | &md_sync_max.attr, |
| 5449 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 5450 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5451 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5452 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5453 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5454 | &md_suspend_lo.attr, |
| 5455 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 5456 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 5457 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5458 | NULL, |
| 5459 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 5460 | static struct attribute_group md_redundancy_group = { |
| 5461 | .name = NULL, |
| 5462 | .attrs = md_redundancy_attrs, |
| 5463 | }; |
| 5464 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5465 | static ssize_t |
| 5466 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 5467 | { |
| 5468 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5469 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 5470 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5471 | |
| 5472 | if (!entry->show) |
| 5473 | return -EIO; |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 5474 | spin_lock(&all_mddevs_lock); |
| 5475 | if (list_empty(&mddev->all_mddevs)) { |
| 5476 | spin_unlock(&all_mddevs_lock); |
| 5477 | return -EBUSY; |
| 5478 | } |
| 5479 | mddev_get(mddev); |
| 5480 | spin_unlock(&all_mddevs_lock); |
| 5481 | |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 5482 | rv = entry->show(mddev, page); |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 5483 | mddev_put(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 5484 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5485 | } |
| 5486 | |
| 5487 | static ssize_t |
| 5488 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 5489 | const char *page, size_t length) |
| 5490 | { |
| 5491 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5492 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 5493 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5494 | |
| 5495 | if (!entry->store) |
| 5496 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 5497 | if (!capable(CAP_SYS_ADMIN)) |
| 5498 | return -EACCES; |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 5499 | spin_lock(&all_mddevs_lock); |
| 5500 | if (list_empty(&mddev->all_mddevs)) { |
| 5501 | spin_unlock(&all_mddevs_lock); |
| 5502 | return -EBUSY; |
| 5503 | } |
| 5504 | mddev_get(mddev); |
| 5505 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5506 | rv = entry->store(mddev, page, length); |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 5507 | mddev_put(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 5508 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5509 | } |
| 5510 | |
| 5511 | static void md_free(struct kobject *ko) |
| 5512 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5513 | struct mddev *mddev = container_of(ko, struct mddev, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 5514 | |
| 5515 | if (mddev->sysfs_state) |
| 5516 | sysfs_put(mddev->sysfs_state); |
| 5517 | |
Bart Van Assche | d8115c35 | 2018-02-28 10:15:29 -0800 | [diff] [blame] | 5518 | if (mddev->gendisk) |
| 5519 | del_gendisk(mddev->gendisk); |
NeilBrown | 6cd18e7 | 2015-04-27 14:12:22 +1000 | [diff] [blame] | 5520 | if (mddev->queue) |
| 5521 | blk_cleanup_queue(mddev->queue); |
Bart Van Assche | d8115c35 | 2018-02-28 10:15:29 -0800 | [diff] [blame] | 5522 | if (mddev->gendisk) |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 5523 | put_disk(mddev->gendisk); |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 5524 | percpu_ref_exit(&mddev->writes_pending); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 5525 | |
Kent Overstreet | 28dec87 | 2018-06-07 20:52:54 -0400 | [diff] [blame] | 5526 | bioset_exit(&mddev->bio_set); |
| 5527 | bioset_exit(&mddev->sync_set); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5528 | kfree(mddev); |
| 5529 | } |
| 5530 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 5531 | static const struct sysfs_ops md_sysfs_ops = { |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5532 | .show = md_attr_show, |
| 5533 | .store = md_attr_store, |
| 5534 | }; |
| 5535 | static struct kobj_type md_ktype = { |
| 5536 | .release = md_free, |
| 5537 | .sysfs_ops = &md_sysfs_ops, |
| 5538 | .default_attrs = md_default_attrs, |
| 5539 | }; |
| 5540 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5541 | int mdp_major = 0; |
| 5542 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 5543 | static void mddev_delayed_delete(struct work_struct *ws) |
| 5544 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5545 | struct mddev *mddev = container_of(ws, struct mddev, del_work); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 5546 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 5547 | sysfs_remove_group(&mddev->kobj, &md_bitmap_group); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 5548 | kobject_del(&mddev->kobj); |
| 5549 | kobject_put(&mddev->kobj); |
| 5550 | } |
| 5551 | |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 5552 | static void no_op(struct percpu_ref *r) {} |
| 5553 | |
NeilBrown | a415c0f | 2017-06-05 16:05:13 +1000 | [diff] [blame] | 5554 | int mddev_init_writes_pending(struct mddev *mddev) |
| 5555 | { |
| 5556 | if (mddev->writes_pending.percpu_count_ptr) |
| 5557 | return 0; |
Roman Gushchin | ddde2af | 2019-05-07 10:01:49 -0700 | [diff] [blame] | 5558 | if (percpu_ref_init(&mddev->writes_pending, no_op, |
| 5559 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL) < 0) |
NeilBrown | a415c0f | 2017-06-05 16:05:13 +1000 | [diff] [blame] | 5560 | return -ENOMEM; |
| 5561 | /* We want to start with the refcount at zero */ |
| 5562 | percpu_ref_put(&mddev->writes_pending); |
| 5563 | return 0; |
| 5564 | } |
| 5565 | EXPORT_SYMBOL_GPL(mddev_init_writes_pending); |
| 5566 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5567 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5568 | { |
NeilBrown | 039b722 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 5569 | /* |
| 5570 | * If dev is zero, name is the name of a device to allocate with |
| 5571 | * an arbitrary minor number. It will be "md_???" |
| 5572 | * If dev is non-zero it must be a device number with a MAJOR of |
| 5573 | * MD_MAJOR or mdp_major. In this case, if "name" is NULL, then |
| 5574 | * the device is being created by opening a node in /dev. |
| 5575 | * If "name" is not NULL, the device is being created by |
| 5576 | * writing to /sys/module/md_mod/parameters/new_array. |
| 5577 | */ |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 5578 | static DEFINE_MUTEX(disks_mutex); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5579 | struct mddev *mddev = mddev_find(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5580 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5581 | int partitioned; |
| 5582 | int shift; |
| 5583 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 5584 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5585 | |
| 5586 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5587 | return -ENODEV; |
| 5588 | |
| 5589 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 5590 | shift = partitioned ? MdpMinorShift : 0; |
| 5591 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5592 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 5593 | /* wait for any previous instance of this device to be |
| 5594 | * completely removed (mddev_delayed_delete). |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5595 | */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 5596 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5597 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 5598 | mutex_lock(&disks_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5599 | error = -EEXIST; |
| 5600 | if (mddev->gendisk) |
| 5601 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5602 | |
NeilBrown | 039b722 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 5603 | if (name && !dev) { |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5604 | /* Need to ensure that 'name' is not a duplicate. |
| 5605 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5606 | struct mddev *mddev2; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5607 | spin_lock(&all_mddevs_lock); |
| 5608 | |
| 5609 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 5610 | if (mddev2->gendisk && |
| 5611 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 5612 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5613 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5614 | } |
| 5615 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5616 | } |
NeilBrown | 039b722 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 5617 | if (name && dev) |
| 5618 | /* |
| 5619 | * Creating /dev/mdNNN via "newarray", so adjust hold_active. |
| 5620 | */ |
| 5621 | mddev->hold_active = UNTIL_STOP; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5622 | |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5623 | error = -ENOMEM; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5624 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5625 | if (!mddev->queue) |
| 5626 | goto abort; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5627 | mddev->queue->queuedata = mddev; |
| 5628 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5629 | blk_queue_make_request(mddev->queue, md_make_request); |
Martin K. Petersen | b1bd055 | 2012-01-11 16:27:11 +0100 | [diff] [blame] | 5630 | blk_set_stacking_limits(&mddev->queue->limits); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5631 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5632 | disk = alloc_disk(1 << shift); |
| 5633 | if (!disk) { |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5634 | blk_cleanup_queue(mddev->queue); |
| 5635 | mddev->queue = NULL; |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5636 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5637 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5638 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5639 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5640 | if (name) |
| 5641 | strcpy(disk->disk_name, name); |
| 5642 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5643 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f46 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 5644 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5645 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5646 | disk->fops = &md_fops; |
| 5647 | disk->private_data = mddev; |
| 5648 | disk->queue = mddev->queue; |
Jens Axboe | 56883a7 | 2016-03-30 10:16:53 -0600 | [diff] [blame] | 5649 | blk_queue_write_cache(mddev->queue, true, true); |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 5650 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5651 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 5652 | * remove it now. |
| 5653 | */ |
| 5654 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5655 | mddev->gendisk = disk; |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 5656 | /* As soon as we call add_disk(), another thread could get |
| 5657 | * through to md_open, so make sure it doesn't get too far |
| 5658 | */ |
| 5659 | mutex_lock(&mddev->open_mutex); |
| 5660 | add_disk(disk); |
| 5661 | |
Kent Overstreet | 28dec87 | 2018-06-07 20:52:54 -0400 | [diff] [blame] | 5662 | error = kobject_add(&mddev->kobj, &disk_to_dev(disk)->kobj, "%s", "md"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5663 | if (error) { |
| 5664 | /* This isn't possible, but as kobject_init_and_add is marked |
| 5665 | * __must_check, we must do something with the result |
| 5666 | */ |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5667 | pr_debug("md: cannot register %s/md - name in use\n", |
| 5668 | disk->disk_name); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5669 | error = 0; |
| 5670 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5671 | if (mddev->kobj.sd && |
| 5672 | sysfs_create_group(&mddev->kobj, &md_bitmap_group)) |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5673 | pr_debug("pointless warning\n"); |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 5674 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5675 | abort: |
| 5676 | mutex_unlock(&disks_mutex); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5677 | if (!error && mddev->kobj.sd) { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 5678 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5679 | mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state"); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 5680 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5681 | mddev_put(mddev); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5682 | return error; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5683 | } |
| 5684 | |
| 5685 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 5686 | { |
NeilBrown | 78b6350 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 5687 | if (create_on_open) |
| 5688 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5689 | return NULL; |
| 5690 | } |
| 5691 | |
Kees Cook | e4dca7b | 2017-10-17 19:04:42 -0700 | [diff] [blame] | 5692 | static int add_named_array(const char *val, const struct kernel_param *kp) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5693 | { |
NeilBrown | 039b722 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 5694 | /* |
| 5695 | * val must be "md_*" or "mdNNN". |
| 5696 | * For "md_*" we allocate an array with a large free minor number, and |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5697 | * set the name to val. val must not already be an active name. |
NeilBrown | 039b722 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 5698 | * For "mdNNN" we allocate an array with the minor number NNN |
| 5699 | * which must not already be in use. |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5700 | */ |
| 5701 | int len = strlen(val); |
| 5702 | char buf[DISK_NAME_LEN]; |
NeilBrown | 039b722 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 5703 | unsigned long devnum; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5704 | |
| 5705 | while (len && val[len-1] == '\n') |
| 5706 | len--; |
| 5707 | if (len >= DISK_NAME_LEN) |
| 5708 | return -E2BIG; |
| 5709 | strlcpy(buf, val, len+1); |
NeilBrown | 039b722 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 5710 | if (strncmp(buf, "md_", 3) == 0) |
| 5711 | return md_alloc(0, buf); |
| 5712 | if (strncmp(buf, "md", 2) == 0 && |
| 5713 | isdigit(buf[2]) && |
| 5714 | kstrtoul(buf+2, 10, &devnum) == 0 && |
| 5715 | devnum <= MINORMASK) |
| 5716 | return md_alloc(MKDEV(MD_MAJOR, devnum), NULL); |
| 5717 | |
| 5718 | return -EINVAL; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5719 | } |
| 5720 | |
Kees Cook | 8376d3c | 2017-10-16 17:01:48 -0700 | [diff] [blame] | 5721 | static void md_safemode_timeout(struct timer_list *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5722 | { |
Kees Cook | 8376d3c | 2017-10-16 17:01:48 -0700 | [diff] [blame] | 5723 | struct mddev *mddev = from_timer(mddev, t, safemode_timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5724 | |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 5725 | mddev->safemode = 1; |
| 5726 | if (mddev->external) |
| 5727 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
| 5728 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5729 | md_wakeup_thread(mddev->thread); |
| 5730 | } |
| 5731 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5732 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5733 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5734 | int md_run(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5735 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5736 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5737 | struct md_rdev *rdev; |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 5738 | struct md_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5739 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 5740 | if (list_empty(&mddev->disks)) |
| 5741 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5742 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5743 | |
| 5744 | if (mddev->pers) |
| 5745 | return -EBUSY; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 5746 | /* Cannot run until previous stop completes properly */ |
| 5747 | if (mddev->sysfs_active) |
| 5748 | return -EBUSY; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 5749 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5750 | /* |
| 5751 | * Analyze all RAID superblock(s) |
| 5752 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 5753 | if (!mddev->raid_disks) { |
| 5754 | if (!mddev->persistent) |
| 5755 | return -EINVAL; |
Yufen Yu | 6a5cb53 | 2019-10-16 16:00:03 +0800 | [diff] [blame] | 5756 | err = analyze_sbs(mddev); |
| 5757 | if (err) |
| 5758 | return -EINVAL; |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 5759 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5760 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5761 | if (mddev->level != LEVEL_NONE) |
| 5762 | request_module("md-level-%d", mddev->level); |
| 5763 | else if (mddev->clevel[0]) |
| 5764 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5765 | |
| 5766 | /* |
| 5767 | * Drop all container device buffers, from now on |
| 5768 | * the only valid external interface is through the md |
| 5769 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5770 | */ |
Heinz Mauelshagen | 4b6c106 | 2018-02-02 23:13:19 +0100 | [diff] [blame] | 5771 | mddev->has_superblocks = false; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5772 | rdev_for_each(rdev, mddev) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5773 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5774 | continue; |
| 5775 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 5776 | invalidate_bdev(rdev->bdev); |
NeilBrown | 97b20ef | 2017-04-13 08:53:48 +1000 | [diff] [blame] | 5777 | if (mddev->ro != 1 && |
| 5778 | (bdev_read_only(rdev->bdev) || |
| 5779 | bdev_read_only(rdev->meta_bdev))) { |
| 5780 | mddev->ro = 1; |
| 5781 | if (mddev->gendisk) |
| 5782 | set_disk_ro(mddev->gendisk, 1); |
| 5783 | } |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5784 | |
Heinz Mauelshagen | 4b6c106 | 2018-02-02 23:13:19 +0100 | [diff] [blame] | 5785 | if (rdev->sb_page) |
| 5786 | mddev->has_superblocks = true; |
| 5787 | |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5788 | /* perform some consistency tests on the device. |
| 5789 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5790 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5791 | */ |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 5792 | if (rdev->meta_bdev) { |
| 5793 | /* Nothing to check */; |
| 5794 | } else if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5795 | if (mddev->dev_sectors && |
| 5796 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5797 | > rdev->sb_start) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5798 | pr_warn("md: %s: data overlaps metadata\n", |
| 5799 | mdname(mddev)); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5800 | return -EINVAL; |
| 5801 | } |
| 5802 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5803 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5804 | > rdev->data_offset) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5805 | pr_warn("md: %s: metadata overlaps data\n", |
| 5806 | mdname(mddev)); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5807 | return -EINVAL; |
| 5808 | } |
| 5809 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5810 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5811 | } |
| 5812 | |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 5813 | if (!bioset_initialized(&mddev->bio_set)) { |
| 5814 | err = bioset_init(&mddev->bio_set, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS); |
| 5815 | if (err) |
| 5816 | return err; |
Ming Lei | 1027317 | 2017-02-14 23:29:00 +0800 | [diff] [blame] | 5817 | } |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 5818 | if (!bioset_initialized(&mddev->sync_set)) { |
| 5819 | err = bioset_init(&mddev->sync_set, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS); |
| 5820 | if (err) |
Kent Overstreet | 28dec87 | 2018-06-07 20:52:54 -0400 | [diff] [blame] | 5821 | return err; |
NeilBrown | 5a85071 | 2017-06-21 09:12:21 +1000 | [diff] [blame] | 5822 | } |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 5823 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5824 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5825 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5826 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5827 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5828 | if (mddev->level != LEVEL_NONE) |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5829 | pr_warn("md: personality for level %d is not loaded!\n", |
| 5830 | mddev->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5831 | else |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5832 | pr_warn("md: personality for level %s is not loaded!\n", |
| 5833 | mddev->clevel); |
Shaohua Li | bfc9dfd | 2018-06-13 08:39:49 -0700 | [diff] [blame] | 5834 | err = -EINVAL; |
| 5835 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5836 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5837 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5838 | if (mddev->level != pers->level) { |
| 5839 | mddev->level = pers->level; |
| 5840 | mddev->new_level = pers->level; |
| 5841 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5842 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5843 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5844 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5845 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5846 | /* This personality cannot handle reshaping... */ |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5847 | module_put(pers->owner); |
Shaohua Li | bfc9dfd | 2018-06-13 08:39:49 -0700 | [diff] [blame] | 5848 | err = -EINVAL; |
| 5849 | goto abort; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5850 | } |
| 5851 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5852 | if (pers->sync_request) { |
| 5853 | /* Warn if this is a potentially silly |
| 5854 | * configuration. |
| 5855 | */ |
| 5856 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5857 | struct md_rdev *rdev2; |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5858 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5859 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5860 | rdev_for_each(rdev, mddev) |
| 5861 | rdev_for_each(rdev2, mddev) { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5862 | if (rdev < rdev2 && |
| 5863 | rdev->bdev->bd_contains == |
| 5864 | rdev2->bdev->bd_contains) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5865 | pr_warn("%s: WARNING: %s appears to be on the same physical disk as %s.\n", |
| 5866 | mdname(mddev), |
| 5867 | bdevname(rdev->bdev,b), |
| 5868 | bdevname(rdev2->bdev,b2)); |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5869 | warned = 1; |
| 5870 | } |
| 5871 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5872 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5873 | if (warned) |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5874 | pr_warn("True protection against single-disk failure might be compromised.\n"); |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5875 | } |
| 5876 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 5877 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5878 | /* may be over-ridden by personality */ |
| 5879 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 5880 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5881 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5882 | |
NeilBrown | 0f9552b5 | 2009-12-30 12:08:50 +1100 | [diff] [blame] | 5883 | if (start_readonly && mddev->ro == 0) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5884 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 5885 | |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5886 | err = pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 5887 | if (err) |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5888 | pr_warn("md: pers->run() failed ...\n"); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5889 | else if (pers->size(mddev, 0, 0) < mddev->array_sectors) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5890 | WARN_ONCE(!mddev->external_size, |
| 5891 | "%s: default size too small, but 'external_size' not in effect?\n", |
| 5892 | __func__); |
| 5893 | pr_warn("md: invalid array_size %llu > default size %llu\n", |
| 5894 | (unsigned long long)mddev->array_sectors / 2, |
| 5895 | (unsigned long long)pers->size(mddev, 0, 0) / 2); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5896 | err = -EINVAL; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5897 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5898 | if (err == 0 && pers->sync_request && |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 5899 | (mddev->bitmap_info.file || mddev->bitmap_info.offset)) { |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 5900 | struct bitmap *bitmap; |
| 5901 | |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 5902 | bitmap = md_bitmap_create(mddev, -1); |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 5903 | if (IS_ERR(bitmap)) { |
| 5904 | err = PTR_ERR(bitmap); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5905 | pr_warn("%s: failed to create bitmap (%d)\n", |
| 5906 | mdname(mddev), err); |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 5907 | } else |
| 5908 | mddev->bitmap = bitmap; |
| 5909 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 5910 | } |
Guoqing Jiang | d494549 | 2019-06-14 17:10:39 +0800 | [diff] [blame] | 5911 | if (err) |
| 5912 | goto bitmap_abort; |
Guoqing Jiang | 3e148a3 | 2019-06-19 17:30:46 +0800 | [diff] [blame] | 5913 | |
| 5914 | if (mddev->bitmap_info.max_write_behind > 0) { |
Guoqing Jiang | 3e173ab | 2019-12-23 10:48:54 +0100 | [diff] [blame] | 5915 | bool create_pool = false; |
Guoqing Jiang | 3e148a3 | 2019-06-19 17:30:46 +0800 | [diff] [blame] | 5916 | |
| 5917 | rdev_for_each(rdev, mddev) { |
| 5918 | if (test_bit(WriteMostly, &rdev->flags) && |
Guoqing Jiang | 404659c | 2019-12-23 10:48:53 +0100 | [diff] [blame] | 5919 | rdev_init_serial(rdev)) |
Guoqing Jiang | 3e173ab | 2019-12-23 10:48:54 +0100 | [diff] [blame] | 5920 | create_pool = true; |
Guoqing Jiang | 3e148a3 | 2019-06-19 17:30:46 +0800 | [diff] [blame] | 5921 | } |
Guoqing Jiang | 3e173ab | 2019-12-23 10:48:54 +0100 | [diff] [blame] | 5922 | if (create_pool && mddev->serial_info_pool == NULL) { |
Guoqing Jiang | 404659c | 2019-12-23 10:48:53 +0100 | [diff] [blame] | 5923 | mddev->serial_info_pool = |
| 5924 | mempool_create_kmalloc_pool(NR_SERIAL_INFOS, |
| 5925 | sizeof(struct serial_info)); |
| 5926 | if (!mddev->serial_info_pool) { |
Guoqing Jiang | 3e148a3 | 2019-06-19 17:30:46 +0800 | [diff] [blame] | 5927 | err = -ENOMEM; |
Guoqing Jiang | d494549 | 2019-06-14 17:10:39 +0800 | [diff] [blame] | 5928 | goto bitmap_abort; |
Guoqing Jiang | 3e148a3 | 2019-06-19 17:30:46 +0800 | [diff] [blame] | 5929 | } |
| 5930 | } |
| 5931 | } |
| 5932 | |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 5933 | if (mddev->queue) { |
Shaohua Li | bb086a8 | 2016-09-30 09:45:40 -0700 | [diff] [blame] | 5934 | bool nonrot = true; |
| 5935 | |
| 5936 | rdev_for_each(rdev, mddev) { |
| 5937 | if (rdev->raid_disk >= 0 && |
| 5938 | !blk_queue_nonrot(bdev_get_queue(rdev->bdev))) { |
| 5939 | nonrot = false; |
| 5940 | break; |
| 5941 | } |
| 5942 | } |
| 5943 | if (mddev->degraded) |
| 5944 | nonrot = false; |
| 5945 | if (nonrot) |
Bart Van Assche | 8b904b5 | 2018-03-07 17:10:10 -0800 | [diff] [blame] | 5946 | blk_queue_flag_set(QUEUE_FLAG_NONROT, mddev->queue); |
Shaohua Li | bb086a8 | 2016-09-30 09:45:40 -0700 | [diff] [blame] | 5947 | else |
Bart Van Assche | 8b904b5 | 2018-03-07 17:10:10 -0800 | [diff] [blame] | 5948 | blk_queue_flag_clear(QUEUE_FLAG_NONROT, mddev->queue); |
Jan Kara | dc3b17c | 2017-02-02 15:56:50 +0100 | [diff] [blame] | 5949 | mddev->queue->backing_dev_info->congested_data = mddev; |
| 5950 | mddev->queue->backing_dev_info->congested_fn = md_congested; |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 5951 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5952 | if (pers->sync_request) { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5953 | if (mddev->kobj.sd && |
| 5954 | sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5955 | pr_warn("md: cannot register extra attributes for %s\n", |
| 5956 | mdname(mddev)); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5957 | mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5958 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 5959 | mddev->ro = 0; |
| 5960 | |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 5961 | atomic_set(&mddev->max_corr_read_errors, |
| 5962 | MD_DEFAULT_MAX_CORRECTED_READ_ERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5963 | mddev->safemode = 0; |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 5964 | if (mddev_is_clustered(mddev)) |
| 5965 | mddev->safemode_delay = 0; |
| 5966 | else |
| 5967 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5968 | mddev->in_sync = 1; |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5969 | smp_wmb(); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5970 | spin_lock(&mddev->lock); |
| 5971 | mddev->pers = pers; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5972 | spin_unlock(&mddev->lock); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5973 | rdev_for_each(rdev, mddev) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5974 | if (rdev->raid_disk >= 0) |
Yufen Yu | e5b521e | 2019-06-14 15:41:07 -0700 | [diff] [blame] | 5975 | sysfs_link_rdev(mddev, rdev); /* failure here is OK */ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5976 | |
NeilBrown | a4a3d26 | 2015-07-17 11:57:30 +1000 | [diff] [blame] | 5977 | if (mddev->degraded && !mddev->ro) |
| 5978 | /* This ensures that recovering status is reported immediately |
| 5979 | * via sysfs - until a lack of spares is confirmed. |
| 5980 | */ |
| 5981 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5982 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5983 | |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 5984 | if (mddev->sb_flags) |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5985 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5986 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5987 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5988 | return 0; |
Xiao Ni | b126194 | 2018-01-24 12:17:38 +0800 | [diff] [blame] | 5989 | |
Guoqing Jiang | d494549 | 2019-06-14 17:10:39 +0800 | [diff] [blame] | 5990 | bitmap_abort: |
| 5991 | mddev_detach(mddev); |
| 5992 | if (mddev->private) |
| 5993 | pers->free(mddev, mddev->private); |
| 5994 | mddev->private = NULL; |
| 5995 | module_put(pers->owner); |
| 5996 | md_bitmap_destroy(mddev); |
Xiao Ni | b126194 | 2018-01-24 12:17:38 +0800 | [diff] [blame] | 5997 | abort: |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 5998 | bioset_exit(&mddev->bio_set); |
| 5999 | bioset_exit(&mddev->sync_set); |
Xiao Ni | b126194 | 2018-01-24 12:17:38 +0800 | [diff] [blame] | 6000 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6001 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 6002 | EXPORT_SYMBOL_GPL(md_run); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6003 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6004 | static int do_md_run(struct mddev *mddev) |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 6005 | { |
| 6006 | int err; |
| 6007 | |
NeilBrown | 9d4b45d | 2019-08-20 10:21:09 +1000 | [diff] [blame] | 6008 | set_bit(MD_NOT_READY, &mddev->flags); |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 6009 | err = md_run(mddev); |
| 6010 | if (err) |
| 6011 | goto out; |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 6012 | err = md_bitmap_load(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6013 | if (err) { |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 6014 | md_bitmap_destroy(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6015 | goto out; |
| 6016 | } |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 6017 | |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 6018 | if (mddev_is_clustered(mddev)) |
| 6019 | md_allow_write(mddev); |
| 6020 | |
Song Liu | d5d885f | 2017-11-19 22:17:01 -0800 | [diff] [blame] | 6021 | /* run start up tasks that require md_thread */ |
| 6022 | md_start(mddev); |
| 6023 | |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 6024 | md_wakeup_thread(mddev->thread); |
| 6025 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
| 6026 | |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 6027 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 6028 | revalidate_disk(mddev->gendisk); |
NeilBrown | 9d4b45d | 2019-08-20 10:21:09 +1000 | [diff] [blame] | 6029 | clear_bit(MD_NOT_READY, &mddev->flags); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6030 | mddev->changed = 1; |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 6031 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
NeilBrown | 9d4b45d | 2019-08-20 10:21:09 +1000 | [diff] [blame] | 6032 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
| 6033 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 6034 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 6035 | out: |
NeilBrown | 9d4b45d | 2019-08-20 10:21:09 +1000 | [diff] [blame] | 6036 | clear_bit(MD_NOT_READY, &mddev->flags); |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 6037 | return err; |
| 6038 | } |
| 6039 | |
Song Liu | d5d885f | 2017-11-19 22:17:01 -0800 | [diff] [blame] | 6040 | int md_start(struct mddev *mddev) |
| 6041 | { |
| 6042 | int ret = 0; |
| 6043 | |
| 6044 | if (mddev->pers->start) { |
| 6045 | set_bit(MD_RECOVERY_WAIT, &mddev->recovery); |
| 6046 | md_wakeup_thread(mddev->thread); |
| 6047 | ret = mddev->pers->start(mddev); |
| 6048 | clear_bit(MD_RECOVERY_WAIT, &mddev->recovery); |
| 6049 | md_wakeup_thread(mddev->sync_thread); |
| 6050 | } |
| 6051 | return ret; |
| 6052 | } |
| 6053 | EXPORT_SYMBOL_GPL(md_start); |
| 6054 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6055 | static int restart_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6056 | { |
| 6057 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | 97b20ef | 2017-04-13 08:53:48 +1000 | [diff] [blame] | 6058 | struct md_rdev *rdev; |
| 6059 | bool has_journal = false; |
| 6060 | bool has_readonly = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6061 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 6062 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6063 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 6064 | return -ENXIO; |
| 6065 | if (!mddev->pers) |
| 6066 | return -EINVAL; |
| 6067 | if (!mddev->ro) |
| 6068 | return -EBUSY; |
Song Liu | 339421d | 2015-10-08 21:54:13 -0700 | [diff] [blame] | 6069 | |
NeilBrown | 97b20ef | 2017-04-13 08:53:48 +1000 | [diff] [blame] | 6070 | rcu_read_lock(); |
| 6071 | rdev_for_each_rcu(rdev, mddev) { |
| 6072 | if (test_bit(Journal, &rdev->flags) && |
| 6073 | !test_bit(Faulty, &rdev->flags)) |
| 6074 | has_journal = true; |
| 6075 | if (bdev_read_only(rdev->bdev)) |
| 6076 | has_readonly = true; |
Song Liu | 339421d | 2015-10-08 21:54:13 -0700 | [diff] [blame] | 6077 | } |
NeilBrown | 97b20ef | 2017-04-13 08:53:48 +1000 | [diff] [blame] | 6078 | rcu_read_unlock(); |
| 6079 | if (test_bit(MD_HAS_JOURNAL, &mddev->flags) && !has_journal) |
| 6080 | /* Don't restart rw with journal missing/faulty */ |
| 6081 | return -EINVAL; |
| 6082 | if (has_readonly) |
| 6083 | return -EROFS; |
Song Liu | 339421d | 2015-10-08 21:54:13 -0700 | [diff] [blame] | 6084 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 6085 | mddev->safemode = 0; |
| 6086 | mddev->ro = 0; |
| 6087 | set_disk_ro(disk, 0); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6088 | pr_debug("md: %s switched to read-write mode.\n", mdname(mddev)); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 6089 | /* Kick recovery or resync if necessary */ |
| 6090 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6091 | md_wakeup_thread(mddev->thread); |
| 6092 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6093 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 6094 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6095 | } |
| 6096 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6097 | static void md_clean(struct mddev *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6098 | { |
| 6099 | mddev->array_sectors = 0; |
| 6100 | mddev->external_size = 0; |
| 6101 | mddev->dev_sectors = 0; |
| 6102 | mddev->raid_disks = 0; |
| 6103 | mddev->recovery_cp = 0; |
| 6104 | mddev->resync_min = 0; |
| 6105 | mddev->resync_max = MaxSector; |
| 6106 | mddev->reshape_position = MaxSector; |
| 6107 | mddev->external = 0; |
| 6108 | mddev->persistent = 0; |
| 6109 | mddev->level = LEVEL_NONE; |
| 6110 | mddev->clevel[0] = 0; |
| 6111 | mddev->flags = 0; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 6112 | mddev->sb_flags = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6113 | mddev->ro = 0; |
| 6114 | mddev->metadata_type[0] = 0; |
| 6115 | mddev->chunk_sectors = 0; |
| 6116 | mddev->ctime = mddev->utime = 0; |
| 6117 | mddev->layout = 0; |
| 6118 | mddev->max_disks = 0; |
| 6119 | mddev->events = 0; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 6120 | mddev->can_decrease_events = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6121 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6122 | mddev->reshape_backwards = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6123 | mddev->new_level = LEVEL_NONE; |
| 6124 | mddev->new_layout = 0; |
| 6125 | mddev->new_chunk_sectors = 0; |
| 6126 | mddev->curr_resync = 0; |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 6127 | atomic64_set(&mddev->resync_mismatches, 0); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6128 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 6129 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 6130 | mddev->recovery = 0; |
| 6131 | mddev->in_sync = 0; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6132 | mddev->changed = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6133 | mddev->degraded = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6134 | mddev->safemode = 0; |
NeilBrown | bd69192 | 2015-06-25 17:01:40 +1000 | [diff] [blame] | 6135 | mddev->private = NULL; |
Guoqing Jiang | c20c33f | 2016-08-12 13:42:38 +0800 | [diff] [blame] | 6136 | mddev->cluster_info = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6137 | mddev->bitmap_info.offset = 0; |
| 6138 | mddev->bitmap_info.default_offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 6139 | mddev->bitmap_info.default_space = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6140 | mddev->bitmap_info.chunksize = 0; |
| 6141 | mddev->bitmap_info.daemon_sleep = 0; |
| 6142 | mddev->bitmap_info.max_write_behind = 0; |
Guoqing Jiang | c20c33f | 2016-08-12 13:42:38 +0800 | [diff] [blame] | 6143 | mddev->bitmap_info.nodes = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6144 | } |
| 6145 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6146 | static void __md_stop_writes(struct mddev *mddev) |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 6147 | { |
NeilBrown | 6b6204e | 2013-05-09 09:48:30 +1000 | [diff] [blame] | 6148 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6149 | flush_workqueue(md_misc_wq); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 6150 | if (mddev->sync_thread) { |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 6151 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 6152 | md_reap_sync_thread(mddev); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 6153 | } |
| 6154 | |
| 6155 | del_timer_sync(&mddev->safemode_timer); |
| 6156 | |
Shaohua Li | 034e33f | 2016-11-21 10:29:19 -0800 | [diff] [blame] | 6157 | if (mddev->pers && mddev->pers->quiesce) { |
| 6158 | mddev->pers->quiesce(mddev, 1); |
| 6159 | mddev->pers->quiesce(mddev, 0); |
| 6160 | } |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 6161 | md_bitmap_flush(mddev); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 6162 | |
NeilBrown | b6d428c | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 6163 | if (mddev->ro == 0 && |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 6164 | ((!mddev->in_sync && !mddev_is_clustered(mddev)) || |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 6165 | mddev->sb_flags)) { |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 6166 | /* mark array as shutdown cleanly */ |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 6167 | if (!mddev_is_clustered(mddev)) |
| 6168 | mddev->in_sync = 1; |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 6169 | md_update_sb(mddev, 1); |
| 6170 | } |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 6171 | /* disable policy to guarantee rdevs free resources for serialization */ |
| 6172 | mddev->serialize_policy = 0; |
| 6173 | mddev_destroy_serial_pool(mddev, NULL, true); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 6174 | } |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 6175 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6176 | void md_stop_writes(struct mddev *mddev) |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 6177 | { |
NeilBrown | 29f097c | 2013-11-14 17:54:51 +1100 | [diff] [blame] | 6178 | mddev_lock_nointr(mddev); |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 6179 | __md_stop_writes(mddev); |
| 6180 | mddev_unlock(mddev); |
| 6181 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 6182 | EXPORT_SYMBOL_GPL(md_stop_writes); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 6183 | |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 6184 | static void mddev_detach(struct mddev *mddev) |
| 6185 | { |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 6186 | md_bitmap_wait_behind_writes(mddev); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 6187 | if (mddev->pers && mddev->pers->quiesce) { |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 6188 | mddev->pers->quiesce(mddev, 1); |
| 6189 | mddev->pers->quiesce(mddev, 0); |
| 6190 | } |
| 6191 | md_unregister_thread(&mddev->thread); |
| 6192 | if (mddev->queue) |
| 6193 | blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/ |
| 6194 | } |
| 6195 | |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 6196 | static void __md_stop(struct mddev *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6197 | { |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 6198 | struct md_personality *pers = mddev->pers; |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 6199 | md_bitmap_destroy(mddev); |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 6200 | mddev_detach(mddev); |
NeilBrown | ee5d004 | 2015-07-22 10:20:07 +1000 | [diff] [blame] | 6201 | /* Ensure ->event_work is done */ |
| 6202 | flush_workqueue(md_misc_wq); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 6203 | spin_lock(&mddev->lock); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6204 | mddev->pers = NULL; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 6205 | spin_unlock(&mddev->lock); |
| 6206 | pers->free(mddev, mddev->private); |
NeilBrown | bd69192 | 2015-06-25 17:01:40 +1000 | [diff] [blame] | 6207 | mddev->private = NULL; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 6208 | if (pers->sync_request && mddev->to_remove == NULL) |
| 6209 | mddev->to_remove = &md_redundancy_group; |
| 6210 | module_put(pers->owner); |
NeilBrown | cca9cf9 | 2010-04-01 12:08:16 +1100 | [diff] [blame] | 6211 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Jack Wang | 6aaa58c | 2018-10-19 16:21:31 +0200 | [diff] [blame] | 6212 | } |
| 6213 | |
| 6214 | void md_stop(struct mddev *mddev) |
| 6215 | { |
| 6216 | /* stop the array and free an attached data structures. |
| 6217 | * This is called from dm-raid |
| 6218 | */ |
| 6219 | __md_stop(mddev); |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 6220 | bioset_exit(&mddev->bio_set); |
| 6221 | bioset_exit(&mddev->sync_set); |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 6222 | } |
| 6223 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 6224 | EXPORT_SYMBOL_GPL(md_stop); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6225 | |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 6226 | static int md_set_readonly(struct mddev *mddev, struct block_device *bdev) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 6227 | { |
| 6228 | int err = 0; |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6229 | int did_freeze = 0; |
| 6230 | |
| 6231 | if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) { |
| 6232 | did_freeze = 1; |
| 6233 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 6234 | md_wakeup_thread(mddev->thread); |
| 6235 | } |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6236 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6237 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6238 | if (mddev->sync_thread) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6239 | /* Thread might be blocked waiting for metadata update |
| 6240 | * which will now never happen */ |
| 6241 | wake_up_process(mddev->sync_thread->tsk); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6242 | |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 6243 | if (mddev->external && test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) |
NeilBrown | 88724bf | 2015-09-24 14:00:51 +1000 | [diff] [blame] | 6244 | return -EBUSY; |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6245 | mddev_unlock(mddev); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6246 | wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING, |
| 6247 | &mddev->recovery)); |
NeilBrown | 88724bf | 2015-09-24 14:00:51 +1000 | [diff] [blame] | 6248 | wait_event(mddev->sb_wait, |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 6249 | !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6250 | mddev_lock_nointr(mddev); |
| 6251 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 6252 | mutex_lock(&mddev->open_mutex); |
NeilBrown | 9ba3b7f | 2014-09-09 14:00:15 +1000 | [diff] [blame] | 6253 | if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) || |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6254 | mddev->sync_thread || |
Guoqing Jiang | af8d8e6 | 2016-08-12 13:42:37 +0800 | [diff] [blame] | 6255 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6256 | pr_warn("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6257 | if (did_freeze) { |
| 6258 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 45eaf45 | 2014-10-29 08:49:50 +1100 | [diff] [blame] | 6259 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6260 | md_wakeup_thread(mddev->thread); |
| 6261 | } |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 6262 | err = -EBUSY; |
| 6263 | goto out; |
| 6264 | } |
| 6265 | if (mddev->pers) { |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 6266 | __md_stop_writes(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 6267 | |
| 6268 | err = -ENXIO; |
| 6269 | if (mddev->ro==1) |
| 6270 | goto out; |
| 6271 | mddev->ro = 1; |
| 6272 | set_disk_ro(mddev->gendisk, 1); |
| 6273 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 45eaf45 | 2014-10-29 08:49:50 +1100 | [diff] [blame] | 6274 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6275 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6276 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6277 | err = 0; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 6278 | } |
| 6279 | out: |
| 6280 | mutex_unlock(&mddev->open_mutex); |
| 6281 | return err; |
| 6282 | } |
| 6283 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 6284 | /* mode: |
| 6285 | * 0 - completely stop and dis-assemble array |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 6286 | * 2 - stop but do not disassemble array |
| 6287 | */ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6288 | static int do_md_stop(struct mddev *mddev, int mode, |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 6289 | struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6290 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6291 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6292 | struct md_rdev *rdev; |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6293 | int did_freeze = 0; |
| 6294 | |
| 6295 | if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) { |
| 6296 | did_freeze = 1; |
| 6297 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 6298 | md_wakeup_thread(mddev->thread); |
| 6299 | } |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6300 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6301 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6302 | if (mddev->sync_thread) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6303 | /* Thread might be blocked waiting for metadata update |
| 6304 | * which will now never happen */ |
| 6305 | wake_up_process(mddev->sync_thread->tsk); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6306 | |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6307 | mddev_unlock(mddev); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6308 | wait_event(resync_wait, (mddev->sync_thread == NULL && |
| 6309 | !test_bit(MD_RECOVERY_RUNNING, |
| 6310 | &mddev->recovery))); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6311 | mddev_lock_nointr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6312 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 6313 | mutex_lock(&mddev->open_mutex); |
NeilBrown | 9ba3b7f | 2014-09-09 14:00:15 +1000 | [diff] [blame] | 6314 | if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) || |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6315 | mddev->sysfs_active || |
| 6316 | mddev->sync_thread || |
Guoqing Jiang | af8d8e6 | 2016-08-12 13:42:37 +0800 | [diff] [blame] | 6317 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6318 | pr_warn("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 6319 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6320 | if (did_freeze) { |
| 6321 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 45eaf45 | 2014-10-29 08:49:50 +1100 | [diff] [blame] | 6322 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6323 | md_wakeup_thread(mddev->thread); |
| 6324 | } |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 6325 | return -EBUSY; |
| 6326 | } |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 6327 | if (mddev->pers) { |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 6328 | if (mddev->ro) |
| 6329 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6330 | |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 6331 | __md_stop_writes(mddev); |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 6332 | __md_stop(mddev); |
Jan Kara | dc3b17c | 2017-02-02 15:56:50 +0100 | [diff] [blame] | 6333 | mddev->queue->backing_dev_info->congested_fn = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6334 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 6335 | /* tell userspace to handle 'inactive' */ |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6336 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 6337 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 6338 | rdev_for_each(rdev, mddev) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 6339 | if (rdev->raid_disk >= 0) |
| 6340 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 6341 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 6342 | set_capacity(disk, 0); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 6343 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6344 | mddev->changed = 1; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 6345 | revalidate_disk(disk); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 6346 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 6347 | if (mddev->ro) |
| 6348 | mddev->ro = 0; |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 6349 | } else |
| 6350 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6351 | /* |
| 6352 | * Free resources if final stop |
| 6353 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 6354 | if (mode == 0) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6355 | pr_info("md: %s stopped.\n", mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6356 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6357 | if (mddev->bitmap_info.file) { |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6358 | struct file *f = mddev->bitmap_info.file; |
| 6359 | spin_lock(&mddev->lock); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6360 | mddev->bitmap_info.file = NULL; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6361 | spin_unlock(&mddev->lock); |
| 6362 | fput(f); |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 6363 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6364 | mddev->bitmap_info.offset = 0; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 6365 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6366 | export_array(mddev); |
| 6367 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6368 | md_clean(mddev); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6369 | if (mddev->hold_active == UNTIL_STOP) |
| 6370 | mddev->hold_active = 0; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 6371 | } |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6372 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6373 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 6374 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6375 | } |
| 6376 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 6377 | #ifndef MODULE |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6378 | static void autorun_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6379 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6380 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6381 | int err; |
| 6382 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 6383 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6384 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6385 | |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6386 | pr_info("md: running: "); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6387 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 6388 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6389 | char b[BDEVNAME_SIZE]; |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6390 | pr_cont("<%s>", bdevname(rdev->bdev,b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6391 | } |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6392 | pr_cont("\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6393 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6394 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6395 | if (err) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6396 | pr_warn("md: do_md_run() returned %d\n", err); |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 6397 | do_md_stop(mddev, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6398 | } |
| 6399 | } |
| 6400 | |
| 6401 | /* |
| 6402 | * lets try to run arrays based on all disks that have arrived |
| 6403 | * until now. (those are in pending_raid_disks) |
| 6404 | * |
| 6405 | * the method: pick the first pending disk, collect all disks with |
| 6406 | * the same UUID, remove all from the pending list and put them into |
| 6407 | * the 'same_array' list. Then order this list based on superblock |
| 6408 | * update time (freshest comes first), kick out 'old' disks and |
| 6409 | * compare superblocks. If everything's fine then run it. |
| 6410 | * |
| 6411 | * If "unit" is allocated, then bump its reference count |
| 6412 | */ |
| 6413 | static void autorun_devices(int part) |
| 6414 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6415 | struct md_rdev *rdev0, *rdev, *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6416 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6417 | char b[BDEVNAME_SIZE]; |
| 6418 | |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6419 | pr_info("md: autorun ...\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6420 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 6421 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6422 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 6423 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6424 | rdev0 = list_entry(pending_raid_disks.next, |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6425 | struct md_rdev, same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6426 | |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6427 | pr_debug("md: considering %s ...\n", bdevname(rdev0->bdev,b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6428 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6429 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6430 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6431 | pr_debug("md: adding %s ...\n", |
| 6432 | bdevname(rdev->bdev,b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6433 | list_move(&rdev->same_set, &candidates); |
| 6434 | } |
| 6435 | /* |
| 6436 | * now we have a set of devices, with all of them having |
| 6437 | * mostly sane superblocks. It's time to allocate the |
| 6438 | * mddev. |
| 6439 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 6440 | if (part) { |
| 6441 | dev = MKDEV(mdp_major, |
| 6442 | rdev0->preferred_minor << MdpMinorShift); |
| 6443 | unit = MINOR(dev) >> MdpMinorShift; |
| 6444 | } else { |
| 6445 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 6446 | unit = MINOR(dev); |
| 6447 | } |
| 6448 | if (rdev0->preferred_minor != unit) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6449 | pr_warn("md: unit number in %s is bad: %d\n", |
| 6450 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6451 | break; |
| 6452 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6453 | |
| 6454 | md_probe(dev, NULL, NULL); |
| 6455 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 6456 | if (!mddev || !mddev->gendisk) { |
| 6457 | if (mddev) |
| 6458 | mddev_put(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6459 | break; |
| 6460 | } |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6461 | if (mddev_lock(mddev)) |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6462 | pr_warn("md: %s locked, cannot run\n", mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6463 | else if (mddev->raid_disks || mddev->major_version |
| 6464 | || !list_empty(&mddev->disks)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6465 | pr_warn("md: %s already running, cannot run %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6466 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 6467 | mddev_unlock(mddev); |
| 6468 | } else { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6469 | pr_debug("md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 6470 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6471 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6472 | list_del_init(&rdev->same_set); |
| 6473 | if (bind_rdev_to_array(rdev, mddev)) |
| 6474 | export_rdev(rdev); |
| 6475 | } |
| 6476 | autorun_array(mddev); |
| 6477 | mddev_unlock(mddev); |
| 6478 | } |
| 6479 | /* on success, candidates will be empty, on error |
| 6480 | * it won't... |
| 6481 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6482 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6483 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6484 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6485 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6486 | mddev_put(mddev); |
| 6487 | } |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6488 | pr_info("md: ... autorun DONE.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6489 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 6490 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6491 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6492 | static int get_version(void __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6493 | { |
| 6494 | mdu_version_t ver; |
| 6495 | |
| 6496 | ver.major = MD_MAJOR_VERSION; |
| 6497 | ver.minor = MD_MINOR_VERSION; |
| 6498 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 6499 | |
| 6500 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 6501 | return -EFAULT; |
| 6502 | |
| 6503 | return 0; |
| 6504 | } |
| 6505 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6506 | static int get_array_info(struct mddev *mddev, void __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6507 | { |
| 6508 | mdu_array_info_t info; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 6509 | int nr,working,insync,failed,spare; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6510 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6511 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6512 | nr = working = insync = failed = spare = 0; |
| 6513 | rcu_read_lock(); |
| 6514 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6515 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6516 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6517 | failed++; |
| 6518 | else { |
| 6519 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6520 | if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6521 | insync++; |
Song Liu | b347af8 | 2016-08-11 17:14:45 -0700 | [diff] [blame] | 6522 | else if (test_bit(Journal, &rdev->flags)) |
| 6523 | /* TODO: add journal count to md_u.h */ |
| 6524 | ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6525 | else |
| 6526 | spare++; |
| 6527 | } |
| 6528 | } |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6529 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6530 | |
| 6531 | info.major_version = mddev->major_version; |
| 6532 | info.minor_version = mddev->minor_version; |
| 6533 | info.patch_version = MD_PATCHLEVEL_VERSION; |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 6534 | info.ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6535 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6536 | info.size = mddev->dev_sectors / 2; |
| 6537 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 6538 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6539 | info.nr_disks = nr; |
| 6540 | info.raid_disks = mddev->raid_disks; |
| 6541 | info.md_minor = mddev->md_minor; |
| 6542 | info.not_persistent= !mddev->persistent; |
| 6543 | |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 6544 | info.utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6545 | info.state = 0; |
| 6546 | if (mddev->in_sync) |
| 6547 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6548 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 9bd3592 | 2014-07-02 11:35:06 +1000 | [diff] [blame] | 6549 | info.state |= (1<<MD_SB_BITMAP_PRESENT); |
Goldwyn Rodrigues | ca8895d | 2014-11-26 12:22:03 -0600 | [diff] [blame] | 6550 | if (mddev_is_clustered(mddev)) |
| 6551 | info.state |= (1<<MD_SB_CLUSTERED); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 6552 | info.active_disks = insync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6553 | info.working_disks = working; |
| 6554 | info.failed_disks = failed; |
| 6555 | info.spare_disks = spare; |
| 6556 | |
| 6557 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 6558 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6559 | |
| 6560 | if (copy_to_user(arg, &info, sizeof(info))) |
| 6561 | return -EFAULT; |
| 6562 | |
| 6563 | return 0; |
| 6564 | } |
| 6565 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6566 | static int get_bitmap_file(struct mddev *mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6567 | { |
| 6568 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
NeilBrown | f4ad3d3 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6569 | char *ptr; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6570 | int err; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6571 | |
Benjamin Randazzo | b6878d9 | 2015-07-25 16:36:50 +0200 | [diff] [blame] | 6572 | file = kzalloc(sizeof(*file), GFP_NOIO); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6573 | if (!file) |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6574 | return -ENOMEM; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6575 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6576 | err = 0; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6577 | spin_lock(&mddev->lock); |
Benjamin Randazzo | 25eafe1 | 2015-07-25 16:36:50 +0200 | [diff] [blame] | 6578 | /* bitmap enabled */ |
| 6579 | if (mddev->bitmap_info.file) { |
| 6580 | ptr = file_path(mddev->bitmap_info.file, file->pathname, |
| 6581 | sizeof(file->pathname)); |
| 6582 | if (IS_ERR(ptr)) |
| 6583 | err = PTR_ERR(ptr); |
| 6584 | else |
| 6585 | memmove(file->pathname, ptr, |
| 6586 | sizeof(file->pathname)-(ptr-file->pathname)); |
| 6587 | } |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6588 | spin_unlock(&mddev->lock); |
| 6589 | |
| 6590 | if (err == 0 && |
| 6591 | copy_to_user(arg, file, sizeof(*file))) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6592 | err = -EFAULT; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6593 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6594 | kfree(file); |
| 6595 | return err; |
| 6596 | } |
| 6597 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6598 | static int get_disk_info(struct mddev *mddev, void __user * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6599 | { |
| 6600 | mdu_disk_info_t info; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6601 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6602 | |
| 6603 | if (copy_from_user(&info, arg, sizeof(info))) |
| 6604 | return -EFAULT; |
| 6605 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6606 | rcu_read_lock(); |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 6607 | rdev = md_find_rdev_nr_rcu(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6608 | if (rdev) { |
| 6609 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 6610 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 6611 | info.raid_disk = rdev->raid_disk; |
| 6612 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6613 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6614 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6615 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6616 | info.state |= (1<<MD_DISK_ACTIVE); |
| 6617 | info.state |= (1<<MD_DISK_SYNC); |
| 6618 | } |
Shaohua Li | 9efdca1 | 2015-10-12 16:59:50 -0700 | [diff] [blame] | 6619 | if (test_bit(Journal, &rdev->flags)) |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 6620 | info.state |= (1<<MD_DISK_JOURNAL); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6621 | if (test_bit(WriteMostly, &rdev->flags)) |
| 6622 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 6623 | if (test_bit(FailFast, &rdev->flags)) |
| 6624 | info.state |= (1<<MD_DISK_FAILFAST); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6625 | } else { |
| 6626 | info.major = info.minor = 0; |
| 6627 | info.raid_disk = -1; |
| 6628 | info.state = (1<<MD_DISK_REMOVED); |
| 6629 | } |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6630 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6631 | |
| 6632 | if (copy_to_user(arg, &info, sizeof(info))) |
| 6633 | return -EFAULT; |
| 6634 | |
| 6635 | return 0; |
| 6636 | } |
| 6637 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6638 | static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6639 | { |
| 6640 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6641 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6642 | dev_t dev = MKDEV(info->major,info->minor); |
| 6643 | |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6644 | if (mddev_is_clustered(mddev) && |
| 6645 | !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6646 | pr_warn("%s: Cannot add to clustered mddev.\n", |
| 6647 | mdname(mddev)); |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6648 | return -EINVAL; |
| 6649 | } |
| 6650 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6651 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 6652 | return -EOVERFLOW; |
| 6653 | |
| 6654 | if (!mddev->raid_disks) { |
| 6655 | int err; |
| 6656 | /* expecting a device which has a superblock */ |
| 6657 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 6658 | if (IS_ERR(rdev)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6659 | pr_warn("md: md_import_device returned %ld\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6660 | PTR_ERR(rdev)); |
| 6661 | return PTR_ERR(rdev); |
| 6662 | } |
| 6663 | if (!list_empty(&mddev->disks)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6664 | struct md_rdev *rdev0 |
| 6665 | = list_entry(mddev->disks.next, |
| 6666 | struct md_rdev, same_set); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 6667 | err = super_types[mddev->major_version] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6668 | .load_super(rdev, rdev0, mddev->minor_version); |
| 6669 | if (err < 0) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6670 | pr_warn("md: %s has different UUID to %s\n", |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6671 | bdevname(rdev->bdev,b), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6672 | bdevname(rdev0->bdev,b2)); |
| 6673 | export_rdev(rdev); |
| 6674 | return -EINVAL; |
| 6675 | } |
| 6676 | } |
| 6677 | err = bind_rdev_to_array(rdev, mddev); |
| 6678 | if (err) |
| 6679 | export_rdev(rdev); |
| 6680 | return err; |
| 6681 | } |
| 6682 | |
| 6683 | /* |
| 6684 | * add_new_disk can be used once the array is assembled |
| 6685 | * to add "hot spares". They must already have a superblock |
| 6686 | * written |
| 6687 | */ |
| 6688 | if (mddev->pers) { |
| 6689 | int err; |
| 6690 | if (!mddev->pers->hot_add_disk) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6691 | pr_warn("%s: personality does not support diskops!\n", |
| 6692 | mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6693 | return -EINVAL; |
| 6694 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 6695 | if (mddev->persistent) |
| 6696 | rdev = md_import_device(dev, mddev->major_version, |
| 6697 | mddev->minor_version); |
| 6698 | else |
| 6699 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6700 | if (IS_ERR(rdev)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6701 | pr_warn("md: md_import_device returned %ld\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6702 | PTR_ERR(rdev)); |
| 6703 | return PTR_ERR(rdev); |
| 6704 | } |
NeilBrown | 1a855a0 | 2010-12-09 16:36:28 +1100 | [diff] [blame] | 6705 | /* set saved_raid_disk if appropriate */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6706 | if (!mddev->persistent) { |
| 6707 | if (info->state & (1<<MD_DISK_SYNC) && |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 6708 | info->raid_disk < mddev->raid_disks) { |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6709 | rdev->raid_disk = info->raid_disk; |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 6710 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 6711 | clear_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 6712 | } else |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6713 | rdev->raid_disk = -1; |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 6714 | rdev->saved_raid_disk = rdev->raid_disk; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6715 | } else |
| 6716 | super_types[mddev->major_version]. |
| 6717 | validate_super(mddev, rdev); |
NeilBrown | bedd86b | 2011-05-11 14:26:20 +1000 | [diff] [blame] | 6718 | if ((info->state & (1<<MD_DISK_SYNC)) && |
NeilBrown | f456309 | 2012-07-03 15:59:06 +1000 | [diff] [blame] | 6719 | rdev->raid_disk != info->raid_disk) { |
NeilBrown | bedd86b | 2011-05-11 14:26:20 +1000 | [diff] [blame] | 6720 | /* This was a hot-add request, but events doesn't |
| 6721 | * match, so reject it. |
| 6722 | */ |
| 6723 | export_rdev(rdev); |
| 6724 | return -EINVAL; |
| 6725 | } |
| 6726 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6727 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6728 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 6729 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6730 | else |
| 6731 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 6732 | if (info->state & (1<<MD_DISK_FAILFAST)) |
| 6733 | set_bit(FailFast, &rdev->flags); |
| 6734 | else |
| 6735 | clear_bit(FailFast, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6736 | |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 6737 | if (info->state & (1<<MD_DISK_JOURNAL)) { |
| 6738 | struct md_rdev *rdev2; |
| 6739 | bool has_journal = false; |
| 6740 | |
| 6741 | /* make sure no existing journal disk */ |
| 6742 | rdev_for_each(rdev2, mddev) { |
| 6743 | if (test_bit(Journal, &rdev2->flags)) { |
| 6744 | has_journal = true; |
| 6745 | break; |
| 6746 | } |
| 6747 | } |
NeilBrown | 230b55f | 2017-10-17 14:24:09 +1100 | [diff] [blame] | 6748 | if (has_journal || mddev->bitmap) { |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 6749 | export_rdev(rdev); |
| 6750 | return -EBUSY; |
| 6751 | } |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 6752 | set_bit(Journal, &rdev->flags); |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 6753 | } |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6754 | /* |
| 6755 | * check whether the device shows up in other nodes |
| 6756 | */ |
| 6757 | if (mddev_is_clustered(mddev)) { |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6758 | if (info->state & (1 << MD_DISK_CANDIDATE)) |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6759 | set_bit(Candidate, &rdev->flags); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6760 | else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) { |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6761 | /* --add initiated by this node */ |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6762 | err = md_cluster_ops->add_new_disk(mddev, rdev); |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6763 | if (err) { |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6764 | export_rdev(rdev); |
| 6765 | return err; |
| 6766 | } |
| 6767 | } |
| 6768 | } |
| 6769 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6770 | rdev->raid_disk = -1; |
| 6771 | err = bind_rdev_to_array(rdev, mddev); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6772 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6773 | if (err) |
| 6774 | export_rdev(rdev); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6775 | |
| 6776 | if (mddev_is_clustered(mddev)) { |
Guoqing Jiang | e566aef | 2016-08-12 13:42:34 +0800 | [diff] [blame] | 6777 | if (info->state & (1 << MD_DISK_CANDIDATE)) { |
| 6778 | if (!err) { |
| 6779 | err = md_cluster_ops->new_disk_ack(mddev, |
| 6780 | err == 0); |
| 6781 | if (err) |
| 6782 | md_kick_rdev_from_array(rdev); |
| 6783 | } |
| 6784 | } else { |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6785 | if (err) |
| 6786 | md_cluster_ops->add_new_disk_cancel(mddev); |
| 6787 | else |
| 6788 | err = add_bound_rdev(rdev); |
| 6789 | } |
| 6790 | |
| 6791 | } else if (!err) |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 6792 | err = add_bound_rdev(rdev); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6793 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6794 | return err; |
| 6795 | } |
| 6796 | |
| 6797 | /* otherwise, add_new_disk is only allowed |
| 6798 | * for major_version==0 superblocks |
| 6799 | */ |
| 6800 | if (mddev->major_version != 0) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6801 | pr_warn("%s: ADD_NEW_DISK not supported\n", mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6802 | return -EINVAL; |
| 6803 | } |
| 6804 | |
| 6805 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 6806 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6807 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6808 | if (IS_ERR(rdev)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6809 | pr_warn("md: error, md_import_device() returned %ld\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6810 | PTR_ERR(rdev)); |
| 6811 | return PTR_ERR(rdev); |
| 6812 | } |
| 6813 | rdev->desc_nr = info->number; |
| 6814 | if (info->raid_disk < mddev->raid_disks) |
| 6815 | rdev->raid_disk = info->raid_disk; |
| 6816 | else |
| 6817 | rdev->raid_disk = -1; |
| 6818 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6819 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6820 | if (info->state & (1<<MD_DISK_SYNC)) |
| 6821 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6822 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6823 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 6824 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 6825 | if (info->state & (1<<MD_DISK_FAILFAST)) |
| 6826 | set_bit(FailFast, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6827 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6828 | if (!mddev->persistent) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6829 | pr_debug("md: nonpersistent superblock ...\n"); |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 6830 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
| 6831 | } else |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 6832 | rdev->sb_start = calc_dev_sboffset(rdev); |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 6833 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6834 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 6835 | err = bind_rdev_to_array(rdev, mddev); |
| 6836 | if (err) { |
| 6837 | export_rdev(rdev); |
| 6838 | return err; |
| 6839 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6840 | } |
| 6841 | |
| 6842 | return 0; |
| 6843 | } |
| 6844 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6845 | static int hot_remove_disk(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6846 | { |
| 6847 | char b[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6848 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6849 | |
Yufen Yu | c42a0e2 | 2018-05-04 18:08:10 +0800 | [diff] [blame] | 6850 | if (!mddev->pers) |
| 6851 | return -ENODEV; |
| 6852 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6853 | rdev = find_rdev(mddev, dev); |
| 6854 | if (!rdev) |
| 6855 | return -ENXIO; |
| 6856 | |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 6857 | if (rdev->raid_disk < 0) |
| 6858 | goto kick_rdev; |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6859 | |
NeilBrown | 3ea8929d | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 6860 | clear_bit(Blocked, &rdev->flags); |
| 6861 | remove_and_add_spares(mddev, rdev); |
| 6862 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6863 | if (rdev->raid_disk >= 0) |
| 6864 | goto busy; |
| 6865 | |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 6866 | kick_rdev: |
Goldwyn Rodrigues | 54a8839 | 2015-12-21 10:51:00 +1100 | [diff] [blame] | 6867 | if (mddev_is_clustered(mddev)) |
Goldwyn Rodrigues | 88bcfef | 2015-04-14 10:44:44 -0500 | [diff] [blame] | 6868 | md_cluster_ops->remove_disk(mddev, rdev); |
| 6869 | |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 6870 | md_kick_rdev_from_array(rdev); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 6871 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | 060b068 | 2016-11-04 16:46:03 +1100 | [diff] [blame] | 6872 | if (mddev->thread) |
| 6873 | md_wakeup_thread(mddev->thread); |
| 6874 | else |
| 6875 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6876 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6877 | |
| 6878 | return 0; |
| 6879 | busy: |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6880 | pr_debug("md: cannot remove active disk %s from %s ...\n", |
| 6881 | bdevname(rdev->bdev,b), mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6882 | return -EBUSY; |
| 6883 | } |
| 6884 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6885 | static int hot_add_disk(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6886 | { |
| 6887 | char b[BDEVNAME_SIZE]; |
| 6888 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6889 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6890 | |
| 6891 | if (!mddev->pers) |
| 6892 | return -ENODEV; |
| 6893 | |
| 6894 | if (mddev->major_version != 0) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6895 | pr_warn("%s: HOT_ADD may only be used with version-0 superblocks.\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6896 | mdname(mddev)); |
| 6897 | return -EINVAL; |
| 6898 | } |
| 6899 | if (!mddev->pers->hot_add_disk) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6900 | pr_warn("%s: personality does not support diskops!\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6901 | mdname(mddev)); |
| 6902 | return -EINVAL; |
| 6903 | } |
| 6904 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6905 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6906 | if (IS_ERR(rdev)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6907 | pr_warn("md: error, md_import_device() returned %ld\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6908 | PTR_ERR(rdev)); |
| 6909 | return -EINVAL; |
| 6910 | } |
| 6911 | |
| 6912 | if (mddev->persistent) |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 6913 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6914 | else |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 6915 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6916 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 6917 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6918 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6919 | if (test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6920 | pr_warn("md: can not hot-add faulty %s disk to %s!\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6921 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 6922 | err = -EINVAL; |
| 6923 | goto abort_export; |
| 6924 | } |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6925 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6926 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6927 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 6928 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 6929 | err = bind_rdev_to_array(rdev, mddev); |
| 6930 | if (err) |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 6931 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6932 | |
| 6933 | /* |
| 6934 | * The rest should better be atomic, we can have disk failures |
| 6935 | * noticed in interrupt contexts ... |
| 6936 | */ |
| 6937 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6938 | rdev->raid_disk = -1; |
| 6939 | |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 6940 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | 060b068 | 2016-11-04 16:46:03 +1100 | [diff] [blame] | 6941 | if (!mddev->thread) |
| 6942 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6943 | /* |
| 6944 | * Kick recovery, maybe this spare has to be added to the |
| 6945 | * array immediately. |
| 6946 | */ |
| 6947 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6948 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6949 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6950 | return 0; |
| 6951 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6952 | abort_export: |
| 6953 | export_rdev(rdev); |
| 6954 | return err; |
| 6955 | } |
| 6956 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6957 | static int set_bitmap_file(struct mddev *mddev, int fd) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6958 | { |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6959 | int err = 0; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6960 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6961 | if (mddev->pers) { |
NeilBrown | d66b1b3 | 2014-08-08 15:40:24 +1000 | [diff] [blame] | 6962 | if (!mddev->pers->quiesce || !mddev->thread) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6963 | return -EBUSY; |
| 6964 | if (mddev->recovery || mddev->sync_thread) |
| 6965 | return -EBUSY; |
| 6966 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6967 | } |
| 6968 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6969 | if (fd >= 0) { |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6970 | struct inode *inode; |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6971 | struct file *f; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6972 | |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6973 | if (mddev->bitmap || mddev->bitmap_info.file) |
| 6974 | return -EEXIST; /* cannot add when bitmap is present */ |
| 6975 | f = fget(fd); |
| 6976 | |
| 6977 | if (f == NULL) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6978 | pr_warn("%s: error: failed to get bitmap file\n", |
| 6979 | mdname(mddev)); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6980 | return -EBADF; |
| 6981 | } |
| 6982 | |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6983 | inode = f->f_mapping->host; |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6984 | if (!S_ISREG(inode->i_mode)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6985 | pr_warn("%s: error: bitmap file must be a regular file\n", |
| 6986 | mdname(mddev)); |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6987 | err = -EBADF; |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6988 | } else if (!(f->f_mode & FMODE_WRITE)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6989 | pr_warn("%s: error: bitmap file must open for write\n", |
| 6990 | mdname(mddev)); |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6991 | err = -EBADF; |
| 6992 | } else if (atomic_read(&inode->i_writecount) != 1) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6993 | pr_warn("%s: error: bitmap file is already in use\n", |
| 6994 | mdname(mddev)); |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6995 | err = -EBUSY; |
| 6996 | } |
| 6997 | if (err) { |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6998 | fput(f); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6999 | return err; |
| 7000 | } |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 7001 | mddev->bitmap_info.file = f; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 7002 | mddev->bitmap_info.offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7003 | } else if (mddev->bitmap == NULL) |
| 7004 | return -ENOENT; /* cannot remove what isn't there */ |
| 7005 | err = 0; |
| 7006 | if (mddev->pers) { |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 7007 | if (fd >= 0) { |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 7008 | struct bitmap *bitmap; |
| 7009 | |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 7010 | bitmap = md_bitmap_create(mddev, -1); |
NeilBrown | 9e1cc0a | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 7011 | mddev_suspend(mddev); |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 7012 | if (!IS_ERR(bitmap)) { |
| 7013 | mddev->bitmap = bitmap; |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 7014 | err = md_bitmap_load(mddev); |
NeilBrown | ba599ac | 2015-02-25 11:44:11 +1100 | [diff] [blame] | 7015 | } else |
| 7016 | err = PTR_ERR(bitmap); |
NeilBrown | 52a0d49 | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 7017 | if (err) { |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 7018 | md_bitmap_destroy(mddev); |
NeilBrown | 52a0d49 | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 7019 | fd = -1; |
| 7020 | } |
NeilBrown | 9e1cc0a | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 7021 | mddev_resume(mddev); |
NeilBrown | 52a0d49 | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 7022 | } else if (fd < 0) { |
NeilBrown | 9e1cc0a | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 7023 | mddev_suspend(mddev); |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 7024 | md_bitmap_destroy(mddev); |
NeilBrown | 9e1cc0a | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 7025 | mddev_resume(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 7026 | } |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 7027 | } |
| 7028 | if (fd < 0) { |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 7029 | struct file *f = mddev->bitmap_info.file; |
| 7030 | if (f) { |
| 7031 | spin_lock(&mddev->lock); |
| 7032 | mddev->bitmap_info.file = NULL; |
| 7033 | spin_unlock(&mddev->lock); |
| 7034 | fput(f); |
| 7035 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7036 | } |
| 7037 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7038 | return err; |
| 7039 | } |
| 7040 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7041 | /* |
| 7042 | * set_array_info is used two different ways |
| 7043 | * The original usage is when creating a new array. |
| 7044 | * In this usage, raid_disks is > 0 and it together with |
| 7045 | * level, size, not_persistent,layout,chunksize determine the |
| 7046 | * shape of the array. |
| 7047 | * This will always create an array with a type-0.90.0 superblock. |
| 7048 | * The newer usage is when assembling an array. |
| 7049 | * In this case raid_disks will be 0, and the major_version field is |
| 7050 | * use to determine which style super-blocks are to be found on the devices. |
| 7051 | * The minor and patch _version numbers are also kept incase the |
| 7052 | * super_block handler wishes to interpret them. |
| 7053 | */ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7054 | static int set_array_info(struct mddev *mddev, mdu_array_info_t *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7055 | { |
| 7056 | |
| 7057 | if (info->raid_disks == 0) { |
| 7058 | /* just setting version number for superblock loading */ |
| 7059 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 7060 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7061 | super_types[info->major_version].name == NULL) { |
| 7062 | /* maybe try to auto-load a module? */ |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7063 | pr_warn("md: superblock version %d not known\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7064 | info->major_version); |
| 7065 | return -EINVAL; |
| 7066 | } |
| 7067 | mddev->major_version = info->major_version; |
| 7068 | mddev->minor_version = info->minor_version; |
| 7069 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 7070 | mddev->persistent = !info->not_persistent; |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 7071 | /* ensure mddev_put doesn't delete this now that there |
| 7072 | * is some minimal configuration. |
| 7073 | */ |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 7074 | mddev->ctime = ktime_get_real_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7075 | return 0; |
| 7076 | } |
| 7077 | mddev->major_version = MD_MAJOR_VERSION; |
| 7078 | mddev->minor_version = MD_MINOR_VERSION; |
| 7079 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 7080 | mddev->ctime = ktime_get_real_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7081 | |
| 7082 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 7083 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7084 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7085 | mddev->raid_disks = info->raid_disks; |
| 7086 | /* don't set md_minor, it is determined by which /dev/md* was |
| 7087 | * openned |
| 7088 | */ |
| 7089 | if (info->state & (1<<MD_SB_CLEAN)) |
| 7090 | mddev->recovery_cp = MaxSector; |
| 7091 | else |
| 7092 | mddev->recovery_cp = 0; |
| 7093 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 7094 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7095 | |
| 7096 | mddev->layout = info->layout; |
NeilBrown | 33f2c35 | 2019-09-09 16:52:29 +1000 | [diff] [blame] | 7097 | if (mddev->level == 0) |
| 7098 | /* Cannot trust RAID0 layout info here */ |
| 7099 | mddev->layout = -1; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 7100 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7101 | |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 7102 | if (mddev->persistent) { |
NeilBrown | 1b3bae4 | 2017-03-01 07:31:28 +1100 | [diff] [blame] | 7103 | mddev->max_disks = MD_SB_DISKS; |
| 7104 | mddev->flags = 0; |
| 7105 | mddev->sb_flags = 0; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 7106 | } |
| 7107 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7108 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 7109 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 7110 | mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 7111 | mddev->bitmap_info.offset = 0; |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 7112 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7113 | mddev->reshape_position = MaxSector; |
| 7114 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7115 | /* |
| 7116 | * Generate a 128 bit UUID |
| 7117 | */ |
| 7118 | get_random_bytes(mddev->uuid, 16); |
| 7119 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7120 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 7121 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7122 | mddev->new_layout = mddev->layout; |
| 7123 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7124 | mddev->reshape_backwards = 0; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7125 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7126 | return 0; |
| 7127 | } |
| 7128 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7129 | void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors) |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 7130 | { |
Shaohua Li | efa4b77 | 2017-10-18 22:08:13 -0700 | [diff] [blame] | 7131 | lockdep_assert_held(&mddev->reconfig_mutex); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 7132 | |
| 7133 | if (mddev->external_size) |
| 7134 | return; |
| 7135 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 7136 | mddev->array_sectors = array_sectors; |
| 7137 | } |
| 7138 | EXPORT_SYMBOL(md_set_array_sectors); |
| 7139 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7140 | static int update_size(struct mddev *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 7141 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7142 | struct md_rdev *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 7143 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 7144 | int fit = (num_sectors == 0); |
Guoqing Jiang | 818da59 | 2017-03-01 16:42:40 +0800 | [diff] [blame] | 7145 | sector_t old_dev_sectors = mddev->dev_sectors; |
Guoqing Jiang | ab5a98b | 2016-05-02 11:33:13 -0400 | [diff] [blame] | 7146 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 7147 | if (mddev->pers->resize == NULL) |
| 7148 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 7149 | /* The "num_sectors" is the number of sectors of each device that |
| 7150 | * is used. This can only make sense for arrays with redundancy. |
| 7151 | * linear and raid0 always use whatever space is available. We can only |
| 7152 | * consider changing this number if no resync or reconstruction is |
| 7153 | * 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] | 7154 | * 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] | 7155 | * of each device. If num_sectors is zero, we find the largest size |
| 7156 | * that fits. |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 7157 | */ |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 7158 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 7159 | mddev->sync_thread) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 7160 | return -EBUSY; |
NeilBrown | bd8839e | 2014-05-28 13:39:21 +1000 | [diff] [blame] | 7161 | if (mddev->ro) |
| 7162 | return -EROFS; |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 7163 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7164 | rdev_for_each(rdev, mddev) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7165 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 7166 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 7167 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 7168 | num_sectors = avail; |
| 7169 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 7170 | return -ENOSPC; |
| 7171 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 7172 | rv = mddev->pers->resize(mddev, num_sectors); |
Guoqing Jiang | c948363 | 2017-02-24 11:15:23 +0800 | [diff] [blame] | 7173 | if (!rv) { |
Guoqing Jiang | 818da59 | 2017-03-01 16:42:40 +0800 | [diff] [blame] | 7174 | if (mddev_is_clustered(mddev)) |
| 7175 | md_cluster_ops->update_size(mddev, old_dev_sectors); |
| 7176 | else if (mddev->queue) { |
Guoqing Jiang | c948363 | 2017-02-24 11:15:23 +0800 | [diff] [blame] | 7177 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 7178 | revalidate_disk(mddev->gendisk); |
| 7179 | } |
| 7180 | } |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 7181 | return rv; |
| 7182 | } |
| 7183 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7184 | static int update_raid_disks(struct mddev *mddev, int raid_disks) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 7185 | { |
| 7186 | int rv; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7187 | struct md_rdev *rdev; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 7188 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 7189 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 7190 | return -EINVAL; |
NeilBrown | bd8839e | 2014-05-28 13:39:21 +1000 | [diff] [blame] | 7191 | if (mddev->ro) |
| 7192 | return -EROFS; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 7193 | if (raid_disks <= 0 || |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 7194 | (mddev->max_disks && raid_disks >= mddev->max_disks)) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 7195 | return -EINVAL; |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 7196 | if (mddev->sync_thread || |
| 7197 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 7198 | mddev->reshape_position != MaxSector) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 7199 | return -EBUSY; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7200 | |
| 7201 | rdev_for_each(rdev, mddev) { |
| 7202 | if (mddev->raid_disks < raid_disks && |
| 7203 | rdev->data_offset < rdev->new_data_offset) |
| 7204 | return -EINVAL; |
| 7205 | if (mddev->raid_disks > raid_disks && |
| 7206 | rdev->data_offset > rdev->new_data_offset) |
| 7207 | return -EINVAL; |
| 7208 | } |
| 7209 | |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 7210 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7211 | if (mddev->delta_disks < 0) |
| 7212 | mddev->reshape_backwards = 1; |
| 7213 | else if (mddev->delta_disks > 0) |
| 7214 | mddev->reshape_backwards = 0; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 7215 | |
| 7216 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7217 | if (rv < 0) { |
NeilBrown | de171cb | 2011-01-31 11:57:42 +1100 | [diff] [blame] | 7218 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7219 | mddev->reshape_backwards = 0; |
| 7220 | } |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 7221 | return rv; |
| 7222 | } |
| 7223 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7224 | /* |
| 7225 | * update_array_info is used to change the configuration of an |
| 7226 | * on-line array. |
| 7227 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 7228 | * fields in the info are checked against the array. |
| 7229 | * Any differences that cannot be handled will cause an error. |
| 7230 | * Normally, only one change can be managed at a time. |
| 7231 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7232 | static int update_array_info(struct mddev *mddev, mdu_array_info_t *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7233 | { |
| 7234 | int rv = 0; |
| 7235 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7236 | int state = 0; |
| 7237 | |
| 7238 | /* calculate expected state,ignoring low bits */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 7239 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7240 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7241 | |
| 7242 | if (mddev->major_version != info->major_version || |
| 7243 | mddev->minor_version != info->minor_version || |
| 7244 | /* mddev->patch_version != info->patch_version || */ |
| 7245 | mddev->ctime != info->ctime || |
| 7246 | mddev->level != info->level || |
| 7247 | /* mddev->layout != info->layout || */ |
Firo Yang | 4e02361 | 2015-06-11 09:41:10 +0800 | [diff] [blame] | 7248 | mddev->persistent != !info->not_persistent || |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 7249 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7250 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 7251 | ((state^info->state) & 0xfffffe00) |
| 7252 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7253 | return -EINVAL; |
| 7254 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7255 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 7256 | cnt++; |
| 7257 | if (mddev->raid_disks != info->raid_disks) |
| 7258 | cnt++; |
| 7259 | if (mddev->layout != info->layout) |
| 7260 | cnt++; |
| 7261 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 7262 | cnt++; |
| 7263 | if (cnt == 0) |
| 7264 | return 0; |
| 7265 | if (cnt > 1) |
| 7266 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7267 | |
| 7268 | if (mddev->layout != info->layout) { |
| 7269 | /* Change layout |
| 7270 | * we don't need to do anything at the md level, the |
| 7271 | * personality will take care of it all. |
| 7272 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 7273 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7274 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 7275 | else { |
| 7276 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 7277 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 7278 | if (rv) |
| 7279 | mddev->new_layout = mddev->layout; |
| 7280 | return rv; |
| 7281 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7282 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7283 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 7284 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7285 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 7286 | if (mddev->raid_disks != info->raid_disks) |
| 7287 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7288 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7289 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 7290 | if (mddev->pers->quiesce == NULL || mddev->thread == NULL) { |
| 7291 | rv = -EINVAL; |
| 7292 | goto err; |
| 7293 | } |
| 7294 | if (mddev->recovery || mddev->sync_thread) { |
| 7295 | rv = -EBUSY; |
| 7296 | goto err; |
| 7297 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7298 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 7299 | struct bitmap *bitmap; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7300 | /* add the bitmap */ |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 7301 | if (mddev->bitmap) { |
| 7302 | rv = -EEXIST; |
| 7303 | goto err; |
| 7304 | } |
| 7305 | if (mddev->bitmap_info.default_offset == 0) { |
| 7306 | rv = -EINVAL; |
| 7307 | goto err; |
| 7308 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 7309 | mddev->bitmap_info.offset = |
| 7310 | mddev->bitmap_info.default_offset; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 7311 | mddev->bitmap_info.space = |
| 7312 | mddev->bitmap_info.default_space; |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 7313 | bitmap = md_bitmap_create(mddev, -1); |
NeilBrown | 9e1cc0a | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 7314 | mddev_suspend(mddev); |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 7315 | if (!IS_ERR(bitmap)) { |
| 7316 | mddev->bitmap = bitmap; |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 7317 | rv = md_bitmap_load(mddev); |
NeilBrown | ba599ac | 2015-02-25 11:44:11 +1100 | [diff] [blame] | 7318 | } else |
| 7319 | rv = PTR_ERR(bitmap); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7320 | if (rv) |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 7321 | md_bitmap_destroy(mddev); |
NeilBrown | 9e1cc0a | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 7322 | mddev_resume(mddev); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7323 | } else { |
| 7324 | /* remove the bitmap */ |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 7325 | if (!mddev->bitmap) { |
| 7326 | rv = -ENOENT; |
| 7327 | goto err; |
| 7328 | } |
| 7329 | if (mddev->bitmap->storage.file) { |
| 7330 | rv = -EINVAL; |
| 7331 | goto err; |
| 7332 | } |
Guoqing Jiang | f6a2dc6 | 2015-12-21 10:51:00 +1100 | [diff] [blame] | 7333 | if (mddev->bitmap_info.nodes) { |
| 7334 | /* hold PW on all the bitmap lock */ |
| 7335 | if (md_cluster_ops->lock_all_bitmaps(mddev) <= 0) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7336 | pr_warn("md: can't change bitmap to none since the array is in use by more than one node\n"); |
Guoqing Jiang | f6a2dc6 | 2015-12-21 10:51:00 +1100 | [diff] [blame] | 7337 | rv = -EPERM; |
| 7338 | md_cluster_ops->unlock_all_bitmaps(mddev); |
| 7339 | goto err; |
| 7340 | } |
| 7341 | |
| 7342 | mddev->bitmap_info.nodes = 0; |
| 7343 | md_cluster_ops->leave(mddev); |
| 7344 | } |
NeilBrown | 9e1cc0a | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 7345 | mddev_suspend(mddev); |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 7346 | md_bitmap_destroy(mddev); |
NeilBrown | 9e1cc0a | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 7347 | mddev_resume(mddev); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 7348 | mddev->bitmap_info.offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7349 | } |
| 7350 | } |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7351 | md_update_sb(mddev, 1); |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 7352 | return rv; |
| 7353 | err: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7354 | return rv; |
| 7355 | } |
| 7356 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7357 | static int set_disk_faulty(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7358 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7359 | struct md_rdev *rdev; |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 7360 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7361 | |
| 7362 | if (mddev->pers == NULL) |
| 7363 | return -ENODEV; |
| 7364 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 7365 | rcu_read_lock(); |
Tomasz Majchrzak | 1532d9e | 2017-12-27 10:31:40 +0100 | [diff] [blame] | 7366 | rdev = md_find_rdev_rcu(mddev, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7367 | if (!rdev) |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 7368 | err = -ENODEV; |
| 7369 | else { |
| 7370 | md_error(mddev, rdev); |
| 7371 | if (!test_bit(Faulty, &rdev->flags)) |
| 7372 | err = -EBUSY; |
| 7373 | } |
| 7374 | rcu_read_unlock(); |
| 7375 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7376 | } |
| 7377 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 7378 | /* |
| 7379 | * We have a problem here : there is no easy way to give a CHS |
| 7380 | * virtual geometry. We currently pretend that we have a 2 heads |
| 7381 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 7382 | * dosfs just mad... ;-) |
| 7383 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 7384 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 7385 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7386 | struct mddev *mddev = bdev->bd_disk->private_data; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 7387 | |
| 7388 | geo->heads = 2; |
| 7389 | geo->sectors = 4; |
NeilBrown | 49ce6ce | 2010-03-29 10:51:42 +1100 | [diff] [blame] | 7390 | geo->cylinders = mddev->array_sectors / 8; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 7391 | return 0; |
| 7392 | } |
| 7393 | |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 7394 | static inline bool md_ioctl_valid(unsigned int cmd) |
| 7395 | { |
| 7396 | switch (cmd) { |
| 7397 | case ADD_NEW_DISK: |
| 7398 | case BLKROSET: |
| 7399 | case GET_ARRAY_INFO: |
| 7400 | case GET_BITMAP_FILE: |
| 7401 | case GET_DISK_INFO: |
| 7402 | case HOT_ADD_DISK: |
| 7403 | case HOT_REMOVE_DISK: |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 7404 | case RAID_AUTORUN: |
| 7405 | case RAID_VERSION: |
| 7406 | case RESTART_ARRAY_RW: |
| 7407 | case RUN_ARRAY: |
| 7408 | case SET_ARRAY_INFO: |
| 7409 | case SET_BITMAP_FILE: |
| 7410 | case SET_DISK_FAULTY: |
| 7411 | case STOP_ARRAY: |
| 7412 | case STOP_ARRAY_RO: |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 7413 | case CLUSTERED_DISK_NACK: |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 7414 | return true; |
| 7415 | default: |
| 7416 | return false; |
| 7417 | } |
| 7418 | } |
| 7419 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 7420 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7421 | unsigned int cmd, unsigned long arg) |
| 7422 | { |
| 7423 | int err = 0; |
| 7424 | void __user *argp = (void __user *)arg; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7425 | struct mddev *mddev = NULL; |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 7426 | int ro; |
NeilBrown | 065e519 | 2017-04-06 11:16:33 +0800 | [diff] [blame] | 7427 | bool did_set_md_closing = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7428 | |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 7429 | if (!md_ioctl_valid(cmd)) |
| 7430 | return -ENOTTY; |
| 7431 | |
NeilBrown | 506c9e4 | 2011-12-23 10:17:26 +1100 | [diff] [blame] | 7432 | switch (cmd) { |
| 7433 | case RAID_VERSION: |
| 7434 | case GET_ARRAY_INFO: |
| 7435 | case GET_DISK_INFO: |
| 7436 | break; |
| 7437 | default: |
| 7438 | if (!capable(CAP_SYS_ADMIN)) |
| 7439 | return -EACCES; |
| 7440 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7441 | |
| 7442 | /* |
| 7443 | * Commands dealing with the RAID driver but not any |
| 7444 | * particular array: |
| 7445 | */ |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7446 | switch (cmd) { |
| 7447 | case RAID_VERSION: |
| 7448 | err = get_version(argp); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7449 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7450 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7451 | #ifndef MODULE |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7452 | case RAID_AUTORUN: |
| 7453 | err = 0; |
| 7454 | autostart_arrays(arg); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7455 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7456 | #endif |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7457 | default:; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7458 | } |
| 7459 | |
| 7460 | /* |
| 7461 | * Commands creating/starting a new array: |
| 7462 | */ |
| 7463 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 7464 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7465 | |
| 7466 | if (!mddev) { |
| 7467 | BUG(); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7468 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7469 | } |
| 7470 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 7471 | /* Some actions do not requires the mutex */ |
| 7472 | switch (cmd) { |
| 7473 | case GET_ARRAY_INFO: |
| 7474 | if (!mddev->raid_disks && !mddev->external) |
| 7475 | err = -ENODEV; |
| 7476 | else |
| 7477 | err = get_array_info(mddev, argp); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7478 | goto out; |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 7479 | |
| 7480 | case GET_DISK_INFO: |
| 7481 | if (!mddev->raid_disks && !mddev->external) |
| 7482 | err = -ENODEV; |
| 7483 | else |
| 7484 | err = get_disk_info(mddev, argp); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7485 | goto out; |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 7486 | |
| 7487 | case SET_DISK_FAULTY: |
| 7488 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7489 | goto out; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 7490 | |
| 7491 | case GET_BITMAP_FILE: |
| 7492 | err = get_bitmap_file(mddev, argp); |
| 7493 | goto out; |
| 7494 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 7495 | } |
| 7496 | |
NeilBrown | a7a3f08 | 2012-12-11 13:35:54 +1100 | [diff] [blame] | 7497 | if (cmd == ADD_NEW_DISK) |
| 7498 | /* need to ensure md_delayed_delete() has completed */ |
| 7499 | flush_workqueue(md_misc_wq); |
| 7500 | |
Hannes Reinecke | 90f5f7a | 2013-04-02 08:38:55 +0200 | [diff] [blame] | 7501 | if (cmd == HOT_REMOVE_DISK) |
| 7502 | /* need to ensure recovery thread has run */ |
| 7503 | wait_event_interruptible_timeout(mddev->sb_wait, |
| 7504 | !test_bit(MD_RECOVERY_NEEDED, |
Shaohua Li | 82a301c | 2016-12-08 15:48:18 -0800 | [diff] [blame] | 7505 | &mddev->recovery), |
Hannes Reinecke | 90f5f7a | 2013-04-02 08:38:55 +0200 | [diff] [blame] | 7506 | msecs_to_jiffies(5000)); |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 7507 | if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) { |
| 7508 | /* Need to flush page cache, and ensure no-one else opens |
| 7509 | * and writes |
| 7510 | */ |
| 7511 | mutex_lock(&mddev->open_mutex); |
NeilBrown | 9ba3b7f | 2014-09-09 14:00:15 +1000 | [diff] [blame] | 7512 | if (mddev->pers && atomic_read(&mddev->openers) > 1) { |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 7513 | mutex_unlock(&mddev->open_mutex); |
| 7514 | err = -EBUSY; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7515 | goto out; |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 7516 | } |
NeilBrown | 065e519 | 2017-04-06 11:16:33 +0800 | [diff] [blame] | 7517 | WARN_ON_ONCE(test_bit(MD_CLOSING, &mddev->flags)); |
Guoqing Jiang | af8d8e6 | 2016-08-12 13:42:37 +0800 | [diff] [blame] | 7518 | set_bit(MD_CLOSING, &mddev->flags); |
NeilBrown | 065e519 | 2017-04-06 11:16:33 +0800 | [diff] [blame] | 7519 | did_set_md_closing = true; |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 7520 | mutex_unlock(&mddev->open_mutex); |
| 7521 | sync_blockdev(bdev); |
| 7522 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7523 | err = mddev_lock(mddev); |
| 7524 | if (err) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7525 | pr_debug("md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 7526 | err, cmd); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7527 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7528 | } |
| 7529 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7530 | if (cmd == SET_ARRAY_INFO) { |
| 7531 | mdu_array_info_t info; |
| 7532 | if (!arg) |
| 7533 | memset(&info, 0, sizeof(info)); |
| 7534 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 7535 | err = -EFAULT; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7536 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7537 | } |
| 7538 | if (mddev->pers) { |
| 7539 | err = update_array_info(mddev, &info); |
| 7540 | if (err) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7541 | pr_warn("md: couldn't update array info. %d\n", err); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7542 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7543 | } |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7544 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7545 | } |
| 7546 | if (!list_empty(&mddev->disks)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7547 | pr_warn("md: array %s already has disks!\n", mdname(mddev)); |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7548 | err = -EBUSY; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7549 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7550 | } |
| 7551 | if (mddev->raid_disks) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7552 | pr_warn("md: array %s already initialised!\n", mdname(mddev)); |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7553 | err = -EBUSY; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7554 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7555 | } |
| 7556 | err = set_array_info(mddev, &info); |
| 7557 | if (err) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7558 | pr_warn("md: couldn't set array info. %d\n", err); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7559 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7560 | } |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7561 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7562 | } |
| 7563 | |
| 7564 | /* |
| 7565 | * Commands querying/configuring an existing array: |
| 7566 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7567 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 7568 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 7569 | if ((!mddev->raid_disks && !mddev->external) |
| 7570 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 7571 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 7572 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7573 | err = -ENODEV; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7574 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7575 | } |
| 7576 | |
| 7577 | /* |
| 7578 | * Commands even a read-only array can execute: |
| 7579 | */ |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7580 | switch (cmd) { |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7581 | case RESTART_ARRAY_RW: |
| 7582 | err = restart_array(mddev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7583 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7584 | |
| 7585 | case STOP_ARRAY: |
| 7586 | err = do_md_stop(mddev, 0, bdev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7587 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7588 | |
| 7589 | case STOP_ARRAY_RO: |
| 7590 | err = md_set_readonly(mddev, bdev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7591 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7592 | |
NeilBrown | 3ea8929d | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 7593 | case HOT_REMOVE_DISK: |
| 7594 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7595 | goto unlock; |
NeilBrown | 3ea8929d | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 7596 | |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 7597 | case ADD_NEW_DISK: |
| 7598 | /* We can support ADD_NEW_DISK on read-only arrays |
Wei Fang | 466ad29 | 2016-03-21 19:19:30 +0800 | [diff] [blame] | 7599 | * only if we are re-adding a preexisting device. |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 7600 | * So require mddev->pers and MD_DISK_SYNC. |
| 7601 | */ |
| 7602 | if (mddev->pers) { |
| 7603 | mdu_disk_info_t info; |
| 7604 | if (copy_from_user(&info, argp, sizeof(info))) |
| 7605 | err = -EFAULT; |
| 7606 | else if (!(info.state & (1<<MD_DISK_SYNC))) |
| 7607 | /* Need to clear read-only for this */ |
| 7608 | break; |
| 7609 | else |
| 7610 | err = add_new_disk(mddev, &info); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7611 | goto unlock; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 7612 | } |
| 7613 | break; |
| 7614 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7615 | case BLKROSET: |
| 7616 | if (get_user(ro, (int __user *)(arg))) { |
| 7617 | err = -EFAULT; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7618 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7619 | } |
| 7620 | err = -EINVAL; |
| 7621 | |
| 7622 | /* if the bdev is going readonly the value of mddev->ro |
| 7623 | * does not matter, no writes are coming |
| 7624 | */ |
| 7625 | if (ro) |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7626 | goto unlock; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7627 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7628 | /* are we are already prepared for writes? */ |
| 7629 | if (mddev->ro != 1) |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7630 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7631 | |
| 7632 | /* transitioning to readauto need only happen for |
| 7633 | * arrays that call md_write_start |
| 7634 | */ |
| 7635 | if (mddev->pers) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7636 | err = restart_array(mddev); |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7637 | if (err == 0) { |
| 7638 | mddev->ro = 2; |
| 7639 | set_disk_ro(mddev->gendisk, 0); |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 7640 | } |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7641 | } |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7642 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7643 | } |
| 7644 | |
| 7645 | /* |
| 7646 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7647 | * superblock, so we do not allow them on read-only arrays. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7648 | */ |
NeilBrown | 326eb17 | 2014-09-30 15:36:28 +1000 | [diff] [blame] | 7649 | if (mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7650 | if (mddev->ro == 2) { |
| 7651 | mddev->ro = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7652 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7653 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | f3378b4 | 2013-02-28 11:59:03 +1100 | [diff] [blame] | 7654 | /* mddev_unlock will wake thread */ |
| 7655 | /* If a device failed while we were read-only, we |
| 7656 | * need to make sure the metadata is updated now. |
| 7657 | */ |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 7658 | if (test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)) { |
NeilBrown | f3378b4 | 2013-02-28 11:59:03 +1100 | [diff] [blame] | 7659 | mddev_unlock(mddev); |
| 7660 | wait_event(mddev->sb_wait, |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 7661 | !test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags) && |
| 7662 | !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)); |
NeilBrown | 29f097c | 2013-11-14 17:54:51 +1100 | [diff] [blame] | 7663 | mddev_lock_nointr(mddev); |
NeilBrown | f3378b4 | 2013-02-28 11:59:03 +1100 | [diff] [blame] | 7664 | } |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7665 | } else { |
| 7666 | err = -EROFS; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7667 | goto unlock; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7668 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7669 | } |
| 7670 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7671 | switch (cmd) { |
| 7672 | case ADD_NEW_DISK: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7673 | { |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7674 | mdu_disk_info_t info; |
| 7675 | if (copy_from_user(&info, argp, sizeof(info))) |
| 7676 | err = -EFAULT; |
| 7677 | else |
| 7678 | err = add_new_disk(mddev, &info); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7679 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7680 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7681 | |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 7682 | case CLUSTERED_DISK_NACK: |
| 7683 | if (mddev_is_clustered(mddev)) |
| 7684 | md_cluster_ops->new_disk_ack(mddev, false); |
| 7685 | else |
| 7686 | err = -EINVAL; |
| 7687 | goto unlock; |
| 7688 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7689 | case HOT_ADD_DISK: |
| 7690 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7691 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7692 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7693 | case RUN_ARRAY: |
| 7694 | err = do_md_run(mddev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7695 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7696 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7697 | case SET_BITMAP_FILE: |
| 7698 | err = set_bitmap_file(mddev, (int)arg); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7699 | goto unlock; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7700 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7701 | default: |
| 7702 | err = -EINVAL; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7703 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7704 | } |
| 7705 | |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7706 | unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7707 | if (mddev->hold_active == UNTIL_IOCTL && |
| 7708 | err != -EINVAL) |
| 7709 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7710 | mddev_unlock(mddev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7711 | out: |
NeilBrown | 065e519 | 2017-04-06 11:16:33 +0800 | [diff] [blame] | 7712 | if(did_set_md_closing) |
| 7713 | clear_bit(MD_CLOSING, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7714 | return err; |
| 7715 | } |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 7716 | #ifdef CONFIG_COMPAT |
| 7717 | static int md_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 7718 | unsigned int cmd, unsigned long arg) |
| 7719 | { |
| 7720 | switch (cmd) { |
| 7721 | case HOT_REMOVE_DISK: |
| 7722 | case HOT_ADD_DISK: |
| 7723 | case SET_DISK_FAULTY: |
| 7724 | case SET_BITMAP_FILE: |
| 7725 | /* These take in integer arg, do not convert */ |
| 7726 | break; |
| 7727 | default: |
| 7728 | arg = (unsigned long)compat_ptr(arg); |
| 7729 | break; |
| 7730 | } |
| 7731 | |
| 7732 | return md_ioctl(bdev, mode, cmd, arg); |
| 7733 | } |
| 7734 | #endif /* CONFIG_COMPAT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7735 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 7736 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7737 | { |
| 7738 | /* |
| 7739 | * Succeed if we can lock the mddev, which confirms that |
| 7740 | * it isn't being stopped right now. |
| 7741 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7742 | struct mddev *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7743 | int err; |
| 7744 | |
Yuanhan Liu | 0c09822 | 2012-05-22 13:55:32 +1000 | [diff] [blame] | 7745 | if (!mddev) |
| 7746 | return -ENODEV; |
| 7747 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7748 | if (mddev->gendisk != bdev->bd_disk) { |
| 7749 | /* we are racing with mddev_put which is discarding this |
| 7750 | * bd_disk. |
| 7751 | */ |
| 7752 | mddev_put(mddev); |
| 7753 | /* Wait until bdev->bd_disk is definitely gone */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 7754 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7755 | /* Then retry the open from the top */ |
| 7756 | return -ERESTARTSYS; |
| 7757 | } |
| 7758 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 7759 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 7760 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7761 | goto out; |
| 7762 | |
Guoqing Jiang | af8d8e6 | 2016-08-12 13:42:37 +0800 | [diff] [blame] | 7763 | if (test_bit(MD_CLOSING, &mddev->flags)) { |
| 7764 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | e2342ca | 2016-12-05 16:40:50 +1100 | [diff] [blame] | 7765 | err = -ENODEV; |
| 7766 | goto out; |
Guoqing Jiang | af8d8e6 | 2016-08-12 13:42:37 +0800 | [diff] [blame] | 7767 | } |
| 7768 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7769 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7770 | atomic_inc(&mddev->openers); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 7771 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7772 | |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7773 | check_disk_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7774 | out: |
NeilBrown | e2342ca | 2016-12-05 16:40:50 +1100 | [diff] [blame] | 7775 | if (err) |
| 7776 | mddev_put(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7777 | return err; |
| 7778 | } |
| 7779 | |
Al Viro | db2a144 | 2013-05-05 21:52:57 -0400 | [diff] [blame] | 7780 | static void md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7781 | { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7782 | struct mddev *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7783 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 7784 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7785 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7786 | mddev_put(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7787 | } |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7788 | |
| 7789 | static int md_media_changed(struct gendisk *disk) |
| 7790 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7791 | struct mddev *mddev = disk->private_data; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7792 | |
| 7793 | return mddev->changed; |
| 7794 | } |
| 7795 | |
| 7796 | static int md_revalidate(struct gendisk *disk) |
| 7797 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7798 | struct mddev *mddev = disk->private_data; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7799 | |
| 7800 | mddev->changed = 0; |
| 7801 | return 0; |
| 7802 | } |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 7803 | static const struct block_device_operations md_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7804 | { |
| 7805 | .owner = THIS_MODULE, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 7806 | .open = md_open, |
| 7807 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 7808 | .ioctl = md_ioctl, |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 7809 | #ifdef CONFIG_COMPAT |
| 7810 | .compat_ioctl = md_compat_ioctl, |
| 7811 | #endif |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 7812 | .getgeo = md_getgeo, |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7813 | .media_changed = md_media_changed, |
| 7814 | .revalidate_disk= md_revalidate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7815 | }; |
| 7816 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7817 | static int md_thread(void *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7818 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7819 | struct md_thread *thread = arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7820 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7821 | /* |
| 7822 | * md_thread is a 'system-thread', it's priority should be very |
| 7823 | * high. We avoid resource deadlocks individually in each |
| 7824 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 7825 | * the very same RT priority as kswapd, thus we will never get |
| 7826 | * into a priority inversion deadlock. |
| 7827 | * |
| 7828 | * we definitely have to have equal or higher priority than |
| 7829 | * bdflush, otherwise bdflush will deadlock if there are too |
| 7830 | * many dirty RAID5 blocks. |
| 7831 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7832 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 7833 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 7834 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7835 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 7836 | /* We need to wait INTERRUPTIBLE so that |
| 7837 | * we don't add to the load-average. |
| 7838 | * That means we need to be sure no signals are |
| 7839 | * pending |
| 7840 | */ |
| 7841 | if (signal_pending(current)) |
| 7842 | flush_signals(current); |
| 7843 | |
| 7844 | wait_event_interruptible_timeout |
| 7845 | (thread->wqueue, |
| 7846 | test_bit(THREAD_WAKEUP, &thread->flags) |
Shaohua Li | ce1ccd0 | 2016-11-21 10:29:18 -0800 | [diff] [blame] | 7847 | || kthread_should_stop() || kthread_should_park(), |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 7848 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7849 | |
NeilBrown | 6c98791 | 2011-01-14 09:13:53 +1100 | [diff] [blame] | 7850 | clear_bit(THREAD_WAKEUP, &thread->flags); |
Shaohua Li | ce1ccd0 | 2016-11-21 10:29:18 -0800 | [diff] [blame] | 7851 | if (kthread_should_park()) |
| 7852 | kthread_parkme(); |
NeilBrown | 6c98791 | 2011-01-14 09:13:53 +1100 | [diff] [blame] | 7853 | if (!kthread_should_stop()) |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 7854 | thread->run(thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7855 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 7856 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7857 | return 0; |
| 7858 | } |
| 7859 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7860 | void md_wakeup_thread(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7861 | { |
| 7862 | if (thread) { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 7863 | pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm); |
Guoqing Jiang | d1d9014 | 2017-10-09 10:32:48 +0800 | [diff] [blame] | 7864 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 7865 | wake_up(&thread->wqueue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7866 | } |
| 7867 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7868 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7869 | |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 7870 | struct md_thread *md_register_thread(void (*run) (struct md_thread *), |
| 7871 | struct mddev *mddev, const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7872 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7873 | struct md_thread *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7874 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7875 | thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7876 | if (!thread) |
| 7877 | return NULL; |
| 7878 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7879 | init_waitqueue_head(&thread->wqueue); |
| 7880 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7881 | thread->run = run; |
| 7882 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7883 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 7884 | thread->tsk = kthread_run(md_thread, thread, |
| 7885 | "%s_%s", |
| 7886 | mdname(thread->mddev), |
NeilBrown | 0232605 | 2012-07-03 15:56:52 +1000 | [diff] [blame] | 7887 | name); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 7888 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7889 | kfree(thread); |
| 7890 | return NULL; |
| 7891 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7892 | return thread; |
| 7893 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7894 | EXPORT_SYMBOL(md_register_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7895 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7896 | void md_unregister_thread(struct md_thread **threadp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7897 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7898 | struct md_thread *thread = *threadp; |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7899 | if (!thread) |
| 7900 | return; |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 7901 | pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 7902 | /* Locking ensures that mddev_unlock does not wake_up a |
| 7903 | * non-existent thread |
| 7904 | */ |
| 7905 | spin_lock(&pers_lock); |
| 7906 | *threadp = NULL; |
| 7907 | spin_unlock(&pers_lock); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 7908 | |
| 7909 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7910 | kfree(thread); |
| 7911 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7912 | EXPORT_SYMBOL(md_unregister_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7913 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7914 | void md_error(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7915 | { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7916 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7917 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7918 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 7919 | if (!mddev->pers || !mddev->pers->error_handler) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7920 | return; |
| 7921 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7922 | if (mddev->degraded) |
| 7923 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7924 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7925 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7926 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7927 | md_wakeup_thread(mddev->thread); |
NeilBrown | 768a418 | 2010-07-26 11:49:55 +1000 | [diff] [blame] | 7928 | if (mddev->event_work.func) |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 7929 | queue_work(md_misc_wq, &mddev->event_work); |
Guoqing Jiang | bb9ef71 | 2015-12-28 10:46:38 +0800 | [diff] [blame] | 7930 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7931 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7932 | EXPORT_SYMBOL(md_error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7933 | |
| 7934 | /* seq_file implementation /proc/mdstat */ |
| 7935 | |
| 7936 | static void status_unused(struct seq_file *seq) |
| 7937 | { |
| 7938 | int i = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7939 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7940 | |
| 7941 | seq_printf(seq, "unused devices: "); |
| 7942 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 7943 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7944 | char b[BDEVNAME_SIZE]; |
| 7945 | i++; |
| 7946 | seq_printf(seq, "%s ", |
| 7947 | bdevname(rdev->bdev,b)); |
| 7948 | } |
| 7949 | if (!i) |
| 7950 | seq_printf(seq, "<none>"); |
| 7951 | |
| 7952 | seq_printf(seq, "\n"); |
| 7953 | } |
| 7954 | |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 7955 | static int status_resync(struct seq_file *seq, struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7956 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7957 | sector_t max_sectors, resync, res; |
Mariusz Tkaczyk | 9642fa7 | 2019-06-13 16:11:41 +0200 | [diff] [blame] | 7958 | unsigned long dt, db = 0; |
| 7959 | sector_t rt, curr_mark_cnt, resync_mark_cnt; |
| 7960 | int scale, recovery_active; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7961 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7962 | |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 7963 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 7964 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7965 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7966 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7967 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7968 | |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 7969 | resync = mddev->curr_resync; |
| 7970 | if (resync <= 3) { |
| 7971 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 7972 | /* Still cleaning up */ |
| 7973 | resync = max_sectors; |
Nate Dailey | d2e2ec8 | 2017-11-30 11:33:30 -0500 | [diff] [blame] | 7974 | } else if (resync > max_sectors) |
| 7975 | resync = max_sectors; |
| 7976 | else |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 7977 | resync -= atomic_read(&mddev->recovery_active); |
| 7978 | |
| 7979 | if (resync == 0) { |
Guoqing Jiang | 0357ba2 | 2018-07-02 16:26:25 +0800 | [diff] [blame] | 7980 | if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery)) { |
| 7981 | struct md_rdev *rdev; |
| 7982 | |
| 7983 | rdev_for_each(rdev, mddev) |
| 7984 | if (rdev->raid_disk >= 0 && |
| 7985 | !test_bit(Faulty, &rdev->flags) && |
| 7986 | rdev->recovery_offset != MaxSector && |
| 7987 | rdev->recovery_offset) { |
| 7988 | seq_printf(seq, "\trecover=REMOTE"); |
| 7989 | return 1; |
| 7990 | } |
| 7991 | if (mddev->reshape_position != MaxSector) |
| 7992 | seq_printf(seq, "\treshape=REMOTE"); |
| 7993 | else |
| 7994 | seq_printf(seq, "\tresync=REMOTE"); |
| 7995 | return 1; |
| 7996 | } |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 7997 | if (mddev->recovery_cp < MaxSector) { |
| 7998 | seq_printf(seq, "\tresync=PENDING"); |
| 7999 | return 1; |
| 8000 | } |
| 8001 | return 0; |
| 8002 | } |
| 8003 | if (resync < 3) { |
| 8004 | seq_printf(seq, "\tresync=DELAYED"); |
| 8005 | return 1; |
| 8006 | } |
| 8007 | |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 8008 | WARN_ON(max_sectors == 0); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 8009 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 8010 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 8011 | * u32, as those are the requirements for sector_div. |
| 8012 | * Thus 'scale' must be at least 10 |
| 8013 | */ |
| 8014 | scale = 10; |
| 8015 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 8016 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 8017 | scale++; |
| 8018 | } |
| 8019 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 8020 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 8021 | |
| 8022 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8023 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 8024 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8025 | seq_printf(seq, "["); |
| 8026 | for (i = 0; i < x; i++) |
| 8027 | seq_printf(seq, "="); |
| 8028 | seq_printf(seq, ">"); |
| 8029 | for (i = 0; i < y; i++) |
| 8030 | seq_printf(seq, "."); |
| 8031 | seq_printf(seq, "] "); |
| 8032 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 8033 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 8034 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 8035 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 8036 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 8037 | "check" : |
| 8038 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 8039 | "resync" : "recovery"))), |
| 8040 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 8041 | (unsigned long long) resync/2, |
| 8042 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8043 | |
| 8044 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8045 | * dt: time from mark until now |
| 8046 | * db: blocks written from mark until now |
| 8047 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 8048 | * |
Mariusz Tkaczyk | 9642fa7 | 2019-06-13 16:11:41 +0200 | [diff] [blame] | 8049 | * rt is a sector_t, which is always 64bit now. We are keeping |
| 8050 | * the original algorithm, but it is not really necessary. |
| 8051 | * |
| 8052 | * Original algorithm: |
| 8053 | * So we divide before multiply in case it is 32bit and close |
| 8054 | * to the limit. |
| 8055 | * We scale the divisor (db) by 32 to avoid losing precision |
| 8056 | * near the end of resync when the number of remaining sectors |
| 8057 | * is close to 'db'. |
| 8058 | * We then divide rt by 32 after multiplying by db to compensate. |
| 8059 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8060 | */ |
| 8061 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 8062 | if (!dt) dt++; |
Mariusz Tkaczyk | 9642fa7 | 2019-06-13 16:11:41 +0200 | [diff] [blame] | 8063 | |
| 8064 | curr_mark_cnt = mddev->curr_mark_cnt; |
| 8065 | recovery_active = atomic_read(&mddev->recovery_active); |
| 8066 | resync_mark_cnt = mddev->resync_mark_cnt; |
| 8067 | |
| 8068 | if (curr_mark_cnt >= (recovery_active + resync_mark_cnt)) |
| 8069 | db = curr_mark_cnt - (recovery_active + resync_mark_cnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8070 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 8071 | rt = max_sectors - resync; /* number of remaining sectors */ |
Mariusz Tkaczyk | 9642fa7 | 2019-06-13 16:11:41 +0200 | [diff] [blame] | 8072 | rt = div64_u64(rt, db/32+1); |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 8073 | rt *= dt; |
| 8074 | rt >>= 5; |
| 8075 | |
| 8076 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 8077 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8078 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 8079 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 8080 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8081 | } |
| 8082 | |
| 8083 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 8084 | { |
| 8085 | struct list_head *tmp; |
| 8086 | loff_t l = *pos; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8087 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8088 | |
| 8089 | if (l >= 0x10000) |
| 8090 | return NULL; |
| 8091 | if (!l--) |
| 8092 | /* header */ |
| 8093 | return (void*)1; |
| 8094 | |
| 8095 | spin_lock(&all_mddevs_lock); |
| 8096 | list_for_each(tmp,&all_mddevs) |
| 8097 | if (!l--) { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8098 | mddev = list_entry(tmp, struct mddev, all_mddevs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8099 | mddev_get(mddev); |
| 8100 | spin_unlock(&all_mddevs_lock); |
| 8101 | return mddev; |
| 8102 | } |
| 8103 | spin_unlock(&all_mddevs_lock); |
| 8104 | if (!l--) |
| 8105 | return (void*)2;/* tail */ |
| 8106 | return NULL; |
| 8107 | } |
| 8108 | |
| 8109 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 8110 | { |
| 8111 | struct list_head *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8112 | struct mddev *next_mddev, *mddev = v; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 8113 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8114 | ++*pos; |
| 8115 | if (v == (void*)2) |
| 8116 | return NULL; |
| 8117 | |
| 8118 | spin_lock(&all_mddevs_lock); |
| 8119 | if (v == (void*)1) |
| 8120 | tmp = all_mddevs.next; |
| 8121 | else |
| 8122 | tmp = mddev->all_mddevs.next; |
| 8123 | if (tmp != &all_mddevs) |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8124 | next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8125 | else { |
| 8126 | next_mddev = (void*)2; |
| 8127 | *pos = 0x10000; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 8128 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8129 | spin_unlock(&all_mddevs_lock); |
| 8130 | |
| 8131 | if (v != (void*)1) |
| 8132 | mddev_put(mddev); |
| 8133 | return next_mddev; |
| 8134 | |
| 8135 | } |
| 8136 | |
| 8137 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 8138 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8139 | struct mddev *mddev = v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8140 | |
| 8141 | if (mddev && v != (void*)1 && v != (void*)2) |
| 8142 | mddev_put(mddev); |
| 8143 | } |
| 8144 | |
| 8145 | static int md_seq_show(struct seq_file *seq, void *v) |
| 8146 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8147 | struct mddev *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8148 | sector_t sectors; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8149 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8150 | |
| 8151 | if (v == (void*)1) { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 8152 | struct md_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8153 | seq_printf(seq, "Personalities : "); |
| 8154 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8155 | list_for_each_entry(pers, &pers_list, list) |
| 8156 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8157 | |
| 8158 | spin_unlock(&pers_lock); |
| 8159 | seq_printf(seq, "\n"); |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 8160 | seq->poll_event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8161 | return 0; |
| 8162 | } |
| 8163 | if (v == (void*)2) { |
| 8164 | status_unused(seq); |
| 8165 | return 0; |
| 8166 | } |
| 8167 | |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 8168 | spin_lock(&mddev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8169 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 8170 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 8171 | mddev->pers ? "" : "in"); |
| 8172 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8173 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8174 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8175 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 8176 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8177 | seq_printf(seq, " %s", mddev->pers->name); |
| 8178 | } |
| 8179 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8180 | sectors = 0; |
NeilBrown | f97fcad | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 8181 | rcu_read_lock(); |
| 8182 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8183 | char b[BDEVNAME_SIZE]; |
| 8184 | seq_printf(seq, " %s[%d]", |
| 8185 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 8186 | if (test_bit(WriteMostly, &rdev->flags)) |
| 8187 | seq_printf(seq, "(W)"); |
Shaohua Li | 9efdca1 | 2015-10-12 16:59:50 -0700 | [diff] [blame] | 8188 | if (test_bit(Journal, &rdev->flags)) |
| 8189 | seq_printf(seq, "(J)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 8190 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8191 | seq_printf(seq, "(F)"); |
| 8192 | continue; |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 8193 | } |
| 8194 | if (rdev->raid_disk < 0) |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 8195 | seq_printf(seq, "(S)"); /* spare */ |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 8196 | if (test_bit(Replacement, &rdev->flags)) |
| 8197 | seq_printf(seq, "(R)"); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8198 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8199 | } |
NeilBrown | f97fcad | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 8200 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8201 | |
| 8202 | if (!list_empty(&mddev->disks)) { |
| 8203 | if (mddev->pers) |
| 8204 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 8205 | (unsigned long long) |
| 8206 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8207 | else |
| 8208 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8209 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8210 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 8211 | if (mddev->persistent) { |
| 8212 | if (mddev->major_version != 0 || |
| 8213 | mddev->minor_version != 90) { |
| 8214 | seq_printf(seq," super %d.%d", |
| 8215 | mddev->major_version, |
| 8216 | mddev->minor_version); |
| 8217 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 8218 | } else if (mddev->external) |
| 8219 | seq_printf(seq, " super external:%s", |
| 8220 | mddev->metadata_type); |
| 8221 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 8222 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8223 | |
| 8224 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 8225 | mddev->pers->status(seq, mddev); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 8226 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 8227 | if (mddev->pers->sync_request) { |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 8228 | if (status_resync(seq, mddev)) |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 8229 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 8230 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 8231 | } else |
| 8232 | seq_printf(seq, "\n "); |
| 8233 | |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 8234 | md_bitmap_status(seq, mddev->bitmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8235 | |
| 8236 | seq_printf(seq, "\n"); |
| 8237 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 8238 | spin_unlock(&mddev->lock); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 8239 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8240 | return 0; |
| 8241 | } |
| 8242 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 8243 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8244 | .start = md_seq_start, |
| 8245 | .next = md_seq_next, |
| 8246 | .stop = md_seq_stop, |
| 8247 | .show = md_seq_show, |
| 8248 | }; |
| 8249 | |
| 8250 | static int md_seq_open(struct inode *inode, struct file *file) |
| 8251 | { |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 8252 | struct seq_file *seq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8253 | int error; |
| 8254 | |
| 8255 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 8256 | if (error) |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 8257 | return error; |
| 8258 | |
| 8259 | seq = file->private_data; |
| 8260 | seq->poll_event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8261 | return error; |
| 8262 | } |
| 8263 | |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 8264 | static int md_unloading; |
Al Viro | afc9a42 | 2017-07-03 06:39:46 -0400 | [diff] [blame] | 8265 | static __poll_t mdstat_poll(struct file *filp, poll_table *wait) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 8266 | { |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 8267 | struct seq_file *seq = filp->private_data; |
Al Viro | afc9a42 | 2017-07-03 06:39:46 -0400 | [diff] [blame] | 8268 | __poll_t mask; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 8269 | |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 8270 | if (md_unloading) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 8271 | return EPOLLIN|EPOLLRDNORM|EPOLLERR|EPOLLPRI; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 8272 | poll_wait(filp, &md_event_waiters, wait); |
| 8273 | |
| 8274 | /* always allow read */ |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 8275 | mask = EPOLLIN | EPOLLRDNORM; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 8276 | |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 8277 | if (seq->poll_event != atomic_read(&md_event_count)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 8278 | mask |= EPOLLERR | EPOLLPRI; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 8279 | return mask; |
| 8280 | } |
| 8281 | |
Alexey Dobriyan | 97a3253 | 2020-02-03 17:37:17 -0800 | [diff] [blame] | 8282 | static const struct proc_ops mdstat_proc_ops = { |
| 8283 | .proc_open = md_seq_open, |
| 8284 | .proc_read = seq_read, |
| 8285 | .proc_lseek = seq_lseek, |
| 8286 | .proc_release = seq_release, |
| 8287 | .proc_poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8288 | }; |
| 8289 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 8290 | int register_md_personality(struct md_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8291 | { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8292 | pr_debug("md: %s personality registered for level %d\n", |
| 8293 | p->name, p->level); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8294 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8295 | list_add_tail(&p->list, &pers_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8296 | spin_unlock(&pers_lock); |
| 8297 | return 0; |
| 8298 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 8299 | EXPORT_SYMBOL(register_md_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8300 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 8301 | int unregister_md_personality(struct md_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8302 | { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8303 | pr_debug("md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8304 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8305 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8306 | spin_unlock(&pers_lock); |
| 8307 | return 0; |
| 8308 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 8309 | EXPORT_SYMBOL(unregister_md_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8310 | |
NeilBrown | 6022e75 | 2015-08-13 12:32:55 +1000 | [diff] [blame] | 8311 | int register_md_cluster_operations(struct md_cluster_operations *ops, |
| 8312 | struct module *module) |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 8313 | { |
NeilBrown | 6022e75 | 2015-08-13 12:32:55 +1000 | [diff] [blame] | 8314 | int ret = 0; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 8315 | spin_lock(&pers_lock); |
NeilBrown | 6022e75 | 2015-08-13 12:32:55 +1000 | [diff] [blame] | 8316 | if (md_cluster_ops != NULL) |
| 8317 | ret = -EALREADY; |
| 8318 | else { |
| 8319 | md_cluster_ops = ops; |
| 8320 | md_cluster_mod = module; |
| 8321 | } |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 8322 | spin_unlock(&pers_lock); |
NeilBrown | 6022e75 | 2015-08-13 12:32:55 +1000 | [diff] [blame] | 8323 | return ret; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 8324 | } |
| 8325 | EXPORT_SYMBOL(register_md_cluster_operations); |
| 8326 | |
| 8327 | int unregister_md_cluster_operations(void) |
| 8328 | { |
| 8329 | spin_lock(&pers_lock); |
| 8330 | md_cluster_ops = NULL; |
| 8331 | spin_unlock(&pers_lock); |
| 8332 | return 0; |
| 8333 | } |
| 8334 | EXPORT_SYMBOL(unregister_md_cluster_operations); |
| 8335 | |
| 8336 | int md_setup_cluster(struct mddev *mddev, int nodes) |
| 8337 | { |
Guoqing Jiang | 47a7b0d | 2016-09-04 22:17:28 -0400 | [diff] [blame] | 8338 | if (!md_cluster_ops) |
| 8339 | request_module("md-cluster"); |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 8340 | spin_lock(&pers_lock); |
Guoqing Jiang | 47a7b0d | 2016-09-04 22:17:28 -0400 | [diff] [blame] | 8341 | /* ensure module won't be unloaded */ |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 8342 | if (!md_cluster_ops || !try_module_get(md_cluster_mod)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8343 | pr_warn("can't find md-cluster module or get it's reference.\n"); |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 8344 | spin_unlock(&pers_lock); |
| 8345 | return -ENOENT; |
| 8346 | } |
| 8347 | spin_unlock(&pers_lock); |
| 8348 | |
Goldwyn Rodrigues | cf921cc | 2014-03-30 00:42:49 -0500 | [diff] [blame] | 8349 | return md_cluster_ops->join(mddev, nodes); |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 8350 | } |
| 8351 | |
| 8352 | void md_cluster_stop(struct mddev *mddev) |
| 8353 | { |
Goldwyn Rodrigues | c4ce867 | 2014-03-29 10:20:02 -0500 | [diff] [blame] | 8354 | if (!md_cluster_ops) |
| 8355 | return; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 8356 | md_cluster_ops->leave(mddev); |
| 8357 | module_put(md_cluster_mod); |
| 8358 | } |
| 8359 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8360 | static int is_mddev_idle(struct mddev *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8361 | { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 8362 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8363 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8364 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8365 | |
| 8366 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 8367 | rcu_read_lock(); |
| 8368 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8369 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
Michael Callahan | 59767fb | 2018-07-18 04:47:37 -0700 | [diff] [blame] | 8370 | curr_events = (int)part_stat_read_accum(&disk->part0, sectors) - |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8371 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 8372 | /* sync IO will cause sync_io to increase before the disk_stats |
| 8373 | * as sync_io is counted when a request starts, and |
| 8374 | * disk_stats is counted when it completes. |
| 8375 | * So resync activity will cause curr_events to be smaller than |
| 8376 | * when there was no such activity. |
| 8377 | * non-sync IO will cause disk_stat to increase without |
| 8378 | * increasing sync_io so curr_events will (eventually) |
| 8379 | * be larger than it was before. Once it becomes |
| 8380 | * substantially larger, the test below will cause |
| 8381 | * the array to appear non-idle, and resync will slow |
| 8382 | * down. |
| 8383 | * If there is a lot of outstanding resync activity when |
| 8384 | * we set last_event to curr_events, then all that activity |
| 8385 | * completing might cause the array to appear non-idle |
| 8386 | * and resync will be slowed down even though there might |
| 8387 | * not have been non-resync activity. This will only |
| 8388 | * happen once though. 'last_events' will soon reflect |
| 8389 | * the state where there is little or no outstanding |
| 8390 | * resync requests, and further resync activity will |
| 8391 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 8392 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8393 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8394 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8395 | rdev->last_events = curr_events; |
| 8396 | idle = 0; |
| 8397 | } |
| 8398 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 8399 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8400 | return idle; |
| 8401 | } |
| 8402 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8403 | void md_done_sync(struct mddev *mddev, int blocks, int ok) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8404 | { |
| 8405 | /* another "blocks" (512byte) blocks have been synced */ |
| 8406 | atomic_sub(blocks, &mddev->recovery_active); |
| 8407 | wake_up(&mddev->recovery_wait); |
| 8408 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 8409 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
majianpeng | 0a19caa | 2012-11-19 19:57:34 +0800 | [diff] [blame] | 8410 | set_bit(MD_RECOVERY_ERROR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8411 | md_wakeup_thread(mddev->thread); |
| 8412 | // stop recovery, signal do_sync .... |
| 8413 | } |
| 8414 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 8415 | EXPORT_SYMBOL(md_done_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8416 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 8417 | /* md_write_start(mddev, bi) |
| 8418 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 8419 | * in superblock) before writing, schedule a superblock update |
| 8420 | * and wait for it to complete. |
NeilBrown | cc27b0c | 2017-06-05 16:49:39 +1000 | [diff] [blame] | 8421 | * A return value of 'false' means that the write wasn't recorded |
| 8422 | * and cannot proceed as the array is being suspend. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 8423 | */ |
NeilBrown | cc27b0c | 2017-06-05 16:49:39 +1000 | [diff] [blame] | 8424 | bool md_write_start(struct mddev *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8425 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 8426 | int did_change = 0; |
Heinz Mauelshagen | 4b6c106 | 2018-02-02 23:13:19 +0100 | [diff] [blame] | 8427 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 8428 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | cc27b0c | 2017-06-05 16:49:39 +1000 | [diff] [blame] | 8429 | return true; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 8430 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8431 | BUG_ON(mddev->ro == 1); |
| 8432 | if (mddev->ro == 2) { |
| 8433 | /* need to switch to read/write */ |
| 8434 | mddev->ro = 0; |
| 8435 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 8436 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 8437 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 8438 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8439 | } |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 8440 | rcu_read_lock(); |
| 8441 | percpu_ref_get(&mddev->writes_pending); |
NeilBrown | 55cc39f | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 8442 | smp_mb(); /* Match smp_mb in set_in_sync() */ |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 8443 | if (mddev->safemode == 1) |
| 8444 | mddev->safemode = 0; |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 8445 | /* sync_checkers is always 0 when writes_pending is in per-cpu mode */ |
NeilBrown | 81fe48e | 2017-08-08 16:56:36 +1000 | [diff] [blame] | 8446 | if (mddev->in_sync || mddev->sync_checkers) { |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 8447 | spin_lock(&mddev->lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 8448 | if (mddev->in_sync) { |
| 8449 | mddev->in_sync = 0; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 8450 | set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags); |
| 8451 | set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 8452 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 8453 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 8454 | } |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 8455 | spin_unlock(&mddev->lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 8456 | } |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 8457 | rcu_read_unlock(); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 8458 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 8459 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Heinz Mauelshagen | 4b6c106 | 2018-02-02 23:13:19 +0100 | [diff] [blame] | 8460 | if (!mddev->has_superblocks) |
| 8461 | return true; |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 8462 | wait_event(mddev->sb_wait, |
NeilBrown | d47c8ad | 2017-10-05 16:23:16 +1100 | [diff] [blame] | 8463 | !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags) || |
| 8464 | mddev->suspended); |
NeilBrown | cc27b0c | 2017-06-05 16:49:39 +1000 | [diff] [blame] | 8465 | if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) { |
| 8466 | percpu_ref_put(&mddev->writes_pending); |
| 8467 | return false; |
| 8468 | } |
| 8469 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8470 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 8471 | EXPORT_SYMBOL(md_write_start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8472 | |
NeilBrown | 4972805 | 2017-03-15 14:05:12 +1100 | [diff] [blame] | 8473 | /* md_write_inc can only be called when md_write_start() has |
| 8474 | * already been called at least once of the current request. |
| 8475 | * It increments the counter and is useful when a single request |
| 8476 | * is split into several parts. Each part causes an increment and |
| 8477 | * so needs a matching md_write_end(). |
| 8478 | * Unlike md_write_start(), it is safe to call md_write_inc() inside |
| 8479 | * a spinlocked region. |
| 8480 | */ |
| 8481 | void md_write_inc(struct mddev *mddev, struct bio *bi) |
| 8482 | { |
| 8483 | if (bio_data_dir(bi) != WRITE) |
| 8484 | return; |
| 8485 | WARN_ON_ONCE(mddev->in_sync || mddev->ro); |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 8486 | percpu_ref_get(&mddev->writes_pending); |
NeilBrown | 4972805 | 2017-03-15 14:05:12 +1100 | [diff] [blame] | 8487 | } |
| 8488 | EXPORT_SYMBOL(md_write_inc); |
| 8489 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8490 | void md_write_end(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8491 | { |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 8492 | percpu_ref_put(&mddev->writes_pending); |
| 8493 | |
| 8494 | if (mddev->safemode == 2) |
| 8495 | md_wakeup_thread(mddev->thread); |
| 8496 | else if (mddev->safemode_delay) |
| 8497 | /* The roundup() ensures this only performs locking once |
| 8498 | * every ->safemode_delay jiffies |
| 8499 | */ |
| 8500 | mod_timer(&mddev->safemode_timer, |
| 8501 | roundup(jiffies, mddev->safemode_delay) + |
| 8502 | mddev->safemode_delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8503 | } |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 8504 | |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 8505 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8506 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8507 | /* md_allow_write(mddev) |
| 8508 | * Calling this ensures that the array is marked 'active' so that writes |
| 8509 | * may proceed without blocking. It is important to call this before |
| 8510 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 8511 | * Must be called with mddev_lock held. |
| 8512 | */ |
Artur Paszkiewicz | 2214c26 | 2017-05-08 11:56:55 +0200 | [diff] [blame] | 8513 | void md_allow_write(struct mddev *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8514 | { |
| 8515 | if (!mddev->pers) |
Artur Paszkiewicz | 2214c26 | 2017-05-08 11:56:55 +0200 | [diff] [blame] | 8516 | return; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8517 | if (mddev->ro) |
Artur Paszkiewicz | 2214c26 | 2017-05-08 11:56:55 +0200 | [diff] [blame] | 8518 | return; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 8519 | if (!mddev->pers->sync_request) |
Artur Paszkiewicz | 2214c26 | 2017-05-08 11:56:55 +0200 | [diff] [blame] | 8520 | return; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8521 | |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 8522 | spin_lock(&mddev->lock); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8523 | if (mddev->in_sync) { |
| 8524 | mddev->in_sync = 0; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 8525 | set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags); |
| 8526 | set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8527 | if (mddev->safemode_delay && |
| 8528 | mddev->safemode == 0) |
| 8529 | mddev->safemode = 1; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 8530 | spin_unlock(&mddev->lock); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8531 | md_update_sb(mddev, 0); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 8532 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Artur Paszkiewicz | 2214c26 | 2017-05-08 11:56:55 +0200 | [diff] [blame] | 8533 | /* wait for the dirty state to be recorded in the metadata */ |
| 8534 | wait_event(mddev->sb_wait, |
Artur Paszkiewicz | 2214c26 | 2017-05-08 11:56:55 +0200 | [diff] [blame] | 8535 | !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8536 | } else |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 8537 | spin_unlock(&mddev->lock); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8538 | } |
| 8539 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 8540 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8541 | #define SYNC_MARKS 10 |
| 8542 | #define SYNC_MARK_STEP (3*HZ) |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 8543 | #define UPDATE_FREQUENCY (5*60*HZ) |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 8544 | void md_do_sync(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8545 | { |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 8546 | struct mddev *mddev = thread->mddev; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8547 | struct mddev *mddev2; |
Yufen Yu | e5b521e | 2019-06-14 15:41:07 -0700 | [diff] [blame] | 8548 | unsigned int currspeed = 0, window; |
Xiao Ni | ac7e50a | 2014-08-07 09:37:41 -0400 | [diff] [blame] | 8549 | sector_t max_sectors,j, io_sectors, recovery_done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8550 | unsigned long mark[SYNC_MARKS]; |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 8551 | unsigned long update_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8552 | sector_t mark_cnt[SYNC_MARKS]; |
| 8553 | int last_mark,m; |
| 8554 | struct list_head *tmp; |
| 8555 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8556 | int skipped = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8557 | struct md_rdev *rdev; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 8558 | char *desc, *action = NULL; |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 8559 | struct blk_plug plug; |
Guoqing Jiang | 41a9a0d | 2016-05-02 11:33:08 -0400 | [diff] [blame] | 8560 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8561 | |
| 8562 | /* just incase thread restarts... */ |
Song Liu | d5d885f | 2017-11-19 22:17:01 -0800 | [diff] [blame] | 8563 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
| 8564 | test_bit(MD_RECOVERY_WAIT, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8565 | return; |
NeilBrown | 3991b31 | 2014-05-28 13:39:23 +1000 | [diff] [blame] | 8566 | if (mddev->ro) {/* never try to sync a read-only array */ |
| 8567 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8568 | return; |
NeilBrown | 3991b31 | 2014-05-28 13:39:23 +1000 | [diff] [blame] | 8569 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8570 | |
Guoqing Jiang | 41a9a0d | 2016-05-02 11:33:08 -0400 | [diff] [blame] | 8571 | if (mddev_is_clustered(mddev)) { |
| 8572 | ret = md_cluster_ops->resync_start(mddev); |
| 8573 | if (ret) |
| 8574 | goto skip; |
| 8575 | |
Guoqing Jiang | bb8bf15 | 2016-06-02 23:32:04 -0400 | [diff] [blame] | 8576 | set_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags); |
Guoqing Jiang | 41a9a0d | 2016-05-02 11:33:08 -0400 | [diff] [blame] | 8577 | if (!(test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 8578 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) || |
| 8579 | test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
| 8580 | && ((unsigned long long)mddev->curr_resync_completed |
| 8581 | < (unsigned long long)mddev->resync_max_sectors)) |
| 8582 | goto skip; |
| 8583 | } |
| 8584 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 8585 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 8586 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 8587 | desc = "data-check"; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 8588 | action = "check"; |
| 8589 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 8590 | desc = "requested-resync"; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 8591 | action = "repair"; |
| 8592 | } else |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 8593 | desc = "resync"; |
| 8594 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 8595 | desc = "reshape"; |
| 8596 | else |
| 8597 | desc = "recovery"; |
| 8598 | |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 8599 | mddev->last_sync_action = action ?: desc; |
| 8600 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8601 | /* we overload curr_resync somewhat here. |
| 8602 | * 0 == not engaged in resync at all |
| 8603 | * 2 == checking that there is no conflict with another sync |
| 8604 | * 1 == like 2, but have yielded to allow conflicting resync to |
Yufen Yu | e5b521e | 2019-06-14 15:41:07 -0700 | [diff] [blame] | 8605 | * commence |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8606 | * other == active in resync - this many blocks |
| 8607 | * |
| 8608 | * Before starting a resync we must have set curr_resync to |
| 8609 | * 2, and then checked that every "conflicting" array has curr_resync |
| 8610 | * less than ours. When we find one that is the same or higher |
| 8611 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 8612 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 8613 | * This will mean we have to start checking from the beginning again. |
| 8614 | * |
| 8615 | */ |
| 8616 | |
| 8617 | do { |
Artur Paszkiewicz | c622ca5 | 2016-08-16 14:26:08 +0200 | [diff] [blame] | 8618 | int mddev2_minor = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8619 | mddev->curr_resync = 2; |
| 8620 | |
| 8621 | try_again: |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 8622 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8623 | goto skip; |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 8624 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8625 | if (mddev2 == mddev) |
| 8626 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 8627 | if (!mddev->parallel_resync |
| 8628 | && mddev2->curr_resync |
| 8629 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8630 | DEFINE_WAIT(wq); |
| 8631 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 8632 | /* arbitrarily yield */ |
| 8633 | mddev->curr_resync = 1; |
| 8634 | wake_up(&resync_wait); |
| 8635 | } |
| 8636 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 8637 | /* no need to wait here, we can wait the next |
| 8638 | * time 'round when curr_resync == 2 |
| 8639 | */ |
| 8640 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 8641 | /* We need to wait 'interruptible' so as not to |
| 8642 | * contribute to the load average, and not to |
| 8643 | * be caught by 'softlockup' |
| 8644 | */ |
| 8645 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8646 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 8647 | mddev2->curr_resync >= mddev->curr_resync) { |
Artur Paszkiewicz | c622ca5 | 2016-08-16 14:26:08 +0200 | [diff] [blame] | 8648 | if (mddev2_minor != mddev2->md_minor) { |
| 8649 | mddev2_minor = mddev2->md_minor; |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8650 | pr_info("md: delaying %s of %s until %s has finished (they share one or more physical units)\n", |
| 8651 | desc, mdname(mddev), |
| 8652 | mdname(mddev2)); |
Artur Paszkiewicz | c622ca5 | 2016-08-16 14:26:08 +0200 | [diff] [blame] | 8653 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8654 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 8655 | if (signal_pending(current)) |
| 8656 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8657 | schedule(); |
| 8658 | finish_wait(&resync_wait, &wq); |
| 8659 | goto try_again; |
| 8660 | } |
| 8661 | finish_wait(&resync_wait, &wq); |
| 8662 | } |
| 8663 | } |
| 8664 | } while (mddev->curr_resync < 2); |
| 8665 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8666 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8667 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8668 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8669 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8670 | */ |
| 8671 | max_sectors = mddev->resync_max_sectors; |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 8672 | atomic64_set(&mddev->resync_mismatches, 0); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8673 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 8674 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 8675 | j = mddev->resync_min; |
| 8676 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8677 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 8678 | |
Guoqing Jiang | cb9ee15 | 2018-10-18 16:37:47 +0800 | [diff] [blame] | 8679 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) { |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 8680 | max_sectors = mddev->resync_max_sectors; |
Guoqing Jiang | cb9ee15 | 2018-10-18 16:37:47 +0800 | [diff] [blame] | 8681 | /* |
| 8682 | * If the original node aborts reshaping then we continue the |
| 8683 | * reshaping, so set j again to avoid restart reshape from the |
| 8684 | * first beginning |
| 8685 | */ |
| 8686 | if (mddev_is_clustered(mddev) && |
| 8687 | mddev->reshape_position != MaxSector) |
| 8688 | j = mddev->reshape_position; |
| 8689 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8690 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8691 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8692 | j = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 8693 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 8694 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8695 | if (rdev->raid_disk >= 0 && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 8696 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8697 | !test_bit(Faulty, &rdev->flags) && |
| 8698 | !test_bit(In_sync, &rdev->flags) && |
| 8699 | rdev->recovery_offset < j) |
| 8700 | j = rdev->recovery_offset; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 8701 | rcu_read_unlock(); |
NeilBrown | 133d452 | 2014-07-02 12:04:14 +1000 | [diff] [blame] | 8702 | |
| 8703 | /* If there is a bitmap, we need to make sure all |
| 8704 | * writes that started before we added a spare |
| 8705 | * complete before we start doing a recovery. |
| 8706 | * Otherwise the write might complete and (via |
| 8707 | * bitmap_endwrite) set a bit in the bitmap after the |
| 8708 | * recovery has checked that bit and skipped that |
| 8709 | * region. |
| 8710 | */ |
| 8711 | if (mddev->bitmap) { |
| 8712 | mddev->pers->quiesce(mddev, 1); |
| 8713 | mddev->pers->quiesce(mddev, 0); |
| 8714 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8715 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8716 | |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8717 | pr_info("md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 8718 | pr_debug("md: minimum _guaranteed_ speed: %d KB/sec/disk.\n", speed_min(mddev)); |
| 8719 | pr_debug("md: using maximum available idle IO bandwidth (but not more than %d KB/sec) for %s.\n", |
| 8720 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8721 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8722 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8723 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8724 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8725 | for (m = 0; m < SYNC_MARKS; m++) { |
| 8726 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8727 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8728 | } |
| 8729 | last_mark = 0; |
| 8730 | mddev->resync_mark = mark[last_mark]; |
| 8731 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 8732 | |
| 8733 | /* |
| 8734 | * Tune reconstruction: |
| 8735 | */ |
Yufen Yu | e5b521e | 2019-06-14 15:41:07 -0700 | [diff] [blame] | 8736 | window = 32 * (PAGE_SIZE / 512); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8737 | pr_debug("md: using %dk window, over a total of %lluk.\n", |
| 8738 | window/2, (unsigned long long)max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8739 | |
| 8740 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8741 | last_check = 0; |
| 8742 | |
| 8743 | if (j>2) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8744 | pr_debug("md: resuming %s of %s from checkpoint.\n", |
| 8745 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8746 | mddev->curr_resync = j; |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 8747 | } else |
| 8748 | mddev->curr_resync = 3; /* no longer delayed */ |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 8749 | mddev->curr_resync_completed = j; |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 8750 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
| 8751 | md_new_event(mddev); |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 8752 | update_time = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8753 | |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 8754 | blk_start_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8755 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8756 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8757 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8758 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8759 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 8760 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 8761 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 8762 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 8763 | > (max_sectors >> 4)) || |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 8764 | time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) || |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 8765 | (j - mddev->curr_resync_completed)*2 |
NeilBrown | c5e19d9 | 2015-07-17 12:06:02 +1000 | [diff] [blame] | 8766 | >= mddev->resync_max - mddev->curr_resync_completed || |
| 8767 | mddev->curr_resync_completed > mddev->resync_max |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 8768 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8769 | /* time to update curr_resync_completed */ |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8770 | wait_event(mddev->recovery_wait, |
| 8771 | atomic_read(&mddev->recovery_active) == 0); |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 8772 | mddev->curr_resync_completed = j; |
kernelmail | 35d78c6 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 8773 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && |
| 8774 | j > mddev->recovery_cp) |
| 8775 | mddev->recovery_cp = j; |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 8776 | update_time = jiffies; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 8777 | set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 8778 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8779 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 8780 | |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8781 | while (j >= mddev->resync_max && |
| 8782 | !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 8783 | /* As this condition is controlled by user-space, |
| 8784 | * we can block indefinitely, so use '_interruptible' |
| 8785 | * to avoid triggering warnings. |
| 8786 | */ |
| 8787 | flush_signals(current); /* just in case */ |
| 8788 | wait_event_interruptible(mddev->recovery_wait, |
| 8789 | mddev->resync_max > j |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8790 | || test_bit(MD_RECOVERY_INTR, |
| 8791 | &mddev->recovery)); |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 8792 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 8793 | |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8794 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 8795 | break; |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 8796 | |
NeilBrown | 0931479 | 2015-02-19 16:04:40 +1100 | [diff] [blame] | 8797 | sectors = mddev->pers->sync_request(mddev, j, &skipped); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8798 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 8799 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8800 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8801 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8802 | |
| 8803 | if (!skipped) { /* actual IO requested */ |
| 8804 | io_sectors += sectors; |
| 8805 | atomic_add(sectors, &mddev->recovery_active); |
| 8806 | } |
| 8807 | |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 8808 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 8809 | break; |
| 8810 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8811 | j += sectors; |
NeilBrown | 5ed1df2 | 2015-07-24 13:27:08 +1000 | [diff] [blame] | 8812 | if (j > max_sectors) |
| 8813 | /* when skipping, extra large numbers can be returned. */ |
| 8814 | j = max_sectors; |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 8815 | if (j > 2) |
| 8816 | mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 8817 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 8818 | if (last_check == 0) |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 8819 | /* this is the earliest that rebuild will be |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 8820 | * visible in /proc/mdstat |
| 8821 | */ |
| 8822 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8823 | |
| 8824 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8825 | continue; |
| 8826 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8827 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8828 | repeat: |
| 8829 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 8830 | /* step marks */ |
| 8831 | int next = (last_mark+1) % SYNC_MARKS; |
| 8832 | |
| 8833 | mddev->resync_mark = mark[next]; |
| 8834 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 8835 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8836 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8837 | last_mark = next; |
| 8838 | } |
| 8839 | |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8840 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 8841 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8842 | |
| 8843 | /* |
| 8844 | * this loop exits only if either when we are slower than |
| 8845 | * the 'hard' speed limit, or the system was IO-idle for |
| 8846 | * a jiffy. |
| 8847 | * the system might be non-idle CPU-wise, but we only care |
| 8848 | * about not overloading the IO subsystem. (things like an |
| 8849 | * e2fsck being done on the RAID array should execute fast) |
| 8850 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8851 | cond_resched(); |
| 8852 | |
Xiao Ni | ac7e50a | 2014-08-07 09:37:41 -0400 | [diff] [blame] | 8853 | recovery_done = io_sectors - atomic_read(&mddev->recovery_active); |
| 8854 | currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2 |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8855 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8856 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 8857 | if (currspeed > speed_min(mddev)) { |
NeilBrown | ac8fa41 | 2015-02-19 16:55:00 +1100 | [diff] [blame] | 8858 | if (currspeed > speed_max(mddev)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 8859 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8860 | goto repeat; |
| 8861 | } |
NeilBrown | ac8fa41 | 2015-02-19 16:55:00 +1100 | [diff] [blame] | 8862 | if (!is_mddev_idle(mddev, 0)) { |
| 8863 | /* |
| 8864 | * Give other IO more of a chance. |
| 8865 | * The faster the devices, the less we wait. |
| 8866 | */ |
| 8867 | wait_event(mddev->recovery_wait, |
| 8868 | !atomic_read(&mddev->recovery_active)); |
| 8869 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8870 | } |
| 8871 | } |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8872 | pr_info("md: %s: %s %s.\n",mdname(mddev), desc, |
| 8873 | test_bit(MD_RECOVERY_INTR, &mddev->recovery) |
| 8874 | ? "interrupted" : "done"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8875 | /* |
| 8876 | * this also signals 'finished resyncing' to md_stop |
| 8877 | */ |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 8878 | blk_finish_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8879 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 8880 | |
NeilBrown | 5ed1df2 | 2015-07-24 13:27:08 +1000 | [diff] [blame] | 8881 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 8882 | !test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
NeilBrown | 1217e1d | 2016-10-28 15:59:41 +1100 | [diff] [blame] | 8883 | mddev->curr_resync > 3) { |
NeilBrown | 5ed1df2 | 2015-07-24 13:27:08 +1000 | [diff] [blame] | 8884 | mddev->curr_resync_completed = mddev->curr_resync; |
| 8885 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
| 8886 | } |
NeilBrown | 0931479 | 2015-02-19 16:04:40 +1100 | [diff] [blame] | 8887 | mddev->pers->sync_request(mddev, max_sectors, &skipped); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8888 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 8889 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 1217e1d | 2016-10-28 15:59:41 +1100 | [diff] [blame] | 8890 | mddev->curr_resync > 3) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8891 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 8892 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 8893 | if (mddev->curr_resync >= mddev->recovery_cp) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8894 | pr_debug("md: checkpointing %s of %s.\n", |
| 8895 | desc, mdname(mddev)); |
majianpeng | 0a19caa | 2012-11-19 19:57:34 +0800 | [diff] [blame] | 8896 | if (test_bit(MD_RECOVERY_ERROR, |
| 8897 | &mddev->recovery)) |
| 8898 | mddev->recovery_cp = |
| 8899 | mddev->curr_resync_completed; |
| 8900 | else |
| 8901 | mddev->recovery_cp = |
| 8902 | mddev->curr_resync; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8903 | } |
| 8904 | } else |
| 8905 | mddev->recovery_cp = MaxSector; |
| 8906 | } else { |
| 8907 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 8908 | mddev->curr_resync = MaxSector; |
NeilBrown | db0505d | 2017-10-17 16:18:36 +1100 | [diff] [blame] | 8909 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 8910 | test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) { |
| 8911 | rcu_read_lock(); |
| 8912 | rdev_for_each_rcu(rdev, mddev) |
| 8913 | if (rdev->raid_disk >= 0 && |
| 8914 | mddev->delta_disks >= 0 && |
| 8915 | !test_bit(Journal, &rdev->flags) && |
| 8916 | !test_bit(Faulty, &rdev->flags) && |
| 8917 | !test_bit(In_sync, &rdev->flags) && |
| 8918 | rdev->recovery_offset < mddev->curr_resync) |
| 8919 | rdev->recovery_offset = mddev->curr_resync; |
| 8920 | rcu_read_unlock(); |
| 8921 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8922 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8923 | } |
NeilBrown | db91ff5 | 2012-02-07 12:01:51 +1100 | [diff] [blame] | 8924 | skip: |
Guoqing Jiang | bb8bf15 | 2016-06-02 23:32:04 -0400 | [diff] [blame] | 8925 | /* set CHANGE_PENDING here since maybe another update is needed, |
| 8926 | * so other nodes are informed. It should be harmless for normal |
| 8927 | * raid */ |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 8928 | set_mask_bits(&mddev->sb_flags, 0, |
| 8929 | BIT(MD_SB_CHANGE_PENDING) | BIT(MD_SB_CHANGE_DEVS)); |
Goldwyn Rodrigues | c186b12 | 2015-09-30 13:20:35 -0500 | [diff] [blame] | 8930 | |
BingJing Chang | 8876391 | 2018-02-22 13:34:46 +0800 | [diff] [blame] | 8931 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 8932 | !test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 8933 | mddev->delta_disks > 0 && |
| 8934 | mddev->pers->finish_reshape && |
| 8935 | mddev->pers->size && |
| 8936 | mddev->queue) { |
| 8937 | mddev_lock_nointr(mddev); |
| 8938 | md_set_array_sectors(mddev, mddev->pers->size(mddev, 0, 0)); |
| 8939 | mddev_unlock(mddev); |
Guoqing Jiang | aefb2e5 | 2018-10-18 16:37:44 +0800 | [diff] [blame] | 8940 | if (!mddev_is_clustered(mddev)) { |
| 8941 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 8942 | revalidate_disk(mddev->gendisk); |
| 8943 | } |
BingJing Chang | 8876391 | 2018-02-22 13:34:46 +0800 | [diff] [blame] | 8944 | } |
| 8945 | |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 8946 | spin_lock(&mddev->lock); |
NeilBrown | c07b70a | 2009-12-14 12:49:48 +1100 | [diff] [blame] | 8947 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 8948 | /* We completed so min/max setting can be forgotten if used. */ |
| 8949 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 8950 | mddev->resync_min = 0; |
| 8951 | mddev->resync_max = MaxSector; |
| 8952 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 8953 | mddev->resync_min = mddev->curr_resync_completed; |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 8954 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8955 | mddev->curr_resync = 0; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 8956 | spin_unlock(&mddev->lock); |
| 8957 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8958 | wake_up(&resync_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8959 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 8960 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8961 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 8962 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8963 | |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 8964 | static int remove_and_add_spares(struct mddev *mddev, |
| 8965 | struct md_rdev *this) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8966 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8967 | struct md_rdev *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8968 | int spares = 0; |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 8969 | int removed = 0; |
NeilBrown | d787be4 | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 8970 | bool remove_some = false; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8971 | |
NeilBrown | 39772f0 | 2018-02-03 09:19:30 +1100 | [diff] [blame] | 8972 | if (this && test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 8973 | /* Mustn't remove devices when resync thread is running */ |
| 8974 | return 0; |
| 8975 | |
NeilBrown | d787be4 | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 8976 | rdev_for_each(rdev, mddev) { |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 8977 | if ((this == NULL || rdev == this) && |
| 8978 | rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 8979 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | d787be4 | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 8980 | test_bit(Faulty, &rdev->flags) && |
| 8981 | atomic_read(&rdev->nr_pending)==0) { |
| 8982 | /* Faulty non-Blocked devices with nr_pending == 0 |
| 8983 | * never get nr_pending incremented, |
| 8984 | * never get Faulty cleared, and never get Blocked set. |
| 8985 | * So we can synchronize_rcu now rather than once per device |
| 8986 | */ |
| 8987 | remove_some = true; |
| 8988 | set_bit(RemoveSynchronized, &rdev->flags); |
| 8989 | } |
| 8990 | } |
| 8991 | |
| 8992 | if (remove_some) |
| 8993 | synchronize_rcu(); |
| 8994 | rdev_for_each(rdev, mddev) { |
| 8995 | if ((this == NULL || rdev == this) && |
| 8996 | rdev->raid_disk >= 0 && |
| 8997 | !test_bit(Blocked, &rdev->flags) && |
| 8998 | ((test_bit(RemoveSynchronized, &rdev->flags) || |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 8999 | (!test_bit(In_sync, &rdev->flags) && |
| 9000 | !test_bit(Journal, &rdev->flags))) && |
NeilBrown | d787be4 | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 9001 | atomic_read(&rdev->nr_pending)==0)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9002 | if (mddev->pers->hot_remove_disk( |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 9003 | mddev, rdev) == 0) { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 9004 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | 011abdc | 2018-04-26 14:46:29 +1000 | [diff] [blame] | 9005 | rdev->saved_raid_disk = rdev->raid_disk; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9006 | rdev->raid_disk = -1; |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 9007 | removed++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9008 | } |
| 9009 | } |
NeilBrown | d787be4 | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 9010 | if (remove_some && test_bit(RemoveSynchronized, &rdev->flags)) |
| 9011 | clear_bit(RemoveSynchronized, &rdev->flags); |
| 9012 | } |
| 9013 | |
Jonathan Brassow | 90584fc | 2013-03-07 16:24:26 -0600 | [diff] [blame] | 9014 | if (removed && mddev->kobj.sd) |
| 9015 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9016 | |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 9017 | if (this && removed) |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 9018 | goto no_add; |
| 9019 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 9020 | rdev_for_each(rdev, mddev) { |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 9021 | if (this && this != rdev) |
| 9022 | continue; |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 9023 | if (test_bit(Candidate, &rdev->flags)) |
| 9024 | continue; |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 9025 | if (rdev->raid_disk >= 0 && |
| 9026 | !test_bit(In_sync, &rdev->flags) && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 9027 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 9028 | !test_bit(Faulty, &rdev->flags)) |
| 9029 | spares++; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 9030 | if (rdev->raid_disk >= 0) |
| 9031 | continue; |
| 9032 | if (test_bit(Faulty, &rdev->flags)) |
| 9033 | continue; |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 9034 | if (!test_bit(Journal, &rdev->flags)) { |
| 9035 | if (mddev->ro && |
| 9036 | ! (rdev->saved_raid_disk >= 0 && |
| 9037 | !test_bit(Bitmap_sync, &rdev->flags))) |
| 9038 | continue; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 9039 | |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 9040 | rdev->recovery_offset = 0; |
| 9041 | } |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 9042 | if (mddev->pers-> |
| 9043 | hot_add_disk(mddev, rdev) == 0) { |
| 9044 | if (sysfs_link_rdev(mddev, rdev)) |
| 9045 | /* failure here is OK */; |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 9046 | if (!test_bit(Journal, &rdev->flags)) |
| 9047 | spares++; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 9048 | md_new_event(mddev); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 9049 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 9050 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9051 | } |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 9052 | no_add: |
NeilBrown | 6dafab6 | 2012-09-19 12:54:22 +1000 | [diff] [blame] | 9053 | if (removed) |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 9054 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9055 | return spares; |
| 9056 | } |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 9057 | |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 9058 | static void md_start_sync(struct work_struct *ws) |
| 9059 | { |
| 9060 | struct mddev *mddev = container_of(ws, struct mddev, del_work); |
Goldwyn Rodrigues | c186b12 | 2015-09-30 13:20:35 -0500 | [diff] [blame] | 9061 | |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 9062 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 9063 | mddev, |
| 9064 | "resync"); |
| 9065 | if (!mddev->sync_thread) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 9066 | pr_warn("%s: could not start resync thread...\n", |
| 9067 | mdname(mddev)); |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 9068 | /* leave the spares where they are, it shouldn't hurt */ |
| 9069 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 9070 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 9071 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 9072 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 9073 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 9074 | wake_up(&resync_wait); |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 9075 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 9076 | &mddev->recovery)) |
| 9077 | if (mddev->sysfs_action) |
| 9078 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 9079 | } else |
| 9080 | md_wakeup_thread(mddev->sync_thread); |
| 9081 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 9082 | md_new_event(mddev); |
| 9083 | } |
| 9084 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9085 | /* |
| 9086 | * This routine is regularly called by all per-raid-array threads to |
| 9087 | * deal with generic issues like resync and super-block update. |
| 9088 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 9089 | * need this as they never do any recovery or update the superblock. |
| 9090 | * |
| 9091 | * It does not do any resync itself, but rather "forks" off other threads |
| 9092 | * to do that as needed. |
| 9093 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 9094 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 9095 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9096 | * and wakeups up this thread which will reap the thread and finish up. |
| 9097 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 9098 | * |
| 9099 | * The overall approach is: |
| 9100 | * 1/ if the superblock needs updating, update it. |
| 9101 | * 2/ If a recovery thread is running, don't do anything else. |
| 9102 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 9103 | * 4/ If there are any faulty devices, remove them. |
| 9104 | * 5/ If array is degraded, try to add spares devices |
| 9105 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 9106 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 9107 | void md_check_recovery(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9108 | { |
NeilBrown | 059421e | 2018-10-03 15:04:41 +1000 | [diff] [blame] | 9109 | if (test_bit(MD_ALLOW_SB_UPDATE, &mddev->flags) && mddev->sb_flags) { |
| 9110 | /* Write superblock - thread that called mddev_suspend() |
| 9111 | * holds reconfig_mutex for us. |
| 9112 | */ |
| 9113 | set_bit(MD_UPDATING_SB, &mddev->flags); |
| 9114 | smp_mb__after_atomic(); |
| 9115 | if (test_bit(MD_ALLOW_SB_UPDATE, &mddev->flags)) |
| 9116 | md_update_sb(mddev, 0); |
| 9117 | clear_bit_unlock(MD_UPDATING_SB, &mddev->flags); |
| 9118 | wake_up(&mddev->sb_wait); |
| 9119 | } |
| 9120 | |
Jonathan Brassow | 68866e4 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 9121 | if (mddev->suspended) |
| 9122 | return; |
| 9123 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 9124 | if (mddev->bitmap) |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 9125 | md_bitmap_daemon_work(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9126 | |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 9127 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 9128 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 9129 | pr_debug("md: %s in immediate safe mode\n", |
| 9130 | mdname(mddev)); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 9131 | mddev->safemode = 2; |
| 9132 | } |
| 9133 | flush_signals(current); |
| 9134 | } |
| 9135 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 9136 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 9137 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9138 | if ( ! ( |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 9139 | (mddev->sb_flags & ~ (1<<MD_SB_CHANGE_PENDING)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9140 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 9141 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 9142 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 9143 | (mddev->safemode == 2 |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 9144 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9145 | )) |
| 9146 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 9147 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 9148 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9149 | int spares = 0; |
NeilBrown | 480523f | 2019-08-20 10:21:09 +1000 | [diff] [blame] | 9150 | bool try_set_sync = mddev->safemode != 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 9151 | |
Shaohua Li | afc1f55 | 2017-08-11 20:34:45 -0700 | [diff] [blame] | 9152 | if (!mddev->external && mddev->safemode == 1) |
NeilBrown | 33182d1 | 2017-08-08 16:56:36 +1000 | [diff] [blame] | 9153 | mddev->safemode = 0; |
| 9154 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 9155 | if (mddev->ro) { |
Neil Brown | ab16bfc | 2015-06-17 12:31:46 +1000 | [diff] [blame] | 9156 | struct md_rdev *rdev; |
| 9157 | if (!mddev->external && mddev->in_sync) |
| 9158 | /* 'Blocked' flag not needed as failed devices |
| 9159 | * will be recorded if array switched to read/write. |
| 9160 | * Leaving it set will prevent the device |
| 9161 | * from being removed. |
| 9162 | */ |
| 9163 | rdev_for_each(rdev, mddev) |
| 9164 | clear_bit(Blocked, &rdev->flags); |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 9165 | /* On a read-only array we can: |
| 9166 | * - remove failed devices |
| 9167 | * - add already-in_sync devices if the array itself |
| 9168 | * is in-sync. |
| 9169 | * As we only add devices that are already in-sync, |
| 9170 | * we can activate the spares immediately. |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 9171 | */ |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 9172 | remove_and_add_spares(mddev, NULL); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 9173 | /* There is no thread, but we need to call |
| 9174 | * ->spare_active and clear saved_raid_disk |
| 9175 | */ |
NeilBrown | 2ac295a | 2014-05-29 11:40:03 +1000 | [diff] [blame] | 9176 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 9177 | md_reap_sync_thread(mddev); |
NeilBrown | a4a3d26 | 2015-07-17 11:57:30 +1000 | [diff] [blame] | 9178 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 9179 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 9180 | clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags); |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 9181 | goto unlock; |
| 9182 | } |
| 9183 | |
Guoqing Jiang | 659b254 | 2015-12-21 10:50:59 +1100 | [diff] [blame] | 9184 | if (mddev_is_clustered(mddev)) { |
| 9185 | struct md_rdev *rdev; |
| 9186 | /* kick the device if another node issued a |
| 9187 | * remove disk. |
| 9188 | */ |
| 9189 | rdev_for_each(rdev, mddev) { |
| 9190 | if (test_and_clear_bit(ClusterRemove, &rdev->flags) && |
| 9191 | rdev->raid_disk < 0) |
| 9192 | md_kick_rdev_from_array(rdev); |
| 9193 | } |
| 9194 | } |
| 9195 | |
NeilBrown | 480523f | 2019-08-20 10:21:09 +1000 | [diff] [blame] | 9196 | if (try_set_sync && !mddev->external && !mddev->in_sync) { |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 9197 | spin_lock(&mddev->lock); |
NeilBrown | 6497709b | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 9198 | set_in_sync(mddev); |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 9199 | spin_unlock(&mddev->lock); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 9200 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 9201 | |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 9202 | if (mddev->sb_flags) |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 9203 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 9204 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9205 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 9206 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 9207 | /* resync/recovery still happening */ |
| 9208 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 9209 | goto unlock; |
| 9210 | } |
| 9211 | if (mddev->sync_thread) { |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9212 | md_reap_sync_thread(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9213 | goto unlock; |
| 9214 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 9215 | /* Set RUNNING before clearing NEEDED to avoid |
| 9216 | * any transients in the value of "sync_action". |
| 9217 | */ |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 9218 | mddev->curr_resync_completed = 0; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 9219 | spin_lock(&mddev->lock); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 9220 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 9221 | spin_unlock(&mddev->lock); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 9222 | /* Clear some bits that don't mean anything, but |
| 9223 | * might be left set |
| 9224 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 9225 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 9226 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9227 | |
NeilBrown | ed20958 | 2012-04-24 10:23:14 +1000 | [diff] [blame] | 9228 | if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
| 9229 | test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 9230 | goto not_running; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9231 | /* no recovery is running. |
| 9232 | * remove any failed drives, then |
| 9233 | * add spares if possible. |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 9234 | * Spares are also removed and re-added, to allow |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9235 | * the personality to fail the re-add. |
| 9236 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9237 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9238 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 9239 | if (mddev->pers->check_reshape == NULL || |
| 9240 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9241 | /* Cannot proceed */ |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 9242 | goto not_running; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9243 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 9244 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 9245 | } else if ((spares = remove_and_add_spares(mddev, NULL))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 9246 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 9247 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 9248 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 9249 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 9250 | } else if (mddev->recovery_cp < MaxSector) { |
| 9251 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 9252 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 9253 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 9254 | /* nothing to be done ... */ |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 9255 | goto not_running; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 9256 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9257 | if (mddev->pers->sync_request) { |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 9258 | if (spares) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 9259 | /* We are adding a device or devices to an array |
| 9260 | * which has the bitmap stored on all devices. |
| 9261 | * So make sure all bitmap pages get written |
| 9262 | */ |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 9263 | md_bitmap_write_all(mddev->bitmap); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 9264 | } |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 9265 | INIT_WORK(&mddev->del_work, md_start_sync); |
| 9266 | queue_work(md_misc_wq, &mddev->del_work); |
| 9267 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9268 | } |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 9269 | not_running: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 9270 | if (!mddev->sync_thread) { |
| 9271 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 9272 | wake_up(&resync_wait); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 9273 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 9274 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 9275 | if (mddev->sysfs_action) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 9276 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 9277 | } |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 9278 | unlock: |
| 9279 | wake_up(&mddev->sb_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9280 | mddev_unlock(mddev); |
| 9281 | } |
| 9282 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 9283 | EXPORT_SYMBOL(md_check_recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9284 | |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9285 | void md_reap_sync_thread(struct mddev *mddev) |
| 9286 | { |
| 9287 | struct md_rdev *rdev; |
Guoqing Jiang | aefb2e5 | 2018-10-18 16:37:44 +0800 | [diff] [blame] | 9288 | sector_t old_dev_sectors = mddev->dev_sectors; |
| 9289 | bool is_reshaped = false; |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9290 | |
| 9291 | /* resync has finished, collect result */ |
| 9292 | md_unregister_thread(&mddev->sync_thread); |
| 9293 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
Guoqing Jiang | 0d8ed0e9 | 2019-07-24 11:09:21 +0200 | [diff] [blame] | 9294 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) && |
| 9295 | mddev->degraded != mddev->raid_disks) { |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9296 | /* success...*/ |
| 9297 | /* activate any spares */ |
| 9298 | if (mddev->pers->spare_active(mddev)) { |
| 9299 | sysfs_notify(&mddev->kobj, NULL, |
| 9300 | "degraded"); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 9301 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9302 | } |
| 9303 | } |
| 9304 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
Guoqing Jiang | aefb2e5 | 2018-10-18 16:37:44 +0800 | [diff] [blame] | 9305 | mddev->pers->finish_reshape) { |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9306 | mddev->pers->finish_reshape(mddev); |
Guoqing Jiang | aefb2e5 | 2018-10-18 16:37:44 +0800 | [diff] [blame] | 9307 | if (mddev_is_clustered(mddev)) |
| 9308 | is_reshaped = true; |
| 9309 | } |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9310 | |
| 9311 | /* If array is no-longer degraded, then any saved_raid_disk |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 9312 | * information must be scrapped. |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9313 | */ |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 9314 | if (!mddev->degraded) |
| 9315 | rdev_for_each(rdev, mddev) |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9316 | rdev->saved_raid_disk = -1; |
| 9317 | |
| 9318 | md_update_sb(mddev, 1); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 9319 | /* MD_SB_CHANGE_PENDING should be cleared by md_update_sb, so we can |
Guoqing Jiang | bb8bf15 | 2016-06-02 23:32:04 -0400 | [diff] [blame] | 9320 | * call resync_finish here if MD_CLUSTER_RESYNC_LOCKED is set by |
| 9321 | * clustered raid */ |
| 9322 | if (test_and_clear_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags)) |
| 9323 | md_cluster_ops->resync_finish(mddev); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9324 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | ea358cd | 2015-06-12 20:05:04 +1000 | [diff] [blame] | 9325 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9326 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 9327 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 9328 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 9329 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Guoqing Jiang | aefb2e5 | 2018-10-18 16:37:44 +0800 | [diff] [blame] | 9330 | /* |
| 9331 | * We call md_cluster_ops->update_size here because sync_size could |
| 9332 | * be changed by md_update_sb, and MD_RECOVERY_RESHAPE is cleared, |
| 9333 | * so it is time to update size across cluster. |
| 9334 | */ |
| 9335 | if (mddev_is_clustered(mddev) && is_reshaped |
| 9336 | && !test_bit(MD_CLOSING, &mddev->flags)) |
| 9337 | md_cluster_ops->update_size(mddev, old_dev_sectors); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 9338 | wake_up(&resync_wait); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9339 | /* flag recovery needed just to double check */ |
| 9340 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 9341 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 9342 | md_new_event(mddev); |
| 9343 | if (mddev->event_work.func) |
| 9344 | queue_work(md_misc_wq, &mddev->event_work); |
| 9345 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 9346 | EXPORT_SYMBOL(md_reap_sync_thread); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9347 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 9348 | void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev) |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 9349 | { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 9350 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 9351 | wait_event_timeout(rdev->blocked_wait, |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 9352 | !test_bit(Blocked, &rdev->flags) && |
| 9353 | !test_bit(BlockedBadBlocks, &rdev->flags), |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 9354 | msecs_to_jiffies(5000)); |
| 9355 | rdev_dec_pending(rdev, mddev); |
| 9356 | } |
| 9357 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 9358 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 9359 | void md_finish_reshape(struct mddev *mddev) |
| 9360 | { |
| 9361 | /* called be personality module when reshape completes. */ |
| 9362 | struct md_rdev *rdev; |
| 9363 | |
| 9364 | rdev_for_each(rdev, mddev) { |
| 9365 | if (rdev->data_offset > rdev->new_data_offset) |
| 9366 | rdev->sectors += rdev->data_offset - rdev->new_data_offset; |
| 9367 | else |
| 9368 | rdev->sectors -= rdev->new_data_offset - rdev->data_offset; |
| 9369 | rdev->data_offset = rdev->new_data_offset; |
| 9370 | } |
| 9371 | } |
| 9372 | EXPORT_SYMBOL(md_finish_reshape); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 9373 | |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 9374 | /* Bad block management */ |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 9375 | |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 9376 | /* Returns 1 on success, 0 on failure */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 9377 | int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 9378 | int is_new) |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 9379 | { |
Guoqing Jiang | 85ad1d1 | 2016-05-03 22:22:13 -0400 | [diff] [blame] | 9380 | struct mddev *mddev = rdev->mddev; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 9381 | int rv; |
| 9382 | if (is_new) |
| 9383 | s += rdev->new_data_offset; |
| 9384 | else |
| 9385 | s += rdev->data_offset; |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 9386 | rv = badblocks_set(&rdev->badblocks, s, sectors, 0); |
| 9387 | if (rv == 0) { |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 9388 | /* Make sure they get written out promptly */ |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 9389 | if (test_bit(ExternalBbl, &rdev->flags)) |
| 9390 | sysfs_notify(&rdev->kobj, NULL, |
| 9391 | "unacknowledged_bad_blocks"); |
NeilBrown | 8bd2f0a | 2011-12-08 16:26:08 +1100 | [diff] [blame] | 9392 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 9393 | set_mask_bits(&mddev->sb_flags, 0, |
| 9394 | BIT(MD_SB_CHANGE_CLEAN) | BIT(MD_SB_CHANGE_PENDING)); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 9395 | md_wakeup_thread(rdev->mddev->thread); |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 9396 | return 1; |
| 9397 | } else |
| 9398 | return 0; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 9399 | } |
| 9400 | EXPORT_SYMBOL_GPL(rdev_set_badblocks); |
| 9401 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 9402 | int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
| 9403 | int is_new) |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 9404 | { |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 9405 | int rv; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 9406 | if (is_new) |
| 9407 | s += rdev->new_data_offset; |
| 9408 | else |
| 9409 | s += rdev->data_offset; |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 9410 | rv = badblocks_clear(&rdev->badblocks, s, sectors); |
| 9411 | if ((rv == 0) && test_bit(ExternalBbl, &rdev->flags)) |
| 9412 | sysfs_notify(&rdev->kobj, NULL, "bad_blocks"); |
| 9413 | return rv; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 9414 | } |
| 9415 | EXPORT_SYMBOL_GPL(rdev_clear_badblocks); |
| 9416 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 9417 | static int md_notify_reboot(struct notifier_block *this, |
| 9418 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9419 | { |
| 9420 | struct list_head *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 9421 | struct mddev *mddev; |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 9422 | int need_delay = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9423 | |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 9424 | for_each_mddev(mddev, tmp) { |
| 9425 | if (mddev_trylock(mddev)) { |
NeilBrown | 30b8aa9 | 2012-04-24 10:23:16 +1000 | [diff] [blame] | 9426 | if (mddev->pers) |
| 9427 | __md_stop_writes(mddev); |
NeilBrown | 0f62fb2 | 2014-05-06 09:36:08 +1000 | [diff] [blame] | 9428 | if (mddev->persistent) |
| 9429 | mddev->safemode = 2; |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 9430 | mddev_unlock(mddev); |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 9431 | } |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 9432 | need_delay = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9433 | } |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 9434 | /* |
| 9435 | * certain more exotic SCSI devices are known to be |
| 9436 | * volatile wrt too early system reboots. While the |
| 9437 | * right place to handle this issue is the given |
| 9438 | * driver, we do want to have a safe RAID driver ... |
| 9439 | */ |
| 9440 | if (need_delay) |
| 9441 | mdelay(1000*1); |
| 9442 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9443 | return NOTIFY_DONE; |
| 9444 | } |
| 9445 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 9446 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9447 | .notifier_call = md_notify_reboot, |
| 9448 | .next = NULL, |
| 9449 | .priority = INT_MAX, /* before any real devices */ |
| 9450 | }; |
| 9451 | |
| 9452 | static void md_geninit(void) |
| 9453 | { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 9454 | pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9455 | |
Alexey Dobriyan | 97a3253 | 2020-02-03 17:37:17 -0800 | [diff] [blame] | 9456 | proc_create("mdstat", S_IRUGO, NULL, &mdstat_proc_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9457 | } |
| 9458 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 9459 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9460 | { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 9461 | int ret = -ENOMEM; |
| 9462 | |
Tejun Heo | ada609e | 2011-01-25 14:35:54 +0100 | [diff] [blame] | 9463 | md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 9464 | if (!md_wq) |
| 9465 | goto err_wq; |
| 9466 | |
| 9467 | md_misc_wq = alloc_workqueue("md_misc", 0, 0); |
| 9468 | if (!md_misc_wq) |
| 9469 | goto err_misc_wq; |
| 9470 | |
| 9471 | if ((ret = register_blkdev(MD_MAJOR, "md")) < 0) |
| 9472 | goto err_md; |
| 9473 | |
| 9474 | if ((ret = register_blkdev(0, "mdp")) < 0) |
| 9475 | goto err_mdp; |
| 9476 | mdp_major = ret; |
| 9477 | |
NeilBrown | af5628f | 2014-07-31 13:54:54 +1000 | [diff] [blame] | 9478 | blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE, |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 9479 | md_probe, NULL, NULL); |
| 9480 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9481 | md_probe, NULL, NULL); |
| 9482 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9483 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 9484 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9485 | |
| 9486 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 9487 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9488 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 9489 | err_mdp: |
| 9490 | unregister_blkdev(MD_MAJOR, "md"); |
| 9491 | err_md: |
| 9492 | destroy_workqueue(md_misc_wq); |
| 9493 | err_misc_wq: |
| 9494 | destroy_workqueue(md_wq); |
| 9495 | err_wq: |
| 9496 | return ret; |
| 9497 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9498 | |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9499 | static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev) |
Goldwyn Rodrigues | 1d7e3e9 | 2014-06-07 01:53:00 -0500 | [diff] [blame] | 9500 | { |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9501 | struct mdp_superblock_1 *sb = page_address(rdev->sb_page); |
| 9502 | struct md_rdev *rdev2; |
| 9503 | int role, ret; |
| 9504 | char b[BDEVNAME_SIZE]; |
Goldwyn Rodrigues | 1d7e3e9 | 2014-06-07 01:53:00 -0500 | [diff] [blame] | 9505 | |
Guoqing Jiang | 818da59 | 2017-03-01 16:42:40 +0800 | [diff] [blame] | 9506 | /* |
| 9507 | * If size is changed in another node then we need to |
| 9508 | * do resize as well. |
| 9509 | */ |
| 9510 | if (mddev->dev_sectors != le64_to_cpu(sb->size)) { |
| 9511 | ret = mddev->pers->resize(mddev, le64_to_cpu(sb->size)); |
| 9512 | if (ret) |
| 9513 | pr_info("md-cluster: resize failed\n"); |
| 9514 | else |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 9515 | md_bitmap_update_sb(mddev->bitmap); |
Guoqing Jiang | 818da59 | 2017-03-01 16:42:40 +0800 | [diff] [blame] | 9516 | } |
| 9517 | |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9518 | /* Check for change of roles in the active devices */ |
| 9519 | rdev_for_each(rdev2, mddev) { |
| 9520 | if (test_bit(Faulty, &rdev2->flags)) |
| 9521 | continue; |
| 9522 | |
| 9523 | /* Check if the roles changed */ |
| 9524 | role = le16_to_cpu(sb->dev_roles[rdev2->desc_nr]); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 9525 | |
| 9526 | if (test_bit(Candidate, &rdev2->flags)) { |
| 9527 | if (role == 0xfffe) { |
| 9528 | pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2->bdev,b)); |
| 9529 | md_kick_rdev_from_array(rdev2); |
| 9530 | continue; |
| 9531 | } |
| 9532 | else |
| 9533 | clear_bit(Candidate, &rdev2->flags); |
| 9534 | } |
| 9535 | |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9536 | if (role != rdev2->raid_disk) { |
Guoqing Jiang | ca1e98e | 2018-10-18 16:37:45 +0800 | [diff] [blame] | 9537 | /* |
| 9538 | * got activated except reshape is happening. |
| 9539 | */ |
| 9540 | if (rdev2->raid_disk == -1 && role != 0xffff && |
| 9541 | !(le32_to_cpu(sb->feature_map) & |
| 9542 | MD_FEATURE_RESHAPE_ACTIVE)) { |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9543 | rdev2->saved_raid_disk = role; |
| 9544 | ret = remove_and_add_spares(mddev, rdev2); |
| 9545 | pr_info("Activated spare: %s\n", |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 9546 | bdevname(rdev2->bdev,b)); |
Guoqing Jiang | a578183 | 2016-05-02 11:33:14 -0400 | [diff] [blame] | 9547 | /* wakeup mddev->thread here, so array could |
| 9548 | * perform resync with the new activated disk */ |
| 9549 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 9550 | md_wakeup_thread(mddev->thread); |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9551 | } |
| 9552 | /* device faulty |
| 9553 | * We just want to do the minimum to mark the disk |
| 9554 | * as faulty. The recovery is performed by the |
| 9555 | * one who initiated the error. |
| 9556 | */ |
| 9557 | if ((role == 0xfffe) || (role == 0xfffd)) { |
| 9558 | md_error(mddev, rdev2); |
| 9559 | clear_bit(Blocked, &rdev2->flags); |
| 9560 | } |
| 9561 | } |
Goldwyn Rodrigues | 1d7e3e9 | 2014-06-07 01:53:00 -0500 | [diff] [blame] | 9562 | } |
| 9563 | |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 9564 | if (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) |
| 9565 | update_raid_disks(mddev, le32_to_cpu(sb->raid_disks)); |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9566 | |
Guoqing Jiang | 7564bed | 2018-10-18 16:37:42 +0800 | [diff] [blame] | 9567 | /* |
| 9568 | * Since mddev->delta_disks has already updated in update_raid_disks, |
| 9569 | * so it is time to check reshape. |
| 9570 | */ |
| 9571 | if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery) && |
| 9572 | (le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 9573 | /* |
| 9574 | * reshape is happening in the remote node, we need to |
| 9575 | * update reshape_position and call start_reshape. |
| 9576 | */ |
Christoph Hellwig | ed4d0a4e | 2019-04-04 18:56:10 +0200 | [diff] [blame] | 9577 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
Guoqing Jiang | 7564bed | 2018-10-18 16:37:42 +0800 | [diff] [blame] | 9578 | if (mddev->pers->update_reshape_pos) |
| 9579 | mddev->pers->update_reshape_pos(mddev); |
| 9580 | if (mddev->pers->start_reshape) |
| 9581 | mddev->pers->start_reshape(mddev); |
| 9582 | } else if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery) && |
| 9583 | mddev->reshape_position != MaxSector && |
| 9584 | !(le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 9585 | /* reshape is just done in another node. */ |
| 9586 | mddev->reshape_position = MaxSector; |
| 9587 | if (mddev->pers->update_reshape_pos) |
| 9588 | mddev->pers->update_reshape_pos(mddev); |
| 9589 | } |
| 9590 | |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9591 | /* Finally set the event to be up to date */ |
| 9592 | mddev->events = le64_to_cpu(sb->events); |
| 9593 | } |
| 9594 | |
| 9595 | static int read_rdev(struct mddev *mddev, struct md_rdev *rdev) |
| 9596 | { |
| 9597 | int err; |
| 9598 | struct page *swapout = rdev->sb_page; |
| 9599 | struct mdp_superblock_1 *sb; |
| 9600 | |
| 9601 | /* Store the sb page of the rdev in the swapout temporary |
| 9602 | * variable in case we err in the future |
| 9603 | */ |
| 9604 | rdev->sb_page = NULL; |
NeilBrown | 7f0f0d8 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 9605 | err = alloc_disk_sb(rdev); |
| 9606 | if (err == 0) { |
| 9607 | ClearPageUptodate(rdev->sb_page); |
| 9608 | rdev->sb_loaded = 0; |
| 9609 | err = super_types[mddev->major_version]. |
| 9610 | load_super(rdev, NULL, mddev->minor_version); |
| 9611 | } |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9612 | if (err < 0) { |
| 9613 | pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n", |
| 9614 | __func__, __LINE__, rdev->desc_nr, err); |
NeilBrown | 7f0f0d8 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 9615 | if (rdev->sb_page) |
| 9616 | put_page(rdev->sb_page); |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9617 | rdev->sb_page = swapout; |
| 9618 | rdev->sb_loaded = 1; |
| 9619 | return err; |
| 9620 | } |
| 9621 | |
| 9622 | sb = page_address(rdev->sb_page); |
| 9623 | /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET |
| 9624 | * is not set |
| 9625 | */ |
| 9626 | |
| 9627 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET)) |
| 9628 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 9629 | |
| 9630 | /* The other node finished recovery, call spare_active to set |
| 9631 | * device In_sync and mddev->degraded |
| 9632 | */ |
| 9633 | if (rdev->recovery_offset == MaxSector && |
| 9634 | !test_bit(In_sync, &rdev->flags) && |
| 9635 | mddev->pers->spare_active(mddev)) |
| 9636 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
| 9637 | |
| 9638 | put_page(swapout); |
| 9639 | return 0; |
| 9640 | } |
| 9641 | |
| 9642 | void md_reload_sb(struct mddev *mddev, int nr) |
| 9643 | { |
| 9644 | struct md_rdev *rdev; |
| 9645 | int err; |
| 9646 | |
| 9647 | /* Find the rdev */ |
| 9648 | rdev_for_each_rcu(rdev, mddev) { |
| 9649 | if (rdev->desc_nr == nr) |
| 9650 | break; |
| 9651 | } |
| 9652 | |
| 9653 | if (!rdev || rdev->desc_nr != nr) { |
| 9654 | pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr); |
| 9655 | return; |
| 9656 | } |
| 9657 | |
| 9658 | err = read_rdev(mddev, rdev); |
| 9659 | if (err < 0) |
| 9660 | return; |
| 9661 | |
| 9662 | check_sb_changes(mddev, rdev); |
| 9663 | |
| 9664 | /* Read all rdev's to update recovery_offset */ |
Guoqing Jiang | 0ea9924 | 2018-04-09 17:01:21 +0800 | [diff] [blame] | 9665 | rdev_for_each_rcu(rdev, mddev) { |
| 9666 | if (!test_bit(Faulty, &rdev->flags)) |
| 9667 | read_rdev(mddev, rdev); |
| 9668 | } |
Goldwyn Rodrigues | 1d7e3e9 | 2014-06-07 01:53:00 -0500 | [diff] [blame] | 9669 | } |
| 9670 | EXPORT_SYMBOL(md_reload_sb); |
| 9671 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9672 | #ifndef MODULE |
| 9673 | |
| 9674 | /* |
| 9675 | * Searches all registered partitions for autorun RAID arrays |
| 9676 | * at boot time. |
| 9677 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9678 | |
Cong Wang | 5b1f5bc3 | 2016-06-08 09:20:16 -0700 | [diff] [blame] | 9679 | static DEFINE_MUTEX(detected_devices_mutex); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9680 | static LIST_HEAD(all_detected_devices); |
| 9681 | struct detected_devices_node { |
| 9682 | struct list_head list; |
| 9683 | dev_t dev; |
| 9684 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9685 | |
| 9686 | void md_autodetect_dev(dev_t dev) |
| 9687 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9688 | struct detected_devices_node *node_detected_dev; |
| 9689 | |
| 9690 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 9691 | if (node_detected_dev) { |
| 9692 | node_detected_dev->dev = dev; |
Cong Wang | 5b1f5bc3 | 2016-06-08 09:20:16 -0700 | [diff] [blame] | 9693 | mutex_lock(&detected_devices_mutex); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9694 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
Cong Wang | 5b1f5bc3 | 2016-06-08 09:20:16 -0700 | [diff] [blame] | 9695 | mutex_unlock(&detected_devices_mutex); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9696 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9697 | } |
| 9698 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9699 | static void autostart_arrays(int part) |
| 9700 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 9701 | struct md_rdev *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9702 | struct detected_devices_node *node_detected_dev; |
| 9703 | dev_t dev; |
| 9704 | int i_scanned, i_passed; |
| 9705 | |
| 9706 | i_scanned = 0; |
| 9707 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9708 | |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 9709 | pr_info("md: Autodetecting RAID arrays.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9710 | |
Cong Wang | 5b1f5bc3 | 2016-06-08 09:20:16 -0700 | [diff] [blame] | 9711 | mutex_lock(&detected_devices_mutex); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9712 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 9713 | i_scanned++; |
| 9714 | node_detected_dev = list_entry(all_detected_devices.next, |
| 9715 | struct detected_devices_node, list); |
| 9716 | list_del(&node_detected_dev->list); |
| 9717 | dev = node_detected_dev->dev; |
| 9718 | kfree(node_detected_dev); |
Shaohua Li | 90bcf133 | 2016-09-14 14:26:54 -0700 | [diff] [blame] | 9719 | mutex_unlock(&detected_devices_mutex); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 9720 | rdev = md_import_device(dev,0, 90); |
Shaohua Li | 90bcf133 | 2016-09-14 14:26:54 -0700 | [diff] [blame] | 9721 | mutex_lock(&detected_devices_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9722 | if (IS_ERR(rdev)) |
| 9723 | continue; |
| 9724 | |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 9725 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9726 | continue; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 9727 | |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 9728 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9729 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9730 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9731 | } |
Cong Wang | 5b1f5bc3 | 2016-06-08 09:20:16 -0700 | [diff] [blame] | 9732 | mutex_unlock(&detected_devices_mutex); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9733 | |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 9734 | pr_debug("md: Scanned %d and added %d devices.\n", i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9735 | |
| 9736 | autorun_devices(part); |
| 9737 | } |
| 9738 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 9739 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9740 | |
| 9741 | static __exit void md_exit(void) |
| 9742 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 9743 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9744 | struct list_head *tmp; |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 9745 | int delay = 1; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 9746 | |
NeilBrown | af5628f | 2014-07-31 13:54:54 +1000 | [diff] [blame] | 9747 | blk_unregister_region(MKDEV(MD_MAJOR,0), 512); |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 9748 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9749 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 9750 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9751 | unregister_blkdev(mdp_major, "mdp"); |
| 9752 | unregister_reboot_notifier(&md_notifier); |
| 9753 | unregister_sysctl_table(raid_table_header); |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 9754 | |
| 9755 | /* We cannot unload the modules while some process is |
| 9756 | * waiting for us in select() or poll() - wake them up |
| 9757 | */ |
| 9758 | md_unloading = 1; |
| 9759 | while (waitqueue_active(&md_event_waiters)) { |
| 9760 | /* not safe to leave yet */ |
| 9761 | wake_up(&md_event_waiters); |
| 9762 | msleep(delay); |
| 9763 | delay += delay; |
| 9764 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9765 | remove_proc_entry("mdstat", NULL); |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 9766 | |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 9767 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9768 | export_array(mddev); |
NeilBrown | 9356863 | 2017-02-06 13:41:39 +1100 | [diff] [blame] | 9769 | mddev->ctime = 0; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 9770 | mddev->hold_active = 0; |
NeilBrown | 9356863 | 2017-02-06 13:41:39 +1100 | [diff] [blame] | 9771 | /* |
| 9772 | * for_each_mddev() will call mddev_put() at the end of each |
| 9773 | * iteration. As the mddev is now fully clear, this will |
| 9774 | * schedule the mddev for destruction by a workqueue, and the |
| 9775 | * destroy_workqueue() below will wait for that to complete. |
| 9776 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9777 | } |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 9778 | destroy_workqueue(md_misc_wq); |
| 9779 | destroy_workqueue(md_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9780 | } |
| 9781 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 9782 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9783 | module_exit(md_exit) |
| 9784 | |
Kees Cook | e4dca7b | 2017-10-17 19:04:42 -0700 | [diff] [blame] | 9785 | static int get_ro(char *buffer, const struct kernel_param *kp) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 9786 | { |
| 9787 | return sprintf(buffer, "%d", start_readonly); |
| 9788 | } |
Kees Cook | e4dca7b | 2017-10-17 19:04:42 -0700 | [diff] [blame] | 9789 | static int set_ro(const char *val, const struct kernel_param *kp) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 9790 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 9791 | return kstrtouint(val, 10, (unsigned int *)&start_readonly); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 9792 | } |
| 9793 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 9794 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 9795 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 9796 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | 78b6350 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 9797 | module_param(create_on_open, bool, S_IRUSR|S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 9798 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9799 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 9800 | MODULE_DESCRIPTION("MD RAID framework"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 9801 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 9802 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |