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