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