1fa9e406ahrens/*
2fa9e406ahrens * CDDL HEADER START
3fa9e406ahrens *
4fa9e406ahrens * The contents of this file are subject to the terms of the
5f65e61cahrens * Common Development and Distribution License (the "License").
6f65e61cahrens * You may not use this file except in compliance with the License.
7fa9e406ahrens *
8fa9e406ahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e406ahrens * or http://www.opensolaris.org/os/licensing.
10fa9e406ahrens * See the License for the specific language governing permissions
11fa9e406ahrens * and limitations under the License.
12fa9e406ahrens *
13fa9e406ahrens * When distributing Covered Code, include this CDDL HEADER in each
14fa9e406ahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e406ahrens * If applicable, add the following below this CDDL HEADER, with the
16fa9e406ahrens * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e406ahrens * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e406ahrens *
19fa9e406ahrens * CDDL HEADER END
20fa9e406ahrens */
21fa9e406ahrens/*
223f9d6adLin Ling * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23bc9014eJustin Gibbs * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
24fa9e406ahrens */
25fa9e406ahrens
26fa9e406ahrens#ifndef	_SYS_ZAP_LEAF_H
27fa9e406ahrens#define	_SYS_ZAP_LEAF_H
28fa9e406ahrens
293f9d6adLin Ling#include <sys/zap.h>
303f9d6adLin Ling
31fa9e406ahrens#ifdef	__cplusplus
32fa9e406ahrensextern "C" {
33fa9e406ahrens#endif
34fa9e406ahrens
35fa9e406ahrensstruct zap;
363f1f801Matthew Ahrensstruct zap_name;
373f1f801Matthew Ahrensstruct zap_stats;
38fa9e406ahrens
39fa9e406ahrens#define	ZAP_LEAF_MAGIC 0x2AB1EAF
40fa9e406ahrens
41fa9e406ahrens/* chunk size = 24 bytes */
42f65e61cahrens#define	ZAP_LEAF_CHUNKSIZE 24
43fa9e406ahrens
44f65e61cahrens/*
45f65e61cahrens * The amount of space available for chunks is:
46f65e61cahrens * block size (1<<l->l_bs) - hash entry size (2) * number of hash
47f65e61cahrens * entries - header space (2*chunksize)
48f65e61cahrens */
49f65e61cahrens#define	ZAP_LEAF_NUMCHUNKS(l) \
50f65e61cahrens	(((1<<(l)->l_bs) - 2*ZAP_LEAF_HASH_NUMENTRIES(l)) / \
51f65e61cahrens	ZAP_LEAF_CHUNKSIZE - 2)
52f65e61cahrens
53f65e61cahrens/*
54f65e61cahrens * The amount of space within the chunk available for the array is:
55f65e61cahrens * chunk size - space for type (1) - space for next pointer (2)
56f65e61cahrens */
57f65e61cahrens#define	ZAP_LEAF_ARRAY_BYTES (ZAP_LEAF_CHUNKSIZE - 3)
58f65e61cahrens
5966328ddahrens#define	ZAP_LEAF_ARRAY_NCHUNKS(bytes) \
6066328ddahrens	(((bytes)+ZAP_LEAF_ARRAY_BYTES-1)/ZAP_LEAF_ARRAY_BYTES)
6166328ddahrens
6266328ddahrens/*
6366328ddahrens * Low water mark:  when there are only this many chunks free, start
6466328ddahrens * growing the ptrtbl.  Ideally, this should be larger than a
6566328ddahrens * "reasonably-sized" entry.  20 chunks is more than enough for the
6666328ddahrens * largest directory entry (MAXNAMELEN (256) byte name, 8-byte value),
6766328ddahrens * while still being only around 3% for 16k blocks.
6866328ddahrens */
6966328ddahrens#define	ZAP_LEAF_LOW_WATER (20)
7066328ddahrens
71f65e61cahrens/*
72f65e61cahrens * The leaf hash table has block size / 2^5 (32) number of entries,
73f65e61cahrens * which should be more than enough for the maximum number of entries,
74f65e61cahrens * which is less than block size / CHUNKSIZE (24) / minimum number of
75f65e61cahrens * chunks per entry (3).
76f65e61cahrens */
77f65e61cahrens#define	ZAP_LEAF_HASH_SHIFT(l) ((l)->l_bs - 5)
78f65e61cahrens#define	ZAP_LEAF_HASH_NUMENTRIES(l) (1 << ZAP_LEAF_HASH_SHIFT(l))
79fa9e406ahrens
80f65e61cahrens/*
81f65e61cahrens * The chunks start immediately after the hash table.  The end of the
82f65e61cahrens * hash table is at l_hash + HASH_NUMENTRIES, which we simply cast to a
83f65e61cahrens * chunk_t.
84f65e61cahrens */
85f65e61cahrens#define	ZAP_LEAF_CHUNK(l, idx) \
86f65e61cahrens	((zap_leaf_chunk_t *) \
87c137962Justin T. Gibbs	(zap_leaf_phys(l)->l_hash + ZAP_LEAF_HASH_NUMENTRIES(l)))[idx]
88f65e61cahrens#define	ZAP_LEAF_ENTRY(l, idx) (&ZAP_LEAF_CHUNK(l, idx).l_entry)
89f65e61cahrens
90f65e61cahrenstypedef enum zap_chunk_type {
91f65e61cahrens	ZAP_CHUNK_FREE = 253,
92f65e61cahrens	ZAP_CHUNK_ENTRY = 252,
93f65e61cahrens	ZAP_CHUNK_ARRAY = 251,
94f65e61cahrens	ZAP_CHUNK_TYPE_MAX = 250
95f65e61cahrens} zap_chunk_type_t;
96fa9e406ahrens
97da6c28aamw#define	ZLF_ENTRIES_CDSORTED (1<<0)
98da6c28aamw
99fa9e406ahrens/*
100fa9e406ahrens * TAKE NOTE:
101fa9e406ahrens * If zap_leaf_phys_t is modified, zap_leaf_byteswap() must be modified.
102fa9e406ahrens */
103fa9e406ahrenstypedef struct zap_leaf_phys {
104fa9e406ahrens	struct zap_leaf_header {
105f717074Will Andrews		/* Public to ZAP */
10666328ddahrens		uint64_t lh_block_type;		/* ZBT_LEAF */
10766328ddahrens		uint64_t lh_pad1;
10866328ddahrens		uint64_t lh_prefix;		/* hash prefix of this leaf */
10966328ddahrens		uint32_t lh_magic;		/* ZAP_LEAF_MAGIC */
11066328ddahrens		uint16_t lh_nfree;		/* number free chunks */
11166328ddahrens		uint16_t lh_nentries;		/* number of entries */
11266328ddahrens		uint16_t lh_prefix_len;		/* num bits used to id this */
113fa9e406ahrens
114f717074Will Andrews		/* Private to zap_leaf */
115fa9e406ahrens		uint16_t lh_freelist;		/* chunk head of free list */
116da6c28aamw		uint8_t lh_flags;		/* ZLF_* flags */
117da6c28aamw		uint8_t lh_pad2[11];
118fa9e406ahrens	} l_hdr; /* 2 24-byte chunks */
119fa9e406ahrens
120f65e61cahrens	/*
121f65e61cahrens	 * The header is followed by a hash table with
122f65e61cahrens	 * ZAP_LEAF_HASH_NUMENTRIES(zap) entries.  The hash table is
123f65e61cahrens	 * followed by an array of ZAP_LEAF_NUMCHUNKS(zap)
124f65e61cahrens	 * zap_leaf_chunk structures.  These structures are accessed
125f65e61cahrens	 * with the ZAP_LEAF_CHUNK() macro.
126f65e61cahrens	 */
127f65e61cahrens
128f65e61cahrens	uint16_t l_hash[1];
129fa9e406ahrens} zap_leaf_phys_t;
130fa9e406ahrens
131f65e61cahrenstypedef union zap_leaf_chunk {
132f65e61cahrens	struct zap_leaf_entry {
133f65e61cahrens		uint8_t le_type; 		/* always ZAP_CHUNK_ENTRY */
134486ae71Matthew Ahrens		uint8_t le_value_intlen;	/* size of value's ints */
135f65e61cahrens		uint16_t le_next;		/* next entry in hash chain */
136f65e61cahrens		uint16_t le_name_chunk;		/* first chunk of the name */
137486ae71Matthew Ahrens		uint16_t le_name_numints;	/* ints in name (incl null) */
138f65e61cahrens		uint16_t le_value_chunk;	/* first chunk of the value */
139486ae71Matthew Ahrens		uint16_t le_value_numints;	/* value length in ints */
140f65e61cahrens		uint32_t le_cd;			/* collision differentiator */
141f65e61cahrens		uint64_t le_hash;		/* hash value of the name */
142f65e61cahrens	} l_entry;
143f65e61cahrens	struct zap_leaf_array {
144f65e61cahrens		uint8_t la_type;		/* always ZAP_CHUNK_ARRAY */
145f65e61cahrens		uint8_t la_array[ZAP_LEAF_ARRAY_BYTES];
146f65e61cahrens		uint16_t la_next;		/* next blk or CHAIN_END */
147f65e61cahrens	} l_array;
148f65e61cahrens	struct zap_leaf_free {
149f65e61cahrens		uint8_t lf_type;		/* always ZAP_CHUNK_FREE */
150f65e61cahrens		uint8_t lf_pad[ZAP_LEAF_ARRAY_BYTES];
151f65e61cahrens		uint16_t lf_next;	/* next in free list, or CHAIN_END */
152f65e61cahrens	} l_free;
153f65e61cahrens} zap_leaf_chunk_t;
154f65e61cahrens
155fa9e406ahrenstypedef struct zap_leaf {
156bc9014eJustin Gibbs	dmu_buf_user_t l_dbu;
157088f389ahrens	krwlock_t l_rwlock;
158fa9e406ahrens	uint64_t l_blkid;		/* 1<<ZAP_BLOCK_SHIFT byte block off */
159f65e61cahrens	int l_bs;			/* block size shift */
160fa9e406ahrens	dmu_buf_t *l_dbuf;
161fa9e406ahrens} zap_leaf_t;
162fa9e406ahrens
163c137962Justin T. Gibbsinline zap_leaf_phys_t *
164c137962Justin T. Gibbszap_leaf_phys(zap_leaf_t *l)
165c137962Justin T. Gibbs{
166c137962Justin T. Gibbs	return (l->l_dbuf->db_data);
167c137962Justin T. Gibbs}
168fa9e406ahrens
169fa9e406ahrenstypedef struct zap_entry_handle {
170f717074Will Andrews	/* Set by zap_leaf and public to ZAP */
171fa9e406ahrens	uint64_t zeh_num_integers;
172fa9e406ahrens	uint64_t zeh_hash;
173fa9e406ahrens	uint32_t zeh_cd;
174fa9e406ahrens	uint8_t zeh_integer_size;
175fa9e406ahrens
176f717074Will Andrews	/* Private to zap_leaf */
177fa9e406ahrens	uint16_t zeh_fakechunk;
178fa9e406ahrens	uint16_t *zeh_chunkp;
17966328ddahrens	zap_leaf_t *zeh_leaf;
180fa9e406ahrens} zap_entry_handle_t;
181fa9e406ahrens
182fa9e406ahrens/*
183fa9e406ahrens * Return a handle to the named entry, or ENOENT if not found.  The hash
184fa9e406ahrens * value must equal zap_hash(name).
185fa9e406ahrens */
186fa9e406ahrensextern int zap_leaf_lookup(zap_leaf_t *l,
1873f1f801Matthew Ahrens    struct zap_name *zn, zap_entry_handle_t *zeh);
188fa9e406ahrens
189fa9e406ahrens/*
190fa9e406ahrens * Return a handle to the entry with this hash+cd, or the entry with the
191fa9e406ahrens * next closest hash+cd.
192fa9e406ahrens */
193fa9e406ahrensextern int zap_leaf_lookup_closest(zap_leaf_t *l,
194fa9e406ahrens    uint64_t hash, uint32_t cd, zap_entry_handle_t *zeh);
195fa9e406ahrens
196fa9e406ahrens/*
197fa9e406ahrens * Read the first num_integers in the attribute.  Integer size
198fa9e406ahrens * conversion will be done without sign extension.  Return EINVAL if
199fa9e406ahrens * integer_size is too small.  Return EOVERFLOW if there are more than
200fa9e406ahrens * num_integers in the attribute.
201fa9e406ahrens */
202fa9e406ahrensextern int zap_entry_read(const zap_entry_handle_t *zeh,
203b24ab67Jeff Bonwick    uint8_t integer_size, uint64_t num_integers, void *buf);
204fa9e406ahrens
2053f1f801Matthew Ahrensextern int zap_entry_read_name(struct zap *zap, const zap_entry_handle_t *zeh,
206b24ab67Jeff Bonwick    uint16_t buflen, char *buf);
207fa9e406ahrens
208fa9e406ahrens/*
209fa9e406ahrens * Replace the value of an existing entry.
210fa9e406ahrens *
211f717074Will Andrews * May fail if it runs out of space (ENOSPC).
212fa9e406ahrens */
213fa9e406ahrensextern int zap_entry_update(zap_entry_handle_t *zeh,
214b24ab67Jeff Bonwick    uint8_t integer_size, uint64_t num_integers, const void *buf);
215fa9e406ahrens
216fa9e406ahrens/*
217fa9e406ahrens * Remove an entry.
218fa9e406ahrens */
219fa9e406ahrensextern void zap_entry_remove(zap_entry_handle_t *zeh);
220fa9e406ahrens
221fa9e406ahrens/*
222fa9e406ahrens * Create an entry. An equal entry must not exist, and this entry must
223fa9e406ahrens * belong in this leaf (according to its hash value).  Fills in the
224fa9e406ahrens * entry handle on success.  Returns 0 on success or ENOSPC on failure.
225fa9e406ahrens */
2263f1f801Matthew Ahrensextern int zap_entry_create(zap_leaf_t *l, struct zap_name *zn, uint32_t cd,
227b24ab67Jeff Bonwick    uint8_t integer_size, uint64_t num_integers, const void *buf,
228b24ab67Jeff Bonwick    zap_entry_handle_t *zeh);
229fa9e406ahrens
230f717074Will Andrews/* Determine whether there is another entry with the same normalized form. */
231da6c28aamwextern boolean_t zap_entry_normalization_conflict(zap_entry_handle_t *zeh,
2323f1f801Matthew Ahrens    struct zap_name *zn, const char *name, struct zap *zap);
233da6c28aamw
234da6c28aamw/*
235fa9e406ahrens * Other stuff.
236fa9e406ahrens */
237fa9e406ahrens
238de8267etimhextern void zap_leaf_init(zap_leaf_t *l, boolean_t sort);
239f65e61cahrensextern void zap_leaf_byteswap(zap_leaf_phys_t *buf, int len);
240de8267etimhextern void zap_leaf_split(zap_leaf_t *l, zap_leaf_t *nl, boolean_t sort);
2413f1f801Matthew Ahrensextern void zap_leaf_stats(struct zap *zap, zap_leaf_t *l,
2423f1f801Matthew Ahrens    struct zap_stats *zs);
243fa9e406ahrens
244fa9e406ahrens#ifdef	__cplusplus
245fa9e406ahrens}
246fa9e406ahrens#endif
247fa9e406ahrens
248fa9e406ahrens#endif /* _SYS_ZAP_LEAF_H */
249