xref: /freebsd/sys/contrib/openzfs/module/zfs/uberblock.c (revision 22649d4dba730d46244fd2dff4fd174903c8379f)
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) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
14  * Copyright (c) 2013, 2017 by Delphix. All rights reserved.
15  */
16 
17 #include <sys/zfs_context.h>
18 #include <sys/uberblock_impl.h>
19 #include <sys/vdev_impl.h>
20 #include <sys/mmp.h>
21 
22 int
uberblock_verify(uberblock_t * ub)23 uberblock_verify(uberblock_t *ub)
24 {
25 	if (ub->ub_magic == BSWAP_64((uint64_t)UBERBLOCK_MAGIC))
26 		byteswap_uint64_array(ub, sizeof (uberblock_t));
27 
28 	if (ub->ub_magic != UBERBLOCK_MAGIC)
29 		return (SET_ERROR(EINVAL));
30 
31 	return (0);
32 }
33 
34 /*
35  * Update the uberblock and return TRUE if anything changed in this
36  * transaction group.
37  */
38 boolean_t
uberblock_update(uberblock_t * ub,vdev_t * rvd,uint64_t txg,uint64_t mmp_delay)39 uberblock_update(uberblock_t *ub, vdev_t *rvd, uint64_t txg, uint64_t mmp_delay)
40 {
41 	ASSERT(ub->ub_txg < txg);
42 
43 	/*
44 	 * We explicitly do not set ub_version here, so that older versions
45 	 * continue to be written with the previous uberblock version.
46 	 */
47 	ub->ub_magic = UBERBLOCK_MAGIC;
48 	ub->ub_txg = txg;
49 	ub->ub_guid_sum = rvd->vdev_guid_sum;
50 	ub->ub_timestamp = gethrestime_sec();
51 	ub->ub_software_version = SPA_VERSION;
52 	ub->ub_mmp_magic = MMP_MAGIC;
53 	if (spa_multihost(rvd->vdev_spa)) {
54 		ub->ub_mmp_delay = mmp_delay;
55 		ub->ub_mmp_config = MMP_SEQ_SET(0) |
56 		    MMP_INTERVAL_SET(zfs_multihost_interval) |
57 		    MMP_FAIL_INT_SET(zfs_multihost_fail_intervals);
58 	} else {
59 		ub->ub_mmp_delay = 0;
60 		ub->ub_mmp_config = 0;
61 	}
62 	ub->ub_checkpoint_txg = 0;
63 
64 	return (BP_GET_LOGICAL_BIRTH(&ub->ub_rootbp) == txg);
65 }
66