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> |
Christoph Hellwig | 74cc979c | 2020-03-24 08:25:19 +0100 | [diff] [blame] | 61 | #include <linux/raid/detect.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 62 | #include <linux/slab.h> |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 63 | #include <linux/percpu-refcount.h> |
Christoph Hellwig | c6a564ff | 2020-03-25 16:48:42 +0100 | [diff] [blame] | 64 | #include <linux/part_stat.h> |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 65 | |
Shaohua Li | 504634f | 2016-11-18 09:44:08 -0800 | [diff] [blame] | 66 | #include <trace/events/block.h> |
NeilBrown | 43b2e5d | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 67 | #include "md.h" |
Mike Snitzer | 935fe09 | 2017-10-10 17:02:41 -0400 | [diff] [blame] | 68 | #include "md-bitmap.h" |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 69 | #include "md-cluster.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 71 | /* pers_list is a list of registered personalities protected |
| 72 | * by pers_lock. |
| 73 | * pers_lock does extra service to protect accesses to |
| 74 | * mddev->thread when the mutex cannot be held. |
| 75 | */ |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 76 | static LIST_HEAD(pers_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | static DEFINE_SPINLOCK(pers_lock); |
| 78 | |
Kent Overstreet | 28dec87 | 2018-06-07 20:52:54 -0400 | [diff] [blame] | 79 | static struct kobj_type md_ktype; |
| 80 | |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 81 | struct md_cluster_operations *md_cluster_ops; |
Goldwyn Rodrigues | 589a1c4 | 2014-06-07 02:39:37 -0500 | [diff] [blame] | 82 | EXPORT_SYMBOL(md_cluster_ops); |
Christoph Hellwig | 2b598ee | 2019-04-04 18:56:14 +0200 | [diff] [blame] | 83 | static struct module *md_cluster_mod; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 84 | |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 85 | static DECLARE_WAIT_QUEUE_HEAD(resync_wait); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 86 | static struct workqueue_struct *md_wq; |
| 87 | static struct workqueue_struct *md_misc_wq; |
Guoqing Jiang | cc1ffe6 | 2020-04-04 23:57:08 +0200 | [diff] [blame] | 88 | static struct workqueue_struct *md_rdev_misc_wq; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 89 | |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 90 | static int remove_and_add_spares(struct mddev *mddev, |
| 91 | struct md_rdev *this); |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 92 | static void mddev_detach(struct mddev *mddev); |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 93 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | /* |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 95 | * Default number of read corrections we'll attempt on an rdev |
| 96 | * before ejecting it from the array. We divide the read error |
| 97 | * count by 2 for every hour elapsed between read errors. |
| 98 | */ |
| 99 | #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20 |
Zhao Heming | 7c9d5c5 | 2020-07-21 02:08:52 +0800 | [diff] [blame] | 100 | /* Default safemode delay: 200 msec */ |
| 101 | #define DEFAULT_SAFEMODE_DELAY ((200 * HZ)/1000 +1) |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 102 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit' |
| 104 | * is 1000 KB/sec, so the extra system load does not show up that much. |
| 105 | * Increase it if you want to have more _guaranteed_ speed. Note that |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 106 | * the RAID driver will use the maximum available bandwidth if the IO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | * subsystem is idle. There is also an 'absolute maximum' reconstruction |
| 108 | * speed limit - in case reconstruction slows down your system despite |
| 109 | * idle IO detection. |
| 110 | * |
| 111 | * 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] | 112 | * or /sys/block/mdX/md/sync_speed_{min,max} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | */ |
| 114 | |
| 115 | static int sysctl_speed_limit_min = 1000; |
| 116 | static int sysctl_speed_limit_max = 200000; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 117 | static inline int speed_min(struct mddev *mddev) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 118 | { |
| 119 | return mddev->sync_speed_min ? |
| 120 | mddev->sync_speed_min : sysctl_speed_limit_min; |
| 121 | } |
| 122 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 123 | static inline int speed_max(struct mddev *mddev) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 124 | { |
| 125 | return mddev->sync_speed_max ? |
| 126 | mddev->sync_speed_max : sysctl_speed_limit_max; |
| 127 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 129 | static void rdev_uninit_serial(struct md_rdev *rdev) |
Guoqing Jiang | 3e148a3 | 2019-06-19 17:30:46 +0800 | [diff] [blame] | 130 | { |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 131 | if (!test_and_clear_bit(CollisionCheck, &rdev->flags)) |
| 132 | return; |
Guoqing Jiang | 3e148a3 | 2019-06-19 17:30:46 +0800 | [diff] [blame] | 133 | |
Guoqing Jiang | 025471f | 2019-12-23 10:49:01 +0100 | [diff] [blame] | 134 | kvfree(rdev->serial); |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 135 | rdev->serial = NULL; |
Guoqing Jiang | 3e148a3 | 2019-06-19 17:30:46 +0800 | [diff] [blame] | 136 | } |
| 137 | |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 138 | static void rdevs_uninit_serial(struct mddev *mddev) |
Guoqing Jiang | 11d3a9f | 2019-12-23 10:48:55 +0100 | [diff] [blame] | 139 | { |
| 140 | struct md_rdev *rdev; |
| 141 | |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 142 | rdev_for_each(rdev, mddev) |
| 143 | rdev_uninit_serial(rdev); |
| 144 | } |
| 145 | |
| 146 | static int rdev_init_serial(struct md_rdev *rdev) |
| 147 | { |
Guoqing Jiang | 025471f | 2019-12-23 10:49:01 +0100 | [diff] [blame] | 148 | /* serial_nums equals with BARRIER_BUCKETS_NR */ |
| 149 | int i, serial_nums = 1 << ((PAGE_SHIFT - ilog2(sizeof(atomic_t)))); |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 150 | struct serial_in_rdev *serial = NULL; |
| 151 | |
| 152 | if (test_bit(CollisionCheck, &rdev->flags)) |
| 153 | return 0; |
| 154 | |
Guoqing Jiang | 025471f | 2019-12-23 10:49:01 +0100 | [diff] [blame] | 155 | serial = kvmalloc(sizeof(struct serial_in_rdev) * serial_nums, |
| 156 | GFP_KERNEL); |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 157 | if (!serial) |
| 158 | return -ENOMEM; |
| 159 | |
Guoqing Jiang | 025471f | 2019-12-23 10:49:01 +0100 | [diff] [blame] | 160 | for (i = 0; i < serial_nums; i++) { |
| 161 | struct serial_in_rdev *serial_tmp = &serial[i]; |
| 162 | |
| 163 | spin_lock_init(&serial_tmp->serial_lock); |
| 164 | serial_tmp->serial_rb = RB_ROOT_CACHED; |
| 165 | init_waitqueue_head(&serial_tmp->serial_io_wait); |
| 166 | } |
| 167 | |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 168 | rdev->serial = serial; |
| 169 | set_bit(CollisionCheck, &rdev->flags); |
| 170 | |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | static int rdevs_init_serial(struct mddev *mddev) |
| 175 | { |
| 176 | struct md_rdev *rdev; |
| 177 | int ret = 0; |
| 178 | |
Guoqing Jiang | 11d3a9f | 2019-12-23 10:48:55 +0100 | [diff] [blame] | 179 | rdev_for_each(rdev, mddev) { |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 180 | ret = rdev_init_serial(rdev); |
| 181 | if (ret) |
| 182 | break; |
Guoqing Jiang | 11d3a9f | 2019-12-23 10:48:55 +0100 | [diff] [blame] | 183 | } |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 184 | |
| 185 | /* Free all resources if pool is not existed */ |
| 186 | if (ret && !mddev->serial_info_pool) |
| 187 | rdevs_uninit_serial(mddev); |
| 188 | |
| 189 | return ret; |
Guoqing Jiang | 11d3a9f | 2019-12-23 10:48:55 +0100 | [diff] [blame] | 190 | } |
| 191 | |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 192 | /* |
Guoqing Jiang | de31ee9 | 2019-12-23 10:48:57 +0100 | [diff] [blame] | 193 | * rdev needs to enable serial stuffs if it meets the conditions: |
| 194 | * 1. it is multi-queue device flaged with writemostly. |
| 195 | * 2. the write-behind mode is enabled. |
| 196 | */ |
| 197 | static int rdev_need_serial(struct md_rdev *rdev) |
| 198 | { |
| 199 | return (rdev && rdev->mddev->bitmap_info.max_write_behind > 0 && |
Christoph Hellwig | e556f6b | 2020-06-26 10:01:56 +0200 | [diff] [blame] | 200 | rdev->bdev->bd_disk->queue->nr_hw_queues != 1 && |
Guoqing Jiang | de31ee9 | 2019-12-23 10:48:57 +0100 | [diff] [blame] | 201 | test_bit(WriteMostly, &rdev->flags)); |
| 202 | } |
| 203 | |
| 204 | /* |
| 205 | * Init resource for rdev(s), then create serial_info_pool if: |
| 206 | * 1. rdev is the first device which return true from rdev_enable_serial. |
| 207 | * 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] | 208 | */ |
Guoqing Jiang | 404659c | 2019-12-23 10:48:53 +0100 | [diff] [blame] | 209 | void mddev_create_serial_pool(struct mddev *mddev, struct md_rdev *rdev, |
Guoqing Jiang | 11d3a9f | 2019-12-23 10:48:55 +0100 | [diff] [blame] | 210 | bool is_suspend) |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 211 | { |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 212 | int ret = 0; |
| 213 | |
Guoqing Jiang | de31ee9 | 2019-12-23 10:48:57 +0100 | [diff] [blame] | 214 | if (rdev && !rdev_need_serial(rdev) && |
| 215 | !test_bit(CollisionCheck, &rdev->flags)) |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 216 | return; |
| 217 | |
Guoqing Jiang | de31ee9 | 2019-12-23 10:48:57 +0100 | [diff] [blame] | 218 | if (!is_suspend) |
| 219 | mddev_suspend(mddev); |
| 220 | |
| 221 | if (!rdev) |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 222 | ret = rdevs_init_serial(mddev); |
Guoqing Jiang | de31ee9 | 2019-12-23 10:48:57 +0100 | [diff] [blame] | 223 | else |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 224 | ret = rdev_init_serial(rdev); |
| 225 | if (ret) |
| 226 | goto abort; |
Guoqing Jiang | de31ee9 | 2019-12-23 10:48:57 +0100 | [diff] [blame] | 227 | |
Guoqing Jiang | 404659c | 2019-12-23 10:48:53 +0100 | [diff] [blame] | 228 | if (mddev->serial_info_pool == NULL) { |
Coly Li | 3024ba2 | 2020-04-09 22:17:23 +0800 | [diff] [blame] | 229 | /* |
| 230 | * already in memalloc noio context by |
| 231 | * mddev_suspend() |
| 232 | */ |
Guoqing Jiang | 404659c | 2019-12-23 10:48:53 +0100 | [diff] [blame] | 233 | mddev->serial_info_pool = |
| 234 | mempool_create_kmalloc_pool(NR_SERIAL_INFOS, |
| 235 | sizeof(struct serial_info)); |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 236 | if (!mddev->serial_info_pool) { |
| 237 | rdevs_uninit_serial(mddev); |
Guoqing Jiang | 404659c | 2019-12-23 10:48:53 +0100 | [diff] [blame] | 238 | pr_err("can't alloc memory pool for serialization\n"); |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 239 | } |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 240 | } |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 241 | |
| 242 | abort: |
Guoqing Jiang | de31ee9 | 2019-12-23 10:48:57 +0100 | [diff] [blame] | 243 | if (!is_suspend) |
| 244 | mddev_resume(mddev); |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 245 | } |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 246 | |
| 247 | /* |
Guoqing Jiang | de31ee9 | 2019-12-23 10:48:57 +0100 | [diff] [blame] | 248 | * Free resource from rdev(s), and destroy serial_info_pool under conditions: |
| 249 | * 1. rdev is the last device flaged with CollisionCheck. |
| 250 | * 2. when bitmap is destroyed while policy is not enabled. |
| 251 | * 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] | 252 | */ |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 253 | void mddev_destroy_serial_pool(struct mddev *mddev, struct md_rdev *rdev, |
| 254 | bool is_suspend) |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 255 | { |
Guoqing Jiang | 11d3a9f | 2019-12-23 10:48:55 +0100 | [diff] [blame] | 256 | if (rdev && !test_bit(CollisionCheck, &rdev->flags)) |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 257 | return; |
| 258 | |
Guoqing Jiang | 404659c | 2019-12-23 10:48:53 +0100 | [diff] [blame] | 259 | if (mddev->serial_info_pool) { |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 260 | struct md_rdev *temp; |
Guoqing Jiang | de31ee9 | 2019-12-23 10:48:57 +0100 | [diff] [blame] | 261 | int num = 0; /* used to track if other rdevs need the pool */ |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 262 | |
Guoqing Jiang | 11d3a9f | 2019-12-23 10:48:55 +0100 | [diff] [blame] | 263 | if (!is_suspend) |
| 264 | mddev_suspend(mddev); |
| 265 | rdev_for_each(temp, mddev) { |
| 266 | if (!rdev) { |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 267 | if (!mddev->serialize_policy || |
| 268 | !rdev_need_serial(temp)) |
| 269 | rdev_uninit_serial(temp); |
Guoqing Jiang | de31ee9 | 2019-12-23 10:48:57 +0100 | [diff] [blame] | 270 | else |
| 271 | num++; |
| 272 | } else if (temp != rdev && |
| 273 | test_bit(CollisionCheck, &temp->flags)) |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 274 | num++; |
Guoqing Jiang | 11d3a9f | 2019-12-23 10:48:55 +0100 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | if (rdev) |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 278 | rdev_uninit_serial(rdev); |
Guoqing Jiang | de31ee9 | 2019-12-23 10:48:57 +0100 | [diff] [blame] | 279 | |
| 280 | if (num) |
| 281 | pr_info("The mempool could be used by other devices\n"); |
| 282 | else { |
Guoqing Jiang | 404659c | 2019-12-23 10:48:53 +0100 | [diff] [blame] | 283 | mempool_destroy(mddev->serial_info_pool); |
| 284 | mddev->serial_info_pool = NULL; |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 285 | } |
Guoqing Jiang | 11d3a9f | 2019-12-23 10:48:55 +0100 | [diff] [blame] | 286 | if (!is_suspend) |
| 287 | mddev_resume(mddev); |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | static struct ctl_table_header *raid_table_header; |
| 292 | |
Joe Perches | 82592c3 | 2013-11-14 15:16:18 +1100 | [diff] [blame] | 293 | static struct ctl_table raid_table[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | .procname = "speed_limit_min", |
| 296 | .data = &sysctl_speed_limit_min, |
| 297 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 298 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 299 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | }, |
| 301 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | .procname = "speed_limit_max", |
| 303 | .data = &sysctl_speed_limit_max, |
| 304 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 305 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 306 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 308 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | }; |
| 310 | |
Joe Perches | 82592c3 | 2013-11-14 15:16:18 +1100 | [diff] [blame] | 311 | static struct ctl_table raid_dir_table[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | .procname = "raid", |
| 314 | .maxlen = 0, |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 315 | .mode = S_IRUGO|S_IXUGO, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | .child = raid_table, |
| 317 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 318 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | }; |
| 320 | |
Joe Perches | 82592c3 | 2013-11-14 15:16:18 +1100 | [diff] [blame] | 321 | static struct ctl_table raid_root_table[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | .procname = "dev", |
| 324 | .maxlen = 0, |
| 325 | .mode = 0555, |
| 326 | .child = raid_dir_table, |
| 327 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 328 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | }; |
| 330 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 331 | static int start_readonly; |
| 332 | |
NeilBrown | 78b6350 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 333 | /* |
| 334 | * The original mechanism for creating an md device is to create |
| 335 | * a device node in /dev and to open it. This causes races with device-close. |
| 336 | * The preferred method is to write to the "new_array" module parameter. |
| 337 | * This can avoid races. |
| 338 | * Setting create_on_open to false disables the original mechanism |
| 339 | * so all the races disappear. |
| 340 | */ |
| 341 | static bool create_on_open = true; |
| 342 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 343 | struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs, |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 344 | struct mddev *mddev) |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 345 | { |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 346 | if (!mddev || !bioset_initialized(&mddev->bio_set)) |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 347 | return bio_alloc(gfp_mask, nr_iovecs); |
| 348 | |
Marcos Paulo de Souza | 6251691 | 2019-01-14 06:31:56 -0700 | [diff] [blame] | 349 | return bio_alloc_bioset(gfp_mask, nr_iovecs, &mddev->bio_set); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 350 | } |
| 351 | EXPORT_SYMBOL_GPL(bio_alloc_mddev); |
| 352 | |
NeilBrown | 5a85071 | 2017-06-21 09:12:21 +1000 | [diff] [blame] | 353 | static struct bio *md_bio_alloc_sync(struct mddev *mddev) |
| 354 | { |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 355 | if (!mddev || !bioset_initialized(&mddev->sync_set)) |
NeilBrown | 5a85071 | 2017-06-21 09:12:21 +1000 | [diff] [blame] | 356 | return bio_alloc(GFP_NOIO, 1); |
| 357 | |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 358 | return bio_alloc_bioset(GFP_NOIO, 1, &mddev->sync_set); |
NeilBrown | 5a85071 | 2017-06-21 09:12:21 +1000 | [diff] [blame] | 359 | } |
| 360 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | /* |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 362 | * We have a system wide 'event count' that is incremented |
| 363 | * on any 'interesting' event, and readers of /proc/mdstat |
| 364 | * can use 'poll' or 'select' to find out when the event |
| 365 | * count increases. |
| 366 | * |
| 367 | * Events are: |
| 368 | * start array, stop array, error, add device, remove device, |
| 369 | * start build, activate spare |
| 370 | */ |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 371 | static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 372 | static atomic_t md_event_count; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 373 | void md_new_event(struct mddev *mddev) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 374 | { |
| 375 | atomic_inc(&md_event_count); |
| 376 | wake_up(&md_event_waiters); |
| 377 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 378 | EXPORT_SYMBOL_GPL(md_new_event); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 379 | |
| 380 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | * Enables to iterate over all existing md arrays |
| 382 | * all_mddevs_lock protects this list. |
| 383 | */ |
| 384 | static LIST_HEAD(all_mddevs); |
| 385 | static DEFINE_SPINLOCK(all_mddevs_lock); |
| 386 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | /* |
| 388 | * iterates through all used mddevs in the system. |
| 389 | * We take care to grab the all_mddevs_lock whenever navigating |
| 390 | * the list, and to always hold a refcount when unlocked. |
| 391 | * Any code which breaks out of this loop while own |
| 392 | * a reference to the current mddev and must mddev_put it. |
| 393 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 394 | #define for_each_mddev(_mddev,_tmp) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | \ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 396 | for (({ spin_lock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 397 | _tmp = all_mddevs.next; \ |
| 398 | _mddev = NULL;}); \ |
| 399 | ({ if (_tmp != &all_mddevs) \ |
| 400 | mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | spin_unlock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 402 | if (_mddev) mddev_put(_mddev); \ |
| 403 | _mddev = list_entry(_tmp, struct mddev, all_mddevs); \ |
| 404 | _tmp != &all_mddevs;}); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | ({ spin_lock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 406 | _tmp = _tmp->next;}) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | ) |
| 408 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 409 | /* Rather than calling directly into the personality make_request function, |
| 410 | * IO requests come here first so that we can check if the device is |
| 411 | * being suspended pending a reconfiguration. |
| 412 | * We hold a refcount over the call to ->make_request. By the time that |
| 413 | * call has finished, the bio has been linked into some internal structure |
| 414 | * and so is visible to ->quiesce(), so we don't need the refcount any more. |
| 415 | */ |
NeilBrown | b3143b9 | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 416 | static bool is_suspended(struct mddev *mddev, struct bio *bio) |
| 417 | { |
| 418 | if (mddev->suspended) |
| 419 | return true; |
| 420 | if (bio_data_dir(bio) != WRITE) |
| 421 | return false; |
| 422 | if (mddev->suspend_lo >= mddev->suspend_hi) |
| 423 | return false; |
| 424 | if (bio->bi_iter.bi_sector >= mddev->suspend_hi) |
| 425 | return false; |
| 426 | if (bio_end_sector(bio) < mddev->suspend_lo) |
| 427 | return false; |
| 428 | return true; |
| 429 | } |
| 430 | |
Shaohua Li | 393debc | 2017-09-21 10:23:35 -0700 | [diff] [blame] | 431 | void md_handle_request(struct mddev *mddev, struct bio *bio) |
| 432 | { |
| 433 | check_suspended: |
| 434 | rcu_read_lock(); |
NeilBrown | b3143b9 | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 435 | if (is_suspended(mddev, bio)) { |
Shaohua Li | 393debc | 2017-09-21 10:23:35 -0700 | [diff] [blame] | 436 | DEFINE_WAIT(__wait); |
| 437 | for (;;) { |
| 438 | prepare_to_wait(&mddev->sb_wait, &__wait, |
| 439 | TASK_UNINTERRUPTIBLE); |
NeilBrown | b3143b9 | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 440 | if (!is_suspended(mddev, bio)) |
Shaohua Li | 393debc | 2017-09-21 10:23:35 -0700 | [diff] [blame] | 441 | break; |
| 442 | rcu_read_unlock(); |
| 443 | schedule(); |
| 444 | rcu_read_lock(); |
| 445 | } |
| 446 | finish_wait(&mddev->sb_wait, &__wait); |
| 447 | } |
| 448 | atomic_inc(&mddev->active_io); |
| 449 | rcu_read_unlock(); |
| 450 | |
| 451 | if (!mddev->pers->make_request(mddev, bio)) { |
| 452 | atomic_dec(&mddev->active_io); |
| 453 | wake_up(&mddev->sb_wait); |
| 454 | goto check_suspended; |
| 455 | } |
| 456 | |
| 457 | if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended) |
| 458 | wake_up(&mddev->sb_wait); |
| 459 | } |
| 460 | EXPORT_SYMBOL(md_handle_request); |
| 461 | |
Artur Paszkiewicz | 41d2d84 | 2020-07-03 11:13:09 +0200 | [diff] [blame] | 462 | struct md_io { |
| 463 | struct mddev *mddev; |
| 464 | bio_end_io_t *orig_bi_end_io; |
| 465 | void *orig_bi_private; |
| 466 | unsigned long start_time; |
Christoph Hellwig | 8446fe9 | 2020-11-24 09:36:54 +0100 | [diff] [blame] | 467 | struct block_device *part; |
Artur Paszkiewicz | 41d2d84 | 2020-07-03 11:13:09 +0200 | [diff] [blame] | 468 | }; |
| 469 | |
| 470 | static void md_end_io(struct bio *bio) |
| 471 | { |
| 472 | struct md_io *md_io = bio->bi_private; |
| 473 | struct mddev *mddev = md_io->mddev; |
| 474 | |
Song Liu | 00fe60e | 2020-08-31 15:27:24 -0700 | [diff] [blame] | 475 | part_end_io_acct(md_io->part, bio, md_io->start_time); |
Artur Paszkiewicz | 41d2d84 | 2020-07-03 11:13:09 +0200 | [diff] [blame] | 476 | |
| 477 | bio->bi_end_io = md_io->orig_bi_end_io; |
| 478 | bio->bi_private = md_io->orig_bi_private; |
| 479 | |
| 480 | mempool_free(md_io, &mddev->md_io_pool); |
| 481 | |
| 482 | if (bio->bi_end_io) |
| 483 | bio->bi_end_io(bio); |
| 484 | } |
| 485 | |
Christoph Hellwig | c62b37d | 2020-07-01 10:59:43 +0200 | [diff] [blame] | 486 | static blk_qc_t md_submit_bio(struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | { |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 488 | const int rw = bio_data_dir(bio); |
Christoph Hellwig | e4fc5a7 | 2020-05-08 18:15:14 +0200 | [diff] [blame] | 489 | struct mddev *mddev = bio->bi_disk->private_data; |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 490 | |
Colin Ian King | 9a5a859 | 2020-07-02 12:35:02 +0100 | [diff] [blame] | 491 | if (mddev == NULL || mddev->pers == NULL) { |
| 492 | bio_io_error(bio); |
| 493 | return BLK_QC_T_NONE; |
| 494 | } |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 495 | |
Guilherme G. Piccoli | 62f7b19 | 2019-09-03 16:49:00 -0300 | [diff] [blame] | 496 | if (unlikely(test_bit(MD_BROKEN, &mddev->flags)) && (rw == WRITE)) { |
| 497 | bio_io_error(bio); |
| 498 | return BLK_QC_T_NONE; |
| 499 | } |
| 500 | |
Christoph Hellwig | f695ca3 | 2020-07-01 10:59:39 +0200 | [diff] [blame] | 501 | blk_queue_split(&bio); |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 502 | |
Sebastian Riemer | bbfa57c | 2013-02-21 13:28:09 +1100 | [diff] [blame] | 503 | if (mddev->ro == 1 && unlikely(rw == WRITE)) { |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 504 | if (bio_sectors(bio) != 0) |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 505 | bio->bi_status = BLK_STS_IOERR; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 506 | bio_endio(bio); |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 507 | return BLK_QC_T_NONE; |
Sebastian Riemer | bbfa57c | 2013-02-21 13:28:09 +1100 | [diff] [blame] | 508 | } |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 509 | |
Artur Paszkiewicz | 41d2d84 | 2020-07-03 11:13:09 +0200 | [diff] [blame] | 510 | if (bio->bi_end_io != md_end_io) { |
| 511 | struct md_io *md_io; |
| 512 | |
| 513 | md_io = mempool_alloc(&mddev->md_io_pool, GFP_NOIO); |
| 514 | md_io->mddev = mddev; |
| 515 | md_io->orig_bi_end_io = bio->bi_end_io; |
| 516 | md_io->orig_bi_private = bio->bi_private; |
| 517 | |
| 518 | bio->bi_end_io = md_end_io; |
| 519 | bio->bi_private = md_io; |
| 520 | |
Song Liu | 00fe60e | 2020-08-31 15:27:24 -0700 | [diff] [blame] | 521 | md_io->start_time = part_start_io_acct(mddev->gendisk, |
| 522 | &md_io->part, bio); |
Artur Paszkiewicz | 41d2d84 | 2020-07-03 11:13:09 +0200 | [diff] [blame] | 523 | } |
| 524 | |
Shaohua Li | 9c573de | 2016-04-25 16:52:38 -0700 | [diff] [blame] | 525 | /* bio could be mergeable after passing to underlayer */ |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 526 | bio->bi_opf &= ~REQ_NOMERGE; |
Shaohua Li | 393debc | 2017-09-21 10:23:35 -0700 | [diff] [blame] | 527 | |
| 528 | md_handle_request(mddev, bio); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 529 | |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 530 | return BLK_QC_T_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | } |
| 532 | |
NeilBrown | 9e35b99 | 2010-04-06 14:23:02 +1000 | [diff] [blame] | 533 | /* mddev_suspend makes sure no new requests are submitted |
| 534 | * to the device, and that any requests that have been submitted |
| 535 | * are completely handled. |
NeilBrown | afa0f55 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 536 | * Once mddev_detach() is called and completes, the module will be |
| 537 | * completely unused. |
NeilBrown | 9e35b99 | 2010-04-06 14:23:02 +1000 | [diff] [blame] | 538 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 539 | void mddev_suspend(struct mddev *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 540 | { |
Heinz Mauelshagen | 092398d | 2016-05-03 19:43:57 +0200 | [diff] [blame] | 541 | WARN_ON_ONCE(mddev->thread && current == mddev->thread->tsk); |
NeilBrown | 4d5324f | 2017-10-19 12:17:16 +1100 | [diff] [blame] | 542 | lockdep_assert_held(&mddev->reconfig_mutex); |
Mikulas Patocka | 0dc10e5 | 2015-12-18 15:19:16 +1100 | [diff] [blame] | 543 | if (mddev->suspended++) |
| 544 | return; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 545 | synchronize_rcu(); |
NeilBrown | cc27b0c | 2017-06-05 16:49:39 +1000 | [diff] [blame] | 546 | wake_up(&mddev->sb_wait); |
NeilBrown | 35bfc52 | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 547 | set_bit(MD_ALLOW_SB_UPDATE, &mddev->flags); |
| 548 | smp_mb__after_atomic(); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 549 | wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0); |
| 550 | mddev->pers->quiesce(mddev, 1); |
NeilBrown | 35bfc52 | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 551 | clear_bit_unlock(MD_ALLOW_SB_UPDATE, &mddev->flags); |
| 552 | wait_event(mddev->sb_wait, !test_bit(MD_UPDATING_SB, &mddev->flags)); |
Jonathan Brassow | 0d9f4f1 | 2012-05-16 04:06:14 -0500 | [diff] [blame] | 553 | |
| 554 | del_timer_sync(&mddev->safemode_timer); |
Coly Li | 78f57ef | 2020-04-09 22:17:20 +0800 | [diff] [blame] | 555 | /* restrict memory reclaim I/O during raid array is suspend */ |
| 556 | mddev->noio_flag = memalloc_noio_save(); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 557 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 558 | EXPORT_SYMBOL_GPL(mddev_suspend); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 559 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 560 | void mddev_resume(struct mddev *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 561 | { |
Coly Li | 78f57ef | 2020-04-09 22:17:20 +0800 | [diff] [blame] | 562 | /* entred the memalloc scope from mddev_suspend() */ |
| 563 | memalloc_noio_restore(mddev->noio_flag); |
NeilBrown | 4d5324f | 2017-10-19 12:17:16 +1100 | [diff] [blame] | 564 | lockdep_assert_held(&mddev->reconfig_mutex); |
Mikulas Patocka | 0dc10e5 | 2015-12-18 15:19:16 +1100 | [diff] [blame] | 565 | if (--mddev->suspended) |
| 566 | return; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 567 | wake_up(&mddev->sb_wait); |
| 568 | mddev->pers->quiesce(mddev, 0); |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 569 | |
Jonathan Brassow | 47525e5 | 2012-05-22 13:55:29 +1000 | [diff] [blame] | 570 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 571 | md_wakeup_thread(mddev->thread); |
| 572 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 573 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 574 | EXPORT_SYMBOL_GPL(mddev_resume); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 575 | |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 576 | /* |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 577 | * Generic flush handling for md |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 578 | */ |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 579 | |
| 580 | static void md_end_flush(struct bio *bio) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 581 | { |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 582 | struct md_rdev *rdev = bio->bi_private; |
| 583 | struct mddev *mddev = rdev->mddev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 584 | |
| 585 | rdev_dec_pending(rdev, mddev); |
| 586 | |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 587 | if (atomic_dec_and_test(&mddev->flush_pending)) { |
| 588 | /* The pre-request flush has finished */ |
| 589 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 590 | } |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 591 | bio_put(bio); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 592 | } |
| 593 | |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 594 | static void md_submit_flush_data(struct work_struct *ws); |
| 595 | |
| 596 | static void submit_flushes(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 597 | { |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 598 | struct mddev *mddev = container_of(ws, struct mddev, flush_work); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 599 | struct md_rdev *rdev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 600 | |
NeilBrown | 2bc13b8 | 2019-03-29 10:46:17 -0700 | [diff] [blame] | 601 | mddev->start_flush = ktime_get_boottime(); |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 602 | INIT_WORK(&mddev->flush_work, md_submit_flush_data); |
| 603 | atomic_set(&mddev->flush_pending, 1); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 604 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 605 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 606 | if (rdev->raid_disk >= 0 && |
| 607 | !test_bit(Faulty, &rdev->flags)) { |
| 608 | /* Take two references, one is dropped |
| 609 | * when request finishes, one after |
| 610 | * we reclaim rcu_read_lock |
| 611 | */ |
| 612 | struct bio *bi; |
| 613 | atomic_inc(&rdev->nr_pending); |
| 614 | atomic_inc(&rdev->nr_pending); |
| 615 | rcu_read_unlock(); |
Shaohua Li | b5e1b8c | 2012-05-21 09:26:59 +1000 | [diff] [blame] | 616 | bi = bio_alloc_mddev(GFP_NOIO, 0, mddev); |
Xiao Ni | 5a409b4 | 2018-05-21 11:49:54 +0800 | [diff] [blame] | 617 | bi->bi_end_io = md_end_flush; |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 618 | bi->bi_private = rdev; |
| 619 | bio_set_dev(bi, rdev->bdev); |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 620 | bi->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH; |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 621 | atomic_inc(&mddev->flush_pending); |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 622 | submit_bio(bi); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 623 | rcu_read_lock(); |
| 624 | rdev_dec_pending(rdev, mddev); |
| 625 | } |
| 626 | rcu_read_unlock(); |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 627 | if (atomic_dec_and_test(&mddev->flush_pending)) |
| 628 | queue_work(md_wq, &mddev->flush_work); |
| 629 | } |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 630 | |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 631 | static void md_submit_flush_data(struct work_struct *ws) |
| 632 | { |
| 633 | struct mddev *mddev = container_of(ws, struct mddev, flush_work); |
| 634 | struct bio *bio = mddev->flush_bio; |
| 635 | |
| 636 | /* |
| 637 | * must reset flush_bio before calling into md_handle_request to avoid a |
| 638 | * deadlock, because other bios passed md_handle_request suspend check |
| 639 | * could wait for this and below md_handle_request could wait for those |
| 640 | * bios because of suspend check |
| 641 | */ |
Pankaj Gupta | 81ba3c2 | 2020-11-11 06:16:56 +0100 | [diff] [blame] | 642 | mddev->prev_flush_start = mddev->start_flush; |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 643 | mddev->flush_bio = NULL; |
| 644 | wake_up(&mddev->sb_wait); |
| 645 | |
| 646 | if (bio->bi_iter.bi_size == 0) { |
| 647 | /* an empty barrier - all done */ |
| 648 | bio_endio(bio); |
| 649 | } else { |
| 650 | bio->bi_opf &= ~REQ_PREFLUSH; |
| 651 | md_handle_request(mddev, bio); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 652 | } |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 653 | } |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 654 | |
David Jeffery | 775d783 | 2019-09-16 13:15:14 -0400 | [diff] [blame] | 655 | /* |
| 656 | * Manages consolidation of flushes and submitting any flushes needed for |
| 657 | * a bio with REQ_PREFLUSH. Returns true if the bio is finished or is |
| 658 | * being finished in another context. Returns false if the flushing is |
| 659 | * complete but still needs the I/O portion of the bio to be processed. |
| 660 | */ |
| 661 | bool md_flush_request(struct mddev *mddev, struct bio *bio) |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 662 | { |
Pankaj Gupta | 81ba3c2 | 2020-11-11 06:16:56 +0100 | [diff] [blame] | 663 | ktime_t req_start = ktime_get_boottime(); |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 664 | spin_lock_irq(&mddev->lock); |
Pankaj Gupta | 204d1a6 | 2020-11-11 06:16:57 +0100 | [diff] [blame] | 665 | /* flush requests wait until ongoing flush completes, |
| 666 | * hence coalescing all the pending requests. |
| 667 | */ |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 668 | wait_event_lock_irq(mddev->sb_wait, |
NeilBrown | 2bc13b8 | 2019-03-29 10:46:17 -0700 | [diff] [blame] | 669 | !mddev->flush_bio || |
Pankaj Gupta | a23f2aa | 2020-11-11 06:16:58 +0100 | [diff] [blame] | 670 | ktime_before(req_start, mddev->prev_flush_start), |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 671 | mddev->lock); |
Pankaj Gupta | 204d1a6 | 2020-11-11 06:16:57 +0100 | [diff] [blame] | 672 | /* new request after previous flush is completed */ |
Pankaj Gupta | a23f2aa | 2020-11-11 06:16:58 +0100 | [diff] [blame] | 673 | if (ktime_after(req_start, mddev->prev_flush_start)) { |
NeilBrown | 2bc13b8 | 2019-03-29 10:46:17 -0700 | [diff] [blame] | 674 | WARN_ON(mddev->flush_bio); |
| 675 | mddev->flush_bio = bio; |
| 676 | bio = NULL; |
| 677 | } |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 678 | spin_unlock_irq(&mddev->lock); |
| 679 | |
NeilBrown | 2bc13b8 | 2019-03-29 10:46:17 -0700 | [diff] [blame] | 680 | if (!bio) { |
| 681 | INIT_WORK(&mddev->flush_work, submit_flushes); |
| 682 | queue_work(md_wq, &mddev->flush_work); |
| 683 | } else { |
| 684 | /* flush was performed for some other bio while we waited. */ |
| 685 | if (bio->bi_iter.bi_size == 0) |
| 686 | /* an empty barrier - all done */ |
| 687 | bio_endio(bio); |
| 688 | else { |
| 689 | bio->bi_opf &= ~REQ_PREFLUSH; |
David Jeffery | 775d783 | 2019-09-16 13:15:14 -0400 | [diff] [blame] | 690 | return false; |
NeilBrown | 2bc13b8 | 2019-03-29 10:46:17 -0700 | [diff] [blame] | 691 | } |
| 692 | } |
David Jeffery | 775d783 | 2019-09-16 13:15:14 -0400 | [diff] [blame] | 693 | return true; |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 694 | } |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 695 | EXPORT_SYMBOL(md_flush_request); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 696 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 697 | static inline struct mddev *mddev_get(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | { |
| 699 | atomic_inc(&mddev->active); |
| 700 | return mddev; |
| 701 | } |
| 702 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 703 | static void mddev_delayed_delete(struct work_struct *ws); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 704 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 705 | static void mddev_put(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | { |
| 707 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
| 708 | return; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 709 | if (!mddev->raid_disks && list_empty(&mddev->disks) && |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 710 | mddev->ctime == 0 && !mddev->hold_active) { |
| 711 | /* Array is not configured at all, and not held active, |
| 712 | * so destroy it */ |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 713 | list_del_init(&mddev->all_mddevs); |
Kent Overstreet | 28dec87 | 2018-06-07 20:52:54 -0400 | [diff] [blame] | 714 | |
| 715 | /* |
| 716 | * Call queue_work inside the spinlock so that |
| 717 | * flush_workqueue() after mddev_find will succeed in waiting |
| 718 | * for the work to be done. |
| 719 | */ |
| 720 | INIT_WORK(&mddev->del_work, mddev_delayed_delete); |
| 721 | queue_work(md_misc_wq, &mddev->del_work); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 722 | } |
| 723 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | } |
| 725 | |
Kees Cook | 8376d3c | 2017-10-16 17:01:48 -0700 | [diff] [blame] | 726 | static void md_safemode_timeout(struct timer_list *t); |
Sasha Levin | 25b2edf | 2015-07-24 18:19:58 -0400 | [diff] [blame] | 727 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 728 | void mddev_init(struct mddev *mddev) |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 729 | { |
Kent Overstreet | 28dec87 | 2018-06-07 20:52:54 -0400 | [diff] [blame] | 730 | kobject_init(&mddev->kobj, &md_ktype); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 731 | mutex_init(&mddev->open_mutex); |
| 732 | mutex_init(&mddev->reconfig_mutex); |
| 733 | mutex_init(&mddev->bitmap_info.mutex); |
| 734 | INIT_LIST_HEAD(&mddev->disks); |
| 735 | INIT_LIST_HEAD(&mddev->all_mddevs); |
Kees Cook | 8376d3c | 2017-10-16 17:01:48 -0700 | [diff] [blame] | 736 | timer_setup(&mddev->safemode_timer, md_safemode_timeout, 0); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 737 | atomic_set(&mddev->active, 1); |
| 738 | atomic_set(&mddev->openers, 0); |
| 739 | atomic_set(&mddev->active_io, 0); |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 740 | spin_lock_init(&mddev->lock); |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 741 | atomic_set(&mddev->flush_pending, 0); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 742 | init_waitqueue_head(&mddev->sb_wait); |
| 743 | init_waitqueue_head(&mddev->recovery_wait); |
| 744 | mddev->reshape_position = MaxSector; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 745 | mddev->reshape_backwards = 0; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 746 | mddev->last_sync_action = "none"; |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 747 | mddev->resync_min = 0; |
| 748 | mddev->resync_max = MaxSector; |
| 749 | mddev->level = LEVEL_NONE; |
| 750 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 751 | EXPORT_SYMBOL_GPL(mddev_init); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 752 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 753 | static struct mddev *mddev_find(dev_t unit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 755 | struct mddev *mddev, *new = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | |
NeilBrown | 8f5f02c | 2011-02-16 13:58:51 +1100 | [diff] [blame] | 757 | if (unit && MAJOR(unit) != MD_MAJOR) |
| 758 | unit &= ~((1<<MdpMinorShift)-1); |
| 759 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | retry: |
| 761 | spin_lock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 763 | if (unit) { |
| 764 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 765 | if (mddev->unit == unit) { |
| 766 | mddev_get(mddev); |
| 767 | spin_unlock(&all_mddevs_lock); |
| 768 | kfree(new); |
| 769 | return mddev; |
| 770 | } |
| 771 | |
| 772 | if (new) { |
| 773 | list_add(&new->all_mddevs, &all_mddevs); |
| 774 | spin_unlock(&all_mddevs_lock); |
| 775 | new->hold_active = UNTIL_IOCTL; |
| 776 | return new; |
| 777 | } |
| 778 | } else if (new) { |
| 779 | /* find an unused unit number */ |
| 780 | static int next_minor = 512; |
| 781 | int start = next_minor; |
| 782 | int is_free = 0; |
| 783 | int dev = 0; |
| 784 | while (!is_free) { |
| 785 | dev = MKDEV(MD_MAJOR, next_minor); |
| 786 | next_minor++; |
| 787 | if (next_minor > MINORMASK) |
| 788 | next_minor = 0; |
| 789 | if (next_minor == start) { |
| 790 | /* Oh dear, all in use. */ |
| 791 | spin_unlock(&all_mddevs_lock); |
| 792 | kfree(new); |
| 793 | return NULL; |
| 794 | } |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 795 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 796 | is_free = 1; |
| 797 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 798 | if (mddev->unit == dev) { |
| 799 | is_free = 0; |
| 800 | break; |
| 801 | } |
| 802 | } |
| 803 | new->unit = dev; |
| 804 | new->md_minor = MINOR(dev); |
| 805 | new->hold_active = UNTIL_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | list_add(&new->all_mddevs, &all_mddevs); |
| 807 | spin_unlock(&all_mddevs_lock); |
| 808 | return new; |
| 809 | } |
| 810 | spin_unlock(&all_mddevs_lock); |
| 811 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 812 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | if (!new) |
| 814 | return NULL; |
| 815 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | new->unit = unit; |
| 817 | if (MAJOR(unit) == MD_MAJOR) |
| 818 | new->md_minor = MINOR(unit); |
| 819 | else |
| 820 | new->md_minor = MINOR(unit) >> MdpMinorShift; |
| 821 | |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 822 | mddev_init(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | goto retry; |
| 825 | } |
| 826 | |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 827 | static struct attribute_group md_redundancy_group; |
| 828 | |
NeilBrown | 5c47daf | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 829 | void mddev_unlock(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | { |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 831 | if (mddev->to_remove) { |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 832 | /* These cannot be removed under reconfig_mutex as |
| 833 | * an access to the files will try to take reconfig_mutex |
| 834 | * while holding the file unremovable, which leads to |
| 835 | * a deadlock. |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 836 | * So hold set sysfs_active while the remove in happeing, |
| 837 | * and anything else which might set ->to_remove or my |
| 838 | * otherwise change the sysfs namespace will fail with |
| 839 | * -EBUSY if sysfs_active is still set. |
| 840 | * We set sysfs_active under reconfig_mutex and elsewhere |
| 841 | * test it under the same mutex to ensure its correct value |
| 842 | * is seen. |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 843 | */ |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 844 | struct attribute_group *to_remove = mddev->to_remove; |
| 845 | mddev->to_remove = NULL; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 846 | mddev->sysfs_active = 1; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 847 | mutex_unlock(&mddev->reconfig_mutex); |
| 848 | |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 849 | if (mddev->kobj.sd) { |
| 850 | if (to_remove != &md_redundancy_group) |
| 851 | sysfs_remove_group(&mddev->kobj, to_remove); |
| 852 | if (mddev->pers == NULL || |
| 853 | mddev->pers->sync_request == NULL) { |
| 854 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 855 | if (mddev->sysfs_action) |
| 856 | sysfs_put(mddev->sysfs_action); |
Junxiao Bi | e8efa9b | 2020-08-04 17:27:18 -0700 | [diff] [blame] | 857 | if (mddev->sysfs_completed) |
| 858 | sysfs_put(mddev->sysfs_completed); |
| 859 | if (mddev->sysfs_degraded) |
| 860 | sysfs_put(mddev->sysfs_degraded); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 861 | mddev->sysfs_action = NULL; |
Junxiao Bi | e8efa9b | 2020-08-04 17:27:18 -0700 | [diff] [blame] | 862 | mddev->sysfs_completed = NULL; |
| 863 | mddev->sysfs_degraded = NULL; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 864 | } |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 865 | } |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 866 | mddev->sysfs_active = 0; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 867 | } else |
| 868 | mutex_unlock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | |
Chris Dunlop | 751e67c | 2011-10-19 16:48:26 +1100 | [diff] [blame] | 870 | /* As we've dropped the mutex we need a spinlock to |
| 871 | * make sure the thread doesn't disappear |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 872 | */ |
| 873 | spin_lock(&pers_lock); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 874 | md_wakeup_thread(mddev->thread); |
NeilBrown | 4d5324f | 2017-10-19 12:17:16 +1100 | [diff] [blame] | 875 | wake_up(&mddev->sb_wait); |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 876 | spin_unlock(&pers_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | } |
NeilBrown | 5c47daf | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 878 | EXPORT_SYMBOL_GPL(mddev_unlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 880 | struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr) |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 881 | { |
| 882 | struct md_rdev *rdev; |
| 883 | |
| 884 | rdev_for_each_rcu(rdev, mddev) |
| 885 | if (rdev->desc_nr == nr) |
| 886 | return rdev; |
| 887 | |
| 888 | return NULL; |
| 889 | } |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 890 | EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu); |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 891 | |
| 892 | static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 894 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 896 | rdev_for_each(rdev, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | if (rdev->bdev->bd_dev == dev) |
| 898 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 899 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | return NULL; |
| 901 | } |
| 902 | |
Tomasz Majchrzak | 1532d9e | 2017-12-27 10:31:40 +0100 | [diff] [blame] | 903 | struct md_rdev *md_find_rdev_rcu(struct mddev *mddev, dev_t dev) |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 904 | { |
| 905 | struct md_rdev *rdev; |
| 906 | |
| 907 | rdev_for_each_rcu(rdev, mddev) |
| 908 | if (rdev->bdev->bd_dev == dev) |
| 909 | return rdev; |
| 910 | |
| 911 | return NULL; |
| 912 | } |
Tomasz Majchrzak | 1532d9e | 2017-12-27 10:31:40 +0100 | [diff] [blame] | 913 | EXPORT_SYMBOL_GPL(md_find_rdev_rcu); |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 914 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 915 | static struct md_personality *find_pers(int level, char *clevel) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 916 | { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 917 | struct md_personality *pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 918 | list_for_each_entry(pers, &pers_list, list) { |
| 919 | if (level != LEVEL_NONE && pers->level == level) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 920 | return pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 921 | if (strcmp(pers->name, clevel)==0) |
| 922 | return pers; |
| 923 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 924 | return NULL; |
| 925 | } |
| 926 | |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 927 | /* return the offset of the super block in 512byte sectors */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 928 | static inline sector_t calc_dev_sboffset(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | { |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 930 | sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512; |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 931 | return MD_NEW_SIZE_SECTORS(num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | } |
| 933 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 934 | static int alloc_disk_sb(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | rdev->sb_page = alloc_page(GFP_KERNEL); |
NeilBrown | 7f0f0d8 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 937 | if (!rdev->sb_page) |
Andre Noll | ebc2433 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 938 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | return 0; |
| 940 | } |
| 941 | |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 942 | void md_rdev_clear(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | { |
| 944 | if (rdev->sb_page) { |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 945 | put_page(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | rdev->sb_loaded = 0; |
| 947 | rdev->sb_page = NULL; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 948 | rdev->sb_start = 0; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 949 | rdev->sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 951 | if (rdev->bb_page) { |
| 952 | put_page(rdev->bb_page); |
| 953 | rdev->bb_page = NULL; |
| 954 | } |
Dan Williams | d3b407fb | 2016-01-06 12:19:22 -0800 | [diff] [blame] | 955 | badblocks_exit(&rdev->badblocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | } |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 957 | EXPORT_SYMBOL_GPL(md_rdev_clear); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 959 | static void super_written(struct bio *bio) |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 960 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 961 | struct md_rdev *rdev = bio->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 962 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 963 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 964 | if (bio->bi_status) { |
Guoqing Jiang | b3db8a2 | 2020-07-28 12:01:41 +0200 | [diff] [blame] | 965 | pr_err("md: %s gets error=%d\n", __func__, |
| 966 | blk_status_to_errno(bio->bi_status)); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 967 | md_error(mddev, rdev); |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 968 | if (!test_bit(Faulty, &rdev->flags) |
| 969 | && (bio->bi_opf & MD_FAILFAST)) { |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 970 | set_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags); |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 971 | set_bit(LastDev, &rdev->flags); |
| 972 | } |
| 973 | } else |
| 974 | clear_bit(LastDev, &rdev->flags); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 975 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 976 | if (atomic_dec_and_test(&mddev->pending_writes)) |
| 977 | wake_up(&mddev->sb_wait); |
Shaohua Li | ed3b98c | 2016-03-29 14:00:19 -0700 | [diff] [blame] | 978 | rdev_dec_pending(rdev, mddev); |
Neil Brown | f8b58ed | 2005-06-27 22:29:34 -0700 | [diff] [blame] | 979 | bio_put(bio); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 980 | } |
| 981 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 982 | void md_super_write(struct mddev *mddev, struct md_rdev *rdev, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 983 | sector_t sector, int size, struct page *page) |
| 984 | { |
| 985 | /* write first size bytes of page to sector of rdev |
| 986 | * Increment mddev->pending_writes before returning |
| 987 | * and decrement it on completion, waking up sb_wait |
| 988 | * if zero is reached. |
| 989 | * If an error occurred, call md_error |
| 990 | */ |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 991 | struct bio *bio; |
| 992 | int ff = 0; |
| 993 | |
Heinz Mauelshagen | 4b6c106 | 2018-02-02 23:13:19 +0100 | [diff] [blame] | 994 | if (!page) |
| 995 | return; |
| 996 | |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 997 | if (test_bit(Faulty, &rdev->flags)) |
| 998 | return; |
| 999 | |
NeilBrown | 5a85071 | 2017-06-21 09:12:21 +1000 | [diff] [blame] | 1000 | bio = md_bio_alloc_sync(mddev); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1001 | |
Shaohua Li | ed3b98c | 2016-03-29 14:00:19 -0700 | [diff] [blame] | 1002 | atomic_inc(&rdev->nr_pending); |
| 1003 | |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 1004 | bio_set_dev(bio, rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1005 | bio->bi_iter.bi_sector = sector; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1006 | bio_add_page(bio, page, size, 0); |
| 1007 | bio->bi_private = rdev; |
| 1008 | bio->bi_end_io = super_written; |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 1009 | |
| 1010 | if (test_bit(MD_FAILFAST_SUPPORTED, &mddev->flags) && |
| 1011 | test_bit(FailFast, &rdev->flags) && |
| 1012 | !test_bit(LastDev, &rdev->flags)) |
| 1013 | ff = MD_FAILFAST; |
Jan Kara | 5a8948f | 2017-05-31 09:44:33 +0200 | [diff] [blame] | 1014 | bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH | REQ_FUA | ff; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1015 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1016 | atomic_inc(&mddev->pending_writes); |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 1017 | submit_bio(bio); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1018 | } |
| 1019 | |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 1020 | int md_super_wait(struct mddev *mddev) |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1021 | { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1022 | /* wait for all superblock writes that were scheduled to complete */ |
NeilBrown | 1967cd5 | 2014-09-09 14:20:28 +1000 | [diff] [blame] | 1023 | wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 1024 | if (test_and_clear_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags)) |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 1025 | return -EAGAIN; |
| 1026 | return 0; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1027 | } |
| 1028 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1029 | 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] | 1030 | struct page *page, int op, int op_flags, bool metadata_op) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | { |
NeilBrown | 5a85071 | 2017-06-21 09:12:21 +1000 | [diff] [blame] | 1032 | struct bio *bio = md_bio_alloc_sync(rdev->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | int ret; |
| 1034 | |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 1035 | if (metadata_op && rdev->meta_bdev) |
| 1036 | bio_set_dev(bio, rdev->meta_bdev); |
| 1037 | else |
| 1038 | bio_set_dev(bio, rdev->bdev); |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1039 | bio_set_op_attrs(bio, op, op_flags); |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1040 | if (metadata_op) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1041 | bio->bi_iter.bi_sector = sector + rdev->sb_start; |
NeilBrown | 1fdd6fc9 | 2012-05-21 09:28:32 +1000 | [diff] [blame] | 1042 | else if (rdev->mddev->reshape_position != MaxSector && |
| 1043 | (rdev->mddev->reshape_backwards == |
| 1044 | (sector >= rdev->mddev->reshape_position))) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1045 | bio->bi_iter.bi_sector = sector + rdev->new_data_offset; |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1046 | else |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1047 | bio->bi_iter.bi_sector = sector + rdev->data_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | bio_add_page(bio, page, size, 0); |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 1049 | |
| 1050 | submit_bio_wait(bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 1052 | ret = !bio->bi_status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | bio_put(bio); |
| 1054 | return ret; |
| 1055 | } |
NeilBrown | a8745db | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 1056 | EXPORT_SYMBOL_GPL(sync_page_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1058 | static int read_disk_sb(struct md_rdev *rdev, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | { |
| 1060 | char b[BDEVNAME_SIZE]; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 1061 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | if (rdev->sb_loaded) |
| 1063 | return 0; |
| 1064 | |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1065 | 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] | 1066 | goto fail; |
| 1067 | rdev->sb_loaded = 1; |
| 1068 | return 0; |
| 1069 | |
| 1070 | fail: |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1071 | pr_err("md: disabled device %s, could not read superblock.\n", |
| 1072 | bdevname(rdev->bdev,b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | return -EINVAL; |
| 1074 | } |
| 1075 | |
Amir Goldstein | e6fd209 | 2017-05-04 16:26:20 +0300 | [diff] [blame] | 1076 | 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] | 1077 | { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1078 | return sb1->set_uuid0 == sb2->set_uuid0 && |
Andre Noll | 0571046 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 1079 | sb1->set_uuid1 == sb2->set_uuid1 && |
| 1080 | sb1->set_uuid2 == sb2->set_uuid2 && |
| 1081 | sb1->set_uuid3 == sb2->set_uuid3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | } |
| 1083 | |
Amir Goldstein | e6fd209 | 2017-05-04 16:26:20 +0300 | [diff] [blame] | 1084 | 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] | 1085 | { |
| 1086 | int ret; |
| 1087 | mdp_super_t *tmp1, *tmp2; |
| 1088 | |
| 1089 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); |
| 1090 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); |
| 1091 | |
| 1092 | if (!tmp1 || !tmp2) { |
| 1093 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1094 | goto abort; |
| 1095 | } |
| 1096 | |
| 1097 | *tmp1 = *sb1; |
| 1098 | *tmp2 = *sb2; |
| 1099 | |
| 1100 | /* |
| 1101 | * nr_disks is not constant |
| 1102 | */ |
| 1103 | tmp1->nr_disks = 0; |
| 1104 | tmp2->nr_disks = 0; |
| 1105 | |
Andre Noll | ce0c8e0 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 1106 | ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 | abort: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 1108 | kfree(tmp1); |
| 1109 | kfree(tmp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | return ret; |
| 1111 | } |
| 1112 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 1113 | static u32 md_csum_fold(u32 csum) |
| 1114 | { |
| 1115 | csum = (csum & 0xffff) + (csum >> 16); |
| 1116 | return (csum & 0xffff) + (csum >> 16); |
| 1117 | } |
| 1118 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1119 | static unsigned int calc_sb_csum(mdp_super_t *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | { |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 1121 | u64 newcsum = 0; |
| 1122 | u32 *sb32 = (u32*)sb; |
| 1123 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1124 | unsigned int disk_csum, csum; |
| 1125 | |
| 1126 | disk_csum = sb->sb_csum; |
| 1127 | sb->sb_csum = 0; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 1128 | |
| 1129 | for (i = 0; i < MD_SB_BYTES/4 ; i++) |
| 1130 | newcsum += sb32[i]; |
| 1131 | csum = (newcsum & 0xffffffff) + (newcsum>>32); |
| 1132 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 1133 | #ifdef CONFIG_ALPHA |
| 1134 | /* This used to use csum_partial, which was wrong for several |
| 1135 | * reasons including that different results are returned on |
| 1136 | * different architectures. It isn't critical that we get exactly |
| 1137 | * the same return value as before (we always csum_fold before |
| 1138 | * testing, and that removes any differences). However as we |
| 1139 | * know that csum_partial always returned a 16bit value on |
| 1140 | * alphas, do a fold to maximise conformity to previous behaviour. |
| 1141 | */ |
| 1142 | sb->sb_csum = md_csum_fold(disk_csum); |
| 1143 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | sb->sb_csum = disk_csum; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 1145 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1146 | return csum; |
| 1147 | } |
| 1148 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 | /* |
| 1150 | * Handle superblock details. |
| 1151 | * We want to be able to handle multiple superblock formats |
| 1152 | * so we have a common interface to them all, and an array of |
| 1153 | * different handlers. |
| 1154 | * We rely on user-space to write the initial superblock, and support |
| 1155 | * reading and updating of superblocks. |
| 1156 | * Interface methods are: |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1157 | * 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] | 1158 | * loads and validates a superblock on dev. |
| 1159 | * if refdev != NULL, compare superblocks on both devices |
| 1160 | * Return: |
| 1161 | * 0 - dev has a superblock that is compatible with refdev |
| 1162 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 1163 | * so dev should be used as the refdev in future |
| 1164 | * -EINVAL superblock incompatible or invalid |
| 1165 | * -othererror e.g. -EIO |
| 1166 | * |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1167 | * int validate_super(struct mddev *mddev, struct md_rdev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1168 | * Verify that dev is acceptable into mddev. |
| 1169 | * The first time, mddev->raid_disks will be 0, and data from |
| 1170 | * dev should be merged in. Subsequent calls check that dev |
| 1171 | * is new enough. Return 0 or -EINVAL |
| 1172 | * |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1173 | * void sync_super(struct mddev *mddev, struct md_rdev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | * Update the superblock for rdev with data in mddev |
| 1175 | * This does not write to disc. |
| 1176 | * |
| 1177 | */ |
| 1178 | |
| 1179 | struct super_type { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1180 | char *name; |
| 1181 | struct module *owner; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1182 | int (*load_super)(struct md_rdev *rdev, |
| 1183 | struct md_rdev *refdev, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1184 | int minor_version); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1185 | int (*validate_super)(struct mddev *mddev, |
| 1186 | struct md_rdev *rdev); |
| 1187 | void (*sync_super)(struct mddev *mddev, |
| 1188 | struct md_rdev *rdev); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1189 | unsigned long long (*rdev_size_change)(struct md_rdev *rdev, |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1190 | sector_t num_sectors); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1191 | int (*allow_new_offset)(struct md_rdev *rdev, |
| 1192 | unsigned long long new_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1193 | }; |
| 1194 | |
| 1195 | /* |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 1196 | * Check that the given mddev has no bitmap. |
| 1197 | * |
| 1198 | * This function is called from the run method of all personalities that do not |
| 1199 | * support bitmaps. It prints an error message and returns non-zero if mddev |
| 1200 | * has a bitmap. Otherwise, it returns 0. |
| 1201 | * |
| 1202 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1203 | int md_check_no_bitmap(struct mddev *mddev) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 1204 | { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1205 | if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 1206 | return 0; |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1207 | pr_warn("%s: bitmaps are not supported for %s\n", |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 1208 | mdname(mddev), mddev->pers->name); |
| 1209 | return 1; |
| 1210 | } |
| 1211 | EXPORT_SYMBOL(md_check_no_bitmap); |
| 1212 | |
| 1213 | /* |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1214 | * load_super for 0.90.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1215 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1216 | 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] | 1217 | { |
| 1218 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 1219 | mdp_super_t *sb; |
| 1220 | int ret; |
Yufen Yu | 228fc7d | 2019-10-30 18:47:02 +0800 | [diff] [blame] | 1221 | bool spare_disk = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1222 | |
| 1223 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1224 | * Calculate the position of the superblock (512byte sectors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1225 | * it's at the end of the disk. |
| 1226 | * |
| 1227 | * It also happens to be a multiple of 4Kb. |
| 1228 | */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1229 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1230 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1231 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1232 | if (ret) |
| 1233 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1234 | |
| 1235 | ret = -EINVAL; |
| 1236 | |
| 1237 | bdevname(rdev->bdev, b); |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1238 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | |
| 1240 | if (sb->md_magic != MD_SB_MAGIC) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1241 | pr_warn("md: invalid raid superblock magic on %s\n", b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | goto abort; |
| 1243 | } |
| 1244 | |
| 1245 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1246 | sb->minor_version < 90 || |
| 1247 | sb->minor_version > 91) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1248 | pr_warn("Bad version number %d.%d on %s\n", |
| 1249 | sb->major_version, sb->minor_version, b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | goto abort; |
| 1251 | } |
| 1252 | |
| 1253 | if (sb->raid_disks <= 0) |
| 1254 | goto abort; |
| 1255 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 1256 | 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] | 1257 | pr_warn("md: invalid superblock checksum on %s\n", b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | goto abort; |
| 1259 | } |
| 1260 | |
| 1261 | rdev->preferred_minor = sb->md_minor; |
| 1262 | rdev->data_offset = 0; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1263 | rdev->new_data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1264 | rdev->sb_size = MD_SB_BYTES; |
NeilBrown | 9f2f383 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1265 | rdev->badblocks.shift = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1266 | |
| 1267 | if (sb->level == LEVEL_MULTIPATH) |
| 1268 | rdev->desc_nr = -1; |
| 1269 | else |
| 1270 | rdev->desc_nr = sb->this_disk.number; |
| 1271 | |
Yufen Yu | 228fc7d | 2019-10-30 18:47:02 +0800 | [diff] [blame] | 1272 | /* not spare disk, or LEVEL_MULTIPATH */ |
| 1273 | if (sb->level == LEVEL_MULTIPATH || |
| 1274 | (rdev->desc_nr >= 0 && |
Yufen Yu | 3b7436c | 2019-12-10 15:01:29 +0800 | [diff] [blame] | 1275 | rdev->desc_nr < MD_SB_DISKS && |
Yufen Yu | 228fc7d | 2019-10-30 18:47:02 +0800 | [diff] [blame] | 1276 | sb->disks[rdev->desc_nr].state & |
| 1277 | ((1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))) |
| 1278 | spare_disk = false; |
| 1279 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1280 | if (!refdev) { |
Yufen Yu | 228fc7d | 2019-10-30 18:47:02 +0800 | [diff] [blame] | 1281 | if (!spare_disk) |
Yufen Yu | 6a5cb53 | 2019-10-16 16:00:03 +0800 | [diff] [blame] | 1282 | ret = 1; |
| 1283 | else |
| 1284 | ret = 0; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1285 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1286 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1287 | mdp_super_t *refsb = page_address(refdev->sb_page); |
Amir Goldstein | e6fd209 | 2017-05-04 16:26:20 +0300 | [diff] [blame] | 1288 | if (!md_uuid_equal(refsb, sb)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1289 | pr_warn("md: %s has different UUID to %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1290 | b, bdevname(refdev->bdev,b2)); |
| 1291 | goto abort; |
| 1292 | } |
Amir Goldstein | e6fd209 | 2017-05-04 16:26:20 +0300 | [diff] [blame] | 1293 | if (!md_sb_equal(refsb, sb)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1294 | pr_warn("md: %s has same UUID but different superblock to %s\n", |
| 1295 | b, bdevname(refdev->bdev, b2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | goto abort; |
| 1297 | } |
| 1298 | ev1 = md_event(sb); |
| 1299 | ev2 = md_event(refsb); |
Yufen Yu | 6a5cb53 | 2019-10-16 16:00:03 +0800 | [diff] [blame] | 1300 | |
Yufen Yu | 228fc7d | 2019-10-30 18:47:02 +0800 | [diff] [blame] | 1301 | if (!spare_disk && ev1 > ev2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | ret = 1; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1303 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | ret = 0; |
| 1305 | } |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 1306 | rdev->sectors = rdev->sb_start; |
NeilBrown | 667a531 | 2012-08-16 16:46:12 +1000 | [diff] [blame] | 1307 | /* Limit to 4TB as metadata cannot record more than that. |
| 1308 | * (not needed for Linear and RAID0 as metadata doesn't |
| 1309 | * record this size) |
| 1310 | */ |
Christoph Hellwig | 72deb45 | 2019-04-05 18:08:59 +0200 | [diff] [blame] | 1311 | if ((u64)rdev->sectors >= (2ULL << 32) && sb->level >= 1) |
Arnd Bergmann | 3312c95 | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 1312 | rdev->sectors = (sector_t)(2ULL << 32) - 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1314 | if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1315 | /* "this cannot possibly happen" ... */ |
| 1316 | ret = -EINVAL; |
| 1317 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1318 | abort: |
| 1319 | return ret; |
| 1320 | } |
| 1321 | |
| 1322 | /* |
| 1323 | * validate_super for 0.90.0 |
| 1324 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1325 | static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1326 | { |
| 1327 | mdp_disk_t *desc; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1328 | mdp_super_t *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1329 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1330 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1331 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1332 | clear_bit(Faulty, &rdev->flags); |
| 1333 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1334 | clear_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1335 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1336 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1337 | if (mddev->raid_disks == 0) { |
| 1338 | mddev->major_version = 0; |
| 1339 | mddev->minor_version = sb->minor_version; |
| 1340 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1341 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1342 | mddev->chunk_sectors = sb->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1343 | mddev->ctime = sb->ctime; |
| 1344 | mddev->utime = sb->utime; |
| 1345 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1346 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | mddev->layout = sb->layout; |
| 1348 | mddev->raid_disks = sb->raid_disks; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1349 | mddev->dev_sectors = ((sector_t)sb->size) * 2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1350 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1351 | mddev->bitmap_info.offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1352 | mddev->bitmap_info.space = 0; |
| 1353 | /* bitmap can use 60 K after the 4K superblocks */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1354 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1355 | mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1356 | mddev->reshape_backwards = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1357 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1358 | if (mddev->minor_version >= 91) { |
| 1359 | mddev->reshape_position = sb->reshape_position; |
| 1360 | mddev->delta_disks = sb->delta_disks; |
| 1361 | mddev->new_level = sb->new_level; |
| 1362 | mddev->new_layout = sb->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1363 | mddev->new_chunk_sectors = sb->new_chunk >> 9; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1364 | if (mddev->delta_disks < 0) |
| 1365 | mddev->reshape_backwards = 1; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1366 | } else { |
| 1367 | mddev->reshape_position = MaxSector; |
| 1368 | mddev->delta_disks = 0; |
| 1369 | mddev->new_level = mddev->level; |
| 1370 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1371 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1372 | } |
NeilBrown | 33f2c35 | 2019-09-09 16:52:29 +1000 | [diff] [blame] | 1373 | if (mddev->level == 0) |
| 1374 | mddev->layout = -1; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1375 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1376 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 1377 | mddev->recovery_cp = MaxSector; |
| 1378 | else { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1379 | if (sb->events_hi == sb->cp_events_hi && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1380 | sb->events_lo == sb->cp_events_lo) { |
| 1381 | mddev->recovery_cp = sb->recovery_cp; |
| 1382 | } else |
| 1383 | mddev->recovery_cp = 0; |
| 1384 | } |
| 1385 | |
| 1386 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 1387 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 1388 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 1389 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 1390 | |
| 1391 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1392 | |
| 1393 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1394 | mddev->bitmap_info.file == NULL) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1395 | mddev->bitmap_info.offset = |
| 1396 | mddev->bitmap_info.default_offset; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1397 | mddev->bitmap_info.space = |
Dave Jones | c9ad020 | 2013-08-19 22:26:32 -0400 | [diff] [blame] | 1398 | mddev->bitmap_info.default_space; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1399 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1400 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1401 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1402 | /* Insist on good event counter while assembling, except |
| 1403 | * for spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1404 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1405 | if (sb->disks[rdev->desc_nr].state & ( |
| 1406 | (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE))) |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1407 | if (ev1 < mddev->events) |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1408 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1409 | } else if (mddev->bitmap) { |
| 1410 | /* if adding to array with a bitmap, then we can accept an |
| 1411 | * older device ... but not too old. |
| 1412 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1413 | if (ev1 < mddev->bitmap->events_cleared) |
| 1414 | return 0; |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1415 | if (ev1 < mddev->events) |
| 1416 | set_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1417 | } else { |
| 1418 | if (ev1 < mddev->events) |
| 1419 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1420 | return 0; |
| 1421 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1422 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1423 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1424 | desc = sb->disks + rdev->desc_nr; |
| 1425 | |
| 1426 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1427 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 1428 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 1429 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1430 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1431 | rdev->raid_disk = desc->raid_disk; |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1432 | rdev->saved_raid_disk = desc->raid_disk; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1433 | } else if (desc->state & (1<<MD_DISK_ACTIVE)) { |
| 1434 | /* active but not in sync implies recovery up to |
| 1435 | * reshape position. We don't know exactly where |
| 1436 | * that is, so set to zero for now */ |
| 1437 | if (mddev->minor_version >= 91) { |
| 1438 | rdev->recovery_offset = 0; |
| 1439 | rdev->raid_disk = desc->raid_disk; |
| 1440 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1441 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1442 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 1443 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 1444 | if (desc->state & (1<<MD_DISK_FAILFAST)) |
| 1445 | set_bit(FailFast, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1446 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1447 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1448 | return 0; |
| 1449 | } |
| 1450 | |
| 1451 | /* |
| 1452 | * sync_super for 0.90.0 |
| 1453 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1454 | static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1455 | { |
| 1456 | mdp_super_t *sb; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1457 | struct md_rdev *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1458 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1459 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1460 | /* make rdev->sb match mddev data.. |
| 1461 | * |
| 1462 | * 1/ zero out disks |
| 1463 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 1464 | * 3/ any empty disks < next_spare become removed |
| 1465 | * |
| 1466 | * disks[0] gets initialised to REMOVED because |
| 1467 | * we cannot be sure from other fields if it has |
| 1468 | * been initialised or not. |
| 1469 | */ |
| 1470 | int i; |
| 1471 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 1472 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 1473 | rdev->sb_size = MD_SB_BYTES; |
| 1474 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1475 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1476 | |
| 1477 | memset(sb, 0, sizeof(*sb)); |
| 1478 | |
| 1479 | sb->md_magic = MD_SB_MAGIC; |
| 1480 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1481 | sb->patch_version = mddev->patch_version; |
| 1482 | sb->gvalid_words = 0; /* ignored */ |
| 1483 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 1484 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 1485 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 1486 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 1487 | |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 1488 | sb->ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1489 | sb->level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1490 | sb->size = mddev->dev_sectors / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1491 | sb->raid_disks = mddev->raid_disks; |
| 1492 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1493 | sb->not_persistent = 0; |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 1494 | sb->utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1495 | sb->state = 0; |
| 1496 | sb->events_hi = (mddev->events>>32); |
| 1497 | sb->events_lo = (u32)mddev->events; |
| 1498 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1499 | if (mddev->reshape_position == MaxSector) |
| 1500 | sb->minor_version = 90; |
| 1501 | else { |
| 1502 | sb->minor_version = 91; |
| 1503 | sb->reshape_position = mddev->reshape_position; |
| 1504 | sb->new_level = mddev->new_level; |
| 1505 | sb->delta_disks = mddev->delta_disks; |
| 1506 | sb->new_layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1507 | sb->new_chunk = mddev->new_chunk_sectors << 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1508 | } |
| 1509 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1510 | if (mddev->in_sync) |
| 1511 | { |
| 1512 | sb->recovery_cp = mddev->recovery_cp; |
| 1513 | sb->cp_events_hi = (mddev->events>>32); |
| 1514 | sb->cp_events_lo = (u32)mddev->events; |
| 1515 | if (mddev->recovery_cp == MaxSector) |
| 1516 | sb->state = (1<< MD_SB_CLEAN); |
| 1517 | } else |
| 1518 | sb->recovery_cp = 0; |
| 1519 | |
| 1520 | sb->layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1521 | sb->chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1522 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1523 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1524 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 1525 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1526 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1527 | rdev_for_each(rdev2, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1528 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1529 | int desc_nr; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1530 | int is_active = test_bit(In_sync, &rdev2->flags); |
| 1531 | |
| 1532 | if (rdev2->raid_disk >= 0 && |
| 1533 | sb->minor_version >= 91) |
| 1534 | /* we have nowhere to store the recovery_offset, |
| 1535 | * but if it is not below the reshape_position, |
| 1536 | * we can piggy-back on that. |
| 1537 | */ |
| 1538 | is_active = 1; |
| 1539 | if (rdev2->raid_disk < 0 || |
| 1540 | test_bit(Faulty, &rdev2->flags)) |
| 1541 | is_active = 0; |
| 1542 | if (is_active) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1543 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1544 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1545 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1546 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1547 | d = &sb->disks[rdev2->desc_nr]; |
| 1548 | nr_disks++; |
| 1549 | d->number = rdev2->desc_nr; |
| 1550 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 1551 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1552 | if (is_active) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1553 | d->raid_disk = rdev2->raid_disk; |
| 1554 | else |
| 1555 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 1556 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1557 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1558 | else if (is_active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1559 | d->state = (1<<MD_DISK_ACTIVE); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1560 | if (test_bit(In_sync, &rdev2->flags)) |
| 1561 | d->state |= (1<<MD_DISK_SYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | active++; |
| 1563 | working++; |
| 1564 | } else { |
| 1565 | d->state = 0; |
| 1566 | spare++; |
| 1567 | working++; |
| 1568 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1569 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 1570 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 1571 | if (test_bit(FailFast, &rdev2->flags)) |
| 1572 | d->state |= (1<<MD_DISK_FAILFAST); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1573 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1574 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 1575 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 1576 | mdp_disk_t *d = &sb->disks[i]; |
| 1577 | if (d->state == 0 && d->number == 0) { |
| 1578 | d->number = i; |
| 1579 | d->raid_disk = i; |
| 1580 | d->state = (1<<MD_DISK_REMOVED); |
| 1581 | d->state |= (1<<MD_DISK_FAULTY); |
| 1582 | failed++; |
| 1583 | } |
| 1584 | } |
| 1585 | sb->nr_disks = nr_disks; |
| 1586 | sb->active_disks = active; |
| 1587 | sb->working_disks = working; |
| 1588 | sb->failed_disks = failed; |
| 1589 | sb->spare_disks = spare; |
| 1590 | |
| 1591 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 1592 | sb->sb_csum = calc_sb_csum(sb); |
| 1593 | } |
| 1594 | |
| 1595 | /* |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1596 | * rdev_size_change for 0.90.0 |
| 1597 | */ |
| 1598 | static unsigned long long |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1599 | 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] | 1600 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1601 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1602 | return 0; /* component must fit device */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1603 | if (rdev->mddev->bitmap_info.offset) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1604 | return 0; /* can't move bitmap */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1605 | rdev->sb_start = calc_dev_sboffset(rdev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1606 | if (!num_sectors || num_sectors > rdev->sb_start) |
| 1607 | num_sectors = rdev->sb_start; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1608 | /* Limit to 4TB as metadata cannot record more than that. |
| 1609 | * 4TB == 2^32 KB, or 2*2^32 sectors. |
| 1610 | */ |
Christoph Hellwig | 72deb45 | 2019-04-05 18:08:59 +0200 | [diff] [blame] | 1611 | if ((u64)num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1) |
Arnd Bergmann | 3312c95 | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 1612 | num_sectors = (sector_t)(2ULL << 32) - 2; |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 1613 | do { |
| 1614 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1615 | rdev->sb_page); |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 1616 | } while (md_super_wait(rdev->mddev) < 0); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1617 | return num_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1618 | } |
| 1619 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1620 | static int |
| 1621 | super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset) |
| 1622 | { |
| 1623 | /* non-zero offset changes not possible with v0.90 */ |
| 1624 | return new_offset == 0; |
| 1625 | } |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1626 | |
| 1627 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1628 | * version 1 superblock |
| 1629 | */ |
| 1630 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1631 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1632 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1633 | __le32 disk_csum; |
| 1634 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1635 | unsigned long long newcsum; |
| 1636 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1637 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1638 | |
| 1639 | disk_csum = sb->sb_csum; |
| 1640 | sb->sb_csum = 0; |
| 1641 | newcsum = 0; |
NeilBrown | 1f3c990 | 2012-12-11 13:09:00 +1100 | [diff] [blame] | 1642 | for (; size >= 4; size -= 4) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1643 | newcsum += le32_to_cpu(*isuper++); |
| 1644 | |
| 1645 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1646 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1647 | |
| 1648 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1649 | sb->sb_csum = disk_csum; |
| 1650 | return cpu_to_le32(csum); |
| 1651 | } |
| 1652 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1653 | 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] | 1654 | { |
| 1655 | struct mdp_superblock_1 *sb; |
| 1656 | int ret; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1657 | sector_t sb_start; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1658 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1659 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1660 | int bmask; |
Yufen Yu | 228fc7d | 2019-10-30 18:47:02 +0800 | [diff] [blame] | 1661 | bool spare_disk = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1662 | |
| 1663 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1664 | * Calculate the position of the superblock in 512byte sectors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1665 | * It is always aligned to a 4K boundary and |
| 1666 | * depeding on minor_version, it can be: |
| 1667 | * 0: At least 8K, but less than 12K, from end of device |
| 1668 | * 1: At start of device |
| 1669 | * 2: 4K from start of device. |
| 1670 | */ |
| 1671 | switch(minor_version) { |
| 1672 | case 0: |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1673 | sb_start = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1674 | sb_start -= 8*2; |
| 1675 | sb_start &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1676 | break; |
| 1677 | case 1: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1678 | sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1679 | break; |
| 1680 | case 2: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1681 | sb_start = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1682 | break; |
| 1683 | default: |
| 1684 | return -EINVAL; |
| 1685 | } |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1686 | rdev->sb_start = sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1687 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1688 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1689 | * and it is safe to read 4k, so we do that |
| 1690 | */ |
| 1691 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1692 | if (ret) return ret; |
| 1693 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1694 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1695 | |
| 1696 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1697 | sb->major_version != cpu_to_le32(1) || |
| 1698 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1699 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1700 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1701 | return -EINVAL; |
| 1702 | |
| 1703 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1704 | pr_warn("md: invalid superblock checksum on %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1705 | bdevname(rdev->bdev,b)); |
| 1706 | return -EINVAL; |
| 1707 | } |
| 1708 | if (le64_to_cpu(sb->data_size) < 10) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1709 | pr_warn("md: data_size too small on %s\n", |
| 1710 | bdevname(rdev->bdev,b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1711 | return -EINVAL; |
| 1712 | } |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1713 | if (sb->pad0 || |
| 1714 | sb->pad3[0] || |
| 1715 | memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1]))) |
| 1716 | /* Some padding is non-zero, might be a new feature */ |
| 1717 | return -EINVAL; |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1718 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1719 | rdev->preferred_minor = 0xffff; |
| 1720 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1721 | rdev->new_data_offset = rdev->data_offset; |
| 1722 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) && |
| 1723 | (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET)) |
| 1724 | rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1725 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1726 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1727 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1728 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1729 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1730 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1731 | |
| 1732 | if (minor_version |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1733 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1734 | return -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1735 | if (minor_version |
| 1736 | && rdev->new_data_offset < sb_start + (rdev->sb_size/512)) |
| 1737 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1738 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1739 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1740 | rdev->desc_nr = -1; |
| 1741 | else |
| 1742 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1743 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1744 | if (!rdev->bb_page) { |
| 1745 | rdev->bb_page = alloc_page(GFP_KERNEL); |
| 1746 | if (!rdev->bb_page) |
| 1747 | return -ENOMEM; |
| 1748 | } |
| 1749 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) && |
| 1750 | rdev->badblocks.count == 0) { |
| 1751 | /* need to load the bad block list. |
| 1752 | * Currently we limit it to one page. |
| 1753 | */ |
| 1754 | s32 offset; |
| 1755 | sector_t bb_sector; |
Christoph Hellwig | 00485d0 | 2019-04-04 18:56:12 +0200 | [diff] [blame] | 1756 | __le64 *bbp; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1757 | int i; |
| 1758 | int sectors = le16_to_cpu(sb->bblog_size); |
| 1759 | if (sectors > (PAGE_SIZE / 512)) |
| 1760 | return -EINVAL; |
| 1761 | offset = le32_to_cpu(sb->bblog_offset); |
| 1762 | if (offset == 0) |
| 1763 | return -EINVAL; |
| 1764 | bb_sector = (long long)offset; |
| 1765 | if (!sync_page_io(rdev, bb_sector, sectors << 9, |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1766 | rdev->bb_page, REQ_OP_READ, 0, true)) |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1767 | return -EIO; |
Christoph Hellwig | 00485d0 | 2019-04-04 18:56:12 +0200 | [diff] [blame] | 1768 | bbp = (__le64 *)page_address(rdev->bb_page); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1769 | rdev->badblocks.shift = sb->bblog_shift; |
| 1770 | for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) { |
| 1771 | u64 bb = le64_to_cpu(*bbp); |
| 1772 | int count = bb & (0x3ff); |
| 1773 | u64 sector = bb >> 10; |
| 1774 | sector <<= sb->bblog_shift; |
| 1775 | count <<= sb->bblog_shift; |
| 1776 | if (bb + 1 == 0) |
| 1777 | break; |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 1778 | if (badblocks_set(&rdev->badblocks, sector, count, 1)) |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1779 | return -EINVAL; |
| 1780 | } |
NeilBrown | 486adf7 | 2013-04-24 11:42:44 +1000 | [diff] [blame] | 1781 | } else if (sb->bblog_offset != 0) |
| 1782 | rdev->badblocks.shift = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1783 | |
Pawel Baldysiak | ddc0882 | 2017-08-16 17:13:45 +0200 | [diff] [blame] | 1784 | if ((le32_to_cpu(sb->feature_map) & |
| 1785 | (MD_FEATURE_PPL | MD_FEATURE_MULTIPLE_PPLS))) { |
Artur Paszkiewicz | ea0213e | 2017-03-09 09:59:57 +0100 | [diff] [blame] | 1786 | rdev->ppl.offset = (__s16)le16_to_cpu(sb->ppl.offset); |
| 1787 | rdev->ppl.size = le16_to_cpu(sb->ppl.size); |
| 1788 | rdev->ppl.sector = rdev->sb_start + rdev->ppl.offset; |
| 1789 | } |
| 1790 | |
NeilBrown | 33f2c35 | 2019-09-09 16:52:29 +1000 | [diff] [blame] | 1791 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RAID0_LAYOUT) && |
| 1792 | sb->level != 0) |
| 1793 | return -EINVAL; |
| 1794 | |
Yufen Yu | 228fc7d | 2019-10-30 18:47:02 +0800 | [diff] [blame] | 1795 | /* not spare disk, or LEVEL_MULTIPATH */ |
| 1796 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH) || |
| 1797 | (rdev->desc_nr >= 0 && |
| 1798 | rdev->desc_nr < le32_to_cpu(sb->max_dev) && |
| 1799 | (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX || |
| 1800 | le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL))) |
| 1801 | spare_disk = false; |
Yufen Yu | 6a5cb53 | 2019-10-16 16:00:03 +0800 | [diff] [blame] | 1802 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1803 | if (!refdev) { |
Yufen Yu | 228fc7d | 2019-10-30 18:47:02 +0800 | [diff] [blame] | 1804 | if (!spare_disk) |
Yufen Yu | 6a5cb53 | 2019-10-16 16:00:03 +0800 | [diff] [blame] | 1805 | ret = 1; |
| 1806 | else |
| 1807 | ret = 0; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1808 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1809 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1810 | struct mdp_superblock_1 *refsb = page_address(refdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1811 | |
| 1812 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1813 | sb->level != refsb->level || |
| 1814 | sb->layout != refsb->layout || |
| 1815 | sb->chunksize != refsb->chunksize) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1816 | pr_warn("md: %s has strangely different superblock to %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1817 | bdevname(rdev->bdev,b), |
| 1818 | bdevname(refdev->bdev,b2)); |
| 1819 | return -EINVAL; |
| 1820 | } |
| 1821 | ev1 = le64_to_cpu(sb->events); |
| 1822 | ev2 = le64_to_cpu(refsb->events); |
| 1823 | |
Yufen Yu | 228fc7d | 2019-10-30 18:47:02 +0800 | [diff] [blame] | 1824 | if (!spare_disk && ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1825 | ret = 1; |
| 1826 | else |
| 1827 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1828 | } |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1829 | if (minor_version) { |
| 1830 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9); |
| 1831 | sectors -= rdev->data_offset; |
| 1832 | } else |
| 1833 | sectors = rdev->sb_start; |
| 1834 | if (sectors < le64_to_cpu(sb->data_size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1835 | return -EINVAL; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1836 | rdev->sectors = le64_to_cpu(sb->data_size); |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1837 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1838 | } |
| 1839 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1840 | static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1841 | { |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1842 | struct mdp_superblock_1 *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1843 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1844 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1845 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1846 | clear_bit(Faulty, &rdev->flags); |
| 1847 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1848 | clear_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1849 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1850 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1851 | if (mddev->raid_disks == 0) { |
| 1852 | mddev->major_version = 1; |
| 1853 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1854 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1855 | mddev->chunk_sectors = le32_to_cpu(sb->chunksize); |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 1856 | mddev->ctime = le64_to_cpu(sb->ctime); |
| 1857 | mddev->utime = le64_to_cpu(sb->utime); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1858 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1859 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1860 | mddev->layout = le32_to_cpu(sb->layout); |
| 1861 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1862 | mddev->dev_sectors = le64_to_cpu(sb->size); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1863 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1864 | mddev->bitmap_info.offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1865 | mddev->bitmap_info.space = 0; |
| 1866 | /* Default location for bitmap is 1K after superblock |
| 1867 | * using 3K - total of 4K |
| 1868 | */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1869 | mddev->bitmap_info.default_offset = 1024 >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1870 | mddev->bitmap_info.default_space = (4096-1024) >> 9; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1871 | mddev->reshape_backwards = 0; |
| 1872 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1873 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1874 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1875 | |
| 1876 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1877 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1878 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1879 | mddev->bitmap_info.file == NULL) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1880 | mddev->bitmap_info.offset = |
| 1881 | (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1882 | /* Metadata doesn't record how much space is available. |
| 1883 | * For 1.0, we assume we can use up to the superblock |
| 1884 | * if before, else to 4K beyond superblock. |
| 1885 | * For others, assume no change is possible. |
| 1886 | */ |
| 1887 | if (mddev->minor_version > 0) |
| 1888 | mddev->bitmap_info.space = 0; |
| 1889 | else if (mddev->bitmap_info.offset > 0) |
| 1890 | mddev->bitmap_info.space = |
| 1891 | 8 - mddev->bitmap_info.offset; |
| 1892 | else |
| 1893 | mddev->bitmap_info.space = |
| 1894 | -mddev->bitmap_info.offset; |
| 1895 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1896 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1897 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1898 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1899 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1900 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1901 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1902 | mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1903 | if (mddev->delta_disks < 0 || |
| 1904 | (mddev->delta_disks == 0 && |
| 1905 | (le32_to_cpu(sb->feature_map) |
| 1906 | & MD_FEATURE_RESHAPE_BACKWARDS))) |
| 1907 | mddev->reshape_backwards = 1; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1908 | } else { |
| 1909 | mddev->reshape_position = MaxSector; |
| 1910 | mddev->delta_disks = 0; |
| 1911 | mddev->new_level = mddev->level; |
| 1912 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1913 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1914 | } |
| 1915 | |
NeilBrown | 33f2c35 | 2019-09-09 16:52:29 +1000 | [diff] [blame] | 1916 | if (mddev->level == 0 && |
| 1917 | !(le32_to_cpu(sb->feature_map) & MD_FEATURE_RAID0_LAYOUT)) |
| 1918 | mddev->layout = -1; |
| 1919 | |
Song Liu | 486b0f7 | 2016-08-19 15:34:01 -0700 | [diff] [blame] | 1920 | if (le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL) |
Shaohua Li | a62ab49 | 2016-01-06 14:37:13 -0800 | [diff] [blame] | 1921 | set_bit(MD_HAS_JOURNAL, &mddev->flags); |
Artur Paszkiewicz | ea0213e | 2017-03-09 09:59:57 +0100 | [diff] [blame] | 1922 | |
Pawel Baldysiak | ddc0882 | 2017-08-16 17:13:45 +0200 | [diff] [blame] | 1923 | if (le32_to_cpu(sb->feature_map) & |
| 1924 | (MD_FEATURE_PPL | MD_FEATURE_MULTIPLE_PPLS)) { |
Artur Paszkiewicz | ea0213e | 2017-03-09 09:59:57 +0100 | [diff] [blame] | 1925 | if (le32_to_cpu(sb->feature_map) & |
| 1926 | (MD_FEATURE_BITMAP_OFFSET | MD_FEATURE_JOURNAL)) |
| 1927 | return -EINVAL; |
Pawel Baldysiak | ddc0882 | 2017-08-16 17:13:45 +0200 | [diff] [blame] | 1928 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_PPL) && |
| 1929 | (le32_to_cpu(sb->feature_map) & |
| 1930 | MD_FEATURE_MULTIPLE_PPLS)) |
| 1931 | return -EINVAL; |
Artur Paszkiewicz | ea0213e | 2017-03-09 09:59:57 +0100 | [diff] [blame] | 1932 | set_bit(MD_HAS_PPL, &mddev->flags); |
| 1933 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1934 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1935 | /* Insist of good event counter while assembling, except for |
| 1936 | * spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1937 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1938 | if (rdev->desc_nr >= 0 && |
| 1939 | rdev->desc_nr < le32_to_cpu(sb->max_dev) && |
Song Liu | a3dfbda | 2015-10-08 21:54:11 -0700 | [diff] [blame] | 1940 | (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX || |
| 1941 | le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL)) |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1942 | if (ev1 < mddev->events) |
| 1943 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1944 | } else if (mddev->bitmap) { |
| 1945 | /* If adding to array with a bitmap, then we can accept an |
| 1946 | * older device, but not too old. |
| 1947 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1948 | if (ev1 < mddev->bitmap->events_cleared) |
| 1949 | return 0; |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1950 | if (ev1 < mddev->events) |
| 1951 | set_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1952 | } else { |
| 1953 | if (ev1 < mddev->events) |
| 1954 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1955 | return 0; |
| 1956 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1957 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1958 | int role; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1959 | if (rdev->desc_nr < 0 || |
| 1960 | rdev->desc_nr >= le32_to_cpu(sb->max_dev)) { |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1961 | role = MD_DISK_ROLE_SPARE; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1962 | rdev->desc_nr = -1; |
| 1963 | } else |
| 1964 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1965 | switch(role) { |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1966 | case MD_DISK_ROLE_SPARE: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1967 | break; |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1968 | case MD_DISK_ROLE_FAULTY: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1969 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1970 | break; |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 1971 | case MD_DISK_ROLE_JOURNAL: /* journal device */ |
| 1972 | if (!(le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)) { |
| 1973 | /* journal device without journal feature */ |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1974 | 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] | 1975 | return -EINVAL; |
| 1976 | } |
| 1977 | set_bit(Journal, &rdev->flags); |
Shaohua Li | 3069aa8 | 2015-08-13 14:31:56 -0700 | [diff] [blame] | 1978 | rdev->journal_tail = le64_to_cpu(sb->journal_tail); |
Shaohua Li | 9b15603 | 2015-12-18 15:19:16 +1100 | [diff] [blame] | 1979 | rdev->raid_disk = 0; |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 1980 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1981 | default: |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1982 | rdev->saved_raid_disk = role; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1983 | if ((le32_to_cpu(sb->feature_map) & |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1984 | MD_FEATURE_RECOVERY_OFFSET)) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1985 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1986 | if (!(le32_to_cpu(sb->feature_map) & |
| 1987 | MD_FEATURE_RECOVERY_BITMAP)) |
| 1988 | rdev->saved_raid_disk = -1; |
Guoqing Jiang | 062f5b2a | 2019-07-24 11:09:20 +0200 | [diff] [blame] | 1989 | } else { |
| 1990 | /* |
| 1991 | * If the array is FROZEN, then the device can't |
| 1992 | * be in_sync with rest of array. |
| 1993 | */ |
| 1994 | if (!test_bit(MD_RECOVERY_FROZEN, |
| 1995 | &mddev->recovery)) |
| 1996 | set_bit(In_sync, &rdev->flags); |
| 1997 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1998 | rdev->raid_disk = role; |
| 1999 | break; |
| 2000 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 2001 | if (sb->devflags & WriteMostly1) |
| 2002 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 2003 | if (sb->devflags & FailFast1) |
| 2004 | set_bit(FailFast, &rdev->flags); |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2005 | if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT) |
| 2006 | set_bit(Replacement, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 2007 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2008 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 2009 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2010 | return 0; |
| 2011 | } |
| 2012 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2013 | static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2014 | { |
| 2015 | struct mdp_superblock_1 *sb; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2016 | struct md_rdev *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2017 | int max_dev, i; |
| 2018 | /* make rdev->sb match mddev and rdev data. */ |
| 2019 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2020 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2021 | |
| 2022 | sb->feature_map = 0; |
| 2023 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 2024 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2025 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 2026 | |
| 2027 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 2028 | sb->events = cpu_to_le64(mddev->events); |
| 2029 | if (mddev->in_sync) |
| 2030 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
Shaohua Li | bd18f64 | 2015-09-02 13:49:50 -0700 | [diff] [blame] | 2031 | else if (test_bit(MD_JOURNAL_CLEAN, &mddev->flags)) |
| 2032 | sb->resync_offset = cpu_to_le64(MaxSector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2033 | else |
| 2034 | sb->resync_offset = cpu_to_le64(0); |
| 2035 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 2036 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2037 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 2038 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2039 | sb->size = cpu_to_le64(mddev->dev_sectors); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 2040 | sb->chunksize = cpu_to_le32(mddev->chunk_sectors); |
NeilBrown | 62e1e38 | 2009-05-26 09:40:59 +1000 | [diff] [blame] | 2041 | sb->level = cpu_to_le32(mddev->level); |
| 2042 | sb->layout = cpu_to_le32(mddev->layout); |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 2043 | if (test_bit(FailFast, &rdev->flags)) |
| 2044 | sb->devflags |= FailFast1; |
| 2045 | else |
| 2046 | sb->devflags &= ~FailFast1; |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 2047 | |
NeilBrown | aeb9b211 | 2011-08-25 14:43:08 +1000 | [diff] [blame] | 2048 | if (test_bit(WriteMostly, &rdev->flags)) |
| 2049 | sb->devflags |= WriteMostly1; |
| 2050 | else |
| 2051 | sb->devflags &= ~WriteMostly1; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2052 | sb->data_offset = cpu_to_le64(rdev->data_offset); |
| 2053 | sb->data_size = cpu_to_le64(rdev->sectors); |
NeilBrown | aeb9b211 | 2011-08-25 14:43:08 +1000 | [diff] [blame] | 2054 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 2055 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) { |
| 2056 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 2057 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 2058 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 2059 | |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2060 | if (rdev->raid_disk >= 0 && !test_bit(Journal, &rdev->flags) && |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2061 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 2062 | sb->feature_map |= |
| 2063 | cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 2064 | sb->recovery_offset = |
| 2065 | cpu_to_le64(rdev->recovery_offset); |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2066 | if (rdev->saved_raid_disk >= 0 && mddev->bitmap) |
| 2067 | sb->feature_map |= |
| 2068 | cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 2069 | } |
Shaohua Li | 3069aa8 | 2015-08-13 14:31:56 -0700 | [diff] [blame] | 2070 | /* Note: recovery_offset and journal_tail share space */ |
| 2071 | if (test_bit(Journal, &rdev->flags)) |
| 2072 | sb->journal_tail = cpu_to_le64(rdev->journal_tail); |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2073 | if (test_bit(Replacement, &rdev->flags)) |
| 2074 | sb->feature_map |= |
| 2075 | cpu_to_le32(MD_FEATURE_REPLACEMENT); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 2076 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 2077 | if (mddev->reshape_position != MaxSector) { |
| 2078 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 2079 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 2080 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 2081 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 2082 | sb->new_level = cpu_to_le32(mddev->new_level); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 2083 | sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2084 | if (mddev->delta_disks == 0 && |
| 2085 | mddev->reshape_backwards) |
| 2086 | sb->feature_map |
| 2087 | |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2088 | if (rdev->new_data_offset != rdev->data_offset) { |
| 2089 | sb->feature_map |
| 2090 | |= cpu_to_le32(MD_FEATURE_NEW_OFFSET); |
| 2091 | sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset |
| 2092 | - rdev->data_offset)); |
| 2093 | } |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 2094 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 2095 | |
Goldwyn Rodrigues | 3c462c8 | 2015-08-19 07:35:54 +1000 | [diff] [blame] | 2096 | if (mddev_is_clustered(mddev)) |
| 2097 | sb->feature_map |= cpu_to_le32(MD_FEATURE_CLUSTERED); |
| 2098 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2099 | if (rdev->badblocks.count == 0) |
| 2100 | /* Nothing to do for bad blocks*/ ; |
| 2101 | else if (sb->bblog_offset == 0) |
| 2102 | /* Cannot record bad blocks on this device */ |
| 2103 | md_error(mddev, rdev); |
| 2104 | else { |
| 2105 | struct badblocks *bb = &rdev->badblocks; |
Christoph Hellwig | ae50640 | 2019-04-04 18:56:13 +0200 | [diff] [blame] | 2106 | __le64 *bbp = (__le64 *)page_address(rdev->bb_page); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2107 | u64 *p = bb->page; |
| 2108 | sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS); |
| 2109 | if (bb->changed) { |
| 2110 | unsigned seq; |
| 2111 | |
| 2112 | retry: |
| 2113 | seq = read_seqbegin(&bb->lock); |
| 2114 | |
| 2115 | memset(bbp, 0xff, PAGE_SIZE); |
| 2116 | |
| 2117 | for (i = 0 ; i < bb->count ; i++) { |
majianpeng | 35f9ac2 | 2012-11-08 08:56:27 +0800 | [diff] [blame] | 2118 | u64 internal_bb = p[i]; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2119 | u64 store_bb = ((BB_OFFSET(internal_bb) << 10) |
| 2120 | | BB_LEN(internal_bb)); |
majianpeng | 35f9ac2 | 2012-11-08 08:56:27 +0800 | [diff] [blame] | 2121 | bbp[i] = cpu_to_le64(store_bb); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2122 | } |
NeilBrown | d096293 | 2012-03-19 12:46:41 +1100 | [diff] [blame] | 2123 | bb->changed = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2124 | if (read_seqretry(&bb->lock, seq)) |
| 2125 | goto retry; |
| 2126 | |
| 2127 | bb->sector = (rdev->sb_start + |
| 2128 | (int)le32_to_cpu(sb->bblog_offset)); |
| 2129 | bb->size = le16_to_cpu(sb->bblog_size); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2130 | } |
| 2131 | } |
| 2132 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2133 | max_dev = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2134 | rdev_for_each(rdev2, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2135 | if (rdev2->desc_nr+1 > max_dev) |
| 2136 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2137 | |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 2138 | if (max_dev > le32_to_cpu(sb->max_dev)) { |
| 2139 | int bmask; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2140 | sb->max_dev = cpu_to_le32(max_dev); |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 2141 | rdev->sb_size = max_dev * 2 + 256; |
| 2142 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
| 2143 | if (rdev->sb_size & bmask) |
| 2144 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
NeilBrown | ddcf352 | 2010-09-08 16:48:17 +1000 | [diff] [blame] | 2145 | } else |
| 2146 | max_dev = le32_to_cpu(sb->max_dev); |
| 2147 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2148 | for (i=0; i<max_dev;i++) |
Lidong Zhong | 8df7202 | 2017-06-12 10:45:55 +0800 | [diff] [blame] | 2149 | sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2150 | |
Song Liu | a97b789 | 2015-10-08 21:54:09 -0700 | [diff] [blame] | 2151 | if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) |
| 2152 | sb->feature_map |= cpu_to_le32(MD_FEATURE_JOURNAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2153 | |
Artur Paszkiewicz | ea0213e | 2017-03-09 09:59:57 +0100 | [diff] [blame] | 2154 | if (test_bit(MD_HAS_PPL, &mddev->flags)) { |
Pawel Baldysiak | ddc0882 | 2017-08-16 17:13:45 +0200 | [diff] [blame] | 2155 | if (test_bit(MD_HAS_MULTIPLE_PPLS, &mddev->flags)) |
| 2156 | sb->feature_map |= |
| 2157 | cpu_to_le32(MD_FEATURE_MULTIPLE_PPLS); |
| 2158 | else |
| 2159 | sb->feature_map |= cpu_to_le32(MD_FEATURE_PPL); |
Artur Paszkiewicz | ea0213e | 2017-03-09 09:59:57 +0100 | [diff] [blame] | 2160 | sb->ppl.offset = cpu_to_le16(rdev->ppl.offset); |
| 2161 | sb->ppl.size = cpu_to_le16(rdev->ppl.size); |
| 2162 | } |
| 2163 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2164 | rdev_for_each(rdev2, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2165 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2166 | if (test_bit(Faulty, &rdev2->flags)) |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 2167 | sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2168 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2169 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Song Liu | a97b789 | 2015-10-08 21:54:09 -0700 | [diff] [blame] | 2170 | else if (test_bit(Journal, &rdev2->flags)) |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 2171 | sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_JOURNAL); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 2172 | else if (rdev2->raid_disk >= 0) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 2173 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2174 | else |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 2175 | sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2176 | } |
| 2177 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2178 | sb->sb_csum = calc_sb_1_csum(sb); |
| 2179 | } |
| 2180 | |
Xiao Ni | d9c0fa5 | 2020-06-30 15:55:36 +0800 | [diff] [blame] | 2181 | static sector_t super_1_choose_bm_space(sector_t dev_size) |
| 2182 | { |
| 2183 | sector_t bm_space; |
| 2184 | |
| 2185 | /* if the device is bigger than 8Gig, save 64k for bitmap |
| 2186 | * usage, if bigger than 200Gig, save 128k |
| 2187 | */ |
| 2188 | if (dev_size < 64*2) |
| 2189 | bm_space = 0; |
| 2190 | else if (dev_size - 64*2 >= 200*1024*1024*2) |
| 2191 | bm_space = 128*2; |
| 2192 | else if (dev_size - 4*2 > 8*1024*1024*2) |
| 2193 | bm_space = 64*2; |
| 2194 | else |
| 2195 | bm_space = 4*2; |
| 2196 | return bm_space; |
| 2197 | } |
| 2198 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2199 | static unsigned long long |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2200 | 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] | 2201 | { |
| 2202 | struct mdp_superblock_1 *sb; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 2203 | sector_t max_sectors; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2204 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2205 | return 0; /* component must fit device */ |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2206 | if (rdev->data_offset != rdev->new_data_offset) |
| 2207 | return 0; /* too confusing */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2208 | if (rdev->sb_start < rdev->data_offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2209 | /* minor versions 1 and 2; superblock before data */ |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 2210 | max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 2211 | max_sectors -= rdev->data_offset; |
| 2212 | if (!num_sectors || num_sectors > max_sectors) |
| 2213 | num_sectors = max_sectors; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 2214 | } else if (rdev->mddev->bitmap_info.offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2215 | /* minor version 0 with bitmap we can't move */ |
| 2216 | return 0; |
| 2217 | } else { |
| 2218 | /* minor version 0; superblock after data */ |
Xiao Ni | d9c0fa5 | 2020-06-30 15:55:36 +0800 | [diff] [blame] | 2219 | sector_t sb_start, bm_space; |
| 2220 | sector_t dev_size = i_size_read(rdev->bdev->bd_inode) >> 9; |
| 2221 | |
| 2222 | /* 8K is for superblock */ |
| 2223 | sb_start = dev_size - 8*2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2224 | sb_start &= ~(sector_t)(4*2 - 1); |
Xiao Ni | d9c0fa5 | 2020-06-30 15:55:36 +0800 | [diff] [blame] | 2225 | |
| 2226 | bm_space = super_1_choose_bm_space(dev_size); |
| 2227 | |
| 2228 | /* Space that can be used to store date needs to decrease |
| 2229 | * superblock bitmap space and bad block space(4K) |
| 2230 | */ |
| 2231 | max_sectors = sb_start - bm_space - 4*2; |
| 2232 | |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 2233 | if (!num_sectors || num_sectors > max_sectors) |
| 2234 | num_sectors = max_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2235 | } |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2236 | sb = page_address(rdev->sb_page); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 2237 | sb->data_size = cpu_to_le64(num_sectors); |
Jason Yan | 3fb632e | 2017-03-10 11:27:23 +0800 | [diff] [blame] | 2238 | sb->super_offset = cpu_to_le64(rdev->sb_start); |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2239 | sb->sb_csum = calc_sb_1_csum(sb); |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 2240 | do { |
| 2241 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
| 2242 | rdev->sb_page); |
| 2243 | } while (md_super_wait(rdev->mddev) < 0); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 2244 | return num_sectors; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2245 | |
| 2246 | } |
| 2247 | |
| 2248 | static int |
| 2249 | super_1_allow_new_offset(struct md_rdev *rdev, |
| 2250 | unsigned long long new_offset) |
| 2251 | { |
| 2252 | /* All necessary checks on new >= old have been done */ |
| 2253 | struct bitmap *bitmap; |
| 2254 | if (new_offset >= rdev->data_offset) |
| 2255 | return 1; |
| 2256 | |
| 2257 | /* with 1.0 metadata, there is no metadata to tread on |
| 2258 | * so we can always move back */ |
| 2259 | if (rdev->mddev->minor_version == 0) |
| 2260 | return 1; |
| 2261 | |
| 2262 | /* otherwise we must be sure not to step on |
| 2263 | * any metadata, so stay: |
| 2264 | * 36K beyond start of superblock |
| 2265 | * beyond end of badblocks |
| 2266 | * beyond write-intent bitmap |
| 2267 | */ |
| 2268 | if (rdev->sb_start + (32+4)*2 > new_offset) |
| 2269 | return 0; |
| 2270 | bitmap = rdev->mddev->bitmap; |
| 2271 | if (bitmap && !rdev->mddev->bitmap_info.file && |
| 2272 | rdev->sb_start + rdev->mddev->bitmap_info.offset + |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 2273 | bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2274 | return 0; |
| 2275 | if (rdev->badblocks.sector + rdev->badblocks.size > new_offset) |
| 2276 | return 0; |
| 2277 | |
| 2278 | return 1; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2279 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2280 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 2281 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2282 | [0] = { |
| 2283 | .name = "0.90.0", |
| 2284 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2285 | .load_super = super_90_load, |
| 2286 | .validate_super = super_90_validate, |
| 2287 | .sync_super = super_90_sync, |
| 2288 | .rdev_size_change = super_90_rdev_size_change, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2289 | .allow_new_offset = super_90_allow_new_offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2290 | }, |
| 2291 | [1] = { |
| 2292 | .name = "md-1", |
| 2293 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2294 | .load_super = super_1_load, |
| 2295 | .validate_super = super_1_validate, |
| 2296 | .sync_super = super_1_sync, |
| 2297 | .rdev_size_change = super_1_rdev_size_change, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2298 | .allow_new_offset = super_1_allow_new_offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2299 | }, |
| 2300 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2301 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2302 | static void sync_super(struct mddev *mddev, struct md_rdev *rdev) |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 2303 | { |
| 2304 | if (mddev->sync_super) { |
| 2305 | mddev->sync_super(mddev, rdev); |
| 2306 | return; |
| 2307 | } |
| 2308 | |
| 2309 | BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types)); |
| 2310 | |
| 2311 | super_types[mddev->major_version].sync_super(mddev, rdev); |
| 2312 | } |
| 2313 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2314 | static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2315 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2316 | struct md_rdev *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2317 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2318 | rcu_read_lock(); |
Song Liu | 0b020e8 | 2015-09-03 23:00:35 -0700 | [diff] [blame] | 2319 | rdev_for_each_rcu(rdev, mddev1) { |
| 2320 | if (test_bit(Faulty, &rdev->flags) || |
| 2321 | test_bit(Journal, &rdev->flags) || |
| 2322 | rdev->raid_disk == -1) |
| 2323 | continue; |
| 2324 | rdev_for_each_rcu(rdev2, mddev2) { |
| 2325 | if (test_bit(Faulty, &rdev2->flags) || |
| 2326 | test_bit(Journal, &rdev2->flags) || |
| 2327 | rdev2->raid_disk == -1) |
| 2328 | continue; |
Christoph Hellwig | 61a27e1f | 2020-09-03 07:40:58 +0200 | [diff] [blame] | 2329 | if (rdev->bdev->bd_disk == rdev2->bdev->bd_disk) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2330 | rcu_read_unlock(); |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 2331 | return 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2332 | } |
Song Liu | 0b020e8 | 2015-09-03 23:00:35 -0700 | [diff] [blame] | 2333 | } |
| 2334 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2335 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2336 | return 0; |
| 2337 | } |
| 2338 | |
| 2339 | static LIST_HEAD(pending_raid_disks); |
| 2340 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2341 | /* |
| 2342 | * Try to register data integrity profile for an mddev |
| 2343 | * |
| 2344 | * This is called when an array is started and after a disk has been kicked |
| 2345 | * from the array. It only succeeds if all working and active component devices |
| 2346 | * are integrity capable with matching profiles. |
| 2347 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2348 | int md_integrity_register(struct mddev *mddev) |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2349 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2350 | struct md_rdev *rdev, *reference = NULL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2351 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2352 | if (list_empty(&mddev->disks)) |
| 2353 | return 0; /* nothing to do */ |
Jonathan Brassow | 629acb6 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 2354 | if (!mddev->gendisk || blk_get_integrity(mddev->gendisk)) |
| 2355 | return 0; /* shouldn't register, or already is */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2356 | rdev_for_each(rdev, mddev) { |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2357 | /* skip spares and non-functional disks */ |
| 2358 | if (test_bit(Faulty, &rdev->flags)) |
| 2359 | continue; |
| 2360 | if (rdev->raid_disk < 0) |
| 2361 | continue; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2362 | if (!reference) { |
| 2363 | /* Use the first rdev as the reference */ |
| 2364 | reference = rdev; |
| 2365 | continue; |
| 2366 | } |
| 2367 | /* does this rdev's profile match the reference profile? */ |
| 2368 | if (blk_integrity_compare(reference->bdev->bd_disk, |
| 2369 | rdev->bdev->bd_disk) < 0) |
| 2370 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2371 | } |
Martin K. Petersen | 89078d5 | 2011-03-28 20:09:12 -0400 | [diff] [blame] | 2372 | if (!reference || !bdev_get_integrity(reference->bdev)) |
| 2373 | return 0; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2374 | /* |
| 2375 | * All component devices are integrity capable and have matching |
| 2376 | * profiles, register the common profile for the md device. |
| 2377 | */ |
Martin K. Petersen | 25520d5 | 2015-10-21 13:19:49 -0400 | [diff] [blame] | 2378 | blk_integrity_register(mddev->gendisk, |
| 2379 | bdev_get_integrity(reference->bdev)); |
| 2380 | |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 2381 | pr_debug("md: data integrity enabled on %s\n", mdname(mddev)); |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 2382 | if (bioset_integrity_create(&mddev->bio_set, BIO_POOL_SIZE)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 2383 | pr_err("md: failed to create integrity pool for %s\n", |
Martin K. Petersen | a91a278 | 2011-03-17 11:11:05 +0100 | [diff] [blame] | 2384 | mdname(mddev)); |
| 2385 | return -EINVAL; |
| 2386 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2387 | return 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2388 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2389 | EXPORT_SYMBOL(md_integrity_register); |
| 2390 | |
Dan Williams | 1501efa | 2016-01-13 16:00:07 -0800 | [diff] [blame] | 2391 | /* |
| 2392 | * Attempt to add an rdev, but only if it is consistent with the current |
| 2393 | * integrity profile |
| 2394 | */ |
| 2395 | int md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev) |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2396 | { |
Jonathan Brassow | 2863b9e | 2012-10-11 13:38:58 +1100 | [diff] [blame] | 2397 | struct blk_integrity *bi_mddev; |
Dan Williams | 1501efa | 2016-01-13 16:00:07 -0800 | [diff] [blame] | 2398 | char name[BDEVNAME_SIZE]; |
Jonathan Brassow | 2863b9e | 2012-10-11 13:38:58 +1100 | [diff] [blame] | 2399 | |
| 2400 | if (!mddev->gendisk) |
Dan Williams | 1501efa | 2016-01-13 16:00:07 -0800 | [diff] [blame] | 2401 | return 0; |
Jonathan Brassow | 2863b9e | 2012-10-11 13:38:58 +1100 | [diff] [blame] | 2402 | |
Jonathan Brassow | 2863b9e | 2012-10-11 13:38:58 +1100 | [diff] [blame] | 2403 | bi_mddev = blk_get_integrity(mddev->gendisk); |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2404 | |
| 2405 | if (!bi_mddev) /* nothing to do */ |
Dan Williams | 1501efa | 2016-01-13 16:00:07 -0800 | [diff] [blame] | 2406 | return 0; |
| 2407 | |
| 2408 | if (blk_integrity_compare(mddev->gendisk, rdev->bdev->bd_disk) != 0) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 2409 | pr_err("%s: incompatible integrity profile for %s\n", |
| 2410 | mdname(mddev), bdevname(rdev->bdev, name)); |
Dan Williams | 1501efa | 2016-01-13 16:00:07 -0800 | [diff] [blame] | 2411 | return -ENXIO; |
| 2412 | } |
| 2413 | |
| 2414 | return 0; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2415 | } |
| 2416 | EXPORT_SYMBOL(md_integrity_add_rdev); |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2417 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2418 | 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] | 2419 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 2420 | char b[BDEVNAME_SIZE]; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2421 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2422 | |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2423 | /* prevent duplicates */ |
| 2424 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 2425 | return -EEXIST; |
| 2426 | |
NeilBrown | 97b20ef | 2017-04-13 08:53:48 +1000 | [diff] [blame] | 2427 | if ((bdev_read_only(rdev->bdev) || bdev_read_only(rdev->meta_bdev)) && |
| 2428 | mddev->pers) |
| 2429 | return -EROFS; |
| 2430 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2431 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 2432 | if (!test_bit(Journal, &rdev->flags) && |
| 2433 | rdev->sectors && |
| 2434 | (mddev->dev_sectors == 0 || rdev->sectors < mddev->dev_sectors)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2435 | if (mddev->pers) { |
| 2436 | /* Cannot change size, so fail |
| 2437 | * If mddev->level <= 0, then we don't care |
| 2438 | * about aligning sizes (e.g. linear) |
| 2439 | */ |
| 2440 | if (mddev->level > 0) |
| 2441 | return -ENOSPC; |
| 2442 | } else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2443 | mddev->dev_sectors = rdev->sectors; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2444 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2445 | |
| 2446 | /* Verify rdev->desc_nr is unique. |
| 2447 | * If it is -1, assign a free number, else |
| 2448 | * check number is not in use |
| 2449 | */ |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2450 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2451 | if (rdev->desc_nr < 0) { |
| 2452 | int choice = 0; |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2453 | if (mddev->pers) |
| 2454 | choice = mddev->raid_disks; |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 2455 | while (md_find_rdev_nr_rcu(mddev, choice)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2456 | choice++; |
| 2457 | rdev->desc_nr = choice; |
| 2458 | } else { |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 2459 | if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) { |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2460 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2461 | return -EBUSY; |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2462 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2463 | } |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2464 | rcu_read_unlock(); |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 2465 | if (!test_bit(Journal, &rdev->flags) && |
| 2466 | mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 2467 | pr_warn("md: %s: array is limited to %d devices\n", |
| 2468 | mdname(mddev), mddev->max_disks); |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 2469 | return -EBUSY; |
| 2470 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 2471 | bdevname(rdev->bdev,b); |
Rasmus Villemoes | 90a9bef | 2015-06-25 15:02:36 -0700 | [diff] [blame] | 2472 | strreplace(b, '/', '!'); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2473 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2474 | rdev->mddev = mddev; |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 2475 | pr_debug("md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2476 | |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 2477 | if (mddev->raid_disks) |
Guoqing Jiang | 404659c | 2019-12-23 10:48:53 +0100 | [diff] [blame] | 2478 | mddev_create_serial_pool(mddev, rdev, false); |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 2479 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2480 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2481 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2482 | |
Damien Le Moal | 5e3b8a8 | 2020-07-16 13:54:40 +0900 | [diff] [blame] | 2483 | /* failure here is OK */ |
Christoph Hellwig | 8d65269 | 2020-11-17 08:18:55 +0100 | [diff] [blame] | 2484 | err = sysfs_create_link(&rdev->kobj, bdev_kobj(rdev->bdev), "block"); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2485 | rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state"); |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 2486 | rdev->sysfs_unack_badblocks = |
| 2487 | sysfs_get_dirent_safe(rdev->kobj.sd, "unacknowledged_bad_blocks"); |
| 2488 | rdev->sysfs_badblocks = |
| 2489 | sysfs_get_dirent_safe(rdev->kobj.sd, "bad_blocks"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2490 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2491 | list_add_rcu(&rdev->same_set, &mddev->disks); |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2492 | bd_link_disk_holder(rdev->bdev, mddev->gendisk); |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 2493 | |
| 2494 | /* May as well allow recovery to be retried once */ |
NeilBrown | 5389042 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2495 | mddev->recovery_disabled++; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2496 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2497 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2498 | |
| 2499 | fail: |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 2500 | pr_warn("md: failed to register dev-%s for %s\n", |
| 2501 | b, mdname(mddev)); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2502 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2503 | } |
| 2504 | |
Guoqing Jiang | cc1ffe6 | 2020-04-04 23:57:08 +0200 | [diff] [blame] | 2505 | static void rdev_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2506 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2507 | struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2508 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2509 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2510 | } |
| 2511 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2512 | static void unbind_rdev_from_array(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2513 | { |
| 2514 | char b[BDEVNAME_SIZE]; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 2515 | |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 2516 | bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2517 | list_del_rcu(&rdev->same_set); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 2518 | pr_debug("md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
Guoqing Jiang | 11d3a9f | 2019-12-23 10:48:55 +0100 | [diff] [blame] | 2519 | mddev_destroy_serial_pool(rdev->mddev, rdev, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2520 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2521 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2522 | sysfs_put(rdev->sysfs_state); |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 2523 | sysfs_put(rdev->sysfs_unack_badblocks); |
| 2524 | sysfs_put(rdev->sysfs_badblocks); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2525 | rdev->sysfs_state = NULL; |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 2526 | rdev->sysfs_unack_badblocks = NULL; |
| 2527 | rdev->sysfs_badblocks = NULL; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 2528 | rdev->badblocks.count = 0; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2529 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2530 | * writing to 'remove' to "dev/state". We also need |
| 2531 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2532 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2533 | synchronize_rcu(); |
Guoqing Jiang | cc1ffe6 | 2020-04-04 23:57:08 +0200 | [diff] [blame] | 2534 | INIT_WORK(&rdev->del_work, rdev_delayed_delete); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2535 | kobject_get(&rdev->kobj); |
Guoqing Jiang | cc1ffe6 | 2020-04-04 23:57:08 +0200 | [diff] [blame] | 2536 | queue_work(md_rdev_misc_wq, &rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2537 | } |
| 2538 | |
| 2539 | /* |
| 2540 | * prevent the device from being mounted, repartitioned or |
| 2541 | * otherwise reused by a RAID array (or any other kernel |
| 2542 | * subsystem), by bd_claiming the device. |
| 2543 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2544 | 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] | 2545 | { |
| 2546 | int err = 0; |
| 2547 | struct block_device *bdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2548 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 2549 | bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2550 | shared ? (struct md_rdev *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2551 | if (IS_ERR(bdev)) { |
Christoph Hellwig | ea3edd4 | 2020-03-24 08:25:11 +0100 | [diff] [blame] | 2552 | pr_warn("md: could not open device unknown-block(%u,%u).\n", |
| 2553 | MAJOR(dev), MINOR(dev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2554 | return PTR_ERR(bdev); |
| 2555 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2556 | rdev->bdev = bdev; |
| 2557 | return err; |
| 2558 | } |
| 2559 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2560 | static void unlock_rdev(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2561 | { |
| 2562 | struct block_device *bdev = rdev->bdev; |
| 2563 | rdev->bdev = NULL; |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2564 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2565 | } |
| 2566 | |
| 2567 | void md_autodetect_dev(dev_t dev); |
| 2568 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2569 | static void export_rdev(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2570 | { |
| 2571 | char b[BDEVNAME_SIZE]; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 2572 | |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 2573 | pr_debug("md: export_rdev(%s)\n", bdevname(rdev->bdev,b)); |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 2574 | md_rdev_clear(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2575 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 2576 | if (test_bit(AutoDetected, &rdev->flags)) |
| 2577 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2578 | #endif |
| 2579 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2580 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2581 | } |
| 2582 | |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 2583 | void md_kick_rdev_from_array(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2584 | { |
| 2585 | unbind_rdev_from_array(rdev); |
| 2586 | export_rdev(rdev); |
| 2587 | } |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 2588 | EXPORT_SYMBOL_GPL(md_kick_rdev_from_array); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2589 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2590 | static void export_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2591 | { |
NeilBrown | 0638bb0 | 2014-09-25 17:43:47 +1000 | [diff] [blame] | 2592 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2593 | |
NeilBrown | 0638bb0 | 2014-09-25 17:43:47 +1000 | [diff] [blame] | 2594 | while (!list_empty(&mddev->disks)) { |
| 2595 | rdev = list_first_entry(&mddev->disks, struct md_rdev, |
| 2596 | same_set); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 2597 | md_kick_rdev_from_array(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2598 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2599 | mddev->raid_disks = 0; |
| 2600 | mddev->major_version = 0; |
| 2601 | } |
| 2602 | |
NeilBrown | 6497709b | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 2603 | static bool set_in_sync(struct mddev *mddev) |
| 2604 | { |
Shaohua Li | efa4b77 | 2017-10-18 22:08:13 -0700 | [diff] [blame] | 2605 | lockdep_assert_held(&mddev->lock); |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 2606 | if (!mddev->in_sync) { |
| 2607 | mddev->sync_checkers++; |
| 2608 | spin_unlock(&mddev->lock); |
| 2609 | percpu_ref_switch_to_atomic_sync(&mddev->writes_pending); |
| 2610 | spin_lock(&mddev->lock); |
| 2611 | if (!mddev->in_sync && |
| 2612 | percpu_ref_is_zero(&mddev->writes_pending)) { |
NeilBrown | 6497709b | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 2613 | mddev->in_sync = 1; |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 2614 | /* |
| 2615 | * Ensure ->in_sync is visible before we clear |
| 2616 | * ->sync_checkers. |
| 2617 | */ |
NeilBrown | 55cc39f | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 2618 | smp_mb(); |
NeilBrown | 6497709b | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 2619 | set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags); |
| 2620 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
| 2621 | } |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 2622 | if (--mddev->sync_checkers == 0) |
| 2623 | percpu_ref_switch_to_percpu(&mddev->writes_pending); |
NeilBrown | 6497709b | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 2624 | } |
| 2625 | if (mddev->safemode == 1) |
| 2626 | mddev->safemode = 0; |
| 2627 | return mddev->in_sync; |
| 2628 | } |
| 2629 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2630 | static void sync_sbs(struct mddev *mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2631 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2632 | /* Update each superblock (in-memory image), but |
| 2633 | * if we are allowed to, skip spares which already |
| 2634 | * have the right event counter, or have one earlier |
| 2635 | * (which would mean they aren't being marked as dirty |
| 2636 | * with the rest of the array) |
| 2637 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2638 | struct md_rdev *rdev; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2639 | rdev_for_each(rdev, mddev) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2640 | if (rdev->sb_events == mddev->events || |
| 2641 | (nospares && |
| 2642 | rdev->raid_disk < 0 && |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2643 | rdev->sb_events+1 == mddev->events)) { |
| 2644 | /* Don't update this superblock */ |
| 2645 | rdev->sb_loaded = 2; |
| 2646 | } else { |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 2647 | sync_super(mddev, rdev); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2648 | rdev->sb_loaded = 1; |
| 2649 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2650 | } |
| 2651 | } |
| 2652 | |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2653 | static bool does_sb_need_changing(struct mddev *mddev) |
| 2654 | { |
| 2655 | struct md_rdev *rdev; |
| 2656 | struct mdp_superblock_1 *sb; |
| 2657 | int role; |
| 2658 | |
| 2659 | /* Find a good rdev */ |
| 2660 | rdev_for_each(rdev, mddev) |
| 2661 | if ((rdev->raid_disk >= 0) && !test_bit(Faulty, &rdev->flags)) |
| 2662 | break; |
| 2663 | |
| 2664 | /* No good device found. */ |
| 2665 | if (!rdev) |
| 2666 | return false; |
| 2667 | |
| 2668 | sb = page_address(rdev->sb_page); |
| 2669 | /* Check if a device has become faulty or a spare become active */ |
| 2670 | rdev_for_each(rdev, mddev) { |
| 2671 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
| 2672 | /* Device activated? */ |
| 2673 | if (role == 0xffff && rdev->raid_disk >=0 && |
| 2674 | !test_bit(Faulty, &rdev->flags)) |
| 2675 | return true; |
| 2676 | /* Device turned faulty? */ |
| 2677 | if (test_bit(Faulty, &rdev->flags) && (role < 0xfffd)) |
| 2678 | return true; |
| 2679 | } |
| 2680 | |
| 2681 | /* Check if any mddev parameters have changed */ |
| 2682 | if ((mddev->dev_sectors != le64_to_cpu(sb->size)) || |
| 2683 | (mddev->reshape_position != le64_to_cpu(sb->reshape_position)) || |
Jason Yan | 1345921 | 2017-03-10 11:49:12 +0800 | [diff] [blame] | 2684 | (mddev->layout != le32_to_cpu(sb->layout)) || |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2685 | (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) || |
| 2686 | (mddev->chunk_sectors != le32_to_cpu(sb->chunksize))) |
| 2687 | return true; |
| 2688 | |
| 2689 | return false; |
| 2690 | } |
| 2691 | |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 2692 | void md_update_sb(struct mddev *mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2693 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2694 | struct md_rdev *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2695 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2696 | int nospares = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2697 | int any_badblocks_changed = 0; |
Guoqing Jiang | 23b63f9 | 2015-10-12 17:21:30 +0800 | [diff] [blame] | 2698 | int ret = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2699 | |
NeilBrown | d87f064 | 2013-04-24 11:42:40 +1000 | [diff] [blame] | 2700 | if (mddev->ro) { |
| 2701 | if (force_change) |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2702 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | d87f064 | 2013-04-24 11:42:40 +1000 | [diff] [blame] | 2703 | return; |
| 2704 | } |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2705 | |
Guoqing Jiang | 2c97cf1 | 2016-05-02 11:33:09 -0400 | [diff] [blame] | 2706 | repeat: |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2707 | if (mddev_is_clustered(mddev)) { |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2708 | if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)) |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2709 | force_change = 1; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2710 | if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags)) |
Guoqing Jiang | 85ad1d1 | 2016-05-03 22:22:13 -0400 | [diff] [blame] | 2711 | nospares = 1; |
Guoqing Jiang | 23b63f9 | 2015-10-12 17:21:30 +0800 | [diff] [blame] | 2712 | ret = md_cluster_ops->metadata_update_start(mddev); |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2713 | /* Has someone else has updated the sb */ |
| 2714 | if (!does_sb_need_changing(mddev)) { |
Guoqing Jiang | 23b63f9 | 2015-10-12 17:21:30 +0800 | [diff] [blame] | 2715 | if (ret == 0) |
| 2716 | md_cluster_ops->metadata_update_cancel(mddev); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2717 | bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING), |
| 2718 | BIT(MD_SB_CHANGE_DEVS) | |
| 2719 | BIT(MD_SB_CHANGE_CLEAN)); |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2720 | return; |
| 2721 | } |
| 2722 | } |
Guoqing Jiang | 2c97cf1 | 2016-05-02 11:33:09 -0400 | [diff] [blame] | 2723 | |
NeilBrown | db0505d | 2017-10-17 16:18:36 +1100 | [diff] [blame] | 2724 | /* |
| 2725 | * First make sure individual recovery_offsets are correct |
| 2726 | * curr_resync_completed can only be used during recovery. |
| 2727 | * During reshape/resync it might use array-addresses rather |
| 2728 | * that device addresses. |
| 2729 | */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2730 | rdev_for_each(rdev, mddev) { |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2731 | if (rdev->raid_disk >= 0 && |
| 2732 | mddev->delta_disks >= 0 && |
NeilBrown | db0505d | 2017-10-17 16:18:36 +1100 | [diff] [blame] | 2733 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 2734 | test_bit(MD_RECOVERY_RECOVER, &mddev->recovery) && |
| 2735 | !test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2736 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2737 | !test_bit(In_sync, &rdev->flags) && |
| 2738 | mddev->curr_resync_completed > rdev->recovery_offset) |
| 2739 | rdev->recovery_offset = mddev->curr_resync_completed; |
| 2740 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2741 | } |
Dan Williams | bd52b74 | 2010-08-30 17:33:33 +1000 | [diff] [blame] | 2742 | if (!mddev->persistent) { |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2743 | clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags); |
| 2744 | clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2745 | if (!mddev->external) { |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2746 | clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2747 | rdev_for_each(rdev, mddev) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2748 | if (rdev->badblocks.changed) { |
NeilBrown | d096293 | 2012-03-19 12:46:41 +1100 | [diff] [blame] | 2749 | rdev->badblocks.changed = 0; |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 2750 | ack_all_badblocks(&rdev->badblocks); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2751 | md_error(mddev, rdev); |
| 2752 | } |
| 2753 | clear_bit(Blocked, &rdev->flags); |
| 2754 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2755 | wake_up(&rdev->blocked_wait); |
| 2756 | } |
| 2757 | } |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2758 | wake_up(&mddev->sb_wait); |
| 2759 | return; |
| 2760 | } |
| 2761 | |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 2762 | spin_lock(&mddev->lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2763 | |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 2764 | mddev->utime = ktime_get_real_seconds(); |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2765 | |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2766 | if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)) |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2767 | force_change = 1; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2768 | if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags)) |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2769 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 2770 | * though if events isn't the right even/odd, we will have to do |
| 2771 | * spares after all |
| 2772 | */ |
| 2773 | nospares = 1; |
| 2774 | if (force_change) |
| 2775 | nospares = 0; |
| 2776 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2777 | /* If the array is degraded, then skipping spares is both |
| 2778 | * dangerous and fairly pointless. |
| 2779 | * Dangerous because a device that was removed from the array |
| 2780 | * might have a event_count that still looks up-to-date, |
| 2781 | * so it can be re-added without a resync. |
| 2782 | * Pointless because if there are any spares to skip, |
| 2783 | * then a recovery will happen and soon that array won't |
| 2784 | * be degraded any more and the spare can go back to sleep then. |
| 2785 | */ |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2786 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2787 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2788 | sync_req = mddev->in_sync; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2789 | |
| 2790 | /* If this is just a dirty<->clean transition, and the array is clean |
| 2791 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2792 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2793 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2794 | && mddev->can_decrease_events |
| 2795 | && mddev->events != 1) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2796 | mddev->events--; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2797 | mddev->can_decrease_events = 0; |
| 2798 | } else { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2799 | /* otherwise we have to go forward and ... */ |
| 2800 | mddev->events ++; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2801 | mddev->can_decrease_events = nospares; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2802 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2803 | |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 2804 | /* |
| 2805 | * This 64-bit counter should never wrap. |
| 2806 | * Either we are in around ~1 trillion A.C., assuming |
| 2807 | * 1 reboot per second, or we have a bug... |
| 2808 | */ |
| 2809 | WARN_ON(mddev->events == 0); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2810 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2811 | rdev_for_each(rdev, mddev) { |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2812 | if (rdev->badblocks.changed) |
| 2813 | any_badblocks_changed++; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2814 | if (test_bit(Faulty, &rdev->flags)) |
| 2815 | set_bit(FaultRecorded, &rdev->flags); |
| 2816 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2817 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2818 | sync_sbs(mddev, nospares); |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 2819 | spin_unlock(&mddev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2820 | |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2821 | pr_debug("md: updating %s RAID superblock on device (in sync %d)\n", |
| 2822 | mdname(mddev), mddev->in_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2823 | |
Shaohua Li | 504634f | 2016-11-18 09:44:08 -0800 | [diff] [blame] | 2824 | if (mddev->queue) |
| 2825 | blk_add_trace_msg(mddev->queue, "md md_update_sb"); |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 2826 | rewrite: |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 2827 | md_bitmap_update_sb(mddev->bitmap); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2828 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2829 | char b[BDEVNAME_SIZE]; |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2830 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2831 | if (rdev->sb_loaded != 1) |
| 2832 | continue; /* no noise on spare devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2833 | |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2834 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2835 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2836 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2837 | rdev->sb_page); |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2838 | pr_debug("md: (write) %s's sb offset: %llu\n", |
| 2839 | bdevname(rdev->bdev, b), |
| 2840 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2841 | rdev->sb_events = mddev->events; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2842 | if (rdev->badblocks.size) { |
| 2843 | md_super_write(mddev, rdev, |
| 2844 | rdev->badblocks.sector, |
| 2845 | rdev->badblocks.size << 9, |
| 2846 | rdev->bb_page); |
| 2847 | rdev->badblocks.size = 0; |
| 2848 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2849 | |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2850 | } else |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2851 | pr_debug("md: %s (skipping faulty)\n", |
| 2852 | bdevname(rdev->bdev, b)); |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 2853 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2854 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2855 | /* only need to write one superblock... */ |
| 2856 | break; |
| 2857 | } |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 2858 | if (md_super_wait(mddev) < 0) |
| 2859 | goto rewrite; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2860 | /* 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] | 2861 | |
Guoqing Jiang | 2c97cf1 | 2016-05-02 11:33:09 -0400 | [diff] [blame] | 2862 | if (mddev_is_clustered(mddev) && ret == 0) |
| 2863 | md_cluster_ops->metadata_update_finish(mddev); |
| 2864 | |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2865 | if (mddev->in_sync != sync_req || |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2866 | !bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING), |
| 2867 | BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_CLEAN))) |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2868 | /* have to write it out again */ |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2869 | goto repeat; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2870 | wake_up(&mddev->sb_wait); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 2871 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 2872 | sysfs_notify_dirent_safe(mddev->sysfs_completed); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2873 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2874 | rdev_for_each(rdev, mddev) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2875 | if (test_and_clear_bit(FaultRecorded, &rdev->flags)) |
| 2876 | clear_bit(Blocked, &rdev->flags); |
| 2877 | |
| 2878 | if (any_badblocks_changed) |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 2879 | ack_all_badblocks(&rdev->badblocks); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2880 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2881 | wake_up(&rdev->blocked_wait); |
| 2882 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2883 | } |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 2884 | EXPORT_SYMBOL(md_update_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2885 | |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2886 | static int add_bound_rdev(struct md_rdev *rdev) |
| 2887 | { |
| 2888 | struct mddev *mddev = rdev->mddev; |
| 2889 | int err = 0; |
Shaohua Li | 87d4d91 | 2016-01-06 14:37:14 -0800 | [diff] [blame] | 2890 | bool add_journal = test_bit(Journal, &rdev->flags); |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2891 | |
Shaohua Li | 87d4d91 | 2016-01-06 14:37:14 -0800 | [diff] [blame] | 2892 | if (!mddev->pers->hot_remove_disk || add_journal) { |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2893 | /* If there is hot_add_disk but no hot_remove_disk |
| 2894 | * then added disks for geometry changes, |
| 2895 | * and should be added immediately. |
| 2896 | */ |
| 2897 | super_types[mddev->major_version]. |
| 2898 | validate_super(mddev, rdev); |
Shaohua Li | 87d4d91 | 2016-01-06 14:37:14 -0800 | [diff] [blame] | 2899 | if (add_journal) |
| 2900 | mddev_suspend(mddev); |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2901 | err = mddev->pers->hot_add_disk(mddev, rdev); |
Shaohua Li | 87d4d91 | 2016-01-06 14:37:14 -0800 | [diff] [blame] | 2902 | if (add_journal) |
| 2903 | mddev_resume(mddev); |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2904 | if (err) { |
Guoqing Jiang | db76767 | 2016-06-02 23:32:05 -0400 | [diff] [blame] | 2905 | md_kick_rdev_from_array(rdev); |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2906 | return err; |
| 2907 | } |
| 2908 | } |
| 2909 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
| 2910 | |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2911 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2912 | if (mddev->degraded) |
| 2913 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 2914 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 2915 | md_new_event(mddev); |
| 2916 | md_wakeup_thread(mddev->thread); |
| 2917 | return 0; |
| 2918 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2919 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 2920 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2921 | * We want to accept with case. For this we use cmd_match. |
| 2922 | */ |
| 2923 | static int cmd_match(const char *cmd, const char *str) |
| 2924 | { |
| 2925 | /* See if cmd, written into a sysfs file, matches |
| 2926 | * str. They must either be the same, or cmd can |
| 2927 | * have a trailing newline |
| 2928 | */ |
| 2929 | while (*cmd && *str && *cmd == *str) { |
| 2930 | cmd++; |
| 2931 | str++; |
| 2932 | } |
| 2933 | if (*cmd == '\n') |
| 2934 | cmd++; |
| 2935 | if (*str || *cmd) |
| 2936 | return 0; |
| 2937 | return 1; |
| 2938 | } |
| 2939 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2940 | struct rdev_sysfs_entry { |
| 2941 | struct attribute attr; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2942 | ssize_t (*show)(struct md_rdev *, char *); |
| 2943 | ssize_t (*store)(struct md_rdev *, const char *, size_t); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2944 | }; |
| 2945 | |
| 2946 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2947 | state_show(struct md_rdev *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2948 | { |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 2949 | char *sep = ","; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2950 | size_t len = 0; |
Mark Rutland | 6aa7de0 | 2017-10-23 14:07:29 -0700 | [diff] [blame] | 2951 | unsigned long flags = READ_ONCE(rdev->flags); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2952 | |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2953 | if (test_bit(Faulty, &flags) || |
Tomasz Majchrzak | dcbcb48 | 2016-10-21 16:27:08 +0200 | [diff] [blame] | 2954 | (!test_bit(ExternalBbl, &flags) && |
| 2955 | rdev->badblocks.unacked_exist)) |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 2956 | len += sprintf(page+len, "faulty%s", sep); |
| 2957 | if (test_bit(In_sync, &flags)) |
| 2958 | len += sprintf(page+len, "in_sync%s", sep); |
| 2959 | if (test_bit(Journal, &flags)) |
| 2960 | len += sprintf(page+len, "journal%s", sep); |
| 2961 | if (test_bit(WriteMostly, &flags)) |
| 2962 | len += sprintf(page+len, "write_mostly%s", sep); |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2963 | if (test_bit(Blocked, &flags) || |
NeilBrown | 52c6415 | 2011-12-08 16:22:48 +1100 | [diff] [blame] | 2964 | (rdev->badblocks.unacked_exist |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 2965 | && !test_bit(Faulty, &flags))) |
| 2966 | len += sprintf(page+len, "blocked%s", sep); |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2967 | if (!test_bit(Faulty, &flags) && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2968 | !test_bit(Journal, &flags) && |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 2969 | !test_bit(In_sync, &flags)) |
| 2970 | len += sprintf(page+len, "spare%s", sep); |
| 2971 | if (test_bit(WriteErrorSeen, &flags)) |
| 2972 | len += sprintf(page+len, "write_error%s", sep); |
| 2973 | if (test_bit(WantReplacement, &flags)) |
| 2974 | len += sprintf(page+len, "want_replacement%s", sep); |
| 2975 | if (test_bit(Replacement, &flags)) |
| 2976 | len += sprintf(page+len, "replacement%s", sep); |
| 2977 | if (test_bit(ExternalBbl, &flags)) |
| 2978 | len += sprintf(page+len, "external_bbl%s", sep); |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 2979 | if (test_bit(FailFast, &flags)) |
| 2980 | len += sprintf(page+len, "failfast%s", sep); |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 2981 | |
| 2982 | if (len) |
| 2983 | len -= strlen(sep); |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2984 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2985 | return len+sprintf(page+len, "\n"); |
| 2986 | } |
| 2987 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2988 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2989 | state_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2990 | { |
| 2991 | /* can write |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2992 | * faulty - simulates an error |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2993 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2994 | * writemostly - sets write_mostly |
| 2995 | * -writemostly - clears write_mostly |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2996 | * blocked - sets the Blocked flags |
| 2997 | * -blocked - clears the Blocked and possibly simulates an error |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2998 | * insync - sets Insync providing device isn't active |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2999 | * -insync - clear Insync for a device with a slot assigned, |
| 3000 | * so that it gets rebuilt based on bitmap |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 3001 | * write_error - sets WriteErrorSeen |
| 3002 | * -write_error - clears WriteErrorSeen |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 3003 | * {,-}failfast - set/clear FailFast |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3004 | */ |
| 3005 | int err = -EINVAL; |
| 3006 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 3007 | md_error(rdev->mddev, rdev); |
NeilBrown | 5ef56c8 | 2011-08-25 14:42:51 +1000 | [diff] [blame] | 3008 | if (test_bit(Faulty, &rdev->flags)) |
| 3009 | err = 0; |
| 3010 | else |
| 3011 | err = -EBUSY; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3012 | } else if (cmd_match(buf, "remove")) { |
Shaohua Li | 5d88178 | 2016-07-28 09:06:34 -0700 | [diff] [blame] | 3013 | if (rdev->mddev->pers) { |
| 3014 | clear_bit(Blocked, &rdev->flags); |
| 3015 | remove_and_add_spares(rdev->mddev, rdev); |
| 3016 | } |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3017 | if (rdev->raid_disk >= 0) |
| 3018 | err = -EBUSY; |
| 3019 | else { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3020 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3021 | err = 0; |
Guoqing Jiang | a972090 | 2015-10-12 17:21:27 +0800 | [diff] [blame] | 3022 | if (mddev_is_clustered(mddev)) |
| 3023 | err = md_cluster_ops->remove_disk(mddev, rdev); |
| 3024 | |
| 3025 | if (err == 0) { |
| 3026 | md_kick_rdev_from_array(rdev); |
NeilBrown | 060b068 | 2016-11-04 16:46:03 +1100 | [diff] [blame] | 3027 | if (mddev->pers) { |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 3028 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | 060b068 | 2016-11-04 16:46:03 +1100 | [diff] [blame] | 3029 | md_wakeup_thread(mddev->thread); |
| 3030 | } |
Guoqing Jiang | a972090 | 2015-10-12 17:21:27 +0800 | [diff] [blame] | 3031 | md_new_event(mddev); |
| 3032 | } |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3033 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3034 | } else if (cmd_match(buf, "writemostly")) { |
| 3035 | set_bit(WriteMostly, &rdev->flags); |
Guoqing Jiang | 404659c | 2019-12-23 10:48:53 +0100 | [diff] [blame] | 3036 | mddev_create_serial_pool(rdev->mddev, rdev, false); |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3037 | err = 0; |
| 3038 | } else if (cmd_match(buf, "-writemostly")) { |
Guoqing Jiang | 11d3a9f | 2019-12-23 10:48:55 +0100 | [diff] [blame] | 3039 | mddev_destroy_serial_pool(rdev->mddev, rdev, false); |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3040 | clear_bit(WriteMostly, &rdev->flags); |
| 3041 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3042 | } else if (cmd_match(buf, "blocked")) { |
| 3043 | set_bit(Blocked, &rdev->flags); |
| 3044 | err = 0; |
| 3045 | } else if (cmd_match(buf, "-blocked")) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 3046 | if (!test_bit(Faulty, &rdev->flags) && |
Tomasz Majchrzak | dcbcb48 | 2016-10-21 16:27:08 +0200 | [diff] [blame] | 3047 | !test_bit(ExternalBbl, &rdev->flags) && |
NeilBrown | 7da64a0 | 2011-08-30 16:20:17 +1000 | [diff] [blame] | 3048 | rdev->badblocks.unacked_exist) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 3049 | /* metadata handler doesn't understand badblocks, |
| 3050 | * so we need to fail the device |
| 3051 | */ |
| 3052 | md_error(rdev->mddev, rdev); |
| 3053 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3054 | clear_bit(Blocked, &rdev->flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 3055 | clear_bit(BlockedBadBlocks, &rdev->flags); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3056 | wake_up(&rdev->blocked_wait); |
| 3057 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 3058 | md_wakeup_thread(rdev->mddev->thread); |
| 3059 | |
| 3060 | err = 0; |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 3061 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { |
| 3062 | set_bit(In_sync, &rdev->flags); |
| 3063 | err = 0; |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 3064 | } else if (cmd_match(buf, "failfast")) { |
| 3065 | set_bit(FailFast, &rdev->flags); |
| 3066 | err = 0; |
| 3067 | } else if (cmd_match(buf, "-failfast")) { |
| 3068 | clear_bit(FailFast, &rdev->flags); |
| 3069 | err = 0; |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 3070 | } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0 && |
| 3071 | !test_bit(Journal, &rdev->flags)) { |
NeilBrown | e1960f8 | 2014-09-30 15:24:25 +1000 | [diff] [blame] | 3072 | if (rdev->mddev->pers == NULL) { |
| 3073 | clear_bit(In_sync, &rdev->flags); |
| 3074 | rdev->saved_raid_disk = rdev->raid_disk; |
| 3075 | rdev->raid_disk = -1; |
| 3076 | err = 0; |
| 3077 | } |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 3078 | } else if (cmd_match(buf, "write_error")) { |
| 3079 | set_bit(WriteErrorSeen, &rdev->flags); |
| 3080 | err = 0; |
| 3081 | } else if (cmd_match(buf, "-write_error")) { |
| 3082 | clear_bit(WriteErrorSeen, &rdev->flags); |
| 3083 | err = 0; |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 3084 | } else if (cmd_match(buf, "want_replacement")) { |
| 3085 | /* Any non-spare device that is not a replacement can |
| 3086 | * become want_replacement at any time, but we then need to |
| 3087 | * check if recovery is needed. |
| 3088 | */ |
| 3089 | if (rdev->raid_disk >= 0 && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 3090 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 3091 | !test_bit(Replacement, &rdev->flags)) |
| 3092 | set_bit(WantReplacement, &rdev->flags); |
| 3093 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 3094 | md_wakeup_thread(rdev->mddev->thread); |
| 3095 | err = 0; |
| 3096 | } else if (cmd_match(buf, "-want_replacement")) { |
| 3097 | /* Clearing 'want_replacement' is always allowed. |
| 3098 | * Once replacements starts it is too late though. |
| 3099 | */ |
| 3100 | err = 0; |
| 3101 | clear_bit(WantReplacement, &rdev->flags); |
| 3102 | } else if (cmd_match(buf, "replacement")) { |
| 3103 | /* Can only set a device as a replacement when array has not |
| 3104 | * yet been started. Once running, replacement is automatic |
| 3105 | * from spares, or by assigning 'slot'. |
| 3106 | */ |
| 3107 | if (rdev->mddev->pers) |
| 3108 | err = -EBUSY; |
| 3109 | else { |
| 3110 | set_bit(Replacement, &rdev->flags); |
| 3111 | err = 0; |
| 3112 | } |
| 3113 | } else if (cmd_match(buf, "-replacement")) { |
| 3114 | /* Similarly, can only clear Replacement before start */ |
| 3115 | if (rdev->mddev->pers) |
| 3116 | err = -EBUSY; |
| 3117 | else { |
| 3118 | clear_bit(Replacement, &rdev->flags); |
| 3119 | err = 0; |
| 3120 | } |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 3121 | } else if (cmd_match(buf, "re-add")) { |
Yufen Yu | ee37e62 | 2019-04-02 14:22:14 +0800 | [diff] [blame] | 3122 | if (!rdev->mddev->pers) |
| 3123 | err = -EINVAL; |
| 3124 | else if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1) && |
| 3125 | rdev->saved_raid_disk >= 0) { |
Goldwyn Rodrigues | 97f6cd3 | 2015-04-14 10:45:42 -0500 | [diff] [blame] | 3126 | /* clear_bit is performed _after_ all the devices |
| 3127 | * have their local Faulty bit cleared. If any writes |
| 3128 | * happen in the meantime in the local node, they |
| 3129 | * will land in the local bitmap, which will be synced |
| 3130 | * by this node eventually |
| 3131 | */ |
| 3132 | if (!mddev_is_clustered(rdev->mddev) || |
| 3133 | (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) { |
| 3134 | clear_bit(Faulty, &rdev->flags); |
| 3135 | err = add_bound_rdev(rdev); |
| 3136 | } |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 3137 | } else |
| 3138 | err = -EBUSY; |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 3139 | } else if (cmd_match(buf, "external_bbl") && (rdev->mddev->external)) { |
| 3140 | set_bit(ExternalBbl, &rdev->flags); |
| 3141 | rdev->badblocks.shift = 0; |
| 3142 | err = 0; |
| 3143 | } else if (cmd_match(buf, "-external_bbl") && (rdev->mddev->external)) { |
| 3144 | clear_bit(ExternalBbl, &rdev->flags); |
| 3145 | err = 0; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3146 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 3147 | if (!err) |
| 3148 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3149 | return err ? err : len; |
| 3150 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3151 | static struct rdev_sysfs_entry rdev_state = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 3152 | __ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3153 | |
| 3154 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3155 | errors_show(struct md_rdev *rdev, char *page) |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 3156 | { |
| 3157 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 3158 | } |
| 3159 | |
| 3160 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3161 | errors_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 3162 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3163 | unsigned int n; |
| 3164 | int rv; |
| 3165 | |
| 3166 | rv = kstrtouint(buf, 10, &n); |
| 3167 | if (rv < 0) |
| 3168 | return rv; |
| 3169 | atomic_set(&rdev->corrected_errors, n); |
| 3170 | return len; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 3171 | } |
| 3172 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3173 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 3174 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3175 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3176 | slot_show(struct md_rdev *rdev, char *page) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3177 | { |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 3178 | if (test_bit(Journal, &rdev->flags)) |
| 3179 | return sprintf(page, "journal\n"); |
| 3180 | else if (rdev->raid_disk < 0) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3181 | return sprintf(page, "none\n"); |
| 3182 | else |
| 3183 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 3184 | } |
| 3185 | |
| 3186 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3187 | slot_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3188 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3189 | int slot; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3190 | int err; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3191 | |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 3192 | if (test_bit(Journal, &rdev->flags)) |
| 3193 | return -EBUSY; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3194 | if (strncmp(buf, "none", 4)==0) |
| 3195 | slot = -1; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3196 | else { |
| 3197 | err = kstrtouint(buf, 10, (unsigned int *)&slot); |
| 3198 | if (err < 0) |
| 3199 | return err; |
| 3200 | } |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 3201 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3202 | /* Setting 'slot' on an active array requires also |
| 3203 | * updating the 'rd%d' link, and communicating |
| 3204 | * with the personality with ->hot_*_disk. |
| 3205 | * For now we only support removing |
| 3206 | * failed/spare devices. This normally happens automatically, |
| 3207 | * but not when the metadata is externally managed. |
| 3208 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3209 | if (rdev->raid_disk == -1) |
| 3210 | return -EEXIST; |
| 3211 | /* personality does all needed checks */ |
Namhyung Kim | 01393f3 | 2011-06-09 11:42:54 +1000 | [diff] [blame] | 3212 | if (rdev->mddev->pers->hot_remove_disk == NULL) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3213 | return -EINVAL; |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 3214 | clear_bit(Blocked, &rdev->flags); |
| 3215 | remove_and_add_spares(rdev->mddev, rdev); |
| 3216 | if (rdev->raid_disk >= 0) |
| 3217 | return -EBUSY; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3218 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 3219 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 3220 | } else if (rdev->mddev->pers) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 3221 | /* Activating a spare .. or possibly reactivating |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 3222 | * if we ever get bitmaps working here. |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 3223 | */ |
Goldwyn Rodrigues | cb01c54 | 2015-12-18 15:19:16 +1100 | [diff] [blame] | 3224 | int err; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 3225 | |
| 3226 | if (rdev->raid_disk != -1) |
| 3227 | return -EBUSY; |
| 3228 | |
NeilBrown | c6751b2 | 2011-02-02 11:57:13 +1100 | [diff] [blame] | 3229 | if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery)) |
| 3230 | return -EBUSY; |
| 3231 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 3232 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 3233 | return -EINVAL; |
| 3234 | |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 3235 | if (slot >= rdev->mddev->raid_disks && |
| 3236 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
| 3237 | return -ENOSPC; |
| 3238 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 3239 | rdev->raid_disk = slot; |
| 3240 | if (test_bit(In_sync, &rdev->flags)) |
| 3241 | rdev->saved_raid_disk = slot; |
| 3242 | else |
| 3243 | rdev->saved_raid_disk = -1; |
NeilBrown | d30519f | 2011-10-18 12:13:47 +1100 | [diff] [blame] | 3244 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 3245 | clear_bit(Bitmap_sync, &rdev->flags); |
Guoqing Jiang | 3f79cc2 | 2020-04-04 23:57:11 +0200 | [diff] [blame] | 3246 | err = rdev->mddev->pers->hot_add_disk(rdev->mddev, rdev); |
Goldwyn Rodrigues | cb01c54 | 2015-12-18 15:19:16 +1100 | [diff] [blame] | 3247 | if (err) { |
| 3248 | rdev->raid_disk = -1; |
| 3249 | return err; |
| 3250 | } else |
| 3251 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Damien Le Moal | 5e3b8a8 | 2020-07-16 13:54:40 +0900 | [diff] [blame] | 3252 | /* failure here is OK */; |
| 3253 | sysfs_link_rdev(rdev->mddev, rdev); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 3254 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3255 | } else { |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 3256 | if (slot >= rdev->mddev->raid_disks && |
| 3257 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3258 | return -ENOSPC; |
| 3259 | rdev->raid_disk = slot; |
| 3260 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3261 | clear_bit(Faulty, &rdev->flags); |
| 3262 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3263 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 3264 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3265 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3266 | return len; |
| 3267 | } |
| 3268 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3269 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3270 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3271 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3272 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3273 | offset_show(struct md_rdev *rdev, char *page) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3274 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 3275 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3276 | } |
| 3277 | |
| 3278 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3279 | offset_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3280 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3281 | unsigned long long offset; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 3282 | if (kstrtoull(buf, 10, &offset) < 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3283 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 3284 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3285 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3286 | if (rdev->sectors && rdev->mddev->external) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3287 | /* Must set offset before size, so overlap checks |
| 3288 | * can be sane */ |
| 3289 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3290 | rdev->data_offset = offset; |
NeilBrown | 25f7fd4 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3291 | rdev->new_data_offset = offset; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3292 | return len; |
| 3293 | } |
| 3294 | |
| 3295 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3296 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3297 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3298 | static ssize_t new_offset_show(struct md_rdev *rdev, char *page) |
| 3299 | { |
| 3300 | return sprintf(page, "%llu\n", |
| 3301 | (unsigned long long)rdev->new_data_offset); |
| 3302 | } |
| 3303 | |
| 3304 | static ssize_t new_offset_store(struct md_rdev *rdev, |
| 3305 | const char *buf, size_t len) |
| 3306 | { |
| 3307 | unsigned long long new_offset; |
| 3308 | struct mddev *mddev = rdev->mddev; |
| 3309 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 3310 | if (kstrtoull(buf, 10, &new_offset) < 0) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3311 | return -EINVAL; |
| 3312 | |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 3313 | if (mddev->sync_thread || |
| 3314 | test_bit(MD_RECOVERY_RUNNING,&mddev->recovery)) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3315 | return -EBUSY; |
| 3316 | if (new_offset == rdev->data_offset) |
| 3317 | /* reset is always permitted */ |
| 3318 | ; |
| 3319 | else if (new_offset > rdev->data_offset) { |
| 3320 | /* must not push array size beyond rdev_sectors */ |
| 3321 | if (new_offset - rdev->data_offset |
| 3322 | + mddev->dev_sectors > rdev->sectors) |
| 3323 | return -E2BIG; |
| 3324 | } |
| 3325 | /* Metadata worries about other space details. */ |
| 3326 | |
| 3327 | /* decreasing the offset is inconsistent with a backwards |
| 3328 | * reshape. |
| 3329 | */ |
| 3330 | if (new_offset < rdev->data_offset && |
| 3331 | mddev->reshape_backwards) |
| 3332 | return -EINVAL; |
| 3333 | /* Increasing offset is inconsistent with forwards |
| 3334 | * reshape. reshape_direction should be set to |
| 3335 | * 'backwards' first. |
| 3336 | */ |
| 3337 | if (new_offset > rdev->data_offset && |
| 3338 | !mddev->reshape_backwards) |
| 3339 | return -EINVAL; |
| 3340 | |
| 3341 | if (mddev->pers && mddev->persistent && |
| 3342 | !super_types[mddev->major_version] |
| 3343 | .allow_new_offset(rdev, new_offset)) |
| 3344 | return -E2BIG; |
| 3345 | rdev->new_data_offset = new_offset; |
| 3346 | if (new_offset > rdev->data_offset) |
| 3347 | mddev->reshape_backwards = 1; |
| 3348 | else if (new_offset < rdev->data_offset) |
| 3349 | mddev->reshape_backwards = 0; |
| 3350 | |
| 3351 | return len; |
| 3352 | } |
| 3353 | static struct rdev_sysfs_entry rdev_new_offset = |
| 3354 | __ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store); |
| 3355 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3356 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3357 | rdev_size_show(struct md_rdev *rdev, char *page) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3358 | { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3359 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3360 | } |
| 3361 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3362 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 3363 | { |
| 3364 | /* check if two start/length pairs overlap */ |
| 3365 | if (s1+l1 <= s2) |
| 3366 | return 0; |
| 3367 | if (s2+l2 <= s1) |
| 3368 | return 0; |
| 3369 | return 1; |
| 3370 | } |
| 3371 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3372 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
| 3373 | { |
| 3374 | unsigned long long blocks; |
| 3375 | sector_t new; |
| 3376 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 3377 | if (kstrtoull(buf, 10, &blocks) < 0) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3378 | return -EINVAL; |
| 3379 | |
| 3380 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) |
| 3381 | return -EINVAL; /* sector conversion overflow */ |
| 3382 | |
| 3383 | new = blocks * 2; |
| 3384 | if (new != blocks * 2) |
| 3385 | return -EINVAL; /* unsigned long long to sector_t overflow */ |
| 3386 | |
| 3387 | *sectors = new; |
| 3388 | return 0; |
| 3389 | } |
| 3390 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3391 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3392 | rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3393 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3394 | struct mddev *my_mddev = rdev->mddev; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3395 | sector_t oldsectors = rdev->sectors; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3396 | sector_t sectors; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3397 | |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 3398 | if (test_bit(Journal, &rdev->flags)) |
| 3399 | return -EBUSY; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3400 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 3401 | return -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3402 | if (rdev->data_offset != rdev->new_data_offset) |
| 3403 | return -EINVAL; /* too confusing */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 3404 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 3405 | if (my_mddev->persistent) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3406 | sectors = super_types[my_mddev->major_version]. |
| 3407 | rdev_size_change(rdev, sectors); |
| 3408 | if (!sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 3409 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3410 | } else if (!sectors) |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 3411 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) - |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3412 | rdev->data_offset; |
NeilBrown | a646853 | 2013-02-21 14:33:17 +1100 | [diff] [blame] | 3413 | if (!my_mddev->pers->resize) |
| 3414 | /* Cannot change size for RAID0 or Linear etc */ |
| 3415 | return -EINVAL; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 3416 | } |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3417 | if (sectors < my_mddev->dev_sectors) |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 3418 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 3419 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3420 | rdev->sectors = sectors; |
| 3421 | if (sectors > oldsectors && my_mddev->external) { |
NeilBrown | 8b1afc3 | 2014-09-29 15:33:20 +1000 | [diff] [blame] | 3422 | /* Need to check that all other rdevs with the same |
| 3423 | * ->bdev do not overlap. 'rcu' is sufficient to walk |
| 3424 | * the rdev lists safely. |
| 3425 | * This check does not provide a hard guarantee, it |
| 3426 | * just helps avoid dangerous mistakes. |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3427 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3428 | struct mddev *mddev; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3429 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3430 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3431 | |
NeilBrown | 8b1afc3 | 2014-09-29 15:33:20 +1000 | [diff] [blame] | 3432 | rcu_read_lock(); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 3433 | for_each_mddev(mddev, tmp) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3434 | struct md_rdev *rdev2; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3435 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3436 | rdev_for_each(rdev2, mddev) |
NeilBrown | f21e9ff | 2011-01-31 12:10:09 +1100 | [diff] [blame] | 3437 | if (rdev->bdev == rdev2->bdev && |
| 3438 | rdev != rdev2 && |
| 3439 | overlaps(rdev->data_offset, rdev->sectors, |
| 3440 | rdev2->data_offset, |
| 3441 | rdev2->sectors)) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3442 | overlap = 1; |
| 3443 | break; |
| 3444 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3445 | if (overlap) { |
| 3446 | mddev_put(mddev); |
| 3447 | break; |
| 3448 | } |
| 3449 | } |
NeilBrown | 8b1afc3 | 2014-09-29 15:33:20 +1000 | [diff] [blame] | 3450 | rcu_read_unlock(); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3451 | if (overlap) { |
| 3452 | /* Someone else could have slipped in a size |
| 3453 | * change here, but doing so is just silly. |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3454 | * We put oldsectors back because we *know* it is |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3455 | * safe, and trust userspace not to race with |
| 3456 | * itself |
| 3457 | */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3458 | rdev->sectors = oldsectors; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3459 | return -EBUSY; |
| 3460 | } |
| 3461 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3462 | return len; |
| 3463 | } |
| 3464 | |
| 3465 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3466 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3467 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3468 | static ssize_t recovery_start_show(struct md_rdev *rdev, char *page) |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3469 | { |
| 3470 | unsigned long long recovery_start = rdev->recovery_offset; |
| 3471 | |
| 3472 | if (test_bit(In_sync, &rdev->flags) || |
| 3473 | recovery_start == MaxSector) |
| 3474 | return sprintf(page, "none\n"); |
| 3475 | |
| 3476 | return sprintf(page, "%llu\n", recovery_start); |
| 3477 | } |
| 3478 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3479 | 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] | 3480 | { |
| 3481 | unsigned long long recovery_start; |
| 3482 | |
| 3483 | if (cmd_match(buf, "none")) |
| 3484 | recovery_start = MaxSector; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 3485 | else if (kstrtoull(buf, 10, &recovery_start)) |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3486 | return -EINVAL; |
| 3487 | |
| 3488 | if (rdev->mddev->pers && |
| 3489 | rdev->raid_disk >= 0) |
| 3490 | return -EBUSY; |
| 3491 | |
| 3492 | rdev->recovery_offset = recovery_start; |
| 3493 | if (recovery_start == MaxSector) |
| 3494 | set_bit(In_sync, &rdev->flags); |
| 3495 | else |
| 3496 | clear_bit(In_sync, &rdev->flags); |
| 3497 | return len; |
| 3498 | } |
| 3499 | |
| 3500 | static struct rdev_sysfs_entry rdev_recovery_start = |
| 3501 | __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store); |
| 3502 | |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 3503 | /* sysfs access to bad-blocks list. |
| 3504 | * We present two files. |
| 3505 | * 'bad-blocks' lists sector numbers and lengths of ranges that |
| 3506 | * are recorded as bad. The list is truncated to fit within |
| 3507 | * the one-page limit of sysfs. |
| 3508 | * Writing "sector length" to this file adds an acknowledged |
| 3509 | * bad block list. |
| 3510 | * 'unacknowledged-bad-blocks' lists bad blocks that have not yet |
| 3511 | * been acknowledged. Writing to this file adds bad blocks |
| 3512 | * without acknowledging them. This is largely for testing. |
| 3513 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3514 | static ssize_t bb_show(struct md_rdev *rdev, char *page) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3515 | { |
| 3516 | return badblocks_show(&rdev->badblocks, page, 0); |
| 3517 | } |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3518 | 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] | 3519 | { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 3520 | int rv = badblocks_store(&rdev->badblocks, page, len, 0); |
| 3521 | /* Maybe that ack was all we needed */ |
| 3522 | if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags)) |
| 3523 | wake_up(&rdev->blocked_wait); |
| 3524 | return rv; |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3525 | } |
| 3526 | static struct rdev_sysfs_entry rdev_bad_blocks = |
| 3527 | __ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store); |
| 3528 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3529 | static ssize_t ubb_show(struct md_rdev *rdev, char *page) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3530 | { |
| 3531 | return badblocks_show(&rdev->badblocks, page, 1); |
| 3532 | } |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3533 | 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] | 3534 | { |
| 3535 | return badblocks_store(&rdev->badblocks, page, len, 1); |
| 3536 | } |
| 3537 | static struct rdev_sysfs_entry rdev_unack_bad_blocks = |
| 3538 | __ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store); |
| 3539 | |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 3540 | static ssize_t |
| 3541 | ppl_sector_show(struct md_rdev *rdev, char *page) |
| 3542 | { |
| 3543 | return sprintf(page, "%llu\n", (unsigned long long)rdev->ppl.sector); |
| 3544 | } |
| 3545 | |
| 3546 | static ssize_t |
| 3547 | ppl_sector_store(struct md_rdev *rdev, const char *buf, size_t len) |
| 3548 | { |
| 3549 | unsigned long long sector; |
| 3550 | |
| 3551 | if (kstrtoull(buf, 10, §or) < 0) |
| 3552 | return -EINVAL; |
| 3553 | if (sector != (sector_t)sector) |
| 3554 | return -EINVAL; |
| 3555 | |
| 3556 | if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) && |
| 3557 | rdev->raid_disk >= 0) |
| 3558 | return -EBUSY; |
| 3559 | |
| 3560 | if (rdev->mddev->persistent) { |
| 3561 | if (rdev->mddev->major_version == 0) |
| 3562 | return -EINVAL; |
| 3563 | if ((sector > rdev->sb_start && |
| 3564 | sector - rdev->sb_start > S16_MAX) || |
| 3565 | (sector < rdev->sb_start && |
| 3566 | rdev->sb_start - sector > -S16_MIN)) |
| 3567 | return -EINVAL; |
| 3568 | rdev->ppl.offset = sector - rdev->sb_start; |
| 3569 | } else if (!rdev->mddev->external) { |
| 3570 | return -EBUSY; |
| 3571 | } |
| 3572 | rdev->ppl.sector = sector; |
| 3573 | return len; |
| 3574 | } |
| 3575 | |
| 3576 | static struct rdev_sysfs_entry rdev_ppl_sector = |
| 3577 | __ATTR(ppl_sector, S_IRUGO|S_IWUSR, ppl_sector_show, ppl_sector_store); |
| 3578 | |
| 3579 | static ssize_t |
| 3580 | ppl_size_show(struct md_rdev *rdev, char *page) |
| 3581 | { |
| 3582 | return sprintf(page, "%u\n", rdev->ppl.size); |
| 3583 | } |
| 3584 | |
| 3585 | static ssize_t |
| 3586 | ppl_size_store(struct md_rdev *rdev, const char *buf, size_t len) |
| 3587 | { |
| 3588 | unsigned int size; |
| 3589 | |
| 3590 | if (kstrtouint(buf, 10, &size) < 0) |
| 3591 | return -EINVAL; |
| 3592 | |
| 3593 | if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) && |
| 3594 | rdev->raid_disk >= 0) |
| 3595 | return -EBUSY; |
| 3596 | |
| 3597 | if (rdev->mddev->persistent) { |
| 3598 | if (rdev->mddev->major_version == 0) |
| 3599 | return -EINVAL; |
| 3600 | if (size > U16_MAX) |
| 3601 | return -EINVAL; |
| 3602 | } else if (!rdev->mddev->external) { |
| 3603 | return -EBUSY; |
| 3604 | } |
| 3605 | rdev->ppl.size = size; |
| 3606 | return len; |
| 3607 | } |
| 3608 | |
| 3609 | static struct rdev_sysfs_entry rdev_ppl_size = |
| 3610 | __ATTR(ppl_size, S_IRUGO|S_IWUSR, ppl_size_show, ppl_size_store); |
| 3611 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3612 | static struct attribute *rdev_default_attrs[] = { |
| 3613 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 3614 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3615 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3616 | &rdev_offset.attr, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3617 | &rdev_new_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3618 | &rdev_size.attr, |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3619 | &rdev_recovery_start.attr, |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3620 | &rdev_bad_blocks.attr, |
| 3621 | &rdev_unack_bad_blocks.attr, |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 3622 | &rdev_ppl_sector.attr, |
| 3623 | &rdev_ppl_size.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3624 | NULL, |
| 3625 | }; |
| 3626 | static ssize_t |
| 3627 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 3628 | { |
| 3629 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3630 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3631 | |
| 3632 | if (!entry->show) |
| 3633 | return -EIO; |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 3634 | if (!rdev->mddev) |
Marcos Paulo de Souza | 168b305 | 2019-06-14 15:41:06 -0700 | [diff] [blame] | 3635 | return -ENODEV; |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 3636 | return entry->show(rdev, page); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3637 | } |
| 3638 | |
| 3639 | static ssize_t |
| 3640 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 3641 | const char *page, size_t length) |
| 3642 | { |
| 3643 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3644 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3645 | ssize_t rv; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3646 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3647 | |
| 3648 | if (!entry->store) |
| 3649 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 3650 | if (!capable(CAP_SYS_ADMIN)) |
| 3651 | return -EACCES; |
Pawel Baldysiak | c42d324 | 2019-03-27 13:48:21 +0100 | [diff] [blame] | 3652 | rv = mddev ? mddev_lock(mddev) : -ENODEV; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 3653 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3654 | if (rdev->mddev == NULL) |
Pawel Baldysiak | c42d324 | 2019-03-27 13:48:21 +0100 | [diff] [blame] | 3655 | rv = -ENODEV; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3656 | else |
| 3657 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 3658 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 3659 | } |
| 3660 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3661 | } |
| 3662 | |
| 3663 | static void rdev_free(struct kobject *ko) |
| 3664 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3665 | struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3666 | kfree(rdev); |
| 3667 | } |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 3668 | static const struct sysfs_ops rdev_sysfs_ops = { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3669 | .show = rdev_attr_show, |
| 3670 | .store = rdev_attr_store, |
| 3671 | }; |
| 3672 | static struct kobj_type rdev_ktype = { |
| 3673 | .release = rdev_free, |
| 3674 | .sysfs_ops = &rdev_sysfs_ops, |
| 3675 | .default_attrs = rdev_default_attrs, |
| 3676 | }; |
| 3677 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3678 | int md_rdev_init(struct md_rdev *rdev) |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3679 | { |
| 3680 | rdev->desc_nr = -1; |
| 3681 | rdev->saved_raid_disk = -1; |
| 3682 | rdev->raid_disk = -1; |
| 3683 | rdev->flags = 0; |
| 3684 | rdev->data_offset = 0; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3685 | rdev->new_data_offset = 0; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3686 | rdev->sb_events = 0; |
Arnd Bergmann | 0e3ef49 | 2016-06-17 17:33:10 +0200 | [diff] [blame] | 3687 | rdev->last_read_error = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3688 | rdev->sb_loaded = 0; |
| 3689 | rdev->bb_page = NULL; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3690 | atomic_set(&rdev->nr_pending, 0); |
| 3691 | atomic_set(&rdev->read_errors, 0); |
| 3692 | atomic_set(&rdev->corrected_errors, 0); |
| 3693 | |
| 3694 | INIT_LIST_HEAD(&rdev->same_set); |
| 3695 | init_waitqueue_head(&rdev->blocked_wait); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3696 | |
| 3697 | /* Add space to store bad block list. |
| 3698 | * This reserves the space even on arrays where it cannot |
| 3699 | * be used - I wonder if that matters |
| 3700 | */ |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 3701 | return badblocks_init(&rdev->badblocks, 0); |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3702 | } |
| 3703 | EXPORT_SYMBOL_GPL(md_rdev_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3704 | /* |
| 3705 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 3706 | * |
| 3707 | * mark the device faulty if: |
| 3708 | * |
| 3709 | * - the device is nonexistent (zero size) |
| 3710 | * - the device has no valid superblock |
| 3711 | * |
| 3712 | * a faulty rdev _never_ has rdev->sb set. |
| 3713 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3714 | 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] | 3715 | { |
| 3716 | char b[BDEVNAME_SIZE]; |
| 3717 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3718 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3719 | sector_t size; |
| 3720 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 3721 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3722 | if (!rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3723 | return ERR_PTR(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3724 | |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3725 | err = md_rdev_init(rdev); |
| 3726 | if (err) |
| 3727 | goto abort_free; |
| 3728 | err = alloc_disk_sb(rdev); |
| 3729 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3730 | goto abort_free; |
| 3731 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3732 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3733 | if (err) |
| 3734 | goto abort_free; |
| 3735 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 3736 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3737 | |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 3738 | size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3739 | if (!size) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3740 | pr_warn("md: %s has zero or unknown size, marking faulty!\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3741 | bdevname(rdev->bdev,b)); |
| 3742 | err = -EINVAL; |
| 3743 | goto abort_free; |
| 3744 | } |
| 3745 | |
| 3746 | if (super_format >= 0) { |
| 3747 | err = super_types[super_format]. |
| 3748 | load_super(rdev, NULL, super_minor); |
| 3749 | if (err == -EINVAL) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3750 | 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] | 3751 | bdevname(rdev->bdev,b), |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3752 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3753 | goto abort_free; |
| 3754 | } |
| 3755 | if (err < 0) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3756 | pr_warn("md: could not read %s's sb, not importing!\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3757 | bdevname(rdev->bdev,b)); |
| 3758 | goto abort_free; |
| 3759 | } |
| 3760 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3761 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3762 | return rdev; |
| 3763 | |
| 3764 | abort_free: |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3765 | if (rdev->bdev) |
| 3766 | unlock_rdev(rdev); |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 3767 | md_rdev_clear(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3768 | kfree(rdev); |
| 3769 | return ERR_PTR(err); |
| 3770 | } |
| 3771 | |
| 3772 | /* |
| 3773 | * Check a full RAID array for plausibility |
| 3774 | */ |
| 3775 | |
Yufen Yu | 6a5cb53 | 2019-10-16 16:00:03 +0800 | [diff] [blame] | 3776 | static int analyze_sbs(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3777 | { |
| 3778 | int i; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3779 | struct md_rdev *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3780 | char b[BDEVNAME_SIZE]; |
| 3781 | |
| 3782 | freshest = NULL; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3783 | rdev_for_each_safe(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3784 | switch (super_types[mddev->major_version]. |
| 3785 | load_super(rdev, freshest, mddev->minor_version)) { |
| 3786 | case 1: |
| 3787 | freshest = rdev; |
| 3788 | break; |
| 3789 | case 0: |
| 3790 | break; |
| 3791 | default: |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3792 | pr_warn("md: fatal superblock inconsistency in %s -- removing from array\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3793 | bdevname(rdev->bdev,b)); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 3794 | md_kick_rdev_from_array(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3795 | } |
| 3796 | |
Yufen Yu | 6a5cb53 | 2019-10-16 16:00:03 +0800 | [diff] [blame] | 3797 | /* Cannot find a valid fresh disk */ |
| 3798 | if (!freshest) { |
| 3799 | pr_warn("md: cannot find a valid disk\n"); |
| 3800 | return -EINVAL; |
| 3801 | } |
| 3802 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3803 | super_types[mddev->major_version]. |
| 3804 | validate_super(mddev, freshest); |
| 3805 | |
| 3806 | i = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3807 | rdev_for_each_safe(rdev, tmp, mddev) { |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 3808 | if (mddev->max_disks && |
| 3809 | (rdev->desc_nr >= mddev->max_disks || |
| 3810 | i > mddev->max_disks)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3811 | pr_warn("md: %s: %s: only %d devices permitted\n", |
| 3812 | mdname(mddev), bdevname(rdev->bdev, b), |
| 3813 | mddev->max_disks); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 3814 | md_kick_rdev_from_array(rdev); |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 3815 | continue; |
| 3816 | } |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 3817 | if (rdev != freshest) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3818 | if (super_types[mddev->major_version]. |
| 3819 | validate_super(mddev, rdev)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3820 | pr_warn("md: kicking non-fresh %s from array!\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3821 | bdevname(rdev->bdev,b)); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 3822 | md_kick_rdev_from_array(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3823 | continue; |
| 3824 | } |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 3825 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3826 | if (mddev->level == LEVEL_MULTIPATH) { |
| 3827 | rdev->desc_nr = i++; |
| 3828 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3829 | set_bit(In_sync, &rdev->flags); |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 3830 | } else if (rdev->raid_disk >= |
| 3831 | (mddev->raid_disks - min(0, mddev->delta_disks)) && |
| 3832 | !test_bit(Journal, &rdev->flags)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 3833 | rdev->raid_disk = -1; |
| 3834 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3835 | } |
| 3836 | } |
Yufen Yu | 6a5cb53 | 2019-10-16 16:00:03 +0800 | [diff] [blame] | 3837 | |
| 3838 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3839 | } |
| 3840 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3841 | /* Read a fixed-point number. |
| 3842 | * Numbers in sysfs attributes should be in "standard" units where |
| 3843 | * possible, so time should be in seconds. |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3844 | * However we internally use a a much smaller unit such as |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3845 | * milliseconds or jiffies. |
| 3846 | * This function takes a decimal number with a possible fractional |
| 3847 | * component, and produces an integer which is the result of |
| 3848 | * multiplying that number by 10^'scale'. |
| 3849 | * all without any floating-point arithmetic. |
| 3850 | */ |
| 3851 | int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale) |
| 3852 | { |
| 3853 | unsigned long result = 0; |
| 3854 | long decimals = -1; |
| 3855 | while (isdigit(*cp) || (*cp == '.' && decimals < 0)) { |
| 3856 | if (*cp == '.') |
| 3857 | decimals = 0; |
| 3858 | else if (decimals < scale) { |
| 3859 | unsigned int value; |
| 3860 | value = *cp - '0'; |
| 3861 | result = result * 10 + value; |
| 3862 | if (decimals >= 0) |
| 3863 | decimals++; |
| 3864 | } |
| 3865 | cp++; |
| 3866 | } |
| 3867 | if (*cp == '\n') |
| 3868 | cp++; |
| 3869 | if (*cp) |
| 3870 | return -EINVAL; |
| 3871 | if (decimals < 0) |
| 3872 | decimals = 0; |
Andy Shevchenko | cf89160 | 2019-07-23 23:41:55 +0300 | [diff] [blame] | 3873 | *res = result * int_pow(10, scale - decimals); |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3874 | return 0; |
| 3875 | } |
| 3876 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3877 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3878 | safe_delay_show(struct mddev *mddev, char *page) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3879 | { |
| 3880 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 3881 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 3882 | } |
| 3883 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3884 | safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3885 | { |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3886 | unsigned long msec; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 3887 | |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 3888 | if (mddev_is_clustered(mddev)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3889 | pr_warn("md: Safemode is disabled for clustered mode\n"); |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 3890 | return -EINVAL; |
| 3891 | } |
| 3892 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3893 | if (strict_strtoul_scaled(cbuf, &msec, 3) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3894 | return -EINVAL; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3895 | if (msec == 0) |
| 3896 | mddev->safemode_delay = 0; |
| 3897 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3898 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 1b30e66 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 3899 | unsigned long new_delay = (msec*HZ)/1000; |
| 3900 | |
| 3901 | if (new_delay == 0) |
| 3902 | new_delay = 1; |
| 3903 | mddev->safemode_delay = new_delay; |
| 3904 | if (new_delay < old_delay || old_delay == 0) |
| 3905 | mod_timer(&mddev->safemode_timer, jiffies+1); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3906 | } |
| 3907 | return len; |
| 3908 | } |
| 3909 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3910 | __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] | 3911 | |
| 3912 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3913 | level_show(struct mddev *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3914 | { |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3915 | struct md_personality *p; |
| 3916 | int ret; |
| 3917 | spin_lock(&mddev->lock); |
| 3918 | p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3919 | if (p) |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3920 | ret = sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3921 | else if (mddev->clevel[0]) |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3922 | ret = sprintf(page, "%s\n", mddev->clevel); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3923 | else if (mddev->level != LEVEL_NONE) |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3924 | ret = sprintf(page, "%d\n", mddev->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3925 | else |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3926 | ret = 0; |
| 3927 | spin_unlock(&mddev->lock); |
| 3928 | return ret; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3929 | } |
| 3930 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3931 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3932 | level_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3933 | { |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3934 | char clevel[16]; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3935 | ssize_t rv; |
| 3936 | size_t slen = len; |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3937 | struct md_personality *pers, *oldpers; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3938 | long level; |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3939 | void *priv, *oldpriv; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3940 | struct md_rdev *rdev; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3941 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3942 | if (slen == 0 || slen >= sizeof(clevel)) |
| 3943 | return -EINVAL; |
| 3944 | |
| 3945 | rv = mddev_lock(mddev); |
| 3946 | if (rv) |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3947 | return rv; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3948 | |
| 3949 | if (mddev->pers == NULL) { |
| 3950 | strncpy(mddev->clevel, buf, slen); |
| 3951 | if (mddev->clevel[slen-1] == '\n') |
| 3952 | slen--; |
| 3953 | mddev->clevel[slen] = 0; |
| 3954 | mddev->level = LEVEL_NONE; |
| 3955 | rv = len; |
| 3956 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3957 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3958 | rv = -EROFS; |
NeilBrown | bd8839e | 2014-05-28 13:39:21 +1000 | [diff] [blame] | 3959 | if (mddev->ro) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3960 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3961 | |
| 3962 | /* request to change the personality. Need to ensure: |
| 3963 | * - array is not engaged in resync/recovery/reshape |
| 3964 | * - old personality can be suspended |
| 3965 | * - new personality will access other array. |
| 3966 | */ |
| 3967 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3968 | rv = -EBUSY; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 3969 | if (mddev->sync_thread || |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 3970 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 3971 | mddev->reshape_position != MaxSector || |
| 3972 | mddev->sysfs_active) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3973 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3974 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3975 | rv = -EINVAL; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3976 | if (!mddev->pers->quiesce) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3977 | pr_warn("md: %s: %s does not support online personality change\n", |
| 3978 | mdname(mddev), mddev->pers->name); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3979 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3980 | } |
| 3981 | |
| 3982 | /* Now find the new personality */ |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3983 | strncpy(clevel, buf, slen); |
| 3984 | if (clevel[slen-1] == '\n') |
| 3985 | slen--; |
| 3986 | clevel[slen] = 0; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 3987 | if (kstrtol(clevel, 10, &level)) |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3988 | level = LEVEL_NONE; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3989 | |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3990 | if (request_module("md-%s", clevel) != 0) |
| 3991 | request_module("md-level-%s", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3992 | spin_lock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3993 | pers = find_pers(level, clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3994 | if (!pers || !try_module_get(pers->owner)) { |
| 3995 | spin_unlock(&pers_lock); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3996 | pr_warn("md: personality %s not loaded\n", clevel); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3997 | rv = -EINVAL; |
| 3998 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3999 | } |
| 4000 | spin_unlock(&pers_lock); |
| 4001 | |
| 4002 | if (pers == mddev->pers) { |
| 4003 | /* Nothing to do! */ |
| 4004 | module_put(pers->owner); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4005 | rv = len; |
| 4006 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4007 | } |
| 4008 | if (!pers->takeover) { |
| 4009 | module_put(pers->owner); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 4010 | pr_warn("md: %s: %s does not support personality takeover\n", |
| 4011 | mdname(mddev), clevel); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4012 | rv = -EINVAL; |
| 4013 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4014 | } |
| 4015 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 4016 | rdev_for_each(rdev, mddev) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 4017 | rdev->new_raid_disk = rdev->raid_disk; |
| 4018 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4019 | /* ->takeover must set new_* and/or delta_disks |
| 4020 | * if it succeeds, and may set them when it fails. |
| 4021 | */ |
| 4022 | priv = pers->takeover(mddev); |
| 4023 | if (IS_ERR(priv)) { |
| 4024 | mddev->new_level = mddev->level; |
| 4025 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4026 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4027 | mddev->raid_disks -= mddev->delta_disks; |
| 4028 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4029 | mddev->reshape_backwards = 0; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4030 | module_put(pers->owner); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 4031 | pr_warn("md: %s: %s would not accept array\n", |
| 4032 | mdname(mddev), clevel); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4033 | rv = PTR_ERR(priv); |
| 4034 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4035 | } |
| 4036 | |
| 4037 | /* Looks like we have a winner */ |
| 4038 | mddev_suspend(mddev); |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 4039 | mddev_detach(mddev); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 4040 | |
| 4041 | spin_lock(&mddev->lock); |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 4042 | oldpers = mddev->pers; |
| 4043 | oldpriv = mddev->private; |
| 4044 | mddev->pers = pers; |
| 4045 | mddev->private = priv; |
| 4046 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
| 4047 | mddev->level = mddev->new_level; |
| 4048 | mddev->layout = mddev->new_layout; |
| 4049 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
| 4050 | mddev->delta_disks = 0; |
| 4051 | mddev->reshape_backwards = 0; |
| 4052 | mddev->degraded = 0; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 4053 | spin_unlock(&mddev->lock); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 4054 | |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 4055 | if (oldpers->sync_request == NULL && |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 4056 | mddev->external) { |
| 4057 | /* We are converting from a no-redundancy array |
| 4058 | * to a redundancy array and metadata is managed |
| 4059 | * externally so we need to be sure that writes |
| 4060 | * won't block due to a need to transition |
| 4061 | * clean->dirty |
| 4062 | * until external management is started. |
| 4063 | */ |
| 4064 | mddev->in_sync = 0; |
| 4065 | mddev->safemode_delay = 0; |
| 4066 | mddev->safemode = 0; |
| 4067 | } |
| 4068 | |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 4069 | oldpers->free(mddev, oldpriv); |
| 4070 | |
| 4071 | if (oldpers->sync_request == NULL && |
| 4072 | pers->sync_request != NULL) { |
| 4073 | /* need to add the md_redundancy_group */ |
| 4074 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 4075 | pr_warn("md: cannot register extra attributes for %s\n", |
| 4076 | mdname(mddev)); |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 4077 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action"); |
Junxiao Bi | e8efa9b | 2020-08-04 17:27:18 -0700 | [diff] [blame] | 4078 | mddev->sysfs_completed = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_completed"); |
| 4079 | mddev->sysfs_degraded = sysfs_get_dirent_safe(mddev->kobj.sd, "degraded"); |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 4080 | } |
| 4081 | if (oldpers->sync_request != NULL && |
| 4082 | pers->sync_request == NULL) { |
| 4083 | /* need to remove the md_redundancy_group */ |
| 4084 | if (mddev->to_remove == NULL) |
| 4085 | mddev->to_remove = &md_redundancy_group; |
| 4086 | } |
| 4087 | |
Alexey Obitotskiy | 4cb9da7 | 2016-06-23 12:11:01 +0200 | [diff] [blame] | 4088 | module_put(oldpers->owner); |
| 4089 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 4090 | rdev_for_each(rdev, mddev) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 4091 | if (rdev->raid_disk < 0) |
| 4092 | continue; |
NeilBrown | bf2cb0d | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4093 | if (rdev->new_raid_disk >= mddev->raid_disks) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 4094 | rdev->new_raid_disk = -1; |
| 4095 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 4096 | continue; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4097 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 4098 | } |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 4099 | rdev_for_each(rdev, mddev) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 4100 | if (rdev->raid_disk < 0) |
| 4101 | continue; |
| 4102 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 4103 | continue; |
| 4104 | rdev->raid_disk = rdev->new_raid_disk; |
| 4105 | if (rdev->raid_disk < 0) |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 4106 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 4107 | else { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4108 | if (sysfs_link_rdev(mddev, rdev)) |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 4109 | pr_warn("md: cannot register rd%d for %s after level change\n", |
| 4110 | rdev->raid_disk, mdname(mddev)); |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 4111 | } |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 4112 | } |
| 4113 | |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 4114 | if (pers->sync_request == NULL) { |
Trela, Maciej | 9af204c | 2010-03-08 16:02:44 +1100 | [diff] [blame] | 4115 | /* this is now an array without redundancy, so |
| 4116 | * it must always be in_sync |
| 4117 | */ |
| 4118 | mddev->in_sync = 1; |
| 4119 | del_timer_sync(&mddev->safemode_timer); |
| 4120 | } |
NeilBrown | 02e5f5c | 2013-11-14 15:16:15 +1100 | [diff] [blame] | 4121 | blk_set_stacking_limits(&mddev->queue->limits); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4122 | pers->run(mddev); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 4123 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
Jonathan Brassow | 47525e5 | 2012-05-22 13:55:29 +1000 | [diff] [blame] | 4124 | mddev_resume(mddev); |
NeilBrown | 830778a | 2014-01-14 15:17:03 +1100 | [diff] [blame] | 4125 | if (!mddev->thread) |
| 4126 | md_update_sb(mddev, 1); |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 4127 | sysfs_notify_dirent_safe(mddev->sysfs_level); |
Dan Williams | bb7f8d2 | 2010-05-01 18:14:57 -0700 | [diff] [blame] | 4128 | md_new_event(mddev); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4129 | rv = len; |
| 4130 | out_unlock: |
| 4131 | mddev_unlock(mddev); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4132 | return rv; |
| 4133 | } |
| 4134 | |
| 4135 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4136 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4137 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4138 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4139 | layout_show(struct mddev *mddev, char *page) |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4140 | { |
| 4141 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4142 | if (mddev->reshape_position != MaxSector && |
| 4143 | mddev->layout != mddev->new_layout) |
| 4144 | return sprintf(page, "%d (%d)\n", |
| 4145 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4146 | return sprintf(page, "%d\n", mddev->layout); |
| 4147 | } |
| 4148 | |
| 4149 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4150 | layout_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4151 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4152 | unsigned int n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4153 | int err; |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4154 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4155 | err = kstrtouint(buf, 10, &n); |
| 4156 | if (err < 0) |
| 4157 | return err; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4158 | err = mddev_lock(mddev); |
| 4159 | if (err) |
| 4160 | return err; |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4161 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 4162 | if (mddev->pers) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 4163 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4164 | err = -EBUSY; |
| 4165 | else if (mddev->ro) |
| 4166 | err = -EROFS; |
| 4167 | else { |
| 4168 | mddev->new_layout = n; |
| 4169 | err = mddev->pers->check_reshape(mddev); |
| 4170 | if (err) |
| 4171 | mddev->new_layout = mddev->layout; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 4172 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 4173 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4174 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 4175 | if (mddev->reshape_position == MaxSector) |
| 4176 | mddev->layout = n; |
| 4177 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4178 | mddev_unlock(mddev); |
| 4179 | return err ?: len; |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4180 | } |
| 4181 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4182 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4183 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4184 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4185 | raid_disks_show(struct mddev *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4186 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 4187 | if (mddev->raid_disks == 0) |
| 4188 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4189 | if (mddev->reshape_position != MaxSector && |
| 4190 | mddev->delta_disks != 0) |
| 4191 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 4192 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4193 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 4194 | } |
| 4195 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4196 | static int update_raid_disks(struct mddev *mddev, int raid_disks); |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4197 | |
| 4198 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4199 | raid_disks_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4200 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4201 | unsigned int n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4202 | int err; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4203 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4204 | err = kstrtouint(buf, 10, &n); |
| 4205 | if (err < 0) |
| 4206 | return err; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4207 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4208 | err = mddev_lock(mddev); |
| 4209 | if (err) |
| 4210 | return err; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4211 | if (mddev->pers) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4212 | err = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4213 | else if (mddev->reshape_position != MaxSector) { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4214 | struct md_rdev *rdev; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4215 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4216 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4217 | err = -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4218 | rdev_for_each(rdev, mddev) { |
| 4219 | if (olddisks < n && |
| 4220 | rdev->data_offset < rdev->new_data_offset) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4221 | goto out_unlock; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4222 | if (olddisks > n && |
| 4223 | rdev->data_offset > rdev->new_data_offset) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4224 | goto out_unlock; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4225 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4226 | err = 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4227 | mddev->delta_disks = n - olddisks; |
| 4228 | mddev->raid_disks = n; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4229 | mddev->reshape_backwards = (mddev->delta_disks < 0); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4230 | } else |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4231 | mddev->raid_disks = n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4232 | out_unlock: |
| 4233 | mddev_unlock(mddev); |
| 4234 | return err ? err : len; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4235 | } |
| 4236 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4237 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4238 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4239 | static ssize_t |
Sebastian Parschauer | ec164d07 | 2020-07-28 12:01:39 +0200 | [diff] [blame] | 4240 | uuid_show(struct mddev *mddev, char *page) |
| 4241 | { |
| 4242 | return sprintf(page, "%pU\n", mddev->uuid); |
| 4243 | } |
| 4244 | static struct md_sysfs_entry md_uuid = |
| 4245 | __ATTR(uuid, S_IRUGO, uuid_show, NULL); |
| 4246 | |
| 4247 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4248 | chunk_size_show(struct mddev *mddev, char *page) |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4249 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4250 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4251 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 4252 | return sprintf(page, "%d (%d)\n", |
| 4253 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4254 | mddev->chunk_sectors << 9); |
| 4255 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4256 | } |
| 4257 | |
| 4258 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4259 | chunk_size_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4260 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4261 | unsigned long n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4262 | int err; |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4263 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4264 | err = kstrtoul(buf, 10, &n); |
| 4265 | if (err < 0) |
| 4266 | return err; |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4267 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4268 | err = mddev_lock(mddev); |
| 4269 | if (err) |
| 4270 | return err; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 4271 | if (mddev->pers) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 4272 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4273 | err = -EBUSY; |
| 4274 | else if (mddev->ro) |
| 4275 | err = -EROFS; |
| 4276 | else { |
| 4277 | mddev->new_chunk_sectors = n >> 9; |
| 4278 | err = mddev->pers->check_reshape(mddev); |
| 4279 | if (err) |
| 4280 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 4281 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 4282 | } else { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4283 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 4284 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4285 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 4286 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4287 | mddev_unlock(mddev); |
| 4288 | return err ?: len; |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4289 | } |
| 4290 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4291 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4292 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4293 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4294 | resync_start_show(struct mddev *mddev, char *page) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4295 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 4296 | if (mddev->recovery_cp == MaxSector) |
| 4297 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4298 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 4299 | } |
| 4300 | |
| 4301 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4302 | resync_start_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4303 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4304 | unsigned long long n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4305 | int err; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4306 | |
| 4307 | if (cmd_match(buf, "none")) |
| 4308 | n = MaxSector; |
| 4309 | else { |
| 4310 | err = kstrtoull(buf, 10, &n); |
| 4311 | if (err < 0) |
| 4312 | return err; |
| 4313 | if (n != (sector_t)n) |
| 4314 | return -EINVAL; |
| 4315 | } |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4316 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4317 | err = mddev_lock(mddev); |
| 4318 | if (err) |
| 4319 | return err; |
NeilBrown | b098636 | 2011-05-11 15:52:21 +1000 | [diff] [blame] | 4320 | if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4321 | err = -EBUSY; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4322 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4323 | if (!err) { |
| 4324 | mddev->recovery_cp = n; |
| 4325 | if (mddev->pers) |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 4326 | set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4327 | } |
| 4328 | mddev_unlock(mddev); |
| 4329 | return err ?: len; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4330 | } |
| 4331 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 4332 | __ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR, |
| 4333 | resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4334 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4335 | /* |
| 4336 | * The array state can be: |
| 4337 | * |
| 4338 | * clear |
| 4339 | * No devices, no size, no level |
| 4340 | * Equivalent to STOP_ARRAY ioctl |
| 4341 | * inactive |
| 4342 | * May have some settings, but array is not active |
| 4343 | * all IO results in error |
| 4344 | * When written, doesn't tear down array, but just stops it |
| 4345 | * suspended (not supported yet) |
| 4346 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 4347 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4348 | * readonly |
| 4349 | * no resync can happen. no superblocks get written. |
| 4350 | * write requests fail |
| 4351 | * read-auto |
| 4352 | * like readonly, but behaves like 'clean' on a write request. |
| 4353 | * |
| 4354 | * clean - no pending writes, but otherwise active. |
| 4355 | * When written to inactive array, starts without resync |
| 4356 | * If a write request arrives then |
| 4357 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 4358 | * if not known, block and switch to write-pending |
| 4359 | * If written to an active array that has pending writes, then fails. |
| 4360 | * active |
| 4361 | * fully active: IO and resync can be happening. |
| 4362 | * When written to inactive array, starts with resync |
| 4363 | * |
| 4364 | * write-pending |
| 4365 | * clean, but writes are blocked waiting for 'active' to be written. |
| 4366 | * |
| 4367 | * active-idle |
| 4368 | * like active, but no writes have been seen for a while (100msec). |
| 4369 | * |
Guilherme G. Piccoli | 62f7b19 | 2019-09-03 16:49:00 -0300 | [diff] [blame] | 4370 | * broken |
| 4371 | * RAID0/LINEAR-only: same as clean, but array is missing a member. |
| 4372 | * It's useful because RAID0/LINEAR mounted-arrays aren't stopped |
| 4373 | * when a member is gone, so this state will at least alert the |
| 4374 | * user that something is wrong. |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4375 | */ |
| 4376 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
Guilherme G. Piccoli | 62f7b19 | 2019-09-03 16:49:00 -0300 | [diff] [blame] | 4377 | write_pending, active_idle, broken, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 4378 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4379 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
Guilherme G. Piccoli | 62f7b19 | 2019-09-03 16:49:00 -0300 | [diff] [blame] | 4380 | "write-pending", "active-idle", "broken", NULL }; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4381 | |
| 4382 | static int match_word(const char *word, char **list) |
| 4383 | { |
| 4384 | int n; |
| 4385 | for (n=0; list[n]; n++) |
| 4386 | if (cmd_match(word, list[n])) |
| 4387 | break; |
| 4388 | return n; |
| 4389 | } |
| 4390 | |
| 4391 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4392 | array_state_show(struct mddev *mddev, char *page) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4393 | { |
| 4394 | enum array_state st = inactive; |
| 4395 | |
Guilherme G. Piccoli | 62f7b19 | 2019-09-03 16:49:00 -0300 | [diff] [blame] | 4396 | if (mddev->pers && !test_bit(MD_NOT_READY, &mddev->flags)) { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4397 | switch(mddev->ro) { |
| 4398 | case 1: |
| 4399 | st = readonly; |
| 4400 | break; |
| 4401 | case 2: |
| 4402 | st = read_auto; |
| 4403 | break; |
| 4404 | case 0: |
NeilBrown | 55cc39f | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 4405 | spin_lock(&mddev->lock); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 4406 | if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4407 | st = write_pending; |
Tomasz Majchrzak | 16f8894 | 2016-10-24 12:47:28 +0200 | [diff] [blame] | 4408 | else if (mddev->in_sync) |
| 4409 | st = clean; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4410 | else if (mddev->safemode) |
| 4411 | st = active_idle; |
| 4412 | else |
| 4413 | st = active; |
NeilBrown | 55cc39f | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 4414 | spin_unlock(&mddev->lock); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4415 | } |
Guilherme G. Piccoli | 62f7b19 | 2019-09-03 16:49:00 -0300 | [diff] [blame] | 4416 | |
| 4417 | if (test_bit(MD_BROKEN, &mddev->flags) && st == clean) |
| 4418 | st = broken; |
| 4419 | } else { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4420 | if (list_empty(&mddev->disks) && |
| 4421 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4422 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4423 | st = clear; |
| 4424 | else |
| 4425 | st = inactive; |
| 4426 | } |
| 4427 | return sprintf(page, "%s\n", array_states[st]); |
| 4428 | } |
| 4429 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 4430 | static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev); |
| 4431 | static int md_set_readonly(struct mddev *mddev, struct block_device *bdev); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4432 | static int restart_array(struct mddev *mddev); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4433 | |
| 4434 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4435 | array_state_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4436 | { |
NeilBrown | 6497709b | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 4437 | int err = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4438 | enum array_state st = match_word(buf, array_states); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4439 | |
| 4440 | if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) { |
| 4441 | /* don't take reconfig_mutex when toggling between |
| 4442 | * clean and active |
| 4443 | */ |
| 4444 | spin_lock(&mddev->lock); |
| 4445 | if (st == active) { |
| 4446 | restart_array(mddev); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 4447 | clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags); |
Tomasz Majchrzak | 91a6c4a | 2016-10-25 17:07:08 +0200 | [diff] [blame] | 4448 | md_wakeup_thread(mddev->thread); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4449 | wake_up(&mddev->sb_wait); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4450 | } else /* st == clean */ { |
| 4451 | restart_array(mddev); |
NeilBrown | 6497709b | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 4452 | if (!set_in_sync(mddev)) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4453 | err = -EBUSY; |
| 4454 | } |
Tomasz Majchrzak | 573275b | 2016-06-30 10:47:09 +0200 | [diff] [blame] | 4455 | if (!err) |
| 4456 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4457 | spin_unlock(&mddev->lock); |
NeilBrown | c008f1d | 2015-06-12 19:46:44 +1000 | [diff] [blame] | 4458 | return err ?: len; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4459 | } |
| 4460 | err = mddev_lock(mddev); |
| 4461 | if (err) |
| 4462 | return err; |
| 4463 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4464 | switch(st) { |
| 4465 | case bad_word: |
| 4466 | break; |
| 4467 | case clear: |
| 4468 | /* stopping an active array */ |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 4469 | err = do_md_stop(mddev, 0, NULL); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4470 | break; |
| 4471 | case inactive: |
| 4472 | /* stopping an active array */ |
NeilBrown | 90cf195 | 2012-07-31 10:04:55 +1000 | [diff] [blame] | 4473 | if (mddev->pers) |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 4474 | err = do_md_stop(mddev, 2, NULL); |
NeilBrown | 90cf195 | 2012-07-31 10:04:55 +1000 | [diff] [blame] | 4475 | else |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4476 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4477 | break; |
| 4478 | case suspended: |
| 4479 | break; /* not supported yet */ |
| 4480 | case readonly: |
| 4481 | if (mddev->pers) |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 4482 | err = md_set_readonly(mddev, NULL); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4483 | else { |
| 4484 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 4485 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4486 | err = do_md_run(mddev); |
| 4487 | } |
| 4488 | break; |
| 4489 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4490 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4491 | if (mddev->ro == 0) |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 4492 | err = md_set_readonly(mddev, NULL); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4493 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 4494 | err = restart_array(mddev); |
| 4495 | if (err == 0) { |
| 4496 | mddev->ro = 2; |
| 4497 | set_disk_ro(mddev->gendisk, 0); |
| 4498 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4499 | } else { |
| 4500 | mddev->ro = 2; |
| 4501 | err = do_md_run(mddev); |
| 4502 | } |
| 4503 | break; |
| 4504 | case clean: |
| 4505 | if (mddev->pers) { |
Song Liu | 339421d | 2015-10-08 21:54:13 -0700 | [diff] [blame] | 4506 | err = restart_array(mddev); |
| 4507 | if (err) |
| 4508 | break; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 4509 | spin_lock(&mddev->lock); |
NeilBrown | 6497709b | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 4510 | if (!set_in_sync(mddev)) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4511 | err = -EBUSY; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 4512 | spin_unlock(&mddev->lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 4513 | } else |
| 4514 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4515 | break; |
| 4516 | case active: |
| 4517 | if (mddev->pers) { |
Song Liu | 339421d | 2015-10-08 21:54:13 -0700 | [diff] [blame] | 4518 | err = restart_array(mddev); |
| 4519 | if (err) |
| 4520 | break; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 4521 | clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4522 | wake_up(&mddev->sb_wait); |
| 4523 | err = 0; |
| 4524 | } else { |
| 4525 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 4526 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4527 | err = do_md_run(mddev); |
| 4528 | } |
| 4529 | break; |
| 4530 | case write_pending: |
| 4531 | case active_idle: |
Guilherme G. Piccoli | 62f7b19 | 2019-09-03 16:49:00 -0300 | [diff] [blame] | 4532 | case broken: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4533 | /* these cannot be set */ |
| 4534 | break; |
| 4535 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4536 | |
| 4537 | if (!err) { |
NeilBrown | 1d23f17 | 2011-12-08 15:49:12 +1100 | [diff] [blame] | 4538 | if (mddev->hold_active == UNTIL_IOCTL) |
| 4539 | mddev->hold_active = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4540 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4541 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4542 | mddev_unlock(mddev); |
| 4543 | return err ?: len; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4544 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4545 | static struct md_sysfs_entry md_array_state = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 4546 | __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] | 4547 | |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4548 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4549 | max_corrected_read_errors_show(struct mddev *mddev, char *page) { |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4550 | return sprintf(page, "%d\n", |
| 4551 | atomic_read(&mddev->max_corr_read_errors)); |
| 4552 | } |
| 4553 | |
| 4554 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4555 | 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] | 4556 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4557 | unsigned int n; |
| 4558 | int rv; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4559 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4560 | rv = kstrtouint(buf, 10, &n); |
| 4561 | if (rv < 0) |
| 4562 | return rv; |
| 4563 | atomic_set(&mddev->max_corr_read_errors, n); |
| 4564 | return len; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4565 | } |
| 4566 | |
| 4567 | static struct md_sysfs_entry max_corr_read_errors = |
| 4568 | __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show, |
| 4569 | max_corrected_read_errors_store); |
| 4570 | |
| 4571 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4572 | null_show(struct mddev *mddev, char *page) |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4573 | { |
| 4574 | return -EINVAL; |
| 4575 | } |
| 4576 | |
Guoqing Jiang | cc1ffe6 | 2020-04-04 23:57:08 +0200 | [diff] [blame] | 4577 | /* need to ensure rdev_delayed_delete() has completed */ |
| 4578 | static void flush_rdev_wq(struct mddev *mddev) |
| 4579 | { |
| 4580 | struct md_rdev *rdev; |
| 4581 | |
| 4582 | rcu_read_lock(); |
| 4583 | rdev_for_each_rcu(rdev, mddev) |
| 4584 | if (work_pending(&rdev->del_work)) { |
| 4585 | flush_workqueue(md_rdev_misc_wq); |
| 4586 | break; |
| 4587 | } |
| 4588 | rcu_read_unlock(); |
| 4589 | } |
| 4590 | |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4591 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4592 | new_dev_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4593 | { |
| 4594 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 4595 | /* The new device is added to the array. |
| 4596 | * If the array has a persistent superblock, we read the |
| 4597 | * superblock to initialise info and check validity. |
| 4598 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 4599 | * which mainly checks size. |
| 4600 | */ |
| 4601 | char *e; |
| 4602 | int major = simple_strtoul(buf, &e, 10); |
| 4603 | int minor; |
| 4604 | dev_t dev; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4605 | struct md_rdev *rdev; |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4606 | int err; |
| 4607 | |
| 4608 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 4609 | return -EINVAL; |
| 4610 | minor = simple_strtoul(e+1, &e, 10); |
| 4611 | if (*e && *e != '\n') |
| 4612 | return -EINVAL; |
| 4613 | dev = MKDEV(major, minor); |
| 4614 | if (major != MAJOR(dev) || |
| 4615 | minor != MINOR(dev)) |
| 4616 | return -EOVERFLOW; |
| 4617 | |
Guoqing Jiang | cc1ffe6 | 2020-04-04 23:57:08 +0200 | [diff] [blame] | 4618 | flush_rdev_wq(mddev); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4619 | err = mddev_lock(mddev); |
| 4620 | if (err) |
| 4621 | return err; |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4622 | if (mddev->persistent) { |
| 4623 | rdev = md_import_device(dev, mddev->major_version, |
| 4624 | mddev->minor_version); |
| 4625 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4626 | struct md_rdev *rdev0 |
| 4627 | = list_entry(mddev->disks.next, |
| 4628 | struct md_rdev, same_set); |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4629 | err = super_types[mddev->major_version] |
| 4630 | .load_super(rdev, rdev0, mddev->minor_version); |
| 4631 | if (err < 0) |
| 4632 | goto out; |
| 4633 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 4634 | } else if (mddev->external) |
| 4635 | rdev = md_import_device(dev, -2, -1); |
| 4636 | else |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4637 | rdev = md_import_device(dev, -1, -1); |
| 4638 | |
NeilBrown | 9a8c0fa | 2015-06-25 17:06:40 +1000 | [diff] [blame] | 4639 | if (IS_ERR(rdev)) { |
| 4640 | mddev_unlock(mddev); |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4641 | return PTR_ERR(rdev); |
NeilBrown | 9a8c0fa | 2015-06-25 17:06:40 +1000 | [diff] [blame] | 4642 | } |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4643 | err = bind_rdev_to_array(rdev, mddev); |
| 4644 | out: |
| 4645 | if (err) |
| 4646 | export_rdev(rdev); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4647 | mddev_unlock(mddev); |
Alexey Obitotskiy | 5492c46 | 2017-07-28 15:49:25 +0200 | [diff] [blame] | 4648 | if (!err) |
| 4649 | md_new_event(mddev); |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4650 | return err ? err : len; |
| 4651 | } |
| 4652 | |
| 4653 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4654 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4655 | |
| 4656 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4657 | bitmap_store(struct mddev *mddev, const char *buf, size_t len) |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4658 | { |
| 4659 | char *end; |
| 4660 | unsigned long chunk, end_chunk; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4661 | int err; |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4662 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4663 | err = mddev_lock(mddev); |
| 4664 | if (err) |
| 4665 | return err; |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4666 | if (!mddev->bitmap) |
| 4667 | goto out; |
| 4668 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 4669 | while (*buf) { |
| 4670 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 4671 | if (buf == end) break; |
| 4672 | if (*end == '-') { /* range */ |
| 4673 | buf = end + 1; |
| 4674 | end_chunk = simple_strtoul(buf, &end, 0); |
| 4675 | if (buf == end) break; |
| 4676 | } |
| 4677 | if (*end && !isspace(*end)) break; |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 4678 | md_bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 4679 | buf = skip_spaces(end); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4680 | } |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 4681 | md_bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4682 | out: |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4683 | mddev_unlock(mddev); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4684 | return len; |
| 4685 | } |
| 4686 | |
| 4687 | static struct md_sysfs_entry md_bitmap = |
| 4688 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 4689 | |
| 4690 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4691 | size_show(struct mddev *mddev, char *page) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4692 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4693 | return sprintf(page, "%llu\n", |
| 4694 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4695 | } |
| 4696 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4697 | static int update_size(struct mddev *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4698 | |
| 4699 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4700 | size_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4701 | { |
| 4702 | /* If array is inactive, we can reduce the component size, but |
| 4703 | * not increase it (except from 0). |
| 4704 | * If array is active, we can try an on-line resize |
| 4705 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4706 | sector_t sectors; |
| 4707 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4708 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4709 | if (err < 0) |
| 4710 | return err; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4711 | err = mddev_lock(mddev); |
| 4712 | if (err) |
| 4713 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4714 | if (mddev->pers) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4715 | err = update_size(mddev, sectors); |
Xiao Ni | 4ba1e78 | 2016-06-12 17:18:00 +0800 | [diff] [blame] | 4716 | if (err == 0) |
| 4717 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4718 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4719 | if (mddev->dev_sectors == 0 || |
| 4720 | mddev->dev_sectors > sectors) |
| 4721 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4722 | else |
| 4723 | err = -ENOSPC; |
| 4724 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4725 | mddev_unlock(mddev); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4726 | return err ? err : len; |
| 4727 | } |
| 4728 | |
| 4729 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4730 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4731 | |
Masanari Iida | 83f0d77 | 2012-10-30 00:18:08 +0900 | [diff] [blame] | 4732 | /* Metadata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4733 | * This is one of |
| 4734 | * 'none' for arrays with no metadata (good luck...) |
| 4735 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4736 | * or N.M for internally known formats |
| 4737 | */ |
| 4738 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4739 | metadata_show(struct mddev *mddev, char *page) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4740 | { |
| 4741 | if (mddev->persistent) |
| 4742 | return sprintf(page, "%d.%d\n", |
| 4743 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4744 | else if (mddev->external) |
| 4745 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4746 | else |
| 4747 | return sprintf(page, "none\n"); |
| 4748 | } |
| 4749 | |
| 4750 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4751 | metadata_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4752 | { |
| 4753 | int major, minor; |
| 4754 | char *e; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4755 | int err; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 4756 | /* Changing the details of 'external' metadata is |
| 4757 | * always permitted. Otherwise there must be |
| 4758 | * no devices attached to the array. |
| 4759 | */ |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4760 | |
| 4761 | err = mddev_lock(mddev); |
| 4762 | if (err) |
| 4763 | return err; |
| 4764 | err = -EBUSY; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 4765 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 4766 | ; |
| 4767 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4768 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4769 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4770 | err = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4771 | if (cmd_match(buf, "none")) { |
| 4772 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4773 | mddev->external = 0; |
| 4774 | mddev->major_version = 0; |
| 4775 | mddev->minor_version = 90; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4776 | goto out_unlock; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4777 | } |
| 4778 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 4779 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4780 | if (namelen >= sizeof(mddev->metadata_type)) |
| 4781 | namelen = sizeof(mddev->metadata_type)-1; |
| 4782 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 4783 | mddev->metadata_type[namelen] = 0; |
| 4784 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 4785 | mddev->metadata_type[--namelen] = 0; |
| 4786 | mddev->persistent = 0; |
| 4787 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4788 | mddev->major_version = 0; |
| 4789 | mddev->minor_version = 90; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4790 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4791 | } |
| 4792 | major = simple_strtoul(buf, &e, 10); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4793 | err = -EINVAL; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4794 | if (e==buf || *e != '.') |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4795 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4796 | buf = e+1; |
| 4797 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 4798 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4799 | goto out_unlock; |
| 4800 | err = -ENOENT; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 4801 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4802 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4803 | mddev->major_version = major; |
| 4804 | mddev->minor_version = minor; |
| 4805 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4806 | mddev->external = 0; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4807 | err = 0; |
| 4808 | out_unlock: |
| 4809 | mddev_unlock(mddev); |
| 4810 | return err ?: len; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4811 | } |
| 4812 | |
| 4813 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 4814 | __ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4815 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4816 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4817 | action_show(struct mddev *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4818 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4819 | char *type = "idle"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4820 | unsigned long recovery = mddev->recovery; |
| 4821 | if (test_bit(MD_RECOVERY_FROZEN, &recovery)) |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4822 | type = "frozen"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4823 | else if (test_bit(MD_RECOVERY_RUNNING, &recovery) || |
| 4824 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) { |
| 4825 | if (test_bit(MD_RECOVERY_RESHAPE, &recovery)) |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 4826 | type = "reshape"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4827 | else if (test_bit(MD_RECOVERY_SYNC, &recovery)) { |
| 4828 | if (!test_bit(MD_RECOVERY_REQUESTED, &recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4829 | type = "resync"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4830 | else if (test_bit(MD_RECOVERY_CHECK, &recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4831 | type = "check"; |
| 4832 | else |
| 4833 | type = "repair"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4834 | } else if (test_bit(MD_RECOVERY_RECOVER, &recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4835 | type = "recover"; |
NeilBrown | 985ca97 | 2015-07-06 12:26:57 +1000 | [diff] [blame] | 4836 | else if (mddev->reshape_position != MaxSector) |
| 4837 | type = "reshape"; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4838 | } |
| 4839 | return sprintf(page, "%s\n", type); |
| 4840 | } |
| 4841 | |
| 4842 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4843 | action_store(struct mddev *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4844 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4845 | if (!mddev->pers || !mddev->pers->sync_request) |
| 4846 | return -EINVAL; |
| 4847 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4848 | |
| 4849 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4850 | if (cmd_match(page, "frozen")) |
| 4851 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4852 | else |
| 4853 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 8e8e251 | 2015-06-12 19:51:27 +1000 | [diff] [blame] | 4854 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 4855 | mddev_lock(mddev) == 0) { |
Guoqing Jiang | cc1ffe6 | 2020-04-04 23:57:08 +0200 | [diff] [blame] | 4856 | if (work_pending(&mddev->del_work)) |
| 4857 | flush_workqueue(md_misc_wq); |
NeilBrown | 8e8e251 | 2015-06-12 19:51:27 +1000 | [diff] [blame] | 4858 | if (mddev->sync_thread) { |
| 4859 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4860 | md_reap_sync_thread(mddev); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4861 | } |
NeilBrown | 8e8e251 | 2015-06-12 19:51:27 +1000 | [diff] [blame] | 4862 | mddev_unlock(mddev); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4863 | } |
NeilBrown | 312045e | 2015-12-21 11:01:21 +1100 | [diff] [blame] | 4864 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4865 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4866 | else if (cmd_match(page, "resync")) |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4867 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4868 | else if (cmd_match(page, "recover")) { |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4869 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4870 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4871 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4872 | int err; |
| 4873 | if (mddev->pers->start_reshape == NULL) |
| 4874 | return -EINVAL; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4875 | err = mddev_lock(mddev); |
| 4876 | if (!err) { |
NeilBrown | 312045e | 2015-12-21 11:01:21 +1100 | [diff] [blame] | 4877 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4878 | err = -EBUSY; |
| 4879 | else { |
| 4880 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4881 | err = mddev->pers->start_reshape(mddev); |
| 4882 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4883 | mddev_unlock(mddev); |
| 4884 | } |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4885 | if (err) |
| 4886 | return err; |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 4887 | sysfs_notify_dirent_safe(mddev->sysfs_degraded); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4888 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 4889 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4890 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 4891 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4892 | return -EINVAL; |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4893 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4894 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 4895 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4896 | } |
NeilBrown | 48c26dd | 2012-10-11 14:19:39 +1100 | [diff] [blame] | 4897 | if (mddev->ro == 2) { |
| 4898 | /* A write to sync_action is enough to justify |
| 4899 | * canceling read-auto mode |
| 4900 | */ |
| 4901 | mddev->ro = 0; |
| 4902 | md_wakeup_thread(mddev->sync_thread); |
| 4903 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 4904 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4905 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4906 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4907 | return len; |
| 4908 | } |
| 4909 | |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 4910 | static struct md_sysfs_entry md_scan_mode = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 4911 | __ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 4912 | |
| 4913 | static ssize_t |
| 4914 | last_sync_action_show(struct mddev *mddev, char *page) |
| 4915 | { |
| 4916 | return sprintf(page, "%s\n", mddev->last_sync_action); |
| 4917 | } |
| 4918 | |
| 4919 | static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action); |
| 4920 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4921 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4922 | mismatch_cnt_show(struct mddev *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4923 | { |
| 4924 | return sprintf(page, "%llu\n", |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 4925 | (unsigned long long) |
| 4926 | atomic64_read(&mddev->resync_mismatches)); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4927 | } |
| 4928 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4929 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4930 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4931 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4932 | sync_min_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4933 | { |
| 4934 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 4935 | mddev->sync_speed_min ? "local": "system"); |
| 4936 | } |
| 4937 | |
| 4938 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4939 | sync_min_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4940 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4941 | unsigned int min; |
| 4942 | int rv; |
| 4943 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4944 | if (strncmp(buf, "system", 6)==0) { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4945 | min = 0; |
| 4946 | } else { |
| 4947 | rv = kstrtouint(buf, 10, &min); |
| 4948 | if (rv < 0) |
| 4949 | return rv; |
| 4950 | if (min == 0) |
| 4951 | return -EINVAL; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4952 | } |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4953 | mddev->sync_speed_min = min; |
| 4954 | return len; |
| 4955 | } |
| 4956 | |
| 4957 | static struct md_sysfs_entry md_sync_min = |
| 4958 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 4959 | |
| 4960 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4961 | sync_max_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4962 | { |
| 4963 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 4964 | mddev->sync_speed_max ? "local": "system"); |
| 4965 | } |
| 4966 | |
| 4967 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4968 | sync_max_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4969 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4970 | unsigned int max; |
| 4971 | int rv; |
| 4972 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4973 | if (strncmp(buf, "system", 6)==0) { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4974 | max = 0; |
| 4975 | } else { |
| 4976 | rv = kstrtouint(buf, 10, &max); |
| 4977 | if (rv < 0) |
| 4978 | return rv; |
| 4979 | if (max == 0) |
| 4980 | return -EINVAL; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4981 | } |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4982 | mddev->sync_speed_max = max; |
| 4983 | return len; |
| 4984 | } |
| 4985 | |
| 4986 | static struct md_sysfs_entry md_sync_max = |
| 4987 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 4988 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4989 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4990 | degraded_show(struct mddev *mddev, char *page) |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4991 | { |
| 4992 | return sprintf(page, "%d\n", mddev->degraded); |
| 4993 | } |
| 4994 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4995 | |
| 4996 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4997 | sync_force_parallel_show(struct mddev *mddev, char *page) |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4998 | { |
| 4999 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 5000 | } |
| 5001 | |
| 5002 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5003 | 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] | 5004 | { |
| 5005 | long n; |
| 5006 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 5007 | if (kstrtol(buf, 10, &n)) |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 5008 | return -EINVAL; |
| 5009 | |
| 5010 | if (n != 0 && n != 1) |
| 5011 | return -EINVAL; |
| 5012 | |
| 5013 | mddev->parallel_resync = n; |
| 5014 | |
| 5015 | if (mddev->sync_thread) |
| 5016 | wake_up(&resync_wait); |
| 5017 | |
| 5018 | return len; |
| 5019 | } |
| 5020 | |
| 5021 | /* force parallel resync, even with shared block devices */ |
| 5022 | static struct md_sysfs_entry md_sync_force_parallel = |
| 5023 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 5024 | sync_force_parallel_show, sync_force_parallel_store); |
| 5025 | |
| 5026 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5027 | sync_speed_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5028 | { |
| 5029 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 5030 | if (mddev->curr_resync == 0) |
| 5031 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 5032 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 5033 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5034 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 5035 | db = resync - mddev->resync_mark_cnt; |
| 5036 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5037 | } |
| 5038 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 5039 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5040 | |
| 5041 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5042 | sync_completed_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5043 | { |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 5044 | unsigned long long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5045 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 5046 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 5047 | return sprintf(page, "none\n"); |
| 5048 | |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 5049 | if (mddev->curr_resync == 1 || |
| 5050 | mddev->curr_resync == 2) |
| 5051 | return sprintf(page, "delayed\n"); |
| 5052 | |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 5053 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 5054 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5055 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5056 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5057 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5058 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 5059 | resync = mddev->curr_resync_completed; |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 5060 | return sprintf(page, "%llu / %llu\n", resync, max_sectors); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5061 | } |
| 5062 | |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 5063 | static struct md_sysfs_entry md_sync_completed = |
| 5064 | __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5065 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5066 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5067 | min_sync_show(struct mddev *mddev, char *page) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5068 | { |
| 5069 | return sprintf(page, "%llu\n", |
| 5070 | (unsigned long long)mddev->resync_min); |
| 5071 | } |
| 5072 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5073 | min_sync_store(struct mddev *mddev, const char *buf, size_t len) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5074 | { |
| 5075 | unsigned long long min; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5076 | int err; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5077 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 5078 | if (kstrtoull(buf, 10, &min)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5079 | return -EINVAL; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5080 | |
| 5081 | spin_lock(&mddev->lock); |
| 5082 | err = -EINVAL; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5083 | if (min > mddev->resync_max) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5084 | goto out_unlock; |
| 5085 | |
| 5086 | err = -EBUSY; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5087 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5088 | goto out_unlock; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5089 | |
NeilBrown | 50c37b1 | 2015-03-23 17:36:38 +1100 | [diff] [blame] | 5090 | /* Round down to multiple of 4K for safety */ |
| 5091 | mddev->resync_min = round_down(min, 8); |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5092 | err = 0; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5093 | |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5094 | out_unlock: |
| 5095 | spin_unlock(&mddev->lock); |
| 5096 | return err ?: len; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5097 | } |
| 5098 | |
| 5099 | static struct md_sysfs_entry md_min_sync = |
| 5100 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 5101 | |
| 5102 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5103 | max_sync_show(struct mddev *mddev, char *page) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5104 | { |
| 5105 | if (mddev->resync_max == MaxSector) |
| 5106 | return sprintf(page, "max\n"); |
| 5107 | else |
| 5108 | return sprintf(page, "%llu\n", |
| 5109 | (unsigned long long)mddev->resync_max); |
| 5110 | } |
| 5111 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5112 | max_sync_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5113 | { |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5114 | int err; |
| 5115 | spin_lock(&mddev->lock); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5116 | if (strncmp(buf, "max", 3) == 0) |
| 5117 | mddev->resync_max = MaxSector; |
| 5118 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5119 | unsigned long long max; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5120 | int chunk; |
| 5121 | |
| 5122 | err = -EINVAL; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 5123 | if (kstrtoull(buf, 10, &max)) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5124 | goto out_unlock; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5125 | if (max < mddev->resync_min) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5126 | goto out_unlock; |
| 5127 | |
| 5128 | err = -EBUSY; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5129 | if (max < mddev->resync_max && |
NeilBrown | 4d484a4 | 2009-08-13 10:41:50 +1000 | [diff] [blame] | 5130 | mddev->ro == 0 && |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5131 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5132 | goto out_unlock; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5133 | |
| 5134 | /* Must be a multiple of chunk_size */ |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5135 | chunk = mddev->chunk_sectors; |
| 5136 | if (chunk) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 5137 | sector_t temp = max; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5138 | |
| 5139 | err = -EINVAL; |
| 5140 | if (sector_div(temp, chunk)) |
| 5141 | goto out_unlock; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5142 | } |
| 5143 | mddev->resync_max = max; |
| 5144 | } |
| 5145 | wake_up(&mddev->recovery_wait); |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5146 | err = 0; |
| 5147 | out_unlock: |
| 5148 | spin_unlock(&mddev->lock); |
| 5149 | return err ?: len; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5150 | } |
| 5151 | |
| 5152 | static struct md_sysfs_entry md_max_sync = |
| 5153 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 5154 | |
| 5155 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5156 | suspend_lo_show(struct mddev *mddev, char *page) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5157 | { |
| 5158 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 5159 | } |
| 5160 | |
| 5161 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5162 | suspend_lo_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5163 | { |
NeilBrown | b03e0cc | 2017-10-19 12:49:15 +1100 | [diff] [blame] | 5164 | unsigned long long new; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5165 | int err; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5166 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 5167 | err = kstrtoull(buf, 10, &new); |
| 5168 | if (err < 0) |
| 5169 | return err; |
| 5170 | if (new != (sector_t)new) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5171 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 5172 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5173 | err = mddev_lock(mddev); |
| 5174 | if (err) |
| 5175 | return err; |
| 5176 | err = -EINVAL; |
| 5177 | if (mddev->pers == NULL || |
| 5178 | mddev->pers->quiesce == NULL) |
| 5179 | goto unlock; |
NeilBrown | b03e0cc | 2017-10-19 12:49:15 +1100 | [diff] [blame] | 5180 | mddev_suspend(mddev); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 5181 | mddev->suspend_lo = new; |
NeilBrown | b03e0cc | 2017-10-19 12:49:15 +1100 | [diff] [blame] | 5182 | mddev_resume(mddev); |
| 5183 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5184 | err = 0; |
| 5185 | unlock: |
| 5186 | mddev_unlock(mddev); |
| 5187 | return err ?: len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5188 | } |
| 5189 | static struct md_sysfs_entry md_suspend_lo = |
| 5190 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 5191 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5192 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5193 | suspend_hi_show(struct mddev *mddev, char *page) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5194 | { |
| 5195 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 5196 | } |
| 5197 | |
| 5198 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5199 | suspend_hi_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5200 | { |
NeilBrown | b03e0cc | 2017-10-19 12:49:15 +1100 | [diff] [blame] | 5201 | unsigned long long new; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5202 | int err; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5203 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 5204 | err = kstrtoull(buf, 10, &new); |
| 5205 | if (err < 0) |
| 5206 | return err; |
| 5207 | if (new != (sector_t)new) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5208 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 5209 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5210 | err = mddev_lock(mddev); |
| 5211 | if (err) |
| 5212 | return err; |
| 5213 | err = -EINVAL; |
NeilBrown | b03e0cc | 2017-10-19 12:49:15 +1100 | [diff] [blame] | 5214 | if (mddev->pers == NULL) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5215 | goto unlock; |
NeilBrown | b03e0cc | 2017-10-19 12:49:15 +1100 | [diff] [blame] | 5216 | |
| 5217 | mddev_suspend(mddev); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 5218 | mddev->suspend_hi = new; |
NeilBrown | b03e0cc | 2017-10-19 12:49:15 +1100 | [diff] [blame] | 5219 | mddev_resume(mddev); |
| 5220 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5221 | err = 0; |
| 5222 | unlock: |
| 5223 | mddev_unlock(mddev); |
| 5224 | return err ?: len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5225 | } |
| 5226 | static struct md_sysfs_entry md_suspend_hi = |
| 5227 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 5228 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 5229 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5230 | reshape_position_show(struct mddev *mddev, char *page) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 5231 | { |
| 5232 | if (mddev->reshape_position != MaxSector) |
| 5233 | return sprintf(page, "%llu\n", |
| 5234 | (unsigned long long)mddev->reshape_position); |
| 5235 | strcpy(page, "none\n"); |
| 5236 | return 5; |
| 5237 | } |
| 5238 | |
| 5239 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5240 | reshape_position_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 5241 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5242 | struct md_rdev *rdev; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 5243 | unsigned long long new; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5244 | int err; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5245 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 5246 | err = kstrtoull(buf, 10, &new); |
| 5247 | if (err < 0) |
| 5248 | return err; |
| 5249 | if (new != (sector_t)new) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 5250 | return -EINVAL; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5251 | err = mddev_lock(mddev); |
| 5252 | if (err) |
| 5253 | return err; |
| 5254 | err = -EBUSY; |
| 5255 | if (mddev->pers) |
| 5256 | goto unlock; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 5257 | mddev->reshape_position = new; |
| 5258 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5259 | mddev->reshape_backwards = 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 5260 | mddev->new_level = mddev->level; |
| 5261 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5262 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5263 | rdev_for_each(rdev, mddev) |
| 5264 | rdev->new_data_offset = rdev->data_offset; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5265 | err = 0; |
| 5266 | unlock: |
| 5267 | mddev_unlock(mddev); |
| 5268 | return err ?: len; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 5269 | } |
| 5270 | |
| 5271 | static struct md_sysfs_entry md_reshape_position = |
| 5272 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 5273 | reshape_position_store); |
| 5274 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5275 | static ssize_t |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5276 | reshape_direction_show(struct mddev *mddev, char *page) |
| 5277 | { |
| 5278 | return sprintf(page, "%s\n", |
| 5279 | mddev->reshape_backwards ? "backwards" : "forwards"); |
| 5280 | } |
| 5281 | |
| 5282 | static ssize_t |
| 5283 | reshape_direction_store(struct mddev *mddev, const char *buf, size_t len) |
| 5284 | { |
| 5285 | int backwards = 0; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5286 | int err; |
| 5287 | |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5288 | if (cmd_match(buf, "forwards")) |
| 5289 | backwards = 0; |
| 5290 | else if (cmd_match(buf, "backwards")) |
| 5291 | backwards = 1; |
| 5292 | else |
| 5293 | return -EINVAL; |
| 5294 | if (mddev->reshape_backwards == backwards) |
| 5295 | return len; |
| 5296 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5297 | err = mddev_lock(mddev); |
| 5298 | if (err) |
| 5299 | return err; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5300 | /* check if we are allowed to change */ |
| 5301 | if (mddev->delta_disks) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5302 | err = -EBUSY; |
| 5303 | else if (mddev->persistent && |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5304 | mddev->major_version == 0) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5305 | err = -EINVAL; |
| 5306 | else |
| 5307 | mddev->reshape_backwards = backwards; |
| 5308 | mddev_unlock(mddev); |
| 5309 | return err ?: len; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5310 | } |
| 5311 | |
| 5312 | static struct md_sysfs_entry md_reshape_direction = |
| 5313 | __ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show, |
| 5314 | reshape_direction_store); |
| 5315 | |
| 5316 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5317 | array_size_show(struct mddev *mddev, char *page) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5318 | { |
| 5319 | if (mddev->external_size) |
| 5320 | return sprintf(page, "%llu\n", |
| 5321 | (unsigned long long)mddev->array_sectors/2); |
| 5322 | else |
| 5323 | return sprintf(page, "default\n"); |
| 5324 | } |
| 5325 | |
| 5326 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5327 | array_size_store(struct mddev *mddev, const char *buf, size_t len) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5328 | { |
| 5329 | sector_t sectors; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5330 | int err; |
| 5331 | |
| 5332 | err = mddev_lock(mddev); |
| 5333 | if (err) |
| 5334 | return err; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5335 | |
Guoqing Jiang | ab5a98b | 2016-05-02 11:33:13 -0400 | [diff] [blame] | 5336 | /* cluster raid doesn't support change array_sectors */ |
Zhilong Liu | b670883 | 2017-04-10 14:15:55 +0800 | [diff] [blame] | 5337 | if (mddev_is_clustered(mddev)) { |
| 5338 | mddev_unlock(mddev); |
Guoqing Jiang | ab5a98b | 2016-05-02 11:33:13 -0400 | [diff] [blame] | 5339 | return -EINVAL; |
Zhilong Liu | b670883 | 2017-04-10 14:15:55 +0800 | [diff] [blame] | 5340 | } |
Guoqing Jiang | ab5a98b | 2016-05-02 11:33:13 -0400 | [diff] [blame] | 5341 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5342 | if (strncmp(buf, "default", 7) == 0) { |
| 5343 | if (mddev->pers) |
| 5344 | sectors = mddev->pers->size(mddev, 0, 0); |
| 5345 | else |
| 5346 | sectors = mddev->array_sectors; |
| 5347 | |
| 5348 | mddev->external_size = 0; |
| 5349 | } else { |
| 5350 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5351 | err = -EINVAL; |
| 5352 | else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
| 5353 | err = -E2BIG; |
| 5354 | else |
| 5355 | mddev->external_size = 1; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5356 | } |
| 5357 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5358 | if (!err) { |
| 5359 | mddev->array_sectors = sectors; |
Christoph Hellwig | 2c247c5 | 2020-11-16 15:57:11 +0100 | [diff] [blame] | 5360 | if (mddev->pers) |
| 5361 | set_capacity_and_notify(mddev->gendisk, |
| 5362 | mddev->array_sectors); |
NeilBrown | cbe6ef1 | 2011-02-16 13:58:38 +1100 | [diff] [blame] | 5363 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5364 | mddev_unlock(mddev); |
| 5365 | return err ?: len; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5366 | } |
| 5367 | |
| 5368 | static struct md_sysfs_entry md_array_size = |
| 5369 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 5370 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5371 | |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 5372 | static ssize_t |
| 5373 | consistency_policy_show(struct mddev *mddev, char *page) |
| 5374 | { |
| 5375 | int ret; |
| 5376 | |
| 5377 | if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) { |
| 5378 | ret = sprintf(page, "journal\n"); |
| 5379 | } else if (test_bit(MD_HAS_PPL, &mddev->flags)) { |
| 5380 | ret = sprintf(page, "ppl\n"); |
| 5381 | } else if (mddev->bitmap) { |
| 5382 | ret = sprintf(page, "bitmap\n"); |
| 5383 | } else if (mddev->pers) { |
| 5384 | if (mddev->pers->sync_request) |
| 5385 | ret = sprintf(page, "resync\n"); |
| 5386 | else |
| 5387 | ret = sprintf(page, "none\n"); |
| 5388 | } else { |
| 5389 | ret = sprintf(page, "unknown\n"); |
| 5390 | } |
| 5391 | |
| 5392 | return ret; |
| 5393 | } |
| 5394 | |
| 5395 | static ssize_t |
| 5396 | consistency_policy_store(struct mddev *mddev, const char *buf, size_t len) |
| 5397 | { |
Artur Paszkiewicz | ba903a3 | 2017-03-09 10:00:03 +0100 | [diff] [blame] | 5398 | int err = 0; |
| 5399 | |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 5400 | if (mddev->pers) { |
Artur Paszkiewicz | ba903a3 | 2017-03-09 10:00:03 +0100 | [diff] [blame] | 5401 | if (mddev->pers->change_consistency_policy) |
| 5402 | err = mddev->pers->change_consistency_policy(mddev, buf); |
| 5403 | else |
| 5404 | err = -EBUSY; |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 5405 | } else if (mddev->external && strncmp(buf, "ppl", 3) == 0) { |
| 5406 | set_bit(MD_HAS_PPL, &mddev->flags); |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 5407 | } else { |
Artur Paszkiewicz | ba903a3 | 2017-03-09 10:00:03 +0100 | [diff] [blame] | 5408 | err = -EINVAL; |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 5409 | } |
Artur Paszkiewicz | ba903a3 | 2017-03-09 10:00:03 +0100 | [diff] [blame] | 5410 | |
| 5411 | return err ? err : len; |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 5412 | } |
| 5413 | |
| 5414 | static struct md_sysfs_entry md_consistency_policy = |
| 5415 | __ATTR(consistency_policy, S_IRUGO | S_IWUSR, consistency_policy_show, |
| 5416 | consistency_policy_store); |
| 5417 | |
Guoqing Jiang | 9a56784 | 2019-07-24 11:09:19 +0200 | [diff] [blame] | 5418 | static ssize_t fail_last_dev_show(struct mddev *mddev, char *page) |
| 5419 | { |
| 5420 | return sprintf(page, "%d\n", mddev->fail_last_dev); |
| 5421 | } |
| 5422 | |
| 5423 | /* |
| 5424 | * Setting fail_last_dev to true to allow last device to be forcibly removed |
| 5425 | * from RAID1/RAID10. |
| 5426 | */ |
| 5427 | static ssize_t |
| 5428 | fail_last_dev_store(struct mddev *mddev, const char *buf, size_t len) |
| 5429 | { |
| 5430 | int ret; |
| 5431 | bool value; |
| 5432 | |
| 5433 | ret = kstrtobool(buf, &value); |
| 5434 | if (ret) |
| 5435 | return ret; |
| 5436 | |
| 5437 | if (value != mddev->fail_last_dev) |
| 5438 | mddev->fail_last_dev = value; |
| 5439 | |
| 5440 | return len; |
| 5441 | } |
| 5442 | static struct md_sysfs_entry md_fail_last_dev = |
| 5443 | __ATTR(fail_last_dev, S_IRUGO | S_IWUSR, fail_last_dev_show, |
| 5444 | fail_last_dev_store); |
| 5445 | |
Guoqing Jiang | 3938f5f | 2019-12-23 10:48:56 +0100 | [diff] [blame] | 5446 | static ssize_t serialize_policy_show(struct mddev *mddev, char *page) |
| 5447 | { |
| 5448 | if (mddev->pers == NULL || (mddev->pers->level != 1)) |
| 5449 | return sprintf(page, "n/a\n"); |
| 5450 | else |
| 5451 | return sprintf(page, "%d\n", mddev->serialize_policy); |
| 5452 | } |
| 5453 | |
| 5454 | /* |
| 5455 | * Setting serialize_policy to true to enforce write IO is not reordered |
| 5456 | * for raid1. |
| 5457 | */ |
| 5458 | static ssize_t |
| 5459 | serialize_policy_store(struct mddev *mddev, const char *buf, size_t len) |
| 5460 | { |
| 5461 | int err; |
| 5462 | bool value; |
| 5463 | |
| 5464 | err = kstrtobool(buf, &value); |
| 5465 | if (err) |
| 5466 | return err; |
| 5467 | |
| 5468 | if (value == mddev->serialize_policy) |
| 5469 | return len; |
| 5470 | |
| 5471 | err = mddev_lock(mddev); |
| 5472 | if (err) |
| 5473 | return err; |
| 5474 | if (mddev->pers == NULL || (mddev->pers->level != 1)) { |
| 5475 | pr_err("md: serialize_policy is only effective for raid1\n"); |
| 5476 | err = -EINVAL; |
| 5477 | goto unlock; |
| 5478 | } |
| 5479 | |
| 5480 | mddev_suspend(mddev); |
| 5481 | if (value) |
| 5482 | mddev_create_serial_pool(mddev, NULL, true); |
| 5483 | else |
| 5484 | mddev_destroy_serial_pool(mddev, NULL, true); |
| 5485 | mddev->serialize_policy = value; |
| 5486 | mddev_resume(mddev); |
| 5487 | unlock: |
| 5488 | mddev_unlock(mddev); |
| 5489 | return err ?: len; |
| 5490 | } |
| 5491 | |
| 5492 | static struct md_sysfs_entry md_serialize_policy = |
| 5493 | __ATTR(serialize_policy, S_IRUGO | S_IWUSR, serialize_policy_show, |
| 5494 | serialize_policy_store); |
| 5495 | |
| 5496 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5497 | static struct attribute *md_default_attrs[] = { |
| 5498 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 5499 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5500 | &md_raid_disks.attr, |
Sebastian Parschauer | ec164d07 | 2020-07-28 12:01:39 +0200 | [diff] [blame] | 5501 | &md_uuid.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 5502 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5503 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 5504 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 5505 | &md_metadata.attr, |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 5506 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 5507 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5508 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 5509 | &md_reshape_position.attr, |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5510 | &md_reshape_direction.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5511 | &md_array_size.attr, |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 5512 | &max_corr_read_errors.attr, |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 5513 | &md_consistency_policy.attr, |
Guoqing Jiang | 9a56784 | 2019-07-24 11:09:19 +0200 | [diff] [blame] | 5514 | &md_fail_last_dev.attr, |
Guoqing Jiang | 3938f5f | 2019-12-23 10:48:56 +0100 | [diff] [blame] | 5515 | &md_serialize_policy.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 5516 | NULL, |
| 5517 | }; |
| 5518 | |
| 5519 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 5520 | &md_scan_mode.attr, |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 5521 | &md_last_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 5522 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5523 | &md_sync_min.attr, |
| 5524 | &md_sync_max.attr, |
| 5525 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 5526 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5527 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5528 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5529 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5530 | &md_suspend_lo.attr, |
| 5531 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 5532 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 5533 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5534 | NULL, |
| 5535 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 5536 | static struct attribute_group md_redundancy_group = { |
| 5537 | .name = NULL, |
| 5538 | .attrs = md_redundancy_attrs, |
| 5539 | }; |
| 5540 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5541 | static ssize_t |
| 5542 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 5543 | { |
| 5544 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5545 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 5546 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5547 | |
| 5548 | if (!entry->show) |
| 5549 | return -EIO; |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 5550 | spin_lock(&all_mddevs_lock); |
| 5551 | if (list_empty(&mddev->all_mddevs)) { |
| 5552 | spin_unlock(&all_mddevs_lock); |
| 5553 | return -EBUSY; |
| 5554 | } |
| 5555 | mddev_get(mddev); |
| 5556 | spin_unlock(&all_mddevs_lock); |
| 5557 | |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 5558 | rv = entry->show(mddev, page); |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 5559 | mddev_put(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 5560 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5561 | } |
| 5562 | |
| 5563 | static ssize_t |
| 5564 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 5565 | const char *page, size_t length) |
| 5566 | { |
| 5567 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5568 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 5569 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5570 | |
| 5571 | if (!entry->store) |
| 5572 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 5573 | if (!capable(CAP_SYS_ADMIN)) |
| 5574 | return -EACCES; |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 5575 | spin_lock(&all_mddevs_lock); |
| 5576 | if (list_empty(&mddev->all_mddevs)) { |
| 5577 | spin_unlock(&all_mddevs_lock); |
| 5578 | return -EBUSY; |
| 5579 | } |
| 5580 | mddev_get(mddev); |
| 5581 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5582 | rv = entry->store(mddev, page, length); |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 5583 | mddev_put(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 5584 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5585 | } |
| 5586 | |
| 5587 | static void md_free(struct kobject *ko) |
| 5588 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5589 | struct mddev *mddev = container_of(ko, struct mddev, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 5590 | |
| 5591 | if (mddev->sysfs_state) |
| 5592 | sysfs_put(mddev->sysfs_state); |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 5593 | if (mddev->sysfs_level) |
| 5594 | sysfs_put(mddev->sysfs_level); |
| 5595 | |
Bart Van Assche | d8115c35 | 2018-02-28 10:15:29 -0800 | [diff] [blame] | 5596 | if (mddev->gendisk) |
| 5597 | del_gendisk(mddev->gendisk); |
NeilBrown | 6cd18e7 | 2015-04-27 14:12:22 +1000 | [diff] [blame] | 5598 | if (mddev->queue) |
| 5599 | blk_cleanup_queue(mddev->queue); |
Bart Van Assche | d8115c35 | 2018-02-28 10:15:29 -0800 | [diff] [blame] | 5600 | if (mddev->gendisk) |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 5601 | put_disk(mddev->gendisk); |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 5602 | percpu_ref_exit(&mddev->writes_pending); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 5603 | |
Kent Overstreet | 28dec87 | 2018-06-07 20:52:54 -0400 | [diff] [blame] | 5604 | bioset_exit(&mddev->bio_set); |
| 5605 | bioset_exit(&mddev->sync_set); |
Artur Paszkiewicz | 41d2d84 | 2020-07-03 11:13:09 +0200 | [diff] [blame] | 5606 | mempool_exit(&mddev->md_io_pool); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5607 | kfree(mddev); |
| 5608 | } |
| 5609 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 5610 | static const struct sysfs_ops md_sysfs_ops = { |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5611 | .show = md_attr_show, |
| 5612 | .store = md_attr_store, |
| 5613 | }; |
| 5614 | static struct kobj_type md_ktype = { |
| 5615 | .release = md_free, |
| 5616 | .sysfs_ops = &md_sysfs_ops, |
| 5617 | .default_attrs = md_default_attrs, |
| 5618 | }; |
| 5619 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5620 | int mdp_major = 0; |
| 5621 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 5622 | static void mddev_delayed_delete(struct work_struct *ws) |
| 5623 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5624 | struct mddev *mddev = container_of(ws, struct mddev, del_work); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 5625 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 5626 | sysfs_remove_group(&mddev->kobj, &md_bitmap_group); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 5627 | kobject_del(&mddev->kobj); |
| 5628 | kobject_put(&mddev->kobj); |
| 5629 | } |
| 5630 | |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 5631 | static void no_op(struct percpu_ref *r) {} |
| 5632 | |
NeilBrown | a415c0f | 2017-06-05 16:05:13 +1000 | [diff] [blame] | 5633 | int mddev_init_writes_pending(struct mddev *mddev) |
| 5634 | { |
| 5635 | if (mddev->writes_pending.percpu_count_ptr) |
| 5636 | return 0; |
Roman Gushchin | ddde2af | 2019-05-07 10:01:49 -0700 | [diff] [blame] | 5637 | if (percpu_ref_init(&mddev->writes_pending, no_op, |
| 5638 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL) < 0) |
NeilBrown | a415c0f | 2017-06-05 16:05:13 +1000 | [diff] [blame] | 5639 | return -ENOMEM; |
| 5640 | /* We want to start with the refcount at zero */ |
| 5641 | percpu_ref_put(&mddev->writes_pending); |
| 5642 | return 0; |
| 5643 | } |
| 5644 | EXPORT_SYMBOL_GPL(mddev_init_writes_pending); |
| 5645 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5646 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5647 | { |
NeilBrown | 039b722 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 5648 | /* |
| 5649 | * If dev is zero, name is the name of a device to allocate with |
| 5650 | * an arbitrary minor number. It will be "md_???" |
| 5651 | * If dev is non-zero it must be a device number with a MAJOR of |
| 5652 | * MD_MAJOR or mdp_major. In this case, if "name" is NULL, then |
| 5653 | * the device is being created by opening a node in /dev. |
| 5654 | * If "name" is not NULL, the device is being created by |
| 5655 | * writing to /sys/module/md_mod/parameters/new_array. |
| 5656 | */ |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 5657 | static DEFINE_MUTEX(disks_mutex); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5658 | struct mddev *mddev = mddev_find(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5659 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5660 | int partitioned; |
| 5661 | int shift; |
| 5662 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 5663 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5664 | |
| 5665 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5666 | return -ENODEV; |
| 5667 | |
| 5668 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 5669 | shift = partitioned ? MdpMinorShift : 0; |
| 5670 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5671 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 5672 | /* wait for any previous instance of this device to be |
| 5673 | * completely removed (mddev_delayed_delete). |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5674 | */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 5675 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5676 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 5677 | mutex_lock(&disks_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5678 | error = -EEXIST; |
| 5679 | if (mddev->gendisk) |
| 5680 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5681 | |
NeilBrown | 039b722 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 5682 | if (name && !dev) { |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5683 | /* Need to ensure that 'name' is not a duplicate. |
| 5684 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5685 | struct mddev *mddev2; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5686 | spin_lock(&all_mddevs_lock); |
| 5687 | |
| 5688 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 5689 | if (mddev2->gendisk && |
| 5690 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 5691 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5692 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5693 | } |
| 5694 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5695 | } |
NeilBrown | 039b722 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 5696 | if (name && dev) |
| 5697 | /* |
| 5698 | * Creating /dev/mdNNN via "newarray", so adjust hold_active. |
| 5699 | */ |
| 5700 | mddev->hold_active = UNTIL_STOP; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5701 | |
Artur Paszkiewicz | 41d2d84 | 2020-07-03 11:13:09 +0200 | [diff] [blame] | 5702 | error = mempool_init_kmalloc_pool(&mddev->md_io_pool, BIO_POOL_SIZE, |
| 5703 | sizeof(struct md_io)); |
| 5704 | if (error) |
| 5705 | goto abort; |
| 5706 | |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5707 | error = -ENOMEM; |
Christoph Hellwig | c62b37d | 2020-07-01 10:59:43 +0200 | [diff] [blame] | 5708 | mddev->queue = blk_alloc_queue(NUMA_NO_NODE); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5709 | if (!mddev->queue) |
| 5710 | goto abort; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5711 | |
Martin K. Petersen | b1bd055 | 2012-01-11 16:27:11 +0100 | [diff] [blame] | 5712 | blk_set_stacking_limits(&mddev->queue->limits); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5713 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5714 | disk = alloc_disk(1 << shift); |
| 5715 | if (!disk) { |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5716 | blk_cleanup_queue(mddev->queue); |
| 5717 | mddev->queue = NULL; |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5718 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5719 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5720 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5721 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5722 | if (name) |
| 5723 | strcpy(disk->disk_name, name); |
| 5724 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5725 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f46 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 5726 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5727 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5728 | disk->fops = &md_fops; |
| 5729 | disk->private_data = mddev; |
| 5730 | disk->queue = mddev->queue; |
Jens Axboe | 56883a7 | 2016-03-30 10:16:53 -0600 | [diff] [blame] | 5731 | blk_queue_write_cache(mddev->queue, true, true); |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 5732 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5733 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 5734 | * remove it now. |
| 5735 | */ |
| 5736 | disk->flags |= GENHD_FL_EXT_DEVT; |
Christoph Hellwig | a564e23 | 2020-07-08 14:25:41 +0200 | [diff] [blame] | 5737 | disk->events |= DISK_EVENT_MEDIA_CHANGE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5738 | mddev->gendisk = disk; |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 5739 | /* As soon as we call add_disk(), another thread could get |
| 5740 | * through to md_open, so make sure it doesn't get too far |
| 5741 | */ |
| 5742 | mutex_lock(&mddev->open_mutex); |
| 5743 | add_disk(disk); |
| 5744 | |
Kent Overstreet | 28dec87 | 2018-06-07 20:52:54 -0400 | [diff] [blame] | 5745 | error = kobject_add(&mddev->kobj, &disk_to_dev(disk)->kobj, "%s", "md"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5746 | if (error) { |
| 5747 | /* This isn't possible, but as kobject_init_and_add is marked |
| 5748 | * __must_check, we must do something with the result |
| 5749 | */ |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5750 | pr_debug("md: cannot register %s/md - name in use\n", |
| 5751 | disk->disk_name); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5752 | error = 0; |
| 5753 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5754 | if (mddev->kobj.sd && |
| 5755 | sysfs_create_group(&mddev->kobj, &md_bitmap_group)) |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5756 | pr_debug("pointless warning\n"); |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 5757 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5758 | abort: |
| 5759 | mutex_unlock(&disks_mutex); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5760 | if (!error && mddev->kobj.sd) { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 5761 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5762 | mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state"); |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 5763 | mddev->sysfs_level = sysfs_get_dirent_safe(mddev->kobj.sd, "level"); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 5764 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5765 | mddev_put(mddev); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5766 | return error; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5767 | } |
| 5768 | |
Christoph Hellwig | 28144f9 | 2020-10-29 15:58:34 +0100 | [diff] [blame] | 5769 | static void md_probe(dev_t dev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5770 | { |
Christoph Hellwig | 28144f9 | 2020-10-29 15:58:34 +0100 | [diff] [blame] | 5771 | if (MAJOR(dev) == MD_MAJOR && MINOR(dev) >= 512) |
| 5772 | return; |
NeilBrown | 78b6350 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 5773 | if (create_on_open) |
| 5774 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5775 | } |
| 5776 | |
Kees Cook | e4dca7b | 2017-10-17 19:04:42 -0700 | [diff] [blame] | 5777 | static int add_named_array(const char *val, const struct kernel_param *kp) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5778 | { |
NeilBrown | 039b722 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 5779 | /* |
| 5780 | * val must be "md_*" or "mdNNN". |
| 5781 | * For "md_*" we allocate an array with a large free minor number, and |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5782 | * set the name to val. val must not already be an active name. |
NeilBrown | 039b722 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 5783 | * For "mdNNN" we allocate an array with the minor number NNN |
| 5784 | * which must not already be in use. |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5785 | */ |
| 5786 | int len = strlen(val); |
| 5787 | char buf[DISK_NAME_LEN]; |
NeilBrown | 039b722 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 5788 | unsigned long devnum; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5789 | |
| 5790 | while (len && val[len-1] == '\n') |
| 5791 | len--; |
| 5792 | if (len >= DISK_NAME_LEN) |
| 5793 | return -E2BIG; |
| 5794 | strlcpy(buf, val, len+1); |
NeilBrown | 039b722 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 5795 | if (strncmp(buf, "md_", 3) == 0) |
| 5796 | return md_alloc(0, buf); |
| 5797 | if (strncmp(buf, "md", 2) == 0 && |
| 5798 | isdigit(buf[2]) && |
| 5799 | kstrtoul(buf+2, 10, &devnum) == 0 && |
| 5800 | devnum <= MINORMASK) |
| 5801 | return md_alloc(MKDEV(MD_MAJOR, devnum), NULL); |
| 5802 | |
| 5803 | return -EINVAL; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5804 | } |
| 5805 | |
Kees Cook | 8376d3c | 2017-10-16 17:01:48 -0700 | [diff] [blame] | 5806 | static void md_safemode_timeout(struct timer_list *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5807 | { |
Kees Cook | 8376d3c | 2017-10-16 17:01:48 -0700 | [diff] [blame] | 5808 | struct mddev *mddev = from_timer(mddev, t, safemode_timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5809 | |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 5810 | mddev->safemode = 1; |
| 5811 | if (mddev->external) |
| 5812 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
| 5813 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5814 | md_wakeup_thread(mddev->thread); |
| 5815 | } |
| 5816 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5817 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5818 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5819 | int md_run(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5820 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5821 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5822 | struct md_rdev *rdev; |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 5823 | struct md_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5824 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 5825 | if (list_empty(&mddev->disks)) |
| 5826 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5827 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5828 | |
| 5829 | if (mddev->pers) |
| 5830 | return -EBUSY; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 5831 | /* Cannot run until previous stop completes properly */ |
| 5832 | if (mddev->sysfs_active) |
| 5833 | return -EBUSY; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 5834 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5835 | /* |
| 5836 | * Analyze all RAID superblock(s) |
| 5837 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 5838 | if (!mddev->raid_disks) { |
| 5839 | if (!mddev->persistent) |
| 5840 | return -EINVAL; |
Yufen Yu | 6a5cb53 | 2019-10-16 16:00:03 +0800 | [diff] [blame] | 5841 | err = analyze_sbs(mddev); |
| 5842 | if (err) |
| 5843 | return -EINVAL; |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 5844 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5845 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5846 | if (mddev->level != LEVEL_NONE) |
| 5847 | request_module("md-level-%d", mddev->level); |
| 5848 | else if (mddev->clevel[0]) |
| 5849 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5850 | |
| 5851 | /* |
| 5852 | * Drop all container device buffers, from now on |
| 5853 | * the only valid external interface is through the md |
| 5854 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5855 | */ |
Heinz Mauelshagen | 4b6c106 | 2018-02-02 23:13:19 +0100 | [diff] [blame] | 5856 | mddev->has_superblocks = false; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5857 | rdev_for_each(rdev, mddev) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5858 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5859 | continue; |
| 5860 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 5861 | invalidate_bdev(rdev->bdev); |
NeilBrown | 97b20ef | 2017-04-13 08:53:48 +1000 | [diff] [blame] | 5862 | if (mddev->ro != 1 && |
| 5863 | (bdev_read_only(rdev->bdev) || |
| 5864 | bdev_read_only(rdev->meta_bdev))) { |
| 5865 | mddev->ro = 1; |
| 5866 | if (mddev->gendisk) |
| 5867 | set_disk_ro(mddev->gendisk, 1); |
| 5868 | } |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5869 | |
Heinz Mauelshagen | 4b6c106 | 2018-02-02 23:13:19 +0100 | [diff] [blame] | 5870 | if (rdev->sb_page) |
| 5871 | mddev->has_superblocks = true; |
| 5872 | |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5873 | /* perform some consistency tests on the device. |
| 5874 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5875 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5876 | */ |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 5877 | if (rdev->meta_bdev) { |
| 5878 | /* Nothing to check */; |
| 5879 | } else if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5880 | if (mddev->dev_sectors && |
| 5881 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5882 | > rdev->sb_start) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5883 | pr_warn("md: %s: data overlaps metadata\n", |
| 5884 | mdname(mddev)); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5885 | return -EINVAL; |
| 5886 | } |
| 5887 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5888 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5889 | > rdev->data_offset) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5890 | pr_warn("md: %s: metadata overlaps data\n", |
| 5891 | mdname(mddev)); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5892 | return -EINVAL; |
| 5893 | } |
| 5894 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5895 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5896 | } |
| 5897 | |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 5898 | if (!bioset_initialized(&mddev->bio_set)) { |
| 5899 | err = bioset_init(&mddev->bio_set, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS); |
| 5900 | if (err) |
| 5901 | return err; |
Ming Lei | 1027317 | 2017-02-14 23:29:00 +0800 | [diff] [blame] | 5902 | } |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 5903 | if (!bioset_initialized(&mddev->sync_set)) { |
| 5904 | err = bioset_init(&mddev->sync_set, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS); |
| 5905 | if (err) |
Kent Overstreet | 28dec87 | 2018-06-07 20:52:54 -0400 | [diff] [blame] | 5906 | return err; |
NeilBrown | 5a85071 | 2017-06-21 09:12:21 +1000 | [diff] [blame] | 5907 | } |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 5908 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5909 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5910 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5911 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5912 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5913 | if (mddev->level != LEVEL_NONE) |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5914 | pr_warn("md: personality for level %d is not loaded!\n", |
| 5915 | mddev->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5916 | else |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5917 | pr_warn("md: personality for level %s is not loaded!\n", |
| 5918 | mddev->clevel); |
Shaohua Li | bfc9dfd | 2018-06-13 08:39:49 -0700 | [diff] [blame] | 5919 | err = -EINVAL; |
| 5920 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5921 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5922 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5923 | if (mddev->level != pers->level) { |
| 5924 | mddev->level = pers->level; |
| 5925 | mddev->new_level = pers->level; |
| 5926 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5927 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5928 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5929 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5930 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5931 | /* This personality cannot handle reshaping... */ |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5932 | module_put(pers->owner); |
Shaohua Li | bfc9dfd | 2018-06-13 08:39:49 -0700 | [diff] [blame] | 5933 | err = -EINVAL; |
| 5934 | goto abort; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5935 | } |
| 5936 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5937 | if (pers->sync_request) { |
| 5938 | /* Warn if this is a potentially silly |
| 5939 | * configuration. |
| 5940 | */ |
| 5941 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5942 | struct md_rdev *rdev2; |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5943 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5944 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5945 | rdev_for_each(rdev, mddev) |
| 5946 | rdev_for_each(rdev2, mddev) { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5947 | if (rdev < rdev2 && |
Christoph Hellwig | 61a27e1f | 2020-09-03 07:40:58 +0200 | [diff] [blame] | 5948 | rdev->bdev->bd_disk == |
| 5949 | rdev2->bdev->bd_disk) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5950 | pr_warn("%s: WARNING: %s appears to be on the same physical disk as %s.\n", |
| 5951 | mdname(mddev), |
| 5952 | bdevname(rdev->bdev,b), |
| 5953 | bdevname(rdev2->bdev,b2)); |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5954 | warned = 1; |
| 5955 | } |
| 5956 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5957 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5958 | if (warned) |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5959 | pr_warn("True protection against single-disk failure might be compromised.\n"); |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5960 | } |
| 5961 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 5962 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5963 | /* may be over-ridden by personality */ |
| 5964 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 5965 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5966 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5967 | |
NeilBrown | 0f9552b5 | 2009-12-30 12:08:50 +1100 | [diff] [blame] | 5968 | if (start_readonly && mddev->ro == 0) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5969 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 5970 | |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5971 | err = pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 5972 | if (err) |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5973 | pr_warn("md: pers->run() failed ...\n"); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5974 | else if (pers->size(mddev, 0, 0) < mddev->array_sectors) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5975 | WARN_ONCE(!mddev->external_size, |
| 5976 | "%s: default size too small, but 'external_size' not in effect?\n", |
| 5977 | __func__); |
| 5978 | pr_warn("md: invalid array_size %llu > default size %llu\n", |
| 5979 | (unsigned long long)mddev->array_sectors / 2, |
| 5980 | (unsigned long long)pers->size(mddev, 0, 0) / 2); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5981 | err = -EINVAL; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5982 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5983 | if (err == 0 && pers->sync_request && |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 5984 | (mddev->bitmap_info.file || mddev->bitmap_info.offset)) { |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 5985 | struct bitmap *bitmap; |
| 5986 | |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 5987 | bitmap = md_bitmap_create(mddev, -1); |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 5988 | if (IS_ERR(bitmap)) { |
| 5989 | err = PTR_ERR(bitmap); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5990 | pr_warn("%s: failed to create bitmap (%d)\n", |
| 5991 | mdname(mddev), err); |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 5992 | } else |
| 5993 | mddev->bitmap = bitmap; |
| 5994 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 5995 | } |
Guoqing Jiang | d494549 | 2019-06-14 17:10:39 +0800 | [diff] [blame] | 5996 | if (err) |
| 5997 | goto bitmap_abort; |
Guoqing Jiang | 3e148a3 | 2019-06-19 17:30:46 +0800 | [diff] [blame] | 5998 | |
| 5999 | if (mddev->bitmap_info.max_write_behind > 0) { |
Guoqing Jiang | 3e173ab | 2019-12-23 10:48:54 +0100 | [diff] [blame] | 6000 | bool create_pool = false; |
Guoqing Jiang | 3e148a3 | 2019-06-19 17:30:46 +0800 | [diff] [blame] | 6001 | |
| 6002 | rdev_for_each(rdev, mddev) { |
| 6003 | if (test_bit(WriteMostly, &rdev->flags) && |
Guoqing Jiang | 404659c | 2019-12-23 10:48:53 +0100 | [diff] [blame] | 6004 | rdev_init_serial(rdev)) |
Guoqing Jiang | 3e173ab | 2019-12-23 10:48:54 +0100 | [diff] [blame] | 6005 | create_pool = true; |
Guoqing Jiang | 3e148a3 | 2019-06-19 17:30:46 +0800 | [diff] [blame] | 6006 | } |
Guoqing Jiang | 3e173ab | 2019-12-23 10:48:54 +0100 | [diff] [blame] | 6007 | if (create_pool && mddev->serial_info_pool == NULL) { |
Guoqing Jiang | 404659c | 2019-12-23 10:48:53 +0100 | [diff] [blame] | 6008 | mddev->serial_info_pool = |
| 6009 | mempool_create_kmalloc_pool(NR_SERIAL_INFOS, |
| 6010 | sizeof(struct serial_info)); |
| 6011 | if (!mddev->serial_info_pool) { |
Guoqing Jiang | 3e148a3 | 2019-06-19 17:30:46 +0800 | [diff] [blame] | 6012 | err = -ENOMEM; |
Guoqing Jiang | d494549 | 2019-06-14 17:10:39 +0800 | [diff] [blame] | 6013 | goto bitmap_abort; |
Guoqing Jiang | 3e148a3 | 2019-06-19 17:30:46 +0800 | [diff] [blame] | 6014 | } |
| 6015 | } |
| 6016 | } |
| 6017 | |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 6018 | if (mddev->queue) { |
Shaohua Li | bb086a8 | 2016-09-30 09:45:40 -0700 | [diff] [blame] | 6019 | bool nonrot = true; |
| 6020 | |
| 6021 | rdev_for_each(rdev, mddev) { |
| 6022 | if (rdev->raid_disk >= 0 && |
| 6023 | !blk_queue_nonrot(bdev_get_queue(rdev->bdev))) { |
| 6024 | nonrot = false; |
| 6025 | break; |
| 6026 | } |
| 6027 | } |
| 6028 | if (mddev->degraded) |
| 6029 | nonrot = false; |
| 6030 | if (nonrot) |
Bart Van Assche | 8b904b5 | 2018-03-07 17:10:10 -0800 | [diff] [blame] | 6031 | blk_queue_flag_set(QUEUE_FLAG_NONROT, mddev->queue); |
Shaohua Li | bb086a8 | 2016-09-30 09:45:40 -0700 | [diff] [blame] | 6032 | else |
Bart Van Assche | 8b904b5 | 2018-03-07 17:10:10 -0800 | [diff] [blame] | 6033 | blk_queue_flag_clear(QUEUE_FLAG_NONROT, mddev->queue); |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 6034 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 6035 | if (pers->sync_request) { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6036 | if (mddev->kobj.sd && |
| 6037 | sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6038 | pr_warn("md: cannot register extra attributes for %s\n", |
| 6039 | mdname(mddev)); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6040 | mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action"); |
Junxiao Bi | e8efa9b | 2020-08-04 17:27:18 -0700 | [diff] [blame] | 6041 | mddev->sysfs_completed = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_completed"); |
| 6042 | mddev->sysfs_degraded = sysfs_get_dirent_safe(mddev->kobj.sd, "degraded"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 6043 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 6044 | mddev->ro = 0; |
| 6045 | |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 6046 | atomic_set(&mddev->max_corr_read_errors, |
| 6047 | MD_DEFAULT_MAX_CORRECTED_READ_ERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6048 | mddev->safemode = 0; |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 6049 | if (mddev_is_clustered(mddev)) |
| 6050 | mddev->safemode_delay = 0; |
| 6051 | else |
Zhao Heming | 7c9d5c5 | 2020-07-21 02:08:52 +0800 | [diff] [blame] | 6052 | mddev->safemode_delay = DEFAULT_SAFEMODE_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6053 | mddev->in_sync = 1; |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 6054 | smp_wmb(); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 6055 | spin_lock(&mddev->lock); |
| 6056 | mddev->pers = pers; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 6057 | spin_unlock(&mddev->lock); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 6058 | rdev_for_each(rdev, mddev) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 6059 | if (rdev->raid_disk >= 0) |
Yufen Yu | e5b521e | 2019-06-14 15:41:07 -0700 | [diff] [blame] | 6060 | sysfs_link_rdev(mddev, rdev); /* failure here is OK */ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6061 | |
NeilBrown | a4a3d26 | 2015-07-17 11:57:30 +1000 | [diff] [blame] | 6062 | if (mddev->degraded && !mddev->ro) |
| 6063 | /* This ensures that recovering status is reported immediately |
| 6064 | * via sysfs - until a lack of spares is confirmed. |
| 6065 | */ |
| 6066 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6067 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6068 | |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 6069 | if (mddev->sb_flags) |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6070 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6071 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6072 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6073 | return 0; |
Xiao Ni | b126194 | 2018-01-24 12:17:38 +0800 | [diff] [blame] | 6074 | |
Guoqing Jiang | d494549 | 2019-06-14 17:10:39 +0800 | [diff] [blame] | 6075 | bitmap_abort: |
| 6076 | mddev_detach(mddev); |
| 6077 | if (mddev->private) |
| 6078 | pers->free(mddev, mddev->private); |
| 6079 | mddev->private = NULL; |
| 6080 | module_put(pers->owner); |
| 6081 | md_bitmap_destroy(mddev); |
Xiao Ni | b126194 | 2018-01-24 12:17:38 +0800 | [diff] [blame] | 6082 | abort: |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 6083 | bioset_exit(&mddev->bio_set); |
| 6084 | bioset_exit(&mddev->sync_set); |
Xiao Ni | b126194 | 2018-01-24 12:17:38 +0800 | [diff] [blame] | 6085 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6086 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 6087 | EXPORT_SYMBOL_GPL(md_run); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6088 | |
Christoph Hellwig | 7e0adbf | 2020-06-07 17:31:19 +0200 | [diff] [blame] | 6089 | int do_md_run(struct mddev *mddev) |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 6090 | { |
| 6091 | int err; |
| 6092 | |
NeilBrown | 9d4b45d | 2019-08-20 10:21:09 +1000 | [diff] [blame] | 6093 | set_bit(MD_NOT_READY, &mddev->flags); |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 6094 | err = md_run(mddev); |
| 6095 | if (err) |
| 6096 | goto out; |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 6097 | err = md_bitmap_load(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6098 | if (err) { |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 6099 | md_bitmap_destroy(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6100 | goto out; |
| 6101 | } |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 6102 | |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 6103 | if (mddev_is_clustered(mddev)) |
| 6104 | md_allow_write(mddev); |
| 6105 | |
Song Liu | d5d885f | 2017-11-19 22:17:01 -0800 | [diff] [blame] | 6106 | /* run start up tasks that require md_thread */ |
| 6107 | md_start(mddev); |
| 6108 | |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 6109 | md_wakeup_thread(mddev->thread); |
| 6110 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
| 6111 | |
Christoph Hellwig | 2c247c5 | 2020-11-16 15:57:11 +0100 | [diff] [blame] | 6112 | set_capacity_and_notify(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 9d4b45d | 2019-08-20 10:21:09 +1000 | [diff] [blame] | 6113 | clear_bit(MD_NOT_READY, &mddev->flags); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6114 | mddev->changed = 1; |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 6115 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
NeilBrown | 9d4b45d | 2019-08-20 10:21:09 +1000 | [diff] [blame] | 6116 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
| 6117 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 6118 | sysfs_notify_dirent_safe(mddev->sysfs_degraded); |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 6119 | out: |
NeilBrown | 9d4b45d | 2019-08-20 10:21:09 +1000 | [diff] [blame] | 6120 | clear_bit(MD_NOT_READY, &mddev->flags); |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 6121 | return err; |
| 6122 | } |
| 6123 | |
Song Liu | d5d885f | 2017-11-19 22:17:01 -0800 | [diff] [blame] | 6124 | int md_start(struct mddev *mddev) |
| 6125 | { |
| 6126 | int ret = 0; |
| 6127 | |
| 6128 | if (mddev->pers->start) { |
| 6129 | set_bit(MD_RECOVERY_WAIT, &mddev->recovery); |
| 6130 | md_wakeup_thread(mddev->thread); |
| 6131 | ret = mddev->pers->start(mddev); |
| 6132 | clear_bit(MD_RECOVERY_WAIT, &mddev->recovery); |
| 6133 | md_wakeup_thread(mddev->sync_thread); |
| 6134 | } |
| 6135 | return ret; |
| 6136 | } |
| 6137 | EXPORT_SYMBOL_GPL(md_start); |
| 6138 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6139 | static int restart_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6140 | { |
| 6141 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | 97b20ef | 2017-04-13 08:53:48 +1000 | [diff] [blame] | 6142 | struct md_rdev *rdev; |
| 6143 | bool has_journal = false; |
| 6144 | bool has_readonly = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6145 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 6146 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6147 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 6148 | return -ENXIO; |
| 6149 | if (!mddev->pers) |
| 6150 | return -EINVAL; |
| 6151 | if (!mddev->ro) |
| 6152 | return -EBUSY; |
Song Liu | 339421d | 2015-10-08 21:54:13 -0700 | [diff] [blame] | 6153 | |
NeilBrown | 97b20ef | 2017-04-13 08:53:48 +1000 | [diff] [blame] | 6154 | rcu_read_lock(); |
| 6155 | rdev_for_each_rcu(rdev, mddev) { |
| 6156 | if (test_bit(Journal, &rdev->flags) && |
| 6157 | !test_bit(Faulty, &rdev->flags)) |
| 6158 | has_journal = true; |
| 6159 | if (bdev_read_only(rdev->bdev)) |
| 6160 | has_readonly = true; |
Song Liu | 339421d | 2015-10-08 21:54:13 -0700 | [diff] [blame] | 6161 | } |
NeilBrown | 97b20ef | 2017-04-13 08:53:48 +1000 | [diff] [blame] | 6162 | rcu_read_unlock(); |
| 6163 | if (test_bit(MD_HAS_JOURNAL, &mddev->flags) && !has_journal) |
| 6164 | /* Don't restart rw with journal missing/faulty */ |
| 6165 | return -EINVAL; |
| 6166 | if (has_readonly) |
| 6167 | return -EROFS; |
Song Liu | 339421d | 2015-10-08 21:54:13 -0700 | [diff] [blame] | 6168 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 6169 | mddev->safemode = 0; |
| 6170 | mddev->ro = 0; |
| 6171 | set_disk_ro(disk, 0); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6172 | pr_debug("md: %s switched to read-write mode.\n", mdname(mddev)); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 6173 | /* Kick recovery or resync if necessary */ |
| 6174 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6175 | md_wakeup_thread(mddev->thread); |
| 6176 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6177 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 6178 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6179 | } |
| 6180 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6181 | static void md_clean(struct mddev *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6182 | { |
| 6183 | mddev->array_sectors = 0; |
| 6184 | mddev->external_size = 0; |
| 6185 | mddev->dev_sectors = 0; |
| 6186 | mddev->raid_disks = 0; |
| 6187 | mddev->recovery_cp = 0; |
| 6188 | mddev->resync_min = 0; |
| 6189 | mddev->resync_max = MaxSector; |
| 6190 | mddev->reshape_position = MaxSector; |
| 6191 | mddev->external = 0; |
| 6192 | mddev->persistent = 0; |
| 6193 | mddev->level = LEVEL_NONE; |
| 6194 | mddev->clevel[0] = 0; |
| 6195 | mddev->flags = 0; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 6196 | mddev->sb_flags = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6197 | mddev->ro = 0; |
| 6198 | mddev->metadata_type[0] = 0; |
| 6199 | mddev->chunk_sectors = 0; |
| 6200 | mddev->ctime = mddev->utime = 0; |
| 6201 | mddev->layout = 0; |
| 6202 | mddev->max_disks = 0; |
| 6203 | mddev->events = 0; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 6204 | mddev->can_decrease_events = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6205 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6206 | mddev->reshape_backwards = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6207 | mddev->new_level = LEVEL_NONE; |
| 6208 | mddev->new_layout = 0; |
| 6209 | mddev->new_chunk_sectors = 0; |
| 6210 | mddev->curr_resync = 0; |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 6211 | atomic64_set(&mddev->resync_mismatches, 0); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6212 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 6213 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 6214 | mddev->recovery = 0; |
| 6215 | mddev->in_sync = 0; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6216 | mddev->changed = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6217 | mddev->degraded = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6218 | mddev->safemode = 0; |
NeilBrown | bd69192 | 2015-06-25 17:01:40 +1000 | [diff] [blame] | 6219 | mddev->private = NULL; |
Guoqing Jiang | c20c33f | 2016-08-12 13:42:38 +0800 | [diff] [blame] | 6220 | mddev->cluster_info = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6221 | mddev->bitmap_info.offset = 0; |
| 6222 | mddev->bitmap_info.default_offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 6223 | mddev->bitmap_info.default_space = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6224 | mddev->bitmap_info.chunksize = 0; |
| 6225 | mddev->bitmap_info.daemon_sleep = 0; |
| 6226 | mddev->bitmap_info.max_write_behind = 0; |
Guoqing Jiang | c20c33f | 2016-08-12 13:42:38 +0800 | [diff] [blame] | 6227 | mddev->bitmap_info.nodes = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6228 | } |
| 6229 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6230 | static void __md_stop_writes(struct mddev *mddev) |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 6231 | { |
NeilBrown | 6b6204e | 2013-05-09 09:48:30 +1000 | [diff] [blame] | 6232 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Guoqing Jiang | 21e0958 | 2020-04-04 23:57:07 +0200 | [diff] [blame] | 6233 | if (work_pending(&mddev->del_work)) |
| 6234 | flush_workqueue(md_misc_wq); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 6235 | if (mddev->sync_thread) { |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 6236 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 6237 | md_reap_sync_thread(mddev); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 6238 | } |
| 6239 | |
| 6240 | del_timer_sync(&mddev->safemode_timer); |
| 6241 | |
Shaohua Li | 034e33f | 2016-11-21 10:29:19 -0800 | [diff] [blame] | 6242 | if (mddev->pers && mddev->pers->quiesce) { |
| 6243 | mddev->pers->quiesce(mddev, 1); |
| 6244 | mddev->pers->quiesce(mddev, 0); |
| 6245 | } |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 6246 | md_bitmap_flush(mddev); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 6247 | |
NeilBrown | b6d428c | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 6248 | if (mddev->ro == 0 && |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 6249 | ((!mddev->in_sync && !mddev_is_clustered(mddev)) || |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 6250 | mddev->sb_flags)) { |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 6251 | /* mark array as shutdown cleanly */ |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 6252 | if (!mddev_is_clustered(mddev)) |
| 6253 | mddev->in_sync = 1; |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 6254 | md_update_sb(mddev, 1); |
| 6255 | } |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 6256 | /* disable policy to guarantee rdevs free resources for serialization */ |
| 6257 | mddev->serialize_policy = 0; |
| 6258 | mddev_destroy_serial_pool(mddev, NULL, true); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 6259 | } |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 6260 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6261 | void md_stop_writes(struct mddev *mddev) |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 6262 | { |
NeilBrown | 29f097c | 2013-11-14 17:54:51 +1100 | [diff] [blame] | 6263 | mddev_lock_nointr(mddev); |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 6264 | __md_stop_writes(mddev); |
| 6265 | mddev_unlock(mddev); |
| 6266 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 6267 | EXPORT_SYMBOL_GPL(md_stop_writes); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 6268 | |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 6269 | static void mddev_detach(struct mddev *mddev) |
| 6270 | { |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 6271 | md_bitmap_wait_behind_writes(mddev); |
Guoqing Jiang | 6b40bec | 2020-02-11 11:10:04 +0100 | [diff] [blame] | 6272 | if (mddev->pers && mddev->pers->quiesce && !mddev->suspended) { |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 6273 | mddev->pers->quiesce(mddev, 1); |
| 6274 | mddev->pers->quiesce(mddev, 0); |
| 6275 | } |
| 6276 | md_unregister_thread(&mddev->thread); |
| 6277 | if (mddev->queue) |
| 6278 | blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/ |
| 6279 | } |
| 6280 | |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 6281 | static void __md_stop(struct mddev *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6282 | { |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 6283 | struct md_personality *pers = mddev->pers; |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 6284 | md_bitmap_destroy(mddev); |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 6285 | mddev_detach(mddev); |
NeilBrown | ee5d004 | 2015-07-22 10:20:07 +1000 | [diff] [blame] | 6286 | /* Ensure ->event_work is done */ |
Guoqing Jiang | 21e0958 | 2020-04-04 23:57:07 +0200 | [diff] [blame] | 6287 | if (mddev->event_work.func) |
| 6288 | flush_workqueue(md_misc_wq); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 6289 | spin_lock(&mddev->lock); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6290 | mddev->pers = NULL; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 6291 | spin_unlock(&mddev->lock); |
| 6292 | pers->free(mddev, mddev->private); |
NeilBrown | bd69192 | 2015-06-25 17:01:40 +1000 | [diff] [blame] | 6293 | mddev->private = NULL; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 6294 | if (pers->sync_request && mddev->to_remove == NULL) |
| 6295 | mddev->to_remove = &md_redundancy_group; |
| 6296 | module_put(pers->owner); |
NeilBrown | cca9cf9 | 2010-04-01 12:08:16 +1100 | [diff] [blame] | 6297 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Jack Wang | 6aaa58c | 2018-10-19 16:21:31 +0200 | [diff] [blame] | 6298 | } |
| 6299 | |
| 6300 | void md_stop(struct mddev *mddev) |
| 6301 | { |
| 6302 | /* stop the array and free an attached data structures. |
| 6303 | * This is called from dm-raid |
| 6304 | */ |
| 6305 | __md_stop(mddev); |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 6306 | bioset_exit(&mddev->bio_set); |
| 6307 | bioset_exit(&mddev->sync_set); |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 6308 | } |
| 6309 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 6310 | EXPORT_SYMBOL_GPL(md_stop); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6311 | |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 6312 | static int md_set_readonly(struct mddev *mddev, struct block_device *bdev) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 6313 | { |
| 6314 | int err = 0; |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6315 | int did_freeze = 0; |
| 6316 | |
| 6317 | if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) { |
| 6318 | did_freeze = 1; |
| 6319 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 6320 | md_wakeup_thread(mddev->thread); |
| 6321 | } |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6322 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6323 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6324 | if (mddev->sync_thread) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6325 | /* Thread might be blocked waiting for metadata update |
| 6326 | * which will now never happen */ |
| 6327 | wake_up_process(mddev->sync_thread->tsk); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6328 | |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 6329 | if (mddev->external && test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) |
NeilBrown | 88724bf | 2015-09-24 14:00:51 +1000 | [diff] [blame] | 6330 | return -EBUSY; |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6331 | mddev_unlock(mddev); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6332 | wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING, |
| 6333 | &mddev->recovery)); |
NeilBrown | 88724bf | 2015-09-24 14:00:51 +1000 | [diff] [blame] | 6334 | wait_event(mddev->sb_wait, |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 6335 | !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6336 | mddev_lock_nointr(mddev); |
| 6337 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 6338 | mutex_lock(&mddev->open_mutex); |
NeilBrown | 9ba3b7f | 2014-09-09 14:00:15 +1000 | [diff] [blame] | 6339 | if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) || |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6340 | mddev->sync_thread || |
Guoqing Jiang | af8d8e6 | 2016-08-12 13:42:37 +0800 | [diff] [blame] | 6341 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6342 | pr_warn("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6343 | if (did_freeze) { |
| 6344 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 45eaf45 | 2014-10-29 08:49:50 +1100 | [diff] [blame] | 6345 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6346 | md_wakeup_thread(mddev->thread); |
| 6347 | } |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 6348 | err = -EBUSY; |
| 6349 | goto out; |
| 6350 | } |
| 6351 | if (mddev->pers) { |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 6352 | __md_stop_writes(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 6353 | |
| 6354 | err = -ENXIO; |
| 6355 | if (mddev->ro==1) |
| 6356 | goto out; |
| 6357 | mddev->ro = 1; |
| 6358 | set_disk_ro(mddev->gendisk, 1); |
| 6359 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 45eaf45 | 2014-10-29 08:49:50 +1100 | [diff] [blame] | 6360 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6361 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6362 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6363 | err = 0; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 6364 | } |
| 6365 | out: |
| 6366 | mutex_unlock(&mddev->open_mutex); |
| 6367 | return err; |
| 6368 | } |
| 6369 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 6370 | /* mode: |
| 6371 | * 0 - completely stop and dis-assemble array |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 6372 | * 2 - stop but do not disassemble array |
| 6373 | */ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6374 | static int do_md_stop(struct mddev *mddev, int mode, |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 6375 | struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6376 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6377 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6378 | struct md_rdev *rdev; |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6379 | int did_freeze = 0; |
| 6380 | |
| 6381 | if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) { |
| 6382 | did_freeze = 1; |
| 6383 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 6384 | md_wakeup_thread(mddev->thread); |
| 6385 | } |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6386 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6387 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6388 | if (mddev->sync_thread) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6389 | /* Thread might be blocked waiting for metadata update |
| 6390 | * which will now never happen */ |
| 6391 | wake_up_process(mddev->sync_thread->tsk); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6392 | |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6393 | mddev_unlock(mddev); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6394 | wait_event(resync_wait, (mddev->sync_thread == NULL && |
| 6395 | !test_bit(MD_RECOVERY_RUNNING, |
| 6396 | &mddev->recovery))); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6397 | mddev_lock_nointr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6398 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 6399 | mutex_lock(&mddev->open_mutex); |
NeilBrown | 9ba3b7f | 2014-09-09 14:00:15 +1000 | [diff] [blame] | 6400 | if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) || |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6401 | mddev->sysfs_active || |
| 6402 | mddev->sync_thread || |
Guoqing Jiang | af8d8e6 | 2016-08-12 13:42:37 +0800 | [diff] [blame] | 6403 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6404 | pr_warn("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 6405 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6406 | if (did_freeze) { |
| 6407 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 45eaf45 | 2014-10-29 08:49:50 +1100 | [diff] [blame] | 6408 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6409 | md_wakeup_thread(mddev->thread); |
| 6410 | } |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 6411 | return -EBUSY; |
| 6412 | } |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 6413 | if (mddev->pers) { |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 6414 | if (mddev->ro) |
| 6415 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6416 | |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 6417 | __md_stop_writes(mddev); |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 6418 | __md_stop(mddev); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6419 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 6420 | /* tell userspace to handle 'inactive' */ |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6421 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 6422 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 6423 | rdev_for_each(rdev, mddev) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 6424 | if (rdev->raid_disk >= 0) |
| 6425 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 6426 | |
Christoph Hellwig | 2c247c5 | 2020-11-16 15:57:11 +0100 | [diff] [blame] | 6427 | set_capacity_and_notify(disk, 0); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 6428 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6429 | mddev->changed = 1; |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 6430 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 6431 | if (mddev->ro) |
| 6432 | mddev->ro = 0; |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 6433 | } else |
| 6434 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6435 | /* |
| 6436 | * Free resources if final stop |
| 6437 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 6438 | if (mode == 0) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6439 | pr_info("md: %s stopped.\n", mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6440 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6441 | if (mddev->bitmap_info.file) { |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6442 | struct file *f = mddev->bitmap_info.file; |
| 6443 | spin_lock(&mddev->lock); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6444 | mddev->bitmap_info.file = NULL; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6445 | spin_unlock(&mddev->lock); |
| 6446 | fput(f); |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 6447 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6448 | mddev->bitmap_info.offset = 0; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 6449 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6450 | export_array(mddev); |
| 6451 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6452 | md_clean(mddev); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6453 | if (mddev->hold_active == UNTIL_STOP) |
| 6454 | mddev->hold_active = 0; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 6455 | } |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6456 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6457 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 6458 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6459 | } |
| 6460 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 6461 | #ifndef MODULE |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6462 | static void autorun_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6463 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6464 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6465 | int err; |
| 6466 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 6467 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6468 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6469 | |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6470 | pr_info("md: running: "); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6471 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 6472 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6473 | char b[BDEVNAME_SIZE]; |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6474 | pr_cont("<%s>", bdevname(rdev->bdev,b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6475 | } |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6476 | pr_cont("\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6477 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6478 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6479 | if (err) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6480 | pr_warn("md: do_md_run() returned %d\n", err); |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 6481 | do_md_stop(mddev, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6482 | } |
| 6483 | } |
| 6484 | |
| 6485 | /* |
| 6486 | * lets try to run arrays based on all disks that have arrived |
| 6487 | * until now. (those are in pending_raid_disks) |
| 6488 | * |
| 6489 | * the method: pick the first pending disk, collect all disks with |
| 6490 | * the same UUID, remove all from the pending list and put them into |
| 6491 | * the 'same_array' list. Then order this list based on superblock |
| 6492 | * update time (freshest comes first), kick out 'old' disks and |
| 6493 | * compare superblocks. If everything's fine then run it. |
| 6494 | * |
| 6495 | * If "unit" is allocated, then bump its reference count |
| 6496 | */ |
| 6497 | static void autorun_devices(int part) |
| 6498 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6499 | struct md_rdev *rdev0, *rdev, *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6500 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6501 | char b[BDEVNAME_SIZE]; |
| 6502 | |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6503 | pr_info("md: autorun ...\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6504 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 6505 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6506 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 6507 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6508 | rdev0 = list_entry(pending_raid_disks.next, |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6509 | struct md_rdev, same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6510 | |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6511 | pr_debug("md: considering %s ...\n", bdevname(rdev0->bdev,b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6512 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6513 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6514 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6515 | pr_debug("md: adding %s ...\n", |
| 6516 | bdevname(rdev->bdev,b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6517 | list_move(&rdev->same_set, &candidates); |
| 6518 | } |
| 6519 | /* |
| 6520 | * now we have a set of devices, with all of them having |
| 6521 | * mostly sane superblocks. It's time to allocate the |
| 6522 | * mddev. |
| 6523 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 6524 | if (part) { |
| 6525 | dev = MKDEV(mdp_major, |
| 6526 | rdev0->preferred_minor << MdpMinorShift); |
| 6527 | unit = MINOR(dev) >> MdpMinorShift; |
| 6528 | } else { |
| 6529 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 6530 | unit = MINOR(dev); |
| 6531 | } |
| 6532 | if (rdev0->preferred_minor != unit) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6533 | pr_warn("md: unit number in %s is bad: %d\n", |
| 6534 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6535 | break; |
| 6536 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6537 | |
Christoph Hellwig | 28144f9 | 2020-10-29 15:58:34 +0100 | [diff] [blame] | 6538 | md_probe(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6539 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 6540 | if (!mddev || !mddev->gendisk) { |
| 6541 | if (mddev) |
| 6542 | mddev_put(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6543 | break; |
| 6544 | } |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6545 | if (mddev_lock(mddev)) |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6546 | pr_warn("md: %s locked, cannot run\n", mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6547 | else if (mddev->raid_disks || mddev->major_version |
| 6548 | || !list_empty(&mddev->disks)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6549 | pr_warn("md: %s already running, cannot run %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6550 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 6551 | mddev_unlock(mddev); |
| 6552 | } else { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6553 | pr_debug("md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 6554 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6555 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6556 | list_del_init(&rdev->same_set); |
| 6557 | if (bind_rdev_to_array(rdev, mddev)) |
| 6558 | export_rdev(rdev); |
| 6559 | } |
| 6560 | autorun_array(mddev); |
| 6561 | mddev_unlock(mddev); |
| 6562 | } |
| 6563 | /* on success, candidates will be empty, on error |
| 6564 | * it won't... |
| 6565 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6566 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6567 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6568 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6569 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6570 | mddev_put(mddev); |
| 6571 | } |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6572 | pr_info("md: ... autorun DONE.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6573 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 6574 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6575 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6576 | static int get_version(void __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6577 | { |
| 6578 | mdu_version_t ver; |
| 6579 | |
| 6580 | ver.major = MD_MAJOR_VERSION; |
| 6581 | ver.minor = MD_MINOR_VERSION; |
| 6582 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 6583 | |
| 6584 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 6585 | return -EFAULT; |
| 6586 | |
| 6587 | return 0; |
| 6588 | } |
| 6589 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6590 | static int get_array_info(struct mddev *mddev, void __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6591 | { |
| 6592 | mdu_array_info_t info; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 6593 | int nr,working,insync,failed,spare; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6594 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6595 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6596 | nr = working = insync = failed = spare = 0; |
| 6597 | rcu_read_lock(); |
| 6598 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6599 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6600 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6601 | failed++; |
| 6602 | else { |
| 6603 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6604 | if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6605 | insync++; |
Song Liu | b347af8 | 2016-08-11 17:14:45 -0700 | [diff] [blame] | 6606 | else if (test_bit(Journal, &rdev->flags)) |
| 6607 | /* TODO: add journal count to md_u.h */ |
| 6608 | ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6609 | else |
| 6610 | spare++; |
| 6611 | } |
| 6612 | } |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6613 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6614 | |
| 6615 | info.major_version = mddev->major_version; |
| 6616 | info.minor_version = mddev->minor_version; |
| 6617 | info.patch_version = MD_PATCHLEVEL_VERSION; |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 6618 | info.ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6619 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6620 | info.size = mddev->dev_sectors / 2; |
| 6621 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 6622 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6623 | info.nr_disks = nr; |
| 6624 | info.raid_disks = mddev->raid_disks; |
| 6625 | info.md_minor = mddev->md_minor; |
| 6626 | info.not_persistent= !mddev->persistent; |
| 6627 | |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 6628 | info.utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6629 | info.state = 0; |
| 6630 | if (mddev->in_sync) |
| 6631 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6632 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 9bd3592 | 2014-07-02 11:35:06 +1000 | [diff] [blame] | 6633 | info.state |= (1<<MD_SB_BITMAP_PRESENT); |
Goldwyn Rodrigues | ca8895d | 2014-11-26 12:22:03 -0600 | [diff] [blame] | 6634 | if (mddev_is_clustered(mddev)) |
| 6635 | info.state |= (1<<MD_SB_CLUSTERED); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 6636 | info.active_disks = insync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6637 | info.working_disks = working; |
| 6638 | info.failed_disks = failed; |
| 6639 | info.spare_disks = spare; |
| 6640 | |
| 6641 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 6642 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6643 | |
| 6644 | if (copy_to_user(arg, &info, sizeof(info))) |
| 6645 | return -EFAULT; |
| 6646 | |
| 6647 | return 0; |
| 6648 | } |
| 6649 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6650 | static int get_bitmap_file(struct mddev *mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6651 | { |
| 6652 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
NeilBrown | f4ad3d3 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6653 | char *ptr; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6654 | int err; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6655 | |
Benjamin Randazzo | b6878d9 | 2015-07-25 16:36:50 +0200 | [diff] [blame] | 6656 | file = kzalloc(sizeof(*file), GFP_NOIO); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6657 | if (!file) |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6658 | return -ENOMEM; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6659 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6660 | err = 0; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6661 | spin_lock(&mddev->lock); |
Benjamin Randazzo | 25eafe1 | 2015-07-25 16:36:50 +0200 | [diff] [blame] | 6662 | /* bitmap enabled */ |
| 6663 | if (mddev->bitmap_info.file) { |
| 6664 | ptr = file_path(mddev->bitmap_info.file, file->pathname, |
| 6665 | sizeof(file->pathname)); |
| 6666 | if (IS_ERR(ptr)) |
| 6667 | err = PTR_ERR(ptr); |
| 6668 | else |
| 6669 | memmove(file->pathname, ptr, |
| 6670 | sizeof(file->pathname)-(ptr-file->pathname)); |
| 6671 | } |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6672 | spin_unlock(&mddev->lock); |
| 6673 | |
| 6674 | if (err == 0 && |
| 6675 | copy_to_user(arg, file, sizeof(*file))) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6676 | err = -EFAULT; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6677 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6678 | kfree(file); |
| 6679 | return err; |
| 6680 | } |
| 6681 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6682 | static int get_disk_info(struct mddev *mddev, void __user * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6683 | { |
| 6684 | mdu_disk_info_t info; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6685 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6686 | |
| 6687 | if (copy_from_user(&info, arg, sizeof(info))) |
| 6688 | return -EFAULT; |
| 6689 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6690 | rcu_read_lock(); |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 6691 | rdev = md_find_rdev_nr_rcu(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6692 | if (rdev) { |
| 6693 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 6694 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 6695 | info.raid_disk = rdev->raid_disk; |
| 6696 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6697 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6698 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6699 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6700 | info.state |= (1<<MD_DISK_ACTIVE); |
| 6701 | info.state |= (1<<MD_DISK_SYNC); |
| 6702 | } |
Shaohua Li | 9efdca1 | 2015-10-12 16:59:50 -0700 | [diff] [blame] | 6703 | if (test_bit(Journal, &rdev->flags)) |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 6704 | info.state |= (1<<MD_DISK_JOURNAL); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6705 | if (test_bit(WriteMostly, &rdev->flags)) |
| 6706 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 6707 | if (test_bit(FailFast, &rdev->flags)) |
| 6708 | info.state |= (1<<MD_DISK_FAILFAST); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6709 | } else { |
| 6710 | info.major = info.minor = 0; |
| 6711 | info.raid_disk = -1; |
| 6712 | info.state = (1<<MD_DISK_REMOVED); |
| 6713 | } |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6714 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6715 | |
| 6716 | if (copy_to_user(arg, &info, sizeof(info))) |
| 6717 | return -EFAULT; |
| 6718 | |
| 6719 | return 0; |
| 6720 | } |
| 6721 | |
Christoph Hellwig | 7e0adbf | 2020-06-07 17:31:19 +0200 | [diff] [blame] | 6722 | int md_add_new_disk(struct mddev *mddev, struct mdu_disk_info_s *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6723 | { |
| 6724 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6725 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6726 | dev_t dev = MKDEV(info->major,info->minor); |
| 6727 | |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6728 | if (mddev_is_clustered(mddev) && |
| 6729 | !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6730 | pr_warn("%s: Cannot add to clustered mddev.\n", |
| 6731 | mdname(mddev)); |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6732 | return -EINVAL; |
| 6733 | } |
| 6734 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6735 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 6736 | return -EOVERFLOW; |
| 6737 | |
| 6738 | if (!mddev->raid_disks) { |
| 6739 | int err; |
| 6740 | /* expecting a device which has a superblock */ |
| 6741 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 6742 | if (IS_ERR(rdev)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6743 | pr_warn("md: md_import_device returned %ld\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6744 | PTR_ERR(rdev)); |
| 6745 | return PTR_ERR(rdev); |
| 6746 | } |
| 6747 | if (!list_empty(&mddev->disks)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6748 | struct md_rdev *rdev0 |
| 6749 | = list_entry(mddev->disks.next, |
| 6750 | struct md_rdev, same_set); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 6751 | err = super_types[mddev->major_version] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6752 | .load_super(rdev, rdev0, mddev->minor_version); |
| 6753 | if (err < 0) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6754 | pr_warn("md: %s has different UUID to %s\n", |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6755 | bdevname(rdev->bdev,b), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6756 | bdevname(rdev0->bdev,b2)); |
| 6757 | export_rdev(rdev); |
| 6758 | return -EINVAL; |
| 6759 | } |
| 6760 | } |
| 6761 | err = bind_rdev_to_array(rdev, mddev); |
| 6762 | if (err) |
| 6763 | export_rdev(rdev); |
| 6764 | return err; |
| 6765 | } |
| 6766 | |
| 6767 | /* |
Christoph Hellwig | 7e0adbf | 2020-06-07 17:31:19 +0200 | [diff] [blame] | 6768 | * md_add_new_disk can be used once the array is assembled |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6769 | * to add "hot spares". They must already have a superblock |
| 6770 | * written |
| 6771 | */ |
| 6772 | if (mddev->pers) { |
| 6773 | int err; |
| 6774 | if (!mddev->pers->hot_add_disk) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6775 | pr_warn("%s: personality does not support diskops!\n", |
| 6776 | mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6777 | return -EINVAL; |
| 6778 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 6779 | if (mddev->persistent) |
| 6780 | rdev = md_import_device(dev, mddev->major_version, |
| 6781 | mddev->minor_version); |
| 6782 | else |
| 6783 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6784 | if (IS_ERR(rdev)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6785 | pr_warn("md: md_import_device returned %ld\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6786 | PTR_ERR(rdev)); |
| 6787 | return PTR_ERR(rdev); |
| 6788 | } |
NeilBrown | 1a855a0 | 2010-12-09 16:36:28 +1100 | [diff] [blame] | 6789 | /* set saved_raid_disk if appropriate */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6790 | if (!mddev->persistent) { |
| 6791 | if (info->state & (1<<MD_DISK_SYNC) && |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 6792 | info->raid_disk < mddev->raid_disks) { |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6793 | rdev->raid_disk = info->raid_disk; |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 6794 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 6795 | clear_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 6796 | } else |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6797 | rdev->raid_disk = -1; |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 6798 | rdev->saved_raid_disk = rdev->raid_disk; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6799 | } else |
| 6800 | super_types[mddev->major_version]. |
| 6801 | validate_super(mddev, rdev); |
NeilBrown | bedd86b | 2011-05-11 14:26:20 +1000 | [diff] [blame] | 6802 | if ((info->state & (1<<MD_DISK_SYNC)) && |
NeilBrown | f456309 | 2012-07-03 15:59:06 +1000 | [diff] [blame] | 6803 | rdev->raid_disk != info->raid_disk) { |
NeilBrown | bedd86b | 2011-05-11 14:26:20 +1000 | [diff] [blame] | 6804 | /* This was a hot-add request, but events doesn't |
| 6805 | * match, so reject it. |
| 6806 | */ |
| 6807 | export_rdev(rdev); |
| 6808 | return -EINVAL; |
| 6809 | } |
| 6810 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6811 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6812 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 6813 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6814 | else |
| 6815 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 6816 | if (info->state & (1<<MD_DISK_FAILFAST)) |
| 6817 | set_bit(FailFast, &rdev->flags); |
| 6818 | else |
| 6819 | clear_bit(FailFast, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6820 | |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 6821 | if (info->state & (1<<MD_DISK_JOURNAL)) { |
| 6822 | struct md_rdev *rdev2; |
| 6823 | bool has_journal = false; |
| 6824 | |
| 6825 | /* make sure no existing journal disk */ |
| 6826 | rdev_for_each(rdev2, mddev) { |
| 6827 | if (test_bit(Journal, &rdev2->flags)) { |
| 6828 | has_journal = true; |
| 6829 | break; |
| 6830 | } |
| 6831 | } |
NeilBrown | 230b55f | 2017-10-17 14:24:09 +1100 | [diff] [blame] | 6832 | if (has_journal || mddev->bitmap) { |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 6833 | export_rdev(rdev); |
| 6834 | return -EBUSY; |
| 6835 | } |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 6836 | set_bit(Journal, &rdev->flags); |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 6837 | } |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6838 | /* |
| 6839 | * check whether the device shows up in other nodes |
| 6840 | */ |
| 6841 | if (mddev_is_clustered(mddev)) { |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6842 | if (info->state & (1 << MD_DISK_CANDIDATE)) |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6843 | set_bit(Candidate, &rdev->flags); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6844 | else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) { |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6845 | /* --add initiated by this node */ |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6846 | err = md_cluster_ops->add_new_disk(mddev, rdev); |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6847 | if (err) { |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6848 | export_rdev(rdev); |
| 6849 | return err; |
| 6850 | } |
| 6851 | } |
| 6852 | } |
| 6853 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6854 | rdev->raid_disk = -1; |
| 6855 | err = bind_rdev_to_array(rdev, mddev); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6856 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6857 | if (err) |
| 6858 | export_rdev(rdev); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6859 | |
| 6860 | if (mddev_is_clustered(mddev)) { |
Guoqing Jiang | e566aef | 2016-08-12 13:42:34 +0800 | [diff] [blame] | 6861 | if (info->state & (1 << MD_DISK_CANDIDATE)) { |
| 6862 | if (!err) { |
| 6863 | err = md_cluster_ops->new_disk_ack(mddev, |
| 6864 | err == 0); |
| 6865 | if (err) |
| 6866 | md_kick_rdev_from_array(rdev); |
| 6867 | } |
| 6868 | } else { |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6869 | if (err) |
| 6870 | md_cluster_ops->add_new_disk_cancel(mddev); |
| 6871 | else |
| 6872 | err = add_bound_rdev(rdev); |
| 6873 | } |
| 6874 | |
| 6875 | } else if (!err) |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 6876 | err = add_bound_rdev(rdev); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6877 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6878 | return err; |
| 6879 | } |
| 6880 | |
Christoph Hellwig | 7e0adbf | 2020-06-07 17:31:19 +0200 | [diff] [blame] | 6881 | /* otherwise, md_add_new_disk is only allowed |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6882 | * for major_version==0 superblocks |
| 6883 | */ |
| 6884 | if (mddev->major_version != 0) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6885 | pr_warn("%s: ADD_NEW_DISK not supported\n", mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6886 | return -EINVAL; |
| 6887 | } |
| 6888 | |
| 6889 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 6890 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6891 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6892 | if (IS_ERR(rdev)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6893 | pr_warn("md: error, md_import_device() returned %ld\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6894 | PTR_ERR(rdev)); |
| 6895 | return PTR_ERR(rdev); |
| 6896 | } |
| 6897 | rdev->desc_nr = info->number; |
| 6898 | if (info->raid_disk < mddev->raid_disks) |
| 6899 | rdev->raid_disk = info->raid_disk; |
| 6900 | else |
| 6901 | rdev->raid_disk = -1; |
| 6902 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6903 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6904 | if (info->state & (1<<MD_DISK_SYNC)) |
| 6905 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6906 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6907 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 6908 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 6909 | if (info->state & (1<<MD_DISK_FAILFAST)) |
| 6910 | set_bit(FailFast, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6911 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6912 | if (!mddev->persistent) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6913 | pr_debug("md: nonpersistent superblock ...\n"); |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 6914 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
| 6915 | } else |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 6916 | rdev->sb_start = calc_dev_sboffset(rdev); |
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 | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 6919 | err = bind_rdev_to_array(rdev, mddev); |
| 6920 | if (err) { |
| 6921 | export_rdev(rdev); |
| 6922 | return err; |
| 6923 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6924 | } |
| 6925 | |
| 6926 | return 0; |
| 6927 | } |
| 6928 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6929 | static int hot_remove_disk(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6930 | { |
| 6931 | char b[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6932 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6933 | |
Yufen Yu | c42a0e2 | 2018-05-04 18:08:10 +0800 | [diff] [blame] | 6934 | if (!mddev->pers) |
| 6935 | return -ENODEV; |
| 6936 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6937 | rdev = find_rdev(mddev, dev); |
| 6938 | if (!rdev) |
| 6939 | return -ENXIO; |
| 6940 | |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 6941 | if (rdev->raid_disk < 0) |
| 6942 | goto kick_rdev; |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6943 | |
NeilBrown | 3ea8929d | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 6944 | clear_bit(Blocked, &rdev->flags); |
| 6945 | remove_and_add_spares(mddev, rdev); |
| 6946 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6947 | if (rdev->raid_disk >= 0) |
| 6948 | goto busy; |
| 6949 | |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 6950 | kick_rdev: |
Zhao Heming | bca5b06 | 2020-11-19 19:41:34 +0800 | [diff] [blame] | 6951 | if (mddev_is_clustered(mddev)) { |
| 6952 | if (md_cluster_ops->remove_disk(mddev, rdev)) |
| 6953 | goto busy; |
| 6954 | } |
Goldwyn Rodrigues | 88bcfef | 2015-04-14 10:44:44 -0500 | [diff] [blame] | 6955 | |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 6956 | md_kick_rdev_from_array(rdev); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 6957 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | 060b068 | 2016-11-04 16:46:03 +1100 | [diff] [blame] | 6958 | if (mddev->thread) |
| 6959 | md_wakeup_thread(mddev->thread); |
| 6960 | else |
| 6961 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6962 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6963 | |
| 6964 | return 0; |
| 6965 | busy: |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6966 | pr_debug("md: cannot remove active disk %s from %s ...\n", |
| 6967 | bdevname(rdev->bdev,b), mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6968 | return -EBUSY; |
| 6969 | } |
| 6970 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6971 | static int hot_add_disk(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6972 | { |
| 6973 | char b[BDEVNAME_SIZE]; |
| 6974 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6975 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6976 | |
| 6977 | if (!mddev->pers) |
| 6978 | return -ENODEV; |
| 6979 | |
| 6980 | if (mddev->major_version != 0) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6981 | 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] | 6982 | mdname(mddev)); |
| 6983 | return -EINVAL; |
| 6984 | } |
| 6985 | if (!mddev->pers->hot_add_disk) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6986 | pr_warn("%s: personality does not support diskops!\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6987 | mdname(mddev)); |
| 6988 | return -EINVAL; |
| 6989 | } |
| 6990 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6991 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6992 | if (IS_ERR(rdev)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6993 | pr_warn("md: error, md_import_device() returned %ld\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6994 | PTR_ERR(rdev)); |
| 6995 | return -EINVAL; |
| 6996 | } |
| 6997 | |
| 6998 | if (mddev->persistent) |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 6999 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7000 | else |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 7001 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7002 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 7003 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7004 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7005 | if (test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7006 | 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] | 7007 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 7008 | err = -EINVAL; |
| 7009 | goto abort_export; |
| 7010 | } |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 7011 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7012 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7013 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 7014 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 7015 | err = bind_rdev_to_array(rdev, mddev); |
| 7016 | if (err) |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 7017 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7018 | |
| 7019 | /* |
| 7020 | * The rest should better be atomic, we can have disk failures |
| 7021 | * noticed in interrupt contexts ... |
| 7022 | */ |
| 7023 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7024 | rdev->raid_disk = -1; |
| 7025 | |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 7026 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | 060b068 | 2016-11-04 16:46:03 +1100 | [diff] [blame] | 7027 | if (!mddev->thread) |
| 7028 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7029 | /* |
| 7030 | * Kick recovery, maybe this spare has to be added to the |
| 7031 | * array immediately. |
| 7032 | */ |
| 7033 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7034 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7035 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7036 | return 0; |
| 7037 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7038 | abort_export: |
| 7039 | export_rdev(rdev); |
| 7040 | return err; |
| 7041 | } |
| 7042 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7043 | static int set_bitmap_file(struct mddev *mddev, int fd) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7044 | { |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 7045 | int err = 0; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7046 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7047 | if (mddev->pers) { |
NeilBrown | d66b1b3 | 2014-08-08 15:40:24 +1000 | [diff] [blame] | 7048 | if (!mddev->pers->quiesce || !mddev->thread) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7049 | return -EBUSY; |
| 7050 | if (mddev->recovery || mddev->sync_thread) |
| 7051 | return -EBUSY; |
| 7052 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7053 | } |
| 7054 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7055 | if (fd >= 0) { |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 7056 | struct inode *inode; |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 7057 | struct file *f; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7058 | |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 7059 | if (mddev->bitmap || mddev->bitmap_info.file) |
| 7060 | return -EEXIST; /* cannot add when bitmap is present */ |
| 7061 | f = fget(fd); |
| 7062 | |
| 7063 | if (f == NULL) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7064 | pr_warn("%s: error: failed to get bitmap file\n", |
| 7065 | mdname(mddev)); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7066 | return -EBADF; |
| 7067 | } |
| 7068 | |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 7069 | inode = f->f_mapping->host; |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 7070 | if (!S_ISREG(inode->i_mode)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7071 | pr_warn("%s: error: bitmap file must be a regular file\n", |
| 7072 | mdname(mddev)); |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 7073 | err = -EBADF; |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 7074 | } else if (!(f->f_mode & FMODE_WRITE)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7075 | pr_warn("%s: error: bitmap file must open for write\n", |
| 7076 | mdname(mddev)); |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 7077 | err = -EBADF; |
| 7078 | } else if (atomic_read(&inode->i_writecount) != 1) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7079 | pr_warn("%s: error: bitmap file is already in use\n", |
| 7080 | mdname(mddev)); |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 7081 | err = -EBUSY; |
| 7082 | } |
| 7083 | if (err) { |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 7084 | fput(f); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7085 | return err; |
| 7086 | } |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 7087 | mddev->bitmap_info.file = f; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 7088 | mddev->bitmap_info.offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7089 | } else if (mddev->bitmap == NULL) |
| 7090 | return -ENOENT; /* cannot remove what isn't there */ |
| 7091 | err = 0; |
| 7092 | if (mddev->pers) { |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 7093 | if (fd >= 0) { |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 7094 | struct bitmap *bitmap; |
| 7095 | |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 7096 | bitmap = md_bitmap_create(mddev, -1); |
NeilBrown | 9e1cc0a | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 7097 | mddev_suspend(mddev); |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 7098 | if (!IS_ERR(bitmap)) { |
| 7099 | mddev->bitmap = bitmap; |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 7100 | err = md_bitmap_load(mddev); |
NeilBrown | ba599ac | 2015-02-25 11:44:11 +1100 | [diff] [blame] | 7101 | } else |
| 7102 | err = PTR_ERR(bitmap); |
NeilBrown | 52a0d49 | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 7103 | if (err) { |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 7104 | md_bitmap_destroy(mddev); |
NeilBrown | 52a0d49 | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 7105 | fd = -1; |
| 7106 | } |
NeilBrown | 9e1cc0a | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 7107 | mddev_resume(mddev); |
NeilBrown | 52a0d49 | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 7108 | } else if (fd < 0) { |
NeilBrown | 9e1cc0a | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 7109 | mddev_suspend(mddev); |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 7110 | md_bitmap_destroy(mddev); |
NeilBrown | 9e1cc0a | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 7111 | mddev_resume(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 7112 | } |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 7113 | } |
| 7114 | if (fd < 0) { |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 7115 | struct file *f = mddev->bitmap_info.file; |
| 7116 | if (f) { |
| 7117 | spin_lock(&mddev->lock); |
| 7118 | mddev->bitmap_info.file = NULL; |
| 7119 | spin_unlock(&mddev->lock); |
| 7120 | fput(f); |
| 7121 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7122 | } |
| 7123 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7124 | return err; |
| 7125 | } |
| 7126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7127 | /* |
Christoph Hellwig | 7e0adbf | 2020-06-07 17:31:19 +0200 | [diff] [blame] | 7128 | * md_set_array_info is used two different ways |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7129 | * The original usage is when creating a new array. |
| 7130 | * In this usage, raid_disks is > 0 and it together with |
| 7131 | * level, size, not_persistent,layout,chunksize determine the |
| 7132 | * shape of the array. |
| 7133 | * This will always create an array with a type-0.90.0 superblock. |
| 7134 | * The newer usage is when assembling an array. |
| 7135 | * In this case raid_disks will be 0, and the major_version field is |
| 7136 | * use to determine which style super-blocks are to be found on the devices. |
| 7137 | * The minor and patch _version numbers are also kept incase the |
| 7138 | * super_block handler wishes to interpret them. |
| 7139 | */ |
Christoph Hellwig | 7e0adbf | 2020-06-07 17:31:19 +0200 | [diff] [blame] | 7140 | int md_set_array_info(struct mddev *mddev, struct mdu_array_info_s *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7141 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7142 | if (info->raid_disks == 0) { |
| 7143 | /* just setting version number for superblock loading */ |
| 7144 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 7145 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7146 | super_types[info->major_version].name == NULL) { |
| 7147 | /* maybe try to auto-load a module? */ |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7148 | pr_warn("md: superblock version %d not known\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7149 | info->major_version); |
| 7150 | return -EINVAL; |
| 7151 | } |
| 7152 | mddev->major_version = info->major_version; |
| 7153 | mddev->minor_version = info->minor_version; |
| 7154 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 7155 | mddev->persistent = !info->not_persistent; |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 7156 | /* ensure mddev_put doesn't delete this now that there |
| 7157 | * is some minimal configuration. |
| 7158 | */ |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 7159 | mddev->ctime = ktime_get_real_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7160 | return 0; |
| 7161 | } |
| 7162 | mddev->major_version = MD_MAJOR_VERSION; |
| 7163 | mddev->minor_version = MD_MINOR_VERSION; |
| 7164 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 7165 | mddev->ctime = ktime_get_real_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7166 | |
| 7167 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 7168 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7169 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7170 | mddev->raid_disks = info->raid_disks; |
| 7171 | /* don't set md_minor, it is determined by which /dev/md* was |
| 7172 | * openned |
| 7173 | */ |
| 7174 | if (info->state & (1<<MD_SB_CLEAN)) |
| 7175 | mddev->recovery_cp = MaxSector; |
| 7176 | else |
| 7177 | mddev->recovery_cp = 0; |
| 7178 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 7179 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7180 | |
| 7181 | mddev->layout = info->layout; |
NeilBrown | 33f2c35 | 2019-09-09 16:52:29 +1000 | [diff] [blame] | 7182 | if (mddev->level == 0) |
| 7183 | /* Cannot trust RAID0 layout info here */ |
| 7184 | mddev->layout = -1; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 7185 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7186 | |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 7187 | if (mddev->persistent) { |
NeilBrown | 1b3bae4 | 2017-03-01 07:31:28 +1100 | [diff] [blame] | 7188 | mddev->max_disks = MD_SB_DISKS; |
| 7189 | mddev->flags = 0; |
| 7190 | mddev->sb_flags = 0; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 7191 | } |
| 7192 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7193 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 7194 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 7195 | mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 7196 | mddev->bitmap_info.offset = 0; |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 7197 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7198 | mddev->reshape_position = MaxSector; |
| 7199 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7200 | /* |
| 7201 | * Generate a 128 bit UUID |
| 7202 | */ |
| 7203 | get_random_bytes(mddev->uuid, 16); |
| 7204 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7205 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 7206 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7207 | mddev->new_layout = mddev->layout; |
| 7208 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7209 | mddev->reshape_backwards = 0; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7210 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7211 | return 0; |
| 7212 | } |
| 7213 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7214 | void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors) |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 7215 | { |
Shaohua Li | efa4b77 | 2017-10-18 22:08:13 -0700 | [diff] [blame] | 7216 | lockdep_assert_held(&mddev->reconfig_mutex); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 7217 | |
| 7218 | if (mddev->external_size) |
| 7219 | return; |
| 7220 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 7221 | mddev->array_sectors = array_sectors; |
| 7222 | } |
| 7223 | EXPORT_SYMBOL(md_set_array_sectors); |
| 7224 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7225 | static int update_size(struct mddev *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 7226 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7227 | struct md_rdev *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 7228 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 7229 | int fit = (num_sectors == 0); |
Guoqing Jiang | 818da59 | 2017-03-01 16:42:40 +0800 | [diff] [blame] | 7230 | sector_t old_dev_sectors = mddev->dev_sectors; |
Guoqing Jiang | ab5a98b | 2016-05-02 11:33:13 -0400 | [diff] [blame] | 7231 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 7232 | if (mddev->pers->resize == NULL) |
| 7233 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 7234 | /* The "num_sectors" is the number of sectors of each device that |
| 7235 | * is used. This can only make sense for arrays with redundancy. |
| 7236 | * linear and raid0 always use whatever space is available. We can only |
| 7237 | * consider changing this number if no resync or reconstruction is |
| 7238 | * 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] | 7239 | * 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] | 7240 | * of each device. If num_sectors is zero, we find the largest size |
| 7241 | * that fits. |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 7242 | */ |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 7243 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 7244 | mddev->sync_thread) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 7245 | return -EBUSY; |
NeilBrown | bd8839e | 2014-05-28 13:39:21 +1000 | [diff] [blame] | 7246 | if (mddev->ro) |
| 7247 | return -EROFS; |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 7248 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7249 | rdev_for_each(rdev, mddev) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7250 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 7251 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 7252 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 7253 | num_sectors = avail; |
| 7254 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 7255 | return -ENOSPC; |
| 7256 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 7257 | rv = mddev->pers->resize(mddev, num_sectors); |
Guoqing Jiang | c948363 | 2017-02-24 11:15:23 +0800 | [diff] [blame] | 7258 | if (!rv) { |
Guoqing Jiang | 818da59 | 2017-03-01 16:42:40 +0800 | [diff] [blame] | 7259 | if (mddev_is_clustered(mddev)) |
| 7260 | md_cluster_ops->update_size(mddev, old_dev_sectors); |
| 7261 | else if (mddev->queue) { |
Christoph Hellwig | 2c247c5 | 2020-11-16 15:57:11 +0100 | [diff] [blame] | 7262 | set_capacity_and_notify(mddev->gendisk, |
| 7263 | mddev->array_sectors); |
Guoqing Jiang | c948363 | 2017-02-24 11:15:23 +0800 | [diff] [blame] | 7264 | } |
| 7265 | } |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 7266 | return rv; |
| 7267 | } |
| 7268 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7269 | static int update_raid_disks(struct mddev *mddev, int raid_disks) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 7270 | { |
| 7271 | int rv; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7272 | struct md_rdev *rdev; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 7273 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 7274 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 7275 | return -EINVAL; |
NeilBrown | bd8839e | 2014-05-28 13:39:21 +1000 | [diff] [blame] | 7276 | if (mddev->ro) |
| 7277 | return -EROFS; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 7278 | if (raid_disks <= 0 || |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 7279 | (mddev->max_disks && raid_disks >= mddev->max_disks)) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 7280 | return -EINVAL; |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 7281 | if (mddev->sync_thread || |
| 7282 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
Zhao Heming | a8da01f | 2020-11-19 19:41:33 +0800 | [diff] [blame] | 7283 | test_bit(MD_RESYNCING_REMOTE, &mddev->recovery) || |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 7284 | mddev->reshape_position != MaxSector) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 7285 | return -EBUSY; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7286 | |
| 7287 | rdev_for_each(rdev, mddev) { |
| 7288 | if (mddev->raid_disks < raid_disks && |
| 7289 | rdev->data_offset < rdev->new_data_offset) |
| 7290 | return -EINVAL; |
| 7291 | if (mddev->raid_disks > raid_disks && |
| 7292 | rdev->data_offset > rdev->new_data_offset) |
| 7293 | return -EINVAL; |
| 7294 | } |
| 7295 | |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 7296 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7297 | if (mddev->delta_disks < 0) |
| 7298 | mddev->reshape_backwards = 1; |
| 7299 | else if (mddev->delta_disks > 0) |
| 7300 | mddev->reshape_backwards = 0; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 7301 | |
| 7302 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7303 | if (rv < 0) { |
NeilBrown | de171cb | 2011-01-31 11:57:42 +1100 | [diff] [blame] | 7304 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7305 | mddev->reshape_backwards = 0; |
| 7306 | } |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 7307 | return rv; |
| 7308 | } |
| 7309 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7310 | /* |
| 7311 | * update_array_info is used to change the configuration of an |
| 7312 | * on-line array. |
| 7313 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 7314 | * fields in the info are checked against the array. |
| 7315 | * Any differences that cannot be handled will cause an error. |
| 7316 | * Normally, only one change can be managed at a time. |
| 7317 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7318 | 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] | 7319 | { |
| 7320 | int rv = 0; |
| 7321 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7322 | int state = 0; |
| 7323 | |
| 7324 | /* calculate expected state,ignoring low bits */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 7325 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7326 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7327 | |
| 7328 | if (mddev->major_version != info->major_version || |
| 7329 | mddev->minor_version != info->minor_version || |
| 7330 | /* mddev->patch_version != info->patch_version || */ |
| 7331 | mddev->ctime != info->ctime || |
| 7332 | mddev->level != info->level || |
| 7333 | /* mddev->layout != info->layout || */ |
Firo Yang | 4e02361 | 2015-06-11 09:41:10 +0800 | [diff] [blame] | 7334 | mddev->persistent != !info->not_persistent || |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 7335 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7336 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 7337 | ((state^info->state) & 0xfffffe00) |
| 7338 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7339 | return -EINVAL; |
| 7340 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7341 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 7342 | cnt++; |
| 7343 | if (mddev->raid_disks != info->raid_disks) |
| 7344 | cnt++; |
| 7345 | if (mddev->layout != info->layout) |
| 7346 | cnt++; |
| 7347 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 7348 | cnt++; |
| 7349 | if (cnt == 0) |
| 7350 | return 0; |
| 7351 | if (cnt > 1) |
| 7352 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7353 | |
| 7354 | if (mddev->layout != info->layout) { |
| 7355 | /* Change layout |
| 7356 | * we don't need to do anything at the md level, the |
| 7357 | * personality will take care of it all. |
| 7358 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 7359 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7360 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 7361 | else { |
| 7362 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 7363 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 7364 | if (rv) |
| 7365 | mddev->new_layout = mddev->layout; |
| 7366 | return rv; |
| 7367 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7368 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7369 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 7370 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7371 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 7372 | if (mddev->raid_disks != info->raid_disks) |
| 7373 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7374 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7375 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 7376 | if (mddev->pers->quiesce == NULL || mddev->thread == NULL) { |
| 7377 | rv = -EINVAL; |
| 7378 | goto err; |
| 7379 | } |
| 7380 | if (mddev->recovery || mddev->sync_thread) { |
| 7381 | rv = -EBUSY; |
| 7382 | goto err; |
| 7383 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7384 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 7385 | struct bitmap *bitmap; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7386 | /* add the bitmap */ |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 7387 | if (mddev->bitmap) { |
| 7388 | rv = -EEXIST; |
| 7389 | goto err; |
| 7390 | } |
| 7391 | if (mddev->bitmap_info.default_offset == 0) { |
| 7392 | rv = -EINVAL; |
| 7393 | goto err; |
| 7394 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 7395 | mddev->bitmap_info.offset = |
| 7396 | mddev->bitmap_info.default_offset; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 7397 | mddev->bitmap_info.space = |
| 7398 | mddev->bitmap_info.default_space; |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 7399 | bitmap = md_bitmap_create(mddev, -1); |
NeilBrown | 9e1cc0a | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 7400 | mddev_suspend(mddev); |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 7401 | if (!IS_ERR(bitmap)) { |
| 7402 | mddev->bitmap = bitmap; |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 7403 | rv = md_bitmap_load(mddev); |
NeilBrown | ba599ac | 2015-02-25 11:44:11 +1100 | [diff] [blame] | 7404 | } else |
| 7405 | rv = PTR_ERR(bitmap); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7406 | if (rv) |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 7407 | md_bitmap_destroy(mddev); |
NeilBrown | 9e1cc0a | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 7408 | mddev_resume(mddev); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7409 | } else { |
| 7410 | /* remove the bitmap */ |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 7411 | if (!mddev->bitmap) { |
| 7412 | rv = -ENOENT; |
| 7413 | goto err; |
| 7414 | } |
| 7415 | if (mddev->bitmap->storage.file) { |
| 7416 | rv = -EINVAL; |
| 7417 | goto err; |
| 7418 | } |
Guoqing Jiang | f6a2dc6 | 2015-12-21 10:51:00 +1100 | [diff] [blame] | 7419 | if (mddev->bitmap_info.nodes) { |
| 7420 | /* hold PW on all the bitmap lock */ |
| 7421 | if (md_cluster_ops->lock_all_bitmaps(mddev) <= 0) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7422 | 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] | 7423 | rv = -EPERM; |
| 7424 | md_cluster_ops->unlock_all_bitmaps(mddev); |
| 7425 | goto err; |
| 7426 | } |
| 7427 | |
| 7428 | mddev->bitmap_info.nodes = 0; |
| 7429 | md_cluster_ops->leave(mddev); |
Zhao Heming | edee9df | 2020-07-21 02:08:53 +0800 | [diff] [blame] | 7430 | module_put(md_cluster_mod); |
Zhao Heming | 7c9d5c5 | 2020-07-21 02:08:52 +0800 | [diff] [blame] | 7431 | mddev->safemode_delay = DEFAULT_SAFEMODE_DELAY; |
Guoqing Jiang | f6a2dc6 | 2015-12-21 10:51:00 +1100 | [diff] [blame] | 7432 | } |
NeilBrown | 9e1cc0a | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 7433 | mddev_suspend(mddev); |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 7434 | md_bitmap_destroy(mddev); |
NeilBrown | 9e1cc0a | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 7435 | mddev_resume(mddev); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 7436 | mddev->bitmap_info.offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7437 | } |
| 7438 | } |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7439 | md_update_sb(mddev, 1); |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 7440 | return rv; |
| 7441 | err: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7442 | return rv; |
| 7443 | } |
| 7444 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7445 | static int set_disk_faulty(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7446 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7447 | struct md_rdev *rdev; |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 7448 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7449 | |
| 7450 | if (mddev->pers == NULL) |
| 7451 | return -ENODEV; |
| 7452 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 7453 | rcu_read_lock(); |
Tomasz Majchrzak | 1532d9e | 2017-12-27 10:31:40 +0100 | [diff] [blame] | 7454 | rdev = md_find_rdev_rcu(mddev, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7455 | if (!rdev) |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 7456 | err = -ENODEV; |
| 7457 | else { |
| 7458 | md_error(mddev, rdev); |
| 7459 | if (!test_bit(Faulty, &rdev->flags)) |
| 7460 | err = -EBUSY; |
| 7461 | } |
| 7462 | rcu_read_unlock(); |
| 7463 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7464 | } |
| 7465 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 7466 | /* |
| 7467 | * We have a problem here : there is no easy way to give a CHS |
| 7468 | * virtual geometry. We currently pretend that we have a 2 heads |
| 7469 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 7470 | * dosfs just mad... ;-) |
| 7471 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 7472 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 7473 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7474 | struct mddev *mddev = bdev->bd_disk->private_data; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 7475 | |
| 7476 | geo->heads = 2; |
| 7477 | geo->sectors = 4; |
NeilBrown | 49ce6ce | 2010-03-29 10:51:42 +1100 | [diff] [blame] | 7478 | geo->cylinders = mddev->array_sectors / 8; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 7479 | return 0; |
| 7480 | } |
| 7481 | |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 7482 | static inline bool md_ioctl_valid(unsigned int cmd) |
| 7483 | { |
| 7484 | switch (cmd) { |
| 7485 | case ADD_NEW_DISK: |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 7486 | case GET_ARRAY_INFO: |
| 7487 | case GET_BITMAP_FILE: |
| 7488 | case GET_DISK_INFO: |
| 7489 | case HOT_ADD_DISK: |
| 7490 | case HOT_REMOVE_DISK: |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 7491 | case RAID_VERSION: |
| 7492 | case RESTART_ARRAY_RW: |
| 7493 | case RUN_ARRAY: |
| 7494 | case SET_ARRAY_INFO: |
| 7495 | case SET_BITMAP_FILE: |
| 7496 | case SET_DISK_FAULTY: |
| 7497 | case STOP_ARRAY: |
| 7498 | case STOP_ARRAY_RO: |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 7499 | case CLUSTERED_DISK_NACK: |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 7500 | return true; |
| 7501 | default: |
| 7502 | return false; |
| 7503 | } |
| 7504 | } |
| 7505 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 7506 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7507 | unsigned int cmd, unsigned long arg) |
| 7508 | { |
| 7509 | int err = 0; |
| 7510 | void __user *argp = (void __user *)arg; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7511 | struct mddev *mddev = NULL; |
NeilBrown | 065e519 | 2017-04-06 11:16:33 +0800 | [diff] [blame] | 7512 | bool did_set_md_closing = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7513 | |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 7514 | if (!md_ioctl_valid(cmd)) |
| 7515 | return -ENOTTY; |
| 7516 | |
NeilBrown | 506c9e4 | 2011-12-23 10:17:26 +1100 | [diff] [blame] | 7517 | switch (cmd) { |
| 7518 | case RAID_VERSION: |
| 7519 | case GET_ARRAY_INFO: |
| 7520 | case GET_DISK_INFO: |
| 7521 | break; |
| 7522 | default: |
| 7523 | if (!capable(CAP_SYS_ADMIN)) |
| 7524 | return -EACCES; |
| 7525 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7526 | |
| 7527 | /* |
| 7528 | * Commands dealing with the RAID driver but not any |
| 7529 | * particular array: |
| 7530 | */ |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7531 | switch (cmd) { |
| 7532 | case RAID_VERSION: |
| 7533 | err = get_version(argp); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7534 | goto out; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7535 | default:; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7536 | } |
| 7537 | |
| 7538 | /* |
| 7539 | * Commands creating/starting a new array: |
| 7540 | */ |
| 7541 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 7542 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7543 | |
| 7544 | if (!mddev) { |
| 7545 | BUG(); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7546 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7547 | } |
| 7548 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 7549 | /* Some actions do not requires the mutex */ |
| 7550 | switch (cmd) { |
| 7551 | case GET_ARRAY_INFO: |
| 7552 | if (!mddev->raid_disks && !mddev->external) |
| 7553 | err = -ENODEV; |
| 7554 | else |
| 7555 | err = get_array_info(mddev, argp); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7556 | goto out; |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 7557 | |
| 7558 | case GET_DISK_INFO: |
| 7559 | if (!mddev->raid_disks && !mddev->external) |
| 7560 | err = -ENODEV; |
| 7561 | else |
| 7562 | err = get_disk_info(mddev, argp); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7563 | goto out; |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 7564 | |
| 7565 | case SET_DISK_FAULTY: |
| 7566 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7567 | goto out; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 7568 | |
| 7569 | case GET_BITMAP_FILE: |
| 7570 | err = get_bitmap_file(mddev, argp); |
| 7571 | goto out; |
| 7572 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 7573 | } |
| 7574 | |
Guoqing Jiang | 78b990c | 2020-04-04 23:57:10 +0200 | [diff] [blame] | 7575 | if (cmd == ADD_NEW_DISK || cmd == HOT_ADD_DISK) |
Guoqing Jiang | cc1ffe6 | 2020-04-04 23:57:08 +0200 | [diff] [blame] | 7576 | flush_rdev_wq(mddev); |
NeilBrown | a7a3f08 | 2012-12-11 13:35:54 +1100 | [diff] [blame] | 7577 | |
Hannes Reinecke | 90f5f7a | 2013-04-02 08:38:55 +0200 | [diff] [blame] | 7578 | if (cmd == HOT_REMOVE_DISK) |
| 7579 | /* need to ensure recovery thread has run */ |
| 7580 | wait_event_interruptible_timeout(mddev->sb_wait, |
| 7581 | !test_bit(MD_RECOVERY_NEEDED, |
Shaohua Li | 82a301c | 2016-12-08 15:48:18 -0800 | [diff] [blame] | 7582 | &mddev->recovery), |
Hannes Reinecke | 90f5f7a | 2013-04-02 08:38:55 +0200 | [diff] [blame] | 7583 | msecs_to_jiffies(5000)); |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 7584 | if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) { |
| 7585 | /* Need to flush page cache, and ensure no-one else opens |
| 7586 | * and writes |
| 7587 | */ |
| 7588 | mutex_lock(&mddev->open_mutex); |
NeilBrown | 9ba3b7f | 2014-09-09 14:00:15 +1000 | [diff] [blame] | 7589 | if (mddev->pers && atomic_read(&mddev->openers) > 1) { |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 7590 | mutex_unlock(&mddev->open_mutex); |
| 7591 | err = -EBUSY; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7592 | goto out; |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 7593 | } |
Dae R. Jeong | c731b84 | 2020-10-22 10:21:28 +0900 | [diff] [blame] | 7594 | if (test_and_set_bit(MD_CLOSING, &mddev->flags)) { |
| 7595 | mutex_unlock(&mddev->open_mutex); |
| 7596 | err = -EBUSY; |
| 7597 | goto out; |
| 7598 | } |
NeilBrown | 065e519 | 2017-04-06 11:16:33 +0800 | [diff] [blame] | 7599 | did_set_md_closing = true; |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 7600 | mutex_unlock(&mddev->open_mutex); |
| 7601 | sync_blockdev(bdev); |
| 7602 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7603 | err = mddev_lock(mddev); |
| 7604 | if (err) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7605 | pr_debug("md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 7606 | err, cmd); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7607 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7608 | } |
| 7609 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7610 | if (cmd == SET_ARRAY_INFO) { |
| 7611 | mdu_array_info_t info; |
| 7612 | if (!arg) |
| 7613 | memset(&info, 0, sizeof(info)); |
| 7614 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 7615 | err = -EFAULT; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7616 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7617 | } |
| 7618 | if (mddev->pers) { |
| 7619 | err = update_array_info(mddev, &info); |
| 7620 | if (err) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7621 | pr_warn("md: couldn't update array info. %d\n", err); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7622 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7623 | } |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7624 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7625 | } |
| 7626 | if (!list_empty(&mddev->disks)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7627 | pr_warn("md: array %s already has disks!\n", mdname(mddev)); |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7628 | err = -EBUSY; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7629 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7630 | } |
| 7631 | if (mddev->raid_disks) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7632 | pr_warn("md: array %s already initialised!\n", mdname(mddev)); |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7633 | err = -EBUSY; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7634 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7635 | } |
Christoph Hellwig | 7e0adbf | 2020-06-07 17:31:19 +0200 | [diff] [blame] | 7636 | err = md_set_array_info(mddev, &info); |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7637 | if (err) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7638 | pr_warn("md: couldn't set array info. %d\n", err); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7639 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7640 | } |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7641 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7642 | } |
| 7643 | |
| 7644 | /* |
| 7645 | * Commands querying/configuring an existing array: |
| 7646 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7647 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 7648 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 7649 | if ((!mddev->raid_disks && !mddev->external) |
| 7650 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 7651 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 7652 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7653 | err = -ENODEV; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7654 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7655 | } |
| 7656 | |
| 7657 | /* |
| 7658 | * Commands even a read-only array can execute: |
| 7659 | */ |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7660 | switch (cmd) { |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7661 | case RESTART_ARRAY_RW: |
| 7662 | err = restart_array(mddev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7663 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7664 | |
| 7665 | case STOP_ARRAY: |
| 7666 | err = do_md_stop(mddev, 0, bdev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7667 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7668 | |
| 7669 | case STOP_ARRAY_RO: |
| 7670 | err = md_set_readonly(mddev, bdev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7671 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7672 | |
NeilBrown | 3ea8929d | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 7673 | case HOT_REMOVE_DISK: |
| 7674 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7675 | goto unlock; |
NeilBrown | 3ea8929d | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 7676 | |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 7677 | case ADD_NEW_DISK: |
| 7678 | /* We can support ADD_NEW_DISK on read-only arrays |
Wei Fang | 466ad29 | 2016-03-21 19:19:30 +0800 | [diff] [blame] | 7679 | * only if we are re-adding a preexisting device. |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 7680 | * So require mddev->pers and MD_DISK_SYNC. |
| 7681 | */ |
| 7682 | if (mddev->pers) { |
| 7683 | mdu_disk_info_t info; |
| 7684 | if (copy_from_user(&info, argp, sizeof(info))) |
| 7685 | err = -EFAULT; |
| 7686 | else if (!(info.state & (1<<MD_DISK_SYNC))) |
| 7687 | /* Need to clear read-only for this */ |
| 7688 | break; |
| 7689 | else |
Christoph Hellwig | 7e0adbf | 2020-06-07 17:31:19 +0200 | [diff] [blame] | 7690 | err = md_add_new_disk(mddev, &info); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7691 | goto unlock; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 7692 | } |
| 7693 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7694 | } |
| 7695 | |
| 7696 | /* |
| 7697 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7698 | * superblock, so we do not allow them on read-only arrays. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7699 | */ |
NeilBrown | 326eb17 | 2014-09-30 15:36:28 +1000 | [diff] [blame] | 7700 | if (mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7701 | if (mddev->ro == 2) { |
| 7702 | mddev->ro = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7703 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7704 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | f3378b4 | 2013-02-28 11:59:03 +1100 | [diff] [blame] | 7705 | /* mddev_unlock will wake thread */ |
| 7706 | /* If a device failed while we were read-only, we |
| 7707 | * need to make sure the metadata is updated now. |
| 7708 | */ |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 7709 | if (test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)) { |
NeilBrown | f3378b4 | 2013-02-28 11:59:03 +1100 | [diff] [blame] | 7710 | mddev_unlock(mddev); |
| 7711 | wait_event(mddev->sb_wait, |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 7712 | !test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags) && |
| 7713 | !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)); |
NeilBrown | 29f097c | 2013-11-14 17:54:51 +1100 | [diff] [blame] | 7714 | mddev_lock_nointr(mddev); |
NeilBrown | f3378b4 | 2013-02-28 11:59:03 +1100 | [diff] [blame] | 7715 | } |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7716 | } else { |
| 7717 | err = -EROFS; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7718 | goto unlock; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7719 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7720 | } |
| 7721 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7722 | switch (cmd) { |
| 7723 | case ADD_NEW_DISK: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7724 | { |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7725 | mdu_disk_info_t info; |
| 7726 | if (copy_from_user(&info, argp, sizeof(info))) |
| 7727 | err = -EFAULT; |
| 7728 | else |
Christoph Hellwig | 7e0adbf | 2020-06-07 17:31:19 +0200 | [diff] [blame] | 7729 | err = md_add_new_disk(mddev, &info); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7730 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7731 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7732 | |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 7733 | case CLUSTERED_DISK_NACK: |
| 7734 | if (mddev_is_clustered(mddev)) |
| 7735 | md_cluster_ops->new_disk_ack(mddev, false); |
| 7736 | else |
| 7737 | err = -EINVAL; |
| 7738 | goto unlock; |
| 7739 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7740 | case HOT_ADD_DISK: |
| 7741 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7742 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7743 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7744 | case RUN_ARRAY: |
| 7745 | err = do_md_run(mddev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7746 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7747 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7748 | case SET_BITMAP_FILE: |
| 7749 | err = set_bitmap_file(mddev, (int)arg); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7750 | goto unlock; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7751 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7752 | default: |
| 7753 | err = -EINVAL; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7754 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7755 | } |
| 7756 | |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7757 | unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7758 | if (mddev->hold_active == UNTIL_IOCTL && |
| 7759 | err != -EINVAL) |
| 7760 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7761 | mddev_unlock(mddev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7762 | out: |
NeilBrown | 065e519 | 2017-04-06 11:16:33 +0800 | [diff] [blame] | 7763 | if(did_set_md_closing) |
| 7764 | clear_bit(MD_CLOSING, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7765 | return err; |
| 7766 | } |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 7767 | #ifdef CONFIG_COMPAT |
| 7768 | static int md_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 7769 | unsigned int cmd, unsigned long arg) |
| 7770 | { |
| 7771 | switch (cmd) { |
| 7772 | case HOT_REMOVE_DISK: |
| 7773 | case HOT_ADD_DISK: |
| 7774 | case SET_DISK_FAULTY: |
| 7775 | case SET_BITMAP_FILE: |
| 7776 | /* These take in integer arg, do not convert */ |
| 7777 | break; |
| 7778 | default: |
| 7779 | arg = (unsigned long)compat_ptr(arg); |
| 7780 | break; |
| 7781 | } |
| 7782 | |
| 7783 | return md_ioctl(bdev, mode, cmd, arg); |
| 7784 | } |
| 7785 | #endif /* CONFIG_COMPAT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7786 | |
Christoph Hellwig | 118cf08 | 2020-11-03 11:00:13 +0100 | [diff] [blame] | 7787 | static int md_set_read_only(struct block_device *bdev, bool ro) |
| 7788 | { |
| 7789 | struct mddev *mddev = bdev->bd_disk->private_data; |
| 7790 | int err; |
| 7791 | |
| 7792 | err = mddev_lock(mddev); |
| 7793 | if (err) |
| 7794 | return err; |
| 7795 | |
| 7796 | if (!mddev->raid_disks && !mddev->external) { |
| 7797 | err = -ENODEV; |
| 7798 | goto out_unlock; |
| 7799 | } |
| 7800 | |
| 7801 | /* |
| 7802 | * Transitioning to read-auto need only happen for arrays that call |
| 7803 | * md_write_start and which are not ready for writes yet. |
| 7804 | */ |
| 7805 | if (!ro && mddev->ro == 1 && mddev->pers) { |
| 7806 | err = restart_array(mddev); |
| 7807 | if (err) |
| 7808 | goto out_unlock; |
| 7809 | mddev->ro = 2; |
| 7810 | } |
| 7811 | |
| 7812 | out_unlock: |
| 7813 | mddev_unlock(mddev); |
| 7814 | return err; |
| 7815 | } |
| 7816 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 7817 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7818 | { |
| 7819 | /* |
| 7820 | * Succeed if we can lock the mddev, which confirms that |
| 7821 | * it isn't being stopped right now. |
| 7822 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7823 | struct mddev *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7824 | int err; |
| 7825 | |
Yuanhan Liu | 0c09822 | 2012-05-22 13:55:32 +1000 | [diff] [blame] | 7826 | if (!mddev) |
| 7827 | return -ENODEV; |
| 7828 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7829 | if (mddev->gendisk != bdev->bd_disk) { |
| 7830 | /* we are racing with mddev_put which is discarding this |
| 7831 | * bd_disk. |
| 7832 | */ |
| 7833 | mddev_put(mddev); |
| 7834 | /* Wait until bdev->bd_disk is definitely gone */ |
Guoqing Jiang | f6766ff | 2020-04-04 23:57:09 +0200 | [diff] [blame] | 7835 | if (work_pending(&mddev->del_work)) |
| 7836 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7837 | /* Then retry the open from the top */ |
| 7838 | return -ERESTARTSYS; |
| 7839 | } |
| 7840 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 7841 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 7842 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7843 | goto out; |
| 7844 | |
Guoqing Jiang | af8d8e6 | 2016-08-12 13:42:37 +0800 | [diff] [blame] | 7845 | if (test_bit(MD_CLOSING, &mddev->flags)) { |
| 7846 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | e2342ca | 2016-12-05 16:40:50 +1100 | [diff] [blame] | 7847 | err = -ENODEV; |
| 7848 | goto out; |
Guoqing Jiang | af8d8e6 | 2016-08-12 13:42:37 +0800 | [diff] [blame] | 7849 | } |
| 7850 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7851 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7852 | atomic_inc(&mddev->openers); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 7853 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7854 | |
Christoph Hellwig | 818077d | 2020-09-08 16:53:43 +0200 | [diff] [blame] | 7855 | bdev_check_media_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7856 | out: |
NeilBrown | e2342ca | 2016-12-05 16:40:50 +1100 | [diff] [blame] | 7857 | if (err) |
| 7858 | mddev_put(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7859 | return err; |
| 7860 | } |
| 7861 | |
Al Viro | db2a144 | 2013-05-05 21:52:57 -0400 | [diff] [blame] | 7862 | static void md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7863 | { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7864 | struct mddev *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7865 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 7866 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7867 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7868 | mddev_put(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7869 | } |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7870 | |
Christoph Hellwig | a564e23 | 2020-07-08 14:25:41 +0200 | [diff] [blame] | 7871 | static unsigned int md_check_events(struct gendisk *disk, unsigned int clearing) |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7872 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7873 | struct mddev *mddev = disk->private_data; |
Christoph Hellwig | a564e23 | 2020-07-08 14:25:41 +0200 | [diff] [blame] | 7874 | unsigned int ret = 0; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7875 | |
Christoph Hellwig | a564e23 | 2020-07-08 14:25:41 +0200 | [diff] [blame] | 7876 | if (mddev->changed) |
| 7877 | ret = DISK_EVENT_MEDIA_CHANGE; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7878 | mddev->changed = 0; |
Christoph Hellwig | a564e23 | 2020-07-08 14:25:41 +0200 | [diff] [blame] | 7879 | return ret; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7880 | } |
Christoph Hellwig | a564e23 | 2020-07-08 14:25:41 +0200 | [diff] [blame] | 7881 | |
Christoph Hellwig | 7e0adbf | 2020-06-07 17:31:19 +0200 | [diff] [blame] | 7882 | const struct block_device_operations md_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7883 | { |
| 7884 | .owner = THIS_MODULE, |
Christoph Hellwig | c62b37d | 2020-07-01 10:59:43 +0200 | [diff] [blame] | 7885 | .submit_bio = md_submit_bio, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 7886 | .open = md_open, |
| 7887 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 7888 | .ioctl = md_ioctl, |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 7889 | #ifdef CONFIG_COMPAT |
| 7890 | .compat_ioctl = md_compat_ioctl, |
| 7891 | #endif |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 7892 | .getgeo = md_getgeo, |
Christoph Hellwig | a564e23 | 2020-07-08 14:25:41 +0200 | [diff] [blame] | 7893 | .check_events = md_check_events, |
Christoph Hellwig | 118cf08 | 2020-11-03 11:00:13 +0100 | [diff] [blame] | 7894 | .set_read_only = md_set_read_only, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7895 | }; |
| 7896 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7897 | static int md_thread(void *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7898 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7899 | struct md_thread *thread = arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7900 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7901 | /* |
| 7902 | * md_thread is a 'system-thread', it's priority should be very |
| 7903 | * high. We avoid resource deadlocks individually in each |
| 7904 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 7905 | * the very same RT priority as kswapd, thus we will never get |
| 7906 | * into a priority inversion deadlock. |
| 7907 | * |
| 7908 | * we definitely have to have equal or higher priority than |
| 7909 | * bdflush, otherwise bdflush will deadlock if there are too |
| 7910 | * many dirty RAID5 blocks. |
| 7911 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7912 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 7913 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 7914 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7915 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 7916 | /* We need to wait INTERRUPTIBLE so that |
| 7917 | * we don't add to the load-average. |
| 7918 | * That means we need to be sure no signals are |
| 7919 | * pending |
| 7920 | */ |
| 7921 | if (signal_pending(current)) |
| 7922 | flush_signals(current); |
| 7923 | |
| 7924 | wait_event_interruptible_timeout |
| 7925 | (thread->wqueue, |
| 7926 | test_bit(THREAD_WAKEUP, &thread->flags) |
Shaohua Li | ce1ccd0 | 2016-11-21 10:29:18 -0800 | [diff] [blame] | 7927 | || kthread_should_stop() || kthread_should_park(), |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 7928 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7929 | |
NeilBrown | 6c98791 | 2011-01-14 09:13:53 +1100 | [diff] [blame] | 7930 | clear_bit(THREAD_WAKEUP, &thread->flags); |
Shaohua Li | ce1ccd0 | 2016-11-21 10:29:18 -0800 | [diff] [blame] | 7931 | if (kthread_should_park()) |
| 7932 | kthread_parkme(); |
NeilBrown | 6c98791 | 2011-01-14 09:13:53 +1100 | [diff] [blame] | 7933 | if (!kthread_should_stop()) |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 7934 | thread->run(thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7935 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 7936 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7937 | return 0; |
| 7938 | } |
| 7939 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7940 | void md_wakeup_thread(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7941 | { |
| 7942 | if (thread) { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 7943 | pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm); |
Guoqing Jiang | d1d9014 | 2017-10-09 10:32:48 +0800 | [diff] [blame] | 7944 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 7945 | wake_up(&thread->wqueue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7946 | } |
| 7947 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7948 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7949 | |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 7950 | struct md_thread *md_register_thread(void (*run) (struct md_thread *), |
| 7951 | struct mddev *mddev, const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7952 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7953 | struct md_thread *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7954 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7955 | thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7956 | if (!thread) |
| 7957 | return NULL; |
| 7958 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7959 | init_waitqueue_head(&thread->wqueue); |
| 7960 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7961 | thread->run = run; |
| 7962 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7963 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 7964 | thread->tsk = kthread_run(md_thread, thread, |
| 7965 | "%s_%s", |
| 7966 | mdname(thread->mddev), |
NeilBrown | 0232605 | 2012-07-03 15:56:52 +1000 | [diff] [blame] | 7967 | name); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 7968 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7969 | kfree(thread); |
| 7970 | return NULL; |
| 7971 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7972 | return thread; |
| 7973 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7974 | EXPORT_SYMBOL(md_register_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7975 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7976 | void md_unregister_thread(struct md_thread **threadp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7977 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7978 | struct md_thread *thread = *threadp; |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7979 | if (!thread) |
| 7980 | return; |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 7981 | pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 7982 | /* Locking ensures that mddev_unlock does not wake_up a |
| 7983 | * non-existent thread |
| 7984 | */ |
| 7985 | spin_lock(&pers_lock); |
| 7986 | *threadp = NULL; |
| 7987 | spin_unlock(&pers_lock); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 7988 | |
| 7989 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7990 | kfree(thread); |
| 7991 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7992 | EXPORT_SYMBOL(md_unregister_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7993 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7994 | void md_error(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7995 | { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7996 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7997 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7998 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 7999 | if (!mddev->pers || !mddev->pers->error_handler) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8000 | return; |
| 8001 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8002 | if (mddev->degraded) |
| 8003 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 8004 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8005 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 8006 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 8007 | md_wakeup_thread(mddev->thread); |
NeilBrown | 768a418 | 2010-07-26 11:49:55 +1000 | [diff] [blame] | 8008 | if (mddev->event_work.func) |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8009 | queue_work(md_misc_wq, &mddev->event_work); |
Guoqing Jiang | bb9ef71 | 2015-12-28 10:46:38 +0800 | [diff] [blame] | 8010 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8011 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 8012 | EXPORT_SYMBOL(md_error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8013 | |
| 8014 | /* seq_file implementation /proc/mdstat */ |
| 8015 | |
| 8016 | static void status_unused(struct seq_file *seq) |
| 8017 | { |
| 8018 | int i = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8019 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8020 | |
| 8021 | seq_printf(seq, "unused devices: "); |
| 8022 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 8023 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8024 | char b[BDEVNAME_SIZE]; |
| 8025 | i++; |
| 8026 | seq_printf(seq, "%s ", |
| 8027 | bdevname(rdev->bdev,b)); |
| 8028 | } |
| 8029 | if (!i) |
| 8030 | seq_printf(seq, "<none>"); |
| 8031 | |
| 8032 | seq_printf(seq, "\n"); |
| 8033 | } |
| 8034 | |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 8035 | static int status_resync(struct seq_file *seq, struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8036 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 8037 | sector_t max_sectors, resync, res; |
Mariusz Tkaczyk | 9642fa7 | 2019-06-13 16:11:41 +0200 | [diff] [blame] | 8038 | unsigned long dt, db = 0; |
| 8039 | sector_t rt, curr_mark_cnt, resync_mark_cnt; |
| 8040 | int scale, recovery_active; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 8041 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8042 | |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 8043 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 8044 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 8045 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8046 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 8047 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8048 | |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 8049 | resync = mddev->curr_resync; |
| 8050 | if (resync <= 3) { |
| 8051 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 8052 | /* Still cleaning up */ |
| 8053 | resync = max_sectors; |
Nate Dailey | d2e2ec8 | 2017-11-30 11:33:30 -0500 | [diff] [blame] | 8054 | } else if (resync > max_sectors) |
| 8055 | resync = max_sectors; |
| 8056 | else |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 8057 | resync -= atomic_read(&mddev->recovery_active); |
| 8058 | |
| 8059 | if (resync == 0) { |
Guoqing Jiang | 0357ba2 | 2018-07-02 16:26:25 +0800 | [diff] [blame] | 8060 | if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery)) { |
| 8061 | struct md_rdev *rdev; |
| 8062 | |
| 8063 | rdev_for_each(rdev, mddev) |
| 8064 | if (rdev->raid_disk >= 0 && |
| 8065 | !test_bit(Faulty, &rdev->flags) && |
| 8066 | rdev->recovery_offset != MaxSector && |
| 8067 | rdev->recovery_offset) { |
| 8068 | seq_printf(seq, "\trecover=REMOTE"); |
| 8069 | return 1; |
| 8070 | } |
| 8071 | if (mddev->reshape_position != MaxSector) |
| 8072 | seq_printf(seq, "\treshape=REMOTE"); |
| 8073 | else |
| 8074 | seq_printf(seq, "\tresync=REMOTE"); |
| 8075 | return 1; |
| 8076 | } |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 8077 | if (mddev->recovery_cp < MaxSector) { |
| 8078 | seq_printf(seq, "\tresync=PENDING"); |
| 8079 | return 1; |
| 8080 | } |
| 8081 | return 0; |
| 8082 | } |
| 8083 | if (resync < 3) { |
| 8084 | seq_printf(seq, "\tresync=DELAYED"); |
| 8085 | return 1; |
| 8086 | } |
| 8087 | |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 8088 | WARN_ON(max_sectors == 0); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 8089 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 8090 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 8091 | * u32, as those are the requirements for sector_div. |
| 8092 | * Thus 'scale' must be at least 10 |
| 8093 | */ |
| 8094 | scale = 10; |
| 8095 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 8096 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 8097 | scale++; |
| 8098 | } |
| 8099 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 8100 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 8101 | |
| 8102 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8103 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 8104 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8105 | seq_printf(seq, "["); |
| 8106 | for (i = 0; i < x; i++) |
| 8107 | seq_printf(seq, "="); |
| 8108 | seq_printf(seq, ">"); |
| 8109 | for (i = 0; i < y; i++) |
| 8110 | seq_printf(seq, "."); |
| 8111 | seq_printf(seq, "] "); |
| 8112 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 8113 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 8114 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 8115 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 8116 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 8117 | "check" : |
| 8118 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 8119 | "resync" : "recovery"))), |
| 8120 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 8121 | (unsigned long long) resync/2, |
| 8122 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8123 | |
| 8124 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8125 | * dt: time from mark until now |
| 8126 | * db: blocks written from mark until now |
| 8127 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 8128 | * |
Mariusz Tkaczyk | 9642fa7 | 2019-06-13 16:11:41 +0200 | [diff] [blame] | 8129 | * rt is a sector_t, which is always 64bit now. We are keeping |
| 8130 | * the original algorithm, but it is not really necessary. |
| 8131 | * |
| 8132 | * Original algorithm: |
| 8133 | * So we divide before multiply in case it is 32bit and close |
| 8134 | * to the limit. |
| 8135 | * We scale the divisor (db) by 32 to avoid losing precision |
| 8136 | * near the end of resync when the number of remaining sectors |
| 8137 | * is close to 'db'. |
| 8138 | * We then divide rt by 32 after multiplying by db to compensate. |
| 8139 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8140 | */ |
| 8141 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 8142 | if (!dt) dt++; |
Mariusz Tkaczyk | 9642fa7 | 2019-06-13 16:11:41 +0200 | [diff] [blame] | 8143 | |
| 8144 | curr_mark_cnt = mddev->curr_mark_cnt; |
| 8145 | recovery_active = atomic_read(&mddev->recovery_active); |
| 8146 | resync_mark_cnt = mddev->resync_mark_cnt; |
| 8147 | |
| 8148 | if (curr_mark_cnt >= (recovery_active + resync_mark_cnt)) |
| 8149 | db = curr_mark_cnt - (recovery_active + resync_mark_cnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8150 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 8151 | rt = max_sectors - resync; /* number of remaining sectors */ |
Mariusz Tkaczyk | 9642fa7 | 2019-06-13 16:11:41 +0200 | [diff] [blame] | 8152 | rt = div64_u64(rt, db/32+1); |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 8153 | rt *= dt; |
| 8154 | rt >>= 5; |
| 8155 | |
| 8156 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 8157 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8158 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 8159 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 8160 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8161 | } |
| 8162 | |
| 8163 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 8164 | { |
| 8165 | struct list_head *tmp; |
| 8166 | loff_t l = *pos; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8167 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8168 | |
| 8169 | if (l >= 0x10000) |
| 8170 | return NULL; |
| 8171 | if (!l--) |
| 8172 | /* header */ |
| 8173 | return (void*)1; |
| 8174 | |
| 8175 | spin_lock(&all_mddevs_lock); |
| 8176 | list_for_each(tmp,&all_mddevs) |
| 8177 | if (!l--) { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8178 | mddev = list_entry(tmp, struct mddev, all_mddevs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8179 | mddev_get(mddev); |
| 8180 | spin_unlock(&all_mddevs_lock); |
| 8181 | return mddev; |
| 8182 | } |
| 8183 | spin_unlock(&all_mddevs_lock); |
| 8184 | if (!l--) |
| 8185 | return (void*)2;/* tail */ |
| 8186 | return NULL; |
| 8187 | } |
| 8188 | |
| 8189 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 8190 | { |
| 8191 | struct list_head *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8192 | struct mddev *next_mddev, *mddev = v; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 8193 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8194 | ++*pos; |
| 8195 | if (v == (void*)2) |
| 8196 | return NULL; |
| 8197 | |
| 8198 | spin_lock(&all_mddevs_lock); |
| 8199 | if (v == (void*)1) |
| 8200 | tmp = all_mddevs.next; |
| 8201 | else |
| 8202 | tmp = mddev->all_mddevs.next; |
| 8203 | if (tmp != &all_mddevs) |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8204 | next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8205 | else { |
| 8206 | next_mddev = (void*)2; |
| 8207 | *pos = 0x10000; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 8208 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8209 | spin_unlock(&all_mddevs_lock); |
| 8210 | |
| 8211 | if (v != (void*)1) |
| 8212 | mddev_put(mddev); |
| 8213 | return next_mddev; |
| 8214 | |
| 8215 | } |
| 8216 | |
| 8217 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 8218 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8219 | struct mddev *mddev = v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8220 | |
| 8221 | if (mddev && v != (void*)1 && v != (void*)2) |
| 8222 | mddev_put(mddev); |
| 8223 | } |
| 8224 | |
| 8225 | static int md_seq_show(struct seq_file *seq, void *v) |
| 8226 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8227 | struct mddev *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8228 | sector_t sectors; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8229 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8230 | |
| 8231 | if (v == (void*)1) { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 8232 | struct md_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8233 | seq_printf(seq, "Personalities : "); |
| 8234 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8235 | list_for_each_entry(pers, &pers_list, list) |
| 8236 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8237 | |
| 8238 | spin_unlock(&pers_lock); |
| 8239 | seq_printf(seq, "\n"); |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 8240 | seq->poll_event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8241 | return 0; |
| 8242 | } |
| 8243 | if (v == (void*)2) { |
| 8244 | status_unused(seq); |
| 8245 | return 0; |
| 8246 | } |
| 8247 | |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 8248 | spin_lock(&mddev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8249 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 8250 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 8251 | mddev->pers ? "" : "in"); |
| 8252 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8253 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8254 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8255 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 8256 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8257 | seq_printf(seq, " %s", mddev->pers->name); |
| 8258 | } |
| 8259 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8260 | sectors = 0; |
NeilBrown | f97fcad | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 8261 | rcu_read_lock(); |
| 8262 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8263 | char b[BDEVNAME_SIZE]; |
| 8264 | seq_printf(seq, " %s[%d]", |
| 8265 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 8266 | if (test_bit(WriteMostly, &rdev->flags)) |
| 8267 | seq_printf(seq, "(W)"); |
Shaohua Li | 9efdca1 | 2015-10-12 16:59:50 -0700 | [diff] [blame] | 8268 | if (test_bit(Journal, &rdev->flags)) |
| 8269 | seq_printf(seq, "(J)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 8270 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8271 | seq_printf(seq, "(F)"); |
| 8272 | continue; |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 8273 | } |
| 8274 | if (rdev->raid_disk < 0) |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 8275 | seq_printf(seq, "(S)"); /* spare */ |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 8276 | if (test_bit(Replacement, &rdev->flags)) |
| 8277 | seq_printf(seq, "(R)"); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8278 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8279 | } |
NeilBrown | f97fcad | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 8280 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8281 | |
| 8282 | if (!list_empty(&mddev->disks)) { |
| 8283 | if (mddev->pers) |
| 8284 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 8285 | (unsigned long long) |
| 8286 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8287 | else |
| 8288 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8289 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8290 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 8291 | if (mddev->persistent) { |
| 8292 | if (mddev->major_version != 0 || |
| 8293 | mddev->minor_version != 90) { |
| 8294 | seq_printf(seq," super %d.%d", |
| 8295 | mddev->major_version, |
| 8296 | mddev->minor_version); |
| 8297 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 8298 | } else if (mddev->external) |
| 8299 | seq_printf(seq, " super external:%s", |
| 8300 | mddev->metadata_type); |
| 8301 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 8302 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8303 | |
| 8304 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 8305 | mddev->pers->status(seq, mddev); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 8306 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 8307 | if (mddev->pers->sync_request) { |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 8308 | if (status_resync(seq, mddev)) |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 8309 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 8310 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 8311 | } else |
| 8312 | seq_printf(seq, "\n "); |
| 8313 | |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 8314 | md_bitmap_status(seq, mddev->bitmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8315 | |
| 8316 | seq_printf(seq, "\n"); |
| 8317 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 8318 | spin_unlock(&mddev->lock); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 8319 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8320 | return 0; |
| 8321 | } |
| 8322 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 8323 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8324 | .start = md_seq_start, |
| 8325 | .next = md_seq_next, |
| 8326 | .stop = md_seq_stop, |
| 8327 | .show = md_seq_show, |
| 8328 | }; |
| 8329 | |
| 8330 | static int md_seq_open(struct inode *inode, struct file *file) |
| 8331 | { |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 8332 | struct seq_file *seq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8333 | int error; |
| 8334 | |
| 8335 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 8336 | if (error) |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 8337 | return error; |
| 8338 | |
| 8339 | seq = file->private_data; |
| 8340 | seq->poll_event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8341 | return error; |
| 8342 | } |
| 8343 | |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 8344 | static int md_unloading; |
Al Viro | afc9a42 | 2017-07-03 06:39:46 -0400 | [diff] [blame] | 8345 | static __poll_t mdstat_poll(struct file *filp, poll_table *wait) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 8346 | { |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 8347 | struct seq_file *seq = filp->private_data; |
Al Viro | afc9a42 | 2017-07-03 06:39:46 -0400 | [diff] [blame] | 8348 | __poll_t mask; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 8349 | |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 8350 | if (md_unloading) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 8351 | return EPOLLIN|EPOLLRDNORM|EPOLLERR|EPOLLPRI; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 8352 | poll_wait(filp, &md_event_waiters, wait); |
| 8353 | |
| 8354 | /* always allow read */ |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 8355 | mask = EPOLLIN | EPOLLRDNORM; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 8356 | |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 8357 | if (seq->poll_event != atomic_read(&md_event_count)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 8358 | mask |= EPOLLERR | EPOLLPRI; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 8359 | return mask; |
| 8360 | } |
| 8361 | |
Alexey Dobriyan | 97a3253 | 2020-02-03 17:37:17 -0800 | [diff] [blame] | 8362 | static const struct proc_ops mdstat_proc_ops = { |
| 8363 | .proc_open = md_seq_open, |
| 8364 | .proc_read = seq_read, |
| 8365 | .proc_lseek = seq_lseek, |
| 8366 | .proc_release = seq_release, |
| 8367 | .proc_poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8368 | }; |
| 8369 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 8370 | int register_md_personality(struct md_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8371 | { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8372 | pr_debug("md: %s personality registered for level %d\n", |
| 8373 | p->name, p->level); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8374 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8375 | list_add_tail(&p->list, &pers_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8376 | spin_unlock(&pers_lock); |
| 8377 | return 0; |
| 8378 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 8379 | EXPORT_SYMBOL(register_md_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8380 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 8381 | int unregister_md_personality(struct md_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8382 | { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8383 | pr_debug("md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8384 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8385 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8386 | spin_unlock(&pers_lock); |
| 8387 | return 0; |
| 8388 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 8389 | EXPORT_SYMBOL(unregister_md_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8390 | |
NeilBrown | 6022e75 | 2015-08-13 12:32:55 +1000 | [diff] [blame] | 8391 | int register_md_cluster_operations(struct md_cluster_operations *ops, |
| 8392 | struct module *module) |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 8393 | { |
NeilBrown | 6022e75 | 2015-08-13 12:32:55 +1000 | [diff] [blame] | 8394 | int ret = 0; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 8395 | spin_lock(&pers_lock); |
NeilBrown | 6022e75 | 2015-08-13 12:32:55 +1000 | [diff] [blame] | 8396 | if (md_cluster_ops != NULL) |
| 8397 | ret = -EALREADY; |
| 8398 | else { |
| 8399 | md_cluster_ops = ops; |
| 8400 | md_cluster_mod = module; |
| 8401 | } |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 8402 | spin_unlock(&pers_lock); |
NeilBrown | 6022e75 | 2015-08-13 12:32:55 +1000 | [diff] [blame] | 8403 | return ret; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 8404 | } |
| 8405 | EXPORT_SYMBOL(register_md_cluster_operations); |
| 8406 | |
| 8407 | int unregister_md_cluster_operations(void) |
| 8408 | { |
| 8409 | spin_lock(&pers_lock); |
| 8410 | md_cluster_ops = NULL; |
| 8411 | spin_unlock(&pers_lock); |
| 8412 | return 0; |
| 8413 | } |
| 8414 | EXPORT_SYMBOL(unregister_md_cluster_operations); |
| 8415 | |
| 8416 | int md_setup_cluster(struct mddev *mddev, int nodes) |
| 8417 | { |
Zhao Heming | 7c9d5c5 | 2020-07-21 02:08:52 +0800 | [diff] [blame] | 8418 | int ret; |
Guoqing Jiang | 47a7b0d | 2016-09-04 22:17:28 -0400 | [diff] [blame] | 8419 | if (!md_cluster_ops) |
| 8420 | request_module("md-cluster"); |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 8421 | spin_lock(&pers_lock); |
Guoqing Jiang | 47a7b0d | 2016-09-04 22:17:28 -0400 | [diff] [blame] | 8422 | /* ensure module won't be unloaded */ |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 8423 | if (!md_cluster_ops || !try_module_get(md_cluster_mod)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8424 | 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] | 8425 | spin_unlock(&pers_lock); |
| 8426 | return -ENOENT; |
| 8427 | } |
| 8428 | spin_unlock(&pers_lock); |
| 8429 | |
Zhao Heming | 7c9d5c5 | 2020-07-21 02:08:52 +0800 | [diff] [blame] | 8430 | ret = md_cluster_ops->join(mddev, nodes); |
| 8431 | if (!ret) |
| 8432 | mddev->safemode_delay = 0; |
| 8433 | return ret; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 8434 | } |
| 8435 | |
| 8436 | void md_cluster_stop(struct mddev *mddev) |
| 8437 | { |
Goldwyn Rodrigues | c4ce867 | 2014-03-29 10:20:02 -0500 | [diff] [blame] | 8438 | if (!md_cluster_ops) |
| 8439 | return; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 8440 | md_cluster_ops->leave(mddev); |
| 8441 | module_put(md_cluster_mod); |
| 8442 | } |
| 8443 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8444 | static int is_mddev_idle(struct mddev *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8445 | { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 8446 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8447 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8448 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8449 | |
| 8450 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 8451 | rcu_read_lock(); |
| 8452 | rdev_for_each_rcu(rdev, mddev) { |
Christoph Hellwig | 4245e52 | 2020-09-03 07:40:59 +0200 | [diff] [blame] | 8453 | struct gendisk *disk = rdev->bdev->bd_disk; |
Christoph Hellwig | 8446fe9 | 2020-11-24 09:36:54 +0100 | [diff] [blame] | 8454 | curr_events = (int)part_stat_read_accum(disk->part0, sectors) - |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8455 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 8456 | /* sync IO will cause sync_io to increase before the disk_stats |
| 8457 | * as sync_io is counted when a request starts, and |
| 8458 | * disk_stats is counted when it completes. |
| 8459 | * So resync activity will cause curr_events to be smaller than |
| 8460 | * when there was no such activity. |
| 8461 | * non-sync IO will cause disk_stat to increase without |
| 8462 | * increasing sync_io so curr_events will (eventually) |
| 8463 | * be larger than it was before. Once it becomes |
| 8464 | * substantially larger, the test below will cause |
| 8465 | * the array to appear non-idle, and resync will slow |
| 8466 | * down. |
| 8467 | * If there is a lot of outstanding resync activity when |
| 8468 | * we set last_event to curr_events, then all that activity |
| 8469 | * completing might cause the array to appear non-idle |
| 8470 | * and resync will be slowed down even though there might |
| 8471 | * not have been non-resync activity. This will only |
| 8472 | * happen once though. 'last_events' will soon reflect |
| 8473 | * the state where there is little or no outstanding |
| 8474 | * resync requests, and further resync activity will |
| 8475 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 8476 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8477 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8478 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8479 | rdev->last_events = curr_events; |
| 8480 | idle = 0; |
| 8481 | } |
| 8482 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 8483 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8484 | return idle; |
| 8485 | } |
| 8486 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8487 | void md_done_sync(struct mddev *mddev, int blocks, int ok) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8488 | { |
| 8489 | /* another "blocks" (512byte) blocks have been synced */ |
| 8490 | atomic_sub(blocks, &mddev->recovery_active); |
| 8491 | wake_up(&mddev->recovery_wait); |
| 8492 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 8493 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
majianpeng | 0a19caa | 2012-11-19 19:57:34 +0800 | [diff] [blame] | 8494 | set_bit(MD_RECOVERY_ERROR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8495 | md_wakeup_thread(mddev->thread); |
| 8496 | // stop recovery, signal do_sync .... |
| 8497 | } |
| 8498 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 8499 | EXPORT_SYMBOL(md_done_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8500 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 8501 | /* md_write_start(mddev, bi) |
| 8502 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 8503 | * in superblock) before writing, schedule a superblock update |
| 8504 | * and wait for it to complete. |
NeilBrown | cc27b0c | 2017-06-05 16:49:39 +1000 | [diff] [blame] | 8505 | * A return value of 'false' means that the write wasn't recorded |
| 8506 | * and cannot proceed as the array is being suspend. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 8507 | */ |
NeilBrown | cc27b0c | 2017-06-05 16:49:39 +1000 | [diff] [blame] | 8508 | bool md_write_start(struct mddev *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8509 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 8510 | int did_change = 0; |
Heinz Mauelshagen | 4b6c106 | 2018-02-02 23:13:19 +0100 | [diff] [blame] | 8511 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 8512 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | cc27b0c | 2017-06-05 16:49:39 +1000 | [diff] [blame] | 8513 | return true; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 8514 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8515 | BUG_ON(mddev->ro == 1); |
| 8516 | if (mddev->ro == 2) { |
| 8517 | /* need to switch to read/write */ |
| 8518 | mddev->ro = 0; |
| 8519 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 8520 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 8521 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 8522 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8523 | } |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 8524 | rcu_read_lock(); |
| 8525 | percpu_ref_get(&mddev->writes_pending); |
NeilBrown | 55cc39f | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 8526 | smp_mb(); /* Match smp_mb in set_in_sync() */ |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 8527 | if (mddev->safemode == 1) |
| 8528 | mddev->safemode = 0; |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 8529 | /* sync_checkers is always 0 when writes_pending is in per-cpu mode */ |
NeilBrown | 81fe48e | 2017-08-08 16:56:36 +1000 | [diff] [blame] | 8530 | if (mddev->in_sync || mddev->sync_checkers) { |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 8531 | spin_lock(&mddev->lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 8532 | if (mddev->in_sync) { |
| 8533 | mddev->in_sync = 0; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 8534 | set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags); |
| 8535 | set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 8536 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 8537 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 8538 | } |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 8539 | spin_unlock(&mddev->lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 8540 | } |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 8541 | rcu_read_unlock(); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 8542 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 8543 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Heinz Mauelshagen | 4b6c106 | 2018-02-02 23:13:19 +0100 | [diff] [blame] | 8544 | if (!mddev->has_superblocks) |
| 8545 | return true; |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 8546 | wait_event(mddev->sb_wait, |
NeilBrown | d47c8ad | 2017-10-05 16:23:16 +1100 | [diff] [blame] | 8547 | !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags) || |
| 8548 | mddev->suspended); |
NeilBrown | cc27b0c | 2017-06-05 16:49:39 +1000 | [diff] [blame] | 8549 | if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) { |
| 8550 | percpu_ref_put(&mddev->writes_pending); |
| 8551 | return false; |
| 8552 | } |
| 8553 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8554 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 8555 | EXPORT_SYMBOL(md_write_start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8556 | |
NeilBrown | 4972805 | 2017-03-15 14:05:12 +1100 | [diff] [blame] | 8557 | /* md_write_inc can only be called when md_write_start() has |
| 8558 | * already been called at least once of the current request. |
| 8559 | * It increments the counter and is useful when a single request |
| 8560 | * is split into several parts. Each part causes an increment and |
| 8561 | * so needs a matching md_write_end(). |
| 8562 | * Unlike md_write_start(), it is safe to call md_write_inc() inside |
| 8563 | * a spinlocked region. |
| 8564 | */ |
| 8565 | void md_write_inc(struct mddev *mddev, struct bio *bi) |
| 8566 | { |
| 8567 | if (bio_data_dir(bi) != WRITE) |
| 8568 | return; |
| 8569 | WARN_ON_ONCE(mddev->in_sync || mddev->ro); |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 8570 | percpu_ref_get(&mddev->writes_pending); |
NeilBrown | 4972805 | 2017-03-15 14:05:12 +1100 | [diff] [blame] | 8571 | } |
| 8572 | EXPORT_SYMBOL(md_write_inc); |
| 8573 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8574 | void md_write_end(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8575 | { |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 8576 | percpu_ref_put(&mddev->writes_pending); |
| 8577 | |
| 8578 | if (mddev->safemode == 2) |
| 8579 | md_wakeup_thread(mddev->thread); |
| 8580 | else if (mddev->safemode_delay) |
| 8581 | /* The roundup() ensures this only performs locking once |
| 8582 | * every ->safemode_delay jiffies |
| 8583 | */ |
| 8584 | mod_timer(&mddev->safemode_timer, |
| 8585 | roundup(jiffies, mddev->safemode_delay) + |
| 8586 | mddev->safemode_delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8587 | } |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 8588 | |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 8589 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8590 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8591 | /* md_allow_write(mddev) |
| 8592 | * Calling this ensures that the array is marked 'active' so that writes |
| 8593 | * may proceed without blocking. It is important to call this before |
| 8594 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 8595 | * Must be called with mddev_lock held. |
| 8596 | */ |
Artur Paszkiewicz | 2214c26 | 2017-05-08 11:56:55 +0200 | [diff] [blame] | 8597 | void md_allow_write(struct mddev *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8598 | { |
| 8599 | if (!mddev->pers) |
Artur Paszkiewicz | 2214c26 | 2017-05-08 11:56:55 +0200 | [diff] [blame] | 8600 | return; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8601 | if (mddev->ro) |
Artur Paszkiewicz | 2214c26 | 2017-05-08 11:56:55 +0200 | [diff] [blame] | 8602 | return; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 8603 | if (!mddev->pers->sync_request) |
Artur Paszkiewicz | 2214c26 | 2017-05-08 11:56:55 +0200 | [diff] [blame] | 8604 | return; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8605 | |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 8606 | spin_lock(&mddev->lock); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8607 | if (mddev->in_sync) { |
| 8608 | mddev->in_sync = 0; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 8609 | set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags); |
| 8610 | set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8611 | if (mddev->safemode_delay && |
| 8612 | mddev->safemode == 0) |
| 8613 | mddev->safemode = 1; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 8614 | spin_unlock(&mddev->lock); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8615 | md_update_sb(mddev, 0); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 8616 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Artur Paszkiewicz | 2214c26 | 2017-05-08 11:56:55 +0200 | [diff] [blame] | 8617 | /* wait for the dirty state to be recorded in the metadata */ |
| 8618 | wait_event(mddev->sb_wait, |
Artur Paszkiewicz | 2214c26 | 2017-05-08 11:56:55 +0200 | [diff] [blame] | 8619 | !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8620 | } else |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 8621 | spin_unlock(&mddev->lock); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8622 | } |
| 8623 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 8624 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8625 | #define SYNC_MARKS 10 |
| 8626 | #define SYNC_MARK_STEP (3*HZ) |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 8627 | #define UPDATE_FREQUENCY (5*60*HZ) |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 8628 | void md_do_sync(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8629 | { |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 8630 | struct mddev *mddev = thread->mddev; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8631 | struct mddev *mddev2; |
Yufen Yu | e5b521e | 2019-06-14 15:41:07 -0700 | [diff] [blame] | 8632 | unsigned int currspeed = 0, window; |
Xiao Ni | ac7e50a | 2014-08-07 09:37:41 -0400 | [diff] [blame] | 8633 | sector_t max_sectors,j, io_sectors, recovery_done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8634 | unsigned long mark[SYNC_MARKS]; |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 8635 | unsigned long update_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8636 | sector_t mark_cnt[SYNC_MARKS]; |
| 8637 | int last_mark,m; |
| 8638 | struct list_head *tmp; |
| 8639 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8640 | int skipped = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8641 | struct md_rdev *rdev; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 8642 | char *desc, *action = NULL; |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 8643 | struct blk_plug plug; |
Guoqing Jiang | 41a9a0d | 2016-05-02 11:33:08 -0400 | [diff] [blame] | 8644 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8645 | |
| 8646 | /* just incase thread restarts... */ |
Song Liu | d5d885f | 2017-11-19 22:17:01 -0800 | [diff] [blame] | 8647 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
| 8648 | test_bit(MD_RECOVERY_WAIT, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8649 | return; |
NeilBrown | 3991b31 | 2014-05-28 13:39:23 +1000 | [diff] [blame] | 8650 | if (mddev->ro) {/* never try to sync a read-only array */ |
| 8651 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8652 | return; |
NeilBrown | 3991b31 | 2014-05-28 13:39:23 +1000 | [diff] [blame] | 8653 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8654 | |
Guoqing Jiang | 41a9a0d | 2016-05-02 11:33:08 -0400 | [diff] [blame] | 8655 | if (mddev_is_clustered(mddev)) { |
| 8656 | ret = md_cluster_ops->resync_start(mddev); |
| 8657 | if (ret) |
| 8658 | goto skip; |
| 8659 | |
Guoqing Jiang | bb8bf15 | 2016-06-02 23:32:04 -0400 | [diff] [blame] | 8660 | set_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags); |
Guoqing Jiang | 41a9a0d | 2016-05-02 11:33:08 -0400 | [diff] [blame] | 8661 | if (!(test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 8662 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) || |
| 8663 | test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
| 8664 | && ((unsigned long long)mddev->curr_resync_completed |
| 8665 | < (unsigned long long)mddev->resync_max_sectors)) |
| 8666 | goto skip; |
| 8667 | } |
| 8668 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 8669 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 8670 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 8671 | desc = "data-check"; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 8672 | action = "check"; |
| 8673 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 8674 | desc = "requested-resync"; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 8675 | action = "repair"; |
| 8676 | } else |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 8677 | desc = "resync"; |
| 8678 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 8679 | desc = "reshape"; |
| 8680 | else |
| 8681 | desc = "recovery"; |
| 8682 | |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 8683 | mddev->last_sync_action = action ?: desc; |
| 8684 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8685 | /* we overload curr_resync somewhat here. |
| 8686 | * 0 == not engaged in resync at all |
| 8687 | * 2 == checking that there is no conflict with another sync |
| 8688 | * 1 == like 2, but have yielded to allow conflicting resync to |
Yufen Yu | e5b521e | 2019-06-14 15:41:07 -0700 | [diff] [blame] | 8689 | * commence |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8690 | * other == active in resync - this many blocks |
| 8691 | * |
| 8692 | * Before starting a resync we must have set curr_resync to |
| 8693 | * 2, and then checked that every "conflicting" array has curr_resync |
| 8694 | * less than ours. When we find one that is the same or higher |
| 8695 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 8696 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 8697 | * This will mean we have to start checking from the beginning again. |
| 8698 | * |
| 8699 | */ |
| 8700 | |
| 8701 | do { |
Artur Paszkiewicz | c622ca5 | 2016-08-16 14:26:08 +0200 | [diff] [blame] | 8702 | int mddev2_minor = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8703 | mddev->curr_resync = 2; |
| 8704 | |
| 8705 | try_again: |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 8706 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8707 | goto skip; |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 8708 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8709 | if (mddev2 == mddev) |
| 8710 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 8711 | if (!mddev->parallel_resync |
| 8712 | && mddev2->curr_resync |
| 8713 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8714 | DEFINE_WAIT(wq); |
| 8715 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 8716 | /* arbitrarily yield */ |
| 8717 | mddev->curr_resync = 1; |
| 8718 | wake_up(&resync_wait); |
| 8719 | } |
| 8720 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 8721 | /* no need to wait here, we can wait the next |
| 8722 | * time 'round when curr_resync == 2 |
| 8723 | */ |
| 8724 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 8725 | /* We need to wait 'interruptible' so as not to |
| 8726 | * contribute to the load average, and not to |
| 8727 | * be caught by 'softlockup' |
| 8728 | */ |
| 8729 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8730 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 8731 | mddev2->curr_resync >= mddev->curr_resync) { |
Artur Paszkiewicz | c622ca5 | 2016-08-16 14:26:08 +0200 | [diff] [blame] | 8732 | if (mddev2_minor != mddev2->md_minor) { |
| 8733 | mddev2_minor = mddev2->md_minor; |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8734 | pr_info("md: delaying %s of %s until %s has finished (they share one or more physical units)\n", |
| 8735 | desc, mdname(mddev), |
| 8736 | mdname(mddev2)); |
Artur Paszkiewicz | c622ca5 | 2016-08-16 14:26:08 +0200 | [diff] [blame] | 8737 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8738 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 8739 | if (signal_pending(current)) |
| 8740 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8741 | schedule(); |
| 8742 | finish_wait(&resync_wait, &wq); |
| 8743 | goto try_again; |
| 8744 | } |
| 8745 | finish_wait(&resync_wait, &wq); |
| 8746 | } |
| 8747 | } |
| 8748 | } while (mddev->curr_resync < 2); |
| 8749 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8750 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8751 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8752 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8753 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8754 | */ |
| 8755 | max_sectors = mddev->resync_max_sectors; |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 8756 | atomic64_set(&mddev->resync_mismatches, 0); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8757 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 8758 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 8759 | j = mddev->resync_min; |
| 8760 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8761 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 8762 | |
Guoqing Jiang | cb9ee15 | 2018-10-18 16:37:47 +0800 | [diff] [blame] | 8763 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) { |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 8764 | max_sectors = mddev->resync_max_sectors; |
Guoqing Jiang | cb9ee15 | 2018-10-18 16:37:47 +0800 | [diff] [blame] | 8765 | /* |
| 8766 | * If the original node aborts reshaping then we continue the |
| 8767 | * reshaping, so set j again to avoid restart reshape from the |
| 8768 | * first beginning |
| 8769 | */ |
| 8770 | if (mddev_is_clustered(mddev) && |
| 8771 | mddev->reshape_position != MaxSector) |
| 8772 | j = mddev->reshape_position; |
| 8773 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8774 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8775 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8776 | j = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 8777 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 8778 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8779 | if (rdev->raid_disk >= 0 && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 8780 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8781 | !test_bit(Faulty, &rdev->flags) && |
| 8782 | !test_bit(In_sync, &rdev->flags) && |
| 8783 | rdev->recovery_offset < j) |
| 8784 | j = rdev->recovery_offset; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 8785 | rcu_read_unlock(); |
NeilBrown | 133d452 | 2014-07-02 12:04:14 +1000 | [diff] [blame] | 8786 | |
| 8787 | /* If there is a bitmap, we need to make sure all |
| 8788 | * writes that started before we added a spare |
| 8789 | * complete before we start doing a recovery. |
| 8790 | * Otherwise the write might complete and (via |
| 8791 | * bitmap_endwrite) set a bit in the bitmap after the |
| 8792 | * recovery has checked that bit and skipped that |
| 8793 | * region. |
| 8794 | */ |
| 8795 | if (mddev->bitmap) { |
| 8796 | mddev->pers->quiesce(mddev, 1); |
| 8797 | mddev->pers->quiesce(mddev, 0); |
| 8798 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8799 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8800 | |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8801 | pr_info("md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 8802 | pr_debug("md: minimum _guaranteed_ speed: %d KB/sec/disk.\n", speed_min(mddev)); |
| 8803 | pr_debug("md: using maximum available idle IO bandwidth (but not more than %d KB/sec) for %s.\n", |
| 8804 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8805 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8806 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8807 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8808 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8809 | for (m = 0; m < SYNC_MARKS; m++) { |
| 8810 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8811 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8812 | } |
| 8813 | last_mark = 0; |
| 8814 | mddev->resync_mark = mark[last_mark]; |
| 8815 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 8816 | |
| 8817 | /* |
| 8818 | * Tune reconstruction: |
| 8819 | */ |
Yufen Yu | e5b521e | 2019-06-14 15:41:07 -0700 | [diff] [blame] | 8820 | window = 32 * (PAGE_SIZE / 512); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8821 | pr_debug("md: using %dk window, over a total of %lluk.\n", |
| 8822 | window/2, (unsigned long long)max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8823 | |
| 8824 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8825 | last_check = 0; |
| 8826 | |
| 8827 | if (j>2) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8828 | pr_debug("md: resuming %s of %s from checkpoint.\n", |
| 8829 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8830 | mddev->curr_resync = j; |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 8831 | } else |
| 8832 | mddev->curr_resync = 3; /* no longer delayed */ |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 8833 | mddev->curr_resync_completed = j; |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 8834 | sysfs_notify_dirent_safe(mddev->sysfs_completed); |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 8835 | md_new_event(mddev); |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 8836 | update_time = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8837 | |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 8838 | blk_start_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8839 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8840 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8841 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8842 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8843 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 8844 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 8845 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 8846 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 8847 | > (max_sectors >> 4)) || |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 8848 | time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) || |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 8849 | (j - mddev->curr_resync_completed)*2 |
NeilBrown | c5e19d9 | 2015-07-17 12:06:02 +1000 | [diff] [blame] | 8850 | >= mddev->resync_max - mddev->curr_resync_completed || |
| 8851 | mddev->curr_resync_completed > mddev->resync_max |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 8852 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8853 | /* time to update curr_resync_completed */ |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8854 | wait_event(mddev->recovery_wait, |
| 8855 | atomic_read(&mddev->recovery_active) == 0); |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 8856 | mddev->curr_resync_completed = j; |
kernelmail | 35d78c6 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 8857 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && |
| 8858 | j > mddev->recovery_cp) |
| 8859 | mddev->recovery_cp = j; |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 8860 | update_time = jiffies; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 8861 | set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags); |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 8862 | sysfs_notify_dirent_safe(mddev->sysfs_completed); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8863 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 8864 | |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8865 | while (j >= mddev->resync_max && |
| 8866 | !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 8867 | /* As this condition is controlled by user-space, |
| 8868 | * we can block indefinitely, so use '_interruptible' |
| 8869 | * to avoid triggering warnings. |
| 8870 | */ |
| 8871 | flush_signals(current); /* just in case */ |
| 8872 | wait_event_interruptible(mddev->recovery_wait, |
| 8873 | mddev->resync_max > j |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8874 | || test_bit(MD_RECOVERY_INTR, |
| 8875 | &mddev->recovery)); |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 8876 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 8877 | |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8878 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 8879 | break; |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 8880 | |
NeilBrown | 0931479 | 2015-02-19 16:04:40 +1100 | [diff] [blame] | 8881 | sectors = mddev->pers->sync_request(mddev, j, &skipped); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8882 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 8883 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8884 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8885 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8886 | |
| 8887 | if (!skipped) { /* actual IO requested */ |
| 8888 | io_sectors += sectors; |
| 8889 | atomic_add(sectors, &mddev->recovery_active); |
| 8890 | } |
| 8891 | |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 8892 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 8893 | break; |
| 8894 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8895 | j += sectors; |
NeilBrown | 5ed1df2 | 2015-07-24 13:27:08 +1000 | [diff] [blame] | 8896 | if (j > max_sectors) |
| 8897 | /* when skipping, extra large numbers can be returned. */ |
| 8898 | j = max_sectors; |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 8899 | if (j > 2) |
| 8900 | mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 8901 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 8902 | if (last_check == 0) |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 8903 | /* this is the earliest that rebuild will be |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 8904 | * visible in /proc/mdstat |
| 8905 | */ |
| 8906 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8907 | |
| 8908 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8909 | continue; |
| 8910 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8911 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8912 | repeat: |
| 8913 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 8914 | /* step marks */ |
| 8915 | int next = (last_mark+1) % SYNC_MARKS; |
| 8916 | |
| 8917 | mddev->resync_mark = mark[next]; |
| 8918 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 8919 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8920 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8921 | last_mark = next; |
| 8922 | } |
| 8923 | |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8924 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 8925 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8926 | |
| 8927 | /* |
| 8928 | * this loop exits only if either when we are slower than |
| 8929 | * the 'hard' speed limit, or the system was IO-idle for |
| 8930 | * a jiffy. |
| 8931 | * the system might be non-idle CPU-wise, but we only care |
| 8932 | * about not overloading the IO subsystem. (things like an |
| 8933 | * e2fsck being done on the RAID array should execute fast) |
| 8934 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8935 | cond_resched(); |
| 8936 | |
Xiao Ni | ac7e50a | 2014-08-07 09:37:41 -0400 | [diff] [blame] | 8937 | recovery_done = io_sectors - atomic_read(&mddev->recovery_active); |
| 8938 | currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2 |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8939 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8940 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 8941 | if (currspeed > speed_min(mddev)) { |
NeilBrown | ac8fa41 | 2015-02-19 16:55:00 +1100 | [diff] [blame] | 8942 | if (currspeed > speed_max(mddev)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 8943 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8944 | goto repeat; |
| 8945 | } |
NeilBrown | ac8fa41 | 2015-02-19 16:55:00 +1100 | [diff] [blame] | 8946 | if (!is_mddev_idle(mddev, 0)) { |
| 8947 | /* |
| 8948 | * Give other IO more of a chance. |
| 8949 | * The faster the devices, the less we wait. |
| 8950 | */ |
| 8951 | wait_event(mddev->recovery_wait, |
| 8952 | !atomic_read(&mddev->recovery_active)); |
| 8953 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8954 | } |
| 8955 | } |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8956 | pr_info("md: %s: %s %s.\n",mdname(mddev), desc, |
| 8957 | test_bit(MD_RECOVERY_INTR, &mddev->recovery) |
| 8958 | ? "interrupted" : "done"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8959 | /* |
| 8960 | * this also signals 'finished resyncing' to md_stop |
| 8961 | */ |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 8962 | blk_finish_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8963 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 8964 | |
NeilBrown | 5ed1df2 | 2015-07-24 13:27:08 +1000 | [diff] [blame] | 8965 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 8966 | !test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
NeilBrown | 1217e1d | 2016-10-28 15:59:41 +1100 | [diff] [blame] | 8967 | mddev->curr_resync > 3) { |
NeilBrown | 5ed1df2 | 2015-07-24 13:27:08 +1000 | [diff] [blame] | 8968 | mddev->curr_resync_completed = mddev->curr_resync; |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 8969 | sysfs_notify_dirent_safe(mddev->sysfs_completed); |
NeilBrown | 5ed1df2 | 2015-07-24 13:27:08 +1000 | [diff] [blame] | 8970 | } |
NeilBrown | 0931479 | 2015-02-19 16:04:40 +1100 | [diff] [blame] | 8971 | mddev->pers->sync_request(mddev, max_sectors, &skipped); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8972 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 8973 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 1217e1d | 2016-10-28 15:59:41 +1100 | [diff] [blame] | 8974 | mddev->curr_resync > 3) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8975 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 8976 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 8977 | if (mddev->curr_resync >= mddev->recovery_cp) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8978 | pr_debug("md: checkpointing %s of %s.\n", |
| 8979 | desc, mdname(mddev)); |
majianpeng | 0a19caa | 2012-11-19 19:57:34 +0800 | [diff] [blame] | 8980 | if (test_bit(MD_RECOVERY_ERROR, |
| 8981 | &mddev->recovery)) |
| 8982 | mddev->recovery_cp = |
| 8983 | mddev->curr_resync_completed; |
| 8984 | else |
| 8985 | mddev->recovery_cp = |
| 8986 | mddev->curr_resync; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8987 | } |
| 8988 | } else |
| 8989 | mddev->recovery_cp = MaxSector; |
| 8990 | } else { |
| 8991 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 8992 | mddev->curr_resync = MaxSector; |
NeilBrown | db0505d | 2017-10-17 16:18:36 +1100 | [diff] [blame] | 8993 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 8994 | test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) { |
| 8995 | rcu_read_lock(); |
| 8996 | rdev_for_each_rcu(rdev, mddev) |
| 8997 | if (rdev->raid_disk >= 0 && |
| 8998 | mddev->delta_disks >= 0 && |
| 8999 | !test_bit(Journal, &rdev->flags) && |
| 9000 | !test_bit(Faulty, &rdev->flags) && |
| 9001 | !test_bit(In_sync, &rdev->flags) && |
| 9002 | rdev->recovery_offset < mddev->curr_resync) |
| 9003 | rdev->recovery_offset = mddev->curr_resync; |
| 9004 | rcu_read_unlock(); |
| 9005 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 9006 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9007 | } |
NeilBrown | db91ff5 | 2012-02-07 12:01:51 +1100 | [diff] [blame] | 9008 | skip: |
Guoqing Jiang | bb8bf15 | 2016-06-02 23:32:04 -0400 | [diff] [blame] | 9009 | /* set CHANGE_PENDING here since maybe another update is needed, |
| 9010 | * so other nodes are informed. It should be harmless for normal |
| 9011 | * raid */ |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 9012 | set_mask_bits(&mddev->sb_flags, 0, |
| 9013 | BIT(MD_SB_CHANGE_PENDING) | BIT(MD_SB_CHANGE_DEVS)); |
Goldwyn Rodrigues | c186b12 | 2015-09-30 13:20:35 -0500 | [diff] [blame] | 9014 | |
BingJing Chang | 8876391 | 2018-02-22 13:34:46 +0800 | [diff] [blame] | 9015 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 9016 | !test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 9017 | mddev->delta_disks > 0 && |
| 9018 | mddev->pers->finish_reshape && |
| 9019 | mddev->pers->size && |
| 9020 | mddev->queue) { |
| 9021 | mddev_lock_nointr(mddev); |
| 9022 | md_set_array_sectors(mddev, mddev->pers->size(mddev, 0, 0)); |
| 9023 | mddev_unlock(mddev); |
Christoph Hellwig | 2c247c5 | 2020-11-16 15:57:11 +0100 | [diff] [blame] | 9024 | if (!mddev_is_clustered(mddev)) |
| 9025 | set_capacity_and_notify(mddev->gendisk, |
| 9026 | mddev->array_sectors); |
BingJing Chang | 8876391 | 2018-02-22 13:34:46 +0800 | [diff] [blame] | 9027 | } |
| 9028 | |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 9029 | spin_lock(&mddev->lock); |
NeilBrown | c07b70a | 2009-12-14 12:49:48 +1100 | [diff] [blame] | 9030 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 9031 | /* We completed so min/max setting can be forgotten if used. */ |
| 9032 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 9033 | mddev->resync_min = 0; |
| 9034 | mddev->resync_max = MaxSector; |
| 9035 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 9036 | mddev->resync_min = mddev->curr_resync_completed; |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 9037 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9038 | mddev->curr_resync = 0; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 9039 | spin_unlock(&mddev->lock); |
| 9040 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9041 | wake_up(&resync_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9042 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 9043 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9044 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 9045 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9046 | |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 9047 | static int remove_and_add_spares(struct mddev *mddev, |
| 9048 | struct md_rdev *this) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9049 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 9050 | struct md_rdev *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9051 | int spares = 0; |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 9052 | int removed = 0; |
NeilBrown | d787be4 | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 9053 | bool remove_some = false; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9054 | |
NeilBrown | 39772f0 | 2018-02-03 09:19:30 +1100 | [diff] [blame] | 9055 | if (this && test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 9056 | /* Mustn't remove devices when resync thread is running */ |
| 9057 | return 0; |
| 9058 | |
NeilBrown | d787be4 | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 9059 | rdev_for_each(rdev, mddev) { |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 9060 | if ((this == NULL || rdev == this) && |
| 9061 | rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 9062 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | d787be4 | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 9063 | test_bit(Faulty, &rdev->flags) && |
| 9064 | atomic_read(&rdev->nr_pending)==0) { |
| 9065 | /* Faulty non-Blocked devices with nr_pending == 0 |
| 9066 | * never get nr_pending incremented, |
| 9067 | * never get Faulty cleared, and never get Blocked set. |
| 9068 | * So we can synchronize_rcu now rather than once per device |
| 9069 | */ |
| 9070 | remove_some = true; |
| 9071 | set_bit(RemoveSynchronized, &rdev->flags); |
| 9072 | } |
| 9073 | } |
| 9074 | |
| 9075 | if (remove_some) |
| 9076 | synchronize_rcu(); |
| 9077 | rdev_for_each(rdev, mddev) { |
| 9078 | if ((this == NULL || rdev == this) && |
| 9079 | rdev->raid_disk >= 0 && |
| 9080 | !test_bit(Blocked, &rdev->flags) && |
| 9081 | ((test_bit(RemoveSynchronized, &rdev->flags) || |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 9082 | (!test_bit(In_sync, &rdev->flags) && |
| 9083 | !test_bit(Journal, &rdev->flags))) && |
NeilBrown | d787be4 | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 9084 | atomic_read(&rdev->nr_pending)==0)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9085 | if (mddev->pers->hot_remove_disk( |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 9086 | mddev, rdev) == 0) { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 9087 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | 011abdc | 2018-04-26 14:46:29 +1000 | [diff] [blame] | 9088 | rdev->saved_raid_disk = rdev->raid_disk; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9089 | rdev->raid_disk = -1; |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 9090 | removed++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9091 | } |
| 9092 | } |
NeilBrown | d787be4 | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 9093 | if (remove_some && test_bit(RemoveSynchronized, &rdev->flags)) |
| 9094 | clear_bit(RemoveSynchronized, &rdev->flags); |
| 9095 | } |
| 9096 | |
Jonathan Brassow | 90584fc | 2013-03-07 16:24:26 -0600 | [diff] [blame] | 9097 | if (removed && mddev->kobj.sd) |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 9098 | sysfs_notify_dirent_safe(mddev->sysfs_degraded); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9099 | |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 9100 | if (this && removed) |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 9101 | goto no_add; |
| 9102 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 9103 | rdev_for_each(rdev, mddev) { |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 9104 | if (this && this != rdev) |
| 9105 | continue; |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 9106 | if (test_bit(Candidate, &rdev->flags)) |
| 9107 | continue; |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 9108 | if (rdev->raid_disk >= 0 && |
| 9109 | !test_bit(In_sync, &rdev->flags) && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 9110 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 9111 | !test_bit(Faulty, &rdev->flags)) |
| 9112 | spares++; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 9113 | if (rdev->raid_disk >= 0) |
| 9114 | continue; |
| 9115 | if (test_bit(Faulty, &rdev->flags)) |
| 9116 | continue; |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 9117 | if (!test_bit(Journal, &rdev->flags)) { |
| 9118 | if (mddev->ro && |
| 9119 | ! (rdev->saved_raid_disk >= 0 && |
| 9120 | !test_bit(Bitmap_sync, &rdev->flags))) |
| 9121 | continue; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 9122 | |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 9123 | rdev->recovery_offset = 0; |
| 9124 | } |
Guoqing Jiang | 3f79cc2 | 2020-04-04 23:57:11 +0200 | [diff] [blame] | 9125 | if (mddev->pers->hot_add_disk(mddev, rdev) == 0) { |
Damien Le Moal | 5e3b8a8 | 2020-07-16 13:54:40 +0900 | [diff] [blame] | 9126 | /* failure here is OK */ |
| 9127 | sysfs_link_rdev(mddev, rdev); |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 9128 | if (!test_bit(Journal, &rdev->flags)) |
| 9129 | spares++; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 9130 | md_new_event(mddev); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 9131 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 9132 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9133 | } |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 9134 | no_add: |
NeilBrown | 6dafab6 | 2012-09-19 12:54:22 +1000 | [diff] [blame] | 9135 | if (removed) |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 9136 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9137 | return spares; |
| 9138 | } |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 9139 | |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 9140 | static void md_start_sync(struct work_struct *ws) |
| 9141 | { |
| 9142 | struct mddev *mddev = container_of(ws, struct mddev, del_work); |
Goldwyn Rodrigues | c186b12 | 2015-09-30 13:20:35 -0500 | [diff] [blame] | 9143 | |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 9144 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 9145 | mddev, |
| 9146 | "resync"); |
| 9147 | if (!mddev->sync_thread) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 9148 | pr_warn("%s: could not start resync thread...\n", |
| 9149 | mdname(mddev)); |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 9150 | /* leave the spares where they are, it shouldn't hurt */ |
| 9151 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 9152 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 9153 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 9154 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 9155 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 9156 | wake_up(&resync_wait); |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 9157 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 9158 | &mddev->recovery)) |
| 9159 | if (mddev->sysfs_action) |
| 9160 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 9161 | } else |
| 9162 | md_wakeup_thread(mddev->sync_thread); |
| 9163 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 9164 | md_new_event(mddev); |
| 9165 | } |
| 9166 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9167 | /* |
| 9168 | * This routine is regularly called by all per-raid-array threads to |
| 9169 | * deal with generic issues like resync and super-block update. |
| 9170 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 9171 | * need this as they never do any recovery or update the superblock. |
| 9172 | * |
| 9173 | * It does not do any resync itself, but rather "forks" off other threads |
| 9174 | * to do that as needed. |
| 9175 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 9176 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 9177 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9178 | * and wakeups up this thread which will reap the thread and finish up. |
| 9179 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 9180 | * |
| 9181 | * The overall approach is: |
| 9182 | * 1/ if the superblock needs updating, update it. |
| 9183 | * 2/ If a recovery thread is running, don't do anything else. |
| 9184 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 9185 | * 4/ If there are any faulty devices, remove them. |
| 9186 | * 5/ If array is degraded, try to add spares devices |
| 9187 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 9188 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 9189 | void md_check_recovery(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9190 | { |
NeilBrown | 059421e | 2018-10-03 15:04:41 +1000 | [diff] [blame] | 9191 | if (test_bit(MD_ALLOW_SB_UPDATE, &mddev->flags) && mddev->sb_flags) { |
| 9192 | /* Write superblock - thread that called mddev_suspend() |
| 9193 | * holds reconfig_mutex for us. |
| 9194 | */ |
| 9195 | set_bit(MD_UPDATING_SB, &mddev->flags); |
| 9196 | smp_mb__after_atomic(); |
| 9197 | if (test_bit(MD_ALLOW_SB_UPDATE, &mddev->flags)) |
| 9198 | md_update_sb(mddev, 0); |
| 9199 | clear_bit_unlock(MD_UPDATING_SB, &mddev->flags); |
| 9200 | wake_up(&mddev->sb_wait); |
| 9201 | } |
| 9202 | |
Jonathan Brassow | 68866e4 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 9203 | if (mddev->suspended) |
| 9204 | return; |
| 9205 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 9206 | if (mddev->bitmap) |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 9207 | md_bitmap_daemon_work(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9208 | |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 9209 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 9210 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 9211 | pr_debug("md: %s in immediate safe mode\n", |
| 9212 | mdname(mddev)); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 9213 | mddev->safemode = 2; |
| 9214 | } |
| 9215 | flush_signals(current); |
| 9216 | } |
| 9217 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 9218 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 9219 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9220 | if ( ! ( |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 9221 | (mddev->sb_flags & ~ (1<<MD_SB_CHANGE_PENDING)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9222 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 9223 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 9224 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 9225 | (mddev->safemode == 2 |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 9226 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9227 | )) |
| 9228 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 9229 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 9230 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9231 | int spares = 0; |
NeilBrown | 480523f | 2019-08-20 10:21:09 +1000 | [diff] [blame] | 9232 | bool try_set_sync = mddev->safemode != 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 9233 | |
Shaohua Li | afc1f55 | 2017-08-11 20:34:45 -0700 | [diff] [blame] | 9234 | if (!mddev->external && mddev->safemode == 1) |
NeilBrown | 33182d1 | 2017-08-08 16:56:36 +1000 | [diff] [blame] | 9235 | mddev->safemode = 0; |
| 9236 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 9237 | if (mddev->ro) { |
Neil Brown | ab16bfc | 2015-06-17 12:31:46 +1000 | [diff] [blame] | 9238 | struct md_rdev *rdev; |
| 9239 | if (!mddev->external && mddev->in_sync) |
| 9240 | /* 'Blocked' flag not needed as failed devices |
| 9241 | * will be recorded if array switched to read/write. |
| 9242 | * Leaving it set will prevent the device |
| 9243 | * from being removed. |
| 9244 | */ |
| 9245 | rdev_for_each(rdev, mddev) |
| 9246 | clear_bit(Blocked, &rdev->flags); |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 9247 | /* On a read-only array we can: |
| 9248 | * - remove failed devices |
| 9249 | * - add already-in_sync devices if the array itself |
| 9250 | * is in-sync. |
| 9251 | * As we only add devices that are already in-sync, |
| 9252 | * we can activate the spares immediately. |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 9253 | */ |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 9254 | remove_and_add_spares(mddev, NULL); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 9255 | /* There is no thread, but we need to call |
| 9256 | * ->spare_active and clear saved_raid_disk |
| 9257 | */ |
NeilBrown | 2ac295a | 2014-05-29 11:40:03 +1000 | [diff] [blame] | 9258 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 9259 | md_reap_sync_thread(mddev); |
NeilBrown | a4a3d26 | 2015-07-17 11:57:30 +1000 | [diff] [blame] | 9260 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 9261 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 9262 | clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags); |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 9263 | goto unlock; |
| 9264 | } |
| 9265 | |
Guoqing Jiang | 659b254 | 2015-12-21 10:50:59 +1100 | [diff] [blame] | 9266 | if (mddev_is_clustered(mddev)) { |
| 9267 | struct md_rdev *rdev; |
| 9268 | /* kick the device if another node issued a |
| 9269 | * remove disk. |
| 9270 | */ |
| 9271 | rdev_for_each(rdev, mddev) { |
| 9272 | if (test_and_clear_bit(ClusterRemove, &rdev->flags) && |
| 9273 | rdev->raid_disk < 0) |
| 9274 | md_kick_rdev_from_array(rdev); |
| 9275 | } |
| 9276 | } |
| 9277 | |
NeilBrown | 480523f | 2019-08-20 10:21:09 +1000 | [diff] [blame] | 9278 | if (try_set_sync && !mddev->external && !mddev->in_sync) { |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 9279 | spin_lock(&mddev->lock); |
NeilBrown | 6497709b | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 9280 | set_in_sync(mddev); |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 9281 | spin_unlock(&mddev->lock); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 9282 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 9283 | |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 9284 | if (mddev->sb_flags) |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 9285 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 9286 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9287 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 9288 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 9289 | /* resync/recovery still happening */ |
| 9290 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 9291 | goto unlock; |
| 9292 | } |
| 9293 | if (mddev->sync_thread) { |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9294 | md_reap_sync_thread(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9295 | goto unlock; |
| 9296 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 9297 | /* Set RUNNING before clearing NEEDED to avoid |
| 9298 | * any transients in the value of "sync_action". |
| 9299 | */ |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 9300 | mddev->curr_resync_completed = 0; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 9301 | spin_lock(&mddev->lock); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 9302 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 9303 | spin_unlock(&mddev->lock); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 9304 | /* Clear some bits that don't mean anything, but |
| 9305 | * might be left set |
| 9306 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 9307 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 9308 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9309 | |
NeilBrown | ed20958 | 2012-04-24 10:23:14 +1000 | [diff] [blame] | 9310 | if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
| 9311 | test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 9312 | goto not_running; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9313 | /* no recovery is running. |
| 9314 | * remove any failed drives, then |
| 9315 | * add spares if possible. |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 9316 | * Spares are also removed and re-added, to allow |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9317 | * the personality to fail the re-add. |
| 9318 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9319 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9320 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 9321 | if (mddev->pers->check_reshape == NULL || |
| 9322 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9323 | /* Cannot proceed */ |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 9324 | goto not_running; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9325 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 9326 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 9327 | } else if ((spares = remove_and_add_spares(mddev, NULL))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 9328 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 9329 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 9330 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 9331 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 9332 | } else if (mddev->recovery_cp < MaxSector) { |
| 9333 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 9334 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 9335 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 9336 | /* nothing to be done ... */ |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 9337 | goto not_running; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 9338 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9339 | if (mddev->pers->sync_request) { |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 9340 | if (spares) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 9341 | /* We are adding a device or devices to an array |
| 9342 | * which has the bitmap stored on all devices. |
| 9343 | * So make sure all bitmap pages get written |
| 9344 | */ |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 9345 | md_bitmap_write_all(mddev->bitmap); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 9346 | } |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 9347 | INIT_WORK(&mddev->del_work, md_start_sync); |
| 9348 | queue_work(md_misc_wq, &mddev->del_work); |
| 9349 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9350 | } |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 9351 | not_running: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 9352 | if (!mddev->sync_thread) { |
| 9353 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 9354 | wake_up(&resync_wait); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 9355 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 9356 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 9357 | if (mddev->sysfs_action) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 9358 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 9359 | } |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 9360 | unlock: |
| 9361 | wake_up(&mddev->sb_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9362 | mddev_unlock(mddev); |
| 9363 | } |
| 9364 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 9365 | EXPORT_SYMBOL(md_check_recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9366 | |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9367 | void md_reap_sync_thread(struct mddev *mddev) |
| 9368 | { |
| 9369 | struct md_rdev *rdev; |
Guoqing Jiang | aefb2e5 | 2018-10-18 16:37:44 +0800 | [diff] [blame] | 9370 | sector_t old_dev_sectors = mddev->dev_sectors; |
| 9371 | bool is_reshaped = false; |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9372 | |
| 9373 | /* resync has finished, collect result */ |
| 9374 | md_unregister_thread(&mddev->sync_thread); |
| 9375 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
Guoqing Jiang | 0d8ed0e9 | 2019-07-24 11:09:21 +0200 | [diff] [blame] | 9376 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) && |
| 9377 | mddev->degraded != mddev->raid_disks) { |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9378 | /* success...*/ |
| 9379 | /* activate any spares */ |
| 9380 | if (mddev->pers->spare_active(mddev)) { |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 9381 | sysfs_notify_dirent_safe(mddev->sysfs_degraded); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 9382 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9383 | } |
| 9384 | } |
| 9385 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
Guoqing Jiang | aefb2e5 | 2018-10-18 16:37:44 +0800 | [diff] [blame] | 9386 | mddev->pers->finish_reshape) { |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9387 | mddev->pers->finish_reshape(mddev); |
Guoqing Jiang | aefb2e5 | 2018-10-18 16:37:44 +0800 | [diff] [blame] | 9388 | if (mddev_is_clustered(mddev)) |
| 9389 | is_reshaped = true; |
| 9390 | } |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9391 | |
| 9392 | /* If array is no-longer degraded, then any saved_raid_disk |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 9393 | * information must be scrapped. |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9394 | */ |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 9395 | if (!mddev->degraded) |
| 9396 | rdev_for_each(rdev, mddev) |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9397 | rdev->saved_raid_disk = -1; |
| 9398 | |
| 9399 | md_update_sb(mddev, 1); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 9400 | /* 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] | 9401 | * call resync_finish here if MD_CLUSTER_RESYNC_LOCKED is set by |
| 9402 | * clustered raid */ |
| 9403 | if (test_and_clear_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags)) |
| 9404 | md_cluster_ops->resync_finish(mddev); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9405 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | ea358cd | 2015-06-12 20:05:04 +1000 | [diff] [blame] | 9406 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9407 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 9408 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 9409 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 9410 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Guoqing Jiang | aefb2e5 | 2018-10-18 16:37:44 +0800 | [diff] [blame] | 9411 | /* |
| 9412 | * We call md_cluster_ops->update_size here because sync_size could |
| 9413 | * be changed by md_update_sb, and MD_RECOVERY_RESHAPE is cleared, |
| 9414 | * so it is time to update size across cluster. |
| 9415 | */ |
| 9416 | if (mddev_is_clustered(mddev) && is_reshaped |
| 9417 | && !test_bit(MD_CLOSING, &mddev->flags)) |
| 9418 | md_cluster_ops->update_size(mddev, old_dev_sectors); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 9419 | wake_up(&resync_wait); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9420 | /* flag recovery needed just to double check */ |
| 9421 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 9422 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 9423 | md_new_event(mddev); |
| 9424 | if (mddev->event_work.func) |
| 9425 | queue_work(md_misc_wq, &mddev->event_work); |
| 9426 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 9427 | EXPORT_SYMBOL(md_reap_sync_thread); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9428 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 9429 | 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] | 9430 | { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 9431 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 9432 | wait_event_timeout(rdev->blocked_wait, |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 9433 | !test_bit(Blocked, &rdev->flags) && |
| 9434 | !test_bit(BlockedBadBlocks, &rdev->flags), |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 9435 | msecs_to_jiffies(5000)); |
| 9436 | rdev_dec_pending(rdev, mddev); |
| 9437 | } |
| 9438 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 9439 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 9440 | void md_finish_reshape(struct mddev *mddev) |
| 9441 | { |
| 9442 | /* called be personality module when reshape completes. */ |
| 9443 | struct md_rdev *rdev; |
| 9444 | |
| 9445 | rdev_for_each(rdev, mddev) { |
| 9446 | if (rdev->data_offset > rdev->new_data_offset) |
| 9447 | rdev->sectors += rdev->data_offset - rdev->new_data_offset; |
| 9448 | else |
| 9449 | rdev->sectors -= rdev->new_data_offset - rdev->data_offset; |
| 9450 | rdev->data_offset = rdev->new_data_offset; |
| 9451 | } |
| 9452 | } |
| 9453 | EXPORT_SYMBOL(md_finish_reshape); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 9454 | |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 9455 | /* Bad block management */ |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 9456 | |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 9457 | /* Returns 1 on success, 0 on failure */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 9458 | int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 9459 | int is_new) |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 9460 | { |
Guoqing Jiang | 85ad1d1 | 2016-05-03 22:22:13 -0400 | [diff] [blame] | 9461 | struct mddev *mddev = rdev->mddev; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 9462 | int rv; |
| 9463 | if (is_new) |
| 9464 | s += rdev->new_data_offset; |
| 9465 | else |
| 9466 | s += rdev->data_offset; |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 9467 | rv = badblocks_set(&rdev->badblocks, s, sectors, 0); |
| 9468 | if (rv == 0) { |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 9469 | /* Make sure they get written out promptly */ |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 9470 | if (test_bit(ExternalBbl, &rdev->flags)) |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 9471 | sysfs_notify_dirent_safe(rdev->sysfs_unack_badblocks); |
NeilBrown | 8bd2f0a | 2011-12-08 16:26:08 +1100 | [diff] [blame] | 9472 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 9473 | set_mask_bits(&mddev->sb_flags, 0, |
| 9474 | BIT(MD_SB_CHANGE_CLEAN) | BIT(MD_SB_CHANGE_PENDING)); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 9475 | md_wakeup_thread(rdev->mddev->thread); |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 9476 | return 1; |
| 9477 | } else |
| 9478 | return 0; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 9479 | } |
| 9480 | EXPORT_SYMBOL_GPL(rdev_set_badblocks); |
| 9481 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 9482 | int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
| 9483 | int is_new) |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 9484 | { |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 9485 | int rv; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 9486 | if (is_new) |
| 9487 | s += rdev->new_data_offset; |
| 9488 | else |
| 9489 | s += rdev->data_offset; |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 9490 | rv = badblocks_clear(&rdev->badblocks, s, sectors); |
| 9491 | if ((rv == 0) && test_bit(ExternalBbl, &rdev->flags)) |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 9492 | sysfs_notify_dirent_safe(rdev->sysfs_badblocks); |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 9493 | return rv; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 9494 | } |
| 9495 | EXPORT_SYMBOL_GPL(rdev_clear_badblocks); |
| 9496 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 9497 | static int md_notify_reboot(struct notifier_block *this, |
| 9498 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9499 | { |
| 9500 | struct list_head *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 9501 | struct mddev *mddev; |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 9502 | int need_delay = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9503 | |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 9504 | for_each_mddev(mddev, tmp) { |
| 9505 | if (mddev_trylock(mddev)) { |
NeilBrown | 30b8aa9 | 2012-04-24 10:23:16 +1000 | [diff] [blame] | 9506 | if (mddev->pers) |
| 9507 | __md_stop_writes(mddev); |
NeilBrown | 0f62fb2 | 2014-05-06 09:36:08 +1000 | [diff] [blame] | 9508 | if (mddev->persistent) |
| 9509 | mddev->safemode = 2; |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 9510 | mddev_unlock(mddev); |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 9511 | } |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 9512 | need_delay = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9513 | } |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 9514 | /* |
| 9515 | * certain more exotic SCSI devices are known to be |
| 9516 | * volatile wrt too early system reboots. While the |
| 9517 | * right place to handle this issue is the given |
| 9518 | * driver, we do want to have a safe RAID driver ... |
| 9519 | */ |
| 9520 | if (need_delay) |
| 9521 | mdelay(1000*1); |
| 9522 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9523 | return NOTIFY_DONE; |
| 9524 | } |
| 9525 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 9526 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9527 | .notifier_call = md_notify_reboot, |
| 9528 | .next = NULL, |
| 9529 | .priority = INT_MAX, /* before any real devices */ |
| 9530 | }; |
| 9531 | |
| 9532 | static void md_geninit(void) |
| 9533 | { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 9534 | 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] | 9535 | |
Alexey Dobriyan | 97a3253 | 2020-02-03 17:37:17 -0800 | [diff] [blame] | 9536 | proc_create("mdstat", S_IRUGO, NULL, &mdstat_proc_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9537 | } |
| 9538 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 9539 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9540 | { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 9541 | int ret = -ENOMEM; |
| 9542 | |
Tejun Heo | ada609e | 2011-01-25 14:35:54 +0100 | [diff] [blame] | 9543 | md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 9544 | if (!md_wq) |
| 9545 | goto err_wq; |
| 9546 | |
| 9547 | md_misc_wq = alloc_workqueue("md_misc", 0, 0); |
| 9548 | if (!md_misc_wq) |
| 9549 | goto err_misc_wq; |
| 9550 | |
Guoqing Jiang | cc1ffe6 | 2020-04-04 23:57:08 +0200 | [diff] [blame] | 9551 | md_rdev_misc_wq = alloc_workqueue("md_rdev_misc", 0, 0); |
Guoqing Jiang | cf0b9b4 | 2020-10-08 05:19:09 +0200 | [diff] [blame] | 9552 | if (!md_rdev_misc_wq) |
Guoqing Jiang | cc1ffe6 | 2020-04-04 23:57:08 +0200 | [diff] [blame] | 9553 | goto err_rdev_misc_wq; |
| 9554 | |
Christoph Hellwig | 28144f9 | 2020-10-29 15:58:34 +0100 | [diff] [blame] | 9555 | ret = __register_blkdev(MD_MAJOR, "md", md_probe); |
| 9556 | if (ret < 0) |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 9557 | goto err_md; |
| 9558 | |
Christoph Hellwig | 28144f9 | 2020-10-29 15:58:34 +0100 | [diff] [blame] | 9559 | ret = __register_blkdev(0, "mdp", md_probe); |
| 9560 | if (ret < 0) |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 9561 | goto err_mdp; |
| 9562 | mdp_major = ret; |
| 9563 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9564 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 9565 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9566 | |
| 9567 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 9568 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9569 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 9570 | err_mdp: |
| 9571 | unregister_blkdev(MD_MAJOR, "md"); |
| 9572 | err_md: |
Guoqing Jiang | cc1ffe6 | 2020-04-04 23:57:08 +0200 | [diff] [blame] | 9573 | destroy_workqueue(md_rdev_misc_wq); |
| 9574 | err_rdev_misc_wq: |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 9575 | destroy_workqueue(md_misc_wq); |
| 9576 | err_misc_wq: |
| 9577 | destroy_workqueue(md_wq); |
| 9578 | err_wq: |
| 9579 | return ret; |
| 9580 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9581 | |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9582 | static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev) |
Goldwyn Rodrigues | 1d7e3e9 | 2014-06-07 01:53:00 -0500 | [diff] [blame] | 9583 | { |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9584 | struct mdp_superblock_1 *sb = page_address(rdev->sb_page); |
| 9585 | struct md_rdev *rdev2; |
| 9586 | int role, ret; |
| 9587 | char b[BDEVNAME_SIZE]; |
Goldwyn Rodrigues | 1d7e3e9 | 2014-06-07 01:53:00 -0500 | [diff] [blame] | 9588 | |
Guoqing Jiang | 818da59 | 2017-03-01 16:42:40 +0800 | [diff] [blame] | 9589 | /* |
| 9590 | * If size is changed in another node then we need to |
| 9591 | * do resize as well. |
| 9592 | */ |
| 9593 | if (mddev->dev_sectors != le64_to_cpu(sb->size)) { |
| 9594 | ret = mddev->pers->resize(mddev, le64_to_cpu(sb->size)); |
| 9595 | if (ret) |
| 9596 | pr_info("md-cluster: resize failed\n"); |
| 9597 | else |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 9598 | md_bitmap_update_sb(mddev->bitmap); |
Guoqing Jiang | 818da59 | 2017-03-01 16:42:40 +0800 | [diff] [blame] | 9599 | } |
| 9600 | |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9601 | /* Check for change of roles in the active devices */ |
| 9602 | rdev_for_each(rdev2, mddev) { |
| 9603 | if (test_bit(Faulty, &rdev2->flags)) |
| 9604 | continue; |
| 9605 | |
| 9606 | /* Check if the roles changed */ |
| 9607 | role = le16_to_cpu(sb->dev_roles[rdev2->desc_nr]); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 9608 | |
| 9609 | if (test_bit(Candidate, &rdev2->flags)) { |
| 9610 | if (role == 0xfffe) { |
| 9611 | pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2->bdev,b)); |
| 9612 | md_kick_rdev_from_array(rdev2); |
| 9613 | continue; |
| 9614 | } |
| 9615 | else |
| 9616 | clear_bit(Candidate, &rdev2->flags); |
| 9617 | } |
| 9618 | |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9619 | if (role != rdev2->raid_disk) { |
Guoqing Jiang | ca1e98e | 2018-10-18 16:37:45 +0800 | [diff] [blame] | 9620 | /* |
| 9621 | * got activated except reshape is happening. |
| 9622 | */ |
| 9623 | if (rdev2->raid_disk == -1 && role != 0xffff && |
| 9624 | !(le32_to_cpu(sb->feature_map) & |
| 9625 | MD_FEATURE_RESHAPE_ACTIVE)) { |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9626 | rdev2->saved_raid_disk = role; |
| 9627 | ret = remove_and_add_spares(mddev, rdev2); |
| 9628 | pr_info("Activated spare: %s\n", |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 9629 | bdevname(rdev2->bdev,b)); |
Guoqing Jiang | a578183 | 2016-05-02 11:33:14 -0400 | [diff] [blame] | 9630 | /* wakeup mddev->thread here, so array could |
| 9631 | * perform resync with the new activated disk */ |
| 9632 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 9633 | md_wakeup_thread(mddev->thread); |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9634 | } |
| 9635 | /* device faulty |
| 9636 | * We just want to do the minimum to mark the disk |
| 9637 | * as faulty. The recovery is performed by the |
| 9638 | * one who initiated the error. |
| 9639 | */ |
| 9640 | if ((role == 0xfffe) || (role == 0xfffd)) { |
| 9641 | md_error(mddev, rdev2); |
| 9642 | clear_bit(Blocked, &rdev2->flags); |
| 9643 | } |
| 9644 | } |
Goldwyn Rodrigues | 1d7e3e9 | 2014-06-07 01:53:00 -0500 | [diff] [blame] | 9645 | } |
| 9646 | |
Zhao Heming | a8da01f | 2020-11-19 19:41:33 +0800 | [diff] [blame] | 9647 | if (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) { |
| 9648 | ret = update_raid_disks(mddev, le32_to_cpu(sb->raid_disks)); |
| 9649 | if (ret) |
| 9650 | pr_warn("md: updating array disks failed. %d\n", ret); |
| 9651 | } |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9652 | |
Guoqing Jiang | 7564bed | 2018-10-18 16:37:42 +0800 | [diff] [blame] | 9653 | /* |
| 9654 | * Since mddev->delta_disks has already updated in update_raid_disks, |
| 9655 | * so it is time to check reshape. |
| 9656 | */ |
| 9657 | if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery) && |
| 9658 | (le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 9659 | /* |
| 9660 | * reshape is happening in the remote node, we need to |
| 9661 | * update reshape_position and call start_reshape. |
| 9662 | */ |
Christoph Hellwig | ed4d0a4e | 2019-04-04 18:56:10 +0200 | [diff] [blame] | 9663 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
Guoqing Jiang | 7564bed | 2018-10-18 16:37:42 +0800 | [diff] [blame] | 9664 | if (mddev->pers->update_reshape_pos) |
| 9665 | mddev->pers->update_reshape_pos(mddev); |
| 9666 | if (mddev->pers->start_reshape) |
| 9667 | mddev->pers->start_reshape(mddev); |
| 9668 | } else if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery) && |
| 9669 | mddev->reshape_position != MaxSector && |
| 9670 | !(le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 9671 | /* reshape is just done in another node. */ |
| 9672 | mddev->reshape_position = MaxSector; |
| 9673 | if (mddev->pers->update_reshape_pos) |
| 9674 | mddev->pers->update_reshape_pos(mddev); |
| 9675 | } |
| 9676 | |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9677 | /* Finally set the event to be up to date */ |
| 9678 | mddev->events = le64_to_cpu(sb->events); |
| 9679 | } |
| 9680 | |
| 9681 | static int read_rdev(struct mddev *mddev, struct md_rdev *rdev) |
| 9682 | { |
| 9683 | int err; |
| 9684 | struct page *swapout = rdev->sb_page; |
| 9685 | struct mdp_superblock_1 *sb; |
| 9686 | |
| 9687 | /* Store the sb page of the rdev in the swapout temporary |
| 9688 | * variable in case we err in the future |
| 9689 | */ |
| 9690 | rdev->sb_page = NULL; |
NeilBrown | 7f0f0d8 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 9691 | err = alloc_disk_sb(rdev); |
| 9692 | if (err == 0) { |
| 9693 | ClearPageUptodate(rdev->sb_page); |
| 9694 | rdev->sb_loaded = 0; |
| 9695 | err = super_types[mddev->major_version]. |
| 9696 | load_super(rdev, NULL, mddev->minor_version); |
| 9697 | } |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9698 | if (err < 0) { |
| 9699 | pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n", |
| 9700 | __func__, __LINE__, rdev->desc_nr, err); |
NeilBrown | 7f0f0d8 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 9701 | if (rdev->sb_page) |
| 9702 | put_page(rdev->sb_page); |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9703 | rdev->sb_page = swapout; |
| 9704 | rdev->sb_loaded = 1; |
| 9705 | return err; |
| 9706 | } |
| 9707 | |
| 9708 | sb = page_address(rdev->sb_page); |
| 9709 | /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET |
| 9710 | * is not set |
| 9711 | */ |
| 9712 | |
| 9713 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET)) |
| 9714 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 9715 | |
| 9716 | /* The other node finished recovery, call spare_active to set |
| 9717 | * device In_sync and mddev->degraded |
| 9718 | */ |
| 9719 | if (rdev->recovery_offset == MaxSector && |
| 9720 | !test_bit(In_sync, &rdev->flags) && |
| 9721 | mddev->pers->spare_active(mddev)) |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 9722 | sysfs_notify_dirent_safe(mddev->sysfs_degraded); |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9723 | |
| 9724 | put_page(swapout); |
| 9725 | return 0; |
| 9726 | } |
| 9727 | |
| 9728 | void md_reload_sb(struct mddev *mddev, int nr) |
| 9729 | { |
| 9730 | struct md_rdev *rdev; |
| 9731 | int err; |
| 9732 | |
| 9733 | /* Find the rdev */ |
| 9734 | rdev_for_each_rcu(rdev, mddev) { |
| 9735 | if (rdev->desc_nr == nr) |
| 9736 | break; |
| 9737 | } |
| 9738 | |
| 9739 | if (!rdev || rdev->desc_nr != nr) { |
| 9740 | pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr); |
| 9741 | return; |
| 9742 | } |
| 9743 | |
| 9744 | err = read_rdev(mddev, rdev); |
| 9745 | if (err < 0) |
| 9746 | return; |
| 9747 | |
| 9748 | check_sb_changes(mddev, rdev); |
| 9749 | |
| 9750 | /* Read all rdev's to update recovery_offset */ |
Guoqing Jiang | 0ea9924 | 2018-04-09 17:01:21 +0800 | [diff] [blame] | 9751 | rdev_for_each_rcu(rdev, mddev) { |
| 9752 | if (!test_bit(Faulty, &rdev->flags)) |
| 9753 | read_rdev(mddev, rdev); |
| 9754 | } |
Goldwyn Rodrigues | 1d7e3e9 | 2014-06-07 01:53:00 -0500 | [diff] [blame] | 9755 | } |
| 9756 | EXPORT_SYMBOL(md_reload_sb); |
| 9757 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9758 | #ifndef MODULE |
| 9759 | |
| 9760 | /* |
| 9761 | * Searches all registered partitions for autorun RAID arrays |
| 9762 | * at boot time. |
| 9763 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9764 | |
Cong Wang | 5b1f5bc3 | 2016-06-08 09:20:16 -0700 | [diff] [blame] | 9765 | static DEFINE_MUTEX(detected_devices_mutex); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9766 | static LIST_HEAD(all_detected_devices); |
| 9767 | struct detected_devices_node { |
| 9768 | struct list_head list; |
| 9769 | dev_t dev; |
| 9770 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9771 | |
| 9772 | void md_autodetect_dev(dev_t dev) |
| 9773 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9774 | struct detected_devices_node *node_detected_dev; |
| 9775 | |
| 9776 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 9777 | if (node_detected_dev) { |
| 9778 | node_detected_dev->dev = dev; |
Cong Wang | 5b1f5bc3 | 2016-06-08 09:20:16 -0700 | [diff] [blame] | 9779 | mutex_lock(&detected_devices_mutex); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9780 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
Cong Wang | 5b1f5bc3 | 2016-06-08 09:20:16 -0700 | [diff] [blame] | 9781 | mutex_unlock(&detected_devices_mutex); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9782 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9783 | } |
| 9784 | |
Christoph Hellwig | d82fa81 | 2020-06-06 15:00:24 +0200 | [diff] [blame] | 9785 | void md_autostart_arrays(int part) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9786 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 9787 | struct md_rdev *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9788 | struct detected_devices_node *node_detected_dev; |
| 9789 | dev_t dev; |
| 9790 | int i_scanned, i_passed; |
| 9791 | |
| 9792 | i_scanned = 0; |
| 9793 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9794 | |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 9795 | pr_info("md: Autodetecting RAID arrays.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9796 | |
Cong Wang | 5b1f5bc3 | 2016-06-08 09:20:16 -0700 | [diff] [blame] | 9797 | mutex_lock(&detected_devices_mutex); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9798 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 9799 | i_scanned++; |
| 9800 | node_detected_dev = list_entry(all_detected_devices.next, |
| 9801 | struct detected_devices_node, list); |
| 9802 | list_del(&node_detected_dev->list); |
| 9803 | dev = node_detected_dev->dev; |
| 9804 | kfree(node_detected_dev); |
Shaohua Li | 90bcf133 | 2016-09-14 14:26:54 -0700 | [diff] [blame] | 9805 | mutex_unlock(&detected_devices_mutex); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 9806 | rdev = md_import_device(dev,0, 90); |
Shaohua Li | 90bcf133 | 2016-09-14 14:26:54 -0700 | [diff] [blame] | 9807 | mutex_lock(&detected_devices_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9808 | if (IS_ERR(rdev)) |
| 9809 | continue; |
| 9810 | |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 9811 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9812 | continue; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 9813 | |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 9814 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9815 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9816 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9817 | } |
Cong Wang | 5b1f5bc3 | 2016-06-08 09:20:16 -0700 | [diff] [blame] | 9818 | mutex_unlock(&detected_devices_mutex); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9819 | |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 9820 | 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] | 9821 | |
| 9822 | autorun_devices(part); |
| 9823 | } |
| 9824 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 9825 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9826 | |
| 9827 | static __exit void md_exit(void) |
| 9828 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 9829 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9830 | struct list_head *tmp; |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 9831 | int delay = 1; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 9832 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 9833 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9834 | unregister_blkdev(mdp_major, "mdp"); |
| 9835 | unregister_reboot_notifier(&md_notifier); |
| 9836 | unregister_sysctl_table(raid_table_header); |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 9837 | |
| 9838 | /* We cannot unload the modules while some process is |
| 9839 | * waiting for us in select() or poll() - wake them up |
| 9840 | */ |
| 9841 | md_unloading = 1; |
| 9842 | while (waitqueue_active(&md_event_waiters)) { |
| 9843 | /* not safe to leave yet */ |
| 9844 | wake_up(&md_event_waiters); |
| 9845 | msleep(delay); |
| 9846 | delay += delay; |
| 9847 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9848 | remove_proc_entry("mdstat", NULL); |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 9849 | |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 9850 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9851 | export_array(mddev); |
NeilBrown | 9356863 | 2017-02-06 13:41:39 +1100 | [diff] [blame] | 9852 | mddev->ctime = 0; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 9853 | mddev->hold_active = 0; |
NeilBrown | 9356863 | 2017-02-06 13:41:39 +1100 | [diff] [blame] | 9854 | /* |
| 9855 | * for_each_mddev() will call mddev_put() at the end of each |
| 9856 | * iteration. As the mddev is now fully clear, this will |
| 9857 | * schedule the mddev for destruction by a workqueue, and the |
| 9858 | * destroy_workqueue() below will wait for that to complete. |
| 9859 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9860 | } |
Guoqing Jiang | cc1ffe6 | 2020-04-04 23:57:08 +0200 | [diff] [blame] | 9861 | destroy_workqueue(md_rdev_misc_wq); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 9862 | destroy_workqueue(md_misc_wq); |
| 9863 | destroy_workqueue(md_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9864 | } |
| 9865 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 9866 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9867 | module_exit(md_exit) |
| 9868 | |
Kees Cook | e4dca7b | 2017-10-17 19:04:42 -0700 | [diff] [blame] | 9869 | static int get_ro(char *buffer, const struct kernel_param *kp) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 9870 | { |
Xiongfeng Wang | 3f99980 | 2020-05-11 16:23:25 +0800 | [diff] [blame] | 9871 | return sprintf(buffer, "%d\n", start_readonly); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 9872 | } |
Kees Cook | e4dca7b | 2017-10-17 19:04:42 -0700 | [diff] [blame] | 9873 | static int set_ro(const char *val, const struct kernel_param *kp) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 9874 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 9875 | return kstrtouint(val, 10, (unsigned int *)&start_readonly); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 9876 | } |
| 9877 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 9878 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 9879 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 9880 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | 78b6350 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 9881 | module_param(create_on_open, bool, S_IRUSR|S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 9882 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9883 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 9884 | MODULE_DESCRIPTION("MD RAID framework"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 9885 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 9886 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |