]>
Commit | Line | Data |
---|---|---|
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> | |
428870ff BB |
32 | |
33 | int ddt_zap_leaf_blockshift = 12; | |
34 | int ddt_zap_indirect_blockshift = 12; | |
35 | ||
36 | static int | |
37 | ddt_zap_create(objset_t *os, uint64_t *objectp, dmu_tx_t *tx, boolean_t prehash) | |
38 | { | |
39 | zap_flags_t flags = ZAP_FLAG_HASH64 | ZAP_FLAG_UINT64_KEY; | |
40 | ||
41 | if (prehash) | |
42 | flags |= ZAP_FLAG_PRE_HASHED_KEY; | |
43 | ||
44 | *objectp = zap_create_flags(os, 0, flags, DMU_OT_DDT_ZAP, | |
45 | ddt_zap_leaf_blockshift, ddt_zap_indirect_blockshift, | |
46 | DMU_OT_NONE, 0, tx); | |
47 | ||
48 | return (*objectp == 0 ? ENOTSUP : 0); | |
49 | } | |
50 | ||
51 | static int | |
52 | ddt_zap_destroy(objset_t *os, uint64_t object, dmu_tx_t *tx) | |
53 | { | |
54 | return (zap_destroy(os, object, tx)); | |
55 | } | |
56 | ||
57 | static int | |
58 | ddt_zap_lookup(objset_t *os, uint64_t object, ddt_entry_t *dde) | |
59 | { | |
5b8c7bbc | 60 | uchar_t *cbuf; |
428870ff BB |
61 | uint64_t one, csize; |
62 | int error; | |
63 | ||
79c76d5b | 64 | cbuf = kmem_alloc(sizeof (dde->dde_phys) + 1, KM_SLEEP); |
5b8c7bbc | 65 | |
428870ff BB |
66 | error = zap_length_uint64(os, object, (uint64_t *)&dde->dde_key, |
67 | DDT_KEY_WORDS, &one, &csize); | |
68 | if (error) | |
5b8c7bbc | 69 | goto out; |
428870ff BB |
70 | |
71 | ASSERT(one == 1); | |
42d3b990 | 72 | ASSERT(csize <= (sizeof (dde->dde_phys) + 1)); |
428870ff BB |
73 | |
74 | error = zap_lookup_uint64(os, object, (uint64_t *)&dde->dde_key, | |
75 | DDT_KEY_WORDS, 1, csize, cbuf); | |
76 | if (error) | |
5b8c7bbc | 77 | goto out; |
428870ff BB |
78 | |
79 | ddt_decompress(cbuf, dde->dde_phys, csize, sizeof (dde->dde_phys)); | |
5b8c7bbc BB |
80 | out: |
81 | kmem_free(cbuf, sizeof (dde->dde_phys) + 1); | |
428870ff | 82 | |
5b8c7bbc | 83 | return (error); |
428870ff BB |
84 | } |
85 | ||
86 | static void | |
87 | ddt_zap_prefetch(objset_t *os, uint64_t object, ddt_entry_t *dde) | |
88 | { | |
89 | (void) zap_prefetch_uint64(os, object, (uint64_t *)&dde->dde_key, | |
90 | DDT_KEY_WORDS); | |
91 | } | |
92 | ||
93 | static int | |
94 | ddt_zap_update(objset_t *os, uint64_t object, ddt_entry_t *dde, dmu_tx_t *tx) | |
95 | { | |
96 | uchar_t cbuf[sizeof (dde->dde_phys) + 1]; | |
97 | uint64_t csize; | |
98 | ||
99 | csize = ddt_compress(dde->dde_phys, cbuf, | |
100 | sizeof (dde->dde_phys), sizeof (cbuf)); | |
101 | ||
102 | return (zap_update_uint64(os, object, (uint64_t *)&dde->dde_key, | |
103 | DDT_KEY_WORDS, 1, csize, cbuf, tx)); | |
104 | } | |
105 | ||
106 | static int | |
107 | ddt_zap_remove(objset_t *os, uint64_t object, ddt_entry_t *dde, dmu_tx_t *tx) | |
108 | { | |
109 | return (zap_remove_uint64(os, object, (uint64_t *)&dde->dde_key, | |
110 | DDT_KEY_WORDS, tx)); | |
111 | } | |
112 | ||
113 | static int | |
114 | ddt_zap_walk(objset_t *os, uint64_t object, ddt_entry_t *dde, uint64_t *walk) | |
115 | { | |
116 | zap_cursor_t zc; | |
117 | zap_attribute_t za; | |
118 | int error; | |
119 | ||
120 | zap_cursor_init_serialized(&zc, os, object, *walk); | |
121 | if ((error = zap_cursor_retrieve(&zc, &za)) == 0) { | |
122 | uchar_t cbuf[sizeof (dde->dde_phys) + 1]; | |
123 | uint64_t csize = za.za_num_integers; | |
124 | ASSERT(za.za_integer_length == 1); | |
125 | error = zap_lookup_uint64(os, object, (uint64_t *)za.za_name, | |
126 | DDT_KEY_WORDS, 1, csize, cbuf); | |
127 | ASSERT(error == 0); | |
128 | if (error == 0) { | |
129 | ddt_decompress(cbuf, dde->dde_phys, csize, | |
130 | sizeof (dde->dde_phys)); | |
131 | dde->dde_key = *(ddt_key_t *)za.za_name; | |
132 | } | |
133 | zap_cursor_advance(&zc); | |
134 | *walk = zap_cursor_serialize(&zc); | |
135 | } | |
136 | zap_cursor_fini(&zc); | |
137 | return (error); | |
138 | } | |
139 | ||
e8fd45a0 BB |
140 | static int |
141 | ddt_zap_count(objset_t *os, uint64_t object, uint64_t *count) | |
428870ff | 142 | { |
d1d7e268 | 143 | return (zap_count(os, object, count)); |
428870ff BB |
144 | } |
145 | ||
146 | const ddt_ops_t ddt_zap_ops = { | |
147 | "zap", | |
148 | ddt_zap_create, | |
149 | ddt_zap_destroy, | |
150 | ddt_zap_lookup, | |
151 | ddt_zap_prefetch, | |
152 | ddt_zap_update, | |
153 | ddt_zap_remove, | |
154 | ddt_zap_walk, | |
155 | ddt_zap_count, | |
156 | }; |