xref: /illumos-gate/usr/src/uts/common/fs/zfs/sys/zap_impl.h (revision a07094369b21309434206d9b3601d162693466fc)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef	_SYS_ZAP_IMPL_H
28 #define	_SYS_ZAP_IMPL_H
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #include <sys/zap.h>
33 #include <sys/zfs_context.h>
34 #include <sys/avl.h>
35 
36 #ifdef	__cplusplus
37 extern "C" {
38 #endif
39 
40 #define	ZAP_MAGIC 0x2F52AB2AB
41 
42 #define	ZAP_BLOCK_SHIFT		17
43 
44 #define	ZAP_MAXCD		(uint32_t)(-1)
45 #define	ZAP_HASHBITS		28
46 #define	MZAP_ENT_LEN		64
47 #define	MZAP_NAME_LEN		(MZAP_ENT_LEN - 8 - 4 - 2)
48 #define	MZAP_MAX_BLKSHIFT	ZAP_BLOCK_SHIFT
49 #define	MZAP_MAX_BLKSZ		(1 << MZAP_MAX_BLKSHIFT)
50 
51 typedef struct mzap_ent_phys {
52 	uint64_t mze_value;
53 	uint32_t mze_cd;
54 	uint16_t mze_pad;	/* in case we want to chain them someday */
55 	char mze_name[MZAP_NAME_LEN];
56 } mzap_ent_phys_t;
57 
58 typedef struct mzap_phys {
59 	uint64_t mz_block_type;	/* ZBT_MICRO */
60 	uint64_t mz_salt;
61 	uint64_t mz_pad[6];
62 	mzap_ent_phys_t mz_chunk[1];
63 	/* actually variable size depending on block size */
64 } mzap_phys_t;
65 
66 typedef struct mzap_ent {
67 	avl_node_t mze_node;
68 	int mze_chunkid;
69 	uint64_t mze_hash;
70 	mzap_ent_phys_t mze_phys;
71 } mzap_ent_t;
72 
73 
74 /*
75  * The (fat) zap is stored in one object. It is an array of
76  * 1<<ZAP_BLOCK_SHIFT byte blocks. The layout looks like one of:
77  *
78  * ptrtbl fits in first block:
79  * 	[zap_phys_t zap_ptrtbl_shift < 6] [zap_leaf_t] ...
80  *
81  * ptrtbl too big for first block:
82  * 	[zap_phys_t zap_ptrtbl_shift >= 6] [zap_leaf_t] [ptrtbl] ...
83  *
84  */
85 
86 struct dmu_buf;
87 struct zap_leaf;
88 
89 #define	ZBT_LEAF		((1ULL << 63) + 0)
90 #define	ZBT_HEADER		((1ULL << 63) + 1)
91 #define	ZBT_MICRO		((1ULL << 63) + 3)
92 /* any other values are ptrtbl blocks */
93 
94 /* 1/2 the block size */
95 #define	ZAP_PTRTBL_MIN_SHIFT (ZAP_BLOCK_SHIFT - 3 - 1)
96 
97 /*
98  * TAKE NOTE:
99  * If zap_phys_t is modified, zap_byteswap() must be modified.
100  */
101 typedef struct zap_phys {
102 	uint64_t zap_block_type;	/* ZBT_HEADER */
103 	uint64_t zap_magic;		/* ZAP_MAGIC */
104 
105 	struct zap_table_phys {
106 		uint64_t zt_blk;	/* starting block number */
107 		uint64_t zt_numblks;	/* number of blocks */
108 		uint64_t zt_shift;	/* bits to index it */
109 		uint64_t zt_nextblk;	/* next (larger) copy start block */
110 		uint64_t zt_blks_copied; /* number source blocks copied */
111 	} zap_ptrtbl;
112 
113 	uint64_t zap_freeblk;		/* the next free block */
114 	uint64_t zap_num_leafs;		/* number of leafs */
115 	uint64_t zap_num_entries;	/* number of entries */
116 	uint64_t zap_salt;		/* salt to stir into hash function */
117 	uint64_t zap_pad[8181];
118 	uint64_t zap_leafs[1 << ZAP_PTRTBL_MIN_SHIFT];
119 } zap_phys_t;
120 
121 typedef struct zap_table_phys zap_table_phys_t;
122 
123 typedef struct zap {
124 	objset_t *zap_objset;
125 	uint64_t zap_object;
126 	struct dmu_buf *zap_dbuf;
127 	krwlock_t zap_rwlock;
128 	int zap_ismicro;
129 	uint64_t zap_salt;
130 	union {
131 		struct {
132 			zap_phys_t *zap_phys;
133 
134 			/*
135 			 * zap_num_entries_mtx protects
136 			 * zap_num_entries
137 			 */
138 			kmutex_t zap_num_entries_mtx;
139 		} zap_fat;
140 		struct {
141 			mzap_phys_t *zap_phys;
142 			int16_t zap_num_entries;
143 			int16_t zap_num_chunks;
144 			int16_t zap_alloc_next;
145 			avl_tree_t zap_avl;
146 		} zap_micro;
147 	} zap_u;
148 } zap_t;
149 
150 #define	zap_f	zap_u.zap_fat
151 #define	zap_m	zap_u.zap_micro
152 
153 uint64_t zap_hash(zap_t *zap, const char *name);
154 int zap_lockdir(objset_t *os, uint64_t obj, dmu_tx_t *tx,
155     krw_t lti, int fatreader, zap_t **zapp);
156 void zap_unlockdir(zap_t *zap);
157 void zap_pageout(dmu_buf_t *db, void *vmzap);
158 
159 void zap_print(zap_t *);
160 struct zap_leaf *zap_create_leaf(zap_t *zd, dmu_tx_t *tx);
161 void zap_destroy_leaf(zap_t *zap, struct zap_leaf *l, dmu_tx_t *tx);
162 uint64_t zap_allocate_blocks(zap_t *zap, int nblocks, dmu_tx_t *tx);
163 
164 #define	ZAP_HASH_IDX(hash, n) (((n) == 0) ? 0 : ((hash) >> (64 - (n))))
165 
166 void fzap_byteswap(void *buf, size_t size);
167 int fzap_count(zap_t *zap, uint64_t *count);
168 int fzap_lookup(zap_t *zap, const char *name,
169     uint64_t integer_size, uint64_t num_integers, void *buf);
170 int fzap_add(zap_t *zap, const char *name,
171     uint64_t integer_size, uint64_t num_integers,
172     const void *val, dmu_tx_t *tx);
173 int fzap_update(zap_t *zap, const char *name,
174     int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx);
175 int fzap_length(zap_t *zap, const char *name,
176     uint64_t *integer_size, uint64_t *num_integers);
177 int fzap_remove(zap_t *zap, const char *name, dmu_tx_t *tx);
178 int fzap_cursor_retrieve(zap_t *zap, zap_cursor_t *zc, zap_attribute_t *za);
179 void fzap_get_stats(zap_t *zap, zap_stats_t *zs);
180 void zap_put_leaf(struct zap_leaf *l);
181 
182 int fzap_add_cd(zap_t *zap, const char *name,
183     uint64_t integer_size, uint64_t num_integers,
184     const void *val, uint32_t cd, dmu_tx_t *tx, struct zap_leaf **lp);
185 void fzap_upgrade(zap_t *zap, dmu_tx_t *tx);
186 
187 #ifdef	__cplusplus
188 }
189 #endif
190 
191 #endif /* _SYS_ZAP_IMPL_H */
192