xref: /freebsd/usr.sbin/makefs/zfs/objset.c (revision 3a3af6b2a160bea72509a9d5ef84e25906b0478a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2022 The FreeBSD Foundation
5  *
6  * This software was developed by Mark Johnston under sponsorship from
7  * the FreeBSD Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are
11  * met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <assert.h>
32 #include <string.h>
33 
34 #include <util.h>
35 
36 #include "zfs.h"
37 
38 #define	DNODES_PER_CHUNK	(MAXBLOCKSIZE / sizeof(dnode_phys_t))
39 
40 struct objset_dnode_chunk {
41 	dnode_phys_t	buf[DNODES_PER_CHUNK];
42 	unsigned int	nextfree;
43 	STAILQ_ENTRY(objset_dnode_chunk) next;
44 };
45 
46 typedef struct zfs_objset {
47 	/* Physical object set. */
48 	objset_phys_t	*phys;
49 	off_t		osloc;
50 	off_t		osblksz;
51 	blkptr_t	osbp;		/* set in objset_write() */
52 
53 	/* Accounting. */
54 	off_t		space;		/* bytes allocated to this objset */
55 
56 	/* dnode allocator. */
57 	uint64_t	dnodecount;
58 	STAILQ_HEAD(, objset_dnode_chunk) dnodechunks;
59 } zfs_objset_t;
60 
61 static void
62 dnode_init(dnode_phys_t *dnode, uint8_t type, uint8_t bonustype,
63     uint16_t bonuslen)
64 {
65 	dnode->dn_indblkshift = MAXBLOCKSHIFT;
66 	dnode->dn_type = type;
67 	dnode->dn_bonustype = bonustype;
68 	dnode->dn_bonuslen = bonuslen;
69 	dnode->dn_checksum = ZIO_CHECKSUM_FLETCHER_4;
70 	dnode->dn_nlevels = 1;
71 	dnode->dn_nblkptr = 1;
72 	dnode->dn_flags = DNODE_FLAG_USED_BYTES;
73 }
74 
75 zfs_objset_t *
76 objset_alloc(zfs_opt_t *zfs, uint64_t type)
77 {
78 	struct objset_dnode_chunk *chunk;
79 	zfs_objset_t *os;
80 
81 	os = ecalloc(1, sizeof(*os));
82 	os->osblksz = sizeof(objset_phys_t);
83 	os->osloc = objset_space_alloc(zfs, os, &os->osblksz);
84 
85 	/*
86 	 * Object ID zero is always reserved for the meta dnode, which is
87 	 * embedded in the objset itself.
88 	 */
89 	STAILQ_INIT(&os->dnodechunks);
90 	chunk = ecalloc(1, sizeof(*chunk));
91 	chunk->nextfree = 1;
92 	STAILQ_INSERT_HEAD(&os->dnodechunks, chunk, next);
93 	os->dnodecount = 1;
94 
95 	os->phys = ecalloc(1, os->osblksz);
96 	os->phys->os_type = type;
97 
98 	dnode_init(&os->phys->os_meta_dnode, DMU_OT_DNODE, DMU_OT_NONE, 0);
99 	os->phys->os_meta_dnode.dn_datablkszsec =
100 	    DNODE_BLOCK_SIZE >> MINBLOCKSHIFT;
101 
102 	return (os);
103 }
104 
105 /*
106  * Write the dnode array and physical object set to disk.
107  */
108 static void
109 _objset_write(zfs_opt_t *zfs, zfs_objset_t *os, struct dnode_cursor *c,
110     off_t loc)
111 {
112 	struct objset_dnode_chunk *chunk, *tmp;
113 	unsigned int total;
114 
115 	/*
116 	 * Write out the dnode array, i.e., the meta-dnode.  For some reason its
117 	 * data blocks must be 16KB in size no matter how large the array is.
118 	 */
119 	total = 0;
120 	STAILQ_FOREACH_SAFE(chunk, &os->dnodechunks, next, tmp) {
121 		unsigned int i;
122 
123 		assert(chunk->nextfree <= os->dnodecount);
124 		assert(chunk->nextfree <= DNODES_PER_CHUNK);
125 
126 		for (i = 0; i < chunk->nextfree; i += DNODES_PER_BLOCK) {
127 			blkptr_t *bp;
128 			uint64_t fill;
129 
130 			if (chunk->nextfree - i < DNODES_PER_BLOCK)
131 				fill = DNODES_PER_BLOCK - (chunk->nextfree - i);
132 			else
133 				fill = 0;
134 			bp = dnode_cursor_next(zfs, c,
135 			    (total + i) * sizeof(dnode_phys_t));
136 			vdev_pwrite_dnode_indir(zfs, &os->phys->os_meta_dnode,
137 			    0, fill, chunk->buf + i, DNODE_BLOCK_SIZE, loc, bp);
138 			loc += DNODE_BLOCK_SIZE;
139 		}
140 		total += i;
141 
142 		free(chunk);
143 	}
144 	dnode_cursor_finish(zfs, c);
145 	STAILQ_INIT(&os->dnodechunks);
146 
147 	/*
148 	 * Write the object set itself.  The saved block pointer will be copied
149 	 * into the referencing DSL dataset or the uberblocks.
150 	 */
151 	vdev_pwrite_data(zfs, DMU_OT_OBJSET, ZIO_CHECKSUM_FLETCHER_4, 0, 1,
152 	    os->phys, os->osblksz, os->osloc, &os->osbp);
153 }
154 
155 void
156 objset_write(zfs_opt_t *zfs, zfs_objset_t *os)
157 {
158 	struct dnode_cursor *c;
159 	off_t dnodeloc, dnodesz;
160 	uint64_t dnodecount;
161 
162 	/*
163 	 * There is a chicken-and-egg problem here when writing the MOS: we
164 	 * cannot write space maps before we're finished allocating space from
165 	 * the vdev, and we can't write the MOS without having allocated space
166 	 * for indirect dnode blocks.  Thus, rather than lazily allocating
167 	 * indirect blocks for the meta-dnode (which would be simpler), they are
168 	 * allocated up-front and before writing space maps.
169 	 */
170 	dnodecount = os->dnodecount;
171 	if (os == zfs->mos)
172 		dnodecount += zfs->mscount;
173 	dnodesz = dnodecount * sizeof(dnode_phys_t);
174 	c = dnode_cursor_init(zfs, os, &os->phys->os_meta_dnode, dnodesz,
175 	    DNODE_BLOCK_SIZE);
176 	dnodesz = roundup2(dnodesz, DNODE_BLOCK_SIZE);
177 	dnodeloc = objset_space_alloc(zfs, os, &dnodesz);
178 
179 	if (os == zfs->mos) {
180 		vdev_spacemap_write(zfs);
181 
182 		/*
183 		 * We've finished allocating space, account for it in $MOS.
184 		 */
185 		dsl_dir_size_set(zfs->mosdsldir, os->space);
186 	}
187 	_objset_write(zfs, os, c, dnodeloc);
188 }
189 
190 dnode_phys_t *
191 objset_dnode_bonus_alloc(zfs_objset_t *os, uint8_t type, uint8_t bonustype,
192     uint16_t bonuslen, uint64_t *idp)
193 {
194 	struct objset_dnode_chunk *chunk;
195 	dnode_phys_t *dnode;
196 
197 	assert(bonuslen <= DN_OLD_MAX_BONUSLEN);
198 	assert(!STAILQ_EMPTY(&os->dnodechunks));
199 
200 	chunk = STAILQ_LAST(&os->dnodechunks, objset_dnode_chunk, next);
201 	if (chunk->nextfree == DNODES_PER_CHUNK) {
202 		chunk = ecalloc(1, sizeof(*chunk));
203 		STAILQ_INSERT_TAIL(&os->dnodechunks, chunk, next);
204 	}
205 	*idp = os->dnodecount++;
206 	dnode = &chunk->buf[chunk->nextfree++];
207 	dnode_init(dnode, type, bonustype, bonuslen);
208 	dnode->dn_datablkszsec = os->osblksz >> MINBLOCKSHIFT;
209 	return (dnode);
210 }
211 
212 dnode_phys_t *
213 objset_dnode_alloc(zfs_objset_t *os, uint8_t type, uint64_t *idp)
214 {
215 	return (objset_dnode_bonus_alloc(os, type, DMU_OT_NONE, 0, idp));
216 }
217 
218 /*
219  * Look up a physical dnode by ID.  This is not used often so a linear search is
220  * fine.
221  */
222 dnode_phys_t *
223 objset_dnode_lookup(zfs_objset_t *os, uint64_t id)
224 {
225 	struct objset_dnode_chunk *chunk;
226 
227 	assert(id > 0);
228 	assert(id < os->dnodecount);
229 
230 	STAILQ_FOREACH(chunk, &os->dnodechunks, next) {
231 		if (id < DNODES_PER_CHUNK)
232 			return (&chunk->buf[id]);
233 		id -= DNODES_PER_CHUNK;
234 	}
235 	assert(0);
236 	return (NULL);
237 }
238 
239 off_t
240 objset_space_alloc(zfs_opt_t *zfs, zfs_objset_t *os, off_t *lenp)
241 {
242 	off_t loc;
243 
244 	loc = vdev_space_alloc(zfs, lenp);
245 	os->space += *lenp;
246 	return (loc);
247 }
248 
249 uint64_t
250 objset_space(const zfs_objset_t *os)
251 {
252 	return (os->space);
253 }
254 
255 void
256 objset_root_blkptr_copy(const zfs_objset_t *os, blkptr_t *bp)
257 {
258 	memcpy(bp, &os->osbp, sizeof(blkptr_t));
259 }
260