]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/ddt_zap.c
cstyle: Resolve C style issues
[mirror_zfs.git] / module / zfs / ddt_zap.c
CommitLineData
428870ff
BB
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 */
25
26#include <sys/zfs_context.h>
27#include <sys/spa.h>
28#include <sys/zio.h>
29#include <sys/ddt.h>
30#include <sys/zap.h>
31#include <sys/dmu_tx.h>
32#include <util/sscanf.h>
33
34int ddt_zap_leaf_blockshift = 12;
35int ddt_zap_indirect_blockshift = 12;
36
37static int
38ddt_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
52static int
53ddt_zap_destroy(objset_t *os, uint64_t object, dmu_tx_t *tx)
54{
55 return (zap_destroy(os, object, tx));
56}
57
58static int
59ddt_zap_lookup(objset_t *os, uint64_t object, ddt_entry_t *dde)
60{
5b8c7bbc 61 uchar_t *cbuf;
428870ff
BB
62 uint64_t one, csize;
63 int error;
64
d75d6f29 65 cbuf = kmem_alloc(sizeof (dde->dde_phys) + 1, KM_PUSHPAGE);
5b8c7bbc 66
428870ff
BB
67 error = zap_length_uint64(os, object, (uint64_t *)&dde->dde_key,
68 DDT_KEY_WORDS, &one, &csize);
69 if (error)
5b8c7bbc 70 goto out;
428870ff
BB
71
72 ASSERT(one == 1);
42d3b990 73 ASSERT(csize <= (sizeof (dde->dde_phys) + 1));
428870ff
BB
74
75 error = zap_lookup_uint64(os, object, (uint64_t *)&dde->dde_key,
76 DDT_KEY_WORDS, 1, csize, cbuf);
77 if (error)
5b8c7bbc 78 goto out;
428870ff
BB
79
80 ddt_decompress(cbuf, dde->dde_phys, csize, sizeof (dde->dde_phys));
5b8c7bbc
BB
81out:
82 kmem_free(cbuf, sizeof (dde->dde_phys) + 1);
428870ff 83
5b8c7bbc 84 return (error);
428870ff
BB
85}
86
87static void
88ddt_zap_prefetch(objset_t *os, uint64_t object, ddt_entry_t *dde)
89{
90 (void) zap_prefetch_uint64(os, object, (uint64_t *)&dde->dde_key,
91 DDT_KEY_WORDS);
92}
93
94static int
95ddt_zap_update(objset_t *os, uint64_t object, ddt_entry_t *dde, dmu_tx_t *tx)
96{
97 uchar_t cbuf[sizeof (dde->dde_phys) + 1];
98 uint64_t csize;
99
100 csize = ddt_compress(dde->dde_phys, cbuf,
101 sizeof (dde->dde_phys), sizeof (cbuf));
102
103 return (zap_update_uint64(os, object, (uint64_t *)&dde->dde_key,
104 DDT_KEY_WORDS, 1, csize, cbuf, tx));
105}
106
107static int
108ddt_zap_remove(objset_t *os, uint64_t object, ddt_entry_t *dde, dmu_tx_t *tx)
109{
110 return (zap_remove_uint64(os, object, (uint64_t *)&dde->dde_key,
111 DDT_KEY_WORDS, tx));
112}
113
114static int
115ddt_zap_walk(objset_t *os, uint64_t object, ddt_entry_t *dde, uint64_t *walk)
116{
117 zap_cursor_t zc;
118 zap_attribute_t za;
119 int error;
120
121 zap_cursor_init_serialized(&zc, os, object, *walk);
122 if ((error = zap_cursor_retrieve(&zc, &za)) == 0) {
123 uchar_t cbuf[sizeof (dde->dde_phys) + 1];
124 uint64_t csize = za.za_num_integers;
125 ASSERT(za.za_integer_length == 1);
126 error = zap_lookup_uint64(os, object, (uint64_t *)za.za_name,
127 DDT_KEY_WORDS, 1, csize, cbuf);
128 ASSERT(error == 0);
129 if (error == 0) {
130 ddt_decompress(cbuf, dde->dde_phys, csize,
131 sizeof (dde->dde_phys));
132 dde->dde_key = *(ddt_key_t *)za.za_name;
133 }
134 zap_cursor_advance(&zc);
135 *walk = zap_cursor_serialize(&zc);
136 }
137 zap_cursor_fini(&zc);
138 return (error);
139}
140
e8fd45a0
BB
141static int
142ddt_zap_count(objset_t *os, uint64_t object, uint64_t *count)
428870ff 143{
d1d7e268 144 return (zap_count(os, object, count));
428870ff
BB
145}
146
147const ddt_ops_t ddt_zap_ops = {
148 "zap",
149 ddt_zap_create,
150 ddt_zap_destroy,
151 ddt_zap_lookup,
152 ddt_zap_prefetch,
153 ddt_zap_update,
154 ddt_zap_remove,
155 ddt_zap_walk,
156 ddt_zap_count,
157};