xref: /onnv-gate/usr/src/uts/common/fs/zfs/sys/zap_impl.h (revision 12450:c77e20e4e046)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51491Sahrens  * Common Development and Distribution License (the "License").
61491Sahrens  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
21789Sahrens /*
2212296SLin.Ling@Sun.COM  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23789Sahrens  */
24789Sahrens 
25789Sahrens #ifndef	_SYS_ZAP_IMPL_H
26789Sahrens #define	_SYS_ZAP_IMPL_H
27789Sahrens 
28789Sahrens #include <sys/zap.h>
29789Sahrens #include <sys/zfs_context.h>
30789Sahrens #include <sys/avl.h>
31789Sahrens 
32789Sahrens #ifdef	__cplusplus
33789Sahrens extern "C" {
34789Sahrens #endif
35789Sahrens 
361491Sahrens extern int fzap_default_block_shift;
371491Sahrens 
382856Snd150628 #define	ZAP_MAGIC 0x2F52AB2ABULL
39789Sahrens 
401491Sahrens #define	FZAP_BLOCK_SHIFT(zap)	((zap)->zap_f.zap_block_shift)
41789Sahrens 
42789Sahrens #define	MZAP_ENT_LEN		64
43789Sahrens #define	MZAP_NAME_LEN		(MZAP_ENT_LEN - 8 - 4 - 2)
441491Sahrens #define	MZAP_MAX_BLKSHIFT	SPA_MAXBLOCKSHIFT
45789Sahrens #define	MZAP_MAX_BLKSZ		(1 << MZAP_MAX_BLKSHIFT)
46789Sahrens 
4710922SJeff.Bonwick@Sun.COM #define	ZAP_NEED_CD		(-1U)
4810922SJeff.Bonwick@Sun.COM 
49789Sahrens typedef struct mzap_ent_phys {
50789Sahrens 	uint64_t mze_value;
51789Sahrens 	uint32_t mze_cd;
52789Sahrens 	uint16_t mze_pad;	/* in case we want to chain them someday */
53789Sahrens 	char mze_name[MZAP_NAME_LEN];
54789Sahrens } mzap_ent_phys_t;
55789Sahrens 
56789Sahrens typedef struct mzap_phys {
57789Sahrens 	uint64_t mz_block_type;	/* ZBT_MICRO */
58789Sahrens 	uint64_t mz_salt;
595331Samw 	uint64_t mz_normflags;
605331Samw 	uint64_t mz_pad[5];
61789Sahrens 	mzap_ent_phys_t mz_chunk[1];
62789Sahrens 	/* actually variable size depending on block size */
63789Sahrens } mzap_phys_t;
64789Sahrens 
65789Sahrens typedef struct mzap_ent {
66789Sahrens 	avl_node_t mze_node;
67789Sahrens 	int mze_chunkid;
68789Sahrens 	uint64_t mze_hash;
6912296SLin.Ling@Sun.COM 	uint32_t mze_cd; /* copy from mze_phys->mze_cd */
70789Sahrens } mzap_ent_t;
71789Sahrens 
7212296SLin.Ling@Sun.COM #define	MZE_PHYS(zap, mze) \
7312296SLin.Ling@Sun.COM 	(&(zap)->zap_m.zap_phys->mz_chunk[(mze)->mze_chunkid])
7412296SLin.Ling@Sun.COM 
75789Sahrens /*
76789Sahrens  * The (fat) zap is stored in one object. It is an array of
771491Sahrens  * 1<<FZAP_BLOCK_SHIFT byte blocks. The layout looks like one of:
78789Sahrens  *
79789Sahrens  * ptrtbl fits in first block:
80789Sahrens  * 	[zap_phys_t zap_ptrtbl_shift < 6] [zap_leaf_t] ...
81789Sahrens  *
82789Sahrens  * ptrtbl too big for first block:
83789Sahrens  * 	[zap_phys_t zap_ptrtbl_shift >= 6] [zap_leaf_t] [ptrtbl] ...
84789Sahrens  *
85789Sahrens  */
86789Sahrens 
87789Sahrens struct dmu_buf;
88789Sahrens struct zap_leaf;
89789Sahrens 
90789Sahrens #define	ZBT_LEAF		((1ULL << 63) + 0)
91789Sahrens #define	ZBT_HEADER		((1ULL << 63) + 1)
92789Sahrens #define	ZBT_MICRO		((1ULL << 63) + 3)
93789Sahrens /* any other values are ptrtbl blocks */
94789Sahrens 
951491Sahrens /*
961491Sahrens  * the embedded pointer table takes up half a block:
971491Sahrens  * block size / entry size (2^3) / 2
981491Sahrens  */
991491Sahrens #define	ZAP_EMBEDDED_PTRTBL_SHIFT(zap) (FZAP_BLOCK_SHIFT(zap) - 3 - 1)
1001491Sahrens 
1011491Sahrens /*
1021491Sahrens  * The embedded pointer table starts half-way through the block.  Since
1031491Sahrens  * the pointer table itself is half the block, it starts at (64-bit)
1041491Sahrens  * word number (1<<ZAP_EMBEDDED_PTRTBL_SHIFT(zap)).
1051491Sahrens  */
1061491Sahrens #define	ZAP_EMBEDDED_PTRTBL_ENT(zap, idx) \
1071491Sahrens 	((uint64_t *)(zap)->zap_f.zap_phys) \
1081491Sahrens 	[(idx) + (1<<ZAP_EMBEDDED_PTRTBL_SHIFT(zap))]
109789Sahrens 
110789Sahrens /*
111789Sahrens  * TAKE NOTE:
112789Sahrens  * If zap_phys_t is modified, zap_byteswap() must be modified.
113789Sahrens  */
114789Sahrens typedef struct zap_phys {
115789Sahrens 	uint64_t zap_block_type;	/* ZBT_HEADER */
116789Sahrens 	uint64_t zap_magic;		/* ZAP_MAGIC */
117789Sahrens 
118789Sahrens 	struct zap_table_phys {
119789Sahrens 		uint64_t zt_blk;	/* starting block number */
120789Sahrens 		uint64_t zt_numblks;	/* number of blocks */
121789Sahrens 		uint64_t zt_shift;	/* bits to index it */
122789Sahrens 		uint64_t zt_nextblk;	/* next (larger) copy start block */
123789Sahrens 		uint64_t zt_blks_copied; /* number source blocks copied */
124789Sahrens 	} zap_ptrtbl;
125789Sahrens 
126789Sahrens 	uint64_t zap_freeblk;		/* the next free block */
127789Sahrens 	uint64_t zap_num_leafs;		/* number of leafs */
128789Sahrens 	uint64_t zap_num_entries;	/* number of entries */
129789Sahrens 	uint64_t zap_salt;		/* salt to stir into hash function */
1305331Samw 	uint64_t zap_normflags;		/* flags for u8_textprep_str() */
13110922SJeff.Bonwick@Sun.COM 	uint64_t zap_flags;		/* zap_flags_t */
1321491Sahrens 	/*
1331491Sahrens 	 * This structure is followed by padding, and then the embedded
1341491Sahrens 	 * pointer table.  The embedded pointer table takes up second
1351491Sahrens 	 * half of the block.  It is accessed using the
1361491Sahrens 	 * ZAP_EMBEDDED_PTRTBL_ENT() macro.
1371491Sahrens 	 */
138789Sahrens } zap_phys_t;
139789Sahrens 
140789Sahrens typedef struct zap_table_phys zap_table_phys_t;
141789Sahrens 
142789Sahrens typedef struct zap {
143789Sahrens 	objset_t *zap_objset;
144789Sahrens 	uint64_t zap_object;
145789Sahrens 	struct dmu_buf *zap_dbuf;
146789Sahrens 	krwlock_t zap_rwlock;
1475331Samw 	boolean_t zap_ismicro;
1485331Samw 	int zap_normflags;
149789Sahrens 	uint64_t zap_salt;
150789Sahrens 	union {
151789Sahrens 		struct {
152789Sahrens 			zap_phys_t *zap_phys;
153789Sahrens 
154789Sahrens 			/*
155789Sahrens 			 * zap_num_entries_mtx protects
156789Sahrens 			 * zap_num_entries
157789Sahrens 			 */
158789Sahrens 			kmutex_t zap_num_entries_mtx;
1591491Sahrens 			int zap_block_shift;
160789Sahrens 		} zap_fat;
161789Sahrens 		struct {
162789Sahrens 			mzap_phys_t *zap_phys;
163789Sahrens 			int16_t zap_num_entries;
164789Sahrens 			int16_t zap_num_chunks;
165789Sahrens 			int16_t zap_alloc_next;
166789Sahrens 			avl_tree_t zap_avl;
167789Sahrens 		} zap_micro;
168789Sahrens 	} zap_u;
169789Sahrens } zap_t;
170789Sahrens 
1715331Samw typedef struct zap_name {
1725331Samw 	zap_t *zn_zap;
17310922SJeff.Bonwick@Sun.COM 	int zn_key_intlen;
17410922SJeff.Bonwick@Sun.COM 	const void *zn_key_orig;
17511165SMatthew.Ahrens@Sun.COM 	int zn_key_orig_numints;
17610922SJeff.Bonwick@Sun.COM 	const void *zn_key_norm;
17711165SMatthew.Ahrens@Sun.COM 	int zn_key_norm_numints;
1785331Samw 	uint64_t zn_hash;
1795331Samw 	matchtype_t zn_matchtype;
1805331Samw 	char zn_normbuf[ZAP_MAXNAMELEN];
1815331Samw } zap_name_t;
1825331Samw 
183789Sahrens #define	zap_f	zap_u.zap_fat
184789Sahrens #define	zap_m	zap_u.zap_micro
185789Sahrens 
1865331Samw boolean_t zap_match(zap_name_t *zn, const char *matchname);
187789Sahrens int zap_lockdir(objset_t *os, uint64_t obj, dmu_tx_t *tx,
1885384Sahrens     krw_t lti, boolean_t fatreader, boolean_t adding, zap_t **zapp);
189789Sahrens void zap_unlockdir(zap_t *zap);
1902641Sahrens void zap_evict(dmu_buf_t *db, void *vmzap);
19110922SJeff.Bonwick@Sun.COM zap_name_t *zap_name_alloc(zap_t *zap, const char *key, matchtype_t mt);
1925331Samw void zap_name_free(zap_name_t *zn);
19310922SJeff.Bonwick@Sun.COM int zap_hashbits(zap_t *zap);
19410922SJeff.Bonwick@Sun.COM uint32_t zap_maxcd(zap_t *zap);
19510922SJeff.Bonwick@Sun.COM uint64_t zap_getflags(zap_t *zap);
196789Sahrens 
197789Sahrens #define	ZAP_HASH_IDX(hash, n) (((n) == 0) ? 0 : ((hash) >> (64 - (n))))
198789Sahrens 
199789Sahrens void fzap_byteswap(void *buf, size_t size);
200789Sahrens int fzap_count(zap_t *zap, uint64_t *count);
2015331Samw int fzap_lookup(zap_name_t *zn,
2025331Samw     uint64_t integer_size, uint64_t num_integers, void *buf,
2035331Samw     char *realname, int rn_len, boolean_t *normalization_conflictp);
204*12450SGeorge.Wilson@Sun.COM void fzap_prefetch(zap_name_t *zn);
2059653SSanjeev.Bagewadi@Sun.COM int fzap_count_write(zap_name_t *zn, int add, uint64_t *towrite,
2069653SSanjeev.Bagewadi@Sun.COM     uint64_t *tooverwrite);
2075331Samw int fzap_add(zap_name_t *zn, uint64_t integer_size, uint64_t num_integers,
208789Sahrens     const void *val, dmu_tx_t *tx);
2095331Samw int fzap_update(zap_name_t *zn,
210789Sahrens     int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx);
2115331Samw int fzap_length(zap_name_t *zn,
212789Sahrens     uint64_t *integer_size, uint64_t *num_integers);
2135331Samw int fzap_remove(zap_name_t *zn, dmu_tx_t *tx);
214789Sahrens int fzap_cursor_retrieve(zap_t *zap, zap_cursor_t *zc, zap_attribute_t *za);
215789Sahrens void fzap_get_stats(zap_t *zap, zap_stats_t *zs);
216885Sahrens void zap_put_leaf(struct zap_leaf *l);
217789Sahrens 
2185331Samw int fzap_add_cd(zap_name_t *zn,
219789Sahrens     uint64_t integer_size, uint64_t num_integers,
2201544Seschrock     const void *val, uint32_t cd, dmu_tx_t *tx);
22110922SJeff.Bonwick@Sun.COM void fzap_upgrade(zap_t *zap, dmu_tx_t *tx, zap_flags_t flags);
22210612SRicardo.M.Correia@Sun.COM int fzap_cursor_move_to_key(zap_cursor_t *zc, zap_name_t *zn);
223789Sahrens 
224789Sahrens #ifdef	__cplusplus
225789Sahrens }
226789Sahrens #endif
227789Sahrens 
228789Sahrens #endif /* _SYS_ZAP_IMPL_H */
229