xref: /freebsd/usr.sbin/makefs/zfs/zap.c (revision c6890399fc00bd628066b94c4add3f7e34815b11)
1240afd8cSMark Johnston /*-
2240afd8cSMark Johnston  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3240afd8cSMark Johnston  *
4240afd8cSMark Johnston  * Copyright (c) 2022 The FreeBSD Foundation
5240afd8cSMark Johnston  *
6240afd8cSMark Johnston  * This software was developed by Mark Johnston under sponsorship from
7240afd8cSMark Johnston  * the FreeBSD Foundation.
8240afd8cSMark Johnston  *
9240afd8cSMark Johnston  * Redistribution and use in source and binary forms, with or without
10240afd8cSMark Johnston  * modification, are permitted provided that the following conditions are
11240afd8cSMark Johnston  * met:
12240afd8cSMark Johnston  * 1. Redistributions of source code must retain the above copyright
13240afd8cSMark Johnston  *    notice, this list of conditions and the following disclaimer.
14240afd8cSMark Johnston  * 2. Redistributions in binary form must reproduce the above copyright
15240afd8cSMark Johnston  *    notice, this list of conditions and the following disclaimer in
16240afd8cSMark Johnston  *    the documentation and/or other materials provided with the distribution.
17240afd8cSMark Johnston  *
18240afd8cSMark Johnston  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19240afd8cSMark Johnston  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20240afd8cSMark Johnston  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21240afd8cSMark Johnston  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22240afd8cSMark Johnston  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23240afd8cSMark Johnston  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24240afd8cSMark Johnston  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25240afd8cSMark Johnston  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26240afd8cSMark Johnston  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27240afd8cSMark Johnston  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28240afd8cSMark Johnston  * SUCH DAMAGE.
29240afd8cSMark Johnston  */
30240afd8cSMark Johnston 
31240afd8cSMark Johnston #include <sys/types.h>
32240afd8cSMark Johnston #include <sys/endian.h>
33240afd8cSMark Johnston 
34240afd8cSMark Johnston #include <assert.h>
35240afd8cSMark Johnston #include <stddef.h>
36*c6890399SJessica Clarke #include <stdlib.h>
37240afd8cSMark Johnston #include <string.h>
38240afd8cSMark Johnston 
39240afd8cSMark Johnston #include <util.h>
40240afd8cSMark Johnston 
41240afd8cSMark Johnston #include "makefs.h"
42240afd8cSMark Johnston #include "zfs.h"
43240afd8cSMark Johnston 
44240afd8cSMark Johnston typedef struct zfs_zap_entry {
45240afd8cSMark Johnston 	char		*name;		/* entry key, private copy */
46240afd8cSMark Johnston 	uint64_t	hash;		/* key hash */
47240afd8cSMark Johnston 	union {
48240afd8cSMark Johnston 		uint8_t	 *valp;
49240afd8cSMark Johnston 		uint16_t *val16p;
50240afd8cSMark Johnston 		uint32_t *val32p;
51240afd8cSMark Johnston 		uint64_t *val64p;
52240afd8cSMark Johnston 	};				/* entry value, an integer array */
53240afd8cSMark Johnston 	uint64_t	val64;		/* embedded value for a common case */
54240afd8cSMark Johnston 	size_t		intsz;		/* array element size; 1, 2, 4 or 8 */
55240afd8cSMark Johnston 	size_t		intcnt;		/* array size */
56240afd8cSMark Johnston 	STAILQ_ENTRY(zfs_zap_entry) next;
57240afd8cSMark Johnston } zfs_zap_entry_t;
58240afd8cSMark Johnston 
59240afd8cSMark Johnston struct zfs_zap {
60240afd8cSMark Johnston 	STAILQ_HEAD(, zfs_zap_entry) kvps;
61240afd8cSMark Johnston 	uint64_t	hashsalt;	/* key hash input */
62240afd8cSMark Johnston 	unsigned long	kvpcnt;		/* number of key-value pairs */
63240afd8cSMark Johnston 	unsigned long	chunks;		/* count of chunks needed for fat ZAP */
64240afd8cSMark Johnston 	bool		micro;		/* can this be a micro ZAP? */
65240afd8cSMark Johnston 
66240afd8cSMark Johnston 	dnode_phys_t	*dnode;		/* backpointer */
67240afd8cSMark Johnston 	zfs_objset_t	*os;		/* backpointer */
68240afd8cSMark Johnston };
69240afd8cSMark Johnston 
70240afd8cSMark Johnston static uint16_t
71240afd8cSMark Johnston zap_entry_chunks(zfs_zap_entry_t *ent)
72240afd8cSMark Johnston {
73240afd8cSMark Johnston 	return (1 + howmany(strlen(ent->name) + 1, ZAP_LEAF_ARRAY_BYTES) +
74240afd8cSMark Johnston 	    howmany(ent->intsz * ent->intcnt, ZAP_LEAF_ARRAY_BYTES));
75240afd8cSMark Johnston }
76240afd8cSMark Johnston 
77240afd8cSMark Johnston static uint64_t
78240afd8cSMark Johnston zap_hash(uint64_t salt, const char *name)
79240afd8cSMark Johnston {
80240afd8cSMark Johnston 	static uint64_t crc64_table[256];
81240afd8cSMark Johnston 	const uint64_t crc64_poly = 0xC96C5795D7870F42UL;
82240afd8cSMark Johnston 	const uint8_t *cp;
83240afd8cSMark Johnston 	uint64_t crc;
84240afd8cSMark Johnston 	uint8_t c;
85240afd8cSMark Johnston 
86240afd8cSMark Johnston 	assert(salt != 0);
87240afd8cSMark Johnston 	if (crc64_table[128] == 0) {
88240afd8cSMark Johnston 		for (int i = 0; i < 256; i++) {
89240afd8cSMark Johnston 			uint64_t *t;
90240afd8cSMark Johnston 
91240afd8cSMark Johnston 			t = crc64_table + i;
92240afd8cSMark Johnston 			*t = i;
93240afd8cSMark Johnston 			for (int j = 8; j > 0; j--)
94240afd8cSMark Johnston 				*t = (*t >> 1) ^ (-(*t & 1) & crc64_poly);
95240afd8cSMark Johnston 		}
96240afd8cSMark Johnston 	}
97240afd8cSMark Johnston 	assert(crc64_table[128] == crc64_poly);
98240afd8cSMark Johnston 
99240afd8cSMark Johnston 	for (cp = (const uint8_t *)name, crc = salt; (c = *cp) != '\0'; cp++)
100240afd8cSMark Johnston 		crc = (crc >> 8) ^ crc64_table[(crc ^ c) & 0xFF];
101240afd8cSMark Johnston 
102240afd8cSMark Johnston 	/*
103240afd8cSMark Johnston 	 * Only use 28 bits, since we need 4 bits in the cookie for the
104240afd8cSMark Johnston 	 * collision differentiator.  We MUST use the high bits, since
105240afd8cSMark Johnston 	 * those are the ones that we first pay attention to when
106240afd8cSMark Johnston 	 * choosing the bucket.
107240afd8cSMark Johnston 	 */
108240afd8cSMark Johnston 	crc &= ~((1ULL << (64 - ZAP_HASHBITS)) - 1);
109240afd8cSMark Johnston 
110240afd8cSMark Johnston 	return (crc);
111240afd8cSMark Johnston }
112240afd8cSMark Johnston 
113240afd8cSMark Johnston zfs_zap_t *
114240afd8cSMark Johnston zap_alloc(zfs_objset_t *os, dnode_phys_t *dnode)
115240afd8cSMark Johnston {
116240afd8cSMark Johnston 	zfs_zap_t *zap;
117240afd8cSMark Johnston 
118240afd8cSMark Johnston 	zap = ecalloc(1, sizeof(*zap));
119240afd8cSMark Johnston 	STAILQ_INIT(&zap->kvps);
120240afd8cSMark Johnston 	zap->hashsalt = ((uint64_t)random() << 32) | random();
121240afd8cSMark Johnston 	zap->micro = true;
122240afd8cSMark Johnston 	zap->kvpcnt = 0;
123240afd8cSMark Johnston 	zap->chunks = 0;
124240afd8cSMark Johnston 	zap->dnode = dnode;
125240afd8cSMark Johnston 	zap->os = os;
126240afd8cSMark Johnston 	return (zap);
127240afd8cSMark Johnston }
128240afd8cSMark Johnston 
129240afd8cSMark Johnston void
130240afd8cSMark Johnston zap_add(zfs_zap_t *zap, const char *name, size_t intsz, size_t intcnt,
131240afd8cSMark Johnston     const uint8_t *val)
132240afd8cSMark Johnston {
133240afd8cSMark Johnston 	zfs_zap_entry_t *ent;
134240afd8cSMark Johnston 
135240afd8cSMark Johnston 	assert(intsz == 1 || intsz == 2 || intsz == 4 || intsz == 8);
136240afd8cSMark Johnston 	assert(strlen(name) + 1 <= ZAP_MAXNAMELEN);
137240afd8cSMark Johnston 	assert(intcnt <= ZAP_MAXVALUELEN && intcnt * intsz <= ZAP_MAXVALUELEN);
138240afd8cSMark Johnston 
139240afd8cSMark Johnston 	ent = ecalloc(1, sizeof(*ent));
140240afd8cSMark Johnston 	ent->name = estrdup(name);
141240afd8cSMark Johnston 	ent->hash = zap_hash(zap->hashsalt, ent->name);
142240afd8cSMark Johnston 	ent->intsz = intsz;
143240afd8cSMark Johnston 	ent->intcnt = intcnt;
144240afd8cSMark Johnston 	if (intsz == sizeof(uint64_t) && intcnt == 1) {
145240afd8cSMark Johnston 		/*
146240afd8cSMark Johnston 		 * Micro-optimization to elide a memory allocation in that most
147240afd8cSMark Johnston 		 * common case where this is a directory entry.
148240afd8cSMark Johnston 		 */
149240afd8cSMark Johnston 		ent->val64p = &ent->val64;
150240afd8cSMark Johnston 	} else {
151240afd8cSMark Johnston 		ent->valp = ecalloc(intcnt, intsz);
152240afd8cSMark Johnston 	}
153240afd8cSMark Johnston 	memcpy(ent->valp, val, intcnt * intsz);
154240afd8cSMark Johnston 	zap->kvpcnt++;
155240afd8cSMark Johnston 	zap->chunks += zap_entry_chunks(ent);
156240afd8cSMark Johnston 	STAILQ_INSERT_TAIL(&zap->kvps, ent, next);
157240afd8cSMark Johnston 
158240afd8cSMark Johnston 	if (zap->micro && (intcnt != 1 || intsz != sizeof(uint64_t) ||
159240afd8cSMark Johnston 	    strlen(name) + 1 > MZAP_NAME_LEN || zap->kvpcnt > MZAP_ENT_MAX))
160240afd8cSMark Johnston 		zap->micro = false;
161240afd8cSMark Johnston }
162240afd8cSMark Johnston 
163240afd8cSMark Johnston void
164240afd8cSMark Johnston zap_add_uint64(zfs_zap_t *zap, const char *name, uint64_t val)
165240afd8cSMark Johnston {
166240afd8cSMark Johnston 	zap_add(zap, name, sizeof(uint64_t), 1, (uint8_t *)&val);
167240afd8cSMark Johnston }
168240afd8cSMark Johnston 
169240afd8cSMark Johnston void
170240afd8cSMark Johnston zap_add_string(zfs_zap_t *zap, const char *name, const char *val)
171240afd8cSMark Johnston {
172240afd8cSMark Johnston 	zap_add(zap, name, 1, strlen(val) + 1, val);
173240afd8cSMark Johnston }
174240afd8cSMark Johnston 
175240afd8cSMark Johnston bool
176240afd8cSMark Johnston zap_entry_exists(zfs_zap_t *zap, const char *name)
177240afd8cSMark Johnston {
178240afd8cSMark Johnston 	zfs_zap_entry_t *ent;
179240afd8cSMark Johnston 
180240afd8cSMark Johnston 	STAILQ_FOREACH(ent, &zap->kvps, next) {
181240afd8cSMark Johnston 		if (strcmp(ent->name, name) == 0)
182240afd8cSMark Johnston 			return (true);
183240afd8cSMark Johnston 	}
184240afd8cSMark Johnston 	return (false);
185240afd8cSMark Johnston }
186240afd8cSMark Johnston 
187240afd8cSMark Johnston static void
188240afd8cSMark Johnston zap_micro_write(zfs_opt_t *zfs, zfs_zap_t *zap)
189240afd8cSMark Johnston {
190240afd8cSMark Johnston 	dnode_phys_t *dnode;
191240afd8cSMark Johnston 	zfs_zap_entry_t *ent;
192240afd8cSMark Johnston 	mzap_phys_t *mzap;
193240afd8cSMark Johnston 	mzap_ent_phys_t *ment;
194240afd8cSMark Johnston 	off_t bytes, loc;
195240afd8cSMark Johnston 
196240afd8cSMark Johnston 	memset(zfs->filebuf, 0, sizeof(zfs->filebuf));
197240afd8cSMark Johnston 	mzap = (mzap_phys_t *)&zfs->filebuf[0];
198240afd8cSMark Johnston 	mzap->mz_block_type = ZBT_MICRO;
199240afd8cSMark Johnston 	mzap->mz_salt = zap->hashsalt;
200240afd8cSMark Johnston 	mzap->mz_normflags = 0;
201240afd8cSMark Johnston 
202240afd8cSMark Johnston 	bytes = sizeof(*mzap) + (zap->kvpcnt - 1) * sizeof(*ment);
203240afd8cSMark Johnston 	assert(bytes <= (off_t)MZAP_MAX_BLKSZ);
204240afd8cSMark Johnston 
205240afd8cSMark Johnston 	ment = &mzap->mz_chunk[0];
206240afd8cSMark Johnston 	STAILQ_FOREACH(ent, &zap->kvps, next) {
207240afd8cSMark Johnston 		memcpy(&ment->mze_value, ent->valp, ent->intsz * ent->intcnt);
208240afd8cSMark Johnston 		ment->mze_cd = 0; /* XXX-MJ */
209240afd8cSMark Johnston 		strlcpy(ment->mze_name, ent->name, sizeof(ment->mze_name));
210240afd8cSMark Johnston 		ment++;
211240afd8cSMark Johnston 	}
212240afd8cSMark Johnston 
213240afd8cSMark Johnston 	loc = objset_space_alloc(zfs, zap->os, &bytes);
214240afd8cSMark Johnston 
215240afd8cSMark Johnston 	dnode = zap->dnode;
216240afd8cSMark Johnston 	dnode->dn_maxblkid = 0;
217240afd8cSMark Johnston 	dnode->dn_datablkszsec = bytes >> MINBLOCKSHIFT;
218240afd8cSMark Johnston 	dnode->dn_flags = DNODE_FLAG_USED_BYTES;
219240afd8cSMark Johnston 
220240afd8cSMark Johnston 	vdev_pwrite_dnode_data(zfs, dnode, zfs->filebuf, bytes, loc);
221240afd8cSMark Johnston }
222240afd8cSMark Johnston 
223240afd8cSMark Johnston /*
224240afd8cSMark Johnston  * Write some data to the fat ZAP leaf chunk starting at index "li".
225240afd8cSMark Johnston  *
226240afd8cSMark Johnston  * Note that individual integers in the value may be split among consecutive
227240afd8cSMark Johnston  * leaves.
228240afd8cSMark Johnston  */
229240afd8cSMark Johnston static void
230240afd8cSMark Johnston zap_fat_write_array_chunk(zap_leaf_t *l, uint16_t li, size_t sz,
231240afd8cSMark Johnston     const uint8_t *val)
232240afd8cSMark Johnston {
233240afd8cSMark Johnston 	struct zap_leaf_array *la;
234240afd8cSMark Johnston 
235240afd8cSMark Johnston 	assert(sz <= ZAP_MAXVALUELEN);
236240afd8cSMark Johnston 
237240afd8cSMark Johnston 	for (uint16_t n, resid = sz; resid > 0; resid -= n, val += n, li++) {
238240afd8cSMark Johnston 		n = MIN(resid, ZAP_LEAF_ARRAY_BYTES);
239240afd8cSMark Johnston 
240240afd8cSMark Johnston 		la = &ZAP_LEAF_CHUNK(l, li).l_array;
241240afd8cSMark Johnston 		assert(la->la_type == ZAP_CHUNK_FREE);
242240afd8cSMark Johnston 		la->la_type = ZAP_CHUNK_ARRAY;
243240afd8cSMark Johnston 		memcpy(la->la_array, val, n);
244240afd8cSMark Johnston 		la->la_next = li + 1;
245240afd8cSMark Johnston 	}
246240afd8cSMark Johnston 	la->la_next = 0xffff;
247240afd8cSMark Johnston }
248240afd8cSMark Johnston 
249240afd8cSMark Johnston /*
250240afd8cSMark Johnston  * Find the shortest hash prefix length which lets us distribute keys without
251240afd8cSMark Johnston  * overflowing a leaf block.  This is not (space) optimal, but is simple, and
252240afd8cSMark Johnston  * directories large enough to overflow a single 128KB leaf block are uncommon.
253240afd8cSMark Johnston  */
254240afd8cSMark Johnston static unsigned int
255240afd8cSMark Johnston zap_fat_write_prefixlen(zfs_zap_t *zap, zap_leaf_t *l)
256240afd8cSMark Johnston {
257240afd8cSMark Johnston 	zfs_zap_entry_t *ent;
258240afd8cSMark Johnston 	unsigned int prefixlen;
259240afd8cSMark Johnston 
260240afd8cSMark Johnston 	if (zap->chunks <= ZAP_LEAF_NUMCHUNKS(l)) {
261240afd8cSMark Johnston 		/*
262240afd8cSMark Johnston 		 * All chunks will fit in a single leaf block.
263240afd8cSMark Johnston 		 */
264240afd8cSMark Johnston 		return (0);
265240afd8cSMark Johnston 	}
266240afd8cSMark Johnston 
267240afd8cSMark Johnston 	for (prefixlen = 1; prefixlen < (unsigned int)l->l_bs; prefixlen++) {
268240afd8cSMark Johnston 		uint32_t *leafchunks;
269240afd8cSMark Johnston 
270240afd8cSMark Johnston 		leafchunks = ecalloc(1u << prefixlen, sizeof(*leafchunks));
271240afd8cSMark Johnston 		STAILQ_FOREACH(ent, &zap->kvps, next) {
272240afd8cSMark Johnston 			uint64_t li;
273240afd8cSMark Johnston 			uint16_t chunks;
274240afd8cSMark Johnston 
275240afd8cSMark Johnston 			li = ZAP_HASH_IDX(ent->hash, prefixlen);
276240afd8cSMark Johnston 
277240afd8cSMark Johnston 			chunks = zap_entry_chunks(ent);
278240afd8cSMark Johnston 			if (ZAP_LEAF_NUMCHUNKS(l) - leafchunks[li] < chunks) {
279240afd8cSMark Johnston 				/*
280240afd8cSMark Johnston 				 * Not enough space, grow the prefix and retry.
281240afd8cSMark Johnston 				 */
282240afd8cSMark Johnston 				break;
283240afd8cSMark Johnston 			}
284240afd8cSMark Johnston 			leafchunks[li] += chunks;
285240afd8cSMark Johnston 		}
286240afd8cSMark Johnston 		free(leafchunks);
287240afd8cSMark Johnston 
288240afd8cSMark Johnston 		if (ent == NULL) {
289240afd8cSMark Johnston 			/*
290240afd8cSMark Johnston 			 * Everything fits, we're done.
291240afd8cSMark Johnston 			 */
292240afd8cSMark Johnston 			break;
293240afd8cSMark Johnston 		}
294240afd8cSMark Johnston 	}
295240afd8cSMark Johnston 
296240afd8cSMark Johnston 	/*
297240afd8cSMark Johnston 	 * If this fails, then we need to expand the pointer table.  For now
298240afd8cSMark Johnston 	 * this situation is unhandled since it is hard to trigger.
299240afd8cSMark Johnston 	 */
300240afd8cSMark Johnston 	assert(prefixlen < (unsigned int)l->l_bs);
301240afd8cSMark Johnston 
302240afd8cSMark Johnston 	return (prefixlen);
303240afd8cSMark Johnston }
304240afd8cSMark Johnston 
305240afd8cSMark Johnston /*
306240afd8cSMark Johnston  * Initialize a fat ZAP leaf block.
307240afd8cSMark Johnston  */
308240afd8cSMark Johnston static void
309240afd8cSMark Johnston zap_fat_write_leaf_init(zap_leaf_t *l, uint64_t prefix, int prefixlen)
310240afd8cSMark Johnston {
311240afd8cSMark Johnston 	zap_leaf_phys_t *leaf;
312240afd8cSMark Johnston 
313240afd8cSMark Johnston 	leaf = l->l_phys;
314240afd8cSMark Johnston 
315240afd8cSMark Johnston 	leaf->l_hdr.lh_block_type = ZBT_LEAF;
316240afd8cSMark Johnston 	leaf->l_hdr.lh_magic = ZAP_LEAF_MAGIC;
317240afd8cSMark Johnston 	leaf->l_hdr.lh_nfree = ZAP_LEAF_NUMCHUNKS(l);
318240afd8cSMark Johnston 	leaf->l_hdr.lh_prefix = prefix;
319240afd8cSMark Johnston 	leaf->l_hdr.lh_prefix_len = prefixlen;
320240afd8cSMark Johnston 
321240afd8cSMark Johnston 	/* Initialize the leaf hash table. */
322240afd8cSMark Johnston 	assert(leaf->l_hdr.lh_nfree < 0xffff);
323240afd8cSMark Johnston 	memset(leaf->l_hash, 0xff,
324240afd8cSMark Johnston 	    ZAP_LEAF_HASH_NUMENTRIES(l) * sizeof(*leaf->l_hash));
325240afd8cSMark Johnston 
326240afd8cSMark Johnston 	/* Initialize the leaf chunks. */
327240afd8cSMark Johnston 	for (uint16_t i = 0; i < ZAP_LEAF_NUMCHUNKS(l); i++) {
328240afd8cSMark Johnston 		struct zap_leaf_free *lf;
329240afd8cSMark Johnston 
330240afd8cSMark Johnston 		lf = &ZAP_LEAF_CHUNK(l, i).l_free;
331240afd8cSMark Johnston 		lf->lf_type = ZAP_CHUNK_FREE;
332240afd8cSMark Johnston 		if (i + 1 == ZAP_LEAF_NUMCHUNKS(l))
333240afd8cSMark Johnston 			lf->lf_next = 0xffff;
334240afd8cSMark Johnston 		else
335240afd8cSMark Johnston 			lf->lf_next = i + 1;
336240afd8cSMark Johnston 	}
337240afd8cSMark Johnston }
338240afd8cSMark Johnston 
339240afd8cSMark Johnston static void
340240afd8cSMark Johnston zap_fat_write(zfs_opt_t *zfs, zfs_zap_t *zap)
341240afd8cSMark Johnston {
342240afd8cSMark Johnston 	struct dnode_cursor *c;
343240afd8cSMark Johnston 	zap_leaf_t l;
344240afd8cSMark Johnston 	zap_phys_t *zaphdr;
345240afd8cSMark Johnston 	struct zap_table_phys *zt;
346240afd8cSMark Johnston 	zfs_zap_entry_t *ent;
347240afd8cSMark Johnston 	dnode_phys_t *dnode;
348240afd8cSMark Johnston 	uint8_t *leafblks;
349240afd8cSMark Johnston 	uint64_t lblkcnt, *ptrhasht;
350240afd8cSMark Johnston 	off_t loc, blksz;
351240afd8cSMark Johnston 	size_t blkshift;
352240afd8cSMark Johnston 	unsigned int prefixlen;
353240afd8cSMark Johnston 	int ptrcnt;
354240afd8cSMark Johnston 
355240afd8cSMark Johnston 	/*
356240afd8cSMark Johnston 	 * For simplicity, always use the largest block size.  This should be ok
357240afd8cSMark Johnston 	 * since most directories will be micro ZAPs, but it's space inefficient
358240afd8cSMark Johnston 	 * for small ZAPs and might need to be revisited.
359240afd8cSMark Johnston 	 */
360240afd8cSMark Johnston 	blkshift = MAXBLOCKSHIFT;
361240afd8cSMark Johnston 	blksz = (off_t)1 << blkshift;
362240afd8cSMark Johnston 
363240afd8cSMark Johnston 	/*
364240afd8cSMark Johnston 	 * Embedded pointer tables give up to 8192 entries.  This ought to be
365240afd8cSMark Johnston 	 * enough for anything except massive directories.
366240afd8cSMark Johnston 	 */
367240afd8cSMark Johnston 	ptrcnt = (blksz / 2) / sizeof(uint64_t);
368240afd8cSMark Johnston 
369240afd8cSMark Johnston 	memset(zfs->filebuf, 0, sizeof(zfs->filebuf));
370240afd8cSMark Johnston 	zaphdr = (zap_phys_t *)&zfs->filebuf[0];
371240afd8cSMark Johnston 	zaphdr->zap_block_type = ZBT_HEADER;
372240afd8cSMark Johnston 	zaphdr->zap_magic = ZAP_MAGIC;
373240afd8cSMark Johnston 	zaphdr->zap_num_entries = zap->kvpcnt;
374240afd8cSMark Johnston 	zaphdr->zap_salt = zap->hashsalt;
375240afd8cSMark Johnston 
376240afd8cSMark Johnston 	l.l_bs = blkshift;
377240afd8cSMark Johnston 	l.l_phys = NULL;
378240afd8cSMark Johnston 
379240afd8cSMark Johnston 	zt = &zaphdr->zap_ptrtbl;
380240afd8cSMark Johnston 	zt->zt_blk = 0;
381240afd8cSMark Johnston 	zt->zt_numblks = 0;
382240afd8cSMark Johnston 	zt->zt_shift = flsll(ptrcnt) - 1;
383240afd8cSMark Johnston 	zt->zt_nextblk = 0;
384240afd8cSMark Johnston 	zt->zt_blks_copied = 0;
385240afd8cSMark Johnston 
386240afd8cSMark Johnston 	/*
387240afd8cSMark Johnston 	 * How many leaf blocks do we need?  Initialize them and update the
388240afd8cSMark Johnston 	 * header.
389240afd8cSMark Johnston 	 */
390240afd8cSMark Johnston 	prefixlen = zap_fat_write_prefixlen(zap, &l);
391240afd8cSMark Johnston 	lblkcnt = 1 << prefixlen;
392240afd8cSMark Johnston 	leafblks = ecalloc(lblkcnt, blksz);
393240afd8cSMark Johnston 	for (unsigned int li = 0; li < lblkcnt; li++) {
394240afd8cSMark Johnston 		l.l_phys = (zap_leaf_phys_t *)(leafblks + li * blksz);
395240afd8cSMark Johnston 		zap_fat_write_leaf_init(&l, li, prefixlen);
396240afd8cSMark Johnston 	}
397240afd8cSMark Johnston 	zaphdr->zap_num_leafs = lblkcnt;
398240afd8cSMark Johnston 	zaphdr->zap_freeblk = lblkcnt + 1;
399240afd8cSMark Johnston 
400240afd8cSMark Johnston 	/*
401240afd8cSMark Johnston 	 * For each entry, figure out which leaf block it belongs to based on
402240afd8cSMark Johnston 	 * the upper bits of its hash, allocate chunks from that leaf, and fill
403240afd8cSMark Johnston 	 * them out.
404240afd8cSMark Johnston 	 */
405240afd8cSMark Johnston 	ptrhasht = (uint64_t *)(&zfs->filebuf[0] + blksz / 2);
406240afd8cSMark Johnston 	STAILQ_FOREACH(ent, &zap->kvps, next) {
407240afd8cSMark Johnston 		struct zap_leaf_entry *le;
408240afd8cSMark Johnston 		uint16_t *lptr;
409240afd8cSMark Johnston 		uint64_t hi, li;
410240afd8cSMark Johnston 		uint16_t namelen, nchunks, nnamechunks, nvalchunks;
411240afd8cSMark Johnston 
412240afd8cSMark Johnston 		hi = ZAP_HASH_IDX(ent->hash, zt->zt_shift);
413240afd8cSMark Johnston 		li = ZAP_HASH_IDX(ent->hash, prefixlen);
414240afd8cSMark Johnston 		assert(ptrhasht[hi] == 0 || ptrhasht[hi] == li + 1);
415240afd8cSMark Johnston 		ptrhasht[hi] = li + 1;
416240afd8cSMark Johnston 		l.l_phys = (zap_leaf_phys_t *)(leafblks + li * blksz);
417240afd8cSMark Johnston 
418240afd8cSMark Johnston 		namelen = strlen(ent->name) + 1;
419240afd8cSMark Johnston 
420240afd8cSMark Johnston 		/*
421240afd8cSMark Johnston 		 * How many leaf chunks do we need for this entry?
422240afd8cSMark Johnston 		 */
423240afd8cSMark Johnston 		nnamechunks = howmany(namelen, ZAP_LEAF_ARRAY_BYTES);
424240afd8cSMark Johnston 		nvalchunks = howmany(ent->intcnt,
425240afd8cSMark Johnston 		    ZAP_LEAF_ARRAY_BYTES / ent->intsz);
426240afd8cSMark Johnston 		nchunks = 1 + nnamechunks + nvalchunks;
427240afd8cSMark Johnston 
428240afd8cSMark Johnston 		/*
429240afd8cSMark Johnston 		 * Allocate a run of free leaf chunks for this entry,
430240afd8cSMark Johnston 		 * potentially extending a hash chain.
431240afd8cSMark Johnston 		 */
432240afd8cSMark Johnston 		assert(l.l_phys->l_hdr.lh_nfree >= nchunks);
433240afd8cSMark Johnston 		l.l_phys->l_hdr.lh_nfree -= nchunks;
434240afd8cSMark Johnston 		l.l_phys->l_hdr.lh_nentries++;
435240afd8cSMark Johnston 		lptr = ZAP_LEAF_HASH_ENTPTR(&l, ent->hash);
436240afd8cSMark Johnston 		while (*lptr != 0xffff) {
437240afd8cSMark Johnston 			assert(*lptr < ZAP_LEAF_NUMCHUNKS(&l));
438240afd8cSMark Johnston 			le = ZAP_LEAF_ENTRY(&l, *lptr);
439240afd8cSMark Johnston 			assert(le->le_type == ZAP_CHUNK_ENTRY);
440240afd8cSMark Johnston 			le->le_cd++;
441240afd8cSMark Johnston 			lptr = &le->le_next;
442240afd8cSMark Johnston 		}
443240afd8cSMark Johnston 		*lptr = l.l_phys->l_hdr.lh_freelist;
444240afd8cSMark Johnston 		l.l_phys->l_hdr.lh_freelist += nchunks;
445240afd8cSMark Johnston 		assert(l.l_phys->l_hdr.lh_freelist <=
446240afd8cSMark Johnston 		    ZAP_LEAF_NUMCHUNKS(&l));
447240afd8cSMark Johnston 		if (l.l_phys->l_hdr.lh_freelist ==
448240afd8cSMark Johnston 		    ZAP_LEAF_NUMCHUNKS(&l))
449240afd8cSMark Johnston 			l.l_phys->l_hdr.lh_freelist = 0xffff;
450240afd8cSMark Johnston 
451240afd8cSMark Johnston 		/*
452240afd8cSMark Johnston 		 * Integer values must be stored in big-endian format.
453240afd8cSMark Johnston 		 */
454240afd8cSMark Johnston 		switch (ent->intsz) {
455240afd8cSMark Johnston 		case 1:
456240afd8cSMark Johnston 			break;
457240afd8cSMark Johnston 		case 2:
458240afd8cSMark Johnston 			for (uint16_t *v = ent->val16p;
459240afd8cSMark Johnston 			    v - ent->val16p < (ptrdiff_t)ent->intcnt;
460240afd8cSMark Johnston 			    v++)
461240afd8cSMark Johnston 				*v = htobe16(*v);
462240afd8cSMark Johnston 			break;
463240afd8cSMark Johnston 		case 4:
464240afd8cSMark Johnston 			for (uint32_t *v = ent->val32p;
465240afd8cSMark Johnston 			    v - ent->val32p < (ptrdiff_t)ent->intcnt;
466240afd8cSMark Johnston 			    v++)
467240afd8cSMark Johnston 				*v = htobe32(*v);
468240afd8cSMark Johnston 			break;
469240afd8cSMark Johnston 		case 8:
470240afd8cSMark Johnston 			for (uint64_t *v = ent->val64p;
471240afd8cSMark Johnston 			    v - ent->val64p < (ptrdiff_t)ent->intcnt;
472240afd8cSMark Johnston 			    v++)
473240afd8cSMark Johnston 				*v = htobe64(*v);
474240afd8cSMark Johnston 			break;
475240afd8cSMark Johnston 		default:
476240afd8cSMark Johnston 			assert(0);
477240afd8cSMark Johnston 		}
478240afd8cSMark Johnston 
479240afd8cSMark Johnston 		/*
480240afd8cSMark Johnston 		 * Finally, write out the leaf chunks for this entry.
481240afd8cSMark Johnston 		 */
482240afd8cSMark Johnston 		le = ZAP_LEAF_ENTRY(&l, *lptr);
483240afd8cSMark Johnston 		assert(le->le_type == ZAP_CHUNK_FREE);
484240afd8cSMark Johnston 		le->le_type = ZAP_CHUNK_ENTRY;
485240afd8cSMark Johnston 		le->le_next = 0xffff;
486240afd8cSMark Johnston 		le->le_name_chunk = *lptr + 1;
487240afd8cSMark Johnston 		le->le_name_numints = namelen;
488240afd8cSMark Johnston 		le->le_value_chunk = *lptr + 1 + nnamechunks;
489240afd8cSMark Johnston 		le->le_value_intlen = ent->intsz;
490240afd8cSMark Johnston 		le->le_value_numints = ent->intcnt;
491240afd8cSMark Johnston 		le->le_hash = ent->hash;
492240afd8cSMark Johnston 		zap_fat_write_array_chunk(&l, *lptr + 1, namelen, ent->name);
493240afd8cSMark Johnston 		zap_fat_write_array_chunk(&l, *lptr + 1 + nnamechunks,
494240afd8cSMark Johnston 		    ent->intcnt * ent->intsz, ent->valp);
495240afd8cSMark Johnston 	}
496240afd8cSMark Johnston 
497240afd8cSMark Johnston 	/*
498240afd8cSMark Johnston 	 * Initialize unused slots of the pointer table.
499240afd8cSMark Johnston 	 */
500240afd8cSMark Johnston 	for (int i = 0; i < ptrcnt; i++)
501240afd8cSMark Johnston 		if (ptrhasht[i] == 0)
502240afd8cSMark Johnston 			ptrhasht[i] = (i >> (zt->zt_shift - prefixlen)) + 1;
503240afd8cSMark Johnston 
504240afd8cSMark Johnston 	/*
505240afd8cSMark Johnston 	 * Write the whole thing to disk.
506240afd8cSMark Johnston 	 */
507240afd8cSMark Johnston 	dnode = zap->dnode;
508240afd8cSMark Johnston 	dnode->dn_nblkptr = 1;
509240afd8cSMark Johnston 	dnode->dn_datablkszsec = blksz >> MINBLOCKSHIFT;
510240afd8cSMark Johnston 	dnode->dn_maxblkid = lblkcnt + 1;
511240afd8cSMark Johnston 	dnode->dn_flags = DNODE_FLAG_USED_BYTES;
512240afd8cSMark Johnston 
513240afd8cSMark Johnston 	c = dnode_cursor_init(zfs, zap->os, zap->dnode,
514240afd8cSMark Johnston 	    (lblkcnt + 1) * blksz, blksz);
515240afd8cSMark Johnston 
516240afd8cSMark Johnston 	loc = objset_space_alloc(zfs, zap->os, &blksz);
517240afd8cSMark Johnston 	vdev_pwrite_dnode_indir(zfs, dnode, 0, 1, zfs->filebuf, blksz, loc,
518240afd8cSMark Johnston 	    dnode_cursor_next(zfs, c, 0));
519240afd8cSMark Johnston 
520240afd8cSMark Johnston 	for (uint64_t i = 0; i < lblkcnt; i++) {
521240afd8cSMark Johnston 		loc = objset_space_alloc(zfs, zap->os, &blksz);
522240afd8cSMark Johnston 		vdev_pwrite_dnode_indir(zfs, dnode, 0, 1, leafblks + i * blksz,
523240afd8cSMark Johnston 		    blksz, loc, dnode_cursor_next(zfs, c, (i + 1) * blksz));
524240afd8cSMark Johnston 	}
525240afd8cSMark Johnston 
526240afd8cSMark Johnston 	dnode_cursor_finish(zfs, c);
527240afd8cSMark Johnston 
528240afd8cSMark Johnston 	free(leafblks);
529240afd8cSMark Johnston }
530240afd8cSMark Johnston 
531240afd8cSMark Johnston void
532240afd8cSMark Johnston zap_write(zfs_opt_t *zfs, zfs_zap_t *zap)
533240afd8cSMark Johnston {
534240afd8cSMark Johnston 	zfs_zap_entry_t *ent;
535240afd8cSMark Johnston 
536240afd8cSMark Johnston 	if (zap->micro) {
537240afd8cSMark Johnston 		zap_micro_write(zfs, zap);
538240afd8cSMark Johnston 	} else {
539240afd8cSMark Johnston 		assert(!STAILQ_EMPTY(&zap->kvps));
540240afd8cSMark Johnston 		assert(zap->kvpcnt > 0);
541240afd8cSMark Johnston 		zap_fat_write(zfs, zap);
542240afd8cSMark Johnston 	}
543240afd8cSMark Johnston 
544240afd8cSMark Johnston 	while ((ent = STAILQ_FIRST(&zap->kvps)) != NULL) {
545240afd8cSMark Johnston 		STAILQ_REMOVE_HEAD(&zap->kvps, next);
546240afd8cSMark Johnston 		if (ent->val64p != &ent->val64)
547240afd8cSMark Johnston 			free(ent->valp);
548240afd8cSMark Johnston 		free(ent->name);
549240afd8cSMark Johnston 		free(ent);
550240afd8cSMark Johnston 	}
551240afd8cSMark Johnston 	free(zap);
552240afd8cSMark Johnston }
553