xref: /illumos-gate/usr/src/uts/common/fs/zfs/ddt_zap.c (revision ce17336ed725d3b7fdff67bf0a0ee2b55018fec6)
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 /*
23  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright (c) 2018 by Delphix. All rights reserved.
25  */
26 
27 #include <sys/zfs_context.h>
28 #include <sys/spa.h>
29 #include <sys/zio.h>
30 #include <sys/ddt.h>
31 #include <sys/zap.h>
32 #include <sys/dmu_tx.h>
33 #include <util/sscanf.h>
34 
35 int ddt_zap_leaf_blockshift = 12;
36 int ddt_zap_indirect_blockshift = 12;
37 
38 static int
39 ddt_zap_create(objset_t *os, uint64_t *objectp, dmu_tx_t *tx, boolean_t prehash)
40 {
41 	zap_flags_t flags = ZAP_FLAG_HASH64 | ZAP_FLAG_UINT64_KEY;
42 
43 	if (prehash)
44 		flags |= ZAP_FLAG_PRE_HASHED_KEY;
45 
46 	*objectp = zap_create_flags(os, 0, flags, DMU_OT_DDT_ZAP,
47 	    ddt_zap_leaf_blockshift, ddt_zap_indirect_blockshift,
48 	    DMU_OT_NONE, 0, tx);
49 
50 	return (*objectp == 0 ? ENOTSUP : 0);
51 }
52 
53 static int
54 ddt_zap_destroy(objset_t *os, uint64_t object, dmu_tx_t *tx)
55 {
56 	return (zap_destroy(os, object, tx));
57 }
58 
59 static int
60 ddt_zap_lookup(objset_t *os, uint64_t object, ddt_entry_t *dde)
61 {
62 	uchar_t cbuf[sizeof (dde->dde_phys) + 1];
63 	uint64_t one, csize;
64 	int error;
65 
66 	error = zap_length_uint64(os, object, (uint64_t *)&dde->dde_key,
67 	    DDT_KEY_WORDS, &one, &csize);
68 	if (error)
69 		return (error);
70 
71 	ASSERT(one == 1);
72 	ASSERT(csize <= sizeof (cbuf));
73 
74 	error = zap_lookup_uint64(os, object, (uint64_t *)&dde->dde_key,
75 	    DDT_KEY_WORDS, 1, csize, cbuf);
76 	if (error)
77 		return (error);
78 
79 	ddt_decompress(cbuf, dde->dde_phys, csize, sizeof (dde->dde_phys));
80 
81 	return (0);
82 }
83 
84 static void
85 ddt_zap_prefetch(objset_t *os, uint64_t object, ddt_entry_t *dde)
86 {
87 	(void) zap_prefetch_uint64(os, object, (uint64_t *)&dde->dde_key,
88 	    DDT_KEY_WORDS);
89 }
90 
91 static int
92 ddt_zap_update(objset_t *os, uint64_t object, ddt_entry_t *dde, dmu_tx_t *tx)
93 {
94 	uchar_t cbuf[sizeof (dde->dde_phys) + 1];
95 	uint64_t csize;
96 
97 	csize = ddt_compress(dde->dde_phys, cbuf,
98 	    sizeof (dde->dde_phys), sizeof (cbuf));
99 
100 	return (zap_update_uint64(os, object, (uint64_t *)&dde->dde_key,
101 	    DDT_KEY_WORDS, 1, csize, cbuf, tx));
102 }
103 
104 static int
105 ddt_zap_remove(objset_t *os, uint64_t object, ddt_entry_t *dde, dmu_tx_t *tx)
106 {
107 	return (zap_remove_uint64(os, object, (uint64_t *)&dde->dde_key,
108 	    DDT_KEY_WORDS, tx));
109 }
110 
111 static int
112 ddt_zap_walk(objset_t *os, uint64_t object, ddt_entry_t *dde, uint64_t *walk)
113 {
114 	zap_cursor_t zc;
115 	zap_attribute_t za;
116 	int error;
117 
118 	if (*walk == 0) {
119 		/*
120 		 * We don't want to prefetch the entire ZAP object, because
121 		 * it can be enormous.  Also the primary use of DDT iteration
122 		 * is for scrubbing, in which case we will be issuing many
123 		 * scrub i/os for each ZAP block that we read in, so
124 		 * reading the ZAP is unlikely to be the bottleneck.
125 		 */
126 		zap_cursor_init_noprefetch(&zc, os, object);
127 	} else {
128 		zap_cursor_init_serialized(&zc, os, object, *walk);
129 	}
130 	if ((error = zap_cursor_retrieve(&zc, &za)) == 0) {
131 		uchar_t cbuf[sizeof (dde->dde_phys) + 1];
132 		uint64_t csize = za.za_num_integers;
133 		ASSERT(za.za_integer_length == 1);
134 		error = zap_lookup_uint64(os, object, (uint64_t *)za.za_name,
135 		    DDT_KEY_WORDS, 1, csize, cbuf);
136 		ASSERT(error == 0);
137 		if (error == 0) {
138 			ddt_decompress(cbuf, dde->dde_phys, csize,
139 			    sizeof (dde->dde_phys));
140 			dde->dde_key = *(ddt_key_t *)za.za_name;
141 		}
142 		zap_cursor_advance(&zc);
143 		*walk = zap_cursor_serialize(&zc);
144 	}
145 	zap_cursor_fini(&zc);
146 	return (error);
147 }
148 
149 static uint64_t
150 ddt_zap_count(objset_t *os, uint64_t object)
151 {
152 	uint64_t count = 0;
153 
154 	VERIFY(zap_count(os, object, &count) == 0);
155 
156 	return (count);
157 }
158 
159 const ddt_ops_t ddt_zap_ops = {
160 	"zap",
161 	ddt_zap_create,
162 	ddt_zap_destroy,
163 	ddt_zap_lookup,
164 	ddt_zap_prefetch,
165 	ddt_zap_update,
166 	ddt_zap_remove,
167 	ddt_zap_walk,
168 	ddt_zap_count,
169 };
170