]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/mmp.c
Native Encryption for ZFS on Linux
[mirror_zfs.git] / module / zfs / mmp.c
CommitLineData
379ca9cf
OF
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21/*
22 * Copyright (c) 2017 by Lawrence Livermore National Security, LLC.
23 */
24
25#include <sys/abd.h>
26#include <sys/mmp.h>
27#include <sys/spa.h>
28#include <sys/spa_impl.h>
29#include <sys/vdev.h>
30#include <sys/vdev_impl.h>
31#include <sys/zfs_context.h>
32#include <sys/callb.h>
33
34/*
35 * Multi-Modifier Protection (MMP) attempts to prevent a user from importing
36 * or opening a pool on more than one host at a time. In particular, it
37 * prevents "zpool import -f" on a host from succeeding while the pool is
38 * already imported on another host. There are many other ways in which a
39 * device could be used by two hosts for different purposes at the same time
40 * resulting in pool damage. This implementation does not attempt to detect
41 * those cases.
42 *
43 * MMP operates by ensuring there are frequent visible changes on disk (a
44 * "heartbeat") at all times. And by altering the import process to check
45 * for these changes and failing the import when they are detected. This
46 * functionality is enabled by setting the 'multihost' pool property to on.
47 *
48 * Uberblocks written by the txg_sync thread always go into the first
49 * (N-MMP_BLOCKS_PER_LABEL) slots, the remaining slots are reserved for MMP.
50 * They are used to hold uberblocks which are exactly the same as the last
51 * synced uberblock except that the ub_timestamp is frequently updated.
52 * Like all other uberblocks, the slot is written with an embedded checksum,
53 * and slots with invalid checksums are ignored. This provides the
54 * "heartbeat", with no risk of overwriting good uberblocks that must be
55 * preserved, e.g. previous txgs and associated block pointers.
56 *
57 * Two optional fields are added to uberblock structure: ub_mmp_magic and
58 * ub_mmp_delay. The magic field allows zfs to tell whether ub_mmp_delay is
59 * valid. The delay field is a decaying average of the amount of time between
60 * completion of successive MMP writes, in nanoseconds. It is used to predict
61 * how long the import must wait to detect activity in the pool, before
62 * concluding it is not in use.
63 *
64 * During import an activity test may now be performed to determine if
65 * the pool is in use. The activity test is typically required if the
66 * ZPOOL_CONFIG_HOSTID does not match the system hostid, the pool state is
67 * POOL_STATE_ACTIVE, and the pool is not a root pool.
68 *
69 * The activity test finds the "best" uberblock (highest txg & timestamp),
70 * waits some time, and then finds the "best" uberblock again. If the txg
71 * and timestamp in both "best" uberblocks do not match, the pool is in use
72 * by another host and the import fails. Since the granularity of the
73 * timestamp is in seconds this activity test must take a bare minimum of one
74 * second. In order to assure the accuracy of the activity test, the default
75 * values result in an activity test duration of 10x the mmp write interval.
76 *
77 * The "zpool import" activity test can be expected to take a minimum time of
78 * zfs_multihost_import_intervals * zfs_multihost_interval milliseconds. If the
79 * "best" uberblock has a valid ub_mmp_delay field, then the duration of the
80 * test may take longer if MMP writes were occurring less frequently than
81 * expected. Additionally, the duration is then extended by a random 25% to
82 * attempt to to detect simultaneous imports. For example, if both partner
83 * hosts are rebooted at the same time and automatically attempt to import the
84 * pool.
85 */
86
87/*
88 * Used to control the frequency of mmp writes which are performed when the
89 * 'multihost' pool property is on. This is one factor used to determine the
90 * length of the activity check during import.
91 *
92 * The mmp write period is zfs_multihost_interval / leaf-vdevs milliseconds.
93 * This means that on average an mmp write will be issued for each leaf vdev
94 * every zfs_multihost_interval milliseconds. In practice, the observed period
95 * can vary with the I/O load and this observed value is the delay which is
96 * stored in the uberblock. The minimum allowed value is 100 ms.
97 */
98ulong_t zfs_multihost_interval = MMP_DEFAULT_INTERVAL;
99
100/*
101 * Used to control the duration of the activity test on import. Smaller values
102 * of zfs_multihost_import_intervals will reduce the import time but increase
103 * the risk of failing to detect an active pool. The total activity check time
104 * is never allowed to drop below one second. A value of 0 is ignored and
105 * treated as if it was set to 1.
106 */
107uint_t zfs_multihost_import_intervals = MMP_DEFAULT_IMPORT_INTERVALS;
108
109/*
110 * Controls the behavior of the pool when mmp write failures are detected.
111 *
112 * When zfs_multihost_fail_intervals = 0 then mmp write failures are ignored.
113 * The failures will still be reported to the ZED which depending on its
114 * configuration may take action such as suspending the pool or taking a
115 * device offline.
116 *
117 * When zfs_multihost_fail_intervals > 0 then sequential mmp write failures will
118 * cause the pool to be suspended. This occurs when
119 * zfs_multihost_fail_intervals * zfs_multihost_interval milliseconds have
120 * passed since the last successful mmp write. This guarantees the activity
121 * test will see mmp writes if the
122 * pool is imported.
123 */
124uint_t zfs_multihost_fail_intervals = MMP_DEFAULT_FAIL_INTERVALS;
125
c25b8f99 126static void mmp_thread(void *arg);
379ca9cf
OF
127
128void
129mmp_init(spa_t *spa)
130{
131 mmp_thread_t *mmp = &spa->spa_mmp;
132
133 mutex_init(&mmp->mmp_thread_lock, NULL, MUTEX_DEFAULT, NULL);
134 cv_init(&mmp->mmp_thread_cv, NULL, CV_DEFAULT, NULL);
135 mutex_init(&mmp->mmp_io_lock, NULL, MUTEX_DEFAULT, NULL);
136}
137
138void
139mmp_fini(spa_t *spa)
140{
141 mmp_thread_t *mmp = &spa->spa_mmp;
142
143 mutex_destroy(&mmp->mmp_thread_lock);
144 cv_destroy(&mmp->mmp_thread_cv);
145 mutex_destroy(&mmp->mmp_io_lock);
146}
147
148static void
149mmp_thread_enter(mmp_thread_t *mmp, callb_cpr_t *cpr)
150{
151 CALLB_CPR_INIT(cpr, &mmp->mmp_thread_lock, callb_generic_cpr, FTAG);
152 mutex_enter(&mmp->mmp_thread_lock);
153}
154
155static void
156mmp_thread_exit(mmp_thread_t *mmp, kthread_t **mpp, callb_cpr_t *cpr)
157{
158 ASSERT(*mpp != NULL);
159 *mpp = NULL;
160 cv_broadcast(&mmp->mmp_thread_cv);
161 CALLB_CPR_EXIT(cpr); /* drops &mmp->mmp_thread_lock */
162 thread_exit();
163}
164
165void
166mmp_thread_start(spa_t *spa)
167{
168 mmp_thread_t *mmp = &spa->spa_mmp;
169
170 if (spa_writeable(spa)) {
171 mutex_enter(&mmp->mmp_thread_lock);
172 if (!mmp->mmp_thread) {
173 dprintf("mmp_thread_start pool %s\n",
174 spa->spa_name);
175 mmp->mmp_thread = thread_create(NULL, 0, mmp_thread,
176 spa, 0, &p0, TS_RUN, defclsyspri);
177 }
178 mutex_exit(&mmp->mmp_thread_lock);
179 }
180}
181
182void
183mmp_thread_stop(spa_t *spa)
184{
185 mmp_thread_t *mmp = &spa->spa_mmp;
186
187 mutex_enter(&mmp->mmp_thread_lock);
188 mmp->mmp_thread_exiting = 1;
189 cv_broadcast(&mmp->mmp_thread_cv);
190
191 while (mmp->mmp_thread) {
192 cv_wait(&mmp->mmp_thread_cv, &mmp->mmp_thread_lock);
193 }
194 mutex_exit(&mmp->mmp_thread_lock);
195
196 ASSERT(mmp->mmp_thread == NULL);
197 mmp->mmp_thread_exiting = 0;
198}
199
200/*
201 * Randomly choose a leaf vdev, to write an MMP block to. It must be
202 * writable. It must not have an outstanding mmp write (if so then
203 * there is a problem, and a new write will also block).
204 *
205 * We try 10 times to pick a random leaf without an outstanding write.
206 * If 90% of the leaves have pending writes, this gives us a >65%
207 * chance of finding one we can write to. There will be at least
208 * (zfs_multihost_fail_intervals) tries before the inability to write an MMP
209 * block causes serious problems.
210 */
211static vdev_t *
212vdev_random_leaf(spa_t *spa)
213{
214 vdev_t *vd, *child;
215 int pending_writes = 10;
216
217 ASSERT(spa);
218 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
219
220 /*
221 * Since we hold SCL_STATE, neither pool nor vdev state can
222 * change. Therefore, if the root is not dead, there is a
223 * child that is not dead, and so on down to a leaf.
224 */
225 if (!vdev_writeable(spa->spa_root_vdev))
226 return (NULL);
227
228 vd = spa->spa_root_vdev;
229 while (!vd->vdev_ops->vdev_op_leaf) {
230 child = vd->vdev_child[spa_get_random(vd->vdev_children)];
231
232 if (!vdev_writeable(child))
233 continue;
234
235 if (child->vdev_ops->vdev_op_leaf && child->vdev_mmp_pending) {
236 if (pending_writes-- > 0)
237 continue;
238 else
239 return (NULL);
240 }
241
242 vd = child;
243 }
244 return (vd);
245}
246
247static void
248mmp_write_done(zio_t *zio)
249{
250 spa_t *spa = zio->io_spa;
251 vdev_t *vd = zio->io_vd;
252 mmp_thread_t *mts = zio->io_private;
253
254 mutex_enter(&mts->mmp_io_lock);
255 vd->vdev_mmp_pending = 0;
256
257 if (zio->io_error)
258 goto unlock;
259
260 /*
261 * Mmp writes are queued on a fixed schedule, but under many
262 * circumstances, such as a busy device or faulty hardware,
263 * the writes will complete at variable, much longer,
264 * intervals. In these cases, another node checking for
265 * activity must wait longer to account for these delays.
266 *
267 * The mmp_delay is calculated as a decaying average of the interval
268 * between completed mmp writes. This is used to predict how long
269 * the import must wait to detect activity in the pool, before
270 * concluding it is not in use.
271 *
272 * Do not set mmp_delay if the multihost property is not on,
273 * so as not to trigger an activity check on import.
274 */
275 if (spa_multihost(spa)) {
276 hrtime_t delay = gethrtime() - mts->mmp_last_write;
277
278 if (delay > mts->mmp_delay)
279 mts->mmp_delay = delay;
280 else
281 mts->mmp_delay = (delay + mts->mmp_delay * 127) /
282 128;
283 } else {
284 mts->mmp_delay = 0;
285 }
286 mts->mmp_last_write = gethrtime();
287
288unlock:
289 mutex_exit(&mts->mmp_io_lock);
ffb195c2 290 spa_config_exit(spa, SCL_STATE, FTAG);
379ca9cf
OF
291
292 abd_free(zio->io_abd);
293}
294
295/*
296 * When the uberblock on-disk is updated by a spa_sync,
297 * creating a new "best" uberblock, update the one stored
298 * in the mmp thread state, used for mmp writes.
299 */
300void
301mmp_update_uberblock(spa_t *spa, uberblock_t *ub)
302{
303 mmp_thread_t *mmp = &spa->spa_mmp;
304
305 mutex_enter(&mmp->mmp_io_lock);
306 mmp->mmp_ub = *ub;
307 mmp->mmp_ub.ub_timestamp = gethrestime_sec();
308 mutex_exit(&mmp->mmp_io_lock);
309}
310
311/*
312 * Choose a random vdev, label, and MMP block, and write over it
313 * with a copy of the last-synced uberblock, whose timestamp
314 * has been updated to reflect that the pool is in use.
315 */
316static void
317mmp_write_uberblock(spa_t *spa)
318{
319 int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL;
320 mmp_thread_t *mmp = &spa->spa_mmp;
321 uberblock_t *ub;
322 vdev_t *vd;
323 int label;
324 uint64_t offset;
325
ffb195c2 326 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
379ca9cf 327 vd = vdev_random_leaf(spa);
ffb195c2
OF
328 if (vd == NULL || !vdev_writeable(vd)) {
329 spa_config_exit(spa, SCL_STATE, FTAG);
379ca9cf 330 return;
ffb195c2 331 }
379ca9cf
OF
332
333 mutex_enter(&mmp->mmp_io_lock);
334
335 if (mmp->mmp_zio_root == NULL)
336 mmp->mmp_zio_root = zio_root(spa, NULL, NULL,
337 flags | ZIO_FLAG_GODFATHER);
338
339 ub = &mmp->mmp_ub;
340 ub->ub_timestamp = gethrestime_sec();
341 ub->ub_mmp_magic = MMP_MAGIC;
342 ub->ub_mmp_delay = mmp->mmp_delay;
343 vd->vdev_mmp_pending = gethrtime();
344
345 zio_t *zio = zio_null(mmp->mmp_zio_root, spa, NULL, NULL, NULL, flags);
346 abd_t *ub_abd = abd_alloc_for_io(VDEV_UBERBLOCK_SIZE(vd), B_TRUE);
347 abd_zero(ub_abd, VDEV_UBERBLOCK_SIZE(vd));
348 abd_copy_from_buf(ub_abd, ub, sizeof (uberblock_t));
349
350 mutex_exit(&mmp->mmp_io_lock);
351
352 offset = VDEV_UBERBLOCK_OFFSET(vd, VDEV_UBERBLOCK_COUNT(vd) -
353 MMP_BLOCKS_PER_LABEL + spa_get_random(MMP_BLOCKS_PER_LABEL));
354
355 label = spa_get_random(VDEV_LABELS);
356 vdev_label_write(zio, vd, label, ub_abd, offset,
357 VDEV_UBERBLOCK_SIZE(vd), mmp_write_done, mmp,
358 flags | ZIO_FLAG_DONT_PROPAGATE);
359
360 spa_mmp_history_add(ub->ub_txg, ub->ub_timestamp, ub->ub_mmp_delay, vd,
361 label);
362
363 zio_nowait(zio);
364}
365
366static void
c25b8f99 367mmp_thread(void *arg)
379ca9cf 368{
c25b8f99 369 spa_t *spa = (spa_t *)arg;
379ca9cf
OF
370 mmp_thread_t *mmp = &spa->spa_mmp;
371 boolean_t last_spa_suspended = spa_suspended(spa);
372 boolean_t last_spa_multihost = spa_multihost(spa);
373 callb_cpr_t cpr;
374 hrtime_t max_fail_ns = zfs_multihost_fail_intervals *
375 MSEC2NSEC(MAX(zfs_multihost_interval, MMP_MIN_INTERVAL));
376
377 mmp_thread_enter(mmp, &cpr);
378
379 /*
380 * The mmp_write_done() function calculates mmp_delay based on the
381 * prior value of mmp_delay and the elapsed time since the last write.
382 * For the first mmp write, there is no "last write", so we start
383 * with fake, but reasonable, default non-zero values.
384 */
385 mmp->mmp_delay = MSEC2NSEC(MAX(zfs_multihost_interval,
802ae562 386 MMP_MIN_INTERVAL)) / MAX(vdev_count_leaves(spa), 1);
379ca9cf
OF
387 mmp->mmp_last_write = gethrtime() - mmp->mmp_delay;
388
389 while (!mmp->mmp_thread_exiting) {
390 uint64_t mmp_fail_intervals = zfs_multihost_fail_intervals;
391 uint64_t mmp_interval = MSEC2NSEC(
392 MAX(zfs_multihost_interval, MMP_MIN_INTERVAL));
393 boolean_t suspended = spa_suspended(spa);
394 boolean_t multihost = spa_multihost(spa);
395 hrtime_t start, next_time;
396
397 start = gethrtime();
398 if (multihost) {
399 next_time = start + mmp_interval /
802ae562 400 MAX(vdev_count_leaves(spa), 1);
379ca9cf
OF
401 } else {
402 next_time = start + MSEC2NSEC(MMP_DEFAULT_INTERVAL);
403 }
404
405 /*
406 * When MMP goes off => on, or spa goes suspended =>
407 * !suspended, we know no writes occurred recently. We
408 * update mmp_last_write to give us some time to try.
409 */
410 if ((!last_spa_multihost && multihost) ||
411 (last_spa_suspended && !suspended)) {
412 mutex_enter(&mmp->mmp_io_lock);
413 mmp->mmp_last_write = gethrtime();
414 mutex_exit(&mmp->mmp_io_lock);
415 } else if (last_spa_multihost && !multihost) {
416 mutex_enter(&mmp->mmp_io_lock);
417 mmp->mmp_delay = 0;
418 mutex_exit(&mmp->mmp_io_lock);
419 }
420 last_spa_multihost = multihost;
421 last_spa_suspended = suspended;
422
423 /*
424 * Smooth max_fail_ns when its factors are decreased, because
425 * making (max_fail_ns < mmp_interval) results in the pool being
426 * immediately suspended before writes can occur at the new
427 * higher frequency.
428 */
429 if ((mmp_interval * mmp_fail_intervals) < max_fail_ns) {
430 max_fail_ns = ((31 * max_fail_ns) + (mmp_interval *
431 mmp_fail_intervals)) / 32;
432 } else {
433 max_fail_ns = mmp_interval * mmp_fail_intervals;
434 }
435
436 /*
437 * Suspend the pool if no MMP write has succeeded in over
438 * mmp_interval * mmp_fail_intervals nanoseconds.
439 */
440 if (!suspended && mmp_fail_intervals && multihost &&
441 (start - mmp->mmp_last_write) > max_fail_ns) {
442 zio_suspend(spa, NULL);
443 }
444
ffb195c2 445 if (multihost)
379ca9cf 446 mmp_write_uberblock(spa);
379ca9cf
OF
447
448 CALLB_CPR_SAFE_BEGIN(&cpr);
449 (void) cv_timedwait_sig(&mmp->mmp_thread_cv,
450 &mmp->mmp_thread_lock, ddi_get_lbolt() +
b6e5c403 451 ((next_time - gethrtime()) / (NANOSEC / hz)));
379ca9cf
OF
452 CALLB_CPR_SAFE_END(&cpr, &mmp->mmp_thread_lock);
453 }
454
455 /* Outstanding writes are allowed to complete. */
456 if (mmp->mmp_zio_root)
457 zio_wait(mmp->mmp_zio_root);
458
459 mmp->mmp_zio_root = NULL;
460 mmp_thread_exit(mmp, &mmp->mmp_thread, &cpr);
461}
462
0582e403
OF
463/*
464 * Signal the MMP thread to wake it, when it is sleeping on
465 * its cv. Used when some module parameter has changed and
466 * we want the thread to know about it.
467 * Only signal if the pool is active and mmp thread is
468 * running, otherwise there is no thread to wake.
469 */
470static void
471mmp_signal_thread(spa_t *spa)
472{
473 mmp_thread_t *mmp = &spa->spa_mmp;
474
475 mutex_enter(&mmp->mmp_thread_lock);
476 if (mmp->mmp_thread)
477 cv_broadcast(&mmp->mmp_thread_cv);
478 mutex_exit(&mmp->mmp_thread_lock);
479}
480
481void
482mmp_signal_all_threads(void)
483{
484 spa_t *spa = NULL;
485
486 mutex_enter(&spa_namespace_lock);
487 while ((spa = spa_next(spa))) {
488 if (spa->spa_state == POOL_STATE_ACTIVE)
489 mmp_signal_thread(spa);
490 }
491 mutex_exit(&spa_namespace_lock);
492}
493
379ca9cf 494#if defined(_KERNEL) && defined(HAVE_SPL)
0582e403
OF
495#include <linux/mod_compat.h>
496
497static int
498param_set_multihost_interval(const char *val, zfs_kernel_param_t *kp)
499{
500 int ret;
501
502 ret = param_set_ulong(val, kp);
503 if (ret < 0)
504 return (ret);
505
506 mmp_signal_all_threads();
507
508 return (ret);
509}
510
379ca9cf
OF
511/* BEGIN CSTYLED */
512module_param(zfs_multihost_fail_intervals, uint, 0644);
513MODULE_PARM_DESC(zfs_multihost_fail_intervals,
514 "Max allowed period without a successful mmp write");
515
0582e403
OF
516module_param_call(zfs_multihost_interval, param_set_multihost_interval,
517 param_get_ulong, &zfs_multihost_interval, 0644);
379ca9cf
OF
518MODULE_PARM_DESC(zfs_multihost_interval,
519 "Milliseconds between mmp writes to each leaf");
520
521module_param(zfs_multihost_import_intervals, uint, 0644);
522MODULE_PARM_DESC(zfs_multihost_import_intervals,
523 "Number of zfs_multihost_interval periods to wait for activity");
524/* END CSTYLED */
525#endif