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; |
Song Liu | 00fe60e | 2020-08-31 15:27:24 -0700 | [diff] [blame] | 467 | struct hd_struct *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 | */ |
NeilBrown | 2bc13b8 | 2019-03-29 10:46:17 -0700 | [diff] [blame] | 642 | mddev->last_flush = 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 | { |
NeilBrown | 2bc13b8 | 2019-03-29 10:46:17 -0700 | [diff] [blame] | 663 | ktime_t start = ktime_get_boottime(); |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 664 | spin_lock_irq(&mddev->lock); |
| 665 | wait_event_lock_irq(mddev->sb_wait, |
NeilBrown | 2bc13b8 | 2019-03-29 10:46:17 -0700 | [diff] [blame] | 666 | !mddev->flush_bio || |
| 667 | ktime_after(mddev->last_flush, start), |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 668 | mddev->lock); |
NeilBrown | 2bc13b8 | 2019-03-29 10:46:17 -0700 | [diff] [blame] | 669 | if (!ktime_after(mddev->last_flush, start)) { |
| 670 | WARN_ON(mddev->flush_bio); |
| 671 | mddev->flush_bio = bio; |
| 672 | bio = NULL; |
| 673 | } |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 674 | spin_unlock_irq(&mddev->lock); |
| 675 | |
NeilBrown | 2bc13b8 | 2019-03-29 10:46:17 -0700 | [diff] [blame] | 676 | if (!bio) { |
| 677 | INIT_WORK(&mddev->flush_work, submit_flushes); |
| 678 | queue_work(md_wq, &mddev->flush_work); |
| 679 | } else { |
| 680 | /* flush was performed for some other bio while we waited. */ |
| 681 | if (bio->bi_iter.bi_size == 0) |
| 682 | /* an empty barrier - all done */ |
| 683 | bio_endio(bio); |
| 684 | else { |
| 685 | bio->bi_opf &= ~REQ_PREFLUSH; |
David Jeffery | 775d783 | 2019-09-16 13:15:14 -0400 | [diff] [blame] | 686 | return false; |
NeilBrown | 2bc13b8 | 2019-03-29 10:46:17 -0700 | [diff] [blame] | 687 | } |
| 688 | } |
David Jeffery | 775d783 | 2019-09-16 13:15:14 -0400 | [diff] [blame] | 689 | return true; |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 690 | } |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 691 | EXPORT_SYMBOL(md_flush_request); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 692 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 693 | static inline struct mddev *mddev_get(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | { |
| 695 | atomic_inc(&mddev->active); |
| 696 | return mddev; |
| 697 | } |
| 698 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 699 | static void mddev_delayed_delete(struct work_struct *ws); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 700 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 701 | static void mddev_put(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | { |
| 703 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
| 704 | return; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 705 | if (!mddev->raid_disks && list_empty(&mddev->disks) && |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 706 | mddev->ctime == 0 && !mddev->hold_active) { |
| 707 | /* Array is not configured at all, and not held active, |
| 708 | * so destroy it */ |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 709 | list_del_init(&mddev->all_mddevs); |
Kent Overstreet | 28dec87 | 2018-06-07 20:52:54 -0400 | [diff] [blame] | 710 | |
| 711 | /* |
| 712 | * Call queue_work inside the spinlock so that |
| 713 | * flush_workqueue() after mddev_find will succeed in waiting |
| 714 | * for the work to be done. |
| 715 | */ |
| 716 | INIT_WORK(&mddev->del_work, mddev_delayed_delete); |
| 717 | queue_work(md_misc_wq, &mddev->del_work); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 718 | } |
| 719 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | } |
| 721 | |
Kees Cook | 8376d3c | 2017-10-16 17:01:48 -0700 | [diff] [blame] | 722 | static void md_safemode_timeout(struct timer_list *t); |
Sasha Levin | 25b2edf | 2015-07-24 18:19:58 -0400 | [diff] [blame] | 723 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 724 | void mddev_init(struct mddev *mddev) |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 725 | { |
Kent Overstreet | 28dec87 | 2018-06-07 20:52:54 -0400 | [diff] [blame] | 726 | kobject_init(&mddev->kobj, &md_ktype); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 727 | mutex_init(&mddev->open_mutex); |
| 728 | mutex_init(&mddev->reconfig_mutex); |
| 729 | mutex_init(&mddev->bitmap_info.mutex); |
| 730 | INIT_LIST_HEAD(&mddev->disks); |
| 731 | INIT_LIST_HEAD(&mddev->all_mddevs); |
Kees Cook | 8376d3c | 2017-10-16 17:01:48 -0700 | [diff] [blame] | 732 | timer_setup(&mddev->safemode_timer, md_safemode_timeout, 0); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 733 | atomic_set(&mddev->active, 1); |
| 734 | atomic_set(&mddev->openers, 0); |
| 735 | atomic_set(&mddev->active_io, 0); |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 736 | spin_lock_init(&mddev->lock); |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 737 | atomic_set(&mddev->flush_pending, 0); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 738 | init_waitqueue_head(&mddev->sb_wait); |
| 739 | init_waitqueue_head(&mddev->recovery_wait); |
| 740 | mddev->reshape_position = MaxSector; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 741 | mddev->reshape_backwards = 0; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 742 | mddev->last_sync_action = "none"; |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 743 | mddev->resync_min = 0; |
| 744 | mddev->resync_max = MaxSector; |
| 745 | mddev->level = LEVEL_NONE; |
| 746 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 747 | EXPORT_SYMBOL_GPL(mddev_init); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 748 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 749 | static struct mddev *mddev_find(dev_t unit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 751 | struct mddev *mddev, *new = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | |
NeilBrown | 8f5f02c | 2011-02-16 13:58:51 +1100 | [diff] [blame] | 753 | if (unit && MAJOR(unit) != MD_MAJOR) |
| 754 | unit &= ~((1<<MdpMinorShift)-1); |
| 755 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | retry: |
| 757 | spin_lock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 759 | if (unit) { |
| 760 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 761 | if (mddev->unit == unit) { |
| 762 | mddev_get(mddev); |
| 763 | spin_unlock(&all_mddevs_lock); |
| 764 | kfree(new); |
| 765 | return mddev; |
| 766 | } |
| 767 | |
| 768 | if (new) { |
| 769 | list_add(&new->all_mddevs, &all_mddevs); |
| 770 | spin_unlock(&all_mddevs_lock); |
| 771 | new->hold_active = UNTIL_IOCTL; |
| 772 | return new; |
| 773 | } |
| 774 | } else if (new) { |
| 775 | /* find an unused unit number */ |
| 776 | static int next_minor = 512; |
| 777 | int start = next_minor; |
| 778 | int is_free = 0; |
| 779 | int dev = 0; |
| 780 | while (!is_free) { |
| 781 | dev = MKDEV(MD_MAJOR, next_minor); |
| 782 | next_minor++; |
| 783 | if (next_minor > MINORMASK) |
| 784 | next_minor = 0; |
| 785 | if (next_minor == start) { |
| 786 | /* Oh dear, all in use. */ |
| 787 | spin_unlock(&all_mddevs_lock); |
| 788 | kfree(new); |
| 789 | return NULL; |
| 790 | } |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 791 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 792 | is_free = 1; |
| 793 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 794 | if (mddev->unit == dev) { |
| 795 | is_free = 0; |
| 796 | break; |
| 797 | } |
| 798 | } |
| 799 | new->unit = dev; |
| 800 | new->md_minor = MINOR(dev); |
| 801 | new->hold_active = UNTIL_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | list_add(&new->all_mddevs, &all_mddevs); |
| 803 | spin_unlock(&all_mddevs_lock); |
| 804 | return new; |
| 805 | } |
| 806 | spin_unlock(&all_mddevs_lock); |
| 807 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 808 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | if (!new) |
| 810 | return NULL; |
| 811 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | new->unit = unit; |
| 813 | if (MAJOR(unit) == MD_MAJOR) |
| 814 | new->md_minor = MINOR(unit); |
| 815 | else |
| 816 | new->md_minor = MINOR(unit) >> MdpMinorShift; |
| 817 | |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 818 | mddev_init(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | goto retry; |
| 821 | } |
| 822 | |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 823 | static struct attribute_group md_redundancy_group; |
| 824 | |
NeilBrown | 5c47daf | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 825 | void mddev_unlock(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | { |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 827 | if (mddev->to_remove) { |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 828 | /* These cannot be removed under reconfig_mutex as |
| 829 | * an access to the files will try to take reconfig_mutex |
| 830 | * while holding the file unremovable, which leads to |
| 831 | * a deadlock. |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 832 | * So hold set sysfs_active while the remove in happeing, |
| 833 | * and anything else which might set ->to_remove or my |
| 834 | * otherwise change the sysfs namespace will fail with |
| 835 | * -EBUSY if sysfs_active is still set. |
| 836 | * We set sysfs_active under reconfig_mutex and elsewhere |
| 837 | * test it under the same mutex to ensure its correct value |
| 838 | * is seen. |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 839 | */ |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 840 | struct attribute_group *to_remove = mddev->to_remove; |
| 841 | mddev->to_remove = NULL; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 842 | mddev->sysfs_active = 1; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 843 | mutex_unlock(&mddev->reconfig_mutex); |
| 844 | |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 845 | if (mddev->kobj.sd) { |
| 846 | if (to_remove != &md_redundancy_group) |
| 847 | sysfs_remove_group(&mddev->kobj, to_remove); |
| 848 | if (mddev->pers == NULL || |
| 849 | mddev->pers->sync_request == NULL) { |
| 850 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 851 | if (mddev->sysfs_action) |
| 852 | sysfs_put(mddev->sysfs_action); |
Junxiao Bi | e8efa9b | 2020-08-04 17:27:18 -0700 | [diff] [blame] | 853 | if (mddev->sysfs_completed) |
| 854 | sysfs_put(mddev->sysfs_completed); |
| 855 | if (mddev->sysfs_degraded) |
| 856 | sysfs_put(mddev->sysfs_degraded); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 857 | mddev->sysfs_action = NULL; |
Junxiao Bi | e8efa9b | 2020-08-04 17:27:18 -0700 | [diff] [blame] | 858 | mddev->sysfs_completed = NULL; |
| 859 | mddev->sysfs_degraded = NULL; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 860 | } |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 861 | } |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 862 | mddev->sysfs_active = 0; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 863 | } else |
| 864 | mutex_unlock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | |
Chris Dunlop | 751e67c | 2011-10-19 16:48:26 +1100 | [diff] [blame] | 866 | /* As we've dropped the mutex we need a spinlock to |
| 867 | * make sure the thread doesn't disappear |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 868 | */ |
| 869 | spin_lock(&pers_lock); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 870 | md_wakeup_thread(mddev->thread); |
NeilBrown | 4d5324f | 2017-10-19 12:17:16 +1100 | [diff] [blame] | 871 | wake_up(&mddev->sb_wait); |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 872 | spin_unlock(&pers_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | } |
NeilBrown | 5c47daf | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 874 | EXPORT_SYMBOL_GPL(mddev_unlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 876 | struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr) |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 877 | { |
| 878 | struct md_rdev *rdev; |
| 879 | |
| 880 | rdev_for_each_rcu(rdev, mddev) |
| 881 | if (rdev->desc_nr == nr) |
| 882 | return rdev; |
| 883 | |
| 884 | return NULL; |
| 885 | } |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 886 | EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu); |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 887 | |
| 888 | static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 890 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 892 | rdev_for_each(rdev, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | if (rdev->bdev->bd_dev == dev) |
| 894 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 895 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | return NULL; |
| 897 | } |
| 898 | |
Tomasz Majchrzak | 1532d9e | 2017-12-27 10:31:40 +0100 | [diff] [blame] | 899 | struct md_rdev *md_find_rdev_rcu(struct mddev *mddev, dev_t dev) |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 900 | { |
| 901 | struct md_rdev *rdev; |
| 902 | |
| 903 | rdev_for_each_rcu(rdev, mddev) |
| 904 | if (rdev->bdev->bd_dev == dev) |
| 905 | return rdev; |
| 906 | |
| 907 | return NULL; |
| 908 | } |
Tomasz Majchrzak | 1532d9e | 2017-12-27 10:31:40 +0100 | [diff] [blame] | 909 | EXPORT_SYMBOL_GPL(md_find_rdev_rcu); |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 910 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 911 | static struct md_personality *find_pers(int level, char *clevel) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 912 | { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 913 | struct md_personality *pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 914 | list_for_each_entry(pers, &pers_list, list) { |
| 915 | if (level != LEVEL_NONE && pers->level == level) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 916 | return pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 917 | if (strcmp(pers->name, clevel)==0) |
| 918 | return pers; |
| 919 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 920 | return NULL; |
| 921 | } |
| 922 | |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 923 | /* return the offset of the super block in 512byte sectors */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 924 | static inline sector_t calc_dev_sboffset(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | { |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 926 | sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512; |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 927 | return MD_NEW_SIZE_SECTORS(num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | } |
| 929 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 930 | static int alloc_disk_sb(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | rdev->sb_page = alloc_page(GFP_KERNEL); |
NeilBrown | 7f0f0d8 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 933 | if (!rdev->sb_page) |
Andre Noll | ebc2433 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 934 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | return 0; |
| 936 | } |
| 937 | |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 938 | void md_rdev_clear(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | { |
| 940 | if (rdev->sb_page) { |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 941 | put_page(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | rdev->sb_loaded = 0; |
| 943 | rdev->sb_page = NULL; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 944 | rdev->sb_start = 0; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 945 | rdev->sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 947 | if (rdev->bb_page) { |
| 948 | put_page(rdev->bb_page); |
| 949 | rdev->bb_page = NULL; |
| 950 | } |
Dan Williams | d3b407fb | 2016-01-06 12:19:22 -0800 | [diff] [blame] | 951 | badblocks_exit(&rdev->badblocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | } |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 953 | EXPORT_SYMBOL_GPL(md_rdev_clear); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 955 | static void super_written(struct bio *bio) |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 956 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 957 | struct md_rdev *rdev = bio->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 958 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 959 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 960 | if (bio->bi_status) { |
Guoqing Jiang | b3db8a2 | 2020-07-28 12:01:41 +0200 | [diff] [blame] | 961 | pr_err("md: %s gets error=%d\n", __func__, |
| 962 | blk_status_to_errno(bio->bi_status)); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 963 | md_error(mddev, rdev); |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 964 | if (!test_bit(Faulty, &rdev->flags) |
| 965 | && (bio->bi_opf & MD_FAILFAST)) { |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 966 | set_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags); |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 967 | set_bit(LastDev, &rdev->flags); |
| 968 | } |
| 969 | } else |
| 970 | clear_bit(LastDev, &rdev->flags); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 971 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 972 | if (atomic_dec_and_test(&mddev->pending_writes)) |
| 973 | wake_up(&mddev->sb_wait); |
Shaohua Li | ed3b98c | 2016-03-29 14:00:19 -0700 | [diff] [blame] | 974 | rdev_dec_pending(rdev, mddev); |
Neil Brown | f8b58ed | 2005-06-27 22:29:34 -0700 | [diff] [blame] | 975 | bio_put(bio); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 976 | } |
| 977 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 978 | void md_super_write(struct mddev *mddev, struct md_rdev *rdev, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 979 | sector_t sector, int size, struct page *page) |
| 980 | { |
| 981 | /* write first size bytes of page to sector of rdev |
| 982 | * Increment mddev->pending_writes before returning |
| 983 | * and decrement it on completion, waking up sb_wait |
| 984 | * if zero is reached. |
| 985 | * If an error occurred, call md_error |
| 986 | */ |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 987 | struct bio *bio; |
| 988 | int ff = 0; |
| 989 | |
Heinz Mauelshagen | 4b6c106 | 2018-02-02 23:13:19 +0100 | [diff] [blame] | 990 | if (!page) |
| 991 | return; |
| 992 | |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 993 | if (test_bit(Faulty, &rdev->flags)) |
| 994 | return; |
| 995 | |
NeilBrown | 5a85071 | 2017-06-21 09:12:21 +1000 | [diff] [blame] | 996 | bio = md_bio_alloc_sync(mddev); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 997 | |
Shaohua Li | ed3b98c | 2016-03-29 14:00:19 -0700 | [diff] [blame] | 998 | atomic_inc(&rdev->nr_pending); |
| 999 | |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 1000 | bio_set_dev(bio, rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1001 | bio->bi_iter.bi_sector = sector; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1002 | bio_add_page(bio, page, size, 0); |
| 1003 | bio->bi_private = rdev; |
| 1004 | bio->bi_end_io = super_written; |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 1005 | |
| 1006 | if (test_bit(MD_FAILFAST_SUPPORTED, &mddev->flags) && |
| 1007 | test_bit(FailFast, &rdev->flags) && |
| 1008 | !test_bit(LastDev, &rdev->flags)) |
| 1009 | ff = MD_FAILFAST; |
Jan Kara | 5a8948f | 2017-05-31 09:44:33 +0200 | [diff] [blame] | 1010 | bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH | REQ_FUA | ff; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1011 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1012 | atomic_inc(&mddev->pending_writes); |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 1013 | submit_bio(bio); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1014 | } |
| 1015 | |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 1016 | int md_super_wait(struct mddev *mddev) |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1017 | { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1018 | /* wait for all superblock writes that were scheduled to complete */ |
NeilBrown | 1967cd5 | 2014-09-09 14:20:28 +1000 | [diff] [blame] | 1019 | wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 1020 | if (test_and_clear_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags)) |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 1021 | return -EAGAIN; |
| 1022 | return 0; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1023 | } |
| 1024 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1025 | 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] | 1026 | struct page *page, int op, int op_flags, bool metadata_op) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | { |
NeilBrown | 5a85071 | 2017-06-21 09:12:21 +1000 | [diff] [blame] | 1028 | struct bio *bio = md_bio_alloc_sync(rdev->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | int ret; |
| 1030 | |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 1031 | if (metadata_op && rdev->meta_bdev) |
| 1032 | bio_set_dev(bio, rdev->meta_bdev); |
| 1033 | else |
| 1034 | bio_set_dev(bio, rdev->bdev); |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1035 | bio_set_op_attrs(bio, op, op_flags); |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1036 | if (metadata_op) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1037 | bio->bi_iter.bi_sector = sector + rdev->sb_start; |
NeilBrown | 1fdd6fc9 | 2012-05-21 09:28:32 +1000 | [diff] [blame] | 1038 | else if (rdev->mddev->reshape_position != MaxSector && |
| 1039 | (rdev->mddev->reshape_backwards == |
| 1040 | (sector >= rdev->mddev->reshape_position))) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1041 | bio->bi_iter.bi_sector = sector + rdev->new_data_offset; |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1042 | else |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1043 | bio->bi_iter.bi_sector = sector + rdev->data_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | bio_add_page(bio, page, size, 0); |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 1045 | |
| 1046 | submit_bio_wait(bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 1048 | ret = !bio->bi_status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | bio_put(bio); |
| 1050 | return ret; |
| 1051 | } |
NeilBrown | a8745db | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 1052 | EXPORT_SYMBOL_GPL(sync_page_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1054 | static int read_disk_sb(struct md_rdev *rdev, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | { |
| 1056 | char b[BDEVNAME_SIZE]; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 1057 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | if (rdev->sb_loaded) |
| 1059 | return 0; |
| 1060 | |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1061 | 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] | 1062 | goto fail; |
| 1063 | rdev->sb_loaded = 1; |
| 1064 | return 0; |
| 1065 | |
| 1066 | fail: |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1067 | pr_err("md: disabled device %s, could not read superblock.\n", |
| 1068 | bdevname(rdev->bdev,b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | return -EINVAL; |
| 1070 | } |
| 1071 | |
Amir Goldstein | e6fd209 | 2017-05-04 16:26:20 +0300 | [diff] [blame] | 1072 | 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] | 1073 | { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1074 | return sb1->set_uuid0 == sb2->set_uuid0 && |
Andre Noll | 0571046 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 1075 | sb1->set_uuid1 == sb2->set_uuid1 && |
| 1076 | sb1->set_uuid2 == sb2->set_uuid2 && |
| 1077 | sb1->set_uuid3 == sb2->set_uuid3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | } |
| 1079 | |
Amir Goldstein | e6fd209 | 2017-05-04 16:26:20 +0300 | [diff] [blame] | 1080 | 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] | 1081 | { |
| 1082 | int ret; |
| 1083 | mdp_super_t *tmp1, *tmp2; |
| 1084 | |
| 1085 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); |
| 1086 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); |
| 1087 | |
| 1088 | if (!tmp1 || !tmp2) { |
| 1089 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1090 | goto abort; |
| 1091 | } |
| 1092 | |
| 1093 | *tmp1 = *sb1; |
| 1094 | *tmp2 = *sb2; |
| 1095 | |
| 1096 | /* |
| 1097 | * nr_disks is not constant |
| 1098 | */ |
| 1099 | tmp1->nr_disks = 0; |
| 1100 | tmp2->nr_disks = 0; |
| 1101 | |
Andre Noll | ce0c8e0 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 1102 | ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | abort: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 1104 | kfree(tmp1); |
| 1105 | kfree(tmp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | return ret; |
| 1107 | } |
| 1108 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 1109 | static u32 md_csum_fold(u32 csum) |
| 1110 | { |
| 1111 | csum = (csum & 0xffff) + (csum >> 16); |
| 1112 | return (csum & 0xffff) + (csum >> 16); |
| 1113 | } |
| 1114 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1115 | static unsigned int calc_sb_csum(mdp_super_t *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | { |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 1117 | u64 newcsum = 0; |
| 1118 | u32 *sb32 = (u32*)sb; |
| 1119 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | unsigned int disk_csum, csum; |
| 1121 | |
| 1122 | disk_csum = sb->sb_csum; |
| 1123 | sb->sb_csum = 0; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 1124 | |
| 1125 | for (i = 0; i < MD_SB_BYTES/4 ; i++) |
| 1126 | newcsum += sb32[i]; |
| 1127 | csum = (newcsum & 0xffffffff) + (newcsum>>32); |
| 1128 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 1129 | #ifdef CONFIG_ALPHA |
| 1130 | /* This used to use csum_partial, which was wrong for several |
| 1131 | * reasons including that different results are returned on |
| 1132 | * different architectures. It isn't critical that we get exactly |
| 1133 | * the same return value as before (we always csum_fold before |
| 1134 | * testing, and that removes any differences). However as we |
| 1135 | * know that csum_partial always returned a 16bit value on |
| 1136 | * alphas, do a fold to maximise conformity to previous behaviour. |
| 1137 | */ |
| 1138 | sb->sb_csum = md_csum_fold(disk_csum); |
| 1139 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | sb->sb_csum = disk_csum; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 1141 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1142 | return csum; |
| 1143 | } |
| 1144 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | /* |
| 1146 | * Handle superblock details. |
| 1147 | * We want to be able to handle multiple superblock formats |
| 1148 | * so we have a common interface to them all, and an array of |
| 1149 | * different handlers. |
| 1150 | * We rely on user-space to write the initial superblock, and support |
| 1151 | * reading and updating of superblocks. |
| 1152 | * Interface methods are: |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1153 | * 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] | 1154 | * loads and validates a superblock on dev. |
| 1155 | * if refdev != NULL, compare superblocks on both devices |
| 1156 | * Return: |
| 1157 | * 0 - dev has a superblock that is compatible with refdev |
| 1158 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 1159 | * so dev should be used as the refdev in future |
| 1160 | * -EINVAL superblock incompatible or invalid |
| 1161 | * -othererror e.g. -EIO |
| 1162 | * |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1163 | * int validate_super(struct mddev *mddev, struct md_rdev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1164 | * Verify that dev is acceptable into mddev. |
| 1165 | * The first time, mddev->raid_disks will be 0, and data from |
| 1166 | * dev should be merged in. Subsequent calls check that dev |
| 1167 | * is new enough. Return 0 or -EINVAL |
| 1168 | * |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1169 | * void sync_super(struct mddev *mddev, struct md_rdev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1170 | * Update the superblock for rdev with data in mddev |
| 1171 | * This does not write to disc. |
| 1172 | * |
| 1173 | */ |
| 1174 | |
| 1175 | struct super_type { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1176 | char *name; |
| 1177 | struct module *owner; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1178 | int (*load_super)(struct md_rdev *rdev, |
| 1179 | struct md_rdev *refdev, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1180 | int minor_version); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1181 | int (*validate_super)(struct mddev *mddev, |
| 1182 | struct md_rdev *rdev); |
| 1183 | void (*sync_super)(struct mddev *mddev, |
| 1184 | struct md_rdev *rdev); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1185 | unsigned long long (*rdev_size_change)(struct md_rdev *rdev, |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1186 | sector_t num_sectors); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1187 | int (*allow_new_offset)(struct md_rdev *rdev, |
| 1188 | unsigned long long new_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | }; |
| 1190 | |
| 1191 | /* |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 1192 | * Check that the given mddev has no bitmap. |
| 1193 | * |
| 1194 | * This function is called from the run method of all personalities that do not |
| 1195 | * support bitmaps. It prints an error message and returns non-zero if mddev |
| 1196 | * has a bitmap. Otherwise, it returns 0. |
| 1197 | * |
| 1198 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1199 | int md_check_no_bitmap(struct mddev *mddev) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 1200 | { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1201 | if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 1202 | return 0; |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1203 | pr_warn("%s: bitmaps are not supported for %s\n", |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 1204 | mdname(mddev), mddev->pers->name); |
| 1205 | return 1; |
| 1206 | } |
| 1207 | EXPORT_SYMBOL(md_check_no_bitmap); |
| 1208 | |
| 1209 | /* |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1210 | * load_super for 0.90.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1211 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1212 | 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] | 1213 | { |
| 1214 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 1215 | mdp_super_t *sb; |
| 1216 | int ret; |
Yufen Yu | 228fc7d | 2019-10-30 18:47:02 +0800 | [diff] [blame] | 1217 | bool spare_disk = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1218 | |
| 1219 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1220 | * Calculate the position of the superblock (512byte sectors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | * it's at the end of the disk. |
| 1222 | * |
| 1223 | * It also happens to be a multiple of 4Kb. |
| 1224 | */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1225 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1226 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1227 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1228 | if (ret) |
| 1229 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1230 | |
| 1231 | ret = -EINVAL; |
| 1232 | |
| 1233 | bdevname(rdev->bdev, b); |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1234 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 | |
| 1236 | if (sb->md_magic != MD_SB_MAGIC) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1237 | pr_warn("md: invalid raid superblock magic on %s\n", b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | goto abort; |
| 1239 | } |
| 1240 | |
| 1241 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1242 | sb->minor_version < 90 || |
| 1243 | sb->minor_version > 91) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1244 | pr_warn("Bad version number %d.%d on %s\n", |
| 1245 | sb->major_version, sb->minor_version, b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | goto abort; |
| 1247 | } |
| 1248 | |
| 1249 | if (sb->raid_disks <= 0) |
| 1250 | goto abort; |
| 1251 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 1252 | 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] | 1253 | pr_warn("md: invalid superblock checksum on %s\n", b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | goto abort; |
| 1255 | } |
| 1256 | |
| 1257 | rdev->preferred_minor = sb->md_minor; |
| 1258 | rdev->data_offset = 0; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1259 | rdev->new_data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1260 | rdev->sb_size = MD_SB_BYTES; |
NeilBrown | 9f2f383 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1261 | rdev->badblocks.shift = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | |
| 1263 | if (sb->level == LEVEL_MULTIPATH) |
| 1264 | rdev->desc_nr = -1; |
| 1265 | else |
| 1266 | rdev->desc_nr = sb->this_disk.number; |
| 1267 | |
Yufen Yu | 228fc7d | 2019-10-30 18:47:02 +0800 | [diff] [blame] | 1268 | /* not spare disk, or LEVEL_MULTIPATH */ |
| 1269 | if (sb->level == LEVEL_MULTIPATH || |
| 1270 | (rdev->desc_nr >= 0 && |
Yufen Yu | 3b7436c | 2019-12-10 15:01:29 +0800 | [diff] [blame] | 1271 | rdev->desc_nr < MD_SB_DISKS && |
Yufen Yu | 228fc7d | 2019-10-30 18:47:02 +0800 | [diff] [blame] | 1272 | sb->disks[rdev->desc_nr].state & |
| 1273 | ((1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))) |
| 1274 | spare_disk = false; |
| 1275 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1276 | if (!refdev) { |
Yufen Yu | 228fc7d | 2019-10-30 18:47:02 +0800 | [diff] [blame] | 1277 | if (!spare_disk) |
Yufen Yu | 6a5cb53 | 2019-10-16 16:00:03 +0800 | [diff] [blame] | 1278 | ret = 1; |
| 1279 | else |
| 1280 | ret = 0; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1281 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1282 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1283 | mdp_super_t *refsb = page_address(refdev->sb_page); |
Amir Goldstein | e6fd209 | 2017-05-04 16:26:20 +0300 | [diff] [blame] | 1284 | if (!md_uuid_equal(refsb, sb)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1285 | pr_warn("md: %s has different UUID to %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1286 | b, bdevname(refdev->bdev,b2)); |
| 1287 | goto abort; |
| 1288 | } |
Amir Goldstein | e6fd209 | 2017-05-04 16:26:20 +0300 | [diff] [blame] | 1289 | if (!md_sb_equal(refsb, sb)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1290 | pr_warn("md: %s has same UUID but different superblock to %s\n", |
| 1291 | b, bdevname(refdev->bdev, b2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1292 | goto abort; |
| 1293 | } |
| 1294 | ev1 = md_event(sb); |
| 1295 | ev2 = md_event(refsb); |
Yufen Yu | 6a5cb53 | 2019-10-16 16:00:03 +0800 | [diff] [blame] | 1296 | |
Yufen Yu | 228fc7d | 2019-10-30 18:47:02 +0800 | [diff] [blame] | 1297 | if (!spare_disk && ev1 > ev2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1298 | ret = 1; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1299 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | ret = 0; |
| 1301 | } |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 1302 | rdev->sectors = rdev->sb_start; |
NeilBrown | 667a531 | 2012-08-16 16:46:12 +1000 | [diff] [blame] | 1303 | /* Limit to 4TB as metadata cannot record more than that. |
| 1304 | * (not needed for Linear and RAID0 as metadata doesn't |
| 1305 | * record this size) |
| 1306 | */ |
Christoph Hellwig | 72deb45 | 2019-04-05 18:08:59 +0200 | [diff] [blame] | 1307 | if ((u64)rdev->sectors >= (2ULL << 32) && sb->level >= 1) |
Arnd Bergmann | 3312c95 | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 1308 | rdev->sectors = (sector_t)(2ULL << 32) - 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1310 | if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1311 | /* "this cannot possibly happen" ... */ |
| 1312 | ret = -EINVAL; |
| 1313 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | abort: |
| 1315 | return ret; |
| 1316 | } |
| 1317 | |
| 1318 | /* |
| 1319 | * validate_super for 0.90.0 |
| 1320 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1321 | static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | { |
| 1323 | mdp_disk_t *desc; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1324 | mdp_super_t *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1325 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1326 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1327 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1328 | clear_bit(Faulty, &rdev->flags); |
| 1329 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1330 | clear_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1331 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1332 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1333 | if (mddev->raid_disks == 0) { |
| 1334 | mddev->major_version = 0; |
| 1335 | mddev->minor_version = sb->minor_version; |
| 1336 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1337 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1338 | mddev->chunk_sectors = sb->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1339 | mddev->ctime = sb->ctime; |
| 1340 | mddev->utime = sb->utime; |
| 1341 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1342 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1343 | mddev->layout = sb->layout; |
| 1344 | mddev->raid_disks = sb->raid_disks; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1345 | mddev->dev_sectors = ((sector_t)sb->size) * 2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1346 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1347 | mddev->bitmap_info.offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1348 | mddev->bitmap_info.space = 0; |
| 1349 | /* bitmap can use 60 K after the 4K superblocks */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1350 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1351 | mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1352 | mddev->reshape_backwards = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1353 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1354 | if (mddev->minor_version >= 91) { |
| 1355 | mddev->reshape_position = sb->reshape_position; |
| 1356 | mddev->delta_disks = sb->delta_disks; |
| 1357 | mddev->new_level = sb->new_level; |
| 1358 | mddev->new_layout = sb->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1359 | mddev->new_chunk_sectors = sb->new_chunk >> 9; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1360 | if (mddev->delta_disks < 0) |
| 1361 | mddev->reshape_backwards = 1; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1362 | } else { |
| 1363 | mddev->reshape_position = MaxSector; |
| 1364 | mddev->delta_disks = 0; |
| 1365 | mddev->new_level = mddev->level; |
| 1366 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1367 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1368 | } |
NeilBrown | 33f2c35 | 2019-09-09 16:52:29 +1000 | [diff] [blame] | 1369 | if (mddev->level == 0) |
| 1370 | mddev->layout = -1; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1371 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1372 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 1373 | mddev->recovery_cp = MaxSector; |
| 1374 | else { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1375 | if (sb->events_hi == sb->cp_events_hi && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1376 | sb->events_lo == sb->cp_events_lo) { |
| 1377 | mddev->recovery_cp = sb->recovery_cp; |
| 1378 | } else |
| 1379 | mddev->recovery_cp = 0; |
| 1380 | } |
| 1381 | |
| 1382 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 1383 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 1384 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 1385 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 1386 | |
| 1387 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1388 | |
| 1389 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1390 | mddev->bitmap_info.file == NULL) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1391 | mddev->bitmap_info.offset = |
| 1392 | mddev->bitmap_info.default_offset; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1393 | mddev->bitmap_info.space = |
Dave Jones | c9ad020 | 2013-08-19 22:26:32 -0400 | [diff] [blame] | 1394 | mddev->bitmap_info.default_space; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1395 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1396 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1397 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1398 | /* Insist on good event counter while assembling, except |
| 1399 | * for spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1400 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1401 | if (sb->disks[rdev->desc_nr].state & ( |
| 1402 | (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE))) |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1403 | if (ev1 < mddev->events) |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1404 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1405 | } else if (mddev->bitmap) { |
| 1406 | /* if adding to array with a bitmap, then we can accept an |
| 1407 | * older device ... but not too old. |
| 1408 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1409 | if (ev1 < mddev->bitmap->events_cleared) |
| 1410 | return 0; |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1411 | if (ev1 < mddev->events) |
| 1412 | set_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1413 | } else { |
| 1414 | if (ev1 < mddev->events) |
| 1415 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1416 | return 0; |
| 1417 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1418 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1419 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1420 | desc = sb->disks + rdev->desc_nr; |
| 1421 | |
| 1422 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1423 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 1424 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 1425 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1426 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1427 | rdev->raid_disk = desc->raid_disk; |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1428 | rdev->saved_raid_disk = desc->raid_disk; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1429 | } else if (desc->state & (1<<MD_DISK_ACTIVE)) { |
| 1430 | /* active but not in sync implies recovery up to |
| 1431 | * reshape position. We don't know exactly where |
| 1432 | * that is, so set to zero for now */ |
| 1433 | if (mddev->minor_version >= 91) { |
| 1434 | rdev->recovery_offset = 0; |
| 1435 | rdev->raid_disk = desc->raid_disk; |
| 1436 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1437 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1438 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 1439 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 1440 | if (desc->state & (1<<MD_DISK_FAILFAST)) |
| 1441 | set_bit(FailFast, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1442 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1443 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1444 | return 0; |
| 1445 | } |
| 1446 | |
| 1447 | /* |
| 1448 | * sync_super for 0.90.0 |
| 1449 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1450 | static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | { |
| 1452 | mdp_super_t *sb; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1453 | struct md_rdev *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1454 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1455 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1456 | /* make rdev->sb match mddev data.. |
| 1457 | * |
| 1458 | * 1/ zero out disks |
| 1459 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 1460 | * 3/ any empty disks < next_spare become removed |
| 1461 | * |
| 1462 | * disks[0] gets initialised to REMOVED because |
| 1463 | * we cannot be sure from other fields if it has |
| 1464 | * been initialised or not. |
| 1465 | */ |
| 1466 | int i; |
| 1467 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 1468 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 1469 | rdev->sb_size = MD_SB_BYTES; |
| 1470 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1471 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | |
| 1473 | memset(sb, 0, sizeof(*sb)); |
| 1474 | |
| 1475 | sb->md_magic = MD_SB_MAGIC; |
| 1476 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | sb->patch_version = mddev->patch_version; |
| 1478 | sb->gvalid_words = 0; /* ignored */ |
| 1479 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 1480 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 1481 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 1482 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 1483 | |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 1484 | sb->ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 | sb->level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1486 | sb->size = mddev->dev_sectors / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | sb->raid_disks = mddev->raid_disks; |
| 1488 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1489 | sb->not_persistent = 0; |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 1490 | sb->utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1491 | sb->state = 0; |
| 1492 | sb->events_hi = (mddev->events>>32); |
| 1493 | sb->events_lo = (u32)mddev->events; |
| 1494 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1495 | if (mddev->reshape_position == MaxSector) |
| 1496 | sb->minor_version = 90; |
| 1497 | else { |
| 1498 | sb->minor_version = 91; |
| 1499 | sb->reshape_position = mddev->reshape_position; |
| 1500 | sb->new_level = mddev->new_level; |
| 1501 | sb->delta_disks = mddev->delta_disks; |
| 1502 | sb->new_layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1503 | sb->new_chunk = mddev->new_chunk_sectors << 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1504 | } |
| 1505 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1506 | if (mddev->in_sync) |
| 1507 | { |
| 1508 | sb->recovery_cp = mddev->recovery_cp; |
| 1509 | sb->cp_events_hi = (mddev->events>>32); |
| 1510 | sb->cp_events_lo = (u32)mddev->events; |
| 1511 | if (mddev->recovery_cp == MaxSector) |
| 1512 | sb->state = (1<< MD_SB_CLEAN); |
| 1513 | } else |
| 1514 | sb->recovery_cp = 0; |
| 1515 | |
| 1516 | sb->layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1517 | sb->chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1518 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1519 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1520 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 1521 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1522 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1523 | rdev_for_each(rdev2, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1524 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1525 | int desc_nr; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1526 | int is_active = test_bit(In_sync, &rdev2->flags); |
| 1527 | |
| 1528 | if (rdev2->raid_disk >= 0 && |
| 1529 | sb->minor_version >= 91) |
| 1530 | /* we have nowhere to store the recovery_offset, |
| 1531 | * but if it is not below the reshape_position, |
| 1532 | * we can piggy-back on that. |
| 1533 | */ |
| 1534 | is_active = 1; |
| 1535 | if (rdev2->raid_disk < 0 || |
| 1536 | test_bit(Faulty, &rdev2->flags)) |
| 1537 | is_active = 0; |
| 1538 | if (is_active) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1539 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1540 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1541 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1542 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1543 | d = &sb->disks[rdev2->desc_nr]; |
| 1544 | nr_disks++; |
| 1545 | d->number = rdev2->desc_nr; |
| 1546 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 1547 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1548 | if (is_active) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1549 | d->raid_disk = rdev2->raid_disk; |
| 1550 | else |
| 1551 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 1552 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1553 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1554 | else if (is_active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1555 | d->state = (1<<MD_DISK_ACTIVE); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1556 | if (test_bit(In_sync, &rdev2->flags)) |
| 1557 | d->state |= (1<<MD_DISK_SYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1558 | active++; |
| 1559 | working++; |
| 1560 | } else { |
| 1561 | d->state = 0; |
| 1562 | spare++; |
| 1563 | working++; |
| 1564 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1565 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 1566 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 1567 | if (test_bit(FailFast, &rdev2->flags)) |
| 1568 | d->state |= (1<<MD_DISK_FAILFAST); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1569 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1570 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 1571 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 1572 | mdp_disk_t *d = &sb->disks[i]; |
| 1573 | if (d->state == 0 && d->number == 0) { |
| 1574 | d->number = i; |
| 1575 | d->raid_disk = i; |
| 1576 | d->state = (1<<MD_DISK_REMOVED); |
| 1577 | d->state |= (1<<MD_DISK_FAULTY); |
| 1578 | failed++; |
| 1579 | } |
| 1580 | } |
| 1581 | sb->nr_disks = nr_disks; |
| 1582 | sb->active_disks = active; |
| 1583 | sb->working_disks = working; |
| 1584 | sb->failed_disks = failed; |
| 1585 | sb->spare_disks = spare; |
| 1586 | |
| 1587 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 1588 | sb->sb_csum = calc_sb_csum(sb); |
| 1589 | } |
| 1590 | |
| 1591 | /* |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1592 | * rdev_size_change for 0.90.0 |
| 1593 | */ |
| 1594 | static unsigned long long |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1595 | 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] | 1596 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1597 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1598 | return 0; /* component must fit device */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1599 | if (rdev->mddev->bitmap_info.offset) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1600 | return 0; /* can't move bitmap */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1601 | rdev->sb_start = calc_dev_sboffset(rdev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1602 | if (!num_sectors || num_sectors > rdev->sb_start) |
| 1603 | num_sectors = rdev->sb_start; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1604 | /* Limit to 4TB as metadata cannot record more than that. |
| 1605 | * 4TB == 2^32 KB, or 2*2^32 sectors. |
| 1606 | */ |
Christoph Hellwig | 72deb45 | 2019-04-05 18:08:59 +0200 | [diff] [blame] | 1607 | if ((u64)num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1) |
Arnd Bergmann | 3312c95 | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 1608 | num_sectors = (sector_t)(2ULL << 32) - 2; |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 1609 | do { |
| 1610 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1611 | rdev->sb_page); |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 1612 | } while (md_super_wait(rdev->mddev) < 0); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1613 | return num_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1614 | } |
| 1615 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1616 | static int |
| 1617 | super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset) |
| 1618 | { |
| 1619 | /* non-zero offset changes not possible with v0.90 */ |
| 1620 | return new_offset == 0; |
| 1621 | } |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1622 | |
| 1623 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1624 | * version 1 superblock |
| 1625 | */ |
| 1626 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1627 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1628 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1629 | __le32 disk_csum; |
| 1630 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1631 | unsigned long long newcsum; |
| 1632 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1633 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1634 | |
| 1635 | disk_csum = sb->sb_csum; |
| 1636 | sb->sb_csum = 0; |
| 1637 | newcsum = 0; |
NeilBrown | 1f3c990 | 2012-12-11 13:09:00 +1100 | [diff] [blame] | 1638 | for (; size >= 4; size -= 4) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1639 | newcsum += le32_to_cpu(*isuper++); |
| 1640 | |
| 1641 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1642 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1643 | |
| 1644 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1645 | sb->sb_csum = disk_csum; |
| 1646 | return cpu_to_le32(csum); |
| 1647 | } |
| 1648 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1649 | 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] | 1650 | { |
| 1651 | struct mdp_superblock_1 *sb; |
| 1652 | int ret; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1653 | sector_t sb_start; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1654 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1655 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1656 | int bmask; |
Yufen Yu | 228fc7d | 2019-10-30 18:47:02 +0800 | [diff] [blame] | 1657 | bool spare_disk = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1658 | |
| 1659 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1660 | * Calculate the position of the superblock in 512byte sectors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1661 | * It is always aligned to a 4K boundary and |
| 1662 | * depeding on minor_version, it can be: |
| 1663 | * 0: At least 8K, but less than 12K, from end of device |
| 1664 | * 1: At start of device |
| 1665 | * 2: 4K from start of device. |
| 1666 | */ |
| 1667 | switch(minor_version) { |
| 1668 | case 0: |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1669 | sb_start = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1670 | sb_start -= 8*2; |
| 1671 | sb_start &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1672 | break; |
| 1673 | case 1: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1674 | sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1675 | break; |
| 1676 | case 2: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1677 | sb_start = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1678 | break; |
| 1679 | default: |
| 1680 | return -EINVAL; |
| 1681 | } |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1682 | rdev->sb_start = sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1683 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1684 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1685 | * and it is safe to read 4k, so we do that |
| 1686 | */ |
| 1687 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1688 | if (ret) return ret; |
| 1689 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1690 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1691 | |
| 1692 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1693 | sb->major_version != cpu_to_le32(1) || |
| 1694 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1695 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1696 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1697 | return -EINVAL; |
| 1698 | |
| 1699 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1700 | pr_warn("md: invalid superblock checksum on %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1701 | bdevname(rdev->bdev,b)); |
| 1702 | return -EINVAL; |
| 1703 | } |
| 1704 | if (le64_to_cpu(sb->data_size) < 10) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1705 | pr_warn("md: data_size too small on %s\n", |
| 1706 | bdevname(rdev->bdev,b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1707 | return -EINVAL; |
| 1708 | } |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1709 | if (sb->pad0 || |
| 1710 | sb->pad3[0] || |
| 1711 | memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1]))) |
| 1712 | /* Some padding is non-zero, might be a new feature */ |
| 1713 | return -EINVAL; |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1714 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1715 | rdev->preferred_minor = 0xffff; |
| 1716 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1717 | rdev->new_data_offset = rdev->data_offset; |
| 1718 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) && |
| 1719 | (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET)) |
| 1720 | rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1721 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1722 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1723 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1724 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1725 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1726 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1727 | |
| 1728 | if (minor_version |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1729 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1730 | return -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1731 | if (minor_version |
| 1732 | && rdev->new_data_offset < sb_start + (rdev->sb_size/512)) |
| 1733 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1734 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1735 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1736 | rdev->desc_nr = -1; |
| 1737 | else |
| 1738 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1739 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1740 | if (!rdev->bb_page) { |
| 1741 | rdev->bb_page = alloc_page(GFP_KERNEL); |
| 1742 | if (!rdev->bb_page) |
| 1743 | return -ENOMEM; |
| 1744 | } |
| 1745 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) && |
| 1746 | rdev->badblocks.count == 0) { |
| 1747 | /* need to load the bad block list. |
| 1748 | * Currently we limit it to one page. |
| 1749 | */ |
| 1750 | s32 offset; |
| 1751 | sector_t bb_sector; |
Christoph Hellwig | 00485d0 | 2019-04-04 18:56:12 +0200 | [diff] [blame] | 1752 | __le64 *bbp; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1753 | int i; |
| 1754 | int sectors = le16_to_cpu(sb->bblog_size); |
| 1755 | if (sectors > (PAGE_SIZE / 512)) |
| 1756 | return -EINVAL; |
| 1757 | offset = le32_to_cpu(sb->bblog_offset); |
| 1758 | if (offset == 0) |
| 1759 | return -EINVAL; |
| 1760 | bb_sector = (long long)offset; |
| 1761 | if (!sync_page_io(rdev, bb_sector, sectors << 9, |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1762 | rdev->bb_page, REQ_OP_READ, 0, true)) |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1763 | return -EIO; |
Christoph Hellwig | 00485d0 | 2019-04-04 18:56:12 +0200 | [diff] [blame] | 1764 | bbp = (__le64 *)page_address(rdev->bb_page); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1765 | rdev->badblocks.shift = sb->bblog_shift; |
| 1766 | for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) { |
| 1767 | u64 bb = le64_to_cpu(*bbp); |
| 1768 | int count = bb & (0x3ff); |
| 1769 | u64 sector = bb >> 10; |
| 1770 | sector <<= sb->bblog_shift; |
| 1771 | count <<= sb->bblog_shift; |
| 1772 | if (bb + 1 == 0) |
| 1773 | break; |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 1774 | if (badblocks_set(&rdev->badblocks, sector, count, 1)) |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1775 | return -EINVAL; |
| 1776 | } |
NeilBrown | 486adf7 | 2013-04-24 11:42:44 +1000 | [diff] [blame] | 1777 | } else if (sb->bblog_offset != 0) |
| 1778 | rdev->badblocks.shift = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1779 | |
Pawel Baldysiak | ddc0882 | 2017-08-16 17:13:45 +0200 | [diff] [blame] | 1780 | if ((le32_to_cpu(sb->feature_map) & |
| 1781 | (MD_FEATURE_PPL | MD_FEATURE_MULTIPLE_PPLS))) { |
Artur Paszkiewicz | ea0213e | 2017-03-09 09:59:57 +0100 | [diff] [blame] | 1782 | rdev->ppl.offset = (__s16)le16_to_cpu(sb->ppl.offset); |
| 1783 | rdev->ppl.size = le16_to_cpu(sb->ppl.size); |
| 1784 | rdev->ppl.sector = rdev->sb_start + rdev->ppl.offset; |
| 1785 | } |
| 1786 | |
NeilBrown | 33f2c35 | 2019-09-09 16:52:29 +1000 | [diff] [blame] | 1787 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RAID0_LAYOUT) && |
| 1788 | sb->level != 0) |
| 1789 | return -EINVAL; |
| 1790 | |
Yufen Yu | 228fc7d | 2019-10-30 18:47:02 +0800 | [diff] [blame] | 1791 | /* not spare disk, or LEVEL_MULTIPATH */ |
| 1792 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH) || |
| 1793 | (rdev->desc_nr >= 0 && |
| 1794 | rdev->desc_nr < le32_to_cpu(sb->max_dev) && |
| 1795 | (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX || |
| 1796 | le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL))) |
| 1797 | spare_disk = false; |
Yufen Yu | 6a5cb53 | 2019-10-16 16:00:03 +0800 | [diff] [blame] | 1798 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1799 | if (!refdev) { |
Yufen Yu | 228fc7d | 2019-10-30 18:47:02 +0800 | [diff] [blame] | 1800 | if (!spare_disk) |
Yufen Yu | 6a5cb53 | 2019-10-16 16:00:03 +0800 | [diff] [blame] | 1801 | ret = 1; |
| 1802 | else |
| 1803 | ret = 0; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1804 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1805 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1806 | struct mdp_superblock_1 *refsb = page_address(refdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1807 | |
| 1808 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1809 | sb->level != refsb->level || |
| 1810 | sb->layout != refsb->layout || |
| 1811 | sb->chunksize != refsb->chunksize) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1812 | pr_warn("md: %s has strangely different superblock to %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1813 | bdevname(rdev->bdev,b), |
| 1814 | bdevname(refdev->bdev,b2)); |
| 1815 | return -EINVAL; |
| 1816 | } |
| 1817 | ev1 = le64_to_cpu(sb->events); |
| 1818 | ev2 = le64_to_cpu(refsb->events); |
| 1819 | |
Yufen Yu | 228fc7d | 2019-10-30 18:47:02 +0800 | [diff] [blame] | 1820 | if (!spare_disk && ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1821 | ret = 1; |
| 1822 | else |
| 1823 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1824 | } |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1825 | if (minor_version) { |
| 1826 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9); |
| 1827 | sectors -= rdev->data_offset; |
| 1828 | } else |
| 1829 | sectors = rdev->sb_start; |
| 1830 | if (sectors < le64_to_cpu(sb->data_size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1831 | return -EINVAL; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1832 | rdev->sectors = le64_to_cpu(sb->data_size); |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1833 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1834 | } |
| 1835 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1836 | static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1837 | { |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1838 | struct mdp_superblock_1 *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1839 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1840 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1841 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1842 | clear_bit(Faulty, &rdev->flags); |
| 1843 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1844 | clear_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1845 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1846 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1847 | if (mddev->raid_disks == 0) { |
| 1848 | mddev->major_version = 1; |
| 1849 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1850 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1851 | mddev->chunk_sectors = le32_to_cpu(sb->chunksize); |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 1852 | mddev->ctime = le64_to_cpu(sb->ctime); |
| 1853 | mddev->utime = le64_to_cpu(sb->utime); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1854 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1855 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1856 | mddev->layout = le32_to_cpu(sb->layout); |
| 1857 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1858 | mddev->dev_sectors = le64_to_cpu(sb->size); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1859 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1860 | mddev->bitmap_info.offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1861 | mddev->bitmap_info.space = 0; |
| 1862 | /* Default location for bitmap is 1K after superblock |
| 1863 | * using 3K - total of 4K |
| 1864 | */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1865 | mddev->bitmap_info.default_offset = 1024 >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1866 | mddev->bitmap_info.default_space = (4096-1024) >> 9; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1867 | mddev->reshape_backwards = 0; |
| 1868 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1869 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1870 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1871 | |
| 1872 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1873 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1874 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1875 | mddev->bitmap_info.file == NULL) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1876 | mddev->bitmap_info.offset = |
| 1877 | (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1878 | /* Metadata doesn't record how much space is available. |
| 1879 | * For 1.0, we assume we can use up to the superblock |
| 1880 | * if before, else to 4K beyond superblock. |
| 1881 | * For others, assume no change is possible. |
| 1882 | */ |
| 1883 | if (mddev->minor_version > 0) |
| 1884 | mddev->bitmap_info.space = 0; |
| 1885 | else if (mddev->bitmap_info.offset > 0) |
| 1886 | mddev->bitmap_info.space = |
| 1887 | 8 - mddev->bitmap_info.offset; |
| 1888 | else |
| 1889 | mddev->bitmap_info.space = |
| 1890 | -mddev->bitmap_info.offset; |
| 1891 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1892 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1893 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1894 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1895 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1896 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1897 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1898 | mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1899 | if (mddev->delta_disks < 0 || |
| 1900 | (mddev->delta_disks == 0 && |
| 1901 | (le32_to_cpu(sb->feature_map) |
| 1902 | & MD_FEATURE_RESHAPE_BACKWARDS))) |
| 1903 | mddev->reshape_backwards = 1; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1904 | } else { |
| 1905 | mddev->reshape_position = MaxSector; |
| 1906 | mddev->delta_disks = 0; |
| 1907 | mddev->new_level = mddev->level; |
| 1908 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1909 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1910 | } |
| 1911 | |
NeilBrown | 33f2c35 | 2019-09-09 16:52:29 +1000 | [diff] [blame] | 1912 | if (mddev->level == 0 && |
| 1913 | !(le32_to_cpu(sb->feature_map) & MD_FEATURE_RAID0_LAYOUT)) |
| 1914 | mddev->layout = -1; |
| 1915 | |
Song Liu | 486b0f7 | 2016-08-19 15:34:01 -0700 | [diff] [blame] | 1916 | if (le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL) |
Shaohua Li | a62ab49 | 2016-01-06 14:37:13 -0800 | [diff] [blame] | 1917 | set_bit(MD_HAS_JOURNAL, &mddev->flags); |
Artur Paszkiewicz | ea0213e | 2017-03-09 09:59:57 +0100 | [diff] [blame] | 1918 | |
Pawel Baldysiak | ddc0882 | 2017-08-16 17:13:45 +0200 | [diff] [blame] | 1919 | if (le32_to_cpu(sb->feature_map) & |
| 1920 | (MD_FEATURE_PPL | MD_FEATURE_MULTIPLE_PPLS)) { |
Artur Paszkiewicz | ea0213e | 2017-03-09 09:59:57 +0100 | [diff] [blame] | 1921 | if (le32_to_cpu(sb->feature_map) & |
| 1922 | (MD_FEATURE_BITMAP_OFFSET | MD_FEATURE_JOURNAL)) |
| 1923 | return -EINVAL; |
Pawel Baldysiak | ddc0882 | 2017-08-16 17:13:45 +0200 | [diff] [blame] | 1924 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_PPL) && |
| 1925 | (le32_to_cpu(sb->feature_map) & |
| 1926 | MD_FEATURE_MULTIPLE_PPLS)) |
| 1927 | return -EINVAL; |
Artur Paszkiewicz | ea0213e | 2017-03-09 09:59:57 +0100 | [diff] [blame] | 1928 | set_bit(MD_HAS_PPL, &mddev->flags); |
| 1929 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1930 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1931 | /* Insist of good event counter while assembling, except for |
| 1932 | * spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1933 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1934 | if (rdev->desc_nr >= 0 && |
| 1935 | rdev->desc_nr < le32_to_cpu(sb->max_dev) && |
Song Liu | a3dfbda | 2015-10-08 21:54:11 -0700 | [diff] [blame] | 1936 | (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX || |
| 1937 | le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL)) |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1938 | if (ev1 < mddev->events) |
| 1939 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1940 | } else if (mddev->bitmap) { |
| 1941 | /* If adding to array with a bitmap, then we can accept an |
| 1942 | * older device, but not too old. |
| 1943 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1944 | if (ev1 < mddev->bitmap->events_cleared) |
| 1945 | return 0; |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1946 | if (ev1 < mddev->events) |
| 1947 | set_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1948 | } else { |
| 1949 | if (ev1 < mddev->events) |
| 1950 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1951 | return 0; |
| 1952 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1953 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1954 | int role; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1955 | if (rdev->desc_nr < 0 || |
| 1956 | rdev->desc_nr >= le32_to_cpu(sb->max_dev)) { |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1957 | role = MD_DISK_ROLE_SPARE; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1958 | rdev->desc_nr = -1; |
| 1959 | } else |
| 1960 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1961 | switch(role) { |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1962 | case MD_DISK_ROLE_SPARE: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1963 | break; |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1964 | case MD_DISK_ROLE_FAULTY: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1965 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1966 | break; |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 1967 | case MD_DISK_ROLE_JOURNAL: /* journal device */ |
| 1968 | if (!(le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)) { |
| 1969 | /* journal device without journal feature */ |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1970 | 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] | 1971 | return -EINVAL; |
| 1972 | } |
| 1973 | set_bit(Journal, &rdev->flags); |
Shaohua Li | 3069aa8 | 2015-08-13 14:31:56 -0700 | [diff] [blame] | 1974 | rdev->journal_tail = le64_to_cpu(sb->journal_tail); |
Shaohua Li | 9b15603 | 2015-12-18 15:19:16 +1100 | [diff] [blame] | 1975 | rdev->raid_disk = 0; |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 1976 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1977 | default: |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1978 | rdev->saved_raid_disk = role; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1979 | if ((le32_to_cpu(sb->feature_map) & |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1980 | MD_FEATURE_RECOVERY_OFFSET)) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1981 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1982 | if (!(le32_to_cpu(sb->feature_map) & |
| 1983 | MD_FEATURE_RECOVERY_BITMAP)) |
| 1984 | rdev->saved_raid_disk = -1; |
Guoqing Jiang | 062f5b2a | 2019-07-24 11:09:20 +0200 | [diff] [blame] | 1985 | } else { |
| 1986 | /* |
| 1987 | * If the array is FROZEN, then the device can't |
| 1988 | * be in_sync with rest of array. |
| 1989 | */ |
| 1990 | if (!test_bit(MD_RECOVERY_FROZEN, |
| 1991 | &mddev->recovery)) |
| 1992 | set_bit(In_sync, &rdev->flags); |
| 1993 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1994 | rdev->raid_disk = role; |
| 1995 | break; |
| 1996 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1997 | if (sb->devflags & WriteMostly1) |
| 1998 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 1999 | if (sb->devflags & FailFast1) |
| 2000 | set_bit(FailFast, &rdev->flags); |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2001 | if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT) |
| 2002 | set_bit(Replacement, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 2003 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2004 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 2005 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2006 | return 0; |
| 2007 | } |
| 2008 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2009 | static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2010 | { |
| 2011 | struct mdp_superblock_1 *sb; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2012 | struct md_rdev *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2013 | int max_dev, i; |
| 2014 | /* make rdev->sb match mddev and rdev data. */ |
| 2015 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2016 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2017 | |
| 2018 | sb->feature_map = 0; |
| 2019 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 2020 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2021 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 2022 | |
| 2023 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 2024 | sb->events = cpu_to_le64(mddev->events); |
| 2025 | if (mddev->in_sync) |
| 2026 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
Shaohua Li | bd18f64 | 2015-09-02 13:49:50 -0700 | [diff] [blame] | 2027 | else if (test_bit(MD_JOURNAL_CLEAN, &mddev->flags)) |
| 2028 | sb->resync_offset = cpu_to_le64(MaxSector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2029 | else |
| 2030 | sb->resync_offset = cpu_to_le64(0); |
| 2031 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 2032 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2033 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 2034 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2035 | sb->size = cpu_to_le64(mddev->dev_sectors); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 2036 | sb->chunksize = cpu_to_le32(mddev->chunk_sectors); |
NeilBrown | 62e1e38 | 2009-05-26 09:40:59 +1000 | [diff] [blame] | 2037 | sb->level = cpu_to_le32(mddev->level); |
| 2038 | sb->layout = cpu_to_le32(mddev->layout); |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 2039 | if (test_bit(FailFast, &rdev->flags)) |
| 2040 | sb->devflags |= FailFast1; |
| 2041 | else |
| 2042 | sb->devflags &= ~FailFast1; |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 2043 | |
NeilBrown | aeb9b211 | 2011-08-25 14:43:08 +1000 | [diff] [blame] | 2044 | if (test_bit(WriteMostly, &rdev->flags)) |
| 2045 | sb->devflags |= WriteMostly1; |
| 2046 | else |
| 2047 | sb->devflags &= ~WriteMostly1; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2048 | sb->data_offset = cpu_to_le64(rdev->data_offset); |
| 2049 | sb->data_size = cpu_to_le64(rdev->sectors); |
NeilBrown | aeb9b211 | 2011-08-25 14:43:08 +1000 | [diff] [blame] | 2050 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 2051 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) { |
| 2052 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 2053 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 2054 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 2055 | |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2056 | if (rdev->raid_disk >= 0 && !test_bit(Journal, &rdev->flags) && |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2057 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 2058 | sb->feature_map |= |
| 2059 | cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 2060 | sb->recovery_offset = |
| 2061 | cpu_to_le64(rdev->recovery_offset); |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2062 | if (rdev->saved_raid_disk >= 0 && mddev->bitmap) |
| 2063 | sb->feature_map |= |
| 2064 | cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 2065 | } |
Shaohua Li | 3069aa8 | 2015-08-13 14:31:56 -0700 | [diff] [blame] | 2066 | /* Note: recovery_offset and journal_tail share space */ |
| 2067 | if (test_bit(Journal, &rdev->flags)) |
| 2068 | sb->journal_tail = cpu_to_le64(rdev->journal_tail); |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2069 | if (test_bit(Replacement, &rdev->flags)) |
| 2070 | sb->feature_map |= |
| 2071 | cpu_to_le32(MD_FEATURE_REPLACEMENT); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 2072 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 2073 | if (mddev->reshape_position != MaxSector) { |
| 2074 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 2075 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 2076 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 2077 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 2078 | sb->new_level = cpu_to_le32(mddev->new_level); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 2079 | sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2080 | if (mddev->delta_disks == 0 && |
| 2081 | mddev->reshape_backwards) |
| 2082 | sb->feature_map |
| 2083 | |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2084 | if (rdev->new_data_offset != rdev->data_offset) { |
| 2085 | sb->feature_map |
| 2086 | |= cpu_to_le32(MD_FEATURE_NEW_OFFSET); |
| 2087 | sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset |
| 2088 | - rdev->data_offset)); |
| 2089 | } |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 2090 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 2091 | |
Goldwyn Rodrigues | 3c462c8 | 2015-08-19 07:35:54 +1000 | [diff] [blame] | 2092 | if (mddev_is_clustered(mddev)) |
| 2093 | sb->feature_map |= cpu_to_le32(MD_FEATURE_CLUSTERED); |
| 2094 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2095 | if (rdev->badblocks.count == 0) |
| 2096 | /* Nothing to do for bad blocks*/ ; |
| 2097 | else if (sb->bblog_offset == 0) |
| 2098 | /* Cannot record bad blocks on this device */ |
| 2099 | md_error(mddev, rdev); |
| 2100 | else { |
| 2101 | struct badblocks *bb = &rdev->badblocks; |
Christoph Hellwig | ae50640 | 2019-04-04 18:56:13 +0200 | [diff] [blame] | 2102 | __le64 *bbp = (__le64 *)page_address(rdev->bb_page); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2103 | u64 *p = bb->page; |
| 2104 | sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS); |
| 2105 | if (bb->changed) { |
| 2106 | unsigned seq; |
| 2107 | |
| 2108 | retry: |
| 2109 | seq = read_seqbegin(&bb->lock); |
| 2110 | |
| 2111 | memset(bbp, 0xff, PAGE_SIZE); |
| 2112 | |
| 2113 | for (i = 0 ; i < bb->count ; i++) { |
majianpeng | 35f9ac2 | 2012-11-08 08:56:27 +0800 | [diff] [blame] | 2114 | u64 internal_bb = p[i]; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2115 | u64 store_bb = ((BB_OFFSET(internal_bb) << 10) |
| 2116 | | BB_LEN(internal_bb)); |
majianpeng | 35f9ac2 | 2012-11-08 08:56:27 +0800 | [diff] [blame] | 2117 | bbp[i] = cpu_to_le64(store_bb); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2118 | } |
NeilBrown | d096293 | 2012-03-19 12:46:41 +1100 | [diff] [blame] | 2119 | bb->changed = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2120 | if (read_seqretry(&bb->lock, seq)) |
| 2121 | goto retry; |
| 2122 | |
| 2123 | bb->sector = (rdev->sb_start + |
| 2124 | (int)le32_to_cpu(sb->bblog_offset)); |
| 2125 | bb->size = le16_to_cpu(sb->bblog_size); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2126 | } |
| 2127 | } |
| 2128 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2129 | max_dev = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2130 | rdev_for_each(rdev2, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2131 | if (rdev2->desc_nr+1 > max_dev) |
| 2132 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2133 | |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 2134 | if (max_dev > le32_to_cpu(sb->max_dev)) { |
| 2135 | int bmask; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2136 | sb->max_dev = cpu_to_le32(max_dev); |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 2137 | rdev->sb_size = max_dev * 2 + 256; |
| 2138 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
| 2139 | if (rdev->sb_size & bmask) |
| 2140 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
NeilBrown | ddcf352 | 2010-09-08 16:48:17 +1000 | [diff] [blame] | 2141 | } else |
| 2142 | max_dev = le32_to_cpu(sb->max_dev); |
| 2143 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2144 | for (i=0; i<max_dev;i++) |
Lidong Zhong | 8df7202 | 2017-06-12 10:45:55 +0800 | [diff] [blame] | 2145 | sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2146 | |
Song Liu | a97b789 | 2015-10-08 21:54:09 -0700 | [diff] [blame] | 2147 | if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) |
| 2148 | sb->feature_map |= cpu_to_le32(MD_FEATURE_JOURNAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2149 | |
Artur Paszkiewicz | ea0213e | 2017-03-09 09:59:57 +0100 | [diff] [blame] | 2150 | if (test_bit(MD_HAS_PPL, &mddev->flags)) { |
Pawel Baldysiak | ddc0882 | 2017-08-16 17:13:45 +0200 | [diff] [blame] | 2151 | if (test_bit(MD_HAS_MULTIPLE_PPLS, &mddev->flags)) |
| 2152 | sb->feature_map |= |
| 2153 | cpu_to_le32(MD_FEATURE_MULTIPLE_PPLS); |
| 2154 | else |
| 2155 | sb->feature_map |= cpu_to_le32(MD_FEATURE_PPL); |
Artur Paszkiewicz | ea0213e | 2017-03-09 09:59:57 +0100 | [diff] [blame] | 2156 | sb->ppl.offset = cpu_to_le16(rdev->ppl.offset); |
| 2157 | sb->ppl.size = cpu_to_le16(rdev->ppl.size); |
| 2158 | } |
| 2159 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2160 | rdev_for_each(rdev2, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2161 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2162 | if (test_bit(Faulty, &rdev2->flags)) |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 2163 | sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2164 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2165 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Song Liu | a97b789 | 2015-10-08 21:54:09 -0700 | [diff] [blame] | 2166 | else if (test_bit(Journal, &rdev2->flags)) |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 2167 | sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_JOURNAL); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 2168 | else if (rdev2->raid_disk >= 0) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 2169 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2170 | else |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 2171 | sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2172 | } |
| 2173 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2174 | sb->sb_csum = calc_sb_1_csum(sb); |
| 2175 | } |
| 2176 | |
Xiao Ni | d9c0fa5 | 2020-06-30 15:55:36 +0800 | [diff] [blame] | 2177 | static sector_t super_1_choose_bm_space(sector_t dev_size) |
| 2178 | { |
| 2179 | sector_t bm_space; |
| 2180 | |
| 2181 | /* if the device is bigger than 8Gig, save 64k for bitmap |
| 2182 | * usage, if bigger than 200Gig, save 128k |
| 2183 | */ |
| 2184 | if (dev_size < 64*2) |
| 2185 | bm_space = 0; |
| 2186 | else if (dev_size - 64*2 >= 200*1024*1024*2) |
| 2187 | bm_space = 128*2; |
| 2188 | else if (dev_size - 4*2 > 8*1024*1024*2) |
| 2189 | bm_space = 64*2; |
| 2190 | else |
| 2191 | bm_space = 4*2; |
| 2192 | return bm_space; |
| 2193 | } |
| 2194 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2195 | static unsigned long long |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2196 | 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] | 2197 | { |
| 2198 | struct mdp_superblock_1 *sb; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 2199 | sector_t max_sectors; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2200 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2201 | return 0; /* component must fit device */ |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2202 | if (rdev->data_offset != rdev->new_data_offset) |
| 2203 | return 0; /* too confusing */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2204 | if (rdev->sb_start < rdev->data_offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2205 | /* minor versions 1 and 2; superblock before data */ |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 2206 | max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 2207 | max_sectors -= rdev->data_offset; |
| 2208 | if (!num_sectors || num_sectors > max_sectors) |
| 2209 | num_sectors = max_sectors; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 2210 | } else if (rdev->mddev->bitmap_info.offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2211 | /* minor version 0 with bitmap we can't move */ |
| 2212 | return 0; |
| 2213 | } else { |
| 2214 | /* minor version 0; superblock after data */ |
Xiao Ni | d9c0fa5 | 2020-06-30 15:55:36 +0800 | [diff] [blame] | 2215 | sector_t sb_start, bm_space; |
| 2216 | sector_t dev_size = i_size_read(rdev->bdev->bd_inode) >> 9; |
| 2217 | |
| 2218 | /* 8K is for superblock */ |
| 2219 | sb_start = dev_size - 8*2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2220 | sb_start &= ~(sector_t)(4*2 - 1); |
Xiao Ni | d9c0fa5 | 2020-06-30 15:55:36 +0800 | [diff] [blame] | 2221 | |
| 2222 | bm_space = super_1_choose_bm_space(dev_size); |
| 2223 | |
| 2224 | /* Space that can be used to store date needs to decrease |
| 2225 | * superblock bitmap space and bad block space(4K) |
| 2226 | */ |
| 2227 | max_sectors = sb_start - bm_space - 4*2; |
| 2228 | |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 2229 | if (!num_sectors || num_sectors > max_sectors) |
| 2230 | num_sectors = max_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2231 | } |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2232 | sb = page_address(rdev->sb_page); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 2233 | sb->data_size = cpu_to_le64(num_sectors); |
Jason Yan | 3fb632e | 2017-03-10 11:27:23 +0800 | [diff] [blame] | 2234 | sb->super_offset = cpu_to_le64(rdev->sb_start); |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2235 | sb->sb_csum = calc_sb_1_csum(sb); |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 2236 | do { |
| 2237 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
| 2238 | rdev->sb_page); |
| 2239 | } while (md_super_wait(rdev->mddev) < 0); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 2240 | return num_sectors; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2241 | |
| 2242 | } |
| 2243 | |
| 2244 | static int |
| 2245 | super_1_allow_new_offset(struct md_rdev *rdev, |
| 2246 | unsigned long long new_offset) |
| 2247 | { |
| 2248 | /* All necessary checks on new >= old have been done */ |
| 2249 | struct bitmap *bitmap; |
| 2250 | if (new_offset >= rdev->data_offset) |
| 2251 | return 1; |
| 2252 | |
| 2253 | /* with 1.0 metadata, there is no metadata to tread on |
| 2254 | * so we can always move back */ |
| 2255 | if (rdev->mddev->minor_version == 0) |
| 2256 | return 1; |
| 2257 | |
| 2258 | /* otherwise we must be sure not to step on |
| 2259 | * any metadata, so stay: |
| 2260 | * 36K beyond start of superblock |
| 2261 | * beyond end of badblocks |
| 2262 | * beyond write-intent bitmap |
| 2263 | */ |
| 2264 | if (rdev->sb_start + (32+4)*2 > new_offset) |
| 2265 | return 0; |
| 2266 | bitmap = rdev->mddev->bitmap; |
| 2267 | if (bitmap && !rdev->mddev->bitmap_info.file && |
| 2268 | rdev->sb_start + rdev->mddev->bitmap_info.offset + |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 2269 | bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2270 | return 0; |
| 2271 | if (rdev->badblocks.sector + rdev->badblocks.size > new_offset) |
| 2272 | return 0; |
| 2273 | |
| 2274 | return 1; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2275 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2276 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 2277 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2278 | [0] = { |
| 2279 | .name = "0.90.0", |
| 2280 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2281 | .load_super = super_90_load, |
| 2282 | .validate_super = super_90_validate, |
| 2283 | .sync_super = super_90_sync, |
| 2284 | .rdev_size_change = super_90_rdev_size_change, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2285 | .allow_new_offset = super_90_allow_new_offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2286 | }, |
| 2287 | [1] = { |
| 2288 | .name = "md-1", |
| 2289 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2290 | .load_super = super_1_load, |
| 2291 | .validate_super = super_1_validate, |
| 2292 | .sync_super = super_1_sync, |
| 2293 | .rdev_size_change = super_1_rdev_size_change, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2294 | .allow_new_offset = super_1_allow_new_offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2295 | }, |
| 2296 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2297 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2298 | static void sync_super(struct mddev *mddev, struct md_rdev *rdev) |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 2299 | { |
| 2300 | if (mddev->sync_super) { |
| 2301 | mddev->sync_super(mddev, rdev); |
| 2302 | return; |
| 2303 | } |
| 2304 | |
| 2305 | BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types)); |
| 2306 | |
| 2307 | super_types[mddev->major_version].sync_super(mddev, rdev); |
| 2308 | } |
| 2309 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2310 | static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2311 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2312 | struct md_rdev *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2313 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2314 | rcu_read_lock(); |
Song Liu | 0b020e8 | 2015-09-03 23:00:35 -0700 | [diff] [blame] | 2315 | rdev_for_each_rcu(rdev, mddev1) { |
| 2316 | if (test_bit(Faulty, &rdev->flags) || |
| 2317 | test_bit(Journal, &rdev->flags) || |
| 2318 | rdev->raid_disk == -1) |
| 2319 | continue; |
| 2320 | rdev_for_each_rcu(rdev2, mddev2) { |
| 2321 | if (test_bit(Faulty, &rdev2->flags) || |
| 2322 | test_bit(Journal, &rdev2->flags) || |
| 2323 | rdev2->raid_disk == -1) |
| 2324 | continue; |
Christoph Hellwig | 61a27e1f | 2020-09-03 07:40:58 +0200 | [diff] [blame] | 2325 | if (rdev->bdev->bd_disk == rdev2->bdev->bd_disk) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2326 | rcu_read_unlock(); |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 2327 | return 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2328 | } |
Song Liu | 0b020e8 | 2015-09-03 23:00:35 -0700 | [diff] [blame] | 2329 | } |
| 2330 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2331 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2332 | return 0; |
| 2333 | } |
| 2334 | |
| 2335 | static LIST_HEAD(pending_raid_disks); |
| 2336 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2337 | /* |
| 2338 | * Try to register data integrity profile for an mddev |
| 2339 | * |
| 2340 | * This is called when an array is started and after a disk has been kicked |
| 2341 | * from the array. It only succeeds if all working and active component devices |
| 2342 | * are integrity capable with matching profiles. |
| 2343 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2344 | int md_integrity_register(struct mddev *mddev) |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2345 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2346 | struct md_rdev *rdev, *reference = NULL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2347 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2348 | if (list_empty(&mddev->disks)) |
| 2349 | return 0; /* nothing to do */ |
Jonathan Brassow | 629acb6 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 2350 | if (!mddev->gendisk || blk_get_integrity(mddev->gendisk)) |
| 2351 | return 0; /* shouldn't register, or already is */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2352 | rdev_for_each(rdev, mddev) { |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2353 | /* skip spares and non-functional disks */ |
| 2354 | if (test_bit(Faulty, &rdev->flags)) |
| 2355 | continue; |
| 2356 | if (rdev->raid_disk < 0) |
| 2357 | continue; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2358 | if (!reference) { |
| 2359 | /* Use the first rdev as the reference */ |
| 2360 | reference = rdev; |
| 2361 | continue; |
| 2362 | } |
| 2363 | /* does this rdev's profile match the reference profile? */ |
| 2364 | if (blk_integrity_compare(reference->bdev->bd_disk, |
| 2365 | rdev->bdev->bd_disk) < 0) |
| 2366 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2367 | } |
Martin K. Petersen | 89078d5 | 2011-03-28 20:09:12 -0400 | [diff] [blame] | 2368 | if (!reference || !bdev_get_integrity(reference->bdev)) |
| 2369 | return 0; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2370 | /* |
| 2371 | * All component devices are integrity capable and have matching |
| 2372 | * profiles, register the common profile for the md device. |
| 2373 | */ |
Martin K. Petersen | 25520d5 | 2015-10-21 13:19:49 -0400 | [diff] [blame] | 2374 | blk_integrity_register(mddev->gendisk, |
| 2375 | bdev_get_integrity(reference->bdev)); |
| 2376 | |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 2377 | pr_debug("md: data integrity enabled on %s\n", mdname(mddev)); |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 2378 | if (bioset_integrity_create(&mddev->bio_set, BIO_POOL_SIZE)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 2379 | pr_err("md: failed to create integrity pool for %s\n", |
Martin K. Petersen | a91a278 | 2011-03-17 11:11:05 +0100 | [diff] [blame] | 2380 | mdname(mddev)); |
| 2381 | return -EINVAL; |
| 2382 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2383 | return 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2384 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2385 | EXPORT_SYMBOL(md_integrity_register); |
| 2386 | |
Dan Williams | 1501efa | 2016-01-13 16:00:07 -0800 | [diff] [blame] | 2387 | /* |
| 2388 | * Attempt to add an rdev, but only if it is consistent with the current |
| 2389 | * integrity profile |
| 2390 | */ |
| 2391 | int md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev) |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2392 | { |
Jonathan Brassow | 2863b9e | 2012-10-11 13:38:58 +1100 | [diff] [blame] | 2393 | struct blk_integrity *bi_mddev; |
Dan Williams | 1501efa | 2016-01-13 16:00:07 -0800 | [diff] [blame] | 2394 | char name[BDEVNAME_SIZE]; |
Jonathan Brassow | 2863b9e | 2012-10-11 13:38:58 +1100 | [diff] [blame] | 2395 | |
| 2396 | if (!mddev->gendisk) |
Dan Williams | 1501efa | 2016-01-13 16:00:07 -0800 | [diff] [blame] | 2397 | return 0; |
Jonathan Brassow | 2863b9e | 2012-10-11 13:38:58 +1100 | [diff] [blame] | 2398 | |
Jonathan Brassow | 2863b9e | 2012-10-11 13:38:58 +1100 | [diff] [blame] | 2399 | bi_mddev = blk_get_integrity(mddev->gendisk); |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2400 | |
| 2401 | if (!bi_mddev) /* nothing to do */ |
Dan Williams | 1501efa | 2016-01-13 16:00:07 -0800 | [diff] [blame] | 2402 | return 0; |
| 2403 | |
| 2404 | if (blk_integrity_compare(mddev->gendisk, rdev->bdev->bd_disk) != 0) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 2405 | pr_err("%s: incompatible integrity profile for %s\n", |
| 2406 | mdname(mddev), bdevname(rdev->bdev, name)); |
Dan Williams | 1501efa | 2016-01-13 16:00:07 -0800 | [diff] [blame] | 2407 | return -ENXIO; |
| 2408 | } |
| 2409 | |
| 2410 | return 0; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2411 | } |
| 2412 | EXPORT_SYMBOL(md_integrity_add_rdev); |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2413 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2414 | 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] | 2415 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 2416 | char b[BDEVNAME_SIZE]; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2417 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2418 | |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2419 | /* prevent duplicates */ |
| 2420 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 2421 | return -EEXIST; |
| 2422 | |
NeilBrown | 97b20ef | 2017-04-13 08:53:48 +1000 | [diff] [blame] | 2423 | if ((bdev_read_only(rdev->bdev) || bdev_read_only(rdev->meta_bdev)) && |
| 2424 | mddev->pers) |
| 2425 | return -EROFS; |
| 2426 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2427 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 2428 | if (!test_bit(Journal, &rdev->flags) && |
| 2429 | rdev->sectors && |
| 2430 | (mddev->dev_sectors == 0 || rdev->sectors < mddev->dev_sectors)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2431 | if (mddev->pers) { |
| 2432 | /* Cannot change size, so fail |
| 2433 | * If mddev->level <= 0, then we don't care |
| 2434 | * about aligning sizes (e.g. linear) |
| 2435 | */ |
| 2436 | if (mddev->level > 0) |
| 2437 | return -ENOSPC; |
| 2438 | } else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2439 | mddev->dev_sectors = rdev->sectors; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2440 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2441 | |
| 2442 | /* Verify rdev->desc_nr is unique. |
| 2443 | * If it is -1, assign a free number, else |
| 2444 | * check number is not in use |
| 2445 | */ |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2446 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2447 | if (rdev->desc_nr < 0) { |
| 2448 | int choice = 0; |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2449 | if (mddev->pers) |
| 2450 | choice = mddev->raid_disks; |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 2451 | while (md_find_rdev_nr_rcu(mddev, choice)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2452 | choice++; |
| 2453 | rdev->desc_nr = choice; |
| 2454 | } else { |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 2455 | if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) { |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2456 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2457 | return -EBUSY; |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2458 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2459 | } |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2460 | rcu_read_unlock(); |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 2461 | if (!test_bit(Journal, &rdev->flags) && |
| 2462 | mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 2463 | pr_warn("md: %s: array is limited to %d devices\n", |
| 2464 | mdname(mddev), mddev->max_disks); |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 2465 | return -EBUSY; |
| 2466 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 2467 | bdevname(rdev->bdev,b); |
Rasmus Villemoes | 90a9bef | 2015-06-25 15:02:36 -0700 | [diff] [blame] | 2468 | strreplace(b, '/', '!'); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2469 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2470 | rdev->mddev = mddev; |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 2471 | pr_debug("md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2472 | |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 2473 | if (mddev->raid_disks) |
Guoqing Jiang | 404659c | 2019-12-23 10:48:53 +0100 | [diff] [blame] | 2474 | mddev_create_serial_pool(mddev, rdev, false); |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 2475 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2476 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2477 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2478 | |
Damien Le Moal | 5e3b8a8 | 2020-07-16 13:54:40 +0900 | [diff] [blame] | 2479 | /* failure here is OK */ |
Christoph Hellwig | 8d65269 | 2020-11-17 08:18:55 +0100 | [diff] [blame^] | 2480 | err = sysfs_create_link(&rdev->kobj, bdev_kobj(rdev->bdev), "block"); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2481 | rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state"); |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 2482 | rdev->sysfs_unack_badblocks = |
| 2483 | sysfs_get_dirent_safe(rdev->kobj.sd, "unacknowledged_bad_blocks"); |
| 2484 | rdev->sysfs_badblocks = |
| 2485 | sysfs_get_dirent_safe(rdev->kobj.sd, "bad_blocks"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2486 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2487 | list_add_rcu(&rdev->same_set, &mddev->disks); |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2488 | bd_link_disk_holder(rdev->bdev, mddev->gendisk); |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 2489 | |
| 2490 | /* May as well allow recovery to be retried once */ |
NeilBrown | 5389042 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2491 | mddev->recovery_disabled++; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2492 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2493 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2494 | |
| 2495 | fail: |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 2496 | pr_warn("md: failed to register dev-%s for %s\n", |
| 2497 | b, mdname(mddev)); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2498 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2499 | } |
| 2500 | |
Guoqing Jiang | cc1ffe6 | 2020-04-04 23:57:08 +0200 | [diff] [blame] | 2501 | static void rdev_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2502 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2503 | struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2504 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2505 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2506 | } |
| 2507 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2508 | static void unbind_rdev_from_array(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2509 | { |
| 2510 | char b[BDEVNAME_SIZE]; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 2511 | |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 2512 | bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2513 | list_del_rcu(&rdev->same_set); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 2514 | pr_debug("md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
Guoqing Jiang | 11d3a9f | 2019-12-23 10:48:55 +0100 | [diff] [blame] | 2515 | mddev_destroy_serial_pool(rdev->mddev, rdev, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2516 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2517 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2518 | sysfs_put(rdev->sysfs_state); |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 2519 | sysfs_put(rdev->sysfs_unack_badblocks); |
| 2520 | sysfs_put(rdev->sysfs_badblocks); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2521 | rdev->sysfs_state = NULL; |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 2522 | rdev->sysfs_unack_badblocks = NULL; |
| 2523 | rdev->sysfs_badblocks = NULL; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 2524 | rdev->badblocks.count = 0; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2525 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2526 | * writing to 'remove' to "dev/state". We also need |
| 2527 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2528 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2529 | synchronize_rcu(); |
Guoqing Jiang | cc1ffe6 | 2020-04-04 23:57:08 +0200 | [diff] [blame] | 2530 | INIT_WORK(&rdev->del_work, rdev_delayed_delete); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2531 | kobject_get(&rdev->kobj); |
Guoqing Jiang | cc1ffe6 | 2020-04-04 23:57:08 +0200 | [diff] [blame] | 2532 | queue_work(md_rdev_misc_wq, &rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2533 | } |
| 2534 | |
| 2535 | /* |
| 2536 | * prevent the device from being mounted, repartitioned or |
| 2537 | * otherwise reused by a RAID array (or any other kernel |
| 2538 | * subsystem), by bd_claiming the device. |
| 2539 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2540 | 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] | 2541 | { |
| 2542 | int err = 0; |
| 2543 | struct block_device *bdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2544 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 2545 | bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2546 | shared ? (struct md_rdev *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2547 | if (IS_ERR(bdev)) { |
Christoph Hellwig | ea3edd4 | 2020-03-24 08:25:11 +0100 | [diff] [blame] | 2548 | pr_warn("md: could not open device unknown-block(%u,%u).\n", |
| 2549 | MAJOR(dev), MINOR(dev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2550 | return PTR_ERR(bdev); |
| 2551 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2552 | rdev->bdev = bdev; |
| 2553 | return err; |
| 2554 | } |
| 2555 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2556 | static void unlock_rdev(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2557 | { |
| 2558 | struct block_device *bdev = rdev->bdev; |
| 2559 | rdev->bdev = NULL; |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2560 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2561 | } |
| 2562 | |
| 2563 | void md_autodetect_dev(dev_t dev); |
| 2564 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2565 | static void export_rdev(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2566 | { |
| 2567 | char b[BDEVNAME_SIZE]; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 2568 | |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 2569 | pr_debug("md: export_rdev(%s)\n", bdevname(rdev->bdev,b)); |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 2570 | md_rdev_clear(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2571 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 2572 | if (test_bit(AutoDetected, &rdev->flags)) |
| 2573 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2574 | #endif |
| 2575 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2576 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2577 | } |
| 2578 | |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 2579 | void md_kick_rdev_from_array(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2580 | { |
| 2581 | unbind_rdev_from_array(rdev); |
| 2582 | export_rdev(rdev); |
| 2583 | } |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 2584 | EXPORT_SYMBOL_GPL(md_kick_rdev_from_array); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2585 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2586 | static void export_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2587 | { |
NeilBrown | 0638bb0 | 2014-09-25 17:43:47 +1000 | [diff] [blame] | 2588 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2589 | |
NeilBrown | 0638bb0 | 2014-09-25 17:43:47 +1000 | [diff] [blame] | 2590 | while (!list_empty(&mddev->disks)) { |
| 2591 | rdev = list_first_entry(&mddev->disks, struct md_rdev, |
| 2592 | same_set); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 2593 | md_kick_rdev_from_array(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2594 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2595 | mddev->raid_disks = 0; |
| 2596 | mddev->major_version = 0; |
| 2597 | } |
| 2598 | |
NeilBrown | 6497709b | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 2599 | static bool set_in_sync(struct mddev *mddev) |
| 2600 | { |
Shaohua Li | efa4b77 | 2017-10-18 22:08:13 -0700 | [diff] [blame] | 2601 | lockdep_assert_held(&mddev->lock); |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 2602 | if (!mddev->in_sync) { |
| 2603 | mddev->sync_checkers++; |
| 2604 | spin_unlock(&mddev->lock); |
| 2605 | percpu_ref_switch_to_atomic_sync(&mddev->writes_pending); |
| 2606 | spin_lock(&mddev->lock); |
| 2607 | if (!mddev->in_sync && |
| 2608 | percpu_ref_is_zero(&mddev->writes_pending)) { |
NeilBrown | 6497709b | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 2609 | mddev->in_sync = 1; |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 2610 | /* |
| 2611 | * Ensure ->in_sync is visible before we clear |
| 2612 | * ->sync_checkers. |
| 2613 | */ |
NeilBrown | 55cc39f | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 2614 | smp_mb(); |
NeilBrown | 6497709b | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 2615 | set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags); |
| 2616 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
| 2617 | } |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 2618 | if (--mddev->sync_checkers == 0) |
| 2619 | percpu_ref_switch_to_percpu(&mddev->writes_pending); |
NeilBrown | 6497709b | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 2620 | } |
| 2621 | if (mddev->safemode == 1) |
| 2622 | mddev->safemode = 0; |
| 2623 | return mddev->in_sync; |
| 2624 | } |
| 2625 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2626 | static void sync_sbs(struct mddev *mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2627 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2628 | /* Update each superblock (in-memory image), but |
| 2629 | * if we are allowed to, skip spares which already |
| 2630 | * have the right event counter, or have one earlier |
| 2631 | * (which would mean they aren't being marked as dirty |
| 2632 | * with the rest of the array) |
| 2633 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2634 | struct md_rdev *rdev; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2635 | rdev_for_each(rdev, mddev) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2636 | if (rdev->sb_events == mddev->events || |
| 2637 | (nospares && |
| 2638 | rdev->raid_disk < 0 && |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2639 | rdev->sb_events+1 == mddev->events)) { |
| 2640 | /* Don't update this superblock */ |
| 2641 | rdev->sb_loaded = 2; |
| 2642 | } else { |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 2643 | sync_super(mddev, rdev); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2644 | rdev->sb_loaded = 1; |
| 2645 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2646 | } |
| 2647 | } |
| 2648 | |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2649 | static bool does_sb_need_changing(struct mddev *mddev) |
| 2650 | { |
| 2651 | struct md_rdev *rdev; |
| 2652 | struct mdp_superblock_1 *sb; |
| 2653 | int role; |
| 2654 | |
| 2655 | /* Find a good rdev */ |
| 2656 | rdev_for_each(rdev, mddev) |
| 2657 | if ((rdev->raid_disk >= 0) && !test_bit(Faulty, &rdev->flags)) |
| 2658 | break; |
| 2659 | |
| 2660 | /* No good device found. */ |
| 2661 | if (!rdev) |
| 2662 | return false; |
| 2663 | |
| 2664 | sb = page_address(rdev->sb_page); |
| 2665 | /* Check if a device has become faulty or a spare become active */ |
| 2666 | rdev_for_each(rdev, mddev) { |
| 2667 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
| 2668 | /* Device activated? */ |
| 2669 | if (role == 0xffff && rdev->raid_disk >=0 && |
| 2670 | !test_bit(Faulty, &rdev->flags)) |
| 2671 | return true; |
| 2672 | /* Device turned faulty? */ |
| 2673 | if (test_bit(Faulty, &rdev->flags) && (role < 0xfffd)) |
| 2674 | return true; |
| 2675 | } |
| 2676 | |
| 2677 | /* Check if any mddev parameters have changed */ |
| 2678 | if ((mddev->dev_sectors != le64_to_cpu(sb->size)) || |
| 2679 | (mddev->reshape_position != le64_to_cpu(sb->reshape_position)) || |
Jason Yan | 1345921 | 2017-03-10 11:49:12 +0800 | [diff] [blame] | 2680 | (mddev->layout != le32_to_cpu(sb->layout)) || |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2681 | (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) || |
| 2682 | (mddev->chunk_sectors != le32_to_cpu(sb->chunksize))) |
| 2683 | return true; |
| 2684 | |
| 2685 | return false; |
| 2686 | } |
| 2687 | |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 2688 | void md_update_sb(struct mddev *mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2689 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2690 | struct md_rdev *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2691 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2692 | int nospares = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2693 | int any_badblocks_changed = 0; |
Guoqing Jiang | 23b63f9 | 2015-10-12 17:21:30 +0800 | [diff] [blame] | 2694 | int ret = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2695 | |
NeilBrown | d87f064 | 2013-04-24 11:42:40 +1000 | [diff] [blame] | 2696 | if (mddev->ro) { |
| 2697 | if (force_change) |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2698 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | d87f064 | 2013-04-24 11:42:40 +1000 | [diff] [blame] | 2699 | return; |
| 2700 | } |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2701 | |
Guoqing Jiang | 2c97cf1 | 2016-05-02 11:33:09 -0400 | [diff] [blame] | 2702 | repeat: |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2703 | if (mddev_is_clustered(mddev)) { |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2704 | if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)) |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2705 | force_change = 1; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2706 | if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags)) |
Guoqing Jiang | 85ad1d1 | 2016-05-03 22:22:13 -0400 | [diff] [blame] | 2707 | nospares = 1; |
Guoqing Jiang | 23b63f9 | 2015-10-12 17:21:30 +0800 | [diff] [blame] | 2708 | ret = md_cluster_ops->metadata_update_start(mddev); |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2709 | /* Has someone else has updated the sb */ |
| 2710 | if (!does_sb_need_changing(mddev)) { |
Guoqing Jiang | 23b63f9 | 2015-10-12 17:21:30 +0800 | [diff] [blame] | 2711 | if (ret == 0) |
| 2712 | md_cluster_ops->metadata_update_cancel(mddev); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2713 | bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING), |
| 2714 | BIT(MD_SB_CHANGE_DEVS) | |
| 2715 | BIT(MD_SB_CHANGE_CLEAN)); |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2716 | return; |
| 2717 | } |
| 2718 | } |
Guoqing Jiang | 2c97cf1 | 2016-05-02 11:33:09 -0400 | [diff] [blame] | 2719 | |
NeilBrown | db0505d | 2017-10-17 16:18:36 +1100 | [diff] [blame] | 2720 | /* |
| 2721 | * First make sure individual recovery_offsets are correct |
| 2722 | * curr_resync_completed can only be used during recovery. |
| 2723 | * During reshape/resync it might use array-addresses rather |
| 2724 | * that device addresses. |
| 2725 | */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2726 | rdev_for_each(rdev, mddev) { |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2727 | if (rdev->raid_disk >= 0 && |
| 2728 | mddev->delta_disks >= 0 && |
NeilBrown | db0505d | 2017-10-17 16:18:36 +1100 | [diff] [blame] | 2729 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 2730 | test_bit(MD_RECOVERY_RECOVER, &mddev->recovery) && |
| 2731 | !test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2732 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2733 | !test_bit(In_sync, &rdev->flags) && |
| 2734 | mddev->curr_resync_completed > rdev->recovery_offset) |
| 2735 | rdev->recovery_offset = mddev->curr_resync_completed; |
| 2736 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2737 | } |
Dan Williams | bd52b74 | 2010-08-30 17:33:33 +1000 | [diff] [blame] | 2738 | if (!mddev->persistent) { |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2739 | clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags); |
| 2740 | clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2741 | if (!mddev->external) { |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2742 | clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2743 | rdev_for_each(rdev, mddev) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2744 | if (rdev->badblocks.changed) { |
NeilBrown | d096293 | 2012-03-19 12:46:41 +1100 | [diff] [blame] | 2745 | rdev->badblocks.changed = 0; |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 2746 | ack_all_badblocks(&rdev->badblocks); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2747 | md_error(mddev, rdev); |
| 2748 | } |
| 2749 | clear_bit(Blocked, &rdev->flags); |
| 2750 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2751 | wake_up(&rdev->blocked_wait); |
| 2752 | } |
| 2753 | } |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2754 | wake_up(&mddev->sb_wait); |
| 2755 | return; |
| 2756 | } |
| 2757 | |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 2758 | spin_lock(&mddev->lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2759 | |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 2760 | mddev->utime = ktime_get_real_seconds(); |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2761 | |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2762 | if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)) |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2763 | force_change = 1; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2764 | if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags)) |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2765 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 2766 | * though if events isn't the right even/odd, we will have to do |
| 2767 | * spares after all |
| 2768 | */ |
| 2769 | nospares = 1; |
| 2770 | if (force_change) |
| 2771 | nospares = 0; |
| 2772 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2773 | /* If the array is degraded, then skipping spares is both |
| 2774 | * dangerous and fairly pointless. |
| 2775 | * Dangerous because a device that was removed from the array |
| 2776 | * might have a event_count that still looks up-to-date, |
| 2777 | * so it can be re-added without a resync. |
| 2778 | * Pointless because if there are any spares to skip, |
| 2779 | * then a recovery will happen and soon that array won't |
| 2780 | * be degraded any more and the spare can go back to sleep then. |
| 2781 | */ |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2782 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2783 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2784 | sync_req = mddev->in_sync; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2785 | |
| 2786 | /* If this is just a dirty<->clean transition, and the array is clean |
| 2787 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2788 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2789 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2790 | && mddev->can_decrease_events |
| 2791 | && mddev->events != 1) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2792 | mddev->events--; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2793 | mddev->can_decrease_events = 0; |
| 2794 | } else { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2795 | /* otherwise we have to go forward and ... */ |
| 2796 | mddev->events ++; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2797 | mddev->can_decrease_events = nospares; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2798 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2799 | |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 2800 | /* |
| 2801 | * This 64-bit counter should never wrap. |
| 2802 | * Either we are in around ~1 trillion A.C., assuming |
| 2803 | * 1 reboot per second, or we have a bug... |
| 2804 | */ |
| 2805 | WARN_ON(mddev->events == 0); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2806 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2807 | rdev_for_each(rdev, mddev) { |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2808 | if (rdev->badblocks.changed) |
| 2809 | any_badblocks_changed++; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2810 | if (test_bit(Faulty, &rdev->flags)) |
| 2811 | set_bit(FaultRecorded, &rdev->flags); |
| 2812 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2813 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2814 | sync_sbs(mddev, nospares); |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 2815 | spin_unlock(&mddev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2816 | |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2817 | pr_debug("md: updating %s RAID superblock on device (in sync %d)\n", |
| 2818 | mdname(mddev), mddev->in_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2819 | |
Shaohua Li | 504634f | 2016-11-18 09:44:08 -0800 | [diff] [blame] | 2820 | if (mddev->queue) |
| 2821 | blk_add_trace_msg(mddev->queue, "md md_update_sb"); |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 2822 | rewrite: |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 2823 | md_bitmap_update_sb(mddev->bitmap); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2824 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2825 | char b[BDEVNAME_SIZE]; |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2826 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2827 | if (rdev->sb_loaded != 1) |
| 2828 | continue; /* no noise on spare devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2829 | |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2830 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2831 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2832 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2833 | rdev->sb_page); |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2834 | pr_debug("md: (write) %s's sb offset: %llu\n", |
| 2835 | bdevname(rdev->bdev, b), |
| 2836 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2837 | rdev->sb_events = mddev->events; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2838 | if (rdev->badblocks.size) { |
| 2839 | md_super_write(mddev, rdev, |
| 2840 | rdev->badblocks.sector, |
| 2841 | rdev->badblocks.size << 9, |
| 2842 | rdev->bb_page); |
| 2843 | rdev->badblocks.size = 0; |
| 2844 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2845 | |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2846 | } else |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2847 | pr_debug("md: %s (skipping faulty)\n", |
| 2848 | bdevname(rdev->bdev, b)); |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 2849 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2850 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2851 | /* only need to write one superblock... */ |
| 2852 | break; |
| 2853 | } |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 2854 | if (md_super_wait(mddev) < 0) |
| 2855 | goto rewrite; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2856 | /* 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] | 2857 | |
Guoqing Jiang | 2c97cf1 | 2016-05-02 11:33:09 -0400 | [diff] [blame] | 2858 | if (mddev_is_clustered(mddev) && ret == 0) |
| 2859 | md_cluster_ops->metadata_update_finish(mddev); |
| 2860 | |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2861 | if (mddev->in_sync != sync_req || |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2862 | !bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING), |
| 2863 | BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_CLEAN))) |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2864 | /* have to write it out again */ |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2865 | goto repeat; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2866 | wake_up(&mddev->sb_wait); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 2867 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 2868 | sysfs_notify_dirent_safe(mddev->sysfs_completed); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2869 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2870 | rdev_for_each(rdev, mddev) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2871 | if (test_and_clear_bit(FaultRecorded, &rdev->flags)) |
| 2872 | clear_bit(Blocked, &rdev->flags); |
| 2873 | |
| 2874 | if (any_badblocks_changed) |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 2875 | ack_all_badblocks(&rdev->badblocks); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2876 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2877 | wake_up(&rdev->blocked_wait); |
| 2878 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2879 | } |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 2880 | EXPORT_SYMBOL(md_update_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2881 | |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2882 | static int add_bound_rdev(struct md_rdev *rdev) |
| 2883 | { |
| 2884 | struct mddev *mddev = rdev->mddev; |
| 2885 | int err = 0; |
Shaohua Li | 87d4d91 | 2016-01-06 14:37:14 -0800 | [diff] [blame] | 2886 | bool add_journal = test_bit(Journal, &rdev->flags); |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2887 | |
Shaohua Li | 87d4d91 | 2016-01-06 14:37:14 -0800 | [diff] [blame] | 2888 | if (!mddev->pers->hot_remove_disk || add_journal) { |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2889 | /* If there is hot_add_disk but no hot_remove_disk |
| 2890 | * then added disks for geometry changes, |
| 2891 | * and should be added immediately. |
| 2892 | */ |
| 2893 | super_types[mddev->major_version]. |
| 2894 | validate_super(mddev, rdev); |
Shaohua Li | 87d4d91 | 2016-01-06 14:37:14 -0800 | [diff] [blame] | 2895 | if (add_journal) |
| 2896 | mddev_suspend(mddev); |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2897 | err = mddev->pers->hot_add_disk(mddev, rdev); |
Shaohua Li | 87d4d91 | 2016-01-06 14:37:14 -0800 | [diff] [blame] | 2898 | if (add_journal) |
| 2899 | mddev_resume(mddev); |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2900 | if (err) { |
Guoqing Jiang | db76767 | 2016-06-02 23:32:05 -0400 | [diff] [blame] | 2901 | md_kick_rdev_from_array(rdev); |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2902 | return err; |
| 2903 | } |
| 2904 | } |
| 2905 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
| 2906 | |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2907 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2908 | if (mddev->degraded) |
| 2909 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 2910 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 2911 | md_new_event(mddev); |
| 2912 | md_wakeup_thread(mddev->thread); |
| 2913 | return 0; |
| 2914 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2915 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 2916 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2917 | * We want to accept with case. For this we use cmd_match. |
| 2918 | */ |
| 2919 | static int cmd_match(const char *cmd, const char *str) |
| 2920 | { |
| 2921 | /* See if cmd, written into a sysfs file, matches |
| 2922 | * str. They must either be the same, or cmd can |
| 2923 | * have a trailing newline |
| 2924 | */ |
| 2925 | while (*cmd && *str && *cmd == *str) { |
| 2926 | cmd++; |
| 2927 | str++; |
| 2928 | } |
| 2929 | if (*cmd == '\n') |
| 2930 | cmd++; |
| 2931 | if (*str || *cmd) |
| 2932 | return 0; |
| 2933 | return 1; |
| 2934 | } |
| 2935 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2936 | struct rdev_sysfs_entry { |
| 2937 | struct attribute attr; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2938 | ssize_t (*show)(struct md_rdev *, char *); |
| 2939 | ssize_t (*store)(struct md_rdev *, const char *, size_t); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2940 | }; |
| 2941 | |
| 2942 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2943 | state_show(struct md_rdev *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2944 | { |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 2945 | char *sep = ","; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2946 | size_t len = 0; |
Mark Rutland | 6aa7de0 | 2017-10-23 14:07:29 -0700 | [diff] [blame] | 2947 | unsigned long flags = READ_ONCE(rdev->flags); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2948 | |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2949 | if (test_bit(Faulty, &flags) || |
Tomasz Majchrzak | dcbcb48 | 2016-10-21 16:27:08 +0200 | [diff] [blame] | 2950 | (!test_bit(ExternalBbl, &flags) && |
| 2951 | rdev->badblocks.unacked_exist)) |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 2952 | len += sprintf(page+len, "faulty%s", sep); |
| 2953 | if (test_bit(In_sync, &flags)) |
| 2954 | len += sprintf(page+len, "in_sync%s", sep); |
| 2955 | if (test_bit(Journal, &flags)) |
| 2956 | len += sprintf(page+len, "journal%s", sep); |
| 2957 | if (test_bit(WriteMostly, &flags)) |
| 2958 | len += sprintf(page+len, "write_mostly%s", sep); |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2959 | if (test_bit(Blocked, &flags) || |
NeilBrown | 52c6415 | 2011-12-08 16:22:48 +1100 | [diff] [blame] | 2960 | (rdev->badblocks.unacked_exist |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 2961 | && !test_bit(Faulty, &flags))) |
| 2962 | len += sprintf(page+len, "blocked%s", sep); |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2963 | if (!test_bit(Faulty, &flags) && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2964 | !test_bit(Journal, &flags) && |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 2965 | !test_bit(In_sync, &flags)) |
| 2966 | len += sprintf(page+len, "spare%s", sep); |
| 2967 | if (test_bit(WriteErrorSeen, &flags)) |
| 2968 | len += sprintf(page+len, "write_error%s", sep); |
| 2969 | if (test_bit(WantReplacement, &flags)) |
| 2970 | len += sprintf(page+len, "want_replacement%s", sep); |
| 2971 | if (test_bit(Replacement, &flags)) |
| 2972 | len += sprintf(page+len, "replacement%s", sep); |
| 2973 | if (test_bit(ExternalBbl, &flags)) |
| 2974 | len += sprintf(page+len, "external_bbl%s", sep); |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 2975 | if (test_bit(FailFast, &flags)) |
| 2976 | len += sprintf(page+len, "failfast%s", sep); |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 2977 | |
| 2978 | if (len) |
| 2979 | len -= strlen(sep); |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2980 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2981 | return len+sprintf(page+len, "\n"); |
| 2982 | } |
| 2983 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2984 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2985 | state_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2986 | { |
| 2987 | /* can write |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2988 | * faulty - simulates an error |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2989 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2990 | * writemostly - sets write_mostly |
| 2991 | * -writemostly - clears write_mostly |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2992 | * blocked - sets the Blocked flags |
| 2993 | * -blocked - clears the Blocked and possibly simulates an error |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2994 | * insync - sets Insync providing device isn't active |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2995 | * -insync - clear Insync for a device with a slot assigned, |
| 2996 | * so that it gets rebuilt based on bitmap |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2997 | * write_error - sets WriteErrorSeen |
| 2998 | * -write_error - clears WriteErrorSeen |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 2999 | * {,-}failfast - set/clear FailFast |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3000 | */ |
| 3001 | int err = -EINVAL; |
| 3002 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 3003 | md_error(rdev->mddev, rdev); |
NeilBrown | 5ef56c8 | 2011-08-25 14:42:51 +1000 | [diff] [blame] | 3004 | if (test_bit(Faulty, &rdev->flags)) |
| 3005 | err = 0; |
| 3006 | else |
| 3007 | err = -EBUSY; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3008 | } else if (cmd_match(buf, "remove")) { |
Shaohua Li | 5d88178 | 2016-07-28 09:06:34 -0700 | [diff] [blame] | 3009 | if (rdev->mddev->pers) { |
| 3010 | clear_bit(Blocked, &rdev->flags); |
| 3011 | remove_and_add_spares(rdev->mddev, rdev); |
| 3012 | } |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3013 | if (rdev->raid_disk >= 0) |
| 3014 | err = -EBUSY; |
| 3015 | else { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3016 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3017 | err = 0; |
Guoqing Jiang | a972090 | 2015-10-12 17:21:27 +0800 | [diff] [blame] | 3018 | if (mddev_is_clustered(mddev)) |
| 3019 | err = md_cluster_ops->remove_disk(mddev, rdev); |
| 3020 | |
| 3021 | if (err == 0) { |
| 3022 | md_kick_rdev_from_array(rdev); |
NeilBrown | 060b068 | 2016-11-04 16:46:03 +1100 | [diff] [blame] | 3023 | if (mddev->pers) { |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 3024 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | 060b068 | 2016-11-04 16:46:03 +1100 | [diff] [blame] | 3025 | md_wakeup_thread(mddev->thread); |
| 3026 | } |
Guoqing Jiang | a972090 | 2015-10-12 17:21:27 +0800 | [diff] [blame] | 3027 | md_new_event(mddev); |
| 3028 | } |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3029 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3030 | } else if (cmd_match(buf, "writemostly")) { |
| 3031 | set_bit(WriteMostly, &rdev->flags); |
Guoqing Jiang | 404659c | 2019-12-23 10:48:53 +0100 | [diff] [blame] | 3032 | mddev_create_serial_pool(rdev->mddev, rdev, false); |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3033 | err = 0; |
| 3034 | } else if (cmd_match(buf, "-writemostly")) { |
Guoqing Jiang | 11d3a9f | 2019-12-23 10:48:55 +0100 | [diff] [blame] | 3035 | mddev_destroy_serial_pool(rdev->mddev, rdev, false); |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3036 | clear_bit(WriteMostly, &rdev->flags); |
| 3037 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3038 | } else if (cmd_match(buf, "blocked")) { |
| 3039 | set_bit(Blocked, &rdev->flags); |
| 3040 | err = 0; |
| 3041 | } else if (cmd_match(buf, "-blocked")) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 3042 | if (!test_bit(Faulty, &rdev->flags) && |
Tomasz Majchrzak | dcbcb48 | 2016-10-21 16:27:08 +0200 | [diff] [blame] | 3043 | !test_bit(ExternalBbl, &rdev->flags) && |
NeilBrown | 7da64a0 | 2011-08-30 16:20:17 +1000 | [diff] [blame] | 3044 | rdev->badblocks.unacked_exist) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 3045 | /* metadata handler doesn't understand badblocks, |
| 3046 | * so we need to fail the device |
| 3047 | */ |
| 3048 | md_error(rdev->mddev, rdev); |
| 3049 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3050 | clear_bit(Blocked, &rdev->flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 3051 | clear_bit(BlockedBadBlocks, &rdev->flags); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3052 | wake_up(&rdev->blocked_wait); |
| 3053 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 3054 | md_wakeup_thread(rdev->mddev->thread); |
| 3055 | |
| 3056 | err = 0; |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 3057 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { |
| 3058 | set_bit(In_sync, &rdev->flags); |
| 3059 | err = 0; |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 3060 | } else if (cmd_match(buf, "failfast")) { |
| 3061 | set_bit(FailFast, &rdev->flags); |
| 3062 | err = 0; |
| 3063 | } else if (cmd_match(buf, "-failfast")) { |
| 3064 | clear_bit(FailFast, &rdev->flags); |
| 3065 | err = 0; |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 3066 | } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0 && |
| 3067 | !test_bit(Journal, &rdev->flags)) { |
NeilBrown | e1960f8 | 2014-09-30 15:24:25 +1000 | [diff] [blame] | 3068 | if (rdev->mddev->pers == NULL) { |
| 3069 | clear_bit(In_sync, &rdev->flags); |
| 3070 | rdev->saved_raid_disk = rdev->raid_disk; |
| 3071 | rdev->raid_disk = -1; |
| 3072 | err = 0; |
| 3073 | } |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 3074 | } else if (cmd_match(buf, "write_error")) { |
| 3075 | set_bit(WriteErrorSeen, &rdev->flags); |
| 3076 | err = 0; |
| 3077 | } else if (cmd_match(buf, "-write_error")) { |
| 3078 | clear_bit(WriteErrorSeen, &rdev->flags); |
| 3079 | err = 0; |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 3080 | } else if (cmd_match(buf, "want_replacement")) { |
| 3081 | /* Any non-spare device that is not a replacement can |
| 3082 | * become want_replacement at any time, but we then need to |
| 3083 | * check if recovery is needed. |
| 3084 | */ |
| 3085 | if (rdev->raid_disk >= 0 && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 3086 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 3087 | !test_bit(Replacement, &rdev->flags)) |
| 3088 | set_bit(WantReplacement, &rdev->flags); |
| 3089 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 3090 | md_wakeup_thread(rdev->mddev->thread); |
| 3091 | err = 0; |
| 3092 | } else if (cmd_match(buf, "-want_replacement")) { |
| 3093 | /* Clearing 'want_replacement' is always allowed. |
| 3094 | * Once replacements starts it is too late though. |
| 3095 | */ |
| 3096 | err = 0; |
| 3097 | clear_bit(WantReplacement, &rdev->flags); |
| 3098 | } else if (cmd_match(buf, "replacement")) { |
| 3099 | /* Can only set a device as a replacement when array has not |
| 3100 | * yet been started. Once running, replacement is automatic |
| 3101 | * from spares, or by assigning 'slot'. |
| 3102 | */ |
| 3103 | if (rdev->mddev->pers) |
| 3104 | err = -EBUSY; |
| 3105 | else { |
| 3106 | set_bit(Replacement, &rdev->flags); |
| 3107 | err = 0; |
| 3108 | } |
| 3109 | } else if (cmd_match(buf, "-replacement")) { |
| 3110 | /* Similarly, can only clear Replacement before start */ |
| 3111 | if (rdev->mddev->pers) |
| 3112 | err = -EBUSY; |
| 3113 | else { |
| 3114 | clear_bit(Replacement, &rdev->flags); |
| 3115 | err = 0; |
| 3116 | } |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 3117 | } else if (cmd_match(buf, "re-add")) { |
Yufen Yu | ee37e62 | 2019-04-02 14:22:14 +0800 | [diff] [blame] | 3118 | if (!rdev->mddev->pers) |
| 3119 | err = -EINVAL; |
| 3120 | else if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1) && |
| 3121 | rdev->saved_raid_disk >= 0) { |
Goldwyn Rodrigues | 97f6cd3 | 2015-04-14 10:45:42 -0500 | [diff] [blame] | 3122 | /* clear_bit is performed _after_ all the devices |
| 3123 | * have their local Faulty bit cleared. If any writes |
| 3124 | * happen in the meantime in the local node, they |
| 3125 | * will land in the local bitmap, which will be synced |
| 3126 | * by this node eventually |
| 3127 | */ |
| 3128 | if (!mddev_is_clustered(rdev->mddev) || |
| 3129 | (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) { |
| 3130 | clear_bit(Faulty, &rdev->flags); |
| 3131 | err = add_bound_rdev(rdev); |
| 3132 | } |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 3133 | } else |
| 3134 | err = -EBUSY; |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 3135 | } else if (cmd_match(buf, "external_bbl") && (rdev->mddev->external)) { |
| 3136 | set_bit(ExternalBbl, &rdev->flags); |
| 3137 | rdev->badblocks.shift = 0; |
| 3138 | err = 0; |
| 3139 | } else if (cmd_match(buf, "-external_bbl") && (rdev->mddev->external)) { |
| 3140 | clear_bit(ExternalBbl, &rdev->flags); |
| 3141 | err = 0; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3142 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 3143 | if (!err) |
| 3144 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3145 | return err ? err : len; |
| 3146 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3147 | static struct rdev_sysfs_entry rdev_state = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 3148 | __ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3149 | |
| 3150 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3151 | errors_show(struct md_rdev *rdev, char *page) |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 3152 | { |
| 3153 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 3154 | } |
| 3155 | |
| 3156 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3157 | errors_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 3158 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3159 | unsigned int n; |
| 3160 | int rv; |
| 3161 | |
| 3162 | rv = kstrtouint(buf, 10, &n); |
| 3163 | if (rv < 0) |
| 3164 | return rv; |
| 3165 | atomic_set(&rdev->corrected_errors, n); |
| 3166 | return len; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 3167 | } |
| 3168 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3169 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 3170 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3171 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3172 | slot_show(struct md_rdev *rdev, char *page) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3173 | { |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 3174 | if (test_bit(Journal, &rdev->flags)) |
| 3175 | return sprintf(page, "journal\n"); |
| 3176 | else if (rdev->raid_disk < 0) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3177 | return sprintf(page, "none\n"); |
| 3178 | else |
| 3179 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 3180 | } |
| 3181 | |
| 3182 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3183 | slot_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3184 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3185 | int slot; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3186 | int err; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3187 | |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 3188 | if (test_bit(Journal, &rdev->flags)) |
| 3189 | return -EBUSY; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3190 | if (strncmp(buf, "none", 4)==0) |
| 3191 | slot = -1; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3192 | else { |
| 3193 | err = kstrtouint(buf, 10, (unsigned int *)&slot); |
| 3194 | if (err < 0) |
| 3195 | return err; |
| 3196 | } |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 3197 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3198 | /* Setting 'slot' on an active array requires also |
| 3199 | * updating the 'rd%d' link, and communicating |
| 3200 | * with the personality with ->hot_*_disk. |
| 3201 | * For now we only support removing |
| 3202 | * failed/spare devices. This normally happens automatically, |
| 3203 | * but not when the metadata is externally managed. |
| 3204 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3205 | if (rdev->raid_disk == -1) |
| 3206 | return -EEXIST; |
| 3207 | /* personality does all needed checks */ |
Namhyung Kim | 01393f3 | 2011-06-09 11:42:54 +1000 | [diff] [blame] | 3208 | if (rdev->mddev->pers->hot_remove_disk == NULL) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3209 | return -EINVAL; |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 3210 | clear_bit(Blocked, &rdev->flags); |
| 3211 | remove_and_add_spares(rdev->mddev, rdev); |
| 3212 | if (rdev->raid_disk >= 0) |
| 3213 | return -EBUSY; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3214 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 3215 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 3216 | } else if (rdev->mddev->pers) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 3217 | /* Activating a spare .. or possibly reactivating |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 3218 | * if we ever get bitmaps working here. |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 3219 | */ |
Goldwyn Rodrigues | cb01c54 | 2015-12-18 15:19:16 +1100 | [diff] [blame] | 3220 | int err; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 3221 | |
| 3222 | if (rdev->raid_disk != -1) |
| 3223 | return -EBUSY; |
| 3224 | |
NeilBrown | c6751b2 | 2011-02-02 11:57:13 +1100 | [diff] [blame] | 3225 | if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery)) |
| 3226 | return -EBUSY; |
| 3227 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 3228 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 3229 | return -EINVAL; |
| 3230 | |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 3231 | if (slot >= rdev->mddev->raid_disks && |
| 3232 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
| 3233 | return -ENOSPC; |
| 3234 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 3235 | rdev->raid_disk = slot; |
| 3236 | if (test_bit(In_sync, &rdev->flags)) |
| 3237 | rdev->saved_raid_disk = slot; |
| 3238 | else |
| 3239 | rdev->saved_raid_disk = -1; |
NeilBrown | d30519f | 2011-10-18 12:13:47 +1100 | [diff] [blame] | 3240 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 3241 | clear_bit(Bitmap_sync, &rdev->flags); |
Guoqing Jiang | 3f79cc2 | 2020-04-04 23:57:11 +0200 | [diff] [blame] | 3242 | err = rdev->mddev->pers->hot_add_disk(rdev->mddev, rdev); |
Goldwyn Rodrigues | cb01c54 | 2015-12-18 15:19:16 +1100 | [diff] [blame] | 3243 | if (err) { |
| 3244 | rdev->raid_disk = -1; |
| 3245 | return err; |
| 3246 | } else |
| 3247 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Damien Le Moal | 5e3b8a8 | 2020-07-16 13:54:40 +0900 | [diff] [blame] | 3248 | /* failure here is OK */; |
| 3249 | sysfs_link_rdev(rdev->mddev, rdev); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 3250 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3251 | } else { |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 3252 | if (slot >= rdev->mddev->raid_disks && |
| 3253 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3254 | return -ENOSPC; |
| 3255 | rdev->raid_disk = slot; |
| 3256 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3257 | clear_bit(Faulty, &rdev->flags); |
| 3258 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3259 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 3260 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3261 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3262 | return len; |
| 3263 | } |
| 3264 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3265 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3266 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3267 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3268 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3269 | offset_show(struct md_rdev *rdev, char *page) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3270 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 3271 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3272 | } |
| 3273 | |
| 3274 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3275 | offset_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3276 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3277 | unsigned long long offset; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 3278 | if (kstrtoull(buf, 10, &offset) < 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3279 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 3280 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3281 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3282 | if (rdev->sectors && rdev->mddev->external) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3283 | /* Must set offset before size, so overlap checks |
| 3284 | * can be sane */ |
| 3285 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3286 | rdev->data_offset = offset; |
NeilBrown | 25f7fd4 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3287 | rdev->new_data_offset = offset; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3288 | return len; |
| 3289 | } |
| 3290 | |
| 3291 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3292 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3293 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3294 | static ssize_t new_offset_show(struct md_rdev *rdev, char *page) |
| 3295 | { |
| 3296 | return sprintf(page, "%llu\n", |
| 3297 | (unsigned long long)rdev->new_data_offset); |
| 3298 | } |
| 3299 | |
| 3300 | static ssize_t new_offset_store(struct md_rdev *rdev, |
| 3301 | const char *buf, size_t len) |
| 3302 | { |
| 3303 | unsigned long long new_offset; |
| 3304 | struct mddev *mddev = rdev->mddev; |
| 3305 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 3306 | if (kstrtoull(buf, 10, &new_offset) < 0) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3307 | return -EINVAL; |
| 3308 | |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 3309 | if (mddev->sync_thread || |
| 3310 | test_bit(MD_RECOVERY_RUNNING,&mddev->recovery)) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3311 | return -EBUSY; |
| 3312 | if (new_offset == rdev->data_offset) |
| 3313 | /* reset is always permitted */ |
| 3314 | ; |
| 3315 | else if (new_offset > rdev->data_offset) { |
| 3316 | /* must not push array size beyond rdev_sectors */ |
| 3317 | if (new_offset - rdev->data_offset |
| 3318 | + mddev->dev_sectors > rdev->sectors) |
| 3319 | return -E2BIG; |
| 3320 | } |
| 3321 | /* Metadata worries about other space details. */ |
| 3322 | |
| 3323 | /* decreasing the offset is inconsistent with a backwards |
| 3324 | * reshape. |
| 3325 | */ |
| 3326 | if (new_offset < rdev->data_offset && |
| 3327 | mddev->reshape_backwards) |
| 3328 | return -EINVAL; |
| 3329 | /* Increasing offset is inconsistent with forwards |
| 3330 | * reshape. reshape_direction should be set to |
| 3331 | * 'backwards' first. |
| 3332 | */ |
| 3333 | if (new_offset > rdev->data_offset && |
| 3334 | !mddev->reshape_backwards) |
| 3335 | return -EINVAL; |
| 3336 | |
| 3337 | if (mddev->pers && mddev->persistent && |
| 3338 | !super_types[mddev->major_version] |
| 3339 | .allow_new_offset(rdev, new_offset)) |
| 3340 | return -E2BIG; |
| 3341 | rdev->new_data_offset = new_offset; |
| 3342 | if (new_offset > rdev->data_offset) |
| 3343 | mddev->reshape_backwards = 1; |
| 3344 | else if (new_offset < rdev->data_offset) |
| 3345 | mddev->reshape_backwards = 0; |
| 3346 | |
| 3347 | return len; |
| 3348 | } |
| 3349 | static struct rdev_sysfs_entry rdev_new_offset = |
| 3350 | __ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store); |
| 3351 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3352 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3353 | rdev_size_show(struct md_rdev *rdev, char *page) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3354 | { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3355 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3356 | } |
| 3357 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3358 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 3359 | { |
| 3360 | /* check if two start/length pairs overlap */ |
| 3361 | if (s1+l1 <= s2) |
| 3362 | return 0; |
| 3363 | if (s2+l2 <= s1) |
| 3364 | return 0; |
| 3365 | return 1; |
| 3366 | } |
| 3367 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3368 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
| 3369 | { |
| 3370 | unsigned long long blocks; |
| 3371 | sector_t new; |
| 3372 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 3373 | if (kstrtoull(buf, 10, &blocks) < 0) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3374 | return -EINVAL; |
| 3375 | |
| 3376 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) |
| 3377 | return -EINVAL; /* sector conversion overflow */ |
| 3378 | |
| 3379 | new = blocks * 2; |
| 3380 | if (new != blocks * 2) |
| 3381 | return -EINVAL; /* unsigned long long to sector_t overflow */ |
| 3382 | |
| 3383 | *sectors = new; |
| 3384 | return 0; |
| 3385 | } |
| 3386 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3387 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3388 | rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3389 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3390 | struct mddev *my_mddev = rdev->mddev; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3391 | sector_t oldsectors = rdev->sectors; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3392 | sector_t sectors; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3393 | |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 3394 | if (test_bit(Journal, &rdev->flags)) |
| 3395 | return -EBUSY; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3396 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 3397 | return -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3398 | if (rdev->data_offset != rdev->new_data_offset) |
| 3399 | return -EINVAL; /* too confusing */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 3400 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 3401 | if (my_mddev->persistent) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3402 | sectors = super_types[my_mddev->major_version]. |
| 3403 | rdev_size_change(rdev, sectors); |
| 3404 | if (!sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 3405 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3406 | } else if (!sectors) |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 3407 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) - |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3408 | rdev->data_offset; |
NeilBrown | a646853 | 2013-02-21 14:33:17 +1100 | [diff] [blame] | 3409 | if (!my_mddev->pers->resize) |
| 3410 | /* Cannot change size for RAID0 or Linear etc */ |
| 3411 | return -EINVAL; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 3412 | } |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3413 | if (sectors < my_mddev->dev_sectors) |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 3414 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 3415 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3416 | rdev->sectors = sectors; |
| 3417 | if (sectors > oldsectors && my_mddev->external) { |
NeilBrown | 8b1afc3 | 2014-09-29 15:33:20 +1000 | [diff] [blame] | 3418 | /* Need to check that all other rdevs with the same |
| 3419 | * ->bdev do not overlap. 'rcu' is sufficient to walk |
| 3420 | * the rdev lists safely. |
| 3421 | * This check does not provide a hard guarantee, it |
| 3422 | * just helps avoid dangerous mistakes. |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3423 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3424 | struct mddev *mddev; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3425 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3426 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3427 | |
NeilBrown | 8b1afc3 | 2014-09-29 15:33:20 +1000 | [diff] [blame] | 3428 | rcu_read_lock(); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 3429 | for_each_mddev(mddev, tmp) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3430 | struct md_rdev *rdev2; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3431 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3432 | rdev_for_each(rdev2, mddev) |
NeilBrown | f21e9ff | 2011-01-31 12:10:09 +1100 | [diff] [blame] | 3433 | if (rdev->bdev == rdev2->bdev && |
| 3434 | rdev != rdev2 && |
| 3435 | overlaps(rdev->data_offset, rdev->sectors, |
| 3436 | rdev2->data_offset, |
| 3437 | rdev2->sectors)) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3438 | overlap = 1; |
| 3439 | break; |
| 3440 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3441 | if (overlap) { |
| 3442 | mddev_put(mddev); |
| 3443 | break; |
| 3444 | } |
| 3445 | } |
NeilBrown | 8b1afc3 | 2014-09-29 15:33:20 +1000 | [diff] [blame] | 3446 | rcu_read_unlock(); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3447 | if (overlap) { |
| 3448 | /* Someone else could have slipped in a size |
| 3449 | * change here, but doing so is just silly. |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3450 | * We put oldsectors back because we *know* it is |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3451 | * safe, and trust userspace not to race with |
| 3452 | * itself |
| 3453 | */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3454 | rdev->sectors = oldsectors; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3455 | return -EBUSY; |
| 3456 | } |
| 3457 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3458 | return len; |
| 3459 | } |
| 3460 | |
| 3461 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3462 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3463 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3464 | static ssize_t recovery_start_show(struct md_rdev *rdev, char *page) |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3465 | { |
| 3466 | unsigned long long recovery_start = rdev->recovery_offset; |
| 3467 | |
| 3468 | if (test_bit(In_sync, &rdev->flags) || |
| 3469 | recovery_start == MaxSector) |
| 3470 | return sprintf(page, "none\n"); |
| 3471 | |
| 3472 | return sprintf(page, "%llu\n", recovery_start); |
| 3473 | } |
| 3474 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3475 | 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] | 3476 | { |
| 3477 | unsigned long long recovery_start; |
| 3478 | |
| 3479 | if (cmd_match(buf, "none")) |
| 3480 | recovery_start = MaxSector; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 3481 | else if (kstrtoull(buf, 10, &recovery_start)) |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3482 | return -EINVAL; |
| 3483 | |
| 3484 | if (rdev->mddev->pers && |
| 3485 | rdev->raid_disk >= 0) |
| 3486 | return -EBUSY; |
| 3487 | |
| 3488 | rdev->recovery_offset = recovery_start; |
| 3489 | if (recovery_start == MaxSector) |
| 3490 | set_bit(In_sync, &rdev->flags); |
| 3491 | else |
| 3492 | clear_bit(In_sync, &rdev->flags); |
| 3493 | return len; |
| 3494 | } |
| 3495 | |
| 3496 | static struct rdev_sysfs_entry rdev_recovery_start = |
| 3497 | __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store); |
| 3498 | |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 3499 | /* sysfs access to bad-blocks list. |
| 3500 | * We present two files. |
| 3501 | * 'bad-blocks' lists sector numbers and lengths of ranges that |
| 3502 | * are recorded as bad. The list is truncated to fit within |
| 3503 | * the one-page limit of sysfs. |
| 3504 | * Writing "sector length" to this file adds an acknowledged |
| 3505 | * bad block list. |
| 3506 | * 'unacknowledged-bad-blocks' lists bad blocks that have not yet |
| 3507 | * been acknowledged. Writing to this file adds bad blocks |
| 3508 | * without acknowledging them. This is largely for testing. |
| 3509 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3510 | static ssize_t bb_show(struct md_rdev *rdev, char *page) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3511 | { |
| 3512 | return badblocks_show(&rdev->badblocks, page, 0); |
| 3513 | } |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3514 | 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] | 3515 | { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 3516 | int rv = badblocks_store(&rdev->badblocks, page, len, 0); |
| 3517 | /* Maybe that ack was all we needed */ |
| 3518 | if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags)) |
| 3519 | wake_up(&rdev->blocked_wait); |
| 3520 | return rv; |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3521 | } |
| 3522 | static struct rdev_sysfs_entry rdev_bad_blocks = |
| 3523 | __ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store); |
| 3524 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3525 | static ssize_t ubb_show(struct md_rdev *rdev, char *page) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3526 | { |
| 3527 | return badblocks_show(&rdev->badblocks, page, 1); |
| 3528 | } |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3529 | 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] | 3530 | { |
| 3531 | return badblocks_store(&rdev->badblocks, page, len, 1); |
| 3532 | } |
| 3533 | static struct rdev_sysfs_entry rdev_unack_bad_blocks = |
| 3534 | __ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store); |
| 3535 | |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 3536 | static ssize_t |
| 3537 | ppl_sector_show(struct md_rdev *rdev, char *page) |
| 3538 | { |
| 3539 | return sprintf(page, "%llu\n", (unsigned long long)rdev->ppl.sector); |
| 3540 | } |
| 3541 | |
| 3542 | static ssize_t |
| 3543 | ppl_sector_store(struct md_rdev *rdev, const char *buf, size_t len) |
| 3544 | { |
| 3545 | unsigned long long sector; |
| 3546 | |
| 3547 | if (kstrtoull(buf, 10, §or) < 0) |
| 3548 | return -EINVAL; |
| 3549 | if (sector != (sector_t)sector) |
| 3550 | return -EINVAL; |
| 3551 | |
| 3552 | if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) && |
| 3553 | rdev->raid_disk >= 0) |
| 3554 | return -EBUSY; |
| 3555 | |
| 3556 | if (rdev->mddev->persistent) { |
| 3557 | if (rdev->mddev->major_version == 0) |
| 3558 | return -EINVAL; |
| 3559 | if ((sector > rdev->sb_start && |
| 3560 | sector - rdev->sb_start > S16_MAX) || |
| 3561 | (sector < rdev->sb_start && |
| 3562 | rdev->sb_start - sector > -S16_MIN)) |
| 3563 | return -EINVAL; |
| 3564 | rdev->ppl.offset = sector - rdev->sb_start; |
| 3565 | } else if (!rdev->mddev->external) { |
| 3566 | return -EBUSY; |
| 3567 | } |
| 3568 | rdev->ppl.sector = sector; |
| 3569 | return len; |
| 3570 | } |
| 3571 | |
| 3572 | static struct rdev_sysfs_entry rdev_ppl_sector = |
| 3573 | __ATTR(ppl_sector, S_IRUGO|S_IWUSR, ppl_sector_show, ppl_sector_store); |
| 3574 | |
| 3575 | static ssize_t |
| 3576 | ppl_size_show(struct md_rdev *rdev, char *page) |
| 3577 | { |
| 3578 | return sprintf(page, "%u\n", rdev->ppl.size); |
| 3579 | } |
| 3580 | |
| 3581 | static ssize_t |
| 3582 | ppl_size_store(struct md_rdev *rdev, const char *buf, size_t len) |
| 3583 | { |
| 3584 | unsigned int size; |
| 3585 | |
| 3586 | if (kstrtouint(buf, 10, &size) < 0) |
| 3587 | return -EINVAL; |
| 3588 | |
| 3589 | if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) && |
| 3590 | rdev->raid_disk >= 0) |
| 3591 | return -EBUSY; |
| 3592 | |
| 3593 | if (rdev->mddev->persistent) { |
| 3594 | if (rdev->mddev->major_version == 0) |
| 3595 | return -EINVAL; |
| 3596 | if (size > U16_MAX) |
| 3597 | return -EINVAL; |
| 3598 | } else if (!rdev->mddev->external) { |
| 3599 | return -EBUSY; |
| 3600 | } |
| 3601 | rdev->ppl.size = size; |
| 3602 | return len; |
| 3603 | } |
| 3604 | |
| 3605 | static struct rdev_sysfs_entry rdev_ppl_size = |
| 3606 | __ATTR(ppl_size, S_IRUGO|S_IWUSR, ppl_size_show, ppl_size_store); |
| 3607 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3608 | static struct attribute *rdev_default_attrs[] = { |
| 3609 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 3610 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3611 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3612 | &rdev_offset.attr, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3613 | &rdev_new_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3614 | &rdev_size.attr, |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3615 | &rdev_recovery_start.attr, |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3616 | &rdev_bad_blocks.attr, |
| 3617 | &rdev_unack_bad_blocks.attr, |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 3618 | &rdev_ppl_sector.attr, |
| 3619 | &rdev_ppl_size.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3620 | NULL, |
| 3621 | }; |
| 3622 | static ssize_t |
| 3623 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 3624 | { |
| 3625 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3626 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3627 | |
| 3628 | if (!entry->show) |
| 3629 | return -EIO; |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 3630 | if (!rdev->mddev) |
Marcos Paulo de Souza | 168b305 | 2019-06-14 15:41:06 -0700 | [diff] [blame] | 3631 | return -ENODEV; |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 3632 | return entry->show(rdev, page); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3633 | } |
| 3634 | |
| 3635 | static ssize_t |
| 3636 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 3637 | const char *page, size_t length) |
| 3638 | { |
| 3639 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3640 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3641 | ssize_t rv; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3642 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3643 | |
| 3644 | if (!entry->store) |
| 3645 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 3646 | if (!capable(CAP_SYS_ADMIN)) |
| 3647 | return -EACCES; |
Pawel Baldysiak | c42d324 | 2019-03-27 13:48:21 +0100 | [diff] [blame] | 3648 | rv = mddev ? mddev_lock(mddev) : -ENODEV; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 3649 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3650 | if (rdev->mddev == NULL) |
Pawel Baldysiak | c42d324 | 2019-03-27 13:48:21 +0100 | [diff] [blame] | 3651 | rv = -ENODEV; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3652 | else |
| 3653 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 3654 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 3655 | } |
| 3656 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3657 | } |
| 3658 | |
| 3659 | static void rdev_free(struct kobject *ko) |
| 3660 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3661 | struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3662 | kfree(rdev); |
| 3663 | } |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 3664 | static const struct sysfs_ops rdev_sysfs_ops = { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3665 | .show = rdev_attr_show, |
| 3666 | .store = rdev_attr_store, |
| 3667 | }; |
| 3668 | static struct kobj_type rdev_ktype = { |
| 3669 | .release = rdev_free, |
| 3670 | .sysfs_ops = &rdev_sysfs_ops, |
| 3671 | .default_attrs = rdev_default_attrs, |
| 3672 | }; |
| 3673 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3674 | int md_rdev_init(struct md_rdev *rdev) |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3675 | { |
| 3676 | rdev->desc_nr = -1; |
| 3677 | rdev->saved_raid_disk = -1; |
| 3678 | rdev->raid_disk = -1; |
| 3679 | rdev->flags = 0; |
| 3680 | rdev->data_offset = 0; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3681 | rdev->new_data_offset = 0; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3682 | rdev->sb_events = 0; |
Arnd Bergmann | 0e3ef49 | 2016-06-17 17:33:10 +0200 | [diff] [blame] | 3683 | rdev->last_read_error = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3684 | rdev->sb_loaded = 0; |
| 3685 | rdev->bb_page = NULL; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3686 | atomic_set(&rdev->nr_pending, 0); |
| 3687 | atomic_set(&rdev->read_errors, 0); |
| 3688 | atomic_set(&rdev->corrected_errors, 0); |
| 3689 | |
| 3690 | INIT_LIST_HEAD(&rdev->same_set); |
| 3691 | init_waitqueue_head(&rdev->blocked_wait); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3692 | |
| 3693 | /* Add space to store bad block list. |
| 3694 | * This reserves the space even on arrays where it cannot |
| 3695 | * be used - I wonder if that matters |
| 3696 | */ |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 3697 | return badblocks_init(&rdev->badblocks, 0); |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3698 | } |
| 3699 | EXPORT_SYMBOL_GPL(md_rdev_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3700 | /* |
| 3701 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 3702 | * |
| 3703 | * mark the device faulty if: |
| 3704 | * |
| 3705 | * - the device is nonexistent (zero size) |
| 3706 | * - the device has no valid superblock |
| 3707 | * |
| 3708 | * a faulty rdev _never_ has rdev->sb set. |
| 3709 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3710 | 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] | 3711 | { |
| 3712 | char b[BDEVNAME_SIZE]; |
| 3713 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3714 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3715 | sector_t size; |
| 3716 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 3717 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3718 | if (!rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3719 | return ERR_PTR(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3720 | |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3721 | err = md_rdev_init(rdev); |
| 3722 | if (err) |
| 3723 | goto abort_free; |
| 3724 | err = alloc_disk_sb(rdev); |
| 3725 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3726 | goto abort_free; |
| 3727 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3728 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3729 | if (err) |
| 3730 | goto abort_free; |
| 3731 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 3732 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3733 | |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 3734 | size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3735 | if (!size) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3736 | pr_warn("md: %s has zero or unknown size, marking faulty!\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3737 | bdevname(rdev->bdev,b)); |
| 3738 | err = -EINVAL; |
| 3739 | goto abort_free; |
| 3740 | } |
| 3741 | |
| 3742 | if (super_format >= 0) { |
| 3743 | err = super_types[super_format]. |
| 3744 | load_super(rdev, NULL, super_minor); |
| 3745 | if (err == -EINVAL) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3746 | 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] | 3747 | bdevname(rdev->bdev,b), |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3748 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3749 | goto abort_free; |
| 3750 | } |
| 3751 | if (err < 0) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3752 | pr_warn("md: could not read %s's sb, not importing!\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3753 | bdevname(rdev->bdev,b)); |
| 3754 | goto abort_free; |
| 3755 | } |
| 3756 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3757 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3758 | return rdev; |
| 3759 | |
| 3760 | abort_free: |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3761 | if (rdev->bdev) |
| 3762 | unlock_rdev(rdev); |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 3763 | md_rdev_clear(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3764 | kfree(rdev); |
| 3765 | return ERR_PTR(err); |
| 3766 | } |
| 3767 | |
| 3768 | /* |
| 3769 | * Check a full RAID array for plausibility |
| 3770 | */ |
| 3771 | |
Yufen Yu | 6a5cb53 | 2019-10-16 16:00:03 +0800 | [diff] [blame] | 3772 | static int analyze_sbs(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3773 | { |
| 3774 | int i; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3775 | struct md_rdev *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3776 | char b[BDEVNAME_SIZE]; |
| 3777 | |
| 3778 | freshest = NULL; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3779 | rdev_for_each_safe(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3780 | switch (super_types[mddev->major_version]. |
| 3781 | load_super(rdev, freshest, mddev->minor_version)) { |
| 3782 | case 1: |
| 3783 | freshest = rdev; |
| 3784 | break; |
| 3785 | case 0: |
| 3786 | break; |
| 3787 | default: |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3788 | pr_warn("md: fatal superblock inconsistency in %s -- removing from array\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3789 | bdevname(rdev->bdev,b)); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 3790 | md_kick_rdev_from_array(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3791 | } |
| 3792 | |
Yufen Yu | 6a5cb53 | 2019-10-16 16:00:03 +0800 | [diff] [blame] | 3793 | /* Cannot find a valid fresh disk */ |
| 3794 | if (!freshest) { |
| 3795 | pr_warn("md: cannot find a valid disk\n"); |
| 3796 | return -EINVAL; |
| 3797 | } |
| 3798 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3799 | super_types[mddev->major_version]. |
| 3800 | validate_super(mddev, freshest); |
| 3801 | |
| 3802 | i = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3803 | rdev_for_each_safe(rdev, tmp, mddev) { |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 3804 | if (mddev->max_disks && |
| 3805 | (rdev->desc_nr >= mddev->max_disks || |
| 3806 | i > mddev->max_disks)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3807 | pr_warn("md: %s: %s: only %d devices permitted\n", |
| 3808 | mdname(mddev), bdevname(rdev->bdev, b), |
| 3809 | mddev->max_disks); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 3810 | md_kick_rdev_from_array(rdev); |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 3811 | continue; |
| 3812 | } |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 3813 | if (rdev != freshest) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3814 | if (super_types[mddev->major_version]. |
| 3815 | validate_super(mddev, rdev)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3816 | pr_warn("md: kicking non-fresh %s from array!\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3817 | bdevname(rdev->bdev,b)); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 3818 | md_kick_rdev_from_array(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3819 | continue; |
| 3820 | } |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 3821 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3822 | if (mddev->level == LEVEL_MULTIPATH) { |
| 3823 | rdev->desc_nr = i++; |
| 3824 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3825 | set_bit(In_sync, &rdev->flags); |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 3826 | } else if (rdev->raid_disk >= |
| 3827 | (mddev->raid_disks - min(0, mddev->delta_disks)) && |
| 3828 | !test_bit(Journal, &rdev->flags)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 3829 | rdev->raid_disk = -1; |
| 3830 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3831 | } |
| 3832 | } |
Yufen Yu | 6a5cb53 | 2019-10-16 16:00:03 +0800 | [diff] [blame] | 3833 | |
| 3834 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3835 | } |
| 3836 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3837 | /* Read a fixed-point number. |
| 3838 | * Numbers in sysfs attributes should be in "standard" units where |
| 3839 | * possible, so time should be in seconds. |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3840 | * However we internally use a a much smaller unit such as |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3841 | * milliseconds or jiffies. |
| 3842 | * This function takes a decimal number with a possible fractional |
| 3843 | * component, and produces an integer which is the result of |
| 3844 | * multiplying that number by 10^'scale'. |
| 3845 | * all without any floating-point arithmetic. |
| 3846 | */ |
| 3847 | int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale) |
| 3848 | { |
| 3849 | unsigned long result = 0; |
| 3850 | long decimals = -1; |
| 3851 | while (isdigit(*cp) || (*cp == '.' && decimals < 0)) { |
| 3852 | if (*cp == '.') |
| 3853 | decimals = 0; |
| 3854 | else if (decimals < scale) { |
| 3855 | unsigned int value; |
| 3856 | value = *cp - '0'; |
| 3857 | result = result * 10 + value; |
| 3858 | if (decimals >= 0) |
| 3859 | decimals++; |
| 3860 | } |
| 3861 | cp++; |
| 3862 | } |
| 3863 | if (*cp == '\n') |
| 3864 | cp++; |
| 3865 | if (*cp) |
| 3866 | return -EINVAL; |
| 3867 | if (decimals < 0) |
| 3868 | decimals = 0; |
Andy Shevchenko | cf89160 | 2019-07-23 23:41:55 +0300 | [diff] [blame] | 3869 | *res = result * int_pow(10, scale - decimals); |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3870 | return 0; |
| 3871 | } |
| 3872 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3873 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3874 | safe_delay_show(struct mddev *mddev, char *page) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3875 | { |
| 3876 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 3877 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 3878 | } |
| 3879 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3880 | safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3881 | { |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3882 | unsigned long msec; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 3883 | |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 3884 | if (mddev_is_clustered(mddev)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3885 | pr_warn("md: Safemode is disabled for clustered mode\n"); |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 3886 | return -EINVAL; |
| 3887 | } |
| 3888 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3889 | if (strict_strtoul_scaled(cbuf, &msec, 3) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3890 | return -EINVAL; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3891 | if (msec == 0) |
| 3892 | mddev->safemode_delay = 0; |
| 3893 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3894 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 1b30e66 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 3895 | unsigned long new_delay = (msec*HZ)/1000; |
| 3896 | |
| 3897 | if (new_delay == 0) |
| 3898 | new_delay = 1; |
| 3899 | mddev->safemode_delay = new_delay; |
| 3900 | if (new_delay < old_delay || old_delay == 0) |
| 3901 | mod_timer(&mddev->safemode_timer, jiffies+1); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3902 | } |
| 3903 | return len; |
| 3904 | } |
| 3905 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3906 | __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] | 3907 | |
| 3908 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3909 | level_show(struct mddev *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3910 | { |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3911 | struct md_personality *p; |
| 3912 | int ret; |
| 3913 | spin_lock(&mddev->lock); |
| 3914 | p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3915 | if (p) |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3916 | ret = sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3917 | else if (mddev->clevel[0]) |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3918 | ret = sprintf(page, "%s\n", mddev->clevel); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3919 | else if (mddev->level != LEVEL_NONE) |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3920 | ret = sprintf(page, "%d\n", mddev->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3921 | else |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3922 | ret = 0; |
| 3923 | spin_unlock(&mddev->lock); |
| 3924 | return ret; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3925 | } |
| 3926 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3927 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3928 | level_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3929 | { |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3930 | char clevel[16]; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3931 | ssize_t rv; |
| 3932 | size_t slen = len; |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3933 | struct md_personality *pers, *oldpers; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3934 | long level; |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3935 | void *priv, *oldpriv; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3936 | struct md_rdev *rdev; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3937 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3938 | if (slen == 0 || slen >= sizeof(clevel)) |
| 3939 | return -EINVAL; |
| 3940 | |
| 3941 | rv = mddev_lock(mddev); |
| 3942 | if (rv) |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3943 | return rv; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3944 | |
| 3945 | if (mddev->pers == NULL) { |
| 3946 | strncpy(mddev->clevel, buf, slen); |
| 3947 | if (mddev->clevel[slen-1] == '\n') |
| 3948 | slen--; |
| 3949 | mddev->clevel[slen] = 0; |
| 3950 | mddev->level = LEVEL_NONE; |
| 3951 | rv = len; |
| 3952 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3953 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3954 | rv = -EROFS; |
NeilBrown | bd8839e | 2014-05-28 13:39:21 +1000 | [diff] [blame] | 3955 | if (mddev->ro) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3956 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3957 | |
| 3958 | /* request to change the personality. Need to ensure: |
| 3959 | * - array is not engaged in resync/recovery/reshape |
| 3960 | * - old personality can be suspended |
| 3961 | * - new personality will access other array. |
| 3962 | */ |
| 3963 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3964 | rv = -EBUSY; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 3965 | if (mddev->sync_thread || |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 3966 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 3967 | mddev->reshape_position != MaxSector || |
| 3968 | mddev->sysfs_active) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3969 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3970 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3971 | rv = -EINVAL; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3972 | if (!mddev->pers->quiesce) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3973 | pr_warn("md: %s: %s does not support online personality change\n", |
| 3974 | mdname(mddev), mddev->pers->name); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3975 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3976 | } |
| 3977 | |
| 3978 | /* Now find the new personality */ |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3979 | strncpy(clevel, buf, slen); |
| 3980 | if (clevel[slen-1] == '\n') |
| 3981 | slen--; |
| 3982 | clevel[slen] = 0; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 3983 | if (kstrtol(clevel, 10, &level)) |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3984 | level = LEVEL_NONE; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3985 | |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3986 | if (request_module("md-%s", clevel) != 0) |
| 3987 | request_module("md-level-%s", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3988 | spin_lock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3989 | pers = find_pers(level, clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3990 | if (!pers || !try_module_get(pers->owner)) { |
| 3991 | spin_unlock(&pers_lock); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3992 | pr_warn("md: personality %s not loaded\n", clevel); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3993 | rv = -EINVAL; |
| 3994 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3995 | } |
| 3996 | spin_unlock(&pers_lock); |
| 3997 | |
| 3998 | if (pers == mddev->pers) { |
| 3999 | /* Nothing to do! */ |
| 4000 | module_put(pers->owner); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4001 | rv = len; |
| 4002 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4003 | } |
| 4004 | if (!pers->takeover) { |
| 4005 | module_put(pers->owner); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 4006 | pr_warn("md: %s: %s does not support personality takeover\n", |
| 4007 | mdname(mddev), clevel); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4008 | rv = -EINVAL; |
| 4009 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4010 | } |
| 4011 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 4012 | rdev_for_each(rdev, mddev) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 4013 | rdev->new_raid_disk = rdev->raid_disk; |
| 4014 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4015 | /* ->takeover must set new_* and/or delta_disks |
| 4016 | * if it succeeds, and may set them when it fails. |
| 4017 | */ |
| 4018 | priv = pers->takeover(mddev); |
| 4019 | if (IS_ERR(priv)) { |
| 4020 | mddev->new_level = mddev->level; |
| 4021 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4022 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4023 | mddev->raid_disks -= mddev->delta_disks; |
| 4024 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4025 | mddev->reshape_backwards = 0; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4026 | module_put(pers->owner); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 4027 | pr_warn("md: %s: %s would not accept array\n", |
| 4028 | mdname(mddev), clevel); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4029 | rv = PTR_ERR(priv); |
| 4030 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4031 | } |
| 4032 | |
| 4033 | /* Looks like we have a winner */ |
| 4034 | mddev_suspend(mddev); |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 4035 | mddev_detach(mddev); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 4036 | |
| 4037 | spin_lock(&mddev->lock); |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 4038 | oldpers = mddev->pers; |
| 4039 | oldpriv = mddev->private; |
| 4040 | mddev->pers = pers; |
| 4041 | mddev->private = priv; |
| 4042 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
| 4043 | mddev->level = mddev->new_level; |
| 4044 | mddev->layout = mddev->new_layout; |
| 4045 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
| 4046 | mddev->delta_disks = 0; |
| 4047 | mddev->reshape_backwards = 0; |
| 4048 | mddev->degraded = 0; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 4049 | spin_unlock(&mddev->lock); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 4050 | |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 4051 | if (oldpers->sync_request == NULL && |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 4052 | mddev->external) { |
| 4053 | /* We are converting from a no-redundancy array |
| 4054 | * to a redundancy array and metadata is managed |
| 4055 | * externally so we need to be sure that writes |
| 4056 | * won't block due to a need to transition |
| 4057 | * clean->dirty |
| 4058 | * until external management is started. |
| 4059 | */ |
| 4060 | mddev->in_sync = 0; |
| 4061 | mddev->safemode_delay = 0; |
| 4062 | mddev->safemode = 0; |
| 4063 | } |
| 4064 | |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 4065 | oldpers->free(mddev, oldpriv); |
| 4066 | |
| 4067 | if (oldpers->sync_request == NULL && |
| 4068 | pers->sync_request != NULL) { |
| 4069 | /* need to add the md_redundancy_group */ |
| 4070 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 4071 | pr_warn("md: cannot register extra attributes for %s\n", |
| 4072 | mdname(mddev)); |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 4073 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action"); |
Junxiao Bi | e8efa9b | 2020-08-04 17:27:18 -0700 | [diff] [blame] | 4074 | mddev->sysfs_completed = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_completed"); |
| 4075 | mddev->sysfs_degraded = sysfs_get_dirent_safe(mddev->kobj.sd, "degraded"); |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 4076 | } |
| 4077 | if (oldpers->sync_request != NULL && |
| 4078 | pers->sync_request == NULL) { |
| 4079 | /* need to remove the md_redundancy_group */ |
| 4080 | if (mddev->to_remove == NULL) |
| 4081 | mddev->to_remove = &md_redundancy_group; |
| 4082 | } |
| 4083 | |
Alexey Obitotskiy | 4cb9da7 | 2016-06-23 12:11:01 +0200 | [diff] [blame] | 4084 | module_put(oldpers->owner); |
| 4085 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 4086 | rdev_for_each(rdev, mddev) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 4087 | if (rdev->raid_disk < 0) |
| 4088 | continue; |
NeilBrown | bf2cb0d | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4089 | if (rdev->new_raid_disk >= mddev->raid_disks) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 4090 | rdev->new_raid_disk = -1; |
| 4091 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 4092 | continue; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4093 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 4094 | } |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 4095 | rdev_for_each(rdev, mddev) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 4096 | if (rdev->raid_disk < 0) |
| 4097 | continue; |
| 4098 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 4099 | continue; |
| 4100 | rdev->raid_disk = rdev->new_raid_disk; |
| 4101 | if (rdev->raid_disk < 0) |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 4102 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 4103 | else { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4104 | if (sysfs_link_rdev(mddev, rdev)) |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 4105 | pr_warn("md: cannot register rd%d for %s after level change\n", |
| 4106 | rdev->raid_disk, mdname(mddev)); |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 4107 | } |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 4108 | } |
| 4109 | |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 4110 | if (pers->sync_request == NULL) { |
Trela, Maciej | 9af204c | 2010-03-08 16:02:44 +1100 | [diff] [blame] | 4111 | /* this is now an array without redundancy, so |
| 4112 | * it must always be in_sync |
| 4113 | */ |
| 4114 | mddev->in_sync = 1; |
| 4115 | del_timer_sync(&mddev->safemode_timer); |
| 4116 | } |
NeilBrown | 02e5f5c | 2013-11-14 15:16:15 +1100 | [diff] [blame] | 4117 | blk_set_stacking_limits(&mddev->queue->limits); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4118 | pers->run(mddev); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 4119 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
Jonathan Brassow | 47525e5 | 2012-05-22 13:55:29 +1000 | [diff] [blame] | 4120 | mddev_resume(mddev); |
NeilBrown | 830778a | 2014-01-14 15:17:03 +1100 | [diff] [blame] | 4121 | if (!mddev->thread) |
| 4122 | md_update_sb(mddev, 1); |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 4123 | sysfs_notify_dirent_safe(mddev->sysfs_level); |
Dan Williams | bb7f8d2 | 2010-05-01 18:14:57 -0700 | [diff] [blame] | 4124 | md_new_event(mddev); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4125 | rv = len; |
| 4126 | out_unlock: |
| 4127 | mddev_unlock(mddev); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4128 | return rv; |
| 4129 | } |
| 4130 | |
| 4131 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4132 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4133 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4134 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4135 | layout_show(struct mddev *mddev, char *page) |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4136 | { |
| 4137 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4138 | if (mddev->reshape_position != MaxSector && |
| 4139 | mddev->layout != mddev->new_layout) |
| 4140 | return sprintf(page, "%d (%d)\n", |
| 4141 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4142 | return sprintf(page, "%d\n", mddev->layout); |
| 4143 | } |
| 4144 | |
| 4145 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4146 | layout_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4147 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4148 | unsigned int n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4149 | int err; |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4150 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4151 | err = kstrtouint(buf, 10, &n); |
| 4152 | if (err < 0) |
| 4153 | return err; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4154 | err = mddev_lock(mddev); |
| 4155 | if (err) |
| 4156 | return err; |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4157 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 4158 | if (mddev->pers) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 4159 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4160 | err = -EBUSY; |
| 4161 | else if (mddev->ro) |
| 4162 | err = -EROFS; |
| 4163 | else { |
| 4164 | mddev->new_layout = n; |
| 4165 | err = mddev->pers->check_reshape(mddev); |
| 4166 | if (err) |
| 4167 | mddev->new_layout = mddev->layout; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 4168 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 4169 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4170 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 4171 | if (mddev->reshape_position == MaxSector) |
| 4172 | mddev->layout = n; |
| 4173 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4174 | mddev_unlock(mddev); |
| 4175 | return err ?: len; |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4176 | } |
| 4177 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4178 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4179 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4180 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4181 | raid_disks_show(struct mddev *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4182 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 4183 | if (mddev->raid_disks == 0) |
| 4184 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4185 | if (mddev->reshape_position != MaxSector && |
| 4186 | mddev->delta_disks != 0) |
| 4187 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 4188 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4189 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 4190 | } |
| 4191 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4192 | static int update_raid_disks(struct mddev *mddev, int raid_disks); |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4193 | |
| 4194 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4195 | raid_disks_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4196 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4197 | unsigned int n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4198 | int err; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4199 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4200 | err = kstrtouint(buf, 10, &n); |
| 4201 | if (err < 0) |
| 4202 | return err; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4203 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4204 | err = mddev_lock(mddev); |
| 4205 | if (err) |
| 4206 | return err; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4207 | if (mddev->pers) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4208 | err = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4209 | else if (mddev->reshape_position != MaxSector) { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4210 | struct md_rdev *rdev; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4211 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4212 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4213 | err = -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4214 | rdev_for_each(rdev, mddev) { |
| 4215 | if (olddisks < n && |
| 4216 | rdev->data_offset < rdev->new_data_offset) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4217 | goto out_unlock; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4218 | if (olddisks > n && |
| 4219 | rdev->data_offset > rdev->new_data_offset) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4220 | goto out_unlock; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4221 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4222 | err = 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4223 | mddev->delta_disks = n - olddisks; |
| 4224 | mddev->raid_disks = n; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4225 | mddev->reshape_backwards = (mddev->delta_disks < 0); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4226 | } else |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4227 | mddev->raid_disks = n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4228 | out_unlock: |
| 4229 | mddev_unlock(mddev); |
| 4230 | return err ? err : len; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4231 | } |
| 4232 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4233 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4234 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4235 | static ssize_t |
Sebastian Parschauer | ec164d07 | 2020-07-28 12:01:39 +0200 | [diff] [blame] | 4236 | uuid_show(struct mddev *mddev, char *page) |
| 4237 | { |
| 4238 | return sprintf(page, "%pU\n", mddev->uuid); |
| 4239 | } |
| 4240 | static struct md_sysfs_entry md_uuid = |
| 4241 | __ATTR(uuid, S_IRUGO, uuid_show, NULL); |
| 4242 | |
| 4243 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4244 | chunk_size_show(struct mddev *mddev, char *page) |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4245 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4246 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4247 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 4248 | return sprintf(page, "%d (%d)\n", |
| 4249 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4250 | mddev->chunk_sectors << 9); |
| 4251 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4252 | } |
| 4253 | |
| 4254 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4255 | chunk_size_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4256 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4257 | unsigned long n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4258 | int err; |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4259 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4260 | err = kstrtoul(buf, 10, &n); |
| 4261 | if (err < 0) |
| 4262 | return err; |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4263 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4264 | err = mddev_lock(mddev); |
| 4265 | if (err) |
| 4266 | return err; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 4267 | if (mddev->pers) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 4268 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4269 | err = -EBUSY; |
| 4270 | else if (mddev->ro) |
| 4271 | err = -EROFS; |
| 4272 | else { |
| 4273 | mddev->new_chunk_sectors = n >> 9; |
| 4274 | err = mddev->pers->check_reshape(mddev); |
| 4275 | if (err) |
| 4276 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 4277 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 4278 | } else { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4279 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 4280 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4281 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 4282 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4283 | mddev_unlock(mddev); |
| 4284 | return err ?: len; |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4285 | } |
| 4286 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4287 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4288 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4289 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4290 | resync_start_show(struct mddev *mddev, char *page) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4291 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 4292 | if (mddev->recovery_cp == MaxSector) |
| 4293 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4294 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 4295 | } |
| 4296 | |
| 4297 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4298 | resync_start_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4299 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4300 | unsigned long long n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4301 | int err; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4302 | |
| 4303 | if (cmd_match(buf, "none")) |
| 4304 | n = MaxSector; |
| 4305 | else { |
| 4306 | err = kstrtoull(buf, 10, &n); |
| 4307 | if (err < 0) |
| 4308 | return err; |
| 4309 | if (n != (sector_t)n) |
| 4310 | return -EINVAL; |
| 4311 | } |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4312 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4313 | err = mddev_lock(mddev); |
| 4314 | if (err) |
| 4315 | return err; |
NeilBrown | b098636 | 2011-05-11 15:52:21 +1000 | [diff] [blame] | 4316 | if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4317 | err = -EBUSY; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4318 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4319 | if (!err) { |
| 4320 | mddev->recovery_cp = n; |
| 4321 | if (mddev->pers) |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 4322 | set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4323 | } |
| 4324 | mddev_unlock(mddev); |
| 4325 | return err ?: len; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4326 | } |
| 4327 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 4328 | __ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR, |
| 4329 | resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4330 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4331 | /* |
| 4332 | * The array state can be: |
| 4333 | * |
| 4334 | * clear |
| 4335 | * No devices, no size, no level |
| 4336 | * Equivalent to STOP_ARRAY ioctl |
| 4337 | * inactive |
| 4338 | * May have some settings, but array is not active |
| 4339 | * all IO results in error |
| 4340 | * When written, doesn't tear down array, but just stops it |
| 4341 | * suspended (not supported yet) |
| 4342 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 4343 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4344 | * readonly |
| 4345 | * no resync can happen. no superblocks get written. |
| 4346 | * write requests fail |
| 4347 | * read-auto |
| 4348 | * like readonly, but behaves like 'clean' on a write request. |
| 4349 | * |
| 4350 | * clean - no pending writes, but otherwise active. |
| 4351 | * When written to inactive array, starts without resync |
| 4352 | * If a write request arrives then |
| 4353 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 4354 | * if not known, block and switch to write-pending |
| 4355 | * If written to an active array that has pending writes, then fails. |
| 4356 | * active |
| 4357 | * fully active: IO and resync can be happening. |
| 4358 | * When written to inactive array, starts with resync |
| 4359 | * |
| 4360 | * write-pending |
| 4361 | * clean, but writes are blocked waiting for 'active' to be written. |
| 4362 | * |
| 4363 | * active-idle |
| 4364 | * like active, but no writes have been seen for a while (100msec). |
| 4365 | * |
Guilherme G. Piccoli | 62f7b19 | 2019-09-03 16:49:00 -0300 | [diff] [blame] | 4366 | * broken |
| 4367 | * RAID0/LINEAR-only: same as clean, but array is missing a member. |
| 4368 | * It's useful because RAID0/LINEAR mounted-arrays aren't stopped |
| 4369 | * when a member is gone, so this state will at least alert the |
| 4370 | * user that something is wrong. |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4371 | */ |
| 4372 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
Guilherme G. Piccoli | 62f7b19 | 2019-09-03 16:49:00 -0300 | [diff] [blame] | 4373 | write_pending, active_idle, broken, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 4374 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4375 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
Guilherme G. Piccoli | 62f7b19 | 2019-09-03 16:49:00 -0300 | [diff] [blame] | 4376 | "write-pending", "active-idle", "broken", NULL }; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4377 | |
| 4378 | static int match_word(const char *word, char **list) |
| 4379 | { |
| 4380 | int n; |
| 4381 | for (n=0; list[n]; n++) |
| 4382 | if (cmd_match(word, list[n])) |
| 4383 | break; |
| 4384 | return n; |
| 4385 | } |
| 4386 | |
| 4387 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4388 | array_state_show(struct mddev *mddev, char *page) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4389 | { |
| 4390 | enum array_state st = inactive; |
| 4391 | |
Guilherme G. Piccoli | 62f7b19 | 2019-09-03 16:49:00 -0300 | [diff] [blame] | 4392 | if (mddev->pers && !test_bit(MD_NOT_READY, &mddev->flags)) { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4393 | switch(mddev->ro) { |
| 4394 | case 1: |
| 4395 | st = readonly; |
| 4396 | break; |
| 4397 | case 2: |
| 4398 | st = read_auto; |
| 4399 | break; |
| 4400 | case 0: |
NeilBrown | 55cc39f | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 4401 | spin_lock(&mddev->lock); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 4402 | if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4403 | st = write_pending; |
Tomasz Majchrzak | 16f8894 | 2016-10-24 12:47:28 +0200 | [diff] [blame] | 4404 | else if (mddev->in_sync) |
| 4405 | st = clean; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4406 | else if (mddev->safemode) |
| 4407 | st = active_idle; |
| 4408 | else |
| 4409 | st = active; |
NeilBrown | 55cc39f | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 4410 | spin_unlock(&mddev->lock); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4411 | } |
Guilherme G. Piccoli | 62f7b19 | 2019-09-03 16:49:00 -0300 | [diff] [blame] | 4412 | |
| 4413 | if (test_bit(MD_BROKEN, &mddev->flags) && st == clean) |
| 4414 | st = broken; |
| 4415 | } else { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4416 | if (list_empty(&mddev->disks) && |
| 4417 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4418 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4419 | st = clear; |
| 4420 | else |
| 4421 | st = inactive; |
| 4422 | } |
| 4423 | return sprintf(page, "%s\n", array_states[st]); |
| 4424 | } |
| 4425 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 4426 | static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev); |
| 4427 | static int md_set_readonly(struct mddev *mddev, struct block_device *bdev); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4428 | static int restart_array(struct mddev *mddev); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4429 | |
| 4430 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4431 | array_state_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4432 | { |
NeilBrown | 6497709b | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 4433 | int err = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4434 | enum array_state st = match_word(buf, array_states); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4435 | |
| 4436 | if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) { |
| 4437 | /* don't take reconfig_mutex when toggling between |
| 4438 | * clean and active |
| 4439 | */ |
| 4440 | spin_lock(&mddev->lock); |
| 4441 | if (st == active) { |
| 4442 | restart_array(mddev); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 4443 | clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags); |
Tomasz Majchrzak | 91a6c4a | 2016-10-25 17:07:08 +0200 | [diff] [blame] | 4444 | md_wakeup_thread(mddev->thread); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4445 | wake_up(&mddev->sb_wait); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4446 | } else /* st == clean */ { |
| 4447 | restart_array(mddev); |
NeilBrown | 6497709b | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 4448 | if (!set_in_sync(mddev)) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4449 | err = -EBUSY; |
| 4450 | } |
Tomasz Majchrzak | 573275b | 2016-06-30 10:47:09 +0200 | [diff] [blame] | 4451 | if (!err) |
| 4452 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4453 | spin_unlock(&mddev->lock); |
NeilBrown | c008f1d | 2015-06-12 19:46:44 +1000 | [diff] [blame] | 4454 | return err ?: len; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4455 | } |
| 4456 | err = mddev_lock(mddev); |
| 4457 | if (err) |
| 4458 | return err; |
| 4459 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4460 | switch(st) { |
| 4461 | case bad_word: |
| 4462 | break; |
| 4463 | case clear: |
| 4464 | /* stopping an active array */ |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 4465 | err = do_md_stop(mddev, 0, NULL); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4466 | break; |
| 4467 | case inactive: |
| 4468 | /* stopping an active array */ |
NeilBrown | 90cf195 | 2012-07-31 10:04:55 +1000 | [diff] [blame] | 4469 | if (mddev->pers) |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 4470 | err = do_md_stop(mddev, 2, NULL); |
NeilBrown | 90cf195 | 2012-07-31 10:04:55 +1000 | [diff] [blame] | 4471 | else |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4472 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4473 | break; |
| 4474 | case suspended: |
| 4475 | break; /* not supported yet */ |
| 4476 | case readonly: |
| 4477 | if (mddev->pers) |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 4478 | err = md_set_readonly(mddev, NULL); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4479 | else { |
| 4480 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 4481 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4482 | err = do_md_run(mddev); |
| 4483 | } |
| 4484 | break; |
| 4485 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4486 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4487 | if (mddev->ro == 0) |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 4488 | err = md_set_readonly(mddev, NULL); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4489 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 4490 | err = restart_array(mddev); |
| 4491 | if (err == 0) { |
| 4492 | mddev->ro = 2; |
| 4493 | set_disk_ro(mddev->gendisk, 0); |
| 4494 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4495 | } else { |
| 4496 | mddev->ro = 2; |
| 4497 | err = do_md_run(mddev); |
| 4498 | } |
| 4499 | break; |
| 4500 | case clean: |
| 4501 | if (mddev->pers) { |
Song Liu | 339421d | 2015-10-08 21:54:13 -0700 | [diff] [blame] | 4502 | err = restart_array(mddev); |
| 4503 | if (err) |
| 4504 | break; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 4505 | spin_lock(&mddev->lock); |
NeilBrown | 6497709b | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 4506 | if (!set_in_sync(mddev)) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4507 | err = -EBUSY; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 4508 | spin_unlock(&mddev->lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 4509 | } else |
| 4510 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4511 | break; |
| 4512 | case active: |
| 4513 | if (mddev->pers) { |
Song Liu | 339421d | 2015-10-08 21:54:13 -0700 | [diff] [blame] | 4514 | err = restart_array(mddev); |
| 4515 | if (err) |
| 4516 | break; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 4517 | clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4518 | wake_up(&mddev->sb_wait); |
| 4519 | err = 0; |
| 4520 | } else { |
| 4521 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 4522 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4523 | err = do_md_run(mddev); |
| 4524 | } |
| 4525 | break; |
| 4526 | case write_pending: |
| 4527 | case active_idle: |
Guilherme G. Piccoli | 62f7b19 | 2019-09-03 16:49:00 -0300 | [diff] [blame] | 4528 | case broken: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4529 | /* these cannot be set */ |
| 4530 | break; |
| 4531 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4532 | |
| 4533 | if (!err) { |
NeilBrown | 1d23f17 | 2011-12-08 15:49:12 +1100 | [diff] [blame] | 4534 | if (mddev->hold_active == UNTIL_IOCTL) |
| 4535 | mddev->hold_active = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4536 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4537 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4538 | mddev_unlock(mddev); |
| 4539 | return err ?: len; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4540 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4541 | static struct md_sysfs_entry md_array_state = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 4542 | __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] | 4543 | |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4544 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4545 | max_corrected_read_errors_show(struct mddev *mddev, char *page) { |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4546 | return sprintf(page, "%d\n", |
| 4547 | atomic_read(&mddev->max_corr_read_errors)); |
| 4548 | } |
| 4549 | |
| 4550 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4551 | 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] | 4552 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4553 | unsigned int n; |
| 4554 | int rv; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4555 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4556 | rv = kstrtouint(buf, 10, &n); |
| 4557 | if (rv < 0) |
| 4558 | return rv; |
| 4559 | atomic_set(&mddev->max_corr_read_errors, n); |
| 4560 | return len; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4561 | } |
| 4562 | |
| 4563 | static struct md_sysfs_entry max_corr_read_errors = |
| 4564 | __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show, |
| 4565 | max_corrected_read_errors_store); |
| 4566 | |
| 4567 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4568 | null_show(struct mddev *mddev, char *page) |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4569 | { |
| 4570 | return -EINVAL; |
| 4571 | } |
| 4572 | |
Guoqing Jiang | cc1ffe6 | 2020-04-04 23:57:08 +0200 | [diff] [blame] | 4573 | /* need to ensure rdev_delayed_delete() has completed */ |
| 4574 | static void flush_rdev_wq(struct mddev *mddev) |
| 4575 | { |
| 4576 | struct md_rdev *rdev; |
| 4577 | |
| 4578 | rcu_read_lock(); |
| 4579 | rdev_for_each_rcu(rdev, mddev) |
| 4580 | if (work_pending(&rdev->del_work)) { |
| 4581 | flush_workqueue(md_rdev_misc_wq); |
| 4582 | break; |
| 4583 | } |
| 4584 | rcu_read_unlock(); |
| 4585 | } |
| 4586 | |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4587 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4588 | new_dev_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4589 | { |
| 4590 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 4591 | /* The new device is added to the array. |
| 4592 | * If the array has a persistent superblock, we read the |
| 4593 | * superblock to initialise info and check validity. |
| 4594 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 4595 | * which mainly checks size. |
| 4596 | */ |
| 4597 | char *e; |
| 4598 | int major = simple_strtoul(buf, &e, 10); |
| 4599 | int minor; |
| 4600 | dev_t dev; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4601 | struct md_rdev *rdev; |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4602 | int err; |
| 4603 | |
| 4604 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 4605 | return -EINVAL; |
| 4606 | minor = simple_strtoul(e+1, &e, 10); |
| 4607 | if (*e && *e != '\n') |
| 4608 | return -EINVAL; |
| 4609 | dev = MKDEV(major, minor); |
| 4610 | if (major != MAJOR(dev) || |
| 4611 | minor != MINOR(dev)) |
| 4612 | return -EOVERFLOW; |
| 4613 | |
Guoqing Jiang | cc1ffe6 | 2020-04-04 23:57:08 +0200 | [diff] [blame] | 4614 | flush_rdev_wq(mddev); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4615 | err = mddev_lock(mddev); |
| 4616 | if (err) |
| 4617 | return err; |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4618 | if (mddev->persistent) { |
| 4619 | rdev = md_import_device(dev, mddev->major_version, |
| 4620 | mddev->minor_version); |
| 4621 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4622 | struct md_rdev *rdev0 |
| 4623 | = list_entry(mddev->disks.next, |
| 4624 | struct md_rdev, same_set); |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4625 | err = super_types[mddev->major_version] |
| 4626 | .load_super(rdev, rdev0, mddev->minor_version); |
| 4627 | if (err < 0) |
| 4628 | goto out; |
| 4629 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 4630 | } else if (mddev->external) |
| 4631 | rdev = md_import_device(dev, -2, -1); |
| 4632 | else |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4633 | rdev = md_import_device(dev, -1, -1); |
| 4634 | |
NeilBrown | 9a8c0fa | 2015-06-25 17:06:40 +1000 | [diff] [blame] | 4635 | if (IS_ERR(rdev)) { |
| 4636 | mddev_unlock(mddev); |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4637 | return PTR_ERR(rdev); |
NeilBrown | 9a8c0fa | 2015-06-25 17:06:40 +1000 | [diff] [blame] | 4638 | } |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4639 | err = bind_rdev_to_array(rdev, mddev); |
| 4640 | out: |
| 4641 | if (err) |
| 4642 | export_rdev(rdev); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4643 | mddev_unlock(mddev); |
Alexey Obitotskiy | 5492c46 | 2017-07-28 15:49:25 +0200 | [diff] [blame] | 4644 | if (!err) |
| 4645 | md_new_event(mddev); |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4646 | return err ? err : len; |
| 4647 | } |
| 4648 | |
| 4649 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4650 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4651 | |
| 4652 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4653 | bitmap_store(struct mddev *mddev, const char *buf, size_t len) |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4654 | { |
| 4655 | char *end; |
| 4656 | unsigned long chunk, end_chunk; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4657 | int err; |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4658 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4659 | err = mddev_lock(mddev); |
| 4660 | if (err) |
| 4661 | return err; |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4662 | if (!mddev->bitmap) |
| 4663 | goto out; |
| 4664 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 4665 | while (*buf) { |
| 4666 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 4667 | if (buf == end) break; |
| 4668 | if (*end == '-') { /* range */ |
| 4669 | buf = end + 1; |
| 4670 | end_chunk = simple_strtoul(buf, &end, 0); |
| 4671 | if (buf == end) break; |
| 4672 | } |
| 4673 | if (*end && !isspace(*end)) break; |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 4674 | md_bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 4675 | buf = skip_spaces(end); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4676 | } |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 4677 | md_bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4678 | out: |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4679 | mddev_unlock(mddev); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4680 | return len; |
| 4681 | } |
| 4682 | |
| 4683 | static struct md_sysfs_entry md_bitmap = |
| 4684 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 4685 | |
| 4686 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4687 | size_show(struct mddev *mddev, char *page) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4688 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4689 | return sprintf(page, "%llu\n", |
| 4690 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4691 | } |
| 4692 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4693 | static int update_size(struct mddev *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4694 | |
| 4695 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4696 | size_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4697 | { |
| 4698 | /* If array is inactive, we can reduce the component size, but |
| 4699 | * not increase it (except from 0). |
| 4700 | * If array is active, we can try an on-line resize |
| 4701 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4702 | sector_t sectors; |
| 4703 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4704 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4705 | if (err < 0) |
| 4706 | return err; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4707 | err = mddev_lock(mddev); |
| 4708 | if (err) |
| 4709 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4710 | if (mddev->pers) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4711 | err = update_size(mddev, sectors); |
Xiao Ni | 4ba1e78 | 2016-06-12 17:18:00 +0800 | [diff] [blame] | 4712 | if (err == 0) |
| 4713 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4714 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4715 | if (mddev->dev_sectors == 0 || |
| 4716 | mddev->dev_sectors > sectors) |
| 4717 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4718 | else |
| 4719 | err = -ENOSPC; |
| 4720 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4721 | mddev_unlock(mddev); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4722 | return err ? err : len; |
| 4723 | } |
| 4724 | |
| 4725 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4726 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4727 | |
Masanari Iida | 83f0d77 | 2012-10-30 00:18:08 +0900 | [diff] [blame] | 4728 | /* Metadata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4729 | * This is one of |
| 4730 | * 'none' for arrays with no metadata (good luck...) |
| 4731 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4732 | * or N.M for internally known formats |
| 4733 | */ |
| 4734 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4735 | metadata_show(struct mddev *mddev, char *page) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4736 | { |
| 4737 | if (mddev->persistent) |
| 4738 | return sprintf(page, "%d.%d\n", |
| 4739 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4740 | else if (mddev->external) |
| 4741 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4742 | else |
| 4743 | return sprintf(page, "none\n"); |
| 4744 | } |
| 4745 | |
| 4746 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4747 | metadata_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4748 | { |
| 4749 | int major, minor; |
| 4750 | char *e; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4751 | int err; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 4752 | /* Changing the details of 'external' metadata is |
| 4753 | * always permitted. Otherwise there must be |
| 4754 | * no devices attached to the array. |
| 4755 | */ |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4756 | |
| 4757 | err = mddev_lock(mddev); |
| 4758 | if (err) |
| 4759 | return err; |
| 4760 | err = -EBUSY; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 4761 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 4762 | ; |
| 4763 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4764 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4765 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4766 | err = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4767 | if (cmd_match(buf, "none")) { |
| 4768 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4769 | mddev->external = 0; |
| 4770 | mddev->major_version = 0; |
| 4771 | mddev->minor_version = 90; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4772 | goto out_unlock; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4773 | } |
| 4774 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 4775 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4776 | if (namelen >= sizeof(mddev->metadata_type)) |
| 4777 | namelen = sizeof(mddev->metadata_type)-1; |
| 4778 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 4779 | mddev->metadata_type[namelen] = 0; |
| 4780 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 4781 | mddev->metadata_type[--namelen] = 0; |
| 4782 | mddev->persistent = 0; |
| 4783 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4784 | mddev->major_version = 0; |
| 4785 | mddev->minor_version = 90; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4786 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4787 | } |
| 4788 | major = simple_strtoul(buf, &e, 10); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4789 | err = -EINVAL; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4790 | if (e==buf || *e != '.') |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4791 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4792 | buf = e+1; |
| 4793 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 4794 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4795 | goto out_unlock; |
| 4796 | err = -ENOENT; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 4797 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4798 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4799 | mddev->major_version = major; |
| 4800 | mddev->minor_version = minor; |
| 4801 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4802 | mddev->external = 0; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4803 | err = 0; |
| 4804 | out_unlock: |
| 4805 | mddev_unlock(mddev); |
| 4806 | return err ?: len; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4807 | } |
| 4808 | |
| 4809 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 4810 | __ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4811 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4812 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4813 | action_show(struct mddev *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4814 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4815 | char *type = "idle"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4816 | unsigned long recovery = mddev->recovery; |
| 4817 | if (test_bit(MD_RECOVERY_FROZEN, &recovery)) |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4818 | type = "frozen"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4819 | else if (test_bit(MD_RECOVERY_RUNNING, &recovery) || |
| 4820 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) { |
| 4821 | if (test_bit(MD_RECOVERY_RESHAPE, &recovery)) |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 4822 | type = "reshape"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4823 | else if (test_bit(MD_RECOVERY_SYNC, &recovery)) { |
| 4824 | if (!test_bit(MD_RECOVERY_REQUESTED, &recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4825 | type = "resync"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4826 | else if (test_bit(MD_RECOVERY_CHECK, &recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4827 | type = "check"; |
| 4828 | else |
| 4829 | type = "repair"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4830 | } else if (test_bit(MD_RECOVERY_RECOVER, &recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4831 | type = "recover"; |
NeilBrown | 985ca97 | 2015-07-06 12:26:57 +1000 | [diff] [blame] | 4832 | else if (mddev->reshape_position != MaxSector) |
| 4833 | type = "reshape"; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4834 | } |
| 4835 | return sprintf(page, "%s\n", type); |
| 4836 | } |
| 4837 | |
| 4838 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4839 | action_store(struct mddev *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4840 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4841 | if (!mddev->pers || !mddev->pers->sync_request) |
| 4842 | return -EINVAL; |
| 4843 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4844 | |
| 4845 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4846 | if (cmd_match(page, "frozen")) |
| 4847 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4848 | else |
| 4849 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 8e8e251 | 2015-06-12 19:51:27 +1000 | [diff] [blame] | 4850 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 4851 | mddev_lock(mddev) == 0) { |
Guoqing Jiang | cc1ffe6 | 2020-04-04 23:57:08 +0200 | [diff] [blame] | 4852 | if (work_pending(&mddev->del_work)) |
| 4853 | flush_workqueue(md_misc_wq); |
NeilBrown | 8e8e251 | 2015-06-12 19:51:27 +1000 | [diff] [blame] | 4854 | if (mddev->sync_thread) { |
| 4855 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4856 | md_reap_sync_thread(mddev); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4857 | } |
NeilBrown | 8e8e251 | 2015-06-12 19:51:27 +1000 | [diff] [blame] | 4858 | mddev_unlock(mddev); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4859 | } |
NeilBrown | 312045e | 2015-12-21 11:01:21 +1100 | [diff] [blame] | 4860 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4861 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4862 | else if (cmd_match(page, "resync")) |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4863 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4864 | else if (cmd_match(page, "recover")) { |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4865 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4866 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4867 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4868 | int err; |
| 4869 | if (mddev->pers->start_reshape == NULL) |
| 4870 | return -EINVAL; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4871 | err = mddev_lock(mddev); |
| 4872 | if (!err) { |
NeilBrown | 312045e | 2015-12-21 11:01:21 +1100 | [diff] [blame] | 4873 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4874 | err = -EBUSY; |
| 4875 | else { |
| 4876 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4877 | err = mddev->pers->start_reshape(mddev); |
| 4878 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4879 | mddev_unlock(mddev); |
| 4880 | } |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4881 | if (err) |
| 4882 | return err; |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 4883 | sysfs_notify_dirent_safe(mddev->sysfs_degraded); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4884 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 4885 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4886 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 4887 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4888 | return -EINVAL; |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4889 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4890 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 4891 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4892 | } |
NeilBrown | 48c26dd | 2012-10-11 14:19:39 +1100 | [diff] [blame] | 4893 | if (mddev->ro == 2) { |
| 4894 | /* A write to sync_action is enough to justify |
| 4895 | * canceling read-auto mode |
| 4896 | */ |
| 4897 | mddev->ro = 0; |
| 4898 | md_wakeup_thread(mddev->sync_thread); |
| 4899 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 4900 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4901 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4902 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4903 | return len; |
| 4904 | } |
| 4905 | |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 4906 | static struct md_sysfs_entry md_scan_mode = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 4907 | __ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 4908 | |
| 4909 | static ssize_t |
| 4910 | last_sync_action_show(struct mddev *mddev, char *page) |
| 4911 | { |
| 4912 | return sprintf(page, "%s\n", mddev->last_sync_action); |
| 4913 | } |
| 4914 | |
| 4915 | static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action); |
| 4916 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4917 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4918 | mismatch_cnt_show(struct mddev *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4919 | { |
| 4920 | return sprintf(page, "%llu\n", |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 4921 | (unsigned long long) |
| 4922 | atomic64_read(&mddev->resync_mismatches)); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4923 | } |
| 4924 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4925 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4926 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4927 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4928 | sync_min_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4929 | { |
| 4930 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 4931 | mddev->sync_speed_min ? "local": "system"); |
| 4932 | } |
| 4933 | |
| 4934 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4935 | sync_min_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4936 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4937 | unsigned int min; |
| 4938 | int rv; |
| 4939 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4940 | if (strncmp(buf, "system", 6)==0) { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4941 | min = 0; |
| 4942 | } else { |
| 4943 | rv = kstrtouint(buf, 10, &min); |
| 4944 | if (rv < 0) |
| 4945 | return rv; |
| 4946 | if (min == 0) |
| 4947 | return -EINVAL; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4948 | } |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4949 | mddev->sync_speed_min = min; |
| 4950 | return len; |
| 4951 | } |
| 4952 | |
| 4953 | static struct md_sysfs_entry md_sync_min = |
| 4954 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 4955 | |
| 4956 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4957 | sync_max_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4958 | { |
| 4959 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 4960 | mddev->sync_speed_max ? "local": "system"); |
| 4961 | } |
| 4962 | |
| 4963 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4964 | sync_max_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4965 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4966 | unsigned int max; |
| 4967 | int rv; |
| 4968 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4969 | if (strncmp(buf, "system", 6)==0) { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4970 | max = 0; |
| 4971 | } else { |
| 4972 | rv = kstrtouint(buf, 10, &max); |
| 4973 | if (rv < 0) |
| 4974 | return rv; |
| 4975 | if (max == 0) |
| 4976 | return -EINVAL; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4977 | } |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4978 | mddev->sync_speed_max = max; |
| 4979 | return len; |
| 4980 | } |
| 4981 | |
| 4982 | static struct md_sysfs_entry md_sync_max = |
| 4983 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 4984 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4985 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4986 | degraded_show(struct mddev *mddev, char *page) |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4987 | { |
| 4988 | return sprintf(page, "%d\n", mddev->degraded); |
| 4989 | } |
| 4990 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4991 | |
| 4992 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4993 | sync_force_parallel_show(struct mddev *mddev, char *page) |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4994 | { |
| 4995 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 4996 | } |
| 4997 | |
| 4998 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4999 | 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] | 5000 | { |
| 5001 | long n; |
| 5002 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 5003 | if (kstrtol(buf, 10, &n)) |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 5004 | return -EINVAL; |
| 5005 | |
| 5006 | if (n != 0 && n != 1) |
| 5007 | return -EINVAL; |
| 5008 | |
| 5009 | mddev->parallel_resync = n; |
| 5010 | |
| 5011 | if (mddev->sync_thread) |
| 5012 | wake_up(&resync_wait); |
| 5013 | |
| 5014 | return len; |
| 5015 | } |
| 5016 | |
| 5017 | /* force parallel resync, even with shared block devices */ |
| 5018 | static struct md_sysfs_entry md_sync_force_parallel = |
| 5019 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 5020 | sync_force_parallel_show, sync_force_parallel_store); |
| 5021 | |
| 5022 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5023 | sync_speed_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5024 | { |
| 5025 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 5026 | if (mddev->curr_resync == 0) |
| 5027 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 5028 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 5029 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5030 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 5031 | db = resync - mddev->resync_mark_cnt; |
| 5032 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5033 | } |
| 5034 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 5035 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5036 | |
| 5037 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5038 | sync_completed_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5039 | { |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 5040 | unsigned long long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5041 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 5042 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 5043 | return sprintf(page, "none\n"); |
| 5044 | |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 5045 | if (mddev->curr_resync == 1 || |
| 5046 | mddev->curr_resync == 2) |
| 5047 | return sprintf(page, "delayed\n"); |
| 5048 | |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 5049 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 5050 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5051 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5052 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5053 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5054 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 5055 | resync = mddev->curr_resync_completed; |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 5056 | return sprintf(page, "%llu / %llu\n", resync, max_sectors); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5057 | } |
| 5058 | |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 5059 | static struct md_sysfs_entry md_sync_completed = |
| 5060 | __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5061 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5062 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5063 | min_sync_show(struct mddev *mddev, char *page) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5064 | { |
| 5065 | return sprintf(page, "%llu\n", |
| 5066 | (unsigned long long)mddev->resync_min); |
| 5067 | } |
| 5068 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5069 | min_sync_store(struct mddev *mddev, const char *buf, size_t len) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5070 | { |
| 5071 | unsigned long long min; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5072 | int err; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5073 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 5074 | if (kstrtoull(buf, 10, &min)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5075 | return -EINVAL; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5076 | |
| 5077 | spin_lock(&mddev->lock); |
| 5078 | err = -EINVAL; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5079 | if (min > mddev->resync_max) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5080 | goto out_unlock; |
| 5081 | |
| 5082 | err = -EBUSY; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5083 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5084 | goto out_unlock; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5085 | |
NeilBrown | 50c37b1 | 2015-03-23 17:36:38 +1100 | [diff] [blame] | 5086 | /* Round down to multiple of 4K for safety */ |
| 5087 | mddev->resync_min = round_down(min, 8); |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5088 | err = 0; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5089 | |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5090 | out_unlock: |
| 5091 | spin_unlock(&mddev->lock); |
| 5092 | return err ?: len; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5093 | } |
| 5094 | |
| 5095 | static struct md_sysfs_entry md_min_sync = |
| 5096 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 5097 | |
| 5098 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5099 | max_sync_show(struct mddev *mddev, char *page) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5100 | { |
| 5101 | if (mddev->resync_max == MaxSector) |
| 5102 | return sprintf(page, "max\n"); |
| 5103 | else |
| 5104 | return sprintf(page, "%llu\n", |
| 5105 | (unsigned long long)mddev->resync_max); |
| 5106 | } |
| 5107 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5108 | max_sync_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5109 | { |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5110 | int err; |
| 5111 | spin_lock(&mddev->lock); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5112 | if (strncmp(buf, "max", 3) == 0) |
| 5113 | mddev->resync_max = MaxSector; |
| 5114 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5115 | unsigned long long max; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5116 | int chunk; |
| 5117 | |
| 5118 | err = -EINVAL; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 5119 | if (kstrtoull(buf, 10, &max)) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5120 | goto out_unlock; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5121 | if (max < mddev->resync_min) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5122 | goto out_unlock; |
| 5123 | |
| 5124 | err = -EBUSY; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5125 | if (max < mddev->resync_max && |
NeilBrown | 4d484a4 | 2009-08-13 10:41:50 +1000 | [diff] [blame] | 5126 | mddev->ro == 0 && |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5127 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5128 | goto out_unlock; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5129 | |
| 5130 | /* Must be a multiple of chunk_size */ |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5131 | chunk = mddev->chunk_sectors; |
| 5132 | if (chunk) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 5133 | sector_t temp = max; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5134 | |
| 5135 | err = -EINVAL; |
| 5136 | if (sector_div(temp, chunk)) |
| 5137 | goto out_unlock; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5138 | } |
| 5139 | mddev->resync_max = max; |
| 5140 | } |
| 5141 | wake_up(&mddev->recovery_wait); |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5142 | err = 0; |
| 5143 | out_unlock: |
| 5144 | spin_unlock(&mddev->lock); |
| 5145 | return err ?: len; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5146 | } |
| 5147 | |
| 5148 | static struct md_sysfs_entry md_max_sync = |
| 5149 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 5150 | |
| 5151 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5152 | suspend_lo_show(struct mddev *mddev, char *page) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5153 | { |
| 5154 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 5155 | } |
| 5156 | |
| 5157 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5158 | suspend_lo_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5159 | { |
NeilBrown | b03e0cc | 2017-10-19 12:49:15 +1100 | [diff] [blame] | 5160 | unsigned long long new; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5161 | int err; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5162 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 5163 | err = kstrtoull(buf, 10, &new); |
| 5164 | if (err < 0) |
| 5165 | return err; |
| 5166 | if (new != (sector_t)new) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5167 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 5168 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5169 | err = mddev_lock(mddev); |
| 5170 | if (err) |
| 5171 | return err; |
| 5172 | err = -EINVAL; |
| 5173 | if (mddev->pers == NULL || |
| 5174 | mddev->pers->quiesce == NULL) |
| 5175 | goto unlock; |
NeilBrown | b03e0cc | 2017-10-19 12:49:15 +1100 | [diff] [blame] | 5176 | mddev_suspend(mddev); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 5177 | mddev->suspend_lo = new; |
NeilBrown | b03e0cc | 2017-10-19 12:49:15 +1100 | [diff] [blame] | 5178 | mddev_resume(mddev); |
| 5179 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5180 | err = 0; |
| 5181 | unlock: |
| 5182 | mddev_unlock(mddev); |
| 5183 | return err ?: len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5184 | } |
| 5185 | static struct md_sysfs_entry md_suspend_lo = |
| 5186 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 5187 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5188 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5189 | suspend_hi_show(struct mddev *mddev, char *page) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5190 | { |
| 5191 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 5192 | } |
| 5193 | |
| 5194 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5195 | suspend_hi_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5196 | { |
NeilBrown | b03e0cc | 2017-10-19 12:49:15 +1100 | [diff] [blame] | 5197 | unsigned long long new; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5198 | int err; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5199 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 5200 | err = kstrtoull(buf, 10, &new); |
| 5201 | if (err < 0) |
| 5202 | return err; |
| 5203 | if (new != (sector_t)new) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5204 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 5205 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5206 | err = mddev_lock(mddev); |
| 5207 | if (err) |
| 5208 | return err; |
| 5209 | err = -EINVAL; |
NeilBrown | b03e0cc | 2017-10-19 12:49:15 +1100 | [diff] [blame] | 5210 | if (mddev->pers == NULL) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5211 | goto unlock; |
NeilBrown | b03e0cc | 2017-10-19 12:49:15 +1100 | [diff] [blame] | 5212 | |
| 5213 | mddev_suspend(mddev); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 5214 | mddev->suspend_hi = new; |
NeilBrown | b03e0cc | 2017-10-19 12:49:15 +1100 | [diff] [blame] | 5215 | mddev_resume(mddev); |
| 5216 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5217 | err = 0; |
| 5218 | unlock: |
| 5219 | mddev_unlock(mddev); |
| 5220 | return err ?: len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5221 | } |
| 5222 | static struct md_sysfs_entry md_suspend_hi = |
| 5223 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 5224 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 5225 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5226 | reshape_position_show(struct mddev *mddev, char *page) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 5227 | { |
| 5228 | if (mddev->reshape_position != MaxSector) |
| 5229 | return sprintf(page, "%llu\n", |
| 5230 | (unsigned long long)mddev->reshape_position); |
| 5231 | strcpy(page, "none\n"); |
| 5232 | return 5; |
| 5233 | } |
| 5234 | |
| 5235 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5236 | reshape_position_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 5237 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5238 | struct md_rdev *rdev; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 5239 | unsigned long long new; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5240 | int err; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5241 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 5242 | err = kstrtoull(buf, 10, &new); |
| 5243 | if (err < 0) |
| 5244 | return err; |
| 5245 | if (new != (sector_t)new) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 5246 | return -EINVAL; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5247 | err = mddev_lock(mddev); |
| 5248 | if (err) |
| 5249 | return err; |
| 5250 | err = -EBUSY; |
| 5251 | if (mddev->pers) |
| 5252 | goto unlock; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 5253 | mddev->reshape_position = new; |
| 5254 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5255 | mddev->reshape_backwards = 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 5256 | mddev->new_level = mddev->level; |
| 5257 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5258 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5259 | rdev_for_each(rdev, mddev) |
| 5260 | rdev->new_data_offset = rdev->data_offset; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5261 | err = 0; |
| 5262 | unlock: |
| 5263 | mddev_unlock(mddev); |
| 5264 | return err ?: len; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 5265 | } |
| 5266 | |
| 5267 | static struct md_sysfs_entry md_reshape_position = |
| 5268 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 5269 | reshape_position_store); |
| 5270 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5271 | static ssize_t |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5272 | reshape_direction_show(struct mddev *mddev, char *page) |
| 5273 | { |
| 5274 | return sprintf(page, "%s\n", |
| 5275 | mddev->reshape_backwards ? "backwards" : "forwards"); |
| 5276 | } |
| 5277 | |
| 5278 | static ssize_t |
| 5279 | reshape_direction_store(struct mddev *mddev, const char *buf, size_t len) |
| 5280 | { |
| 5281 | int backwards = 0; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5282 | int err; |
| 5283 | |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5284 | if (cmd_match(buf, "forwards")) |
| 5285 | backwards = 0; |
| 5286 | else if (cmd_match(buf, "backwards")) |
| 5287 | backwards = 1; |
| 5288 | else |
| 5289 | return -EINVAL; |
| 5290 | if (mddev->reshape_backwards == backwards) |
| 5291 | return len; |
| 5292 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5293 | err = mddev_lock(mddev); |
| 5294 | if (err) |
| 5295 | return err; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5296 | /* check if we are allowed to change */ |
| 5297 | if (mddev->delta_disks) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5298 | err = -EBUSY; |
| 5299 | else if (mddev->persistent && |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5300 | mddev->major_version == 0) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5301 | err = -EINVAL; |
| 5302 | else |
| 5303 | mddev->reshape_backwards = backwards; |
| 5304 | mddev_unlock(mddev); |
| 5305 | return err ?: len; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5306 | } |
| 5307 | |
| 5308 | static struct md_sysfs_entry md_reshape_direction = |
| 5309 | __ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show, |
| 5310 | reshape_direction_store); |
| 5311 | |
| 5312 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5313 | array_size_show(struct mddev *mddev, char *page) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5314 | { |
| 5315 | if (mddev->external_size) |
| 5316 | return sprintf(page, "%llu\n", |
| 5317 | (unsigned long long)mddev->array_sectors/2); |
| 5318 | else |
| 5319 | return sprintf(page, "default\n"); |
| 5320 | } |
| 5321 | |
| 5322 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5323 | array_size_store(struct mddev *mddev, const char *buf, size_t len) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5324 | { |
| 5325 | sector_t sectors; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5326 | int err; |
| 5327 | |
| 5328 | err = mddev_lock(mddev); |
| 5329 | if (err) |
| 5330 | return err; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5331 | |
Guoqing Jiang | ab5a98b | 2016-05-02 11:33:13 -0400 | [diff] [blame] | 5332 | /* cluster raid doesn't support change array_sectors */ |
Zhilong Liu | b670883 | 2017-04-10 14:15:55 +0800 | [diff] [blame] | 5333 | if (mddev_is_clustered(mddev)) { |
| 5334 | mddev_unlock(mddev); |
Guoqing Jiang | ab5a98b | 2016-05-02 11:33:13 -0400 | [diff] [blame] | 5335 | return -EINVAL; |
Zhilong Liu | b670883 | 2017-04-10 14:15:55 +0800 | [diff] [blame] | 5336 | } |
Guoqing Jiang | ab5a98b | 2016-05-02 11:33:13 -0400 | [diff] [blame] | 5337 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5338 | if (strncmp(buf, "default", 7) == 0) { |
| 5339 | if (mddev->pers) |
| 5340 | sectors = mddev->pers->size(mddev, 0, 0); |
| 5341 | else |
| 5342 | sectors = mddev->array_sectors; |
| 5343 | |
| 5344 | mddev->external_size = 0; |
| 5345 | } else { |
| 5346 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5347 | err = -EINVAL; |
| 5348 | else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
| 5349 | err = -E2BIG; |
| 5350 | else |
| 5351 | mddev->external_size = 1; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5352 | } |
| 5353 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5354 | if (!err) { |
| 5355 | mddev->array_sectors = sectors; |
Christoph Hellwig | 2c247c5 | 2020-11-16 15:57:11 +0100 | [diff] [blame] | 5356 | if (mddev->pers) |
| 5357 | set_capacity_and_notify(mddev->gendisk, |
| 5358 | mddev->array_sectors); |
NeilBrown | cbe6ef1 | 2011-02-16 13:58:38 +1100 | [diff] [blame] | 5359 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5360 | mddev_unlock(mddev); |
| 5361 | return err ?: len; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5362 | } |
| 5363 | |
| 5364 | static struct md_sysfs_entry md_array_size = |
| 5365 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 5366 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5367 | |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 5368 | static ssize_t |
| 5369 | consistency_policy_show(struct mddev *mddev, char *page) |
| 5370 | { |
| 5371 | int ret; |
| 5372 | |
| 5373 | if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) { |
| 5374 | ret = sprintf(page, "journal\n"); |
| 5375 | } else if (test_bit(MD_HAS_PPL, &mddev->flags)) { |
| 5376 | ret = sprintf(page, "ppl\n"); |
| 5377 | } else if (mddev->bitmap) { |
| 5378 | ret = sprintf(page, "bitmap\n"); |
| 5379 | } else if (mddev->pers) { |
| 5380 | if (mddev->pers->sync_request) |
| 5381 | ret = sprintf(page, "resync\n"); |
| 5382 | else |
| 5383 | ret = sprintf(page, "none\n"); |
| 5384 | } else { |
| 5385 | ret = sprintf(page, "unknown\n"); |
| 5386 | } |
| 5387 | |
| 5388 | return ret; |
| 5389 | } |
| 5390 | |
| 5391 | static ssize_t |
| 5392 | consistency_policy_store(struct mddev *mddev, const char *buf, size_t len) |
| 5393 | { |
Artur Paszkiewicz | ba903a3 | 2017-03-09 10:00:03 +0100 | [diff] [blame] | 5394 | int err = 0; |
| 5395 | |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 5396 | if (mddev->pers) { |
Artur Paszkiewicz | ba903a3 | 2017-03-09 10:00:03 +0100 | [diff] [blame] | 5397 | if (mddev->pers->change_consistency_policy) |
| 5398 | err = mddev->pers->change_consistency_policy(mddev, buf); |
| 5399 | else |
| 5400 | err = -EBUSY; |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 5401 | } else if (mddev->external && strncmp(buf, "ppl", 3) == 0) { |
| 5402 | set_bit(MD_HAS_PPL, &mddev->flags); |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 5403 | } else { |
Artur Paszkiewicz | ba903a3 | 2017-03-09 10:00:03 +0100 | [diff] [blame] | 5404 | err = -EINVAL; |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 5405 | } |
Artur Paszkiewicz | ba903a3 | 2017-03-09 10:00:03 +0100 | [diff] [blame] | 5406 | |
| 5407 | return err ? err : len; |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 5408 | } |
| 5409 | |
| 5410 | static struct md_sysfs_entry md_consistency_policy = |
| 5411 | __ATTR(consistency_policy, S_IRUGO | S_IWUSR, consistency_policy_show, |
| 5412 | consistency_policy_store); |
| 5413 | |
Guoqing Jiang | 9a56784 | 2019-07-24 11:09:19 +0200 | [diff] [blame] | 5414 | static ssize_t fail_last_dev_show(struct mddev *mddev, char *page) |
| 5415 | { |
| 5416 | return sprintf(page, "%d\n", mddev->fail_last_dev); |
| 5417 | } |
| 5418 | |
| 5419 | /* |
| 5420 | * Setting fail_last_dev to true to allow last device to be forcibly removed |
| 5421 | * from RAID1/RAID10. |
| 5422 | */ |
| 5423 | static ssize_t |
| 5424 | fail_last_dev_store(struct mddev *mddev, const char *buf, size_t len) |
| 5425 | { |
| 5426 | int ret; |
| 5427 | bool value; |
| 5428 | |
| 5429 | ret = kstrtobool(buf, &value); |
| 5430 | if (ret) |
| 5431 | return ret; |
| 5432 | |
| 5433 | if (value != mddev->fail_last_dev) |
| 5434 | mddev->fail_last_dev = value; |
| 5435 | |
| 5436 | return len; |
| 5437 | } |
| 5438 | static struct md_sysfs_entry md_fail_last_dev = |
| 5439 | __ATTR(fail_last_dev, S_IRUGO | S_IWUSR, fail_last_dev_show, |
| 5440 | fail_last_dev_store); |
| 5441 | |
Guoqing Jiang | 3938f5f | 2019-12-23 10:48:56 +0100 | [diff] [blame] | 5442 | static ssize_t serialize_policy_show(struct mddev *mddev, char *page) |
| 5443 | { |
| 5444 | if (mddev->pers == NULL || (mddev->pers->level != 1)) |
| 5445 | return sprintf(page, "n/a\n"); |
| 5446 | else |
| 5447 | return sprintf(page, "%d\n", mddev->serialize_policy); |
| 5448 | } |
| 5449 | |
| 5450 | /* |
| 5451 | * Setting serialize_policy to true to enforce write IO is not reordered |
| 5452 | * for raid1. |
| 5453 | */ |
| 5454 | static ssize_t |
| 5455 | serialize_policy_store(struct mddev *mddev, const char *buf, size_t len) |
| 5456 | { |
| 5457 | int err; |
| 5458 | bool value; |
| 5459 | |
| 5460 | err = kstrtobool(buf, &value); |
| 5461 | if (err) |
| 5462 | return err; |
| 5463 | |
| 5464 | if (value == mddev->serialize_policy) |
| 5465 | return len; |
| 5466 | |
| 5467 | err = mddev_lock(mddev); |
| 5468 | if (err) |
| 5469 | return err; |
| 5470 | if (mddev->pers == NULL || (mddev->pers->level != 1)) { |
| 5471 | pr_err("md: serialize_policy is only effective for raid1\n"); |
| 5472 | err = -EINVAL; |
| 5473 | goto unlock; |
| 5474 | } |
| 5475 | |
| 5476 | mddev_suspend(mddev); |
| 5477 | if (value) |
| 5478 | mddev_create_serial_pool(mddev, NULL, true); |
| 5479 | else |
| 5480 | mddev_destroy_serial_pool(mddev, NULL, true); |
| 5481 | mddev->serialize_policy = value; |
| 5482 | mddev_resume(mddev); |
| 5483 | unlock: |
| 5484 | mddev_unlock(mddev); |
| 5485 | return err ?: len; |
| 5486 | } |
| 5487 | |
| 5488 | static struct md_sysfs_entry md_serialize_policy = |
| 5489 | __ATTR(serialize_policy, S_IRUGO | S_IWUSR, serialize_policy_show, |
| 5490 | serialize_policy_store); |
| 5491 | |
| 5492 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5493 | static struct attribute *md_default_attrs[] = { |
| 5494 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 5495 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5496 | &md_raid_disks.attr, |
Sebastian Parschauer | ec164d07 | 2020-07-28 12:01:39 +0200 | [diff] [blame] | 5497 | &md_uuid.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 5498 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5499 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 5500 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 5501 | &md_metadata.attr, |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 5502 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 5503 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5504 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 5505 | &md_reshape_position.attr, |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5506 | &md_reshape_direction.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5507 | &md_array_size.attr, |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 5508 | &max_corr_read_errors.attr, |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 5509 | &md_consistency_policy.attr, |
Guoqing Jiang | 9a56784 | 2019-07-24 11:09:19 +0200 | [diff] [blame] | 5510 | &md_fail_last_dev.attr, |
Guoqing Jiang | 3938f5f | 2019-12-23 10:48:56 +0100 | [diff] [blame] | 5511 | &md_serialize_policy.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 5512 | NULL, |
| 5513 | }; |
| 5514 | |
| 5515 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 5516 | &md_scan_mode.attr, |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 5517 | &md_last_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 5518 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5519 | &md_sync_min.attr, |
| 5520 | &md_sync_max.attr, |
| 5521 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 5522 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5523 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5524 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5525 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5526 | &md_suspend_lo.attr, |
| 5527 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 5528 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 5529 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5530 | NULL, |
| 5531 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 5532 | static struct attribute_group md_redundancy_group = { |
| 5533 | .name = NULL, |
| 5534 | .attrs = md_redundancy_attrs, |
| 5535 | }; |
| 5536 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5537 | static ssize_t |
| 5538 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 5539 | { |
| 5540 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5541 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 5542 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5543 | |
| 5544 | if (!entry->show) |
| 5545 | return -EIO; |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 5546 | spin_lock(&all_mddevs_lock); |
| 5547 | if (list_empty(&mddev->all_mddevs)) { |
| 5548 | spin_unlock(&all_mddevs_lock); |
| 5549 | return -EBUSY; |
| 5550 | } |
| 5551 | mddev_get(mddev); |
| 5552 | spin_unlock(&all_mddevs_lock); |
| 5553 | |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 5554 | rv = entry->show(mddev, page); |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 5555 | mddev_put(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 5556 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5557 | } |
| 5558 | |
| 5559 | static ssize_t |
| 5560 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 5561 | const char *page, size_t length) |
| 5562 | { |
| 5563 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5564 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 5565 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5566 | |
| 5567 | if (!entry->store) |
| 5568 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 5569 | if (!capable(CAP_SYS_ADMIN)) |
| 5570 | return -EACCES; |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 5571 | spin_lock(&all_mddevs_lock); |
| 5572 | if (list_empty(&mddev->all_mddevs)) { |
| 5573 | spin_unlock(&all_mddevs_lock); |
| 5574 | return -EBUSY; |
| 5575 | } |
| 5576 | mddev_get(mddev); |
| 5577 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5578 | rv = entry->store(mddev, page, length); |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 5579 | mddev_put(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 5580 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5581 | } |
| 5582 | |
| 5583 | static void md_free(struct kobject *ko) |
| 5584 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5585 | struct mddev *mddev = container_of(ko, struct mddev, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 5586 | |
| 5587 | if (mddev->sysfs_state) |
| 5588 | sysfs_put(mddev->sysfs_state); |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 5589 | if (mddev->sysfs_level) |
| 5590 | sysfs_put(mddev->sysfs_level); |
| 5591 | |
Bart Van Assche | d8115c35 | 2018-02-28 10:15:29 -0800 | [diff] [blame] | 5592 | if (mddev->gendisk) |
| 5593 | del_gendisk(mddev->gendisk); |
NeilBrown | 6cd18e7 | 2015-04-27 14:12:22 +1000 | [diff] [blame] | 5594 | if (mddev->queue) |
| 5595 | blk_cleanup_queue(mddev->queue); |
Bart Van Assche | d8115c35 | 2018-02-28 10:15:29 -0800 | [diff] [blame] | 5596 | if (mddev->gendisk) |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 5597 | put_disk(mddev->gendisk); |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 5598 | percpu_ref_exit(&mddev->writes_pending); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 5599 | |
Kent Overstreet | 28dec87 | 2018-06-07 20:52:54 -0400 | [diff] [blame] | 5600 | bioset_exit(&mddev->bio_set); |
| 5601 | bioset_exit(&mddev->sync_set); |
Artur Paszkiewicz | 41d2d84 | 2020-07-03 11:13:09 +0200 | [diff] [blame] | 5602 | mempool_exit(&mddev->md_io_pool); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5603 | kfree(mddev); |
| 5604 | } |
| 5605 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 5606 | static const struct sysfs_ops md_sysfs_ops = { |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5607 | .show = md_attr_show, |
| 5608 | .store = md_attr_store, |
| 5609 | }; |
| 5610 | static struct kobj_type md_ktype = { |
| 5611 | .release = md_free, |
| 5612 | .sysfs_ops = &md_sysfs_ops, |
| 5613 | .default_attrs = md_default_attrs, |
| 5614 | }; |
| 5615 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5616 | int mdp_major = 0; |
| 5617 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 5618 | static void mddev_delayed_delete(struct work_struct *ws) |
| 5619 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5620 | struct mddev *mddev = container_of(ws, struct mddev, del_work); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 5621 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 5622 | sysfs_remove_group(&mddev->kobj, &md_bitmap_group); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 5623 | kobject_del(&mddev->kobj); |
| 5624 | kobject_put(&mddev->kobj); |
| 5625 | } |
| 5626 | |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 5627 | static void no_op(struct percpu_ref *r) {} |
| 5628 | |
NeilBrown | a415c0f | 2017-06-05 16:05:13 +1000 | [diff] [blame] | 5629 | int mddev_init_writes_pending(struct mddev *mddev) |
| 5630 | { |
| 5631 | if (mddev->writes_pending.percpu_count_ptr) |
| 5632 | return 0; |
Roman Gushchin | ddde2af | 2019-05-07 10:01:49 -0700 | [diff] [blame] | 5633 | if (percpu_ref_init(&mddev->writes_pending, no_op, |
| 5634 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL) < 0) |
NeilBrown | a415c0f | 2017-06-05 16:05:13 +1000 | [diff] [blame] | 5635 | return -ENOMEM; |
| 5636 | /* We want to start with the refcount at zero */ |
| 5637 | percpu_ref_put(&mddev->writes_pending); |
| 5638 | return 0; |
| 5639 | } |
| 5640 | EXPORT_SYMBOL_GPL(mddev_init_writes_pending); |
| 5641 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5642 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5643 | { |
NeilBrown | 039b722 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 5644 | /* |
| 5645 | * If dev is zero, name is the name of a device to allocate with |
| 5646 | * an arbitrary minor number. It will be "md_???" |
| 5647 | * If dev is non-zero it must be a device number with a MAJOR of |
| 5648 | * MD_MAJOR or mdp_major. In this case, if "name" is NULL, then |
| 5649 | * the device is being created by opening a node in /dev. |
| 5650 | * If "name" is not NULL, the device is being created by |
| 5651 | * writing to /sys/module/md_mod/parameters/new_array. |
| 5652 | */ |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 5653 | static DEFINE_MUTEX(disks_mutex); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5654 | struct mddev *mddev = mddev_find(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5655 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5656 | int partitioned; |
| 5657 | int shift; |
| 5658 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 5659 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5660 | |
| 5661 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5662 | return -ENODEV; |
| 5663 | |
| 5664 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 5665 | shift = partitioned ? MdpMinorShift : 0; |
| 5666 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5667 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 5668 | /* wait for any previous instance of this device to be |
| 5669 | * completely removed (mddev_delayed_delete). |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5670 | */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 5671 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5672 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 5673 | mutex_lock(&disks_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5674 | error = -EEXIST; |
| 5675 | if (mddev->gendisk) |
| 5676 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5677 | |
NeilBrown | 039b722 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 5678 | if (name && !dev) { |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5679 | /* Need to ensure that 'name' is not a duplicate. |
| 5680 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5681 | struct mddev *mddev2; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5682 | spin_lock(&all_mddevs_lock); |
| 5683 | |
| 5684 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 5685 | if (mddev2->gendisk && |
| 5686 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 5687 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5688 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5689 | } |
| 5690 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5691 | } |
NeilBrown | 039b722 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 5692 | if (name && dev) |
| 5693 | /* |
| 5694 | * Creating /dev/mdNNN via "newarray", so adjust hold_active. |
| 5695 | */ |
| 5696 | mddev->hold_active = UNTIL_STOP; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5697 | |
Artur Paszkiewicz | 41d2d84 | 2020-07-03 11:13:09 +0200 | [diff] [blame] | 5698 | error = mempool_init_kmalloc_pool(&mddev->md_io_pool, BIO_POOL_SIZE, |
| 5699 | sizeof(struct md_io)); |
| 5700 | if (error) |
| 5701 | goto abort; |
| 5702 | |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5703 | error = -ENOMEM; |
Christoph Hellwig | c62b37d | 2020-07-01 10:59:43 +0200 | [diff] [blame] | 5704 | mddev->queue = blk_alloc_queue(NUMA_NO_NODE); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5705 | if (!mddev->queue) |
| 5706 | goto abort; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5707 | |
Martin K. Petersen | b1bd055 | 2012-01-11 16:27:11 +0100 | [diff] [blame] | 5708 | blk_set_stacking_limits(&mddev->queue->limits); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5709 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5710 | disk = alloc_disk(1 << shift); |
| 5711 | if (!disk) { |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5712 | blk_cleanup_queue(mddev->queue); |
| 5713 | mddev->queue = NULL; |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5714 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5715 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5716 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5717 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5718 | if (name) |
| 5719 | strcpy(disk->disk_name, name); |
| 5720 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5721 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f46 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 5722 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5723 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5724 | disk->fops = &md_fops; |
| 5725 | disk->private_data = mddev; |
| 5726 | disk->queue = mddev->queue; |
Jens Axboe | 56883a7 | 2016-03-30 10:16:53 -0600 | [diff] [blame] | 5727 | blk_queue_write_cache(mddev->queue, true, true); |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 5728 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5729 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 5730 | * remove it now. |
| 5731 | */ |
| 5732 | disk->flags |= GENHD_FL_EXT_DEVT; |
Christoph Hellwig | a564e23 | 2020-07-08 14:25:41 +0200 | [diff] [blame] | 5733 | disk->events |= DISK_EVENT_MEDIA_CHANGE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5734 | mddev->gendisk = disk; |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 5735 | /* As soon as we call add_disk(), another thread could get |
| 5736 | * through to md_open, so make sure it doesn't get too far |
| 5737 | */ |
| 5738 | mutex_lock(&mddev->open_mutex); |
| 5739 | add_disk(disk); |
| 5740 | |
Kent Overstreet | 28dec87 | 2018-06-07 20:52:54 -0400 | [diff] [blame] | 5741 | error = kobject_add(&mddev->kobj, &disk_to_dev(disk)->kobj, "%s", "md"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5742 | if (error) { |
| 5743 | /* This isn't possible, but as kobject_init_and_add is marked |
| 5744 | * __must_check, we must do something with the result |
| 5745 | */ |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5746 | pr_debug("md: cannot register %s/md - name in use\n", |
| 5747 | disk->disk_name); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5748 | error = 0; |
| 5749 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5750 | if (mddev->kobj.sd && |
| 5751 | sysfs_create_group(&mddev->kobj, &md_bitmap_group)) |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5752 | pr_debug("pointless warning\n"); |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 5753 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5754 | abort: |
| 5755 | mutex_unlock(&disks_mutex); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5756 | if (!error && mddev->kobj.sd) { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 5757 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5758 | mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state"); |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 5759 | mddev->sysfs_level = sysfs_get_dirent_safe(mddev->kobj.sd, "level"); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 5760 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5761 | mddev_put(mddev); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5762 | return error; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5763 | } |
| 5764 | |
Christoph Hellwig | 28144f9 | 2020-10-29 15:58:34 +0100 | [diff] [blame] | 5765 | static void md_probe(dev_t dev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5766 | { |
Christoph Hellwig | 28144f9 | 2020-10-29 15:58:34 +0100 | [diff] [blame] | 5767 | if (MAJOR(dev) == MD_MAJOR && MINOR(dev) >= 512) |
| 5768 | return; |
NeilBrown | 78b6350 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 5769 | if (create_on_open) |
| 5770 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5771 | } |
| 5772 | |
Kees Cook | e4dca7b | 2017-10-17 19:04:42 -0700 | [diff] [blame] | 5773 | static int add_named_array(const char *val, const struct kernel_param *kp) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5774 | { |
NeilBrown | 039b722 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 5775 | /* |
| 5776 | * val must be "md_*" or "mdNNN". |
| 5777 | * For "md_*" we allocate an array with a large free minor number, and |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5778 | * set the name to val. val must not already be an active name. |
NeilBrown | 039b722 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 5779 | * For "mdNNN" we allocate an array with the minor number NNN |
| 5780 | * which must not already be in use. |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5781 | */ |
| 5782 | int len = strlen(val); |
| 5783 | char buf[DISK_NAME_LEN]; |
NeilBrown | 039b722 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 5784 | unsigned long devnum; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5785 | |
| 5786 | while (len && val[len-1] == '\n') |
| 5787 | len--; |
| 5788 | if (len >= DISK_NAME_LEN) |
| 5789 | return -E2BIG; |
| 5790 | strlcpy(buf, val, len+1); |
NeilBrown | 039b722 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 5791 | if (strncmp(buf, "md_", 3) == 0) |
| 5792 | return md_alloc(0, buf); |
| 5793 | if (strncmp(buf, "md", 2) == 0 && |
| 5794 | isdigit(buf[2]) && |
| 5795 | kstrtoul(buf+2, 10, &devnum) == 0 && |
| 5796 | devnum <= MINORMASK) |
| 5797 | return md_alloc(MKDEV(MD_MAJOR, devnum), NULL); |
| 5798 | |
| 5799 | return -EINVAL; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5800 | } |
| 5801 | |
Kees Cook | 8376d3c | 2017-10-16 17:01:48 -0700 | [diff] [blame] | 5802 | static void md_safemode_timeout(struct timer_list *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5803 | { |
Kees Cook | 8376d3c | 2017-10-16 17:01:48 -0700 | [diff] [blame] | 5804 | struct mddev *mddev = from_timer(mddev, t, safemode_timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5805 | |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 5806 | mddev->safemode = 1; |
| 5807 | if (mddev->external) |
| 5808 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
| 5809 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5810 | md_wakeup_thread(mddev->thread); |
| 5811 | } |
| 5812 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5813 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5814 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5815 | int md_run(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5816 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5817 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5818 | struct md_rdev *rdev; |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 5819 | struct md_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5820 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 5821 | if (list_empty(&mddev->disks)) |
| 5822 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5823 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5824 | |
| 5825 | if (mddev->pers) |
| 5826 | return -EBUSY; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 5827 | /* Cannot run until previous stop completes properly */ |
| 5828 | if (mddev->sysfs_active) |
| 5829 | return -EBUSY; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 5830 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5831 | /* |
| 5832 | * Analyze all RAID superblock(s) |
| 5833 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 5834 | if (!mddev->raid_disks) { |
| 5835 | if (!mddev->persistent) |
| 5836 | return -EINVAL; |
Yufen Yu | 6a5cb53 | 2019-10-16 16:00:03 +0800 | [diff] [blame] | 5837 | err = analyze_sbs(mddev); |
| 5838 | if (err) |
| 5839 | return -EINVAL; |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 5840 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5841 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5842 | if (mddev->level != LEVEL_NONE) |
| 5843 | request_module("md-level-%d", mddev->level); |
| 5844 | else if (mddev->clevel[0]) |
| 5845 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5846 | |
| 5847 | /* |
| 5848 | * Drop all container device buffers, from now on |
| 5849 | * the only valid external interface is through the md |
| 5850 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5851 | */ |
Heinz Mauelshagen | 4b6c106 | 2018-02-02 23:13:19 +0100 | [diff] [blame] | 5852 | mddev->has_superblocks = false; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5853 | rdev_for_each(rdev, mddev) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5854 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5855 | continue; |
| 5856 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 5857 | invalidate_bdev(rdev->bdev); |
NeilBrown | 97b20ef | 2017-04-13 08:53:48 +1000 | [diff] [blame] | 5858 | if (mddev->ro != 1 && |
| 5859 | (bdev_read_only(rdev->bdev) || |
| 5860 | bdev_read_only(rdev->meta_bdev))) { |
| 5861 | mddev->ro = 1; |
| 5862 | if (mddev->gendisk) |
| 5863 | set_disk_ro(mddev->gendisk, 1); |
| 5864 | } |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5865 | |
Heinz Mauelshagen | 4b6c106 | 2018-02-02 23:13:19 +0100 | [diff] [blame] | 5866 | if (rdev->sb_page) |
| 5867 | mddev->has_superblocks = true; |
| 5868 | |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5869 | /* perform some consistency tests on the device. |
| 5870 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5871 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5872 | */ |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 5873 | if (rdev->meta_bdev) { |
| 5874 | /* Nothing to check */; |
| 5875 | } else if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5876 | if (mddev->dev_sectors && |
| 5877 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5878 | > rdev->sb_start) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5879 | pr_warn("md: %s: data overlaps metadata\n", |
| 5880 | mdname(mddev)); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5881 | return -EINVAL; |
| 5882 | } |
| 5883 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5884 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5885 | > rdev->data_offset) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5886 | pr_warn("md: %s: metadata overlaps data\n", |
| 5887 | mdname(mddev)); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5888 | return -EINVAL; |
| 5889 | } |
| 5890 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5891 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5892 | } |
| 5893 | |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 5894 | if (!bioset_initialized(&mddev->bio_set)) { |
| 5895 | err = bioset_init(&mddev->bio_set, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS); |
| 5896 | if (err) |
| 5897 | return err; |
Ming Lei | 1027317 | 2017-02-14 23:29:00 +0800 | [diff] [blame] | 5898 | } |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 5899 | if (!bioset_initialized(&mddev->sync_set)) { |
| 5900 | err = bioset_init(&mddev->sync_set, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS); |
| 5901 | if (err) |
Kent Overstreet | 28dec87 | 2018-06-07 20:52:54 -0400 | [diff] [blame] | 5902 | return err; |
NeilBrown | 5a85071 | 2017-06-21 09:12:21 +1000 | [diff] [blame] | 5903 | } |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 5904 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5905 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5906 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5907 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5908 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5909 | if (mddev->level != LEVEL_NONE) |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5910 | pr_warn("md: personality for level %d is not loaded!\n", |
| 5911 | mddev->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5912 | else |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5913 | pr_warn("md: personality for level %s is not loaded!\n", |
| 5914 | mddev->clevel); |
Shaohua Li | bfc9dfd | 2018-06-13 08:39:49 -0700 | [diff] [blame] | 5915 | err = -EINVAL; |
| 5916 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5917 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5918 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5919 | if (mddev->level != pers->level) { |
| 5920 | mddev->level = pers->level; |
| 5921 | mddev->new_level = pers->level; |
| 5922 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5923 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5924 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5925 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5926 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5927 | /* This personality cannot handle reshaping... */ |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5928 | module_put(pers->owner); |
Shaohua Li | bfc9dfd | 2018-06-13 08:39:49 -0700 | [diff] [blame] | 5929 | err = -EINVAL; |
| 5930 | goto abort; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5931 | } |
| 5932 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5933 | if (pers->sync_request) { |
| 5934 | /* Warn if this is a potentially silly |
| 5935 | * configuration. |
| 5936 | */ |
| 5937 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5938 | struct md_rdev *rdev2; |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5939 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5940 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5941 | rdev_for_each(rdev, mddev) |
| 5942 | rdev_for_each(rdev2, mddev) { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5943 | if (rdev < rdev2 && |
Christoph Hellwig | 61a27e1f | 2020-09-03 07:40:58 +0200 | [diff] [blame] | 5944 | rdev->bdev->bd_disk == |
| 5945 | rdev2->bdev->bd_disk) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5946 | pr_warn("%s: WARNING: %s appears to be on the same physical disk as %s.\n", |
| 5947 | mdname(mddev), |
| 5948 | bdevname(rdev->bdev,b), |
| 5949 | bdevname(rdev2->bdev,b2)); |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5950 | warned = 1; |
| 5951 | } |
| 5952 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5953 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5954 | if (warned) |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5955 | pr_warn("True protection against single-disk failure might be compromised.\n"); |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5956 | } |
| 5957 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 5958 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5959 | /* may be over-ridden by personality */ |
| 5960 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 5961 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5962 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5963 | |
NeilBrown | 0f9552b5 | 2009-12-30 12:08:50 +1100 | [diff] [blame] | 5964 | if (start_readonly && mddev->ro == 0) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5965 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 5966 | |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5967 | err = pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 5968 | if (err) |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5969 | pr_warn("md: pers->run() failed ...\n"); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5970 | else if (pers->size(mddev, 0, 0) < mddev->array_sectors) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5971 | WARN_ONCE(!mddev->external_size, |
| 5972 | "%s: default size too small, but 'external_size' not in effect?\n", |
| 5973 | __func__); |
| 5974 | pr_warn("md: invalid array_size %llu > default size %llu\n", |
| 5975 | (unsigned long long)mddev->array_sectors / 2, |
| 5976 | (unsigned long long)pers->size(mddev, 0, 0) / 2); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5977 | err = -EINVAL; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5978 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5979 | if (err == 0 && pers->sync_request && |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 5980 | (mddev->bitmap_info.file || mddev->bitmap_info.offset)) { |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 5981 | struct bitmap *bitmap; |
| 5982 | |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 5983 | bitmap = md_bitmap_create(mddev, -1); |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 5984 | if (IS_ERR(bitmap)) { |
| 5985 | err = PTR_ERR(bitmap); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5986 | pr_warn("%s: failed to create bitmap (%d)\n", |
| 5987 | mdname(mddev), err); |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 5988 | } else |
| 5989 | mddev->bitmap = bitmap; |
| 5990 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 5991 | } |
Guoqing Jiang | d494549 | 2019-06-14 17:10:39 +0800 | [diff] [blame] | 5992 | if (err) |
| 5993 | goto bitmap_abort; |
Guoqing Jiang | 3e148a3 | 2019-06-19 17:30:46 +0800 | [diff] [blame] | 5994 | |
| 5995 | if (mddev->bitmap_info.max_write_behind > 0) { |
Guoqing Jiang | 3e173ab | 2019-12-23 10:48:54 +0100 | [diff] [blame] | 5996 | bool create_pool = false; |
Guoqing Jiang | 3e148a3 | 2019-06-19 17:30:46 +0800 | [diff] [blame] | 5997 | |
| 5998 | rdev_for_each(rdev, mddev) { |
| 5999 | if (test_bit(WriteMostly, &rdev->flags) && |
Guoqing Jiang | 404659c | 2019-12-23 10:48:53 +0100 | [diff] [blame] | 6000 | rdev_init_serial(rdev)) |
Guoqing Jiang | 3e173ab | 2019-12-23 10:48:54 +0100 | [diff] [blame] | 6001 | create_pool = true; |
Guoqing Jiang | 3e148a3 | 2019-06-19 17:30:46 +0800 | [diff] [blame] | 6002 | } |
Guoqing Jiang | 3e173ab | 2019-12-23 10:48:54 +0100 | [diff] [blame] | 6003 | if (create_pool && mddev->serial_info_pool == NULL) { |
Guoqing Jiang | 404659c | 2019-12-23 10:48:53 +0100 | [diff] [blame] | 6004 | mddev->serial_info_pool = |
| 6005 | mempool_create_kmalloc_pool(NR_SERIAL_INFOS, |
| 6006 | sizeof(struct serial_info)); |
| 6007 | if (!mddev->serial_info_pool) { |
Guoqing Jiang | 3e148a3 | 2019-06-19 17:30:46 +0800 | [diff] [blame] | 6008 | err = -ENOMEM; |
Guoqing Jiang | d494549 | 2019-06-14 17:10:39 +0800 | [diff] [blame] | 6009 | goto bitmap_abort; |
Guoqing Jiang | 3e148a3 | 2019-06-19 17:30:46 +0800 | [diff] [blame] | 6010 | } |
| 6011 | } |
| 6012 | } |
| 6013 | |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 6014 | if (mddev->queue) { |
Shaohua Li | bb086a8 | 2016-09-30 09:45:40 -0700 | [diff] [blame] | 6015 | bool nonrot = true; |
| 6016 | |
| 6017 | rdev_for_each(rdev, mddev) { |
| 6018 | if (rdev->raid_disk >= 0 && |
| 6019 | !blk_queue_nonrot(bdev_get_queue(rdev->bdev))) { |
| 6020 | nonrot = false; |
| 6021 | break; |
| 6022 | } |
| 6023 | } |
| 6024 | if (mddev->degraded) |
| 6025 | nonrot = false; |
| 6026 | if (nonrot) |
Bart Van Assche | 8b904b5 | 2018-03-07 17:10:10 -0800 | [diff] [blame] | 6027 | blk_queue_flag_set(QUEUE_FLAG_NONROT, mddev->queue); |
Shaohua Li | bb086a8 | 2016-09-30 09:45:40 -0700 | [diff] [blame] | 6028 | else |
Bart Van Assche | 8b904b5 | 2018-03-07 17:10:10 -0800 | [diff] [blame] | 6029 | blk_queue_flag_clear(QUEUE_FLAG_NONROT, mddev->queue); |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 6030 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 6031 | if (pers->sync_request) { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6032 | if (mddev->kobj.sd && |
| 6033 | sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6034 | pr_warn("md: cannot register extra attributes for %s\n", |
| 6035 | mdname(mddev)); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6036 | mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action"); |
Junxiao Bi | e8efa9b | 2020-08-04 17:27:18 -0700 | [diff] [blame] | 6037 | mddev->sysfs_completed = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_completed"); |
| 6038 | mddev->sysfs_degraded = sysfs_get_dirent_safe(mddev->kobj.sd, "degraded"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 6039 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 6040 | mddev->ro = 0; |
| 6041 | |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 6042 | atomic_set(&mddev->max_corr_read_errors, |
| 6043 | MD_DEFAULT_MAX_CORRECTED_READ_ERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6044 | mddev->safemode = 0; |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 6045 | if (mddev_is_clustered(mddev)) |
| 6046 | mddev->safemode_delay = 0; |
| 6047 | else |
Zhao Heming | 7c9d5c5 | 2020-07-21 02:08:52 +0800 | [diff] [blame] | 6048 | mddev->safemode_delay = DEFAULT_SAFEMODE_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6049 | mddev->in_sync = 1; |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 6050 | smp_wmb(); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 6051 | spin_lock(&mddev->lock); |
| 6052 | mddev->pers = pers; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 6053 | spin_unlock(&mddev->lock); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 6054 | rdev_for_each(rdev, mddev) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 6055 | if (rdev->raid_disk >= 0) |
Yufen Yu | e5b521e | 2019-06-14 15:41:07 -0700 | [diff] [blame] | 6056 | sysfs_link_rdev(mddev, rdev); /* failure here is OK */ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6057 | |
NeilBrown | a4a3d26 | 2015-07-17 11:57:30 +1000 | [diff] [blame] | 6058 | if (mddev->degraded && !mddev->ro) |
| 6059 | /* This ensures that recovering status is reported immediately |
| 6060 | * via sysfs - until a lack of spares is confirmed. |
| 6061 | */ |
| 6062 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6063 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6064 | |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 6065 | if (mddev->sb_flags) |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6066 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6067 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6068 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6069 | return 0; |
Xiao Ni | b126194 | 2018-01-24 12:17:38 +0800 | [diff] [blame] | 6070 | |
Guoqing Jiang | d494549 | 2019-06-14 17:10:39 +0800 | [diff] [blame] | 6071 | bitmap_abort: |
| 6072 | mddev_detach(mddev); |
| 6073 | if (mddev->private) |
| 6074 | pers->free(mddev, mddev->private); |
| 6075 | mddev->private = NULL; |
| 6076 | module_put(pers->owner); |
| 6077 | md_bitmap_destroy(mddev); |
Xiao Ni | b126194 | 2018-01-24 12:17:38 +0800 | [diff] [blame] | 6078 | abort: |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 6079 | bioset_exit(&mddev->bio_set); |
| 6080 | bioset_exit(&mddev->sync_set); |
Xiao Ni | b126194 | 2018-01-24 12:17:38 +0800 | [diff] [blame] | 6081 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6082 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 6083 | EXPORT_SYMBOL_GPL(md_run); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6084 | |
Christoph Hellwig | 7e0adbf | 2020-06-07 17:31:19 +0200 | [diff] [blame] | 6085 | int do_md_run(struct mddev *mddev) |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 6086 | { |
| 6087 | int err; |
| 6088 | |
NeilBrown | 9d4b45d | 2019-08-20 10:21:09 +1000 | [diff] [blame] | 6089 | set_bit(MD_NOT_READY, &mddev->flags); |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 6090 | err = md_run(mddev); |
| 6091 | if (err) |
| 6092 | goto out; |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 6093 | err = md_bitmap_load(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6094 | if (err) { |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 6095 | md_bitmap_destroy(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6096 | goto out; |
| 6097 | } |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 6098 | |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 6099 | if (mddev_is_clustered(mddev)) |
| 6100 | md_allow_write(mddev); |
| 6101 | |
Song Liu | d5d885f | 2017-11-19 22:17:01 -0800 | [diff] [blame] | 6102 | /* run start up tasks that require md_thread */ |
| 6103 | md_start(mddev); |
| 6104 | |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 6105 | md_wakeup_thread(mddev->thread); |
| 6106 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
| 6107 | |
Christoph Hellwig | 2c247c5 | 2020-11-16 15:57:11 +0100 | [diff] [blame] | 6108 | set_capacity_and_notify(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 9d4b45d | 2019-08-20 10:21:09 +1000 | [diff] [blame] | 6109 | clear_bit(MD_NOT_READY, &mddev->flags); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6110 | mddev->changed = 1; |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 6111 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
NeilBrown | 9d4b45d | 2019-08-20 10:21:09 +1000 | [diff] [blame] | 6112 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
| 6113 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 6114 | sysfs_notify_dirent_safe(mddev->sysfs_degraded); |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 6115 | out: |
NeilBrown | 9d4b45d | 2019-08-20 10:21:09 +1000 | [diff] [blame] | 6116 | clear_bit(MD_NOT_READY, &mddev->flags); |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 6117 | return err; |
| 6118 | } |
| 6119 | |
Song Liu | d5d885f | 2017-11-19 22:17:01 -0800 | [diff] [blame] | 6120 | int md_start(struct mddev *mddev) |
| 6121 | { |
| 6122 | int ret = 0; |
| 6123 | |
| 6124 | if (mddev->pers->start) { |
| 6125 | set_bit(MD_RECOVERY_WAIT, &mddev->recovery); |
| 6126 | md_wakeup_thread(mddev->thread); |
| 6127 | ret = mddev->pers->start(mddev); |
| 6128 | clear_bit(MD_RECOVERY_WAIT, &mddev->recovery); |
| 6129 | md_wakeup_thread(mddev->sync_thread); |
| 6130 | } |
| 6131 | return ret; |
| 6132 | } |
| 6133 | EXPORT_SYMBOL_GPL(md_start); |
| 6134 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6135 | static int restart_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6136 | { |
| 6137 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | 97b20ef | 2017-04-13 08:53:48 +1000 | [diff] [blame] | 6138 | struct md_rdev *rdev; |
| 6139 | bool has_journal = false; |
| 6140 | bool has_readonly = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6141 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 6142 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6143 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 6144 | return -ENXIO; |
| 6145 | if (!mddev->pers) |
| 6146 | return -EINVAL; |
| 6147 | if (!mddev->ro) |
| 6148 | return -EBUSY; |
Song Liu | 339421d | 2015-10-08 21:54:13 -0700 | [diff] [blame] | 6149 | |
NeilBrown | 97b20ef | 2017-04-13 08:53:48 +1000 | [diff] [blame] | 6150 | rcu_read_lock(); |
| 6151 | rdev_for_each_rcu(rdev, mddev) { |
| 6152 | if (test_bit(Journal, &rdev->flags) && |
| 6153 | !test_bit(Faulty, &rdev->flags)) |
| 6154 | has_journal = true; |
| 6155 | if (bdev_read_only(rdev->bdev)) |
| 6156 | has_readonly = true; |
Song Liu | 339421d | 2015-10-08 21:54:13 -0700 | [diff] [blame] | 6157 | } |
NeilBrown | 97b20ef | 2017-04-13 08:53:48 +1000 | [diff] [blame] | 6158 | rcu_read_unlock(); |
| 6159 | if (test_bit(MD_HAS_JOURNAL, &mddev->flags) && !has_journal) |
| 6160 | /* Don't restart rw with journal missing/faulty */ |
| 6161 | return -EINVAL; |
| 6162 | if (has_readonly) |
| 6163 | return -EROFS; |
Song Liu | 339421d | 2015-10-08 21:54:13 -0700 | [diff] [blame] | 6164 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 6165 | mddev->safemode = 0; |
| 6166 | mddev->ro = 0; |
| 6167 | set_disk_ro(disk, 0); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6168 | pr_debug("md: %s switched to read-write mode.\n", mdname(mddev)); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 6169 | /* Kick recovery or resync if necessary */ |
| 6170 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6171 | md_wakeup_thread(mddev->thread); |
| 6172 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6173 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 6174 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6175 | } |
| 6176 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6177 | static void md_clean(struct mddev *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6178 | { |
| 6179 | mddev->array_sectors = 0; |
| 6180 | mddev->external_size = 0; |
| 6181 | mddev->dev_sectors = 0; |
| 6182 | mddev->raid_disks = 0; |
| 6183 | mddev->recovery_cp = 0; |
| 6184 | mddev->resync_min = 0; |
| 6185 | mddev->resync_max = MaxSector; |
| 6186 | mddev->reshape_position = MaxSector; |
| 6187 | mddev->external = 0; |
| 6188 | mddev->persistent = 0; |
| 6189 | mddev->level = LEVEL_NONE; |
| 6190 | mddev->clevel[0] = 0; |
| 6191 | mddev->flags = 0; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 6192 | mddev->sb_flags = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6193 | mddev->ro = 0; |
| 6194 | mddev->metadata_type[0] = 0; |
| 6195 | mddev->chunk_sectors = 0; |
| 6196 | mddev->ctime = mddev->utime = 0; |
| 6197 | mddev->layout = 0; |
| 6198 | mddev->max_disks = 0; |
| 6199 | mddev->events = 0; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 6200 | mddev->can_decrease_events = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6201 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6202 | mddev->reshape_backwards = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6203 | mddev->new_level = LEVEL_NONE; |
| 6204 | mddev->new_layout = 0; |
| 6205 | mddev->new_chunk_sectors = 0; |
| 6206 | mddev->curr_resync = 0; |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 6207 | atomic64_set(&mddev->resync_mismatches, 0); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6208 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 6209 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 6210 | mddev->recovery = 0; |
| 6211 | mddev->in_sync = 0; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6212 | mddev->changed = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6213 | mddev->degraded = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6214 | mddev->safemode = 0; |
NeilBrown | bd69192 | 2015-06-25 17:01:40 +1000 | [diff] [blame] | 6215 | mddev->private = NULL; |
Guoqing Jiang | c20c33f | 2016-08-12 13:42:38 +0800 | [diff] [blame] | 6216 | mddev->cluster_info = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6217 | mddev->bitmap_info.offset = 0; |
| 6218 | mddev->bitmap_info.default_offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 6219 | mddev->bitmap_info.default_space = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6220 | mddev->bitmap_info.chunksize = 0; |
| 6221 | mddev->bitmap_info.daemon_sleep = 0; |
| 6222 | mddev->bitmap_info.max_write_behind = 0; |
Guoqing Jiang | c20c33f | 2016-08-12 13:42:38 +0800 | [diff] [blame] | 6223 | mddev->bitmap_info.nodes = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6224 | } |
| 6225 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6226 | static void __md_stop_writes(struct mddev *mddev) |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 6227 | { |
NeilBrown | 6b6204e | 2013-05-09 09:48:30 +1000 | [diff] [blame] | 6228 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Guoqing Jiang | 21e0958 | 2020-04-04 23:57:07 +0200 | [diff] [blame] | 6229 | if (work_pending(&mddev->del_work)) |
| 6230 | flush_workqueue(md_misc_wq); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 6231 | if (mddev->sync_thread) { |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 6232 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 6233 | md_reap_sync_thread(mddev); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 6234 | } |
| 6235 | |
| 6236 | del_timer_sync(&mddev->safemode_timer); |
| 6237 | |
Shaohua Li | 034e33f | 2016-11-21 10:29:19 -0800 | [diff] [blame] | 6238 | if (mddev->pers && mddev->pers->quiesce) { |
| 6239 | mddev->pers->quiesce(mddev, 1); |
| 6240 | mddev->pers->quiesce(mddev, 0); |
| 6241 | } |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 6242 | md_bitmap_flush(mddev); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 6243 | |
NeilBrown | b6d428c | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 6244 | if (mddev->ro == 0 && |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 6245 | ((!mddev->in_sync && !mddev_is_clustered(mddev)) || |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 6246 | mddev->sb_flags)) { |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 6247 | /* mark array as shutdown cleanly */ |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 6248 | if (!mddev_is_clustered(mddev)) |
| 6249 | mddev->in_sync = 1; |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 6250 | md_update_sb(mddev, 1); |
| 6251 | } |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 6252 | /* disable policy to guarantee rdevs free resources for serialization */ |
| 6253 | mddev->serialize_policy = 0; |
| 6254 | mddev_destroy_serial_pool(mddev, NULL, true); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 6255 | } |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 6256 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6257 | void md_stop_writes(struct mddev *mddev) |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 6258 | { |
NeilBrown | 29f097c | 2013-11-14 17:54:51 +1100 | [diff] [blame] | 6259 | mddev_lock_nointr(mddev); |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 6260 | __md_stop_writes(mddev); |
| 6261 | mddev_unlock(mddev); |
| 6262 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 6263 | EXPORT_SYMBOL_GPL(md_stop_writes); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 6264 | |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 6265 | static void mddev_detach(struct mddev *mddev) |
| 6266 | { |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 6267 | md_bitmap_wait_behind_writes(mddev); |
Guoqing Jiang | 6b40bec | 2020-02-11 11:10:04 +0100 | [diff] [blame] | 6268 | if (mddev->pers && mddev->pers->quiesce && !mddev->suspended) { |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 6269 | mddev->pers->quiesce(mddev, 1); |
| 6270 | mddev->pers->quiesce(mddev, 0); |
| 6271 | } |
| 6272 | md_unregister_thread(&mddev->thread); |
| 6273 | if (mddev->queue) |
| 6274 | blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/ |
| 6275 | } |
| 6276 | |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 6277 | static void __md_stop(struct mddev *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6278 | { |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 6279 | struct md_personality *pers = mddev->pers; |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 6280 | md_bitmap_destroy(mddev); |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 6281 | mddev_detach(mddev); |
NeilBrown | ee5d004 | 2015-07-22 10:20:07 +1000 | [diff] [blame] | 6282 | /* Ensure ->event_work is done */ |
Guoqing Jiang | 21e0958 | 2020-04-04 23:57:07 +0200 | [diff] [blame] | 6283 | if (mddev->event_work.func) |
| 6284 | flush_workqueue(md_misc_wq); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 6285 | spin_lock(&mddev->lock); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6286 | mddev->pers = NULL; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 6287 | spin_unlock(&mddev->lock); |
| 6288 | pers->free(mddev, mddev->private); |
NeilBrown | bd69192 | 2015-06-25 17:01:40 +1000 | [diff] [blame] | 6289 | mddev->private = NULL; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 6290 | if (pers->sync_request && mddev->to_remove == NULL) |
| 6291 | mddev->to_remove = &md_redundancy_group; |
| 6292 | module_put(pers->owner); |
NeilBrown | cca9cf9 | 2010-04-01 12:08:16 +1100 | [diff] [blame] | 6293 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Jack Wang | 6aaa58c | 2018-10-19 16:21:31 +0200 | [diff] [blame] | 6294 | } |
| 6295 | |
| 6296 | void md_stop(struct mddev *mddev) |
| 6297 | { |
| 6298 | /* stop the array and free an attached data structures. |
| 6299 | * This is called from dm-raid |
| 6300 | */ |
| 6301 | __md_stop(mddev); |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 6302 | bioset_exit(&mddev->bio_set); |
| 6303 | bioset_exit(&mddev->sync_set); |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 6304 | } |
| 6305 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 6306 | EXPORT_SYMBOL_GPL(md_stop); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6307 | |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 6308 | static int md_set_readonly(struct mddev *mddev, struct block_device *bdev) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 6309 | { |
| 6310 | int err = 0; |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6311 | int did_freeze = 0; |
| 6312 | |
| 6313 | if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) { |
| 6314 | did_freeze = 1; |
| 6315 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 6316 | md_wakeup_thread(mddev->thread); |
| 6317 | } |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6318 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6319 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6320 | if (mddev->sync_thread) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6321 | /* Thread might be blocked waiting for metadata update |
| 6322 | * which will now never happen */ |
| 6323 | wake_up_process(mddev->sync_thread->tsk); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6324 | |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 6325 | if (mddev->external && test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) |
NeilBrown | 88724bf | 2015-09-24 14:00:51 +1000 | [diff] [blame] | 6326 | return -EBUSY; |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6327 | mddev_unlock(mddev); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6328 | wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING, |
| 6329 | &mddev->recovery)); |
NeilBrown | 88724bf | 2015-09-24 14:00:51 +1000 | [diff] [blame] | 6330 | wait_event(mddev->sb_wait, |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 6331 | !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6332 | mddev_lock_nointr(mddev); |
| 6333 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 6334 | mutex_lock(&mddev->open_mutex); |
NeilBrown | 9ba3b7f | 2014-09-09 14:00:15 +1000 | [diff] [blame] | 6335 | if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) || |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6336 | mddev->sync_thread || |
Guoqing Jiang | af8d8e6 | 2016-08-12 13:42:37 +0800 | [diff] [blame] | 6337 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6338 | pr_warn("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6339 | if (did_freeze) { |
| 6340 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 45eaf45 | 2014-10-29 08:49:50 +1100 | [diff] [blame] | 6341 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6342 | md_wakeup_thread(mddev->thread); |
| 6343 | } |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 6344 | err = -EBUSY; |
| 6345 | goto out; |
| 6346 | } |
| 6347 | if (mddev->pers) { |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 6348 | __md_stop_writes(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 6349 | |
| 6350 | err = -ENXIO; |
| 6351 | if (mddev->ro==1) |
| 6352 | goto out; |
| 6353 | mddev->ro = 1; |
| 6354 | set_disk_ro(mddev->gendisk, 1); |
| 6355 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 45eaf45 | 2014-10-29 08:49:50 +1100 | [diff] [blame] | 6356 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6357 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6358 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6359 | err = 0; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 6360 | } |
| 6361 | out: |
| 6362 | mutex_unlock(&mddev->open_mutex); |
| 6363 | return err; |
| 6364 | } |
| 6365 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 6366 | /* mode: |
| 6367 | * 0 - completely stop and dis-assemble array |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 6368 | * 2 - stop but do not disassemble array |
| 6369 | */ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6370 | static int do_md_stop(struct mddev *mddev, int mode, |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 6371 | struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6372 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6373 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6374 | struct md_rdev *rdev; |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6375 | int did_freeze = 0; |
| 6376 | |
| 6377 | if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) { |
| 6378 | did_freeze = 1; |
| 6379 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 6380 | md_wakeup_thread(mddev->thread); |
| 6381 | } |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6382 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6383 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6384 | if (mddev->sync_thread) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6385 | /* Thread might be blocked waiting for metadata update |
| 6386 | * which will now never happen */ |
| 6387 | wake_up_process(mddev->sync_thread->tsk); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6388 | |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6389 | mddev_unlock(mddev); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6390 | wait_event(resync_wait, (mddev->sync_thread == NULL && |
| 6391 | !test_bit(MD_RECOVERY_RUNNING, |
| 6392 | &mddev->recovery))); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6393 | mddev_lock_nointr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6394 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 6395 | mutex_lock(&mddev->open_mutex); |
NeilBrown | 9ba3b7f | 2014-09-09 14:00:15 +1000 | [diff] [blame] | 6396 | if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) || |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6397 | mddev->sysfs_active || |
| 6398 | mddev->sync_thread || |
Guoqing Jiang | af8d8e6 | 2016-08-12 13:42:37 +0800 | [diff] [blame] | 6399 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6400 | pr_warn("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 6401 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6402 | if (did_freeze) { |
| 6403 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 45eaf45 | 2014-10-29 08:49:50 +1100 | [diff] [blame] | 6404 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6405 | md_wakeup_thread(mddev->thread); |
| 6406 | } |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 6407 | return -EBUSY; |
| 6408 | } |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 6409 | if (mddev->pers) { |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 6410 | if (mddev->ro) |
| 6411 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6412 | |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 6413 | __md_stop_writes(mddev); |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 6414 | __md_stop(mddev); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6415 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 6416 | /* tell userspace to handle 'inactive' */ |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6417 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 6418 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 6419 | rdev_for_each(rdev, mddev) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 6420 | if (rdev->raid_disk >= 0) |
| 6421 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 6422 | |
Christoph Hellwig | 2c247c5 | 2020-11-16 15:57:11 +0100 | [diff] [blame] | 6423 | set_capacity_and_notify(disk, 0); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 6424 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6425 | mddev->changed = 1; |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 6426 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 6427 | if (mddev->ro) |
| 6428 | mddev->ro = 0; |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 6429 | } else |
| 6430 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6431 | /* |
| 6432 | * Free resources if final stop |
| 6433 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 6434 | if (mode == 0) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6435 | pr_info("md: %s stopped.\n", mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6436 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6437 | if (mddev->bitmap_info.file) { |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6438 | struct file *f = mddev->bitmap_info.file; |
| 6439 | spin_lock(&mddev->lock); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6440 | mddev->bitmap_info.file = NULL; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6441 | spin_unlock(&mddev->lock); |
| 6442 | fput(f); |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 6443 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6444 | mddev->bitmap_info.offset = 0; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 6445 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6446 | export_array(mddev); |
| 6447 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6448 | md_clean(mddev); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6449 | if (mddev->hold_active == UNTIL_STOP) |
| 6450 | mddev->hold_active = 0; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 6451 | } |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6452 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6453 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 6454 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6455 | } |
| 6456 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 6457 | #ifndef MODULE |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6458 | static void autorun_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6459 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6460 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6461 | int err; |
| 6462 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 6463 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6464 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6465 | |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6466 | pr_info("md: running: "); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6467 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 6468 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6469 | char b[BDEVNAME_SIZE]; |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6470 | pr_cont("<%s>", bdevname(rdev->bdev,b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6471 | } |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6472 | pr_cont("\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6473 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6474 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6475 | if (err) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6476 | pr_warn("md: do_md_run() returned %d\n", err); |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 6477 | do_md_stop(mddev, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6478 | } |
| 6479 | } |
| 6480 | |
| 6481 | /* |
| 6482 | * lets try to run arrays based on all disks that have arrived |
| 6483 | * until now. (those are in pending_raid_disks) |
| 6484 | * |
| 6485 | * the method: pick the first pending disk, collect all disks with |
| 6486 | * the same UUID, remove all from the pending list and put them into |
| 6487 | * the 'same_array' list. Then order this list based on superblock |
| 6488 | * update time (freshest comes first), kick out 'old' disks and |
| 6489 | * compare superblocks. If everything's fine then run it. |
| 6490 | * |
| 6491 | * If "unit" is allocated, then bump its reference count |
| 6492 | */ |
| 6493 | static void autorun_devices(int part) |
| 6494 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6495 | struct md_rdev *rdev0, *rdev, *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6496 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6497 | char b[BDEVNAME_SIZE]; |
| 6498 | |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6499 | pr_info("md: autorun ...\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6500 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 6501 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6502 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 6503 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6504 | rdev0 = list_entry(pending_raid_disks.next, |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6505 | struct md_rdev, same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6506 | |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6507 | pr_debug("md: considering %s ...\n", bdevname(rdev0->bdev,b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6508 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6509 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6510 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6511 | pr_debug("md: adding %s ...\n", |
| 6512 | bdevname(rdev->bdev,b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6513 | list_move(&rdev->same_set, &candidates); |
| 6514 | } |
| 6515 | /* |
| 6516 | * now we have a set of devices, with all of them having |
| 6517 | * mostly sane superblocks. It's time to allocate the |
| 6518 | * mddev. |
| 6519 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 6520 | if (part) { |
| 6521 | dev = MKDEV(mdp_major, |
| 6522 | rdev0->preferred_minor << MdpMinorShift); |
| 6523 | unit = MINOR(dev) >> MdpMinorShift; |
| 6524 | } else { |
| 6525 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 6526 | unit = MINOR(dev); |
| 6527 | } |
| 6528 | if (rdev0->preferred_minor != unit) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6529 | pr_warn("md: unit number in %s is bad: %d\n", |
| 6530 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6531 | break; |
| 6532 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6533 | |
Christoph Hellwig | 28144f9 | 2020-10-29 15:58:34 +0100 | [diff] [blame] | 6534 | md_probe(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6535 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 6536 | if (!mddev || !mddev->gendisk) { |
| 6537 | if (mddev) |
| 6538 | mddev_put(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6539 | break; |
| 6540 | } |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6541 | if (mddev_lock(mddev)) |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6542 | pr_warn("md: %s locked, cannot run\n", mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6543 | else if (mddev->raid_disks || mddev->major_version |
| 6544 | || !list_empty(&mddev->disks)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6545 | pr_warn("md: %s already running, cannot run %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6546 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 6547 | mddev_unlock(mddev); |
| 6548 | } else { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6549 | pr_debug("md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 6550 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6551 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6552 | list_del_init(&rdev->same_set); |
| 6553 | if (bind_rdev_to_array(rdev, mddev)) |
| 6554 | export_rdev(rdev); |
| 6555 | } |
| 6556 | autorun_array(mddev); |
| 6557 | mddev_unlock(mddev); |
| 6558 | } |
| 6559 | /* on success, candidates will be empty, on error |
| 6560 | * it won't... |
| 6561 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6562 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6563 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6564 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6565 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6566 | mddev_put(mddev); |
| 6567 | } |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6568 | pr_info("md: ... autorun DONE.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6569 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 6570 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6571 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6572 | static int get_version(void __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6573 | { |
| 6574 | mdu_version_t ver; |
| 6575 | |
| 6576 | ver.major = MD_MAJOR_VERSION; |
| 6577 | ver.minor = MD_MINOR_VERSION; |
| 6578 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 6579 | |
| 6580 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 6581 | return -EFAULT; |
| 6582 | |
| 6583 | return 0; |
| 6584 | } |
| 6585 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6586 | static int get_array_info(struct mddev *mddev, void __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6587 | { |
| 6588 | mdu_array_info_t info; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 6589 | int nr,working,insync,failed,spare; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6590 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6591 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6592 | nr = working = insync = failed = spare = 0; |
| 6593 | rcu_read_lock(); |
| 6594 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6595 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6596 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6597 | failed++; |
| 6598 | else { |
| 6599 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6600 | if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6601 | insync++; |
Song Liu | b347af8 | 2016-08-11 17:14:45 -0700 | [diff] [blame] | 6602 | else if (test_bit(Journal, &rdev->flags)) |
| 6603 | /* TODO: add journal count to md_u.h */ |
| 6604 | ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6605 | else |
| 6606 | spare++; |
| 6607 | } |
| 6608 | } |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6609 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6610 | |
| 6611 | info.major_version = mddev->major_version; |
| 6612 | info.minor_version = mddev->minor_version; |
| 6613 | info.patch_version = MD_PATCHLEVEL_VERSION; |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 6614 | info.ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6615 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6616 | info.size = mddev->dev_sectors / 2; |
| 6617 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 6618 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6619 | info.nr_disks = nr; |
| 6620 | info.raid_disks = mddev->raid_disks; |
| 6621 | info.md_minor = mddev->md_minor; |
| 6622 | info.not_persistent= !mddev->persistent; |
| 6623 | |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 6624 | info.utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6625 | info.state = 0; |
| 6626 | if (mddev->in_sync) |
| 6627 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6628 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 9bd3592 | 2014-07-02 11:35:06 +1000 | [diff] [blame] | 6629 | info.state |= (1<<MD_SB_BITMAP_PRESENT); |
Goldwyn Rodrigues | ca8895d | 2014-11-26 12:22:03 -0600 | [diff] [blame] | 6630 | if (mddev_is_clustered(mddev)) |
| 6631 | info.state |= (1<<MD_SB_CLUSTERED); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 6632 | info.active_disks = insync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6633 | info.working_disks = working; |
| 6634 | info.failed_disks = failed; |
| 6635 | info.spare_disks = spare; |
| 6636 | |
| 6637 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 6638 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6639 | |
| 6640 | if (copy_to_user(arg, &info, sizeof(info))) |
| 6641 | return -EFAULT; |
| 6642 | |
| 6643 | return 0; |
| 6644 | } |
| 6645 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6646 | static int get_bitmap_file(struct mddev *mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6647 | { |
| 6648 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
NeilBrown | f4ad3d3 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6649 | char *ptr; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6650 | int err; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6651 | |
Benjamin Randazzo | b6878d9 | 2015-07-25 16:36:50 +0200 | [diff] [blame] | 6652 | file = kzalloc(sizeof(*file), GFP_NOIO); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6653 | if (!file) |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6654 | return -ENOMEM; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6655 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6656 | err = 0; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6657 | spin_lock(&mddev->lock); |
Benjamin Randazzo | 25eafe1 | 2015-07-25 16:36:50 +0200 | [diff] [blame] | 6658 | /* bitmap enabled */ |
| 6659 | if (mddev->bitmap_info.file) { |
| 6660 | ptr = file_path(mddev->bitmap_info.file, file->pathname, |
| 6661 | sizeof(file->pathname)); |
| 6662 | if (IS_ERR(ptr)) |
| 6663 | err = PTR_ERR(ptr); |
| 6664 | else |
| 6665 | memmove(file->pathname, ptr, |
| 6666 | sizeof(file->pathname)-(ptr-file->pathname)); |
| 6667 | } |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6668 | spin_unlock(&mddev->lock); |
| 6669 | |
| 6670 | if (err == 0 && |
| 6671 | copy_to_user(arg, file, sizeof(*file))) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6672 | err = -EFAULT; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6673 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6674 | kfree(file); |
| 6675 | return err; |
| 6676 | } |
| 6677 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6678 | static int get_disk_info(struct mddev *mddev, void __user * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6679 | { |
| 6680 | mdu_disk_info_t info; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6681 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6682 | |
| 6683 | if (copy_from_user(&info, arg, sizeof(info))) |
| 6684 | return -EFAULT; |
| 6685 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6686 | rcu_read_lock(); |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 6687 | rdev = md_find_rdev_nr_rcu(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6688 | if (rdev) { |
| 6689 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 6690 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 6691 | info.raid_disk = rdev->raid_disk; |
| 6692 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6693 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6694 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6695 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6696 | info.state |= (1<<MD_DISK_ACTIVE); |
| 6697 | info.state |= (1<<MD_DISK_SYNC); |
| 6698 | } |
Shaohua Li | 9efdca1 | 2015-10-12 16:59:50 -0700 | [diff] [blame] | 6699 | if (test_bit(Journal, &rdev->flags)) |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 6700 | info.state |= (1<<MD_DISK_JOURNAL); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6701 | if (test_bit(WriteMostly, &rdev->flags)) |
| 6702 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 6703 | if (test_bit(FailFast, &rdev->flags)) |
| 6704 | info.state |= (1<<MD_DISK_FAILFAST); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6705 | } else { |
| 6706 | info.major = info.minor = 0; |
| 6707 | info.raid_disk = -1; |
| 6708 | info.state = (1<<MD_DISK_REMOVED); |
| 6709 | } |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6710 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6711 | |
| 6712 | if (copy_to_user(arg, &info, sizeof(info))) |
| 6713 | return -EFAULT; |
| 6714 | |
| 6715 | return 0; |
| 6716 | } |
| 6717 | |
Christoph Hellwig | 7e0adbf | 2020-06-07 17:31:19 +0200 | [diff] [blame] | 6718 | 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] | 6719 | { |
| 6720 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6721 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6722 | dev_t dev = MKDEV(info->major,info->minor); |
| 6723 | |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6724 | if (mddev_is_clustered(mddev) && |
| 6725 | !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6726 | pr_warn("%s: Cannot add to clustered mddev.\n", |
| 6727 | mdname(mddev)); |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6728 | return -EINVAL; |
| 6729 | } |
| 6730 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6731 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 6732 | return -EOVERFLOW; |
| 6733 | |
| 6734 | if (!mddev->raid_disks) { |
| 6735 | int err; |
| 6736 | /* expecting a device which has a superblock */ |
| 6737 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 6738 | if (IS_ERR(rdev)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6739 | pr_warn("md: md_import_device returned %ld\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6740 | PTR_ERR(rdev)); |
| 6741 | return PTR_ERR(rdev); |
| 6742 | } |
| 6743 | if (!list_empty(&mddev->disks)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6744 | struct md_rdev *rdev0 |
| 6745 | = list_entry(mddev->disks.next, |
| 6746 | struct md_rdev, same_set); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 6747 | err = super_types[mddev->major_version] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6748 | .load_super(rdev, rdev0, mddev->minor_version); |
| 6749 | if (err < 0) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6750 | pr_warn("md: %s has different UUID to %s\n", |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6751 | bdevname(rdev->bdev,b), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6752 | bdevname(rdev0->bdev,b2)); |
| 6753 | export_rdev(rdev); |
| 6754 | return -EINVAL; |
| 6755 | } |
| 6756 | } |
| 6757 | err = bind_rdev_to_array(rdev, mddev); |
| 6758 | if (err) |
| 6759 | export_rdev(rdev); |
| 6760 | return err; |
| 6761 | } |
| 6762 | |
| 6763 | /* |
Christoph Hellwig | 7e0adbf | 2020-06-07 17:31:19 +0200 | [diff] [blame] | 6764 | * md_add_new_disk can be used once the array is assembled |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6765 | * to add "hot spares". They must already have a superblock |
| 6766 | * written |
| 6767 | */ |
| 6768 | if (mddev->pers) { |
| 6769 | int err; |
| 6770 | if (!mddev->pers->hot_add_disk) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6771 | pr_warn("%s: personality does not support diskops!\n", |
| 6772 | mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6773 | return -EINVAL; |
| 6774 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 6775 | if (mddev->persistent) |
| 6776 | rdev = md_import_device(dev, mddev->major_version, |
| 6777 | mddev->minor_version); |
| 6778 | else |
| 6779 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6780 | if (IS_ERR(rdev)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6781 | pr_warn("md: md_import_device returned %ld\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6782 | PTR_ERR(rdev)); |
| 6783 | return PTR_ERR(rdev); |
| 6784 | } |
NeilBrown | 1a855a0 | 2010-12-09 16:36:28 +1100 | [diff] [blame] | 6785 | /* set saved_raid_disk if appropriate */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6786 | if (!mddev->persistent) { |
| 6787 | if (info->state & (1<<MD_DISK_SYNC) && |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 6788 | info->raid_disk < mddev->raid_disks) { |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6789 | rdev->raid_disk = info->raid_disk; |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 6790 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 6791 | clear_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 6792 | } else |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6793 | rdev->raid_disk = -1; |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 6794 | rdev->saved_raid_disk = rdev->raid_disk; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6795 | } else |
| 6796 | super_types[mddev->major_version]. |
| 6797 | validate_super(mddev, rdev); |
NeilBrown | bedd86b | 2011-05-11 14:26:20 +1000 | [diff] [blame] | 6798 | if ((info->state & (1<<MD_DISK_SYNC)) && |
NeilBrown | f456309 | 2012-07-03 15:59:06 +1000 | [diff] [blame] | 6799 | rdev->raid_disk != info->raid_disk) { |
NeilBrown | bedd86b | 2011-05-11 14:26:20 +1000 | [diff] [blame] | 6800 | /* This was a hot-add request, but events doesn't |
| 6801 | * match, so reject it. |
| 6802 | */ |
| 6803 | export_rdev(rdev); |
| 6804 | return -EINVAL; |
| 6805 | } |
| 6806 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6807 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6808 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 6809 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6810 | else |
| 6811 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 6812 | if (info->state & (1<<MD_DISK_FAILFAST)) |
| 6813 | set_bit(FailFast, &rdev->flags); |
| 6814 | else |
| 6815 | clear_bit(FailFast, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6816 | |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 6817 | if (info->state & (1<<MD_DISK_JOURNAL)) { |
| 6818 | struct md_rdev *rdev2; |
| 6819 | bool has_journal = false; |
| 6820 | |
| 6821 | /* make sure no existing journal disk */ |
| 6822 | rdev_for_each(rdev2, mddev) { |
| 6823 | if (test_bit(Journal, &rdev2->flags)) { |
| 6824 | has_journal = true; |
| 6825 | break; |
| 6826 | } |
| 6827 | } |
NeilBrown | 230b55f | 2017-10-17 14:24:09 +1100 | [diff] [blame] | 6828 | if (has_journal || mddev->bitmap) { |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 6829 | export_rdev(rdev); |
| 6830 | return -EBUSY; |
| 6831 | } |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 6832 | set_bit(Journal, &rdev->flags); |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 6833 | } |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6834 | /* |
| 6835 | * check whether the device shows up in other nodes |
| 6836 | */ |
| 6837 | if (mddev_is_clustered(mddev)) { |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6838 | if (info->state & (1 << MD_DISK_CANDIDATE)) |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6839 | set_bit(Candidate, &rdev->flags); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6840 | else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) { |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6841 | /* --add initiated by this node */ |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6842 | err = md_cluster_ops->add_new_disk(mddev, rdev); |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6843 | if (err) { |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6844 | export_rdev(rdev); |
| 6845 | return err; |
| 6846 | } |
| 6847 | } |
| 6848 | } |
| 6849 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6850 | rdev->raid_disk = -1; |
| 6851 | err = bind_rdev_to_array(rdev, mddev); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6852 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6853 | if (err) |
| 6854 | export_rdev(rdev); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6855 | |
| 6856 | if (mddev_is_clustered(mddev)) { |
Guoqing Jiang | e566aef | 2016-08-12 13:42:34 +0800 | [diff] [blame] | 6857 | if (info->state & (1 << MD_DISK_CANDIDATE)) { |
| 6858 | if (!err) { |
| 6859 | err = md_cluster_ops->new_disk_ack(mddev, |
| 6860 | err == 0); |
| 6861 | if (err) |
| 6862 | md_kick_rdev_from_array(rdev); |
| 6863 | } |
| 6864 | } else { |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6865 | if (err) |
| 6866 | md_cluster_ops->add_new_disk_cancel(mddev); |
| 6867 | else |
| 6868 | err = add_bound_rdev(rdev); |
| 6869 | } |
| 6870 | |
| 6871 | } else if (!err) |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 6872 | err = add_bound_rdev(rdev); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6873 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6874 | return err; |
| 6875 | } |
| 6876 | |
Christoph Hellwig | 7e0adbf | 2020-06-07 17:31:19 +0200 | [diff] [blame] | 6877 | /* otherwise, md_add_new_disk is only allowed |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6878 | * for major_version==0 superblocks |
| 6879 | */ |
| 6880 | if (mddev->major_version != 0) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6881 | pr_warn("%s: ADD_NEW_DISK not supported\n", mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6882 | return -EINVAL; |
| 6883 | } |
| 6884 | |
| 6885 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 6886 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6887 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6888 | if (IS_ERR(rdev)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6889 | pr_warn("md: error, md_import_device() returned %ld\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6890 | PTR_ERR(rdev)); |
| 6891 | return PTR_ERR(rdev); |
| 6892 | } |
| 6893 | rdev->desc_nr = info->number; |
| 6894 | if (info->raid_disk < mddev->raid_disks) |
| 6895 | rdev->raid_disk = info->raid_disk; |
| 6896 | else |
| 6897 | rdev->raid_disk = -1; |
| 6898 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6899 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6900 | if (info->state & (1<<MD_DISK_SYNC)) |
| 6901 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6902 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6903 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 6904 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 6905 | if (info->state & (1<<MD_DISK_FAILFAST)) |
| 6906 | set_bit(FailFast, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6907 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6908 | if (!mddev->persistent) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6909 | pr_debug("md: nonpersistent superblock ...\n"); |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 6910 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
| 6911 | } else |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 6912 | rdev->sb_start = calc_dev_sboffset(rdev); |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 6913 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6914 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 6915 | err = bind_rdev_to_array(rdev, mddev); |
| 6916 | if (err) { |
| 6917 | export_rdev(rdev); |
| 6918 | return err; |
| 6919 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6920 | } |
| 6921 | |
| 6922 | return 0; |
| 6923 | } |
| 6924 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6925 | static int hot_remove_disk(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6926 | { |
| 6927 | char b[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6928 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6929 | |
Yufen Yu | c42a0e2 | 2018-05-04 18:08:10 +0800 | [diff] [blame] | 6930 | if (!mddev->pers) |
| 6931 | return -ENODEV; |
| 6932 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6933 | rdev = find_rdev(mddev, dev); |
| 6934 | if (!rdev) |
| 6935 | return -ENXIO; |
| 6936 | |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 6937 | if (rdev->raid_disk < 0) |
| 6938 | goto kick_rdev; |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6939 | |
NeilBrown | 3ea8929d | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 6940 | clear_bit(Blocked, &rdev->flags); |
| 6941 | remove_and_add_spares(mddev, rdev); |
| 6942 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6943 | if (rdev->raid_disk >= 0) |
| 6944 | goto busy; |
| 6945 | |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 6946 | kick_rdev: |
Goldwyn Rodrigues | 54a8839 | 2015-12-21 10:51:00 +1100 | [diff] [blame] | 6947 | if (mddev_is_clustered(mddev)) |
Goldwyn Rodrigues | 88bcfef | 2015-04-14 10:44:44 -0500 | [diff] [blame] | 6948 | md_cluster_ops->remove_disk(mddev, rdev); |
| 6949 | |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 6950 | md_kick_rdev_from_array(rdev); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 6951 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | 060b068 | 2016-11-04 16:46:03 +1100 | [diff] [blame] | 6952 | if (mddev->thread) |
| 6953 | md_wakeup_thread(mddev->thread); |
| 6954 | else |
| 6955 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6956 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6957 | |
| 6958 | return 0; |
| 6959 | busy: |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6960 | pr_debug("md: cannot remove active disk %s from %s ...\n", |
| 6961 | bdevname(rdev->bdev,b), mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6962 | return -EBUSY; |
| 6963 | } |
| 6964 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6965 | static int hot_add_disk(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6966 | { |
| 6967 | char b[BDEVNAME_SIZE]; |
| 6968 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6969 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6970 | |
| 6971 | if (!mddev->pers) |
| 6972 | return -ENODEV; |
| 6973 | |
| 6974 | if (mddev->major_version != 0) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6975 | 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] | 6976 | mdname(mddev)); |
| 6977 | return -EINVAL; |
| 6978 | } |
| 6979 | if (!mddev->pers->hot_add_disk) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6980 | pr_warn("%s: personality does not support diskops!\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6981 | mdname(mddev)); |
| 6982 | return -EINVAL; |
| 6983 | } |
| 6984 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6985 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6986 | if (IS_ERR(rdev)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6987 | pr_warn("md: error, md_import_device() returned %ld\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6988 | PTR_ERR(rdev)); |
| 6989 | return -EINVAL; |
| 6990 | } |
| 6991 | |
| 6992 | if (mddev->persistent) |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 6993 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6994 | else |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 6995 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6996 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 6997 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6998 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6999 | if (test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7000 | 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] | 7001 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 7002 | err = -EINVAL; |
| 7003 | goto abort_export; |
| 7004 | } |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 7005 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7006 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7007 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 7008 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 7009 | err = bind_rdev_to_array(rdev, mddev); |
| 7010 | if (err) |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 7011 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7012 | |
| 7013 | /* |
| 7014 | * The rest should better be atomic, we can have disk failures |
| 7015 | * noticed in interrupt contexts ... |
| 7016 | */ |
| 7017 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7018 | rdev->raid_disk = -1; |
| 7019 | |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 7020 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | 060b068 | 2016-11-04 16:46:03 +1100 | [diff] [blame] | 7021 | if (!mddev->thread) |
| 7022 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7023 | /* |
| 7024 | * Kick recovery, maybe this spare has to be added to the |
| 7025 | * array immediately. |
| 7026 | */ |
| 7027 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7028 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7029 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7030 | return 0; |
| 7031 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7032 | abort_export: |
| 7033 | export_rdev(rdev); |
| 7034 | return err; |
| 7035 | } |
| 7036 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7037 | static int set_bitmap_file(struct mddev *mddev, int fd) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7038 | { |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 7039 | int err = 0; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7040 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7041 | if (mddev->pers) { |
NeilBrown | d66b1b3 | 2014-08-08 15:40:24 +1000 | [diff] [blame] | 7042 | if (!mddev->pers->quiesce || !mddev->thread) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7043 | return -EBUSY; |
| 7044 | if (mddev->recovery || mddev->sync_thread) |
| 7045 | return -EBUSY; |
| 7046 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7047 | } |
| 7048 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7049 | if (fd >= 0) { |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 7050 | struct inode *inode; |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 7051 | struct file *f; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7052 | |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 7053 | if (mddev->bitmap || mddev->bitmap_info.file) |
| 7054 | return -EEXIST; /* cannot add when bitmap is present */ |
| 7055 | f = fget(fd); |
| 7056 | |
| 7057 | if (f == NULL) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7058 | pr_warn("%s: error: failed to get bitmap file\n", |
| 7059 | mdname(mddev)); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7060 | return -EBADF; |
| 7061 | } |
| 7062 | |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 7063 | inode = f->f_mapping->host; |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 7064 | if (!S_ISREG(inode->i_mode)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7065 | pr_warn("%s: error: bitmap file must be a regular file\n", |
| 7066 | mdname(mddev)); |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 7067 | err = -EBADF; |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 7068 | } else if (!(f->f_mode & FMODE_WRITE)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7069 | pr_warn("%s: error: bitmap file must open for write\n", |
| 7070 | mdname(mddev)); |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 7071 | err = -EBADF; |
| 7072 | } else if (atomic_read(&inode->i_writecount) != 1) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7073 | pr_warn("%s: error: bitmap file is already in use\n", |
| 7074 | mdname(mddev)); |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 7075 | err = -EBUSY; |
| 7076 | } |
| 7077 | if (err) { |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 7078 | fput(f); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7079 | return err; |
| 7080 | } |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 7081 | mddev->bitmap_info.file = f; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 7082 | mddev->bitmap_info.offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7083 | } else if (mddev->bitmap == NULL) |
| 7084 | return -ENOENT; /* cannot remove what isn't there */ |
| 7085 | err = 0; |
| 7086 | if (mddev->pers) { |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 7087 | if (fd >= 0) { |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 7088 | struct bitmap *bitmap; |
| 7089 | |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 7090 | bitmap = md_bitmap_create(mddev, -1); |
NeilBrown | 9e1cc0a | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 7091 | mddev_suspend(mddev); |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 7092 | if (!IS_ERR(bitmap)) { |
| 7093 | mddev->bitmap = bitmap; |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 7094 | err = md_bitmap_load(mddev); |
NeilBrown | ba599ac | 2015-02-25 11:44:11 +1100 | [diff] [blame] | 7095 | } else |
| 7096 | err = PTR_ERR(bitmap); |
NeilBrown | 52a0d49 | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 7097 | if (err) { |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 7098 | md_bitmap_destroy(mddev); |
NeilBrown | 52a0d49 | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 7099 | fd = -1; |
| 7100 | } |
NeilBrown | 9e1cc0a | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 7101 | mddev_resume(mddev); |
NeilBrown | 52a0d49 | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 7102 | } else if (fd < 0) { |
NeilBrown | 9e1cc0a | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 7103 | mddev_suspend(mddev); |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 7104 | md_bitmap_destroy(mddev); |
NeilBrown | 9e1cc0a | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 7105 | mddev_resume(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 7106 | } |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 7107 | } |
| 7108 | if (fd < 0) { |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 7109 | struct file *f = mddev->bitmap_info.file; |
| 7110 | if (f) { |
| 7111 | spin_lock(&mddev->lock); |
| 7112 | mddev->bitmap_info.file = NULL; |
| 7113 | spin_unlock(&mddev->lock); |
| 7114 | fput(f); |
| 7115 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7116 | } |
| 7117 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7118 | return err; |
| 7119 | } |
| 7120 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7121 | /* |
Christoph Hellwig | 7e0adbf | 2020-06-07 17:31:19 +0200 | [diff] [blame] | 7122 | * md_set_array_info is used two different ways |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7123 | * The original usage is when creating a new array. |
| 7124 | * In this usage, raid_disks is > 0 and it together with |
| 7125 | * level, size, not_persistent,layout,chunksize determine the |
| 7126 | * shape of the array. |
| 7127 | * This will always create an array with a type-0.90.0 superblock. |
| 7128 | * The newer usage is when assembling an array. |
| 7129 | * In this case raid_disks will be 0, and the major_version field is |
| 7130 | * use to determine which style super-blocks are to be found on the devices. |
| 7131 | * The minor and patch _version numbers are also kept incase the |
| 7132 | * super_block handler wishes to interpret them. |
| 7133 | */ |
Christoph Hellwig | 7e0adbf | 2020-06-07 17:31:19 +0200 | [diff] [blame] | 7134 | 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] | 7135 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7136 | if (info->raid_disks == 0) { |
| 7137 | /* just setting version number for superblock loading */ |
| 7138 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 7139 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7140 | super_types[info->major_version].name == NULL) { |
| 7141 | /* maybe try to auto-load a module? */ |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7142 | pr_warn("md: superblock version %d not known\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7143 | info->major_version); |
| 7144 | return -EINVAL; |
| 7145 | } |
| 7146 | mddev->major_version = info->major_version; |
| 7147 | mddev->minor_version = info->minor_version; |
| 7148 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 7149 | mddev->persistent = !info->not_persistent; |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 7150 | /* ensure mddev_put doesn't delete this now that there |
| 7151 | * is some minimal configuration. |
| 7152 | */ |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 7153 | mddev->ctime = ktime_get_real_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7154 | return 0; |
| 7155 | } |
| 7156 | mddev->major_version = MD_MAJOR_VERSION; |
| 7157 | mddev->minor_version = MD_MINOR_VERSION; |
| 7158 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
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 | |
| 7161 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 7162 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7163 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7164 | mddev->raid_disks = info->raid_disks; |
| 7165 | /* don't set md_minor, it is determined by which /dev/md* was |
| 7166 | * openned |
| 7167 | */ |
| 7168 | if (info->state & (1<<MD_SB_CLEAN)) |
| 7169 | mddev->recovery_cp = MaxSector; |
| 7170 | else |
| 7171 | mddev->recovery_cp = 0; |
| 7172 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 7173 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7174 | |
| 7175 | mddev->layout = info->layout; |
NeilBrown | 33f2c35 | 2019-09-09 16:52:29 +1000 | [diff] [blame] | 7176 | if (mddev->level == 0) |
| 7177 | /* Cannot trust RAID0 layout info here */ |
| 7178 | mddev->layout = -1; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 7179 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7180 | |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 7181 | if (mddev->persistent) { |
NeilBrown | 1b3bae4 | 2017-03-01 07:31:28 +1100 | [diff] [blame] | 7182 | mddev->max_disks = MD_SB_DISKS; |
| 7183 | mddev->flags = 0; |
| 7184 | mddev->sb_flags = 0; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 7185 | } |
| 7186 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7187 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 7188 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 7189 | mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 7190 | mddev->bitmap_info.offset = 0; |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 7191 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7192 | mddev->reshape_position = MaxSector; |
| 7193 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7194 | /* |
| 7195 | * Generate a 128 bit UUID |
| 7196 | */ |
| 7197 | get_random_bytes(mddev->uuid, 16); |
| 7198 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7199 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 7200 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7201 | mddev->new_layout = mddev->layout; |
| 7202 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7203 | mddev->reshape_backwards = 0; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7204 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7205 | return 0; |
| 7206 | } |
| 7207 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7208 | void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors) |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 7209 | { |
Shaohua Li | efa4b77 | 2017-10-18 22:08:13 -0700 | [diff] [blame] | 7210 | lockdep_assert_held(&mddev->reconfig_mutex); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 7211 | |
| 7212 | if (mddev->external_size) |
| 7213 | return; |
| 7214 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 7215 | mddev->array_sectors = array_sectors; |
| 7216 | } |
| 7217 | EXPORT_SYMBOL(md_set_array_sectors); |
| 7218 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7219 | static int update_size(struct mddev *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 7220 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7221 | struct md_rdev *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 7222 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 7223 | int fit = (num_sectors == 0); |
Guoqing Jiang | 818da59 | 2017-03-01 16:42:40 +0800 | [diff] [blame] | 7224 | sector_t old_dev_sectors = mddev->dev_sectors; |
Guoqing Jiang | ab5a98b | 2016-05-02 11:33:13 -0400 | [diff] [blame] | 7225 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 7226 | if (mddev->pers->resize == NULL) |
| 7227 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 7228 | /* The "num_sectors" is the number of sectors of each device that |
| 7229 | * is used. This can only make sense for arrays with redundancy. |
| 7230 | * linear and raid0 always use whatever space is available. We can only |
| 7231 | * consider changing this number if no resync or reconstruction is |
| 7232 | * 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] | 7233 | * 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] | 7234 | * of each device. If num_sectors is zero, we find the largest size |
| 7235 | * that fits. |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 7236 | */ |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 7237 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 7238 | mddev->sync_thread) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 7239 | return -EBUSY; |
NeilBrown | bd8839e | 2014-05-28 13:39:21 +1000 | [diff] [blame] | 7240 | if (mddev->ro) |
| 7241 | return -EROFS; |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 7242 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7243 | rdev_for_each(rdev, mddev) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7244 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 7245 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 7246 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 7247 | num_sectors = avail; |
| 7248 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 7249 | return -ENOSPC; |
| 7250 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 7251 | rv = mddev->pers->resize(mddev, num_sectors); |
Guoqing Jiang | c948363 | 2017-02-24 11:15:23 +0800 | [diff] [blame] | 7252 | if (!rv) { |
Guoqing Jiang | 818da59 | 2017-03-01 16:42:40 +0800 | [diff] [blame] | 7253 | if (mddev_is_clustered(mddev)) |
| 7254 | md_cluster_ops->update_size(mddev, old_dev_sectors); |
| 7255 | else if (mddev->queue) { |
Christoph Hellwig | 2c247c5 | 2020-11-16 15:57:11 +0100 | [diff] [blame] | 7256 | set_capacity_and_notify(mddev->gendisk, |
| 7257 | mddev->array_sectors); |
Guoqing Jiang | c948363 | 2017-02-24 11:15:23 +0800 | [diff] [blame] | 7258 | } |
| 7259 | } |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 7260 | return rv; |
| 7261 | } |
| 7262 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7263 | static int update_raid_disks(struct mddev *mddev, int raid_disks) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 7264 | { |
| 7265 | int rv; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7266 | struct md_rdev *rdev; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 7267 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 7268 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 7269 | return -EINVAL; |
NeilBrown | bd8839e | 2014-05-28 13:39:21 +1000 | [diff] [blame] | 7270 | if (mddev->ro) |
| 7271 | return -EROFS; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 7272 | if (raid_disks <= 0 || |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 7273 | (mddev->max_disks && raid_disks >= mddev->max_disks)) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 7274 | return -EINVAL; |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 7275 | if (mddev->sync_thread || |
| 7276 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 7277 | mddev->reshape_position != MaxSector) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 7278 | return -EBUSY; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7279 | |
| 7280 | rdev_for_each(rdev, mddev) { |
| 7281 | if (mddev->raid_disks < raid_disks && |
| 7282 | rdev->data_offset < rdev->new_data_offset) |
| 7283 | return -EINVAL; |
| 7284 | if (mddev->raid_disks > raid_disks && |
| 7285 | rdev->data_offset > rdev->new_data_offset) |
| 7286 | return -EINVAL; |
| 7287 | } |
| 7288 | |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 7289 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7290 | if (mddev->delta_disks < 0) |
| 7291 | mddev->reshape_backwards = 1; |
| 7292 | else if (mddev->delta_disks > 0) |
| 7293 | mddev->reshape_backwards = 0; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 7294 | |
| 7295 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7296 | if (rv < 0) { |
NeilBrown | de171cb | 2011-01-31 11:57:42 +1100 | [diff] [blame] | 7297 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7298 | mddev->reshape_backwards = 0; |
| 7299 | } |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 7300 | return rv; |
| 7301 | } |
| 7302 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7303 | /* |
| 7304 | * update_array_info is used to change the configuration of an |
| 7305 | * on-line array. |
| 7306 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 7307 | * fields in the info are checked against the array. |
| 7308 | * Any differences that cannot be handled will cause an error. |
| 7309 | * Normally, only one change can be managed at a time. |
| 7310 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7311 | 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] | 7312 | { |
| 7313 | int rv = 0; |
| 7314 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7315 | int state = 0; |
| 7316 | |
| 7317 | /* calculate expected state,ignoring low bits */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 7318 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7319 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7320 | |
| 7321 | if (mddev->major_version != info->major_version || |
| 7322 | mddev->minor_version != info->minor_version || |
| 7323 | /* mddev->patch_version != info->patch_version || */ |
| 7324 | mddev->ctime != info->ctime || |
| 7325 | mddev->level != info->level || |
| 7326 | /* mddev->layout != info->layout || */ |
Firo Yang | 4e02361 | 2015-06-11 09:41:10 +0800 | [diff] [blame] | 7327 | mddev->persistent != !info->not_persistent || |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 7328 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7329 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 7330 | ((state^info->state) & 0xfffffe00) |
| 7331 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7332 | return -EINVAL; |
| 7333 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7334 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 7335 | cnt++; |
| 7336 | if (mddev->raid_disks != info->raid_disks) |
| 7337 | cnt++; |
| 7338 | if (mddev->layout != info->layout) |
| 7339 | cnt++; |
| 7340 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 7341 | cnt++; |
| 7342 | if (cnt == 0) |
| 7343 | return 0; |
| 7344 | if (cnt > 1) |
| 7345 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7346 | |
| 7347 | if (mddev->layout != info->layout) { |
| 7348 | /* Change layout |
| 7349 | * we don't need to do anything at the md level, the |
| 7350 | * personality will take care of it all. |
| 7351 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 7352 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7353 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 7354 | else { |
| 7355 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 7356 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 7357 | if (rv) |
| 7358 | mddev->new_layout = mddev->layout; |
| 7359 | return rv; |
| 7360 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7361 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7362 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 7363 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7364 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 7365 | if (mddev->raid_disks != info->raid_disks) |
| 7366 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7367 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7368 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 7369 | if (mddev->pers->quiesce == NULL || mddev->thread == NULL) { |
| 7370 | rv = -EINVAL; |
| 7371 | goto err; |
| 7372 | } |
| 7373 | if (mddev->recovery || mddev->sync_thread) { |
| 7374 | rv = -EBUSY; |
| 7375 | goto err; |
| 7376 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7377 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 7378 | struct bitmap *bitmap; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7379 | /* add the bitmap */ |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 7380 | if (mddev->bitmap) { |
| 7381 | rv = -EEXIST; |
| 7382 | goto err; |
| 7383 | } |
| 7384 | if (mddev->bitmap_info.default_offset == 0) { |
| 7385 | rv = -EINVAL; |
| 7386 | goto err; |
| 7387 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 7388 | mddev->bitmap_info.offset = |
| 7389 | mddev->bitmap_info.default_offset; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 7390 | mddev->bitmap_info.space = |
| 7391 | mddev->bitmap_info.default_space; |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 7392 | bitmap = md_bitmap_create(mddev, -1); |
NeilBrown | 9e1cc0a | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 7393 | mddev_suspend(mddev); |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 7394 | if (!IS_ERR(bitmap)) { |
| 7395 | mddev->bitmap = bitmap; |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 7396 | rv = md_bitmap_load(mddev); |
NeilBrown | ba599ac | 2015-02-25 11:44:11 +1100 | [diff] [blame] | 7397 | } else |
| 7398 | rv = PTR_ERR(bitmap); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7399 | if (rv) |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 7400 | md_bitmap_destroy(mddev); |
NeilBrown | 9e1cc0a | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 7401 | mddev_resume(mddev); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7402 | } else { |
| 7403 | /* remove the bitmap */ |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 7404 | if (!mddev->bitmap) { |
| 7405 | rv = -ENOENT; |
| 7406 | goto err; |
| 7407 | } |
| 7408 | if (mddev->bitmap->storage.file) { |
| 7409 | rv = -EINVAL; |
| 7410 | goto err; |
| 7411 | } |
Guoqing Jiang | f6a2dc6 | 2015-12-21 10:51:00 +1100 | [diff] [blame] | 7412 | if (mddev->bitmap_info.nodes) { |
| 7413 | /* hold PW on all the bitmap lock */ |
| 7414 | if (md_cluster_ops->lock_all_bitmaps(mddev) <= 0) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7415 | 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] | 7416 | rv = -EPERM; |
| 7417 | md_cluster_ops->unlock_all_bitmaps(mddev); |
| 7418 | goto err; |
| 7419 | } |
| 7420 | |
| 7421 | mddev->bitmap_info.nodes = 0; |
| 7422 | md_cluster_ops->leave(mddev); |
Zhao Heming | edee9df | 2020-07-21 02:08:53 +0800 | [diff] [blame] | 7423 | module_put(md_cluster_mod); |
Zhao Heming | 7c9d5c5 | 2020-07-21 02:08:52 +0800 | [diff] [blame] | 7424 | mddev->safemode_delay = DEFAULT_SAFEMODE_DELAY; |
Guoqing Jiang | f6a2dc6 | 2015-12-21 10:51:00 +1100 | [diff] [blame] | 7425 | } |
NeilBrown | 9e1cc0a | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 7426 | mddev_suspend(mddev); |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 7427 | md_bitmap_destroy(mddev); |
NeilBrown | 9e1cc0a | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 7428 | mddev_resume(mddev); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 7429 | mddev->bitmap_info.offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7430 | } |
| 7431 | } |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7432 | md_update_sb(mddev, 1); |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 7433 | return rv; |
| 7434 | err: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7435 | return rv; |
| 7436 | } |
| 7437 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7438 | static int set_disk_faulty(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7439 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7440 | struct md_rdev *rdev; |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 7441 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7442 | |
| 7443 | if (mddev->pers == NULL) |
| 7444 | return -ENODEV; |
| 7445 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 7446 | rcu_read_lock(); |
Tomasz Majchrzak | 1532d9e | 2017-12-27 10:31:40 +0100 | [diff] [blame] | 7447 | rdev = md_find_rdev_rcu(mddev, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7448 | if (!rdev) |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 7449 | err = -ENODEV; |
| 7450 | else { |
| 7451 | md_error(mddev, rdev); |
| 7452 | if (!test_bit(Faulty, &rdev->flags)) |
| 7453 | err = -EBUSY; |
| 7454 | } |
| 7455 | rcu_read_unlock(); |
| 7456 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7457 | } |
| 7458 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 7459 | /* |
| 7460 | * We have a problem here : there is no easy way to give a CHS |
| 7461 | * virtual geometry. We currently pretend that we have a 2 heads |
| 7462 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 7463 | * dosfs just mad... ;-) |
| 7464 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 7465 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 7466 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7467 | struct mddev *mddev = bdev->bd_disk->private_data; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 7468 | |
| 7469 | geo->heads = 2; |
| 7470 | geo->sectors = 4; |
NeilBrown | 49ce6ce | 2010-03-29 10:51:42 +1100 | [diff] [blame] | 7471 | geo->cylinders = mddev->array_sectors / 8; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 7472 | return 0; |
| 7473 | } |
| 7474 | |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 7475 | static inline bool md_ioctl_valid(unsigned int cmd) |
| 7476 | { |
| 7477 | switch (cmd) { |
| 7478 | case ADD_NEW_DISK: |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 7479 | case GET_ARRAY_INFO: |
| 7480 | case GET_BITMAP_FILE: |
| 7481 | case GET_DISK_INFO: |
| 7482 | case HOT_ADD_DISK: |
| 7483 | case HOT_REMOVE_DISK: |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 7484 | case RAID_VERSION: |
| 7485 | case RESTART_ARRAY_RW: |
| 7486 | case RUN_ARRAY: |
| 7487 | case SET_ARRAY_INFO: |
| 7488 | case SET_BITMAP_FILE: |
| 7489 | case SET_DISK_FAULTY: |
| 7490 | case STOP_ARRAY: |
| 7491 | case STOP_ARRAY_RO: |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 7492 | case CLUSTERED_DISK_NACK: |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 7493 | return true; |
| 7494 | default: |
| 7495 | return false; |
| 7496 | } |
| 7497 | } |
| 7498 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 7499 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7500 | unsigned int cmd, unsigned long arg) |
| 7501 | { |
| 7502 | int err = 0; |
| 7503 | void __user *argp = (void __user *)arg; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7504 | struct mddev *mddev = NULL; |
NeilBrown | 065e519 | 2017-04-06 11:16:33 +0800 | [diff] [blame] | 7505 | bool did_set_md_closing = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7506 | |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 7507 | if (!md_ioctl_valid(cmd)) |
| 7508 | return -ENOTTY; |
| 7509 | |
NeilBrown | 506c9e4 | 2011-12-23 10:17:26 +1100 | [diff] [blame] | 7510 | switch (cmd) { |
| 7511 | case RAID_VERSION: |
| 7512 | case GET_ARRAY_INFO: |
| 7513 | case GET_DISK_INFO: |
| 7514 | break; |
| 7515 | default: |
| 7516 | if (!capable(CAP_SYS_ADMIN)) |
| 7517 | return -EACCES; |
| 7518 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7519 | |
| 7520 | /* |
| 7521 | * Commands dealing with the RAID driver but not any |
| 7522 | * particular array: |
| 7523 | */ |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7524 | switch (cmd) { |
| 7525 | case RAID_VERSION: |
| 7526 | err = get_version(argp); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7527 | goto out; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7528 | default:; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7529 | } |
| 7530 | |
| 7531 | /* |
| 7532 | * Commands creating/starting a new array: |
| 7533 | */ |
| 7534 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 7535 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7536 | |
| 7537 | if (!mddev) { |
| 7538 | BUG(); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7539 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7540 | } |
| 7541 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 7542 | /* Some actions do not requires the mutex */ |
| 7543 | switch (cmd) { |
| 7544 | case GET_ARRAY_INFO: |
| 7545 | if (!mddev->raid_disks && !mddev->external) |
| 7546 | err = -ENODEV; |
| 7547 | else |
| 7548 | err = get_array_info(mddev, argp); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7549 | goto out; |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 7550 | |
| 7551 | case GET_DISK_INFO: |
| 7552 | if (!mddev->raid_disks && !mddev->external) |
| 7553 | err = -ENODEV; |
| 7554 | else |
| 7555 | err = get_disk_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 SET_DISK_FAULTY: |
| 7559 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7560 | goto out; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 7561 | |
| 7562 | case GET_BITMAP_FILE: |
| 7563 | err = get_bitmap_file(mddev, argp); |
| 7564 | goto out; |
| 7565 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 7566 | } |
| 7567 | |
Guoqing Jiang | 78b990c | 2020-04-04 23:57:10 +0200 | [diff] [blame] | 7568 | if (cmd == ADD_NEW_DISK || cmd == HOT_ADD_DISK) |
Guoqing Jiang | cc1ffe6 | 2020-04-04 23:57:08 +0200 | [diff] [blame] | 7569 | flush_rdev_wq(mddev); |
NeilBrown | a7a3f08 | 2012-12-11 13:35:54 +1100 | [diff] [blame] | 7570 | |
Hannes Reinecke | 90f5f7a | 2013-04-02 08:38:55 +0200 | [diff] [blame] | 7571 | if (cmd == HOT_REMOVE_DISK) |
| 7572 | /* need to ensure recovery thread has run */ |
| 7573 | wait_event_interruptible_timeout(mddev->sb_wait, |
| 7574 | !test_bit(MD_RECOVERY_NEEDED, |
Shaohua Li | 82a301c | 2016-12-08 15:48:18 -0800 | [diff] [blame] | 7575 | &mddev->recovery), |
Hannes Reinecke | 90f5f7a | 2013-04-02 08:38:55 +0200 | [diff] [blame] | 7576 | msecs_to_jiffies(5000)); |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 7577 | if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) { |
| 7578 | /* Need to flush page cache, and ensure no-one else opens |
| 7579 | * and writes |
| 7580 | */ |
| 7581 | mutex_lock(&mddev->open_mutex); |
NeilBrown | 9ba3b7f | 2014-09-09 14:00:15 +1000 | [diff] [blame] | 7582 | if (mddev->pers && atomic_read(&mddev->openers) > 1) { |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 7583 | mutex_unlock(&mddev->open_mutex); |
| 7584 | err = -EBUSY; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7585 | goto out; |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 7586 | } |
NeilBrown | 065e519 | 2017-04-06 11:16:33 +0800 | [diff] [blame] | 7587 | WARN_ON_ONCE(test_bit(MD_CLOSING, &mddev->flags)); |
Guoqing Jiang | af8d8e6 | 2016-08-12 13:42:37 +0800 | [diff] [blame] | 7588 | set_bit(MD_CLOSING, &mddev->flags); |
NeilBrown | 065e519 | 2017-04-06 11:16:33 +0800 | [diff] [blame] | 7589 | did_set_md_closing = true; |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 7590 | mutex_unlock(&mddev->open_mutex); |
| 7591 | sync_blockdev(bdev); |
| 7592 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7593 | err = mddev_lock(mddev); |
| 7594 | if (err) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7595 | pr_debug("md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 7596 | err, cmd); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7597 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7598 | } |
| 7599 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7600 | if (cmd == SET_ARRAY_INFO) { |
| 7601 | mdu_array_info_t info; |
| 7602 | if (!arg) |
| 7603 | memset(&info, 0, sizeof(info)); |
| 7604 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 7605 | err = -EFAULT; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7606 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7607 | } |
| 7608 | if (mddev->pers) { |
| 7609 | err = update_array_info(mddev, &info); |
| 7610 | if (err) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7611 | pr_warn("md: couldn't update array info. %d\n", err); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7612 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7613 | } |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7614 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7615 | } |
| 7616 | if (!list_empty(&mddev->disks)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7617 | pr_warn("md: array %s already has disks!\n", mdname(mddev)); |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7618 | err = -EBUSY; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7619 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7620 | } |
| 7621 | if (mddev->raid_disks) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7622 | pr_warn("md: array %s already initialised!\n", mdname(mddev)); |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7623 | err = -EBUSY; |
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 | } |
Christoph Hellwig | 7e0adbf | 2020-06-07 17:31:19 +0200 | [diff] [blame] | 7626 | err = md_set_array_info(mddev, &info); |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7627 | if (err) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7628 | pr_warn("md: couldn't set array info. %d\n", err); |
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 | } |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7631 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7632 | } |
| 7633 | |
| 7634 | /* |
| 7635 | * Commands querying/configuring an existing array: |
| 7636 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7637 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 7638 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 7639 | if ((!mddev->raid_disks && !mddev->external) |
| 7640 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 7641 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 7642 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7643 | err = -ENODEV; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7644 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7645 | } |
| 7646 | |
| 7647 | /* |
| 7648 | * Commands even a read-only array can execute: |
| 7649 | */ |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7650 | switch (cmd) { |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7651 | case RESTART_ARRAY_RW: |
| 7652 | err = restart_array(mddev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7653 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7654 | |
| 7655 | case STOP_ARRAY: |
| 7656 | err = do_md_stop(mddev, 0, bdev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7657 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7658 | |
| 7659 | case STOP_ARRAY_RO: |
| 7660 | err = md_set_readonly(mddev, bdev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7661 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7662 | |
NeilBrown | 3ea8929d | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 7663 | case HOT_REMOVE_DISK: |
| 7664 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7665 | goto unlock; |
NeilBrown | 3ea8929d | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 7666 | |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 7667 | case ADD_NEW_DISK: |
| 7668 | /* We can support ADD_NEW_DISK on read-only arrays |
Wei Fang | 466ad29 | 2016-03-21 19:19:30 +0800 | [diff] [blame] | 7669 | * only if we are re-adding a preexisting device. |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 7670 | * So require mddev->pers and MD_DISK_SYNC. |
| 7671 | */ |
| 7672 | if (mddev->pers) { |
| 7673 | mdu_disk_info_t info; |
| 7674 | if (copy_from_user(&info, argp, sizeof(info))) |
| 7675 | err = -EFAULT; |
| 7676 | else if (!(info.state & (1<<MD_DISK_SYNC))) |
| 7677 | /* Need to clear read-only for this */ |
| 7678 | break; |
| 7679 | else |
Christoph Hellwig | 7e0adbf | 2020-06-07 17:31:19 +0200 | [diff] [blame] | 7680 | err = md_add_new_disk(mddev, &info); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7681 | goto unlock; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 7682 | } |
| 7683 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7684 | } |
| 7685 | |
| 7686 | /* |
| 7687 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7688 | * superblock, so we do not allow them on read-only arrays. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7689 | */ |
NeilBrown | 326eb17 | 2014-09-30 15:36:28 +1000 | [diff] [blame] | 7690 | if (mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7691 | if (mddev->ro == 2) { |
| 7692 | mddev->ro = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7693 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7694 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | f3378b4 | 2013-02-28 11:59:03 +1100 | [diff] [blame] | 7695 | /* mddev_unlock will wake thread */ |
| 7696 | /* If a device failed while we were read-only, we |
| 7697 | * need to make sure the metadata is updated now. |
| 7698 | */ |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 7699 | if (test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)) { |
NeilBrown | f3378b4 | 2013-02-28 11:59:03 +1100 | [diff] [blame] | 7700 | mddev_unlock(mddev); |
| 7701 | wait_event(mddev->sb_wait, |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 7702 | !test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags) && |
| 7703 | !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)); |
NeilBrown | 29f097c | 2013-11-14 17:54:51 +1100 | [diff] [blame] | 7704 | mddev_lock_nointr(mddev); |
NeilBrown | f3378b4 | 2013-02-28 11:59:03 +1100 | [diff] [blame] | 7705 | } |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7706 | } else { |
| 7707 | err = -EROFS; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7708 | goto unlock; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7709 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7710 | } |
| 7711 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7712 | switch (cmd) { |
| 7713 | case ADD_NEW_DISK: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7714 | { |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7715 | mdu_disk_info_t info; |
| 7716 | if (copy_from_user(&info, argp, sizeof(info))) |
| 7717 | err = -EFAULT; |
| 7718 | else |
Christoph Hellwig | 7e0adbf | 2020-06-07 17:31:19 +0200 | [diff] [blame] | 7719 | err = md_add_new_disk(mddev, &info); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7720 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7721 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7722 | |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 7723 | case CLUSTERED_DISK_NACK: |
| 7724 | if (mddev_is_clustered(mddev)) |
| 7725 | md_cluster_ops->new_disk_ack(mddev, false); |
| 7726 | else |
| 7727 | err = -EINVAL; |
| 7728 | goto unlock; |
| 7729 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7730 | case HOT_ADD_DISK: |
| 7731 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7732 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7733 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7734 | case RUN_ARRAY: |
| 7735 | err = do_md_run(mddev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7736 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7737 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7738 | case SET_BITMAP_FILE: |
| 7739 | err = set_bitmap_file(mddev, (int)arg); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7740 | goto unlock; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7741 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7742 | default: |
| 7743 | err = -EINVAL; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7744 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7745 | } |
| 7746 | |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7747 | unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7748 | if (mddev->hold_active == UNTIL_IOCTL && |
| 7749 | err != -EINVAL) |
| 7750 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7751 | mddev_unlock(mddev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7752 | out: |
NeilBrown | 065e519 | 2017-04-06 11:16:33 +0800 | [diff] [blame] | 7753 | if(did_set_md_closing) |
| 7754 | clear_bit(MD_CLOSING, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7755 | return err; |
| 7756 | } |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 7757 | #ifdef CONFIG_COMPAT |
| 7758 | static int md_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 7759 | unsigned int cmd, unsigned long arg) |
| 7760 | { |
| 7761 | switch (cmd) { |
| 7762 | case HOT_REMOVE_DISK: |
| 7763 | case HOT_ADD_DISK: |
| 7764 | case SET_DISK_FAULTY: |
| 7765 | case SET_BITMAP_FILE: |
| 7766 | /* These take in integer arg, do not convert */ |
| 7767 | break; |
| 7768 | default: |
| 7769 | arg = (unsigned long)compat_ptr(arg); |
| 7770 | break; |
| 7771 | } |
| 7772 | |
| 7773 | return md_ioctl(bdev, mode, cmd, arg); |
| 7774 | } |
| 7775 | #endif /* CONFIG_COMPAT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7776 | |
Christoph Hellwig | 118cf08 | 2020-11-03 11:00:13 +0100 | [diff] [blame] | 7777 | static int md_set_read_only(struct block_device *bdev, bool ro) |
| 7778 | { |
| 7779 | struct mddev *mddev = bdev->bd_disk->private_data; |
| 7780 | int err; |
| 7781 | |
| 7782 | err = mddev_lock(mddev); |
| 7783 | if (err) |
| 7784 | return err; |
| 7785 | |
| 7786 | if (!mddev->raid_disks && !mddev->external) { |
| 7787 | err = -ENODEV; |
| 7788 | goto out_unlock; |
| 7789 | } |
| 7790 | |
| 7791 | /* |
| 7792 | * Transitioning to read-auto need only happen for arrays that call |
| 7793 | * md_write_start and which are not ready for writes yet. |
| 7794 | */ |
| 7795 | if (!ro && mddev->ro == 1 && mddev->pers) { |
| 7796 | err = restart_array(mddev); |
| 7797 | if (err) |
| 7798 | goto out_unlock; |
| 7799 | mddev->ro = 2; |
| 7800 | } |
| 7801 | |
| 7802 | out_unlock: |
| 7803 | mddev_unlock(mddev); |
| 7804 | return err; |
| 7805 | } |
| 7806 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 7807 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7808 | { |
| 7809 | /* |
| 7810 | * Succeed if we can lock the mddev, which confirms that |
| 7811 | * it isn't being stopped right now. |
| 7812 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7813 | struct mddev *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7814 | int err; |
| 7815 | |
Yuanhan Liu | 0c09822 | 2012-05-22 13:55:32 +1000 | [diff] [blame] | 7816 | if (!mddev) |
| 7817 | return -ENODEV; |
| 7818 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7819 | if (mddev->gendisk != bdev->bd_disk) { |
| 7820 | /* we are racing with mddev_put which is discarding this |
| 7821 | * bd_disk. |
| 7822 | */ |
| 7823 | mddev_put(mddev); |
| 7824 | /* Wait until bdev->bd_disk is definitely gone */ |
Guoqing Jiang | f6766ff | 2020-04-04 23:57:09 +0200 | [diff] [blame] | 7825 | if (work_pending(&mddev->del_work)) |
| 7826 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7827 | /* Then retry the open from the top */ |
| 7828 | return -ERESTARTSYS; |
| 7829 | } |
| 7830 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 7831 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 7832 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7833 | goto out; |
| 7834 | |
Guoqing Jiang | af8d8e6 | 2016-08-12 13:42:37 +0800 | [diff] [blame] | 7835 | if (test_bit(MD_CLOSING, &mddev->flags)) { |
| 7836 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | e2342ca | 2016-12-05 16:40:50 +1100 | [diff] [blame] | 7837 | err = -ENODEV; |
| 7838 | goto out; |
Guoqing Jiang | af8d8e6 | 2016-08-12 13:42:37 +0800 | [diff] [blame] | 7839 | } |
| 7840 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7841 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7842 | atomic_inc(&mddev->openers); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 7843 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7844 | |
Christoph Hellwig | 818077d | 2020-09-08 16:53:43 +0200 | [diff] [blame] | 7845 | bdev_check_media_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7846 | out: |
NeilBrown | e2342ca | 2016-12-05 16:40:50 +1100 | [diff] [blame] | 7847 | if (err) |
| 7848 | mddev_put(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7849 | return err; |
| 7850 | } |
| 7851 | |
Al Viro | db2a144 | 2013-05-05 21:52:57 -0400 | [diff] [blame] | 7852 | static void md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7853 | { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7854 | struct mddev *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7855 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 7856 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7857 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7858 | mddev_put(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7859 | } |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7860 | |
Christoph Hellwig | a564e23 | 2020-07-08 14:25:41 +0200 | [diff] [blame] | 7861 | static unsigned int md_check_events(struct gendisk *disk, unsigned int clearing) |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7862 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7863 | struct mddev *mddev = disk->private_data; |
Christoph Hellwig | a564e23 | 2020-07-08 14:25:41 +0200 | [diff] [blame] | 7864 | unsigned int ret = 0; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7865 | |
Christoph Hellwig | a564e23 | 2020-07-08 14:25:41 +0200 | [diff] [blame] | 7866 | if (mddev->changed) |
| 7867 | ret = DISK_EVENT_MEDIA_CHANGE; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7868 | mddev->changed = 0; |
Christoph Hellwig | a564e23 | 2020-07-08 14:25:41 +0200 | [diff] [blame] | 7869 | return ret; |
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 | |
Christoph Hellwig | 7e0adbf | 2020-06-07 17:31:19 +0200 | [diff] [blame] | 7872 | const struct block_device_operations md_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7873 | { |
| 7874 | .owner = THIS_MODULE, |
Christoph Hellwig | c62b37d | 2020-07-01 10:59:43 +0200 | [diff] [blame] | 7875 | .submit_bio = md_submit_bio, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 7876 | .open = md_open, |
| 7877 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 7878 | .ioctl = md_ioctl, |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 7879 | #ifdef CONFIG_COMPAT |
| 7880 | .compat_ioctl = md_compat_ioctl, |
| 7881 | #endif |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 7882 | .getgeo = md_getgeo, |
Christoph Hellwig | a564e23 | 2020-07-08 14:25:41 +0200 | [diff] [blame] | 7883 | .check_events = md_check_events, |
Christoph Hellwig | 118cf08 | 2020-11-03 11:00:13 +0100 | [diff] [blame] | 7884 | .set_read_only = md_set_read_only, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7885 | }; |
| 7886 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7887 | static int md_thread(void *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7888 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7889 | struct md_thread *thread = arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7890 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7891 | /* |
| 7892 | * md_thread is a 'system-thread', it's priority should be very |
| 7893 | * high. We avoid resource deadlocks individually in each |
| 7894 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 7895 | * the very same RT priority as kswapd, thus we will never get |
| 7896 | * into a priority inversion deadlock. |
| 7897 | * |
| 7898 | * we definitely have to have equal or higher priority than |
| 7899 | * bdflush, otherwise bdflush will deadlock if there are too |
| 7900 | * many dirty RAID5 blocks. |
| 7901 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7902 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 7903 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 7904 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7905 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 7906 | /* We need to wait INTERRUPTIBLE so that |
| 7907 | * we don't add to the load-average. |
| 7908 | * That means we need to be sure no signals are |
| 7909 | * pending |
| 7910 | */ |
| 7911 | if (signal_pending(current)) |
| 7912 | flush_signals(current); |
| 7913 | |
| 7914 | wait_event_interruptible_timeout |
| 7915 | (thread->wqueue, |
| 7916 | test_bit(THREAD_WAKEUP, &thread->flags) |
Shaohua Li | ce1ccd0 | 2016-11-21 10:29:18 -0800 | [diff] [blame] | 7917 | || kthread_should_stop() || kthread_should_park(), |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 7918 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7919 | |
NeilBrown | 6c98791 | 2011-01-14 09:13:53 +1100 | [diff] [blame] | 7920 | clear_bit(THREAD_WAKEUP, &thread->flags); |
Shaohua Li | ce1ccd0 | 2016-11-21 10:29:18 -0800 | [diff] [blame] | 7921 | if (kthread_should_park()) |
| 7922 | kthread_parkme(); |
NeilBrown | 6c98791 | 2011-01-14 09:13:53 +1100 | [diff] [blame] | 7923 | if (!kthread_should_stop()) |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 7924 | thread->run(thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7925 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 7926 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7927 | return 0; |
| 7928 | } |
| 7929 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7930 | void md_wakeup_thread(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7931 | { |
| 7932 | if (thread) { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 7933 | pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm); |
Guoqing Jiang | d1d9014 | 2017-10-09 10:32:48 +0800 | [diff] [blame] | 7934 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 7935 | wake_up(&thread->wqueue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7936 | } |
| 7937 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7938 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7939 | |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 7940 | struct md_thread *md_register_thread(void (*run) (struct md_thread *), |
| 7941 | struct mddev *mddev, const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7942 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7943 | struct md_thread *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7944 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7945 | thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7946 | if (!thread) |
| 7947 | return NULL; |
| 7948 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7949 | init_waitqueue_head(&thread->wqueue); |
| 7950 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7951 | thread->run = run; |
| 7952 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7953 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 7954 | thread->tsk = kthread_run(md_thread, thread, |
| 7955 | "%s_%s", |
| 7956 | mdname(thread->mddev), |
NeilBrown | 0232605 | 2012-07-03 15:56:52 +1000 | [diff] [blame] | 7957 | name); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 7958 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7959 | kfree(thread); |
| 7960 | return NULL; |
| 7961 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7962 | return thread; |
| 7963 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7964 | EXPORT_SYMBOL(md_register_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7965 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7966 | void md_unregister_thread(struct md_thread **threadp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7967 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7968 | struct md_thread *thread = *threadp; |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7969 | if (!thread) |
| 7970 | return; |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 7971 | pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 7972 | /* Locking ensures that mddev_unlock does not wake_up a |
| 7973 | * non-existent thread |
| 7974 | */ |
| 7975 | spin_lock(&pers_lock); |
| 7976 | *threadp = NULL; |
| 7977 | spin_unlock(&pers_lock); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 7978 | |
| 7979 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7980 | kfree(thread); |
| 7981 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7982 | EXPORT_SYMBOL(md_unregister_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7983 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7984 | void md_error(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7985 | { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7986 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7987 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7988 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 7989 | if (!mddev->pers || !mddev->pers->error_handler) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7990 | return; |
| 7991 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7992 | if (mddev->degraded) |
| 7993 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7994 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7995 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7996 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7997 | md_wakeup_thread(mddev->thread); |
NeilBrown | 768a418 | 2010-07-26 11:49:55 +1000 | [diff] [blame] | 7998 | if (mddev->event_work.func) |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 7999 | queue_work(md_misc_wq, &mddev->event_work); |
Guoqing Jiang | bb9ef71 | 2015-12-28 10:46:38 +0800 | [diff] [blame] | 8000 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8001 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 8002 | EXPORT_SYMBOL(md_error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8003 | |
| 8004 | /* seq_file implementation /proc/mdstat */ |
| 8005 | |
| 8006 | static void status_unused(struct seq_file *seq) |
| 8007 | { |
| 8008 | int i = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8009 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8010 | |
| 8011 | seq_printf(seq, "unused devices: "); |
| 8012 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 8013 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8014 | char b[BDEVNAME_SIZE]; |
| 8015 | i++; |
| 8016 | seq_printf(seq, "%s ", |
| 8017 | bdevname(rdev->bdev,b)); |
| 8018 | } |
| 8019 | if (!i) |
| 8020 | seq_printf(seq, "<none>"); |
| 8021 | |
| 8022 | seq_printf(seq, "\n"); |
| 8023 | } |
| 8024 | |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 8025 | static int status_resync(struct seq_file *seq, struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8026 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 8027 | sector_t max_sectors, resync, res; |
Mariusz Tkaczyk | 9642fa7 | 2019-06-13 16:11:41 +0200 | [diff] [blame] | 8028 | unsigned long dt, db = 0; |
| 8029 | sector_t rt, curr_mark_cnt, resync_mark_cnt; |
| 8030 | int scale, recovery_active; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 8031 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8032 | |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 8033 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 8034 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 8035 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8036 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 8037 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8038 | |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 8039 | resync = mddev->curr_resync; |
| 8040 | if (resync <= 3) { |
| 8041 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 8042 | /* Still cleaning up */ |
| 8043 | resync = max_sectors; |
Nate Dailey | d2e2ec8 | 2017-11-30 11:33:30 -0500 | [diff] [blame] | 8044 | } else if (resync > max_sectors) |
| 8045 | resync = max_sectors; |
| 8046 | else |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 8047 | resync -= atomic_read(&mddev->recovery_active); |
| 8048 | |
| 8049 | if (resync == 0) { |
Guoqing Jiang | 0357ba2 | 2018-07-02 16:26:25 +0800 | [diff] [blame] | 8050 | if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery)) { |
| 8051 | struct md_rdev *rdev; |
| 8052 | |
| 8053 | rdev_for_each(rdev, mddev) |
| 8054 | if (rdev->raid_disk >= 0 && |
| 8055 | !test_bit(Faulty, &rdev->flags) && |
| 8056 | rdev->recovery_offset != MaxSector && |
| 8057 | rdev->recovery_offset) { |
| 8058 | seq_printf(seq, "\trecover=REMOTE"); |
| 8059 | return 1; |
| 8060 | } |
| 8061 | if (mddev->reshape_position != MaxSector) |
| 8062 | seq_printf(seq, "\treshape=REMOTE"); |
| 8063 | else |
| 8064 | seq_printf(seq, "\tresync=REMOTE"); |
| 8065 | return 1; |
| 8066 | } |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 8067 | if (mddev->recovery_cp < MaxSector) { |
| 8068 | seq_printf(seq, "\tresync=PENDING"); |
| 8069 | return 1; |
| 8070 | } |
| 8071 | return 0; |
| 8072 | } |
| 8073 | if (resync < 3) { |
| 8074 | seq_printf(seq, "\tresync=DELAYED"); |
| 8075 | return 1; |
| 8076 | } |
| 8077 | |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 8078 | WARN_ON(max_sectors == 0); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 8079 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 8080 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 8081 | * u32, as those are the requirements for sector_div. |
| 8082 | * Thus 'scale' must be at least 10 |
| 8083 | */ |
| 8084 | scale = 10; |
| 8085 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 8086 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 8087 | scale++; |
| 8088 | } |
| 8089 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 8090 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 8091 | |
| 8092 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8093 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 8094 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8095 | seq_printf(seq, "["); |
| 8096 | for (i = 0; i < x; i++) |
| 8097 | seq_printf(seq, "="); |
| 8098 | seq_printf(seq, ">"); |
| 8099 | for (i = 0; i < y; i++) |
| 8100 | seq_printf(seq, "."); |
| 8101 | seq_printf(seq, "] "); |
| 8102 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 8103 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 8104 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 8105 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 8106 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 8107 | "check" : |
| 8108 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 8109 | "resync" : "recovery"))), |
| 8110 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 8111 | (unsigned long long) resync/2, |
| 8112 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8113 | |
| 8114 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8115 | * dt: time from mark until now |
| 8116 | * db: blocks written from mark until now |
| 8117 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 8118 | * |
Mariusz Tkaczyk | 9642fa7 | 2019-06-13 16:11:41 +0200 | [diff] [blame] | 8119 | * rt is a sector_t, which is always 64bit now. We are keeping |
| 8120 | * the original algorithm, but it is not really necessary. |
| 8121 | * |
| 8122 | * Original algorithm: |
| 8123 | * So we divide before multiply in case it is 32bit and close |
| 8124 | * to the limit. |
| 8125 | * We scale the divisor (db) by 32 to avoid losing precision |
| 8126 | * near the end of resync when the number of remaining sectors |
| 8127 | * is close to 'db'. |
| 8128 | * We then divide rt by 32 after multiplying by db to compensate. |
| 8129 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8130 | */ |
| 8131 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 8132 | if (!dt) dt++; |
Mariusz Tkaczyk | 9642fa7 | 2019-06-13 16:11:41 +0200 | [diff] [blame] | 8133 | |
| 8134 | curr_mark_cnt = mddev->curr_mark_cnt; |
| 8135 | recovery_active = atomic_read(&mddev->recovery_active); |
| 8136 | resync_mark_cnt = mddev->resync_mark_cnt; |
| 8137 | |
| 8138 | if (curr_mark_cnt >= (recovery_active + resync_mark_cnt)) |
| 8139 | db = curr_mark_cnt - (recovery_active + resync_mark_cnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8140 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 8141 | rt = max_sectors - resync; /* number of remaining sectors */ |
Mariusz Tkaczyk | 9642fa7 | 2019-06-13 16:11:41 +0200 | [diff] [blame] | 8142 | rt = div64_u64(rt, db/32+1); |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 8143 | rt *= dt; |
| 8144 | rt >>= 5; |
| 8145 | |
| 8146 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 8147 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8148 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 8149 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 8150 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8151 | } |
| 8152 | |
| 8153 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 8154 | { |
| 8155 | struct list_head *tmp; |
| 8156 | loff_t l = *pos; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8157 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8158 | |
| 8159 | if (l >= 0x10000) |
| 8160 | return NULL; |
| 8161 | if (!l--) |
| 8162 | /* header */ |
| 8163 | return (void*)1; |
| 8164 | |
| 8165 | spin_lock(&all_mddevs_lock); |
| 8166 | list_for_each(tmp,&all_mddevs) |
| 8167 | if (!l--) { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8168 | mddev = list_entry(tmp, struct mddev, all_mddevs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8169 | mddev_get(mddev); |
| 8170 | spin_unlock(&all_mddevs_lock); |
| 8171 | return mddev; |
| 8172 | } |
| 8173 | spin_unlock(&all_mddevs_lock); |
| 8174 | if (!l--) |
| 8175 | return (void*)2;/* tail */ |
| 8176 | return NULL; |
| 8177 | } |
| 8178 | |
| 8179 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 8180 | { |
| 8181 | struct list_head *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8182 | struct mddev *next_mddev, *mddev = v; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 8183 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8184 | ++*pos; |
| 8185 | if (v == (void*)2) |
| 8186 | return NULL; |
| 8187 | |
| 8188 | spin_lock(&all_mddevs_lock); |
| 8189 | if (v == (void*)1) |
| 8190 | tmp = all_mddevs.next; |
| 8191 | else |
| 8192 | tmp = mddev->all_mddevs.next; |
| 8193 | if (tmp != &all_mddevs) |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8194 | next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8195 | else { |
| 8196 | next_mddev = (void*)2; |
| 8197 | *pos = 0x10000; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 8198 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8199 | spin_unlock(&all_mddevs_lock); |
| 8200 | |
| 8201 | if (v != (void*)1) |
| 8202 | mddev_put(mddev); |
| 8203 | return next_mddev; |
| 8204 | |
| 8205 | } |
| 8206 | |
| 8207 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 8208 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8209 | struct mddev *mddev = v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8210 | |
| 8211 | if (mddev && v != (void*)1 && v != (void*)2) |
| 8212 | mddev_put(mddev); |
| 8213 | } |
| 8214 | |
| 8215 | static int md_seq_show(struct seq_file *seq, void *v) |
| 8216 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8217 | struct mddev *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8218 | sector_t sectors; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8219 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8220 | |
| 8221 | if (v == (void*)1) { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 8222 | struct md_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8223 | seq_printf(seq, "Personalities : "); |
| 8224 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8225 | list_for_each_entry(pers, &pers_list, list) |
| 8226 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8227 | |
| 8228 | spin_unlock(&pers_lock); |
| 8229 | seq_printf(seq, "\n"); |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 8230 | seq->poll_event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8231 | return 0; |
| 8232 | } |
| 8233 | if (v == (void*)2) { |
| 8234 | status_unused(seq); |
| 8235 | return 0; |
| 8236 | } |
| 8237 | |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 8238 | spin_lock(&mddev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8239 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 8240 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 8241 | mddev->pers ? "" : "in"); |
| 8242 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8243 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8244 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8245 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 8246 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8247 | seq_printf(seq, " %s", mddev->pers->name); |
| 8248 | } |
| 8249 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8250 | sectors = 0; |
NeilBrown | f97fcad | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 8251 | rcu_read_lock(); |
| 8252 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8253 | char b[BDEVNAME_SIZE]; |
| 8254 | seq_printf(seq, " %s[%d]", |
| 8255 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 8256 | if (test_bit(WriteMostly, &rdev->flags)) |
| 8257 | seq_printf(seq, "(W)"); |
Shaohua Li | 9efdca1 | 2015-10-12 16:59:50 -0700 | [diff] [blame] | 8258 | if (test_bit(Journal, &rdev->flags)) |
| 8259 | seq_printf(seq, "(J)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 8260 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8261 | seq_printf(seq, "(F)"); |
| 8262 | continue; |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 8263 | } |
| 8264 | if (rdev->raid_disk < 0) |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 8265 | seq_printf(seq, "(S)"); /* spare */ |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 8266 | if (test_bit(Replacement, &rdev->flags)) |
| 8267 | seq_printf(seq, "(R)"); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8268 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8269 | } |
NeilBrown | f97fcad | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 8270 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8271 | |
| 8272 | if (!list_empty(&mddev->disks)) { |
| 8273 | if (mddev->pers) |
| 8274 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 8275 | (unsigned long long) |
| 8276 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8277 | else |
| 8278 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8279 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8280 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 8281 | if (mddev->persistent) { |
| 8282 | if (mddev->major_version != 0 || |
| 8283 | mddev->minor_version != 90) { |
| 8284 | seq_printf(seq," super %d.%d", |
| 8285 | mddev->major_version, |
| 8286 | mddev->minor_version); |
| 8287 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 8288 | } else if (mddev->external) |
| 8289 | seq_printf(seq, " super external:%s", |
| 8290 | mddev->metadata_type); |
| 8291 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 8292 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8293 | |
| 8294 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 8295 | mddev->pers->status(seq, mddev); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 8296 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 8297 | if (mddev->pers->sync_request) { |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 8298 | if (status_resync(seq, mddev)) |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 8299 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 8300 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 8301 | } else |
| 8302 | seq_printf(seq, "\n "); |
| 8303 | |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 8304 | md_bitmap_status(seq, mddev->bitmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8305 | |
| 8306 | seq_printf(seq, "\n"); |
| 8307 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 8308 | spin_unlock(&mddev->lock); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 8309 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8310 | return 0; |
| 8311 | } |
| 8312 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 8313 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8314 | .start = md_seq_start, |
| 8315 | .next = md_seq_next, |
| 8316 | .stop = md_seq_stop, |
| 8317 | .show = md_seq_show, |
| 8318 | }; |
| 8319 | |
| 8320 | static int md_seq_open(struct inode *inode, struct file *file) |
| 8321 | { |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 8322 | struct seq_file *seq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8323 | int error; |
| 8324 | |
| 8325 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 8326 | if (error) |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 8327 | return error; |
| 8328 | |
| 8329 | seq = file->private_data; |
| 8330 | seq->poll_event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8331 | return error; |
| 8332 | } |
| 8333 | |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 8334 | static int md_unloading; |
Al Viro | afc9a42 | 2017-07-03 06:39:46 -0400 | [diff] [blame] | 8335 | static __poll_t mdstat_poll(struct file *filp, poll_table *wait) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 8336 | { |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 8337 | struct seq_file *seq = filp->private_data; |
Al Viro | afc9a42 | 2017-07-03 06:39:46 -0400 | [diff] [blame] | 8338 | __poll_t mask; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 8339 | |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 8340 | if (md_unloading) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 8341 | return EPOLLIN|EPOLLRDNORM|EPOLLERR|EPOLLPRI; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 8342 | poll_wait(filp, &md_event_waiters, wait); |
| 8343 | |
| 8344 | /* always allow read */ |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 8345 | mask = EPOLLIN | EPOLLRDNORM; |
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 | if (seq->poll_event != atomic_read(&md_event_count)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 8348 | mask |= EPOLLERR | EPOLLPRI; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 8349 | return mask; |
| 8350 | } |
| 8351 | |
Alexey Dobriyan | 97a3253 | 2020-02-03 17:37:17 -0800 | [diff] [blame] | 8352 | static const struct proc_ops mdstat_proc_ops = { |
| 8353 | .proc_open = md_seq_open, |
| 8354 | .proc_read = seq_read, |
| 8355 | .proc_lseek = seq_lseek, |
| 8356 | .proc_release = seq_release, |
| 8357 | .proc_poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8358 | }; |
| 8359 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 8360 | int register_md_personality(struct md_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8361 | { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8362 | pr_debug("md: %s personality registered for level %d\n", |
| 8363 | p->name, p->level); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8364 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8365 | list_add_tail(&p->list, &pers_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8366 | spin_unlock(&pers_lock); |
| 8367 | return 0; |
| 8368 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 8369 | EXPORT_SYMBOL(register_md_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8370 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 8371 | int unregister_md_personality(struct md_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8372 | { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8373 | pr_debug("md: %s personality unregistered\n", p->name); |
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_del_init(&p->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(unregister_md_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8380 | |
NeilBrown | 6022e75 | 2015-08-13 12:32:55 +1000 | [diff] [blame] | 8381 | int register_md_cluster_operations(struct md_cluster_operations *ops, |
| 8382 | struct module *module) |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 8383 | { |
NeilBrown | 6022e75 | 2015-08-13 12:32:55 +1000 | [diff] [blame] | 8384 | int ret = 0; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 8385 | spin_lock(&pers_lock); |
NeilBrown | 6022e75 | 2015-08-13 12:32:55 +1000 | [diff] [blame] | 8386 | if (md_cluster_ops != NULL) |
| 8387 | ret = -EALREADY; |
| 8388 | else { |
| 8389 | md_cluster_ops = ops; |
| 8390 | md_cluster_mod = module; |
| 8391 | } |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 8392 | spin_unlock(&pers_lock); |
NeilBrown | 6022e75 | 2015-08-13 12:32:55 +1000 | [diff] [blame] | 8393 | return ret; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 8394 | } |
| 8395 | EXPORT_SYMBOL(register_md_cluster_operations); |
| 8396 | |
| 8397 | int unregister_md_cluster_operations(void) |
| 8398 | { |
| 8399 | spin_lock(&pers_lock); |
| 8400 | md_cluster_ops = NULL; |
| 8401 | spin_unlock(&pers_lock); |
| 8402 | return 0; |
| 8403 | } |
| 8404 | EXPORT_SYMBOL(unregister_md_cluster_operations); |
| 8405 | |
| 8406 | int md_setup_cluster(struct mddev *mddev, int nodes) |
| 8407 | { |
Zhao Heming | 7c9d5c5 | 2020-07-21 02:08:52 +0800 | [diff] [blame] | 8408 | int ret; |
Guoqing Jiang | 47a7b0d | 2016-09-04 22:17:28 -0400 | [diff] [blame] | 8409 | if (!md_cluster_ops) |
| 8410 | request_module("md-cluster"); |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 8411 | spin_lock(&pers_lock); |
Guoqing Jiang | 47a7b0d | 2016-09-04 22:17:28 -0400 | [diff] [blame] | 8412 | /* ensure module won't be unloaded */ |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 8413 | if (!md_cluster_ops || !try_module_get(md_cluster_mod)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8414 | 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] | 8415 | spin_unlock(&pers_lock); |
| 8416 | return -ENOENT; |
| 8417 | } |
| 8418 | spin_unlock(&pers_lock); |
| 8419 | |
Zhao Heming | 7c9d5c5 | 2020-07-21 02:08:52 +0800 | [diff] [blame] | 8420 | ret = md_cluster_ops->join(mddev, nodes); |
| 8421 | if (!ret) |
| 8422 | mddev->safemode_delay = 0; |
| 8423 | return ret; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 8424 | } |
| 8425 | |
| 8426 | void md_cluster_stop(struct mddev *mddev) |
| 8427 | { |
Goldwyn Rodrigues | c4ce867 | 2014-03-29 10:20:02 -0500 | [diff] [blame] | 8428 | if (!md_cluster_ops) |
| 8429 | return; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 8430 | md_cluster_ops->leave(mddev); |
| 8431 | module_put(md_cluster_mod); |
| 8432 | } |
| 8433 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8434 | static int is_mddev_idle(struct mddev *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8435 | { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 8436 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8437 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8438 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8439 | |
| 8440 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 8441 | rcu_read_lock(); |
| 8442 | rdev_for_each_rcu(rdev, mddev) { |
Christoph Hellwig | 4245e52 | 2020-09-03 07:40:59 +0200 | [diff] [blame] | 8443 | struct gendisk *disk = rdev->bdev->bd_disk; |
Michael Callahan | 59767fb | 2018-07-18 04:47:37 -0700 | [diff] [blame] | 8444 | curr_events = (int)part_stat_read_accum(&disk->part0, sectors) - |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8445 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 8446 | /* sync IO will cause sync_io to increase before the disk_stats |
| 8447 | * as sync_io is counted when a request starts, and |
| 8448 | * disk_stats is counted when it completes. |
| 8449 | * So resync activity will cause curr_events to be smaller than |
| 8450 | * when there was no such activity. |
| 8451 | * non-sync IO will cause disk_stat to increase without |
| 8452 | * increasing sync_io so curr_events will (eventually) |
| 8453 | * be larger than it was before. Once it becomes |
| 8454 | * substantially larger, the test below will cause |
| 8455 | * the array to appear non-idle, and resync will slow |
| 8456 | * down. |
| 8457 | * If there is a lot of outstanding resync activity when |
| 8458 | * we set last_event to curr_events, then all that activity |
| 8459 | * completing might cause the array to appear non-idle |
| 8460 | * and resync will be slowed down even though there might |
| 8461 | * not have been non-resync activity. This will only |
| 8462 | * happen once though. 'last_events' will soon reflect |
| 8463 | * the state where there is little or no outstanding |
| 8464 | * resync requests, and further resync activity will |
| 8465 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 8466 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8467 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8468 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8469 | rdev->last_events = curr_events; |
| 8470 | idle = 0; |
| 8471 | } |
| 8472 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 8473 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8474 | return idle; |
| 8475 | } |
| 8476 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8477 | void md_done_sync(struct mddev *mddev, int blocks, int ok) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8478 | { |
| 8479 | /* another "blocks" (512byte) blocks have been synced */ |
| 8480 | atomic_sub(blocks, &mddev->recovery_active); |
| 8481 | wake_up(&mddev->recovery_wait); |
| 8482 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 8483 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
majianpeng | 0a19caa | 2012-11-19 19:57:34 +0800 | [diff] [blame] | 8484 | set_bit(MD_RECOVERY_ERROR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8485 | md_wakeup_thread(mddev->thread); |
| 8486 | // stop recovery, signal do_sync .... |
| 8487 | } |
| 8488 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 8489 | EXPORT_SYMBOL(md_done_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8490 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 8491 | /* md_write_start(mddev, bi) |
| 8492 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 8493 | * in superblock) before writing, schedule a superblock update |
| 8494 | * and wait for it to complete. |
NeilBrown | cc27b0c | 2017-06-05 16:49:39 +1000 | [diff] [blame] | 8495 | * A return value of 'false' means that the write wasn't recorded |
| 8496 | * and cannot proceed as the array is being suspend. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 8497 | */ |
NeilBrown | cc27b0c | 2017-06-05 16:49:39 +1000 | [diff] [blame] | 8498 | bool md_write_start(struct mddev *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8499 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 8500 | int did_change = 0; |
Heinz Mauelshagen | 4b6c106 | 2018-02-02 23:13:19 +0100 | [diff] [blame] | 8501 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 8502 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | cc27b0c | 2017-06-05 16:49:39 +1000 | [diff] [blame] | 8503 | return true; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 8504 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8505 | BUG_ON(mddev->ro == 1); |
| 8506 | if (mddev->ro == 2) { |
| 8507 | /* need to switch to read/write */ |
| 8508 | mddev->ro = 0; |
| 8509 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 8510 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 8511 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 8512 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8513 | } |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 8514 | rcu_read_lock(); |
| 8515 | percpu_ref_get(&mddev->writes_pending); |
NeilBrown | 55cc39f | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 8516 | smp_mb(); /* Match smp_mb in set_in_sync() */ |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 8517 | if (mddev->safemode == 1) |
| 8518 | mddev->safemode = 0; |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 8519 | /* sync_checkers is always 0 when writes_pending is in per-cpu mode */ |
NeilBrown | 81fe48e | 2017-08-08 16:56:36 +1000 | [diff] [blame] | 8520 | if (mddev->in_sync || mddev->sync_checkers) { |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 8521 | spin_lock(&mddev->lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 8522 | if (mddev->in_sync) { |
| 8523 | mddev->in_sync = 0; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 8524 | set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags); |
| 8525 | set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 8526 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 8527 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 8528 | } |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 8529 | spin_unlock(&mddev->lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 8530 | } |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 8531 | rcu_read_unlock(); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 8532 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 8533 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Heinz Mauelshagen | 4b6c106 | 2018-02-02 23:13:19 +0100 | [diff] [blame] | 8534 | if (!mddev->has_superblocks) |
| 8535 | return true; |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 8536 | wait_event(mddev->sb_wait, |
NeilBrown | d47c8ad | 2017-10-05 16:23:16 +1100 | [diff] [blame] | 8537 | !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags) || |
| 8538 | mddev->suspended); |
NeilBrown | cc27b0c | 2017-06-05 16:49:39 +1000 | [diff] [blame] | 8539 | if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) { |
| 8540 | percpu_ref_put(&mddev->writes_pending); |
| 8541 | return false; |
| 8542 | } |
| 8543 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8544 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 8545 | EXPORT_SYMBOL(md_write_start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8546 | |
NeilBrown | 4972805 | 2017-03-15 14:05:12 +1100 | [diff] [blame] | 8547 | /* md_write_inc can only be called when md_write_start() has |
| 8548 | * already been called at least once of the current request. |
| 8549 | * It increments the counter and is useful when a single request |
| 8550 | * is split into several parts. Each part causes an increment and |
| 8551 | * so needs a matching md_write_end(). |
| 8552 | * Unlike md_write_start(), it is safe to call md_write_inc() inside |
| 8553 | * a spinlocked region. |
| 8554 | */ |
| 8555 | void md_write_inc(struct mddev *mddev, struct bio *bi) |
| 8556 | { |
| 8557 | if (bio_data_dir(bi) != WRITE) |
| 8558 | return; |
| 8559 | WARN_ON_ONCE(mddev->in_sync || mddev->ro); |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 8560 | percpu_ref_get(&mddev->writes_pending); |
NeilBrown | 4972805 | 2017-03-15 14:05:12 +1100 | [diff] [blame] | 8561 | } |
| 8562 | EXPORT_SYMBOL(md_write_inc); |
| 8563 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8564 | void md_write_end(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8565 | { |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 8566 | percpu_ref_put(&mddev->writes_pending); |
| 8567 | |
| 8568 | if (mddev->safemode == 2) |
| 8569 | md_wakeup_thread(mddev->thread); |
| 8570 | else if (mddev->safemode_delay) |
| 8571 | /* The roundup() ensures this only performs locking once |
| 8572 | * every ->safemode_delay jiffies |
| 8573 | */ |
| 8574 | mod_timer(&mddev->safemode_timer, |
| 8575 | roundup(jiffies, mddev->safemode_delay) + |
| 8576 | mddev->safemode_delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8577 | } |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 8578 | |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 8579 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8580 | |
Xiao Ni | 2628089 | 2020-08-25 13:42:59 +0800 | [diff] [blame] | 8581 | /* This is used by raid0 and raid10 */ |
| 8582 | void md_submit_discard_bio(struct mddev *mddev, struct md_rdev *rdev, |
| 8583 | struct bio *bio, sector_t start, sector_t size) |
| 8584 | { |
| 8585 | struct bio *discard_bio = NULL; |
| 8586 | |
| 8587 | if (__blkdev_issue_discard(rdev->bdev, start, size, |
| 8588 | GFP_NOIO, 0, &discard_bio) || !discard_bio) |
| 8589 | return; |
| 8590 | |
| 8591 | bio_chain(discard_bio, bio); |
| 8592 | bio_clone_blkg_association(discard_bio, bio); |
| 8593 | if (mddev->gendisk) |
| 8594 | trace_block_bio_remap(bdev_get_queue(rdev->bdev), |
| 8595 | discard_bio, disk_devt(mddev->gendisk), |
| 8596 | bio->bi_iter.bi_sector); |
| 8597 | submit_bio_noacct(discard_bio); |
| 8598 | } |
| 8599 | EXPORT_SYMBOL(md_submit_discard_bio); |
| 8600 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8601 | /* md_allow_write(mddev) |
| 8602 | * Calling this ensures that the array is marked 'active' so that writes |
| 8603 | * may proceed without blocking. It is important to call this before |
| 8604 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 8605 | * Must be called with mddev_lock held. |
| 8606 | */ |
Artur Paszkiewicz | 2214c26 | 2017-05-08 11:56:55 +0200 | [diff] [blame] | 8607 | void md_allow_write(struct mddev *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8608 | { |
| 8609 | if (!mddev->pers) |
Artur Paszkiewicz | 2214c26 | 2017-05-08 11:56:55 +0200 | [diff] [blame] | 8610 | return; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8611 | if (mddev->ro) |
Artur Paszkiewicz | 2214c26 | 2017-05-08 11:56:55 +0200 | [diff] [blame] | 8612 | return; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 8613 | if (!mddev->pers->sync_request) |
Artur Paszkiewicz | 2214c26 | 2017-05-08 11:56:55 +0200 | [diff] [blame] | 8614 | return; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8615 | |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 8616 | spin_lock(&mddev->lock); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8617 | if (mddev->in_sync) { |
| 8618 | mddev->in_sync = 0; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 8619 | set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags); |
| 8620 | set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8621 | if (mddev->safemode_delay && |
| 8622 | mddev->safemode == 0) |
| 8623 | mddev->safemode = 1; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 8624 | spin_unlock(&mddev->lock); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8625 | md_update_sb(mddev, 0); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 8626 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Artur Paszkiewicz | 2214c26 | 2017-05-08 11:56:55 +0200 | [diff] [blame] | 8627 | /* wait for the dirty state to be recorded in the metadata */ |
| 8628 | wait_event(mddev->sb_wait, |
Artur Paszkiewicz | 2214c26 | 2017-05-08 11:56:55 +0200 | [diff] [blame] | 8629 | !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8630 | } else |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 8631 | spin_unlock(&mddev->lock); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8632 | } |
| 8633 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 8634 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8635 | #define SYNC_MARKS 10 |
| 8636 | #define SYNC_MARK_STEP (3*HZ) |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 8637 | #define UPDATE_FREQUENCY (5*60*HZ) |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 8638 | void md_do_sync(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8639 | { |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 8640 | struct mddev *mddev = thread->mddev; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8641 | struct mddev *mddev2; |
Yufen Yu | e5b521e | 2019-06-14 15:41:07 -0700 | [diff] [blame] | 8642 | unsigned int currspeed = 0, window; |
Xiao Ni | ac7e50a | 2014-08-07 09:37:41 -0400 | [diff] [blame] | 8643 | sector_t max_sectors,j, io_sectors, recovery_done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8644 | unsigned long mark[SYNC_MARKS]; |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 8645 | unsigned long update_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8646 | sector_t mark_cnt[SYNC_MARKS]; |
| 8647 | int last_mark,m; |
| 8648 | struct list_head *tmp; |
| 8649 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8650 | int skipped = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8651 | struct md_rdev *rdev; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 8652 | char *desc, *action = NULL; |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 8653 | struct blk_plug plug; |
Guoqing Jiang | 41a9a0d | 2016-05-02 11:33:08 -0400 | [diff] [blame] | 8654 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8655 | |
| 8656 | /* just incase thread restarts... */ |
Song Liu | d5d885f | 2017-11-19 22:17:01 -0800 | [diff] [blame] | 8657 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
| 8658 | test_bit(MD_RECOVERY_WAIT, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8659 | return; |
NeilBrown | 3991b31 | 2014-05-28 13:39:23 +1000 | [diff] [blame] | 8660 | if (mddev->ro) {/* never try to sync a read-only array */ |
| 8661 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8662 | return; |
NeilBrown | 3991b31 | 2014-05-28 13:39:23 +1000 | [diff] [blame] | 8663 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8664 | |
Guoqing Jiang | 41a9a0d | 2016-05-02 11:33:08 -0400 | [diff] [blame] | 8665 | if (mddev_is_clustered(mddev)) { |
| 8666 | ret = md_cluster_ops->resync_start(mddev); |
| 8667 | if (ret) |
| 8668 | goto skip; |
| 8669 | |
Guoqing Jiang | bb8bf15 | 2016-06-02 23:32:04 -0400 | [diff] [blame] | 8670 | set_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags); |
Guoqing Jiang | 41a9a0d | 2016-05-02 11:33:08 -0400 | [diff] [blame] | 8671 | if (!(test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 8672 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) || |
| 8673 | test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
| 8674 | && ((unsigned long long)mddev->curr_resync_completed |
| 8675 | < (unsigned long long)mddev->resync_max_sectors)) |
| 8676 | goto skip; |
| 8677 | } |
| 8678 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 8679 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 8680 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 8681 | desc = "data-check"; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 8682 | action = "check"; |
| 8683 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 8684 | desc = "requested-resync"; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 8685 | action = "repair"; |
| 8686 | } else |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 8687 | desc = "resync"; |
| 8688 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 8689 | desc = "reshape"; |
| 8690 | else |
| 8691 | desc = "recovery"; |
| 8692 | |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 8693 | mddev->last_sync_action = action ?: desc; |
| 8694 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8695 | /* we overload curr_resync somewhat here. |
| 8696 | * 0 == not engaged in resync at all |
| 8697 | * 2 == checking that there is no conflict with another sync |
| 8698 | * 1 == like 2, but have yielded to allow conflicting resync to |
Yufen Yu | e5b521e | 2019-06-14 15:41:07 -0700 | [diff] [blame] | 8699 | * commence |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8700 | * other == active in resync - this many blocks |
| 8701 | * |
| 8702 | * Before starting a resync we must have set curr_resync to |
| 8703 | * 2, and then checked that every "conflicting" array has curr_resync |
| 8704 | * less than ours. When we find one that is the same or higher |
| 8705 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 8706 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 8707 | * This will mean we have to start checking from the beginning again. |
| 8708 | * |
| 8709 | */ |
| 8710 | |
| 8711 | do { |
Artur Paszkiewicz | c622ca5 | 2016-08-16 14:26:08 +0200 | [diff] [blame] | 8712 | int mddev2_minor = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8713 | mddev->curr_resync = 2; |
| 8714 | |
| 8715 | try_again: |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 8716 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8717 | goto skip; |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 8718 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8719 | if (mddev2 == mddev) |
| 8720 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 8721 | if (!mddev->parallel_resync |
| 8722 | && mddev2->curr_resync |
| 8723 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8724 | DEFINE_WAIT(wq); |
| 8725 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 8726 | /* arbitrarily yield */ |
| 8727 | mddev->curr_resync = 1; |
| 8728 | wake_up(&resync_wait); |
| 8729 | } |
| 8730 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 8731 | /* no need to wait here, we can wait the next |
| 8732 | * time 'round when curr_resync == 2 |
| 8733 | */ |
| 8734 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 8735 | /* We need to wait 'interruptible' so as not to |
| 8736 | * contribute to the load average, and not to |
| 8737 | * be caught by 'softlockup' |
| 8738 | */ |
| 8739 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8740 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 8741 | mddev2->curr_resync >= mddev->curr_resync) { |
Artur Paszkiewicz | c622ca5 | 2016-08-16 14:26:08 +0200 | [diff] [blame] | 8742 | if (mddev2_minor != mddev2->md_minor) { |
| 8743 | mddev2_minor = mddev2->md_minor; |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8744 | pr_info("md: delaying %s of %s until %s has finished (they share one or more physical units)\n", |
| 8745 | desc, mdname(mddev), |
| 8746 | mdname(mddev2)); |
Artur Paszkiewicz | c622ca5 | 2016-08-16 14:26:08 +0200 | [diff] [blame] | 8747 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8748 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 8749 | if (signal_pending(current)) |
| 8750 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8751 | schedule(); |
| 8752 | finish_wait(&resync_wait, &wq); |
| 8753 | goto try_again; |
| 8754 | } |
| 8755 | finish_wait(&resync_wait, &wq); |
| 8756 | } |
| 8757 | } |
| 8758 | } while (mddev->curr_resync < 2); |
| 8759 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8760 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8761 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8762 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8763 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8764 | */ |
| 8765 | max_sectors = mddev->resync_max_sectors; |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 8766 | atomic64_set(&mddev->resync_mismatches, 0); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8767 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 8768 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 8769 | j = mddev->resync_min; |
| 8770 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8771 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 8772 | |
Guoqing Jiang | cb9ee15 | 2018-10-18 16:37:47 +0800 | [diff] [blame] | 8773 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) { |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 8774 | max_sectors = mddev->resync_max_sectors; |
Guoqing Jiang | cb9ee15 | 2018-10-18 16:37:47 +0800 | [diff] [blame] | 8775 | /* |
| 8776 | * If the original node aborts reshaping then we continue the |
| 8777 | * reshaping, so set j again to avoid restart reshape from the |
| 8778 | * first beginning |
| 8779 | */ |
| 8780 | if (mddev_is_clustered(mddev) && |
| 8781 | mddev->reshape_position != MaxSector) |
| 8782 | j = mddev->reshape_position; |
| 8783 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8784 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8785 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8786 | j = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 8787 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 8788 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8789 | if (rdev->raid_disk >= 0 && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 8790 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8791 | !test_bit(Faulty, &rdev->flags) && |
| 8792 | !test_bit(In_sync, &rdev->flags) && |
| 8793 | rdev->recovery_offset < j) |
| 8794 | j = rdev->recovery_offset; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 8795 | rcu_read_unlock(); |
NeilBrown | 133d452 | 2014-07-02 12:04:14 +1000 | [diff] [blame] | 8796 | |
| 8797 | /* If there is a bitmap, we need to make sure all |
| 8798 | * writes that started before we added a spare |
| 8799 | * complete before we start doing a recovery. |
| 8800 | * Otherwise the write might complete and (via |
| 8801 | * bitmap_endwrite) set a bit in the bitmap after the |
| 8802 | * recovery has checked that bit and skipped that |
| 8803 | * region. |
| 8804 | */ |
| 8805 | if (mddev->bitmap) { |
| 8806 | mddev->pers->quiesce(mddev, 1); |
| 8807 | mddev->pers->quiesce(mddev, 0); |
| 8808 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8809 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8810 | |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8811 | pr_info("md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 8812 | pr_debug("md: minimum _guaranteed_ speed: %d KB/sec/disk.\n", speed_min(mddev)); |
| 8813 | pr_debug("md: using maximum available idle IO bandwidth (but not more than %d KB/sec) for %s.\n", |
| 8814 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8815 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8816 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8817 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8818 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8819 | for (m = 0; m < SYNC_MARKS; m++) { |
| 8820 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8821 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8822 | } |
| 8823 | last_mark = 0; |
| 8824 | mddev->resync_mark = mark[last_mark]; |
| 8825 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 8826 | |
| 8827 | /* |
| 8828 | * Tune reconstruction: |
| 8829 | */ |
Yufen Yu | e5b521e | 2019-06-14 15:41:07 -0700 | [diff] [blame] | 8830 | window = 32 * (PAGE_SIZE / 512); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8831 | pr_debug("md: using %dk window, over a total of %lluk.\n", |
| 8832 | window/2, (unsigned long long)max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8833 | |
| 8834 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8835 | last_check = 0; |
| 8836 | |
| 8837 | if (j>2) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8838 | pr_debug("md: resuming %s of %s from checkpoint.\n", |
| 8839 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8840 | mddev->curr_resync = j; |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 8841 | } else |
| 8842 | mddev->curr_resync = 3; /* no longer delayed */ |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 8843 | mddev->curr_resync_completed = j; |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 8844 | sysfs_notify_dirent_safe(mddev->sysfs_completed); |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 8845 | md_new_event(mddev); |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 8846 | update_time = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8847 | |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 8848 | blk_start_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8849 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8850 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8851 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8852 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8853 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 8854 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 8855 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 8856 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 8857 | > (max_sectors >> 4)) || |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 8858 | time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) || |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 8859 | (j - mddev->curr_resync_completed)*2 |
NeilBrown | c5e19d9 | 2015-07-17 12:06:02 +1000 | [diff] [blame] | 8860 | >= mddev->resync_max - mddev->curr_resync_completed || |
| 8861 | mddev->curr_resync_completed > mddev->resync_max |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 8862 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8863 | /* time to update curr_resync_completed */ |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8864 | wait_event(mddev->recovery_wait, |
| 8865 | atomic_read(&mddev->recovery_active) == 0); |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 8866 | mddev->curr_resync_completed = j; |
kernelmail | 35d78c6 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 8867 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && |
| 8868 | j > mddev->recovery_cp) |
| 8869 | mddev->recovery_cp = j; |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 8870 | update_time = jiffies; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 8871 | set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags); |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 8872 | sysfs_notify_dirent_safe(mddev->sysfs_completed); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8873 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 8874 | |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8875 | while (j >= mddev->resync_max && |
| 8876 | !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 8877 | /* As this condition is controlled by user-space, |
| 8878 | * we can block indefinitely, so use '_interruptible' |
| 8879 | * to avoid triggering warnings. |
| 8880 | */ |
| 8881 | flush_signals(current); /* just in case */ |
| 8882 | wait_event_interruptible(mddev->recovery_wait, |
| 8883 | mddev->resync_max > j |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8884 | || test_bit(MD_RECOVERY_INTR, |
| 8885 | &mddev->recovery)); |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 8886 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 8887 | |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8888 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 8889 | break; |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 8890 | |
NeilBrown | 0931479 | 2015-02-19 16:04:40 +1100 | [diff] [blame] | 8891 | sectors = mddev->pers->sync_request(mddev, j, &skipped); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8892 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 8893 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8894 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8895 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8896 | |
| 8897 | if (!skipped) { /* actual IO requested */ |
| 8898 | io_sectors += sectors; |
| 8899 | atomic_add(sectors, &mddev->recovery_active); |
| 8900 | } |
| 8901 | |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 8902 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 8903 | break; |
| 8904 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8905 | j += sectors; |
NeilBrown | 5ed1df2 | 2015-07-24 13:27:08 +1000 | [diff] [blame] | 8906 | if (j > max_sectors) |
| 8907 | /* when skipping, extra large numbers can be returned. */ |
| 8908 | j = max_sectors; |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 8909 | if (j > 2) |
| 8910 | mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 8911 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 8912 | if (last_check == 0) |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 8913 | /* this is the earliest that rebuild will be |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 8914 | * visible in /proc/mdstat |
| 8915 | */ |
| 8916 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8917 | |
| 8918 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8919 | continue; |
| 8920 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8921 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8922 | repeat: |
| 8923 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 8924 | /* step marks */ |
| 8925 | int next = (last_mark+1) % SYNC_MARKS; |
| 8926 | |
| 8927 | mddev->resync_mark = mark[next]; |
| 8928 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 8929 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8930 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8931 | last_mark = next; |
| 8932 | } |
| 8933 | |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8934 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 8935 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8936 | |
| 8937 | /* |
| 8938 | * this loop exits only if either when we are slower than |
| 8939 | * the 'hard' speed limit, or the system was IO-idle for |
| 8940 | * a jiffy. |
| 8941 | * the system might be non-idle CPU-wise, but we only care |
| 8942 | * about not overloading the IO subsystem. (things like an |
| 8943 | * e2fsck being done on the RAID array should execute fast) |
| 8944 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8945 | cond_resched(); |
| 8946 | |
Xiao Ni | ac7e50a | 2014-08-07 09:37:41 -0400 | [diff] [blame] | 8947 | recovery_done = io_sectors - atomic_read(&mddev->recovery_active); |
| 8948 | currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2 |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8949 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8950 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 8951 | if (currspeed > speed_min(mddev)) { |
NeilBrown | ac8fa41 | 2015-02-19 16:55:00 +1100 | [diff] [blame] | 8952 | if (currspeed > speed_max(mddev)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 8953 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8954 | goto repeat; |
| 8955 | } |
NeilBrown | ac8fa41 | 2015-02-19 16:55:00 +1100 | [diff] [blame] | 8956 | if (!is_mddev_idle(mddev, 0)) { |
| 8957 | /* |
| 8958 | * Give other IO more of a chance. |
| 8959 | * The faster the devices, the less we wait. |
| 8960 | */ |
| 8961 | wait_event(mddev->recovery_wait, |
| 8962 | !atomic_read(&mddev->recovery_active)); |
| 8963 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8964 | } |
| 8965 | } |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8966 | pr_info("md: %s: %s %s.\n",mdname(mddev), desc, |
| 8967 | test_bit(MD_RECOVERY_INTR, &mddev->recovery) |
| 8968 | ? "interrupted" : "done"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8969 | /* |
| 8970 | * this also signals 'finished resyncing' to md_stop |
| 8971 | */ |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 8972 | blk_finish_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8973 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 8974 | |
NeilBrown | 5ed1df2 | 2015-07-24 13:27:08 +1000 | [diff] [blame] | 8975 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 8976 | !test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
NeilBrown | 1217e1d | 2016-10-28 15:59:41 +1100 | [diff] [blame] | 8977 | mddev->curr_resync > 3) { |
NeilBrown | 5ed1df2 | 2015-07-24 13:27:08 +1000 | [diff] [blame] | 8978 | mddev->curr_resync_completed = mddev->curr_resync; |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 8979 | sysfs_notify_dirent_safe(mddev->sysfs_completed); |
NeilBrown | 5ed1df2 | 2015-07-24 13:27:08 +1000 | [diff] [blame] | 8980 | } |
NeilBrown | 0931479 | 2015-02-19 16:04:40 +1100 | [diff] [blame] | 8981 | mddev->pers->sync_request(mddev, max_sectors, &skipped); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8982 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 8983 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 1217e1d | 2016-10-28 15:59:41 +1100 | [diff] [blame] | 8984 | mddev->curr_resync > 3) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8985 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 8986 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 8987 | if (mddev->curr_resync >= mddev->recovery_cp) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8988 | pr_debug("md: checkpointing %s of %s.\n", |
| 8989 | desc, mdname(mddev)); |
majianpeng | 0a19caa | 2012-11-19 19:57:34 +0800 | [diff] [blame] | 8990 | if (test_bit(MD_RECOVERY_ERROR, |
| 8991 | &mddev->recovery)) |
| 8992 | mddev->recovery_cp = |
| 8993 | mddev->curr_resync_completed; |
| 8994 | else |
| 8995 | mddev->recovery_cp = |
| 8996 | mddev->curr_resync; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8997 | } |
| 8998 | } else |
| 8999 | mddev->recovery_cp = MaxSector; |
| 9000 | } else { |
| 9001 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 9002 | mddev->curr_resync = MaxSector; |
NeilBrown | db0505d | 2017-10-17 16:18:36 +1100 | [diff] [blame] | 9003 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 9004 | test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) { |
| 9005 | rcu_read_lock(); |
| 9006 | rdev_for_each_rcu(rdev, mddev) |
| 9007 | if (rdev->raid_disk >= 0 && |
| 9008 | mddev->delta_disks >= 0 && |
| 9009 | !test_bit(Journal, &rdev->flags) && |
| 9010 | !test_bit(Faulty, &rdev->flags) && |
| 9011 | !test_bit(In_sync, &rdev->flags) && |
| 9012 | rdev->recovery_offset < mddev->curr_resync) |
| 9013 | rdev->recovery_offset = mddev->curr_resync; |
| 9014 | rcu_read_unlock(); |
| 9015 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 9016 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9017 | } |
NeilBrown | db91ff5 | 2012-02-07 12:01:51 +1100 | [diff] [blame] | 9018 | skip: |
Guoqing Jiang | bb8bf15 | 2016-06-02 23:32:04 -0400 | [diff] [blame] | 9019 | /* set CHANGE_PENDING here since maybe another update is needed, |
| 9020 | * so other nodes are informed. It should be harmless for normal |
| 9021 | * raid */ |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 9022 | set_mask_bits(&mddev->sb_flags, 0, |
| 9023 | BIT(MD_SB_CHANGE_PENDING) | BIT(MD_SB_CHANGE_DEVS)); |
Goldwyn Rodrigues | c186b12 | 2015-09-30 13:20:35 -0500 | [diff] [blame] | 9024 | |
BingJing Chang | 8876391 | 2018-02-22 13:34:46 +0800 | [diff] [blame] | 9025 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 9026 | !test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 9027 | mddev->delta_disks > 0 && |
| 9028 | mddev->pers->finish_reshape && |
| 9029 | mddev->pers->size && |
| 9030 | mddev->queue) { |
| 9031 | mddev_lock_nointr(mddev); |
| 9032 | md_set_array_sectors(mddev, mddev->pers->size(mddev, 0, 0)); |
| 9033 | mddev_unlock(mddev); |
Christoph Hellwig | 2c247c5 | 2020-11-16 15:57:11 +0100 | [diff] [blame] | 9034 | if (!mddev_is_clustered(mddev)) |
| 9035 | set_capacity_and_notify(mddev->gendisk, |
| 9036 | mddev->array_sectors); |
BingJing Chang | 8876391 | 2018-02-22 13:34:46 +0800 | [diff] [blame] | 9037 | } |
| 9038 | |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 9039 | spin_lock(&mddev->lock); |
NeilBrown | c07b70a | 2009-12-14 12:49:48 +1100 | [diff] [blame] | 9040 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 9041 | /* We completed so min/max setting can be forgotten if used. */ |
| 9042 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 9043 | mddev->resync_min = 0; |
| 9044 | mddev->resync_max = MaxSector; |
| 9045 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 9046 | mddev->resync_min = mddev->curr_resync_completed; |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 9047 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9048 | mddev->curr_resync = 0; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 9049 | spin_unlock(&mddev->lock); |
| 9050 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9051 | wake_up(&resync_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9052 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 9053 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9054 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 9055 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9056 | |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 9057 | static int remove_and_add_spares(struct mddev *mddev, |
| 9058 | struct md_rdev *this) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9059 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 9060 | struct md_rdev *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9061 | int spares = 0; |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 9062 | int removed = 0; |
NeilBrown | d787be4 | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 9063 | bool remove_some = false; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9064 | |
NeilBrown | 39772f0 | 2018-02-03 09:19:30 +1100 | [diff] [blame] | 9065 | if (this && test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 9066 | /* Mustn't remove devices when resync thread is running */ |
| 9067 | return 0; |
| 9068 | |
NeilBrown | d787be4 | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 9069 | rdev_for_each(rdev, mddev) { |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 9070 | if ((this == NULL || rdev == this) && |
| 9071 | rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 9072 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | d787be4 | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 9073 | test_bit(Faulty, &rdev->flags) && |
| 9074 | atomic_read(&rdev->nr_pending)==0) { |
| 9075 | /* Faulty non-Blocked devices with nr_pending == 0 |
| 9076 | * never get nr_pending incremented, |
| 9077 | * never get Faulty cleared, and never get Blocked set. |
| 9078 | * So we can synchronize_rcu now rather than once per device |
| 9079 | */ |
| 9080 | remove_some = true; |
| 9081 | set_bit(RemoveSynchronized, &rdev->flags); |
| 9082 | } |
| 9083 | } |
| 9084 | |
| 9085 | if (remove_some) |
| 9086 | synchronize_rcu(); |
| 9087 | rdev_for_each(rdev, mddev) { |
| 9088 | if ((this == NULL || rdev == this) && |
| 9089 | rdev->raid_disk >= 0 && |
| 9090 | !test_bit(Blocked, &rdev->flags) && |
| 9091 | ((test_bit(RemoveSynchronized, &rdev->flags) || |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 9092 | (!test_bit(In_sync, &rdev->flags) && |
| 9093 | !test_bit(Journal, &rdev->flags))) && |
NeilBrown | d787be4 | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 9094 | atomic_read(&rdev->nr_pending)==0)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9095 | if (mddev->pers->hot_remove_disk( |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 9096 | mddev, rdev) == 0) { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 9097 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | 011abdc | 2018-04-26 14:46:29 +1000 | [diff] [blame] | 9098 | rdev->saved_raid_disk = rdev->raid_disk; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9099 | rdev->raid_disk = -1; |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 9100 | removed++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9101 | } |
| 9102 | } |
NeilBrown | d787be4 | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 9103 | if (remove_some && test_bit(RemoveSynchronized, &rdev->flags)) |
| 9104 | clear_bit(RemoveSynchronized, &rdev->flags); |
| 9105 | } |
| 9106 | |
Jonathan Brassow | 90584fc | 2013-03-07 16:24:26 -0600 | [diff] [blame] | 9107 | if (removed && mddev->kobj.sd) |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 9108 | sysfs_notify_dirent_safe(mddev->sysfs_degraded); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9109 | |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 9110 | if (this && removed) |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 9111 | goto no_add; |
| 9112 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 9113 | rdev_for_each(rdev, mddev) { |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 9114 | if (this && this != rdev) |
| 9115 | continue; |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 9116 | if (test_bit(Candidate, &rdev->flags)) |
| 9117 | continue; |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 9118 | if (rdev->raid_disk >= 0 && |
| 9119 | !test_bit(In_sync, &rdev->flags) && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 9120 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 9121 | !test_bit(Faulty, &rdev->flags)) |
| 9122 | spares++; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 9123 | if (rdev->raid_disk >= 0) |
| 9124 | continue; |
| 9125 | if (test_bit(Faulty, &rdev->flags)) |
| 9126 | continue; |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 9127 | if (!test_bit(Journal, &rdev->flags)) { |
| 9128 | if (mddev->ro && |
| 9129 | ! (rdev->saved_raid_disk >= 0 && |
| 9130 | !test_bit(Bitmap_sync, &rdev->flags))) |
| 9131 | continue; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 9132 | |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 9133 | rdev->recovery_offset = 0; |
| 9134 | } |
Guoqing Jiang | 3f79cc2 | 2020-04-04 23:57:11 +0200 | [diff] [blame] | 9135 | if (mddev->pers->hot_add_disk(mddev, rdev) == 0) { |
Damien Le Moal | 5e3b8a8 | 2020-07-16 13:54:40 +0900 | [diff] [blame] | 9136 | /* failure here is OK */ |
| 9137 | sysfs_link_rdev(mddev, rdev); |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 9138 | if (!test_bit(Journal, &rdev->flags)) |
| 9139 | spares++; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 9140 | md_new_event(mddev); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 9141 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 9142 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9143 | } |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 9144 | no_add: |
NeilBrown | 6dafab6 | 2012-09-19 12:54:22 +1000 | [diff] [blame] | 9145 | if (removed) |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 9146 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9147 | return spares; |
| 9148 | } |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 9149 | |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 9150 | static void md_start_sync(struct work_struct *ws) |
| 9151 | { |
| 9152 | struct mddev *mddev = container_of(ws, struct mddev, del_work); |
Goldwyn Rodrigues | c186b12 | 2015-09-30 13:20:35 -0500 | [diff] [blame] | 9153 | |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 9154 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 9155 | mddev, |
| 9156 | "resync"); |
| 9157 | if (!mddev->sync_thread) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 9158 | pr_warn("%s: could not start resync thread...\n", |
| 9159 | mdname(mddev)); |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 9160 | /* leave the spares where they are, it shouldn't hurt */ |
| 9161 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 9162 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 9163 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 9164 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 9165 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 9166 | wake_up(&resync_wait); |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 9167 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 9168 | &mddev->recovery)) |
| 9169 | if (mddev->sysfs_action) |
| 9170 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 9171 | } else |
| 9172 | md_wakeup_thread(mddev->sync_thread); |
| 9173 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 9174 | md_new_event(mddev); |
| 9175 | } |
| 9176 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9177 | /* |
| 9178 | * This routine is regularly called by all per-raid-array threads to |
| 9179 | * deal with generic issues like resync and super-block update. |
| 9180 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 9181 | * need this as they never do any recovery or update the superblock. |
| 9182 | * |
| 9183 | * It does not do any resync itself, but rather "forks" off other threads |
| 9184 | * to do that as needed. |
| 9185 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 9186 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 9187 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9188 | * and wakeups up this thread which will reap the thread and finish up. |
| 9189 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 9190 | * |
| 9191 | * The overall approach is: |
| 9192 | * 1/ if the superblock needs updating, update it. |
| 9193 | * 2/ If a recovery thread is running, don't do anything else. |
| 9194 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 9195 | * 4/ If there are any faulty devices, remove them. |
| 9196 | * 5/ If array is degraded, try to add spares devices |
| 9197 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 9198 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 9199 | void md_check_recovery(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9200 | { |
NeilBrown | 059421e | 2018-10-03 15:04:41 +1000 | [diff] [blame] | 9201 | if (test_bit(MD_ALLOW_SB_UPDATE, &mddev->flags) && mddev->sb_flags) { |
| 9202 | /* Write superblock - thread that called mddev_suspend() |
| 9203 | * holds reconfig_mutex for us. |
| 9204 | */ |
| 9205 | set_bit(MD_UPDATING_SB, &mddev->flags); |
| 9206 | smp_mb__after_atomic(); |
| 9207 | if (test_bit(MD_ALLOW_SB_UPDATE, &mddev->flags)) |
| 9208 | md_update_sb(mddev, 0); |
| 9209 | clear_bit_unlock(MD_UPDATING_SB, &mddev->flags); |
| 9210 | wake_up(&mddev->sb_wait); |
| 9211 | } |
| 9212 | |
Jonathan Brassow | 68866e4 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 9213 | if (mddev->suspended) |
| 9214 | return; |
| 9215 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 9216 | if (mddev->bitmap) |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 9217 | md_bitmap_daemon_work(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9218 | |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 9219 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 9220 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 9221 | pr_debug("md: %s in immediate safe mode\n", |
| 9222 | mdname(mddev)); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 9223 | mddev->safemode = 2; |
| 9224 | } |
| 9225 | flush_signals(current); |
| 9226 | } |
| 9227 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 9228 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 9229 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9230 | if ( ! ( |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 9231 | (mddev->sb_flags & ~ (1<<MD_SB_CHANGE_PENDING)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9232 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 9233 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 9234 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 9235 | (mddev->safemode == 2 |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 9236 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9237 | )) |
| 9238 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 9239 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 9240 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9241 | int spares = 0; |
NeilBrown | 480523f | 2019-08-20 10:21:09 +1000 | [diff] [blame] | 9242 | bool try_set_sync = mddev->safemode != 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 9243 | |
Shaohua Li | afc1f55 | 2017-08-11 20:34:45 -0700 | [diff] [blame] | 9244 | if (!mddev->external && mddev->safemode == 1) |
NeilBrown | 33182d1 | 2017-08-08 16:56:36 +1000 | [diff] [blame] | 9245 | mddev->safemode = 0; |
| 9246 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 9247 | if (mddev->ro) { |
Neil Brown | ab16bfc | 2015-06-17 12:31:46 +1000 | [diff] [blame] | 9248 | struct md_rdev *rdev; |
| 9249 | if (!mddev->external && mddev->in_sync) |
| 9250 | /* 'Blocked' flag not needed as failed devices |
| 9251 | * will be recorded if array switched to read/write. |
| 9252 | * Leaving it set will prevent the device |
| 9253 | * from being removed. |
| 9254 | */ |
| 9255 | rdev_for_each(rdev, mddev) |
| 9256 | clear_bit(Blocked, &rdev->flags); |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 9257 | /* On a read-only array we can: |
| 9258 | * - remove failed devices |
| 9259 | * - add already-in_sync devices if the array itself |
| 9260 | * is in-sync. |
| 9261 | * As we only add devices that are already in-sync, |
| 9262 | * we can activate the spares immediately. |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 9263 | */ |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 9264 | remove_and_add_spares(mddev, NULL); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 9265 | /* There is no thread, but we need to call |
| 9266 | * ->spare_active and clear saved_raid_disk |
| 9267 | */ |
NeilBrown | 2ac295a | 2014-05-29 11:40:03 +1000 | [diff] [blame] | 9268 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 9269 | md_reap_sync_thread(mddev); |
NeilBrown | a4a3d26 | 2015-07-17 11:57:30 +1000 | [diff] [blame] | 9270 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 9271 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 9272 | clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags); |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 9273 | goto unlock; |
| 9274 | } |
| 9275 | |
Guoqing Jiang | 659b254 | 2015-12-21 10:50:59 +1100 | [diff] [blame] | 9276 | if (mddev_is_clustered(mddev)) { |
| 9277 | struct md_rdev *rdev; |
| 9278 | /* kick the device if another node issued a |
| 9279 | * remove disk. |
| 9280 | */ |
| 9281 | rdev_for_each(rdev, mddev) { |
| 9282 | if (test_and_clear_bit(ClusterRemove, &rdev->flags) && |
| 9283 | rdev->raid_disk < 0) |
| 9284 | md_kick_rdev_from_array(rdev); |
| 9285 | } |
| 9286 | } |
| 9287 | |
NeilBrown | 480523f | 2019-08-20 10:21:09 +1000 | [diff] [blame] | 9288 | if (try_set_sync && !mddev->external && !mddev->in_sync) { |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 9289 | spin_lock(&mddev->lock); |
NeilBrown | 6497709b | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 9290 | set_in_sync(mddev); |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 9291 | spin_unlock(&mddev->lock); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 9292 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 9293 | |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 9294 | if (mddev->sb_flags) |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 9295 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 9296 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9297 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 9298 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 9299 | /* resync/recovery still happening */ |
| 9300 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 9301 | goto unlock; |
| 9302 | } |
| 9303 | if (mddev->sync_thread) { |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9304 | md_reap_sync_thread(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9305 | goto unlock; |
| 9306 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 9307 | /* Set RUNNING before clearing NEEDED to avoid |
| 9308 | * any transients in the value of "sync_action". |
| 9309 | */ |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 9310 | mddev->curr_resync_completed = 0; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 9311 | spin_lock(&mddev->lock); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 9312 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 9313 | spin_unlock(&mddev->lock); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 9314 | /* Clear some bits that don't mean anything, but |
| 9315 | * might be left set |
| 9316 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 9317 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 9318 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9319 | |
NeilBrown | ed20958 | 2012-04-24 10:23:14 +1000 | [diff] [blame] | 9320 | if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
| 9321 | test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 9322 | goto not_running; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9323 | /* no recovery is running. |
| 9324 | * remove any failed drives, then |
| 9325 | * add spares if possible. |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 9326 | * Spares are also removed and re-added, to allow |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9327 | * the personality to fail the re-add. |
| 9328 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9329 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9330 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 9331 | if (mddev->pers->check_reshape == NULL || |
| 9332 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9333 | /* Cannot proceed */ |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 9334 | goto not_running; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9335 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 9336 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 9337 | } else if ((spares = remove_and_add_spares(mddev, NULL))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 9338 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 9339 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 9340 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 9341 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 9342 | } else if (mddev->recovery_cp < MaxSector) { |
| 9343 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 9344 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 9345 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 9346 | /* nothing to be done ... */ |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 9347 | goto not_running; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 9348 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9349 | if (mddev->pers->sync_request) { |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 9350 | if (spares) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 9351 | /* We are adding a device or devices to an array |
| 9352 | * which has the bitmap stored on all devices. |
| 9353 | * So make sure all bitmap pages get written |
| 9354 | */ |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 9355 | md_bitmap_write_all(mddev->bitmap); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 9356 | } |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 9357 | INIT_WORK(&mddev->del_work, md_start_sync); |
| 9358 | queue_work(md_misc_wq, &mddev->del_work); |
| 9359 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9360 | } |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 9361 | not_running: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 9362 | if (!mddev->sync_thread) { |
| 9363 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 9364 | wake_up(&resync_wait); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 9365 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 9366 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 9367 | if (mddev->sysfs_action) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 9368 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 9369 | } |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 9370 | unlock: |
| 9371 | wake_up(&mddev->sb_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9372 | mddev_unlock(mddev); |
| 9373 | } |
| 9374 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 9375 | EXPORT_SYMBOL(md_check_recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9376 | |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9377 | void md_reap_sync_thread(struct mddev *mddev) |
| 9378 | { |
| 9379 | struct md_rdev *rdev; |
Guoqing Jiang | aefb2e5 | 2018-10-18 16:37:44 +0800 | [diff] [blame] | 9380 | sector_t old_dev_sectors = mddev->dev_sectors; |
| 9381 | bool is_reshaped = false; |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9382 | |
| 9383 | /* resync has finished, collect result */ |
| 9384 | md_unregister_thread(&mddev->sync_thread); |
| 9385 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
Guoqing Jiang | 0d8ed0e9 | 2019-07-24 11:09:21 +0200 | [diff] [blame] | 9386 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) && |
| 9387 | mddev->degraded != mddev->raid_disks) { |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9388 | /* success...*/ |
| 9389 | /* activate any spares */ |
| 9390 | if (mddev->pers->spare_active(mddev)) { |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 9391 | sysfs_notify_dirent_safe(mddev->sysfs_degraded); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 9392 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9393 | } |
| 9394 | } |
| 9395 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
Guoqing Jiang | aefb2e5 | 2018-10-18 16:37:44 +0800 | [diff] [blame] | 9396 | mddev->pers->finish_reshape) { |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9397 | mddev->pers->finish_reshape(mddev); |
Guoqing Jiang | aefb2e5 | 2018-10-18 16:37:44 +0800 | [diff] [blame] | 9398 | if (mddev_is_clustered(mddev)) |
| 9399 | is_reshaped = true; |
| 9400 | } |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9401 | |
| 9402 | /* If array is no-longer degraded, then any saved_raid_disk |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 9403 | * information must be scrapped. |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9404 | */ |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 9405 | if (!mddev->degraded) |
| 9406 | rdev_for_each(rdev, mddev) |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9407 | rdev->saved_raid_disk = -1; |
| 9408 | |
| 9409 | md_update_sb(mddev, 1); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 9410 | /* 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] | 9411 | * call resync_finish here if MD_CLUSTER_RESYNC_LOCKED is set by |
| 9412 | * clustered raid */ |
| 9413 | if (test_and_clear_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags)) |
| 9414 | md_cluster_ops->resync_finish(mddev); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9415 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | ea358cd | 2015-06-12 20:05:04 +1000 | [diff] [blame] | 9416 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9417 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 9418 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 9419 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 9420 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Guoqing Jiang | aefb2e5 | 2018-10-18 16:37:44 +0800 | [diff] [blame] | 9421 | /* |
| 9422 | * We call md_cluster_ops->update_size here because sync_size could |
| 9423 | * be changed by md_update_sb, and MD_RECOVERY_RESHAPE is cleared, |
| 9424 | * so it is time to update size across cluster. |
| 9425 | */ |
| 9426 | if (mddev_is_clustered(mddev) && is_reshaped |
| 9427 | && !test_bit(MD_CLOSING, &mddev->flags)) |
| 9428 | md_cluster_ops->update_size(mddev, old_dev_sectors); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 9429 | wake_up(&resync_wait); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9430 | /* flag recovery needed just to double check */ |
| 9431 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 9432 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 9433 | md_new_event(mddev); |
| 9434 | if (mddev->event_work.func) |
| 9435 | queue_work(md_misc_wq, &mddev->event_work); |
| 9436 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 9437 | EXPORT_SYMBOL(md_reap_sync_thread); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9438 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 9439 | 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] | 9440 | { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 9441 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 9442 | wait_event_timeout(rdev->blocked_wait, |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 9443 | !test_bit(Blocked, &rdev->flags) && |
| 9444 | !test_bit(BlockedBadBlocks, &rdev->flags), |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 9445 | msecs_to_jiffies(5000)); |
| 9446 | rdev_dec_pending(rdev, mddev); |
| 9447 | } |
| 9448 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 9449 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 9450 | void md_finish_reshape(struct mddev *mddev) |
| 9451 | { |
| 9452 | /* called be personality module when reshape completes. */ |
| 9453 | struct md_rdev *rdev; |
| 9454 | |
| 9455 | rdev_for_each(rdev, mddev) { |
| 9456 | if (rdev->data_offset > rdev->new_data_offset) |
| 9457 | rdev->sectors += rdev->data_offset - rdev->new_data_offset; |
| 9458 | else |
| 9459 | rdev->sectors -= rdev->new_data_offset - rdev->data_offset; |
| 9460 | rdev->data_offset = rdev->new_data_offset; |
| 9461 | } |
| 9462 | } |
| 9463 | EXPORT_SYMBOL(md_finish_reshape); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 9464 | |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 9465 | /* Bad block management */ |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 9466 | |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 9467 | /* Returns 1 on success, 0 on failure */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 9468 | int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 9469 | int is_new) |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 9470 | { |
Guoqing Jiang | 85ad1d1 | 2016-05-03 22:22:13 -0400 | [diff] [blame] | 9471 | struct mddev *mddev = rdev->mddev; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 9472 | int rv; |
| 9473 | if (is_new) |
| 9474 | s += rdev->new_data_offset; |
| 9475 | else |
| 9476 | s += rdev->data_offset; |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 9477 | rv = badblocks_set(&rdev->badblocks, s, sectors, 0); |
| 9478 | if (rv == 0) { |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 9479 | /* Make sure they get written out promptly */ |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 9480 | if (test_bit(ExternalBbl, &rdev->flags)) |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 9481 | sysfs_notify_dirent_safe(rdev->sysfs_unack_badblocks); |
NeilBrown | 8bd2f0a | 2011-12-08 16:26:08 +1100 | [diff] [blame] | 9482 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 9483 | set_mask_bits(&mddev->sb_flags, 0, |
| 9484 | BIT(MD_SB_CHANGE_CLEAN) | BIT(MD_SB_CHANGE_PENDING)); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 9485 | md_wakeup_thread(rdev->mddev->thread); |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 9486 | return 1; |
| 9487 | } else |
| 9488 | return 0; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 9489 | } |
| 9490 | EXPORT_SYMBOL_GPL(rdev_set_badblocks); |
| 9491 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 9492 | int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
| 9493 | int is_new) |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 9494 | { |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 9495 | int rv; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 9496 | if (is_new) |
| 9497 | s += rdev->new_data_offset; |
| 9498 | else |
| 9499 | s += rdev->data_offset; |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 9500 | rv = badblocks_clear(&rdev->badblocks, s, sectors); |
| 9501 | if ((rv == 0) && test_bit(ExternalBbl, &rdev->flags)) |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 9502 | sysfs_notify_dirent_safe(rdev->sysfs_badblocks); |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 9503 | return rv; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 9504 | } |
| 9505 | EXPORT_SYMBOL_GPL(rdev_clear_badblocks); |
| 9506 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 9507 | static int md_notify_reboot(struct notifier_block *this, |
| 9508 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9509 | { |
| 9510 | struct list_head *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 9511 | struct mddev *mddev; |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 9512 | int need_delay = 0; |
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 | for_each_mddev(mddev, tmp) { |
| 9515 | if (mddev_trylock(mddev)) { |
NeilBrown | 30b8aa9 | 2012-04-24 10:23:16 +1000 | [diff] [blame] | 9516 | if (mddev->pers) |
| 9517 | __md_stop_writes(mddev); |
NeilBrown | 0f62fb2 | 2014-05-06 09:36:08 +1000 | [diff] [blame] | 9518 | if (mddev->persistent) |
| 9519 | mddev->safemode = 2; |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 9520 | mddev_unlock(mddev); |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 9521 | } |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 9522 | need_delay = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9523 | } |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 9524 | /* |
| 9525 | * certain more exotic SCSI devices are known to be |
| 9526 | * volatile wrt too early system reboots. While the |
| 9527 | * right place to handle this issue is the given |
| 9528 | * driver, we do want to have a safe RAID driver ... |
| 9529 | */ |
| 9530 | if (need_delay) |
| 9531 | mdelay(1000*1); |
| 9532 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9533 | return NOTIFY_DONE; |
| 9534 | } |
| 9535 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 9536 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9537 | .notifier_call = md_notify_reboot, |
| 9538 | .next = NULL, |
| 9539 | .priority = INT_MAX, /* before any real devices */ |
| 9540 | }; |
| 9541 | |
| 9542 | static void md_geninit(void) |
| 9543 | { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 9544 | 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] | 9545 | |
Alexey Dobriyan | 97a3253 | 2020-02-03 17:37:17 -0800 | [diff] [blame] | 9546 | proc_create("mdstat", S_IRUGO, NULL, &mdstat_proc_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9547 | } |
| 9548 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 9549 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9550 | { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 9551 | int ret = -ENOMEM; |
| 9552 | |
Tejun Heo | ada609e | 2011-01-25 14:35:54 +0100 | [diff] [blame] | 9553 | md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 9554 | if (!md_wq) |
| 9555 | goto err_wq; |
| 9556 | |
| 9557 | md_misc_wq = alloc_workqueue("md_misc", 0, 0); |
| 9558 | if (!md_misc_wq) |
| 9559 | goto err_misc_wq; |
| 9560 | |
Guoqing Jiang | cc1ffe6 | 2020-04-04 23:57:08 +0200 | [diff] [blame] | 9561 | md_rdev_misc_wq = alloc_workqueue("md_rdev_misc", 0, 0); |
Guoqing Jiang | cf0b9b4 | 2020-10-08 05:19:09 +0200 | [diff] [blame] | 9562 | if (!md_rdev_misc_wq) |
Guoqing Jiang | cc1ffe6 | 2020-04-04 23:57:08 +0200 | [diff] [blame] | 9563 | goto err_rdev_misc_wq; |
| 9564 | |
Christoph Hellwig | 28144f9 | 2020-10-29 15:58:34 +0100 | [diff] [blame] | 9565 | ret = __register_blkdev(MD_MAJOR, "md", md_probe); |
| 9566 | if (ret < 0) |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 9567 | goto err_md; |
| 9568 | |
Christoph Hellwig | 28144f9 | 2020-10-29 15:58:34 +0100 | [diff] [blame] | 9569 | ret = __register_blkdev(0, "mdp", md_probe); |
| 9570 | if (ret < 0) |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 9571 | goto err_mdp; |
| 9572 | mdp_major = ret; |
| 9573 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9574 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 9575 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9576 | |
| 9577 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 9578 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9579 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 9580 | err_mdp: |
| 9581 | unregister_blkdev(MD_MAJOR, "md"); |
| 9582 | err_md: |
Guoqing Jiang | cc1ffe6 | 2020-04-04 23:57:08 +0200 | [diff] [blame] | 9583 | destroy_workqueue(md_rdev_misc_wq); |
| 9584 | err_rdev_misc_wq: |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 9585 | destroy_workqueue(md_misc_wq); |
| 9586 | err_misc_wq: |
| 9587 | destroy_workqueue(md_wq); |
| 9588 | err_wq: |
| 9589 | return ret; |
| 9590 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9591 | |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9592 | static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev) |
Goldwyn Rodrigues | 1d7e3e9 | 2014-06-07 01:53:00 -0500 | [diff] [blame] | 9593 | { |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9594 | struct mdp_superblock_1 *sb = page_address(rdev->sb_page); |
| 9595 | struct md_rdev *rdev2; |
| 9596 | int role, ret; |
| 9597 | char b[BDEVNAME_SIZE]; |
Goldwyn Rodrigues | 1d7e3e9 | 2014-06-07 01:53:00 -0500 | [diff] [blame] | 9598 | |
Guoqing Jiang | 818da59 | 2017-03-01 16:42:40 +0800 | [diff] [blame] | 9599 | /* |
| 9600 | * If size is changed in another node then we need to |
| 9601 | * do resize as well. |
| 9602 | */ |
| 9603 | if (mddev->dev_sectors != le64_to_cpu(sb->size)) { |
| 9604 | ret = mddev->pers->resize(mddev, le64_to_cpu(sb->size)); |
| 9605 | if (ret) |
| 9606 | pr_info("md-cluster: resize failed\n"); |
| 9607 | else |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 9608 | md_bitmap_update_sb(mddev->bitmap); |
Guoqing Jiang | 818da59 | 2017-03-01 16:42:40 +0800 | [diff] [blame] | 9609 | } |
| 9610 | |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9611 | /* Check for change of roles in the active devices */ |
| 9612 | rdev_for_each(rdev2, mddev) { |
| 9613 | if (test_bit(Faulty, &rdev2->flags)) |
| 9614 | continue; |
| 9615 | |
| 9616 | /* Check if the roles changed */ |
| 9617 | role = le16_to_cpu(sb->dev_roles[rdev2->desc_nr]); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 9618 | |
| 9619 | if (test_bit(Candidate, &rdev2->flags)) { |
| 9620 | if (role == 0xfffe) { |
| 9621 | pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2->bdev,b)); |
| 9622 | md_kick_rdev_from_array(rdev2); |
| 9623 | continue; |
| 9624 | } |
| 9625 | else |
| 9626 | clear_bit(Candidate, &rdev2->flags); |
| 9627 | } |
| 9628 | |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9629 | if (role != rdev2->raid_disk) { |
Guoqing Jiang | ca1e98e | 2018-10-18 16:37:45 +0800 | [diff] [blame] | 9630 | /* |
| 9631 | * got activated except reshape is happening. |
| 9632 | */ |
| 9633 | if (rdev2->raid_disk == -1 && role != 0xffff && |
| 9634 | !(le32_to_cpu(sb->feature_map) & |
| 9635 | MD_FEATURE_RESHAPE_ACTIVE)) { |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9636 | rdev2->saved_raid_disk = role; |
| 9637 | ret = remove_and_add_spares(mddev, rdev2); |
| 9638 | pr_info("Activated spare: %s\n", |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 9639 | bdevname(rdev2->bdev,b)); |
Guoqing Jiang | a578183 | 2016-05-02 11:33:14 -0400 | [diff] [blame] | 9640 | /* wakeup mddev->thread here, so array could |
| 9641 | * perform resync with the new activated disk */ |
| 9642 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 9643 | md_wakeup_thread(mddev->thread); |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9644 | } |
| 9645 | /* device faulty |
| 9646 | * We just want to do the minimum to mark the disk |
| 9647 | * as faulty. The recovery is performed by the |
| 9648 | * one who initiated the error. |
| 9649 | */ |
| 9650 | if ((role == 0xfffe) || (role == 0xfffd)) { |
| 9651 | md_error(mddev, rdev2); |
| 9652 | clear_bit(Blocked, &rdev2->flags); |
| 9653 | } |
| 9654 | } |
Goldwyn Rodrigues | 1d7e3e9 | 2014-06-07 01:53:00 -0500 | [diff] [blame] | 9655 | } |
| 9656 | |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 9657 | if (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) |
| 9658 | update_raid_disks(mddev, le32_to_cpu(sb->raid_disks)); |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9659 | |
Guoqing Jiang | 7564bed | 2018-10-18 16:37:42 +0800 | [diff] [blame] | 9660 | /* |
| 9661 | * Since mddev->delta_disks has already updated in update_raid_disks, |
| 9662 | * so it is time to check reshape. |
| 9663 | */ |
| 9664 | if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery) && |
| 9665 | (le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 9666 | /* |
| 9667 | * reshape is happening in the remote node, we need to |
| 9668 | * update reshape_position and call start_reshape. |
| 9669 | */ |
Christoph Hellwig | ed4d0a4e | 2019-04-04 18:56:10 +0200 | [diff] [blame] | 9670 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
Guoqing Jiang | 7564bed | 2018-10-18 16:37:42 +0800 | [diff] [blame] | 9671 | if (mddev->pers->update_reshape_pos) |
| 9672 | mddev->pers->update_reshape_pos(mddev); |
| 9673 | if (mddev->pers->start_reshape) |
| 9674 | mddev->pers->start_reshape(mddev); |
| 9675 | } else if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery) && |
| 9676 | mddev->reshape_position != MaxSector && |
| 9677 | !(le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 9678 | /* reshape is just done in another node. */ |
| 9679 | mddev->reshape_position = MaxSector; |
| 9680 | if (mddev->pers->update_reshape_pos) |
| 9681 | mddev->pers->update_reshape_pos(mddev); |
| 9682 | } |
| 9683 | |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9684 | /* Finally set the event to be up to date */ |
| 9685 | mddev->events = le64_to_cpu(sb->events); |
| 9686 | } |
| 9687 | |
| 9688 | static int read_rdev(struct mddev *mddev, struct md_rdev *rdev) |
| 9689 | { |
| 9690 | int err; |
| 9691 | struct page *swapout = rdev->sb_page; |
| 9692 | struct mdp_superblock_1 *sb; |
| 9693 | |
| 9694 | /* Store the sb page of the rdev in the swapout temporary |
| 9695 | * variable in case we err in the future |
| 9696 | */ |
| 9697 | rdev->sb_page = NULL; |
NeilBrown | 7f0f0d8 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 9698 | err = alloc_disk_sb(rdev); |
| 9699 | if (err == 0) { |
| 9700 | ClearPageUptodate(rdev->sb_page); |
| 9701 | rdev->sb_loaded = 0; |
| 9702 | err = super_types[mddev->major_version]. |
| 9703 | load_super(rdev, NULL, mddev->minor_version); |
| 9704 | } |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9705 | if (err < 0) { |
| 9706 | pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n", |
| 9707 | __func__, __LINE__, rdev->desc_nr, err); |
NeilBrown | 7f0f0d8 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 9708 | if (rdev->sb_page) |
| 9709 | put_page(rdev->sb_page); |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9710 | rdev->sb_page = swapout; |
| 9711 | rdev->sb_loaded = 1; |
| 9712 | return err; |
| 9713 | } |
| 9714 | |
| 9715 | sb = page_address(rdev->sb_page); |
| 9716 | /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET |
| 9717 | * is not set |
| 9718 | */ |
| 9719 | |
| 9720 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET)) |
| 9721 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 9722 | |
| 9723 | /* The other node finished recovery, call spare_active to set |
| 9724 | * device In_sync and mddev->degraded |
| 9725 | */ |
| 9726 | if (rdev->recovery_offset == MaxSector && |
| 9727 | !test_bit(In_sync, &rdev->flags) && |
| 9728 | mddev->pers->spare_active(mddev)) |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 9729 | sysfs_notify_dirent_safe(mddev->sysfs_degraded); |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9730 | |
| 9731 | put_page(swapout); |
| 9732 | return 0; |
| 9733 | } |
| 9734 | |
| 9735 | void md_reload_sb(struct mddev *mddev, int nr) |
| 9736 | { |
| 9737 | struct md_rdev *rdev; |
| 9738 | int err; |
| 9739 | |
| 9740 | /* Find the rdev */ |
| 9741 | rdev_for_each_rcu(rdev, mddev) { |
| 9742 | if (rdev->desc_nr == nr) |
| 9743 | break; |
| 9744 | } |
| 9745 | |
| 9746 | if (!rdev || rdev->desc_nr != nr) { |
| 9747 | pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr); |
| 9748 | return; |
| 9749 | } |
| 9750 | |
| 9751 | err = read_rdev(mddev, rdev); |
| 9752 | if (err < 0) |
| 9753 | return; |
| 9754 | |
| 9755 | check_sb_changes(mddev, rdev); |
| 9756 | |
| 9757 | /* Read all rdev's to update recovery_offset */ |
Guoqing Jiang | 0ea9924 | 2018-04-09 17:01:21 +0800 | [diff] [blame] | 9758 | rdev_for_each_rcu(rdev, mddev) { |
| 9759 | if (!test_bit(Faulty, &rdev->flags)) |
| 9760 | read_rdev(mddev, rdev); |
| 9761 | } |
Goldwyn Rodrigues | 1d7e3e9 | 2014-06-07 01:53:00 -0500 | [diff] [blame] | 9762 | } |
| 9763 | EXPORT_SYMBOL(md_reload_sb); |
| 9764 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9765 | #ifndef MODULE |
| 9766 | |
| 9767 | /* |
| 9768 | * Searches all registered partitions for autorun RAID arrays |
| 9769 | * at boot time. |
| 9770 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9771 | |
Cong Wang | 5b1f5bc3 | 2016-06-08 09:20:16 -0700 | [diff] [blame] | 9772 | static DEFINE_MUTEX(detected_devices_mutex); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9773 | static LIST_HEAD(all_detected_devices); |
| 9774 | struct detected_devices_node { |
| 9775 | struct list_head list; |
| 9776 | dev_t dev; |
| 9777 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9778 | |
| 9779 | void md_autodetect_dev(dev_t dev) |
| 9780 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9781 | struct detected_devices_node *node_detected_dev; |
| 9782 | |
| 9783 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 9784 | if (node_detected_dev) { |
| 9785 | node_detected_dev->dev = dev; |
Cong Wang | 5b1f5bc3 | 2016-06-08 09:20:16 -0700 | [diff] [blame] | 9786 | mutex_lock(&detected_devices_mutex); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9787 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
Cong Wang | 5b1f5bc3 | 2016-06-08 09:20:16 -0700 | [diff] [blame] | 9788 | mutex_unlock(&detected_devices_mutex); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9789 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9790 | } |
| 9791 | |
Christoph Hellwig | d82fa81 | 2020-06-06 15:00:24 +0200 | [diff] [blame] | 9792 | void md_autostart_arrays(int part) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9793 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 9794 | struct md_rdev *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9795 | struct detected_devices_node *node_detected_dev; |
| 9796 | dev_t dev; |
| 9797 | int i_scanned, i_passed; |
| 9798 | |
| 9799 | i_scanned = 0; |
| 9800 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9801 | |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 9802 | pr_info("md: Autodetecting RAID arrays.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9803 | |
Cong Wang | 5b1f5bc3 | 2016-06-08 09:20:16 -0700 | [diff] [blame] | 9804 | mutex_lock(&detected_devices_mutex); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9805 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 9806 | i_scanned++; |
| 9807 | node_detected_dev = list_entry(all_detected_devices.next, |
| 9808 | struct detected_devices_node, list); |
| 9809 | list_del(&node_detected_dev->list); |
| 9810 | dev = node_detected_dev->dev; |
| 9811 | kfree(node_detected_dev); |
Shaohua Li | 90bcf133 | 2016-09-14 14:26:54 -0700 | [diff] [blame] | 9812 | mutex_unlock(&detected_devices_mutex); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 9813 | rdev = md_import_device(dev,0, 90); |
Shaohua Li | 90bcf133 | 2016-09-14 14:26:54 -0700 | [diff] [blame] | 9814 | mutex_lock(&detected_devices_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9815 | if (IS_ERR(rdev)) |
| 9816 | continue; |
| 9817 | |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 9818 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9819 | continue; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 9820 | |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 9821 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9822 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9823 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9824 | } |
Cong Wang | 5b1f5bc3 | 2016-06-08 09:20:16 -0700 | [diff] [blame] | 9825 | mutex_unlock(&detected_devices_mutex); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9826 | |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 9827 | 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] | 9828 | |
| 9829 | autorun_devices(part); |
| 9830 | } |
| 9831 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 9832 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9833 | |
| 9834 | static __exit void md_exit(void) |
| 9835 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 9836 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9837 | struct list_head *tmp; |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 9838 | int delay = 1; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 9839 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 9840 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9841 | unregister_blkdev(mdp_major, "mdp"); |
| 9842 | unregister_reboot_notifier(&md_notifier); |
| 9843 | unregister_sysctl_table(raid_table_header); |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 9844 | |
| 9845 | /* We cannot unload the modules while some process is |
| 9846 | * waiting for us in select() or poll() - wake them up |
| 9847 | */ |
| 9848 | md_unloading = 1; |
| 9849 | while (waitqueue_active(&md_event_waiters)) { |
| 9850 | /* not safe to leave yet */ |
| 9851 | wake_up(&md_event_waiters); |
| 9852 | msleep(delay); |
| 9853 | delay += delay; |
| 9854 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9855 | remove_proc_entry("mdstat", NULL); |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 9856 | |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 9857 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9858 | export_array(mddev); |
NeilBrown | 9356863 | 2017-02-06 13:41:39 +1100 | [diff] [blame] | 9859 | mddev->ctime = 0; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 9860 | mddev->hold_active = 0; |
NeilBrown | 9356863 | 2017-02-06 13:41:39 +1100 | [diff] [blame] | 9861 | /* |
| 9862 | * for_each_mddev() will call mddev_put() at the end of each |
| 9863 | * iteration. As the mddev is now fully clear, this will |
| 9864 | * schedule the mddev for destruction by a workqueue, and the |
| 9865 | * destroy_workqueue() below will wait for that to complete. |
| 9866 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9867 | } |
Guoqing Jiang | cc1ffe6 | 2020-04-04 23:57:08 +0200 | [diff] [blame] | 9868 | destroy_workqueue(md_rdev_misc_wq); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 9869 | destroy_workqueue(md_misc_wq); |
| 9870 | destroy_workqueue(md_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9871 | } |
| 9872 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 9873 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9874 | module_exit(md_exit) |
| 9875 | |
Kees Cook | e4dca7b | 2017-10-17 19:04:42 -0700 | [diff] [blame] | 9876 | static int get_ro(char *buffer, const struct kernel_param *kp) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 9877 | { |
Xiongfeng Wang | 3f99980 | 2020-05-11 16:23:25 +0800 | [diff] [blame] | 9878 | return sprintf(buffer, "%d\n", start_readonly); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 9879 | } |
Kees Cook | e4dca7b | 2017-10-17 19:04:42 -0700 | [diff] [blame] | 9880 | static int set_ro(const char *val, const struct kernel_param *kp) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 9881 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 9882 | return kstrtouint(val, 10, (unsigned int *)&start_readonly); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 9883 | } |
| 9884 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 9885 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 9886 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 9887 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | 78b6350 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 9888 | module_param(create_on_open, bool, S_IRUSR|S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 9889 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9890 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 9891 | MODULE_DESCRIPTION("MD RAID framework"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 9892 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 9893 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |