1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3 * This file and its contents are supplied under the terms of the
4 * Common Development and Distribution License ("CDDL"), version 1.0.
5 * You may only use this file in accordance with the terms of version
6 * 1.0 of the CDDL.
7 *
8 * A full copy of the text of the CDDL should have accompanied this
9 * source. A copy of the CDDL is also available via the Internet at
10 * https://opensource.org/license/CDDL-1.0.
11 */
12 /*
13 * Copyright (c) 2017 by Lawrence Livermore National Security, LLC.
14 */
15
16 #include <sys/abd.h>
17 #include <sys/mmp.h>
18 #include <sys/spa.h>
19 #include <sys/spa_impl.h>
20 #include <sys/time.h>
21 #include <sys/vdev.h>
22 #include <sys/vdev_impl.h>
23 #include <sys/zfs_context.h>
24 #include <sys/callb.h>
25
26 /*
27 * Multi-Modifier Protection (MMP) attempts to prevent a user from importing
28 * or opening a pool on more than one host at a time. In particular, it
29 * prevents "zpool import -f" on a host from succeeding while the pool is
30 * already imported on another host. There are many other ways in which a
31 * device could be used by two hosts for different purposes at the same time
32 * resulting in pool damage. This implementation does not attempt to detect
33 * those cases.
34 *
35 * MMP operates by ensuring there are frequent visible changes on disk (a
36 * "heartbeat") at all times. And by altering the import process to check
37 * for these changes and failing the import when they are detected. This
38 * functionality is enabled by setting the 'multihost' pool property to on.
39 *
40 * Uberblocks written by the txg_sync thread always go into the first
41 * (N-MMP_BLOCKS_PER_LABEL) slots, the remaining slots are reserved for MMP.
42 * They are used to hold uberblocks which are exactly the same as the last
43 * synced uberblock except that the ub_timestamp and mmp_config are frequently
44 * updated. Like all other uberblocks, the slot is written with an embedded
45 * checksum, and slots with invalid checksums are ignored. This provides the
46 * "heartbeat", with no risk of overwriting good uberblocks that must be
47 * preserved, e.g. previous txgs and associated block pointers.
48 *
49 * Three optional fields are added to uberblock structure; ub_mmp_magic,
50 * ub_mmp_config, and ub_mmp_delay. The ub_mmp_magic value allows zfs to tell
51 * whether the other ub_mmp_* fields are valid. The ub_mmp_config field tells
52 * the importing host the settings of zfs_multihost_interval and
53 * zfs_multihost_fail_intervals on the host which last had (or currently has)
54 * the pool imported. These determine how long a host must wait to detect
55 * activity in the pool, before concluding the pool is not in use. The
56 * mmp_delay field is a decaying average of the amount of time between
57 * completion of successive MMP writes, in nanoseconds. It indicates whether
58 * MMP is enabled.
59 *
60 * During import an activity test may now be performed to determine if
61 * the pool is in use. The activity test is typically required if the
62 * ZPOOL_CONFIG_HOSTID does not match the system hostid, the pool state is
63 * POOL_STATE_ACTIVE, and the pool is not a root pool.
64 *
65 * The activity test finds the "best" uberblock (highest txg, timestamp, and, if
66 * ub_mmp_magic is valid, sequence number from ub_mmp_config). It then waits
67 * some time, and finds the "best" uberblock again. If any of the mentioned
68 * fields have different values in the newly read uberblock, the pool is in use
69 * by another host and the import fails. In order to assure the accuracy of the
70 * activity test, the default values result in an activity test duration of 20x
71 * the mmp write interval.
72 *
73 * The duration of the "zpool import" activity test depends on the information
74 * available in the "best" uberblock:
75 *
76 * 1) If uberblock was written by zfs-0.8 or newer and fail_intervals > 0:
77 * ub_mmp_config.fail_intervals * ub_mmp_config.multihost_interval * 2
78 *
79 * In this case, a weak guarantee is provided. Since the host which last had
80 * the pool imported will suspend the pool if no mmp writes land within
81 * fail_intervals * multihost_interval ms, the absence of writes during that
82 * time means either the pool is not imported, or it is imported but the pool
83 * is suspended and no further writes will occur.
84 *
85 * Note that resuming the suspended pool on the remote host would invalidate
86 * this guarantee, and so it is not allowed.
87 *
88 * The factor of 2 provides a conservative safety factor and derives from
89 * MMP_IMPORT_SAFETY_FACTOR;
90 *
91 * 2) If uberblock was written by zfs-0.8 or newer and fail_intervals == 0:
92 * (ub_mmp_config.multihost_interval + ub_mmp_delay) *
93 * zfs_multihost_import_intervals
94 *
95 * In this case no guarantee can provided. However, as long as some devices
96 * are healthy and connected, it is likely that at least one write will land
97 * within (multihost_interval + mmp_delay) because multihost_interval is
98 * enough time for a write to be attempted to each leaf vdev, and mmp_delay
99 * is enough for one to land, based on past delays. Multiplying by
100 * zfs_multihost_import_intervals provides a conservative safety factor.
101 *
102 * 3) If uberblock was written by zfs-0.7:
103 * (zfs_multihost_interval + ub_mmp_delay) * zfs_multihost_import_intervals
104 *
105 * The same logic as case #2 applies, but we do not know remote tunables.
106 *
107 * We use the local value for zfs_multihost_interval because the original MMP
108 * did not record this value in the uberblock.
109 *
110 * ub_mmp_delay >= (zfs_multihost_interval / leaves), so if the other host
111 * has a much larger zfs_multihost_interval set, ub_mmp_delay will reflect
112 * that. We will have waited enough time for zfs_multihost_import_intervals
113 * writes to be issued and all but one to land.
114 *
115 * single device pool example delays
116 *
117 * import_delay = (1 + 1) * 20 = 40s #defaults, no I/O delay
118 * import_delay = (1 + 10) * 20 = 220s #defaults, 10s I/O delay
119 * import_delay = (10 + 10) * 20 = 400s #10s multihost_interval,
120 * no I/O delay
121 * 100 device pool example delays
122 *
123 * import_delay = (1 + .01) * 20 = 20s #defaults, no I/O delay
124 * import_delay = (1 + 10) * 20 = 220s #defaults, 10s I/O delay
125 * import_delay = (10 + .1) * 20 = 202s #10s multihost_interval,
126 * no I/O delay
127 *
128 * 4) Otherwise, this uberblock was written by a pre-MMP zfs:
129 * zfs_multihost_import_intervals * zfs_multihost_interval
130 *
131 * In this case local tunables are used. By default this product = 10s, long
132 * enough for a pool with any activity at all to write at least one
133 * uberblock. No guarantee can be provided.
134 *
135 * Additionally, the duration is then extended by a random 25% to attempt to to
136 * detect simultaneous imports. For example, if both partner hosts are rebooted
137 * at the same time and automatically attempt to import the pool.
138 *
139 * Once the read-only activity check completes and the pool is determined to
140 * be inactive a second check is performed to claim the pool. During this
141 * phase the host writes out MMP uberblocks to each of the devices which are
142 * identical to the best uberblock but with a randomly selected sequence id.
143 * The "best" uberblock is then read back and it must contain this new sequence
144 * number. This check is performed multiple times to ensure that there is
145 * no window where a concurrently importing system can incorrectly determine
146 * the pool to be inactive.
147 */
148
149 /*
150 * Used to control the frequency of mmp writes which are performed when the
151 * 'multihost' pool property is on. This is one factor used to determine the
152 * length of the activity check during import.
153 *
154 * On average an mmp write will be issued for each leaf vdev every
155 * zfs_multihost_interval milliseconds. In practice, the observed period can
156 * vary with the I/O load and this observed value is the ub_mmp_delay which is
157 * stored in the uberblock. The minimum allowed value is 100 ms.
158 */
159 uint64_t zfs_multihost_interval = MMP_DEFAULT_INTERVAL;
160
161 /*
162 * Used to control the duration of the activity test on import. Smaller values
163 * of zfs_multihost_import_intervals will reduce the import time but increase
164 * the risk of failing to detect an active pool. The total activity check time
165 * is never allowed to drop below one second. A value of 0 is ignored and
166 * treated as if it was set to 1.
167 */
168 uint_t zfs_multihost_import_intervals = MMP_DEFAULT_IMPORT_INTERVALS;
169
170 /*
171 * Controls the behavior of the pool when mmp write failures or delays are
172 * detected.
173 *
174 * When zfs_multihost_fail_intervals = 0, mmp write failures or delays are
175 * ignored. The failures will still be reported to the ZED which depending on
176 * its configuration may take action such as suspending the pool or taking a
177 * device offline.
178 *
179 * When zfs_multihost_fail_intervals > 0, the pool will be suspended if
180 * zfs_multihost_fail_intervals * zfs_multihost_interval milliseconds pass
181 * without a successful mmp write. This guarantees the activity test will see
182 * mmp writes if the pool is imported. A value of 1 is ignored and treated as
183 * if it was set to 2, because a single leaf vdev pool will issue a write once
184 * per multihost_interval and thus any variation in latency would cause the
185 * pool to be suspended.
186 */
187 uint_t zfs_multihost_fail_intervals = MMP_DEFAULT_FAIL_INTERVALS;
188
189 #ifndef _KERNEL
190 /*
191 * Manual recovery only, set by zhack. Drops the mirror legs this host
192 * cannot open from the uberblock claim's required write count, so a pool
193 * whose config still expects legs that died with a peer host can be
194 * imported by hand. Every leg which can be opened is still required.
195 *
196 * This is deliberately absent from the kernel module. The write, the wait
197 * and the re-read are untouched, so a competing importer which shares any
198 * visibility with us is still caught; a live peer whose legs are all
199 * invisible from here is not, and cannot be by any write-and-read scheme.
200 * That residual is why this is a manual operation guarded by fencing.
201 */
202 boolean_t mmp_claim_relaxed = B_FALSE;
203 #endif
204
205 static const void *const mmp_tag = "mmp_write_uberblock";
206 static __attribute__((noreturn)) void mmp_thread(void *arg);
207
208 void
mmp_init(spa_t * spa)209 mmp_init(spa_t *spa)
210 {
211 mmp_thread_t *mmp = &spa->spa_mmp;
212
213 mutex_init(&mmp->mmp_thread_lock, NULL, MUTEX_DEFAULT, NULL);
214 cv_init(&mmp->mmp_thread_cv, NULL, CV_DEFAULT, NULL);
215 mutex_init(&mmp->mmp_io_lock, NULL, MUTEX_DEFAULT, NULL);
216 mmp->mmp_kstat_id = 1;
217 }
218
219 void
mmp_fini(spa_t * spa)220 mmp_fini(spa_t *spa)
221 {
222 mmp_thread_t *mmp = &spa->spa_mmp;
223
224 mutex_destroy(&mmp->mmp_thread_lock);
225 cv_destroy(&mmp->mmp_thread_cv);
226 mutex_destroy(&mmp->mmp_io_lock);
227 }
228
229 static void
mmp_thread_enter(mmp_thread_t * mmp,callb_cpr_t * cpr)230 mmp_thread_enter(mmp_thread_t *mmp, callb_cpr_t *cpr)
231 {
232 CALLB_CPR_INIT(cpr, &mmp->mmp_thread_lock, callb_generic_cpr, FTAG);
233 mutex_enter(&mmp->mmp_thread_lock);
234 }
235
236 static void
mmp_thread_exit(mmp_thread_t * mmp,kthread_t ** mpp,callb_cpr_t * cpr)237 mmp_thread_exit(mmp_thread_t *mmp, kthread_t **mpp, callb_cpr_t *cpr)
238 {
239 ASSERT(*mpp != NULL);
240 *mpp = NULL;
241 cv_broadcast(&mmp->mmp_thread_cv);
242 CALLB_CPR_EXIT(cpr); /* drops &mmp->mmp_thread_lock */
243 }
244
245 void
mmp_thread_start(spa_t * spa)246 mmp_thread_start(spa_t *spa)
247 {
248 mmp_thread_t *mmp = &spa->spa_mmp;
249
250 if (spa_writeable(spa)) {
251 mutex_enter(&mmp->mmp_thread_lock);
252 if (!mmp->mmp_thread) {
253 mmp->mmp_thread = thread_create(NULL, 0, mmp_thread,
254 spa, 0, &p0, TS_RUN, defclsyspri);
255 zfs_dbgmsg("mmp: mmp thread started spa=%s "
256 "gethrtime=%llu", spa_name(spa), gethrtime());
257 }
258 mutex_exit(&mmp->mmp_thread_lock);
259 }
260 }
261
262 void
mmp_thread_stop(spa_t * spa)263 mmp_thread_stop(spa_t *spa)
264 {
265 mmp_thread_t *mmp = &spa->spa_mmp;
266
267 mutex_enter(&mmp->mmp_thread_lock);
268 mmp->mmp_thread_exiting = 1;
269 cv_broadcast(&mmp->mmp_thread_cv);
270
271 while (mmp->mmp_thread) {
272 cv_wait(&mmp->mmp_thread_cv, &mmp->mmp_thread_lock);
273 }
274 mutex_exit(&mmp->mmp_thread_lock);
275 zfs_dbgmsg("mmp: mmp thread stopped spa=%s gethrtime=%llu",
276 spa_name(spa), gethrtime());
277
278 ASSERT0P(mmp->mmp_thread);
279 mmp->mmp_thread_exiting = 0;
280 }
281
282 typedef enum mmp_vdev_state_flag {
283 MMP_FAIL_NOT_WRITABLE = (1 << 0),
284 MMP_FAIL_WRITE_PENDING = (1 << 1),
285 } mmp_vdev_state_flag_t;
286
287 /*
288 * Find a leaf vdev to write an MMP block to. It must not have an outstanding
289 * mmp write (if so a new write will also likely block). If there is no usable
290 * leaf, a nonzero error value is returned. The error value returned is a bit
291 * field.
292 *
293 * MMP_FAIL_WRITE_PENDING One or more leaf vdevs are writeable, but have an
294 * outstanding MMP write.
295 * MMP_FAIL_NOT_WRITABLE One or more leaf vdevs are not writeable.
296 */
297
298 static int
mmp_next_leaf(spa_t * spa)299 mmp_next_leaf(spa_t *spa)
300 {
301 vdev_t *leaf;
302 vdev_t *starting_leaf;
303 int fail_mask = 0;
304
305 ASSERT(MUTEX_HELD(&spa->spa_mmp.mmp_io_lock));
306 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER));
307 ASSERT(list_link_active(&spa->spa_leaf_list.list_head) == B_TRUE);
308 ASSERT(!list_is_empty(&spa->spa_leaf_list));
309
310 if (spa->spa_mmp.mmp_leaf_last_gen != spa->spa_leaf_list_gen) {
311 spa->spa_mmp.mmp_last_leaf = list_head(&spa->spa_leaf_list);
312 spa->spa_mmp.mmp_leaf_last_gen = spa->spa_leaf_list_gen;
313 }
314
315 leaf = spa->spa_mmp.mmp_last_leaf;
316 if (leaf == NULL)
317 leaf = list_head(&spa->spa_leaf_list);
318 starting_leaf = leaf;
319
320 do {
321 leaf = list_next(&spa->spa_leaf_list, leaf);
322 if (leaf == NULL) {
323 leaf = list_head(&spa->spa_leaf_list);
324 ASSERT3P(leaf, !=, NULL);
325 }
326
327 /*
328 * We skip unwritable, offline, detached, and dRAID spare
329 * devices as they are either not legal targets or the write
330 * may fail or not be seen by other hosts. Skipped dRAID
331 * spares can never be written so the fail mask is not set.
332 */
333 if (!vdev_writeable(leaf) || leaf->vdev_offline ||
334 leaf->vdev_detached) {
335 fail_mask |= MMP_FAIL_NOT_WRITABLE;
336 } else if (leaf->vdev_ops == &vdev_draid_spare_ops) {
337 continue;
338 } else if (leaf->vdev_mmp_pending != 0) {
339 fail_mask |= MMP_FAIL_WRITE_PENDING;
340 } else {
341 spa->spa_mmp.mmp_last_leaf = leaf;
342 return (0);
343 }
344 } while (leaf != starting_leaf);
345
346 ASSERT(fail_mask);
347
348 return (fail_mask);
349 }
350
351 /*
352 * MMP writes are issued on a fixed schedule, but may complete at variable,
353 * much longer, intervals. The mmp_delay captures long periods between
354 * successful writes for any reason, including disk latency, scheduling delays,
355 * etc.
356 *
357 * The mmp_delay is usually calculated as a decaying average, but if the latest
358 * delay is higher we do not average it, so that we do not hide sudden spikes
359 * which the importing host must wait for.
360 *
361 * If writes are occurring frequently, such as due to a high rate of txg syncs,
362 * the mmp_delay could become very small. Since those short delays depend on
363 * activity we cannot count on, we never allow mmp_delay to get lower than rate
364 * expected if only mmp_thread writes occur.
365 *
366 * If an mmp write was skipped or fails, and we have already waited longer than
367 * mmp_delay, we need to update it so the next write reflects the longer delay.
368 *
369 * Do not set mmp_delay if the multihost property is not on, so as not to
370 * trigger an activity check on import.
371 */
372 static void
mmp_delay_update(spa_t * spa,boolean_t write_completed)373 mmp_delay_update(spa_t *spa, boolean_t write_completed)
374 {
375 mmp_thread_t *mts = &spa->spa_mmp;
376 hrtime_t delay = gethrtime() - mts->mmp_last_write;
377
378 ASSERT(MUTEX_HELD(&mts->mmp_io_lock));
379
380 if (spa_multihost(spa) == B_FALSE) {
381 mts->mmp_delay = 0;
382 return;
383 }
384
385 if (delay > mts->mmp_delay)
386 mts->mmp_delay = delay;
387
388 if (write_completed == B_FALSE)
389 return;
390
391 mts->mmp_last_write = gethrtime();
392
393 /*
394 * strictly less than, in case delay was changed above.
395 */
396 if (delay < mts->mmp_delay) {
397 hrtime_t min_delay =
398 MSEC2NSEC(MMP_INTERVAL_OK(zfs_multihost_interval)) /
399 MAX(1, vdev_count_leaves(spa));
400 mts->mmp_delay = MAX(((delay + mts->mmp_delay * 127) / 128),
401 min_delay);
402 }
403 }
404
405 static void
mmp_write_done(zio_t * zio)406 mmp_write_done(zio_t *zio)
407 {
408 spa_t *spa = zio->io_spa;
409 vdev_t *vd = zio->io_vd;
410 mmp_thread_t *mts = zio->io_private;
411
412 mutex_enter(&mts->mmp_io_lock);
413 uint64_t mmp_kstat_id = vd->vdev_mmp_kstat_id;
414 hrtime_t mmp_write_duration = gethrtime() - vd->vdev_mmp_pending;
415
416 mmp_delay_update(spa, (zio->io_error == 0));
417
418 vd->vdev_mmp_pending = 0;
419 vd->vdev_mmp_kstat_id = 0;
420
421 mutex_exit(&mts->mmp_io_lock);
422 spa_config_exit(spa, SCL_STATE, mmp_tag);
423
424 spa_mmp_history_set(spa, mmp_kstat_id, zio->io_error,
425 mmp_write_duration);
426
427 abd_free(zio->io_abd);
428 }
429
430 /*
431 * When the uberblock on-disk is updated by a spa_sync,
432 * creating a new "best" uberblock, update the one stored
433 * in the mmp thread state, used for mmp writes.
434 */
435 void
mmp_update_uberblock(spa_t * spa,uberblock_t * ub)436 mmp_update_uberblock(spa_t *spa, uberblock_t *ub)
437 {
438 mmp_thread_t *mmp = &spa->spa_mmp;
439
440 mutex_enter(&mmp->mmp_io_lock);
441 mmp->mmp_ub = *ub;
442 mmp->mmp_seq = 1;
443 mmp->mmp_ub.ub_timestamp = gethrestime_sec();
444 mmp_delay_update(spa, B_TRUE);
445 mutex_exit(&mmp->mmp_io_lock);
446 }
447
448 /*
449 * Choose a random vdev, label, and MMP block, and write over it
450 * with a copy of the last-synced uberblock, whose timestamp
451 * has been updated to reflect that the pool is in use.
452 */
453 static void
mmp_write_uberblock(spa_t * spa)454 mmp_write_uberblock(spa_t *spa)
455 {
456 int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL;
457 mmp_thread_t *mmp = &spa->spa_mmp;
458 uberblock_t *ub;
459 vdev_t *vd = NULL;
460 int label, error;
461 uint64_t offset;
462
463 hrtime_t lock_acquire_time = gethrtime();
464 spa_config_enter_priority(spa, SCL_STATE, mmp_tag, RW_READER);
465 lock_acquire_time = gethrtime() - lock_acquire_time;
466 if (lock_acquire_time > (MSEC2NSEC(MMP_MIN_INTERVAL) / 10))
467 zfs_dbgmsg("mmp: long SCL_STATE acquisition, spa=%s "
468 "acquire_time=%llu gethrtime=%llu", spa_name(spa),
469 lock_acquire_time, gethrtime());
470
471 mutex_enter(&mmp->mmp_io_lock);
472
473 error = mmp_next_leaf(spa);
474
475 /*
476 * spa_mmp_history has two types of entries:
477 * Issued MMP write: records time issued, error status, etc.
478 * Skipped MMP write: an MMP write could not be issued because no
479 * suitable leaf vdev was available. See comment above struct
480 * spa_mmp_history for details.
481 */
482
483 if (error) {
484 mmp_delay_update(spa, B_FALSE);
485 if (mmp->mmp_skip_error == error) {
486 spa_mmp_history_set_skip(spa, mmp->mmp_kstat_id - 1);
487 } else {
488 mmp->mmp_skip_error = error;
489 spa_mmp_history_add(spa, mmp->mmp_ub.ub_txg,
490 gethrestime_sec(), mmp->mmp_delay, NULL, 0,
491 mmp->mmp_kstat_id++, error);
492 zfs_dbgmsg("mmp: error choosing leaf, spa=%s "
493 "gethrtime=%llu fail_mask=%#x", spa_name(spa),
494 gethrtime(), error);
495 }
496 mutex_exit(&mmp->mmp_io_lock);
497 spa_config_exit(spa, SCL_STATE, mmp_tag);
498 return;
499 }
500
501 vd = spa->spa_mmp.mmp_last_leaf;
502 if (mmp->mmp_skip_error != 0) {
503 zfs_dbgmsg("mmp: write after skipping due to unavailable "
504 "leaves, spa=%s gethrtime=%llu vdev=%llu error=%d",
505 spa_name(spa), (u_longlong_t)gethrtime(),
506 (u_longlong_t)vd->vdev_guid, mmp->mmp_skip_error);
507 mmp->mmp_skip_error = 0;
508 }
509
510 if (mmp->mmp_zio_root == NULL)
511 mmp->mmp_zio_root = zio_root(spa, NULL, NULL,
512 flags | ZIO_FLAG_GODFATHER);
513
514 if (mmp->mmp_ub.ub_timestamp != gethrestime_sec()) {
515 /*
516 * Want to reset mmp_seq when timestamp advances because after
517 * an mmp_seq wrap new values will not be chosen by
518 * uberblock_compare() as the "best".
519 */
520 mmp->mmp_ub.ub_timestamp = gethrestime_sec();
521 mmp->mmp_seq = 1;
522 }
523
524 ub = &mmp->mmp_ub;
525 ub->ub_mmp_magic = MMP_MAGIC;
526 ub->ub_mmp_delay = mmp->mmp_delay;
527 ub->ub_mmp_config = MMP_SEQ_SET(mmp->mmp_seq) |
528 MMP_INTERVAL_SET(MMP_INTERVAL_OK(zfs_multihost_interval)) |
529 MMP_FAIL_INT_SET(MMP_FAIL_INTVS_OK(
530 zfs_multihost_fail_intervals));
531 vd->vdev_mmp_pending = gethrtime();
532 vd->vdev_mmp_kstat_id = mmp->mmp_kstat_id;
533
534 zio_t *zio = zio_null(mmp->mmp_zio_root, spa, NULL, NULL, NULL, flags);
535 abd_t *ub_abd = abd_alloc_for_io(VDEV_UBERBLOCK_SIZE(vd), B_TRUE);
536 abd_copy_from_buf(ub_abd, ub, sizeof (uberblock_t));
537 abd_zero_off(ub_abd, sizeof (uberblock_t),
538 VDEV_UBERBLOCK_SIZE(vd) - sizeof (uberblock_t));
539
540 mmp->mmp_seq++;
541 mmp->mmp_kstat_id++;
542 mutex_exit(&mmp->mmp_io_lock);
543
544 offset = VDEV_UBERBLOCK_OFFSET(vd, VDEV_UBERBLOCK_COUNT(vd) -
545 MMP_BLOCKS_PER_LABEL + random_in_range(MMP_BLOCKS_PER_LABEL));
546
547 label = random_in_range(VDEV_LABELS);
548 vdev_label_write(zio, vd, label, ub_abd, offset,
549 VDEV_UBERBLOCK_SIZE(vd), mmp_write_done, mmp,
550 flags | ZIO_FLAG_DONT_PROPAGATE);
551
552 (void) spa_mmp_history_add(spa, ub->ub_txg, ub->ub_timestamp,
553 ub->ub_mmp_delay, vd, label, vd->vdev_mmp_kstat_id, 0);
554
555 zio_nowait(zio);
556 }
557
558 static void
mmp_claim_uberblock_sync_done(zio_t * zio)559 mmp_claim_uberblock_sync_done(zio_t *zio)
560 {
561 uint64_t *good_writes = zio->io_private;
562
563 if (zio->io_error == 0 && zio->io_vd->vdev_top->vdev_ms_array != 0)
564 atomic_inc_64(good_writes);
565 }
566
567 /*
568 * Write the uberblock to the first label of all leaves of the specified vdev.
569 * One write per mirror leg the config still expects present, one for a
570 * singleton, and parity+1 for raidz or draid vdevs.
571 */
572 static void
mmp_claim_uberblock_sync(zio_t * zio,uint64_t * good_writes,uint64_t * issued_writes,uint64_t * req_writes,uberblock_t * ub,vdev_t * vd,int flags)573 mmp_claim_uberblock_sync(zio_t *zio, uint64_t *good_writes,
574 uint64_t *issued_writes, uint64_t *req_writes, uberblock_t *ub, vdev_t *vd,
575 int flags)
576 {
577 for (uint64_t c = 0; c < vd->vdev_children; c++) {
578 vdev_t *cvd = vd->vdev_child[c];
579
580 if (cvd->vdev_islog || cvd->vdev_isspare ||
581 cvd->vdev_isl2cache || cvd->vdev_ishole ||
582 cvd->vdev_ops == &vdev_indirect_ops)
583 continue;
584
585 if (cvd->vdev_top == cvd) {
586 uint64_t nparity = vdev_get_nparity(cvd);
587 if (nparity) {
588 *req_writes += nparity + 1;
589 } else {
590 /*
591 * Mirror: any single leg is enough for a
592 * remote host to see the claim, so require a
593 * write to every leg the pool config still
594 * expects to be present. A leg taken out of
595 * service is recorded persistently in the
596 * config (offline, faulted, or removed) and
597 * is seen the same way by every host, so it
598 * is not required and a degraded mirror can
599 * still be claimed. A leg merely unreachable
600 * from this host has none of those states and
601 * stays required, so a host which can see
602 * only some of the legs of an otherwise
603 * healthy mirror still fails the claim and
604 * cannot split the pool.
605 */
606 uint64_t present = 0;
607 for (uint64_t l = 0; l < cvd->vdev_children;
608 l++) {
609 vdev_t *lvd = cvd->vdev_child[l];
610 #ifndef _KERNEL
611 /*
612 * Manual recovery forgives the legs
613 * this host could not open, and marks
614 * that same set offline before it
615 * exports, so the relaxed claim
616 * accepts nothing the later ordinary
617 * imports would not.
618 */
619 if (mmp_claim_relaxed &&
620 lvd->vdev_not_present)
621 continue;
622 #endif
623 if (!lvd->vdev_offline &&
624 !lvd->vdev_faulted &&
625 !lvd->vdev_removed)
626 present++;
627 }
628 *req_writes += MAX(present, 1);
629 }
630 }
631
632 mmp_claim_uberblock_sync(zio, good_writes, issued_writes,
633 req_writes, ub, cvd, flags);
634 }
635
636 if (!vd->vdev_ops->vdev_op_leaf)
637 return;
638
639 if (!vdev_writeable(vd))
640 return;
641
642 if (vd->vdev_ops == &vdev_draid_spare_ops)
643 return;
644
645 /*
646 * Count the writes actually issued alongside those required. A leaf
647 * the config expects present but which cannot be written is never
648 * issued one, so a shortfall here means a device is missing rather
649 * than failing. Gated exactly as good_writes is in
650 * mmp_claim_uberblock_sync_done() so that the two counts describe the
651 * same set of leaves and can be compared.
652 */
653 if (vd->vdev_top->vdev_ms_array != 0)
654 (*issued_writes)++;
655
656 abd_t *ub_abd = abd_alloc_for_io(VDEV_UBERBLOCK_SIZE(vd), B_TRUE);
657 abd_copy_from_buf(ub_abd, ub, sizeof (uberblock_t));
658 abd_zero_off(ub_abd, sizeof (uberblock_t),
659 VDEV_UBERBLOCK_SIZE(vd) - sizeof (uberblock_t));
660
661 vdev_label_write(zio, vd, 0, ub_abd,
662 VDEV_UBERBLOCK_OFFSET(vd, VDEV_UBERBLOCK_COUNT(vd) -
663 MMP_BLOCKS_PER_LABEL), VDEV_UBERBLOCK_SIZE(vd),
664 mmp_claim_uberblock_sync_done, good_writes,
665 flags | ZIO_FLAG_DONT_PROPAGATE);
666
667 abd_free(ub_abd);
668 }
669
670 int
mmp_claim_uberblock(spa_t * spa,vdev_t * vd,uberblock_t * ub)671 mmp_claim_uberblock(spa_t *spa, vdev_t *vd, uberblock_t *ub)
672 {
673 int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL;
674 uint64_t good_writes = 0;
675 uint64_t issued_writes = 0;
676 uint64_t req_writes = 0;
677 zio_t *zio;
678
679 ASSERT(MMP_VALID(ub));
680 ASSERT(MMP_SEQ_VALID(ub));
681
682 spa_config_enter(spa, SCL_ALL, mmp_tag, RW_WRITER);
683
684 /* Sync the uberblock to all writeable leaves */
685 zio = zio_root(spa, NULL, NULL, flags);
686 mmp_claim_uberblock_sync(zio, &good_writes, &issued_writes, &req_writes,
687 ub, vd, flags);
688 (void) zio_wait(zio);
689
690 /* Flush the new uberblocks so they're immediately visible */
691 zio = zio_root(spa, NULL, NULL, flags);
692 zio_flush(zio, vd);
693 (void) zio_wait(zio);
694
695 spa_config_exit(spa, SCL_ALL, mmp_tag);
696
697 zfs_dbgmsg("mmp: claiming uberblock, spa=%s txg=%llu seq=%llu "
698 "req_writes=%llu issued_writes=%llu good_writes=%llu",
699 spa_load_name(spa),
700 (u_longlong_t)ub->ub_txg, (u_longlong_t)MMP_SEQ(ub),
701 (u_longlong_t)req_writes, (u_longlong_t)issued_writes,
702 (u_longlong_t)good_writes);
703
704 /*
705 * To guarantee visibility from a remote host we require a minimum
706 * number of good writes. For raidz/draid vdevs parity+1 writes, for
707 * mirrors one write per leg the config expects present, and for
708 * singletons 1 write.
709 *
710 * Distinguish the two ways of falling short. Too few writes issued
711 * means a device the config expects present could not be written at
712 * all, which persists across retries and is what 'zhack mmp reclaim'
713 * recovers. Enough issued but too few good means the writes reached
714 * present devices and failed, which a retry may clear.
715 */
716 if (req_writes == 0)
717 return (SET_ERROR(EIO));
718
719 if (issued_writes < req_writes)
720 return (SET_ERROR(ENODEV));
721
722 if (good_writes < req_writes)
723 return (SET_ERROR(EIO));
724
725 return (0);
726 }
727
728 static __attribute__((noreturn)) void
mmp_thread(void * arg)729 mmp_thread(void *arg)
730 {
731 spa_t *spa = (spa_t *)arg;
732 mmp_thread_t *mmp = &spa->spa_mmp;
733 boolean_t suspended = spa_suspended(spa);
734 boolean_t multihost = spa_multihost(spa);
735 uint64_t mmp_interval = MSEC2NSEC(MMP_INTERVAL_OK(
736 zfs_multihost_interval));
737 uint32_t mmp_fail_intervals = MMP_FAIL_INTVS_OK(
738 zfs_multihost_fail_intervals);
739 hrtime_t mmp_fail_ns = mmp_fail_intervals * mmp_interval;
740 boolean_t last_spa_suspended;
741 boolean_t last_spa_multihost;
742 uint64_t last_mmp_interval;
743 uint32_t last_mmp_fail_intervals;
744 hrtime_t last_mmp_fail_ns;
745 callb_cpr_t cpr;
746 int skip_wait = 0;
747
748 mmp_thread_enter(mmp, &cpr);
749
750 /*
751 * There have been no MMP writes yet. Setting mmp_last_write here gives
752 * us one mmp_fail_ns period, which is consistent with the activity
753 * check duration, to try to land an MMP write before MMP suspends the
754 * pool (if so configured).
755 */
756
757 mutex_enter(&mmp->mmp_io_lock);
758 mmp->mmp_last_write = gethrtime();
759 mmp->mmp_delay = MSEC2NSEC(MMP_INTERVAL_OK(zfs_multihost_interval));
760 mutex_exit(&mmp->mmp_io_lock);
761
762 while (!mmp->mmp_thread_exiting) {
763 hrtime_t next_time = gethrtime() +
764 MSEC2NSEC(MMP_DEFAULT_INTERVAL);
765 int leaves = MAX(vdev_count_leaves(spa), 1);
766
767 /* Detect changes in tunables or state */
768
769 last_spa_suspended = suspended;
770 last_spa_multihost = multihost;
771 suspended = spa_suspended(spa);
772 multihost = spa_multihost(spa);
773
774 last_mmp_interval = mmp_interval;
775 last_mmp_fail_intervals = mmp_fail_intervals;
776 last_mmp_fail_ns = mmp_fail_ns;
777 mmp_interval = MSEC2NSEC(MMP_INTERVAL_OK(
778 zfs_multihost_interval));
779 mmp_fail_intervals = MMP_FAIL_INTVS_OK(
780 zfs_multihost_fail_intervals);
781
782 /* Smooth so pool is not suspended when reducing tunables */
783 if (mmp_fail_intervals * mmp_interval < mmp_fail_ns) {
784 mmp_fail_ns = (mmp_fail_ns * 31 +
785 mmp_fail_intervals * mmp_interval) / 32;
786 } else {
787 mmp_fail_ns = mmp_fail_intervals *
788 mmp_interval;
789 }
790
791 if (mmp_interval != last_mmp_interval ||
792 mmp_fail_intervals != last_mmp_fail_intervals) {
793 /*
794 * We want other hosts to see new tunables as quickly as
795 * possible. Write out at higher frequency than usual.
796 */
797 skip_wait += leaves;
798 }
799
800 if (multihost)
801 next_time = gethrtime() + mmp_interval / leaves;
802
803 if (mmp_fail_ns != last_mmp_fail_ns) {
804 zfs_dbgmsg("mmp: interval change, spa=%s "
805 "gethrtime=%llu last_mmp_interval=%llu "
806 "mmp_interval=%llu last_mmp_fail_intervals=%u "
807 "mmp_fail_intervals=%u mmp_fail_ns=%llu "
808 "skip_wait=%d leaves=%d next_time=%llu",
809 spa_name(spa), (u_longlong_t)gethrtime(),
810 (u_longlong_t)last_mmp_interval,
811 (u_longlong_t)mmp_interval, last_mmp_fail_intervals,
812 mmp_fail_intervals, (u_longlong_t)mmp_fail_ns,
813 skip_wait, leaves, (u_longlong_t)next_time);
814 }
815
816 /*
817 * MMP off => on, or suspended => !suspended:
818 * No writes occurred recently. Update mmp_last_write to give
819 * us some time to try.
820 */
821 if ((!last_spa_multihost && multihost) ||
822 (last_spa_suspended && !suspended)) {
823 zfs_dbgmsg("mmp: state change spa=%s: gethrtime=%llu "
824 "last_spa_multihost=%u multihost=%u "
825 "last_spa_suspended=%u suspended=%u",
826 spa_name(spa), (u_longlong_t)gethrtime(),
827 last_spa_multihost, multihost, last_spa_suspended,
828 suspended);
829 mutex_enter(&mmp->mmp_io_lock);
830 mmp->mmp_last_write = gethrtime();
831 mmp->mmp_delay = mmp_interval;
832 mutex_exit(&mmp->mmp_io_lock);
833 }
834
835 /*
836 * MMP on => off:
837 * mmp_delay == 0 tells importing node to skip activity check.
838 */
839 if (last_spa_multihost && !multihost) {
840 mutex_enter(&mmp->mmp_io_lock);
841 mmp->mmp_delay = 0;
842 mutex_exit(&mmp->mmp_io_lock);
843 }
844
845 /*
846 * Suspend the pool if no MMP write has succeeded in over
847 * mmp_interval * mmp_fail_intervals nanoseconds.
848 */
849 if (multihost && !suspended && mmp_fail_intervals &&
850 (gethrtime() - mmp->mmp_last_write) > mmp_fail_ns) {
851 zfs_dbgmsg("mmp: suspending pool, spa=%s "
852 "gethrtime=%llu mmp_last_write=%llu "
853 "mmp_interval=%llu mmp_fail_intervals=%llu "
854 "mmp_fail_ns=%llu txg=%llu",
855 spa_name(spa), (u_longlong_t)gethrtime(),
856 (u_longlong_t)mmp->mmp_last_write,
857 (u_longlong_t)mmp_interval,
858 (u_longlong_t)mmp_fail_intervals,
859 (u_longlong_t)mmp_fail_ns,
860 (u_longlong_t)spa->spa_uberblock.ub_txg);
861 cmn_err(CE_WARN, "MMP writes to pool '%s' have not "
862 "succeeded in over %llu ms; suspending pool. "
863 "Hrtime %llu",
864 spa_name(spa),
865 NSEC2MSEC(gethrtime() - mmp->mmp_last_write),
866 gethrtime());
867 zio_suspend(spa, NULL, ZIO_SUSPEND_MMP);
868 }
869
870 if (multihost && !suspended)
871 mmp_write_uberblock(spa);
872
873 if (skip_wait > 0) {
874 next_time = gethrtime() + MSEC2NSEC(MMP_MIN_INTERVAL) /
875 leaves;
876 skip_wait--;
877 }
878
879 CALLB_CPR_SAFE_BEGIN(&cpr);
880 (void) cv_timedwait_idle_hires(&mmp->mmp_thread_cv,
881 &mmp->mmp_thread_lock, next_time, USEC2NSEC(100),
882 CALLOUT_FLAG_ABSOLUTE);
883 CALLB_CPR_SAFE_END(&cpr, &mmp->mmp_thread_lock);
884 }
885
886 /* Outstanding writes are allowed to complete. */
887 zio_wait(mmp->mmp_zio_root);
888
889 mmp->mmp_zio_root = NULL;
890 mmp_thread_exit(mmp, &mmp->mmp_thread, &cpr);
891
892 thread_exit();
893 }
894
895 /*
896 * Signal the MMP thread to wake it, when it is sleeping on
897 * its cv. Used when some module parameter has changed and
898 * we want the thread to know about it.
899 * Only signal if the pool is active and mmp thread is
900 * running, otherwise there is no thread to wake.
901 */
902 static void
mmp_signal_thread(spa_t * spa)903 mmp_signal_thread(spa_t *spa)
904 {
905 mmp_thread_t *mmp = &spa->spa_mmp;
906
907 mutex_enter(&mmp->mmp_thread_lock);
908 if (mmp->mmp_thread)
909 cv_broadcast(&mmp->mmp_thread_cv);
910 mutex_exit(&mmp->mmp_thread_lock);
911 }
912
913 void
mmp_signal_all_threads(void)914 mmp_signal_all_threads(void)
915 {
916 spa_t *spa = NULL;
917
918 spa_namespace_enter(FTAG);
919 while ((spa = spa_next(spa))) {
920 if (spa->spa_state == POOL_STATE_ACTIVE)
921 mmp_signal_thread(spa);
922 }
923 spa_namespace_exit(FTAG);
924 }
925
926 ZFS_MODULE_PARAM_CALL(zfs_multihost, zfs_multihost_, interval,
927 param_set_multihost_interval, spl_param_get_u64, ZMOD_RW,
928 "Milliseconds between mmp writes to each leaf");
929
930 ZFS_MODULE_PARAM(zfs_multihost, zfs_multihost_, fail_intervals, UINT, ZMOD_RW,
931 "Max allowed period without a successful mmp write");
932
933 ZFS_MODULE_PARAM(zfs_multihost, zfs_multihost_, import_intervals, UINT, ZMOD_RW,
934 "Number of zfs_multihost_interval periods to wait for activity");
935