xref: /illumos-gate/usr/src/uts/common/fs/zfs/mmp.c (revision 4348eb90)
1e0f1c0afSOlaf Faaland /*
2e0f1c0afSOlaf Faaland  * CDDL HEADER START
3e0f1c0afSOlaf Faaland  *
4e0f1c0afSOlaf Faaland  * The contents of this file are subject to the terms of the
5e0f1c0afSOlaf Faaland  * Common Development and Distribution License (the "License").
6e0f1c0afSOlaf Faaland  * You may not use this file except in compliance with the License.
7e0f1c0afSOlaf Faaland  *
8e0f1c0afSOlaf Faaland  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9e0f1c0afSOlaf Faaland  * or http://www.opensolaris.org/os/licensing.
10e0f1c0afSOlaf Faaland  * See the License for the specific language governing permissions
11e0f1c0afSOlaf Faaland  * and limitations under the License.
12e0f1c0afSOlaf Faaland  *
13e0f1c0afSOlaf Faaland  * When distributing Covered Code, include this CDDL HEADER in each
14e0f1c0afSOlaf Faaland  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15e0f1c0afSOlaf Faaland  * If applicable, add the following below this CDDL HEADER, with the
16e0f1c0afSOlaf Faaland  * fields enclosed by brackets "[]" replaced with your own identifying
17e0f1c0afSOlaf Faaland  * information: Portions Copyright [yyyy] [name of copyright owner]
18e0f1c0afSOlaf Faaland  *
19e0f1c0afSOlaf Faaland  * CDDL HEADER END
20e0f1c0afSOlaf Faaland  */
21e0f1c0afSOlaf Faaland /*
22e0f1c0afSOlaf Faaland  * Copyright (c) 2017 by Lawrence Livermore National Security, LLC.
23e0f1c0afSOlaf Faaland  * Copyright 2019 Joyent, Inc.
24e0f1c0afSOlaf Faaland  */
25e0f1c0afSOlaf Faaland 
26e0f1c0afSOlaf Faaland #include <sys/abd.h>
27e0f1c0afSOlaf Faaland #include <sys/mmp.h>
28e0f1c0afSOlaf Faaland #include <sys/spa.h>
29e0f1c0afSOlaf Faaland #include <sys/spa_impl.h>
30e0f1c0afSOlaf Faaland #include <sys/time.h>
31e0f1c0afSOlaf Faaland #include <sys/vdev.h>
32e0f1c0afSOlaf Faaland #include <sys/vdev_impl.h>
33e0f1c0afSOlaf Faaland #include <sys/zfs_context.h>
34e0f1c0afSOlaf Faaland #include <sys/callb.h>
35e0f1c0afSOlaf Faaland 
36e0f1c0afSOlaf Faaland /*
37e0f1c0afSOlaf Faaland  * Multi-Modifier Protection (MMP) attempts to prevent a user from importing
38e0f1c0afSOlaf Faaland  * or opening a pool on more than one host at a time.  In particular, it
39e0f1c0afSOlaf Faaland  * prevents "zpool import -f" on a host from succeeding while the pool is
40e0f1c0afSOlaf Faaland  * already imported on another host.  There are many other ways in which a
41e0f1c0afSOlaf Faaland  * device could be used by two hosts for different purposes at the same time
42e0f1c0afSOlaf Faaland  * resulting in pool damage.  This implementation does not attempt to detect
43e0f1c0afSOlaf Faaland  * those cases.
44e0f1c0afSOlaf Faaland  *
45e0f1c0afSOlaf Faaland  * MMP operates by ensuring there are frequent visible changes on disk (a
46e0f1c0afSOlaf Faaland  * "heartbeat") at all times.  And by altering the import process to check
47e0f1c0afSOlaf Faaland  * for these changes and failing the import when they are detected.  This
48e0f1c0afSOlaf Faaland  * functionality is enabled by setting the 'multihost' pool property to on.
49e0f1c0afSOlaf Faaland  *
50e0f1c0afSOlaf Faaland  * Uberblocks written by the txg_sync thread always go into the first
51e0f1c0afSOlaf Faaland  * (N-MMP_BLOCKS_PER_LABEL) slots, the remaining slots are reserved for MMP.
52e0f1c0afSOlaf Faaland  * They are used to hold uberblocks which are exactly the same as the last
53*4348eb90SOlaf Faaland  * synced uberblock except that the ub_timestamp and mmp_config are frequently
54*4348eb90SOlaf Faaland  * updated.  Like all other uberblocks, the slot is written with an embedded
55*4348eb90SOlaf Faaland  * checksum, and slots with invalid checksums are ignored.  This provides the
56e0f1c0afSOlaf Faaland  * "heartbeat", with no risk of overwriting good uberblocks that must be
57e0f1c0afSOlaf Faaland  * preserved, e.g. previous txgs and associated block pointers.
58e0f1c0afSOlaf Faaland  *
59*4348eb90SOlaf Faaland  * Three optional fields are added to uberblock structure; ub_mmp_magic,
60*4348eb90SOlaf Faaland  * ub_mmp_config, and ub_mmp_delay.  The ub_mmp_magic value allows zfs to tell
61*4348eb90SOlaf Faaland  * whether the other ub_mmp_* fields are valid.  The ub_mmp_config field tells
62*4348eb90SOlaf Faaland  * the importing host the settings of zfs_multihost_interval and
63*4348eb90SOlaf Faaland  * zfs_multihost_fail_intervals on the host which last had (or currently has)
64*4348eb90SOlaf Faaland  * the pool imported.  These determine how long a host must wait to detect
65*4348eb90SOlaf Faaland  * activity in the pool, before concluding the pool is not in use.  The
66*4348eb90SOlaf Faaland  * mmp_delay field is a decaying average of the amount of time between
67*4348eb90SOlaf Faaland  * completion of successive MMP writes, in nanoseconds.  It indicates whether
68*4348eb90SOlaf Faaland  * MMP is enabled.
69e0f1c0afSOlaf Faaland  *
70e0f1c0afSOlaf Faaland  * During import an activity test may now be performed to determine if
71e0f1c0afSOlaf Faaland  * the pool is in use.  The activity test is typically required if the
72e0f1c0afSOlaf Faaland  * ZPOOL_CONFIG_HOSTID does not match the system hostid, the pool state is
73e0f1c0afSOlaf Faaland  * POOL_STATE_ACTIVE, and the pool is not a root pool.
74e0f1c0afSOlaf Faaland  *
75*4348eb90SOlaf Faaland  * The activity test finds the "best" uberblock (highest txg, timestamp, and, if
76*4348eb90SOlaf Faaland  * ub_mmp_magic is valid, sequence number from ub_mmp_config).  It then waits
77*4348eb90SOlaf Faaland  * some time, and finds the "best" uberblock again.  If any of the mentioned
78*4348eb90SOlaf Faaland  * fields have different values in the newly read uberblock, the pool is in use
79*4348eb90SOlaf Faaland  * by another host and the import fails.  In order to assure the accuracy of the
80*4348eb90SOlaf Faaland  * activity test, the default values result in an activity test duration of 20x
81*4348eb90SOlaf Faaland  * the mmp write interval.
82e0f1c0afSOlaf Faaland  *
83*4348eb90SOlaf Faaland  * The duration of the "zpool import" activity test depends on the information
84*4348eb90SOlaf Faaland  * available in the "best" uberblock:
85*4348eb90SOlaf Faaland  *
86*4348eb90SOlaf Faaland  * 1) If uberblock was written by zfs-0.8 or newer and fail_intervals > 0:
87*4348eb90SOlaf Faaland  *    ub_mmp_config.fail_intervals * ub_mmp_config.multihost_interval * 2
88*4348eb90SOlaf Faaland  *
89*4348eb90SOlaf Faaland  *    In this case, a weak guarantee is provided.  Since the host which last had
90*4348eb90SOlaf Faaland  *    the pool imported will suspend the pool if no mmp writes land within
91*4348eb90SOlaf Faaland  *    fail_intervals * multihost_interval ms, the absence of writes during that
92*4348eb90SOlaf Faaland  *    time means either the pool is not imported, or it is imported but the pool
93*4348eb90SOlaf Faaland  *    is suspended and no further writes will occur.
94*4348eb90SOlaf Faaland  *
95*4348eb90SOlaf Faaland  *    Note that resuming the suspended pool on the remote host would invalidate
96*4348eb90SOlaf Faaland  *    this guarantee, and so it is not allowed.
97*4348eb90SOlaf Faaland  *
98*4348eb90SOlaf Faaland  *    The factor of 2 provides a conservative safety factor and derives from
99*4348eb90SOlaf Faaland  *    MMP_IMPORT_SAFETY_FACTOR;
100*4348eb90SOlaf Faaland  *
101*4348eb90SOlaf Faaland  * 2) If uberblock was written by zfs-0.8 or newer and fail_intervals == 0:
102*4348eb90SOlaf Faaland  *    (ub_mmp_config.multihost_interval + ub_mmp_delay) *
103*4348eb90SOlaf Faaland  *        zfs_multihost_import_intervals
104*4348eb90SOlaf Faaland  *
105*4348eb90SOlaf Faaland  *    In this case no guarantee can provided.  However, as long as some devices
106*4348eb90SOlaf Faaland  *    are healthy and connected, it is likely that at least one write will land
107*4348eb90SOlaf Faaland  *    within (multihost_interval + mmp_delay) because multihost_interval is
108*4348eb90SOlaf Faaland  *    enough time for a write to be attempted to each leaf vdev, and mmp_delay
109*4348eb90SOlaf Faaland  *    is enough for one to land, based on past delays.  Multiplying by
110*4348eb90SOlaf Faaland  *    zfs_multihost_import_intervals provides a conservative safety factor.
111*4348eb90SOlaf Faaland  *
112*4348eb90SOlaf Faaland  * 3) If uberblock was written by zfs-0.7:
113*4348eb90SOlaf Faaland  *    (zfs_multihost_interval + ub_mmp_delay) * zfs_multihost_import_intervals
114*4348eb90SOlaf Faaland  *
115*4348eb90SOlaf Faaland  *    The same logic as case #2 applies, but we do not know remote tunables.
116*4348eb90SOlaf Faaland  *
117*4348eb90SOlaf Faaland  *    We use the local value for zfs_multihost_interval because the original MMP
118*4348eb90SOlaf Faaland  *    did not record this value in the uberblock.
119*4348eb90SOlaf Faaland  *
120*4348eb90SOlaf Faaland  *    ub_mmp_delay >= (zfs_multihost_interval / leaves), so if the other host
121*4348eb90SOlaf Faaland  *    has a much larger zfs_multihost_interval set, ub_mmp_delay will reflect
122*4348eb90SOlaf Faaland  *    that.  We will have waited enough time for zfs_multihost_import_intervals
123*4348eb90SOlaf Faaland  *    writes to be issued and all but one to land.
124*4348eb90SOlaf Faaland  *
125*4348eb90SOlaf Faaland  *    single device pool example delays
126*4348eb90SOlaf Faaland  *
127*4348eb90SOlaf Faaland  *    import_delay = (1 + 1) * 20   =  40s #defaults, no I/O delay
128*4348eb90SOlaf Faaland  *    import_delay = (1 + 10) * 20  = 220s #defaults, 10s I/O delay
129*4348eb90SOlaf Faaland  *    import_delay = (10 + 10) * 20 = 400s #10s multihost_interval,
130*4348eb90SOlaf Faaland  *                                          no I/O delay
131*4348eb90SOlaf Faaland  *    100 device pool example delays
132*4348eb90SOlaf Faaland  *
133*4348eb90SOlaf Faaland  *    import_delay = (1 + .01) * 20 =  20s #defaults, no I/O delay
134*4348eb90SOlaf Faaland  *    import_delay = (1 + 10) * 20  = 220s #defaults, 10s I/O delay
135*4348eb90SOlaf Faaland  *    import_delay = (10 + .1) * 20 = 202s #10s multihost_interval,
136*4348eb90SOlaf Faaland  *                                          no I/O delay
137*4348eb90SOlaf Faaland  *
138*4348eb90SOlaf Faaland  * 4) Otherwise, this uberblock was written by a pre-MMP zfs:
139*4348eb90SOlaf Faaland  *    zfs_multihost_import_intervals * zfs_multihost_interval
140*4348eb90SOlaf Faaland  *
141*4348eb90SOlaf Faaland  *    In this case local tunables are used.  By default this product = 10s, long
142*4348eb90SOlaf Faaland  *    enough for a pool with any activity at all to write at least one
143*4348eb90SOlaf Faaland  *    uberblock.  No guarantee can be provided.
144*4348eb90SOlaf Faaland  *
145*4348eb90SOlaf Faaland  * Additionally, the duration is then extended by a random 25% to attempt to to
146*4348eb90SOlaf Faaland  * detect simultaneous imports.  For example, if both partner hosts are rebooted
147*4348eb90SOlaf Faaland  * at the same time and automatically attempt to import the pool.
148e0f1c0afSOlaf Faaland  */
149e0f1c0afSOlaf Faaland 
150e0f1c0afSOlaf Faaland /*
151e0f1c0afSOlaf Faaland  * Used to control the frequency of mmp writes which are performed when the
152e0f1c0afSOlaf Faaland  * 'multihost' pool property is on.  This is one factor used to determine the
153e0f1c0afSOlaf Faaland  * length of the activity check during import.
154e0f1c0afSOlaf Faaland  *
155*4348eb90SOlaf Faaland  * On average an mmp write will be issued for each leaf vdev every
156*4348eb90SOlaf Faaland  * zfs_multihost_interval milliseconds.  In practice, the observed period can
157*4348eb90SOlaf Faaland  * vary with the I/O load and this observed value is the ub_mmp_delay which is
158e0f1c0afSOlaf Faaland  * stored in the uberblock.  The minimum allowed value is 100 ms.
159e0f1c0afSOlaf Faaland  */
160e0f1c0afSOlaf Faaland ulong_t zfs_multihost_interval = MMP_DEFAULT_INTERVAL;
161e0f1c0afSOlaf Faaland 
162e0f1c0afSOlaf Faaland /*
163e0f1c0afSOlaf Faaland  * Used to control the duration of the activity test on import.  Smaller values
164e0f1c0afSOlaf Faaland  * of zfs_multihost_import_intervals will reduce the import time but increase
165e0f1c0afSOlaf Faaland  * the risk of failing to detect an active pool.  The total activity check time
166e0f1c0afSOlaf Faaland  * is never allowed to drop below one second.  A value of 0 is ignored and
167e0f1c0afSOlaf Faaland  * treated as if it was set to 1.
168e0f1c0afSOlaf Faaland  */
169e0f1c0afSOlaf Faaland uint_t zfs_multihost_import_intervals = MMP_DEFAULT_IMPORT_INTERVALS;
170e0f1c0afSOlaf Faaland 
171e0f1c0afSOlaf Faaland /*
172*4348eb90SOlaf Faaland  * Controls the behavior of the pool when mmp write failures or delays are
173*4348eb90SOlaf Faaland  * detected.
174e0f1c0afSOlaf Faaland  *
175*4348eb90SOlaf Faaland  * When zfs_multihost_fail_intervals = 0, mmp write failures or delays are
176*4348eb90SOlaf Faaland  * ignored.  The failures will still be reported to the ZED which depending on
177*4348eb90SOlaf Faaland  * its configuration may take action such as suspending the pool or taking a
178e0f1c0afSOlaf Faaland  * device offline.
179e0f1c0afSOlaf Faaland  *
180*4348eb90SOlaf Faaland  * When zfs_multihost_fail_intervals > 0, the pool will be suspended if
181*4348eb90SOlaf Faaland  * zfs_multihost_fail_intervals * zfs_multihost_interval milliseconds pass
182*4348eb90SOlaf Faaland  * without a successful mmp write.  This guarantees the activity test will see
183*4348eb90SOlaf Faaland  * mmp writes if the pool is imported.  A value of 1 is ignored and treated as
184*4348eb90SOlaf Faaland  * if it was set to 2, because a single leaf vdev pool will issue a write once
185*4348eb90SOlaf Faaland  * per multihost_interval and thus any variation in latency would cause the
186*4348eb90SOlaf Faaland  * pool to be suspended.
187e0f1c0afSOlaf Faaland  */
188e0f1c0afSOlaf Faaland uint_t zfs_multihost_fail_intervals = MMP_DEFAULT_FAIL_INTERVALS;
189e0f1c0afSOlaf Faaland 
190e0f1c0afSOlaf Faaland char *mmp_tag = "mmp_write_uberblock";
191e0f1c0afSOlaf Faaland static void mmp_thread(void *arg);
192e0f1c0afSOlaf Faaland 
193e0f1c0afSOlaf Faaland void
mmp_init(spa_t * spa)194e0f1c0afSOlaf Faaland mmp_init(spa_t *spa)
195e0f1c0afSOlaf Faaland {
196e0f1c0afSOlaf Faaland 	mmp_thread_t *mmp = &spa->spa_mmp;
197e0f1c0afSOlaf Faaland 
198e0f1c0afSOlaf Faaland 	mutex_init(&mmp->mmp_thread_lock, NULL, MUTEX_DEFAULT, NULL);
199e0f1c0afSOlaf Faaland 	cv_init(&mmp->mmp_thread_cv, NULL, CV_DEFAULT, NULL);
200e0f1c0afSOlaf Faaland 	mutex_init(&mmp->mmp_io_lock, NULL, MUTEX_DEFAULT, NULL);
201e0f1c0afSOlaf Faaland 	mmp->mmp_kstat_id = 1;
202*4348eb90SOlaf Faaland 
203*4348eb90SOlaf Faaland 	/*
204*4348eb90SOlaf Faaland 	 * mmp_write_done() calculates mmp_delay based on prior mmp_delay and
205*4348eb90SOlaf Faaland 	 * the elapsed time since the last write.  For the first mmp write,
206*4348eb90SOlaf Faaland 	 * there is no "last write", so we start with fake non-zero values.
207*4348eb90SOlaf Faaland 	 */
208*4348eb90SOlaf Faaland 	mmp->mmp_last_write = gethrtime();
209*4348eb90SOlaf Faaland 	mmp->mmp_delay = MSEC2NSEC(MMP_INTERVAL_OK(zfs_multihost_interval));
210e0f1c0afSOlaf Faaland }
211e0f1c0afSOlaf Faaland 
212e0f1c0afSOlaf Faaland void
mmp_fini(spa_t * spa)213e0f1c0afSOlaf Faaland mmp_fini(spa_t *spa)
214e0f1c0afSOlaf Faaland {
215e0f1c0afSOlaf Faaland 	mmp_thread_t *mmp = &spa->spa_mmp;
216e0f1c0afSOlaf Faaland 
217e0f1c0afSOlaf Faaland 	mutex_destroy(&mmp->mmp_thread_lock);
218e0f1c0afSOlaf Faaland 	cv_destroy(&mmp->mmp_thread_cv);
219e0f1c0afSOlaf Faaland 	mutex_destroy(&mmp->mmp_io_lock);
220e0f1c0afSOlaf Faaland }
221e0f1c0afSOlaf Faaland 
222e0f1c0afSOlaf Faaland static void
mmp_thread_enter(mmp_thread_t * mmp,callb_cpr_t * cpr)223e0f1c0afSOlaf Faaland mmp_thread_enter(mmp_thread_t *mmp, callb_cpr_t *cpr)
224e0f1c0afSOlaf Faaland {
225e0f1c0afSOlaf Faaland 	CALLB_CPR_INIT(cpr, &mmp->mmp_thread_lock, callb_generic_cpr, FTAG);
226e0f1c0afSOlaf Faaland 	mutex_enter(&mmp->mmp_thread_lock);
227e0f1c0afSOlaf Faaland }
228e0f1c0afSOlaf Faaland 
229e0f1c0afSOlaf Faaland static void
mmp_thread_exit(mmp_thread_t * mmp,kthread_t ** mpp,callb_cpr_t * cpr)230e0f1c0afSOlaf Faaland mmp_thread_exit(mmp_thread_t *mmp, kthread_t **mpp, callb_cpr_t *cpr)
231e0f1c0afSOlaf Faaland {
232e0f1c0afSOlaf Faaland 	ASSERT(*mpp != NULL);
233e0f1c0afSOlaf Faaland 	*mpp = NULL;
234e0f1c0afSOlaf Faaland 	cv_broadcast(&mmp->mmp_thread_cv);
235e0f1c0afSOlaf Faaland 	CALLB_CPR_EXIT(cpr);		/* drops &mmp->mmp_thread_lock */
236e0f1c0afSOlaf Faaland 	thread_exit();
237e0f1c0afSOlaf Faaland }
238e0f1c0afSOlaf Faaland 
239e0f1c0afSOlaf Faaland void
mmp_thread_start(spa_t * spa)240e0f1c0afSOlaf Faaland mmp_thread_start(spa_t *spa)
241e0f1c0afSOlaf Faaland {
242e0f1c0afSOlaf Faaland 	mmp_thread_t *mmp = &spa->spa_mmp;
243e0f1c0afSOlaf Faaland 
244e0f1c0afSOlaf Faaland 	if (spa_writeable(spa)) {
245e0f1c0afSOlaf Faaland 		mutex_enter(&mmp->mmp_thread_lock);
246e0f1c0afSOlaf Faaland 		if (!mmp->mmp_thread) {
247e0f1c0afSOlaf Faaland 			mmp->mmp_thread = thread_create(NULL, 0, mmp_thread,
248e0f1c0afSOlaf Faaland 			    spa, 0, &p0, TS_RUN, minclsyspri);
249*4348eb90SOlaf Faaland 			zfs_dbgmsg("MMP thread started pool '%s' "
250*4348eb90SOlaf Faaland 			    "gethrtime %llu", spa_name(spa), gethrtime());
251e0f1c0afSOlaf Faaland 		}
252e0f1c0afSOlaf Faaland 		mutex_exit(&mmp->mmp_thread_lock);
253e0f1c0afSOlaf Faaland 	}
254e0f1c0afSOlaf Faaland }
255e0f1c0afSOlaf Faaland 
256e0f1c0afSOlaf Faaland void
mmp_thread_stop(spa_t * spa)257e0f1c0afSOlaf Faaland mmp_thread_stop(spa_t *spa)
258e0f1c0afSOlaf Faaland {
259e0f1c0afSOlaf Faaland 	mmp_thread_t *mmp = &spa->spa_mmp;
260e0f1c0afSOlaf Faaland 
261e0f1c0afSOlaf Faaland 	mutex_enter(&mmp->mmp_thread_lock);
262e0f1c0afSOlaf Faaland 	mmp->mmp_thread_exiting = 1;
263e0f1c0afSOlaf Faaland 	cv_broadcast(&mmp->mmp_thread_cv);
264e0f1c0afSOlaf Faaland 
265e0f1c0afSOlaf Faaland 	while (mmp->mmp_thread) {
266e0f1c0afSOlaf Faaland 		cv_wait(&mmp->mmp_thread_cv, &mmp->mmp_thread_lock);
267e0f1c0afSOlaf Faaland 	}
268e0f1c0afSOlaf Faaland 	mutex_exit(&mmp->mmp_thread_lock);
269*4348eb90SOlaf Faaland 	zfs_dbgmsg("MMP thread stopped pool '%s' gethrtime %llu",
270*4348eb90SOlaf Faaland 	    spa_name(spa), gethrtime());
271e0f1c0afSOlaf Faaland 
272e0f1c0afSOlaf Faaland 	ASSERT(mmp->mmp_thread == NULL);
273e0f1c0afSOlaf Faaland 	mmp->mmp_thread_exiting = 0;
274e0f1c0afSOlaf Faaland }
275e0f1c0afSOlaf Faaland 
276e0f1c0afSOlaf Faaland typedef enum mmp_vdev_state_flag {
277e0f1c0afSOlaf Faaland 	MMP_FAIL_NOT_WRITABLE	= (1 << 0),
278e0f1c0afSOlaf Faaland 	MMP_FAIL_WRITE_PENDING	= (1 << 1),
279e0f1c0afSOlaf Faaland } mmp_vdev_state_flag_t;
280e0f1c0afSOlaf Faaland 
281e0f1c0afSOlaf Faaland /*
282e0f1c0afSOlaf Faaland  * Find a leaf vdev to write an MMP block to.  It must not have an outstanding
283e0f1c0afSOlaf Faaland  * mmp write (if so a new write will also likely block).  If there is no usable
284e0f1c0afSOlaf Faaland  * leaf, a nonzero error value is returned. The error value returned is a bit
285e0f1c0afSOlaf Faaland  * field.
286e0f1c0afSOlaf Faaland  *
287e0f1c0afSOlaf Faaland  * MMP_FAIL_WRITE_PENDING   One or more leaf vdevs are writeable, but have an
288e0f1c0afSOlaf Faaland  *                          outstanding MMP write.
289e0f1c0afSOlaf Faaland  * MMP_FAIL_NOT_WRITABLE    One or more leaf vdevs are not writeable.
290e0f1c0afSOlaf Faaland  */
291e0f1c0afSOlaf Faaland 
292e0f1c0afSOlaf Faaland static int
mmp_next_leaf(spa_t * spa)293e0f1c0afSOlaf Faaland mmp_next_leaf(spa_t *spa)
294e0f1c0afSOlaf Faaland {
295e0f1c0afSOlaf Faaland 	vdev_t *leaf;
296e0f1c0afSOlaf Faaland 	vdev_t *starting_leaf;
297e0f1c0afSOlaf Faaland 	int fail_mask = 0;
298e0f1c0afSOlaf Faaland 
299e0f1c0afSOlaf Faaland 	ASSERT(MUTEX_HELD(&spa->spa_mmp.mmp_io_lock));
300e0f1c0afSOlaf Faaland 	ASSERT(spa_config_held(spa, SCL_STATE, RW_READER));
301e0f1c0afSOlaf Faaland 	ASSERT(list_link_active(&spa->spa_leaf_list.list_head) == B_TRUE);
302e0f1c0afSOlaf Faaland 	ASSERT(!list_is_empty(&spa->spa_leaf_list));
303e0f1c0afSOlaf Faaland 
304e0f1c0afSOlaf Faaland 	if (spa->spa_mmp.mmp_leaf_last_gen != spa->spa_leaf_list_gen) {
305e0f1c0afSOlaf Faaland 		spa->spa_mmp.mmp_last_leaf = list_head(&spa->spa_leaf_list);
306e0f1c0afSOlaf Faaland 		spa->spa_mmp.mmp_leaf_last_gen = spa->spa_leaf_list_gen;
307e0f1c0afSOlaf Faaland 	}
308e0f1c0afSOlaf Faaland 
309e0f1c0afSOlaf Faaland 	leaf = spa->spa_mmp.mmp_last_leaf;
310e0f1c0afSOlaf Faaland 	if (leaf == NULL)
311e0f1c0afSOlaf Faaland 		leaf = list_head(&spa->spa_leaf_list);
312e0f1c0afSOlaf Faaland 	starting_leaf = leaf;
313e0f1c0afSOlaf Faaland 
314e0f1c0afSOlaf Faaland 	do {
315e0f1c0afSOlaf Faaland 		leaf = list_next(&spa->spa_leaf_list, leaf);
316e0f1c0afSOlaf Faaland 		if (leaf == NULL)
317e0f1c0afSOlaf Faaland 			leaf = list_head(&spa->spa_leaf_list);
318e0f1c0afSOlaf Faaland 
319e0f1c0afSOlaf Faaland 		if (!vdev_writeable(leaf)) {
320e0f1c0afSOlaf Faaland 			fail_mask |= MMP_FAIL_NOT_WRITABLE;
321e0f1c0afSOlaf Faaland 		} else if (leaf->vdev_mmp_pending != 0) {
322e0f1c0afSOlaf Faaland 			fail_mask |= MMP_FAIL_WRITE_PENDING;
323e0f1c0afSOlaf Faaland 		} else {
324e0f1c0afSOlaf Faaland 			spa->spa_mmp.mmp_last_leaf = leaf;
325e0f1c0afSOlaf Faaland 			return (0);
326e0f1c0afSOlaf Faaland 		}
327e0f1c0afSOlaf Faaland 	} while (leaf != starting_leaf);
328e0f1c0afSOlaf Faaland 
329e0f1c0afSOlaf Faaland 	ASSERT(fail_mask);
330e0f1c0afSOlaf Faaland 
331e0f1c0afSOlaf Faaland 	return (fail_mask);
332e0f1c0afSOlaf Faaland }
333e0f1c0afSOlaf Faaland 
334e0f1c0afSOlaf Faaland /*
335e0f1c0afSOlaf Faaland  * MMP writes are issued on a fixed schedule, but may complete at variable,
336e0f1c0afSOlaf Faaland  * much longer, intervals.  The mmp_delay captures long periods between
337e0f1c0afSOlaf Faaland  * successful writes for any reason, including disk latency, scheduling delays,
338e0f1c0afSOlaf Faaland  * etc.
339e0f1c0afSOlaf Faaland  *
340e0f1c0afSOlaf Faaland  * The mmp_delay is usually calculated as a decaying average, but if the latest
341e0f1c0afSOlaf Faaland  * delay is higher we do not average it, so that we do not hide sudden spikes
342e0f1c0afSOlaf Faaland  * which the importing host must wait for.
343e0f1c0afSOlaf Faaland  *
344e0f1c0afSOlaf Faaland  * If writes are occurring frequently, such as due to a high rate of txg syncs,
345e0f1c0afSOlaf Faaland  * the mmp_delay could become very small.  Since those short delays depend on
346e0f1c0afSOlaf Faaland  * activity we cannot count on, we never allow mmp_delay to get lower than rate
347e0f1c0afSOlaf Faaland  * expected if only mmp_thread writes occur.
348e0f1c0afSOlaf Faaland  *
349e0f1c0afSOlaf Faaland  * If an mmp write was skipped or fails, and we have already waited longer than
350e0f1c0afSOlaf Faaland  * mmp_delay, we need to update it so the next write reflects the longer delay.
351e0f1c0afSOlaf Faaland  *
352e0f1c0afSOlaf Faaland  * Do not set mmp_delay if the multihost property is not on, so as not to
353e0f1c0afSOlaf Faaland  * trigger an activity check on import.
354e0f1c0afSOlaf Faaland  */
355e0f1c0afSOlaf Faaland static void
mmp_delay_update(spa_t * spa,boolean_t write_completed)356e0f1c0afSOlaf Faaland mmp_delay_update(spa_t *spa, boolean_t write_completed)
357e0f1c0afSOlaf Faaland {
358e0f1c0afSOlaf Faaland 	mmp_thread_t *mts = &spa->spa_mmp;
359e0f1c0afSOlaf Faaland 	hrtime_t delay = gethrtime() - mts->mmp_last_write;
360e0f1c0afSOlaf Faaland 
361e0f1c0afSOlaf Faaland 	ASSERT(MUTEX_HELD(&mts->mmp_io_lock));
362e0f1c0afSOlaf Faaland 
363e0f1c0afSOlaf Faaland 	if (spa_multihost(spa) == B_FALSE) {
364e0f1c0afSOlaf Faaland 		mts->mmp_delay = 0;
365e0f1c0afSOlaf Faaland 		return;
366e0f1c0afSOlaf Faaland 	}
367e0f1c0afSOlaf Faaland 
368e0f1c0afSOlaf Faaland 	if (delay > mts->mmp_delay)
369e0f1c0afSOlaf Faaland 		mts->mmp_delay = delay;
370e0f1c0afSOlaf Faaland 
371e0f1c0afSOlaf Faaland 	if (write_completed == B_FALSE)
372e0f1c0afSOlaf Faaland 		return;
373e0f1c0afSOlaf Faaland 
374e0f1c0afSOlaf Faaland 	mts->mmp_last_write = gethrtime();
375e0f1c0afSOlaf Faaland 
376e0f1c0afSOlaf Faaland 	/*
377e0f1c0afSOlaf Faaland 	 * strictly less than, in case delay was changed above.
378e0f1c0afSOlaf Faaland 	 */
379e0f1c0afSOlaf Faaland 	if (delay < mts->mmp_delay) {
380*4348eb90SOlaf Faaland 		hrtime_t min_delay =
381*4348eb90SOlaf Faaland 		    MSEC2NSEC(MMP_INTERVAL_OK(zfs_multihost_interval)) /
382e0f1c0afSOlaf Faaland 		    MAX(1, vdev_count_leaves(spa));
383e0f1c0afSOlaf Faaland 		mts->mmp_delay = MAX(((delay + mts->mmp_delay * 127) / 128),
384e0f1c0afSOlaf Faaland 		    min_delay);
385e0f1c0afSOlaf Faaland 	}
386e0f1c0afSOlaf Faaland }
387e0f1c0afSOlaf Faaland 
388e0f1c0afSOlaf Faaland static void
mmp_write_done(zio_t * zio)389e0f1c0afSOlaf Faaland mmp_write_done(zio_t *zio)
390e0f1c0afSOlaf Faaland {
391e0f1c0afSOlaf Faaland 	spa_t *spa = zio->io_spa;
392e0f1c0afSOlaf Faaland 	vdev_t *vd = zio->io_vd;
393e0f1c0afSOlaf Faaland 	mmp_thread_t *mts = zio->io_private;
394e0f1c0afSOlaf Faaland 
395e0f1c0afSOlaf Faaland 	mutex_enter(&mts->mmp_io_lock);
396e0f1c0afSOlaf Faaland 	uint64_t mmp_kstat_id = vd->vdev_mmp_kstat_id;
397e0f1c0afSOlaf Faaland 	hrtime_t mmp_write_duration = gethrtime() - vd->vdev_mmp_pending;
398e0f1c0afSOlaf Faaland 
399e0f1c0afSOlaf Faaland 	mmp_delay_update(spa, (zio->io_error == 0));
400e0f1c0afSOlaf Faaland 
401e0f1c0afSOlaf Faaland 	vd->vdev_mmp_pending = 0;
402e0f1c0afSOlaf Faaland 	vd->vdev_mmp_kstat_id = 0;
403e0f1c0afSOlaf Faaland 
404e0f1c0afSOlaf Faaland 	mutex_exit(&mts->mmp_io_lock);
405e0f1c0afSOlaf Faaland 	spa_config_exit(spa, SCL_STATE, mmp_tag);
406e0f1c0afSOlaf Faaland 
407e0f1c0afSOlaf Faaland 	abd_free(zio->io_abd);
408e0f1c0afSOlaf Faaland }
409e0f1c0afSOlaf Faaland 
410e0f1c0afSOlaf Faaland /*
411e0f1c0afSOlaf Faaland  * When the uberblock on-disk is updated by a spa_sync,
412e0f1c0afSOlaf Faaland  * creating a new "best" uberblock, update the one stored
413e0f1c0afSOlaf Faaland  * in the mmp thread state, used for mmp writes.
414e0f1c0afSOlaf Faaland  */
415e0f1c0afSOlaf Faaland void
mmp_update_uberblock(spa_t * spa,uberblock_t * ub)416e0f1c0afSOlaf Faaland mmp_update_uberblock(spa_t *spa, uberblock_t *ub)
417e0f1c0afSOlaf Faaland {
418e0f1c0afSOlaf Faaland 	mmp_thread_t *mmp = &spa->spa_mmp;
419e0f1c0afSOlaf Faaland 
420e0f1c0afSOlaf Faaland 	mutex_enter(&mmp->mmp_io_lock);
421e0f1c0afSOlaf Faaland 	mmp->mmp_ub = *ub;
422*4348eb90SOlaf Faaland 	mmp->mmp_seq = 1;
423e0f1c0afSOlaf Faaland 	mmp->mmp_ub.ub_timestamp = gethrestime_sec();
424e0f1c0afSOlaf Faaland 	mmp_delay_update(spa, B_TRUE);
425e0f1c0afSOlaf Faaland 	mutex_exit(&mmp->mmp_io_lock);
426e0f1c0afSOlaf Faaland }
427e0f1c0afSOlaf Faaland 
428e0f1c0afSOlaf Faaland /*
429e0f1c0afSOlaf Faaland  * Choose a random vdev, label, and MMP block, and write over it
430e0f1c0afSOlaf Faaland  * with a copy of the last-synced uberblock, whose timestamp
431e0f1c0afSOlaf Faaland  * has been updated to reflect that the pool is in use.
432e0f1c0afSOlaf Faaland  */
433e0f1c0afSOlaf Faaland static void
mmp_write_uberblock(spa_t * spa)434e0f1c0afSOlaf Faaland mmp_write_uberblock(spa_t *spa)
435e0f1c0afSOlaf Faaland {
436e0f1c0afSOlaf Faaland 	int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL;
437e0f1c0afSOlaf Faaland 	mmp_thread_t *mmp = &spa->spa_mmp;
438e0f1c0afSOlaf Faaland 	uberblock_t *ub;
439e0f1c0afSOlaf Faaland 	vdev_t *vd = NULL;
440e0f1c0afSOlaf Faaland 	int label, error;
441e0f1c0afSOlaf Faaland 	uint64_t offset;
442e0f1c0afSOlaf Faaland 
443e0f1c0afSOlaf Faaland 	hrtime_t lock_acquire_time = gethrtime();
444e0f1c0afSOlaf Faaland 	spa_config_enter(spa, SCL_STATE, mmp_tag, RW_READER);
445e0f1c0afSOlaf Faaland 	lock_acquire_time = gethrtime() - lock_acquire_time;
446e0f1c0afSOlaf Faaland 	if (lock_acquire_time > (MSEC2NSEC(MMP_MIN_INTERVAL) / 10))
447*4348eb90SOlaf Faaland 		zfs_dbgmsg("MMP SCL_STATE acquisition pool '%s' took %llu ns "
448*4348eb90SOlaf Faaland 		    "gethrtime %llu", spa_name(spa), lock_acquire_time,
449*4348eb90SOlaf Faaland 		    gethrtime());
450e0f1c0afSOlaf Faaland 
451e0f1c0afSOlaf Faaland 	mutex_enter(&mmp->mmp_io_lock);
452e0f1c0afSOlaf Faaland 
453e0f1c0afSOlaf Faaland 	error = mmp_next_leaf(spa);
454e0f1c0afSOlaf Faaland 
455e0f1c0afSOlaf Faaland 	/*
456e0f1c0afSOlaf Faaland 	 * spa_mmp_history has two types of entries:
457e0f1c0afSOlaf Faaland 	 * Issued MMP write: records time issued, error status, etc.
458e0f1c0afSOlaf Faaland 	 * Skipped MMP write: an MMP write could not be issued because no
459e0f1c0afSOlaf Faaland 	 * suitable leaf vdev was available.  See comment above struct
460e0f1c0afSOlaf Faaland 	 * spa_mmp_history for details.
461e0f1c0afSOlaf Faaland 	 */
462e0f1c0afSOlaf Faaland 
463e0f1c0afSOlaf Faaland 	if (error) {
464e0f1c0afSOlaf Faaland 		mmp_delay_update(spa, B_FALSE);
465e0f1c0afSOlaf Faaland 		if (mmp->mmp_skip_error == error) {
466e0f1c0afSOlaf Faaland 			/*
467e0f1c0afSOlaf Faaland 			 * ZoL porting note: the following is TBD
468e0f1c0afSOlaf Faaland 			 * spa_mmp_history_set_skip(spa, mmp->mmp_kstat_id - 1);
469e0f1c0afSOlaf Faaland 			 */
470e0f1c0afSOlaf Faaland 		} else {
471e0f1c0afSOlaf Faaland 			mmp->mmp_skip_error = error;
472e0f1c0afSOlaf Faaland 			/*
473e0f1c0afSOlaf Faaland 			 * ZoL porting note: the following is TBD
474e0f1c0afSOlaf Faaland 			 * spa_mmp_history_add(spa, mmp->mmp_ub.ub_txg,
475e0f1c0afSOlaf Faaland 			 * gethrestime_sec(), mmp->mmp_delay, NULL, 0,
476e0f1c0afSOlaf Faaland 			 * mmp->mmp_kstat_id++, error);
477e0f1c0afSOlaf Faaland 			 */
478*4348eb90SOlaf Faaland 			zfs_dbgmsg("MMP error choosing leaf pool '%s' "
479*4348eb90SOlaf Faaland 			    "gethrtime %llu fail_mask %#x", spa_name(spa),
480*4348eb90SOlaf Faaland 			    gethrtime(), error);
481e0f1c0afSOlaf Faaland 		}
482e0f1c0afSOlaf Faaland 		mutex_exit(&mmp->mmp_io_lock);
483e0f1c0afSOlaf Faaland 		spa_config_exit(spa, SCL_STATE, mmp_tag);
484e0f1c0afSOlaf Faaland 		return;
485e0f1c0afSOlaf Faaland 	}
486e0f1c0afSOlaf Faaland 
487e0f1c0afSOlaf Faaland 	vd = spa->spa_mmp.mmp_last_leaf;
488*4348eb90SOlaf Faaland 	if (mmp->mmp_skip_error != 0) {
489e0f1c0afSOlaf Faaland 		mmp->mmp_skip_error = 0;
490*4348eb90SOlaf Faaland 		zfs_dbgmsg("MMP write after skipping due to unavailable "
491*4348eb90SOlaf Faaland 		    "leaves, pool '%s' gethrtime %llu leaf %#llu",
492*4348eb90SOlaf Faaland 		    spa_name(spa), gethrtime(), vd->vdev_guid);
493*4348eb90SOlaf Faaland 	}
494e0f1c0afSOlaf Faaland 
495e0f1c0afSOlaf Faaland 	if (mmp->mmp_zio_root == NULL)
496e0f1c0afSOlaf Faaland 		mmp->mmp_zio_root = zio_root(spa, NULL, NULL,
497e0f1c0afSOlaf Faaland 		    flags | ZIO_FLAG_GODFATHER);
498e0f1c0afSOlaf Faaland 
499*4348eb90SOlaf Faaland 	if (mmp->mmp_ub.ub_timestamp != gethrestime_sec()) {
500*4348eb90SOlaf Faaland 		/*
501*4348eb90SOlaf Faaland 		 * Want to reset mmp_seq when timestamp advances because after
502*4348eb90SOlaf Faaland 		 * an mmp_seq wrap new values will not be chosen by
503*4348eb90SOlaf Faaland 		 * uberblock_compare() as the "best".
504*4348eb90SOlaf Faaland 		 */
505*4348eb90SOlaf Faaland 		mmp->mmp_ub.ub_timestamp = gethrestime_sec();
506*4348eb90SOlaf Faaland 		mmp->mmp_seq = 1;
507*4348eb90SOlaf Faaland 	}
508*4348eb90SOlaf Faaland 
509e0f1c0afSOlaf Faaland 	ub = &mmp->mmp_ub;
510e0f1c0afSOlaf Faaland 	ub->ub_mmp_magic = MMP_MAGIC;
511e0f1c0afSOlaf Faaland 	ub->ub_mmp_delay = mmp->mmp_delay;
512*4348eb90SOlaf Faaland 	ub->ub_mmp_config = MMP_SEQ_SET(mmp->mmp_seq) |
513*4348eb90SOlaf Faaland 	    MMP_INTERVAL_SET(MMP_INTERVAL_OK(zfs_multihost_interval)) |
514*4348eb90SOlaf Faaland 	    MMP_FAIL_INT_SET(MMP_FAIL_INTVS_OK(
515*4348eb90SOlaf Faaland 	    zfs_multihost_fail_intervals));
516e0f1c0afSOlaf Faaland 	vd->vdev_mmp_pending = gethrtime();
517e0f1c0afSOlaf Faaland 	vd->vdev_mmp_kstat_id = mmp->mmp_kstat_id;
518e0f1c0afSOlaf Faaland 
519e0f1c0afSOlaf Faaland 	zio_t *zio  = zio_null(mmp->mmp_zio_root, spa, NULL, NULL, NULL, flags);
520e0f1c0afSOlaf Faaland 	abd_t *ub_abd = abd_alloc_for_io(VDEV_UBERBLOCK_SIZE(vd), B_TRUE);
521e0f1c0afSOlaf Faaland 	abd_zero(ub_abd, VDEV_UBERBLOCK_SIZE(vd));
522e0f1c0afSOlaf Faaland 	abd_copy_from_buf(ub_abd, ub, sizeof (uberblock_t));
523e0f1c0afSOlaf Faaland 
524*4348eb90SOlaf Faaland 	mmp->mmp_seq++;
525e0f1c0afSOlaf Faaland 	mmp->mmp_kstat_id++;
526e0f1c0afSOlaf Faaland 	mutex_exit(&mmp->mmp_io_lock);
527e0f1c0afSOlaf Faaland 
528e0f1c0afSOlaf Faaland 	offset = VDEV_UBERBLOCK_OFFSET(vd, VDEV_UBERBLOCK_COUNT(vd) -
529e0f1c0afSOlaf Faaland 	    MMP_BLOCKS_PER_LABEL + spa_get_random(MMP_BLOCKS_PER_LABEL));
530e0f1c0afSOlaf Faaland 
531e0f1c0afSOlaf Faaland 	label = spa_get_random(VDEV_LABELS);
532e0f1c0afSOlaf Faaland 	vdev_label_write(zio, vd, label, ub_abd, offset,
533e0f1c0afSOlaf Faaland 	    VDEV_UBERBLOCK_SIZE(vd), mmp_write_done, mmp,
534e0f1c0afSOlaf Faaland 	    flags | ZIO_FLAG_DONT_PROPAGATE);
535e0f1c0afSOlaf Faaland 
536e0f1c0afSOlaf Faaland 	/*
537e0f1c0afSOlaf Faaland 	 * ZoL porting note: the following is TBD
538e0f1c0afSOlaf Faaland 	 * (void) spa_mmp_history_add(spa, ub->ub_txg, ub->ub_timestamp,
539e0f1c0afSOlaf Faaland 	 * ub->ub_mmp_delay, vd, label, vd->vdev_mmp_kstat_id, 0);
540e0f1c0afSOlaf Faaland 	 */
541e0f1c0afSOlaf Faaland 
542e0f1c0afSOlaf Faaland 	zio_nowait(zio);
543e0f1c0afSOlaf Faaland }
544e0f1c0afSOlaf Faaland 
545e0f1c0afSOlaf Faaland static void
mmp_thread(void * arg)546e0f1c0afSOlaf Faaland mmp_thread(void *arg)
547e0f1c0afSOlaf Faaland {
548e0f1c0afSOlaf Faaland 	spa_t *spa = (spa_t *)arg;
549e0f1c0afSOlaf Faaland 	mmp_thread_t *mmp = &spa->spa_mmp;
550*4348eb90SOlaf Faaland 	boolean_t suspended = spa_suspended(spa);
551*4348eb90SOlaf Faaland 	boolean_t multihost = spa_multihost(spa);
552*4348eb90SOlaf Faaland 	uint64_t mmp_interval = MSEC2NSEC(MMP_INTERVAL_OK(
553*4348eb90SOlaf Faaland 	    zfs_multihost_interval));
554*4348eb90SOlaf Faaland 	uint32_t mmp_fail_intervals = MMP_FAIL_INTVS_OK(
555*4348eb90SOlaf Faaland 	    zfs_multihost_fail_intervals);
556*4348eb90SOlaf Faaland 	hrtime_t mmp_fail_ns = mmp_fail_intervals * mmp_interval;
557*4348eb90SOlaf Faaland 	boolean_t last_spa_suspended = suspended;
558*4348eb90SOlaf Faaland 	boolean_t last_spa_multihost = multihost;
559*4348eb90SOlaf Faaland 	uint64_t last_mmp_interval = mmp_interval;
560*4348eb90SOlaf Faaland 	uint32_t last_mmp_fail_intervals = mmp_fail_intervals;
561*4348eb90SOlaf Faaland 	hrtime_t last_mmp_fail_ns = mmp_fail_ns;
562e0f1c0afSOlaf Faaland 	callb_cpr_t cpr;
563*4348eb90SOlaf Faaland 	int skip_wait = 0;
564e0f1c0afSOlaf Faaland 
565e0f1c0afSOlaf Faaland 	mmp_thread_enter(mmp, &cpr);
566e0f1c0afSOlaf Faaland 
567e0f1c0afSOlaf Faaland 	while (!mmp->mmp_thread_exiting) {
568*4348eb90SOlaf Faaland 		hrtime_t next_time = gethrtime() +
569*4348eb90SOlaf Faaland 		    MSEC2NSEC(MMP_DEFAULT_INTERVAL);
570*4348eb90SOlaf Faaland 		int leaves = MAX(vdev_count_leaves(spa), 1);
571*4348eb90SOlaf Faaland 
572*4348eb90SOlaf Faaland 		/* Detect changes in tunables or state */
573*4348eb90SOlaf Faaland 
574*4348eb90SOlaf Faaland 		last_spa_suspended = suspended;
575*4348eb90SOlaf Faaland 		last_spa_multihost = multihost;
576*4348eb90SOlaf Faaland 		suspended = spa_suspended(spa);
577*4348eb90SOlaf Faaland 		multihost = spa_multihost(spa);
578*4348eb90SOlaf Faaland 
579*4348eb90SOlaf Faaland 		last_mmp_interval = mmp_interval;
580*4348eb90SOlaf Faaland 		last_mmp_fail_intervals = mmp_fail_intervals;
581*4348eb90SOlaf Faaland 		last_mmp_fail_ns = mmp_fail_ns;
582*4348eb90SOlaf Faaland 		mmp_interval = MSEC2NSEC(MMP_INTERVAL_OK(
583*4348eb90SOlaf Faaland 		    zfs_multihost_interval));
584*4348eb90SOlaf Faaland 		mmp_fail_intervals = MMP_FAIL_INTVS_OK(
585*4348eb90SOlaf Faaland 		    zfs_multihost_fail_intervals);
586*4348eb90SOlaf Faaland 
587*4348eb90SOlaf Faaland 		/* Smooth so pool is not suspended when reducing tunables */
588*4348eb90SOlaf Faaland 		if (mmp_fail_intervals * mmp_interval < mmp_fail_ns) {
589*4348eb90SOlaf Faaland 			mmp_fail_ns = (mmp_fail_ns * 31 +
590*4348eb90SOlaf Faaland 			    mmp_fail_intervals * mmp_interval) / 32;
591*4348eb90SOlaf Faaland 		} else {
592*4348eb90SOlaf Faaland 			mmp_fail_ns = mmp_fail_intervals *
593*4348eb90SOlaf Faaland 			    mmp_interval;
594*4348eb90SOlaf Faaland 		}
595*4348eb90SOlaf Faaland 
596*4348eb90SOlaf Faaland 		if (mmp_interval != last_mmp_interval ||
597*4348eb90SOlaf Faaland 		    mmp_fail_intervals != last_mmp_fail_intervals) {
598*4348eb90SOlaf Faaland 			/*
599*4348eb90SOlaf Faaland 			 * We want other hosts to see new tunables as quickly as
600*4348eb90SOlaf Faaland 			 * possible.  Write out at higher frequency than usual.
601*4348eb90SOlaf Faaland 			 */
602*4348eb90SOlaf Faaland 			skip_wait += leaves;
603*4348eb90SOlaf Faaland 		}
604e0f1c0afSOlaf Faaland 
605e0f1c0afSOlaf Faaland 		if (multihost)
606*4348eb90SOlaf Faaland 			next_time = gethrtime() + mmp_interval / leaves;
607*4348eb90SOlaf Faaland 
608*4348eb90SOlaf Faaland 		if (mmp_fail_ns != last_mmp_fail_ns) {
609*4348eb90SOlaf Faaland 			zfs_dbgmsg("MMP interval change pool '%s' "
610*4348eb90SOlaf Faaland 			    "gethrtime %llu last_mmp_interval %llu "
611*4348eb90SOlaf Faaland 			    "mmp_interval %llu last_mmp_fail_intervals %u "
612*4348eb90SOlaf Faaland 			    "mmp_fail_intervals %u mmp_fail_ns %llu "
613*4348eb90SOlaf Faaland 			    "skip_wait %d leaves %d next_time %llu",
614*4348eb90SOlaf Faaland 			    spa_name(spa), gethrtime(), last_mmp_interval,
615*4348eb90SOlaf Faaland 			    mmp_interval, last_mmp_fail_intervals,
616*4348eb90SOlaf Faaland 			    mmp_fail_intervals, mmp_fail_ns, skip_wait, leaves,
617*4348eb90SOlaf Faaland 			    next_time);
618*4348eb90SOlaf Faaland 		}
619e0f1c0afSOlaf Faaland 
620e0f1c0afSOlaf Faaland 		/*
621e0f1c0afSOlaf Faaland 		 * MMP off => on, or suspended => !suspended:
622e0f1c0afSOlaf Faaland 		 * No writes occurred recently.  Update mmp_last_write to give
623e0f1c0afSOlaf Faaland 		 * us some time to try.
624e0f1c0afSOlaf Faaland 		 */
625e0f1c0afSOlaf Faaland 		if ((!last_spa_multihost && multihost) ||
626e0f1c0afSOlaf Faaland 		    (last_spa_suspended && !suspended)) {
627*4348eb90SOlaf Faaland 			zfs_dbgmsg("MMP state change pool '%s': gethrtime %llu "
628*4348eb90SOlaf Faaland 			    "last_spa_multihost %u multihost %u "
629*4348eb90SOlaf Faaland 			    "last_spa_suspended %u suspended %u",
630*4348eb90SOlaf Faaland 			    spa_name(spa), last_spa_multihost, multihost,
631*4348eb90SOlaf Faaland 			    last_spa_suspended, suspended);
632e0f1c0afSOlaf Faaland 			mutex_enter(&mmp->mmp_io_lock);
633e0f1c0afSOlaf Faaland 			mmp->mmp_last_write = gethrtime();
634*4348eb90SOlaf Faaland 			mmp->mmp_delay = mmp_interval;
635e0f1c0afSOlaf Faaland 			mutex_exit(&mmp->mmp_io_lock);
636e0f1c0afSOlaf Faaland 		}
637e0f1c0afSOlaf Faaland 
638e0f1c0afSOlaf Faaland 		/*
639e0f1c0afSOlaf Faaland 		 * MMP on => off:
640e0f1c0afSOlaf Faaland 		 * mmp_delay == 0 tells importing node to skip activity check.
641e0f1c0afSOlaf Faaland 		 */
642e0f1c0afSOlaf Faaland 		if (last_spa_multihost && !multihost) {
643e0f1c0afSOlaf Faaland 			mutex_enter(&mmp->mmp_io_lock);
644e0f1c0afSOlaf Faaland 			mmp->mmp_delay = 0;
645e0f1c0afSOlaf Faaland 			mutex_exit(&mmp->mmp_io_lock);
646e0f1c0afSOlaf Faaland 		}
647e0f1c0afSOlaf Faaland 
648e0f1c0afSOlaf Faaland 		/*
649e0f1c0afSOlaf Faaland 		 * Suspend the pool if no MMP write has succeeded in over
650e0f1c0afSOlaf Faaland 		 * mmp_interval * mmp_fail_intervals nanoseconds.
651e0f1c0afSOlaf Faaland 		 */
652*4348eb90SOlaf Faaland 		if (multihost && !suspended && mmp_fail_intervals &&
653*4348eb90SOlaf Faaland 		    (gethrtime() - mmp->mmp_last_write) > mmp_fail_ns) {
654*4348eb90SOlaf Faaland 			zfs_dbgmsg("MMP suspending pool '%s': gethrtime %llu "
655*4348eb90SOlaf Faaland 			    "mmp_last_write %llu mmp_interval %llu "
656*4348eb90SOlaf Faaland 			    "mmp_fail_intervals %llu mmp_fail_ns %llu",
657*4348eb90SOlaf Faaland 			    spa_name(spa), (u_longlong_t)gethrtime(),
658*4348eb90SOlaf Faaland 			    (u_longlong_t)mmp->mmp_last_write,
659*4348eb90SOlaf Faaland 			    (u_longlong_t)mmp_interval,
660*4348eb90SOlaf Faaland 			    (u_longlong_t)mmp_fail_intervals,
661*4348eb90SOlaf Faaland 			    (u_longlong_t)mmp_fail_ns);
662e0f1c0afSOlaf Faaland 			cmn_err(CE_WARN, "MMP writes to pool '%s' have not "
663*4348eb90SOlaf Faaland 			    "succeeded in over %llu ms; suspending pool. "
664*4348eb90SOlaf Faaland 			    "Hrtime %llu",
665e0f1c0afSOlaf Faaland 			    spa_name(spa),
666*4348eb90SOlaf Faaland 			    NSEC2MSEC(gethrtime() - mmp->mmp_last_write),
667*4348eb90SOlaf Faaland 			    gethrtime());
668e0f1c0afSOlaf Faaland 			zio_suspend(spa, NULL, ZIO_SUSPEND_MMP);
669e0f1c0afSOlaf Faaland 		}
670e0f1c0afSOlaf Faaland 
671e0f1c0afSOlaf Faaland 		if (multihost && !suspended)
672e0f1c0afSOlaf Faaland 			mmp_write_uberblock(spa);
673e0f1c0afSOlaf Faaland 
674*4348eb90SOlaf Faaland 		if (skip_wait > 0) {
675*4348eb90SOlaf Faaland 			next_time = gethrtime() + MSEC2NSEC(MMP_MIN_INTERVAL) /
676*4348eb90SOlaf Faaland 			    leaves;
677*4348eb90SOlaf Faaland 			skip_wait--;
678*4348eb90SOlaf Faaland 		}
679*4348eb90SOlaf Faaland 
680e0f1c0afSOlaf Faaland 		CALLB_CPR_SAFE_BEGIN(&cpr);
681e0f1c0afSOlaf Faaland 		(void) cv_timedwait_sig_hrtime(&mmp->mmp_thread_cv,
682e0f1c0afSOlaf Faaland 		    &mmp->mmp_thread_lock, next_time);
683e0f1c0afSOlaf Faaland 		CALLB_CPR_SAFE_END(&cpr, &mmp->mmp_thread_lock);
684e0f1c0afSOlaf Faaland 	}
685e0f1c0afSOlaf Faaland 
686e0f1c0afSOlaf Faaland 	/* Outstanding writes are allowed to complete. */
687e0f1c0afSOlaf Faaland 	if (mmp->mmp_zio_root)
688e0f1c0afSOlaf Faaland 		zio_wait(mmp->mmp_zio_root);
689e0f1c0afSOlaf Faaland 
690e0f1c0afSOlaf Faaland 	mmp->mmp_zio_root = NULL;
691e0f1c0afSOlaf Faaland 	mmp_thread_exit(mmp, &mmp->mmp_thread, &cpr);
692e0f1c0afSOlaf Faaland }
693e0f1c0afSOlaf Faaland 
694e0f1c0afSOlaf Faaland /*
695e0f1c0afSOlaf Faaland  * Signal the MMP thread to wake it, when it is sleeping on
696e0f1c0afSOlaf Faaland  * its cv.  Used when some module parameter has changed and
697e0f1c0afSOlaf Faaland  * we want the thread to know about it.
698e0f1c0afSOlaf Faaland  * Only signal if the pool is active and mmp thread is
699e0f1c0afSOlaf Faaland  * running, otherwise there is no thread to wake.
700e0f1c0afSOlaf Faaland  */
701e0f1c0afSOlaf Faaland static void
mmp_signal_thread(spa_t * spa)702e0f1c0afSOlaf Faaland mmp_signal_thread(spa_t *spa)
703e0f1c0afSOlaf Faaland {
704e0f1c0afSOlaf Faaland 	mmp_thread_t *mmp = &spa->spa_mmp;
705e0f1c0afSOlaf Faaland 
706e0f1c0afSOlaf Faaland 	mutex_enter(&mmp->mmp_thread_lock);
707e0f1c0afSOlaf Faaland 	if (mmp->mmp_thread)
708e0f1c0afSOlaf Faaland 		cv_broadcast(&mmp->mmp_thread_cv);
709e0f1c0afSOlaf Faaland 	mutex_exit(&mmp->mmp_thread_lock);
710e0f1c0afSOlaf Faaland }
711e0f1c0afSOlaf Faaland 
712e0f1c0afSOlaf Faaland void
mmp_signal_all_threads(void)713e0f1c0afSOlaf Faaland mmp_signal_all_threads(void)
714e0f1c0afSOlaf Faaland {
715e0f1c0afSOlaf Faaland 	spa_t *spa = NULL;
716e0f1c0afSOlaf Faaland 
717e0f1c0afSOlaf Faaland 	mutex_enter(&spa_namespace_lock);
718e0f1c0afSOlaf Faaland 	while ((spa = spa_next(spa))) {
719e0f1c0afSOlaf Faaland 		if (spa->spa_state == POOL_STATE_ACTIVE)
720e0f1c0afSOlaf Faaland 			mmp_signal_thread(spa);
721e0f1c0afSOlaf Faaland 	}
722e0f1c0afSOlaf Faaland 	mutex_exit(&spa_namespace_lock);
723e0f1c0afSOlaf Faaland }
724