xref: /freebsd/usr.sbin/makefs/zfs/zfs.h (revision ef067b57)
1240afd8cSMark Johnston /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
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 #ifndef _MAKEFS_ZFS_H_
32240afd8cSMark Johnston #define	_MAKEFS_ZFS_H_
33240afd8cSMark Johnston 
34240afd8cSMark Johnston #include <sys/types.h>
35c6890399SJessica Clarke #include <sys/endian.h>
36240afd8cSMark Johnston #include <sys/queue.h>
37240afd8cSMark Johnston 
38240afd8cSMark Johnston #include <bitstring.h>
39187084ddSMark Johnston #include <stdalign.h>
40240afd8cSMark Johnston #include <stdbool.h>
41240afd8cSMark Johnston 
42240afd8cSMark Johnston #include "makefs.h"
43240afd8cSMark Johnston 
44240afd8cSMark Johnston #include "zfs/nvlist.h"
45240afd8cSMark Johnston #define	ASSERT		assert
46240afd8cSMark Johnston #include "zfs/zfsimpl.h"
47240afd8cSMark Johnston 
48240afd8cSMark Johnston #define	MAXBLOCKSHIFT		17	/* 128KB */
49240afd8cSMark Johnston #define	MAXBLOCKSIZE		((off_t)(1 << MAXBLOCKSHIFT))
50240afd8cSMark Johnston _Static_assert(MAXBLOCKSIZE == SPA_OLDMAXBLOCKSIZE, "");
51240afd8cSMark Johnston #define	MINBLOCKSHIFT		9	/* 512B */
52240afd8cSMark Johnston #define	MINBLOCKSIZE		((off_t)(1 << MINBLOCKSHIFT))
53240afd8cSMark Johnston _Static_assert(MINBLOCKSIZE == SPA_MINBLOCKSIZE, "");
54240afd8cSMark Johnston #define	MINDEVSIZE		((off_t)SPA_MINDEVSIZE)
55240afd8cSMark Johnston 
56240afd8cSMark Johnston /* All data was written in this transaction group. */
57240afd8cSMark Johnston #define	TXG			4
58240afd8cSMark Johnston 
59240afd8cSMark Johnston typedef struct zfs_dsl_dataset zfs_dsl_dataset_t;
60240afd8cSMark Johnston typedef struct zfs_dsl_dir zfs_dsl_dir_t;
61240afd8cSMark Johnston typedef struct zfs_objset zfs_objset_t;
62240afd8cSMark Johnston typedef struct zfs_zap zfs_zap_t;
63240afd8cSMark Johnston 
64240afd8cSMark Johnston struct dataset_desc {
65240afd8cSMark Johnston 	char		*params;
66240afd8cSMark Johnston 	STAILQ_ENTRY(dataset_desc) next;
67240afd8cSMark Johnston };
68240afd8cSMark Johnston 
69240afd8cSMark Johnston typedef struct {
70187084ddSMark Johnston 	/*
71187084ddSMark Johnston 	 * Block buffer, needs to be aligned for various on-disk structures,
72187084ddSMark Johnston 	 * ZAPs, etc..
73187084ddSMark Johnston 	 */
74187084ddSMark Johnston 	char		filebuf[MAXBLOCKSIZE] __aligned(alignof(uint64_t));
75240afd8cSMark Johnston 
76187084ddSMark Johnston 	bool		nowarn;
77240afd8cSMark Johnston 
78240afd8cSMark Johnston 	/* Pool parameters. */
79240afd8cSMark Johnston 	const char	*poolname;
80240afd8cSMark Johnston 	char		*rootpath;	/* implicit mount point prefix */
81240afd8cSMark Johnston 	char		*bootfs;	/* bootable dataset, pool property */
82240afd8cSMark Johnston 	int		ashift;		/* vdev block size */
83240afd8cSMark Johnston 	uint64_t	mssize;		/* metaslab size */
84240afd8cSMark Johnston 	STAILQ_HEAD(, dataset_desc) datasetdescs; /* non-root dataset descrs  */
85240afd8cSMark Johnston 
86240afd8cSMark Johnston 	/* Pool state. */
87240afd8cSMark Johnston 	uint64_t	poolguid;	/* pool and root vdev GUID */
88240afd8cSMark Johnston 	zfs_zap_t	*poolprops;
89240afd8cSMark Johnston 
90240afd8cSMark Johnston 	/* MOS state. */
91240afd8cSMark Johnston 	zfs_objset_t	*mos;		/* meta object set */
92240afd8cSMark Johnston 	uint64_t	objarrid;	/* space map object array */
93240afd8cSMark Johnston 
94240afd8cSMark Johnston 	/* DSL state. */
95240afd8cSMark Johnston 	zfs_dsl_dir_t	*rootdsldir;	/* root DSL directory */
96240afd8cSMark Johnston 	zfs_dsl_dataset_t *rootds;
97240afd8cSMark Johnston 	zfs_dsl_dir_t	*origindsldir;	/* $ORIGIN */
98240afd8cSMark Johnston 	zfs_dsl_dataset_t *originds;
99240afd8cSMark Johnston 	zfs_dsl_dataset_t *snapds;
100240afd8cSMark Johnston 	zfs_zap_t	*cloneszap;
101240afd8cSMark Johnston 	zfs_dsl_dir_t	*freedsldir;	/* $FREE */
102240afd8cSMark Johnston 	zfs_dsl_dir_t	*mosdsldir;	/* $MOS */
103240afd8cSMark Johnston 
104240afd8cSMark Johnston 	/* vdev state. */
105240afd8cSMark Johnston 	int		fd;		/* vdev disk fd */
106240afd8cSMark Johnston 	uint64_t	vdevguid;	/* disk vdev GUID */
107240afd8cSMark Johnston 	off_t		vdevsize;	/* vdev size, including labels */
108240afd8cSMark Johnston 	off_t		asize;		/* vdev size, excluding labels */
109240afd8cSMark Johnston 	bitstr_t	*spacemap;	/* space allocation tracking */
110240afd8cSMark Johnston 	int		spacemapbits;	/* one bit per ashift-sized block */
111240afd8cSMark Johnston 	uint64_t	msshift;	/* log2(metaslab size) */
112240afd8cSMark Johnston 	uint64_t	mscount;	/* number of metaslabs for this vdev */
113240afd8cSMark Johnston } zfs_opt_t;
114240afd8cSMark Johnston 
115240afd8cSMark Johnston /* dsl.c */
116240afd8cSMark Johnston void dsl_init(zfs_opt_t *);
117240afd8cSMark Johnston const char *dsl_dir_fullname(const zfs_dsl_dir_t *);
118240afd8cSMark Johnston uint64_t dsl_dir_id(zfs_dsl_dir_t *);
119240afd8cSMark Johnston uint64_t dsl_dir_dataset_id(zfs_dsl_dir_t *);
120240afd8cSMark Johnston void dsl_dir_foreach(zfs_opt_t *, zfs_dsl_dir_t *,
121240afd8cSMark Johnston     void (*)(zfs_opt_t *, zfs_dsl_dir_t *, void *), void *);
122240afd8cSMark Johnston int dsl_dir_get_canmount(zfs_dsl_dir_t *, uint64_t *);
123240afd8cSMark Johnston char *dsl_dir_get_mountpoint(zfs_opt_t *, zfs_dsl_dir_t *);
124240afd8cSMark Johnston bool dsl_dir_has_dataset(zfs_dsl_dir_t *);
125240afd8cSMark Johnston bool dsl_dir_dataset_has_objset(zfs_dsl_dir_t *);
126240afd8cSMark Johnston void dsl_dir_dataset_write(zfs_opt_t *, zfs_objset_t *, zfs_dsl_dir_t *);
127ef067b57SMark Johnston void dsl_dir_root_finalize(zfs_opt_t *, uint64_t);
128240afd8cSMark Johnston void dsl_write(zfs_opt_t *);
129240afd8cSMark Johnston 
130240afd8cSMark Johnston /* fs.c */
131240afd8cSMark Johnston void fs_build(zfs_opt_t *, int, fsnode *);
132240afd8cSMark Johnston 
133240afd8cSMark Johnston /* objset.c */
134240afd8cSMark Johnston zfs_objset_t *objset_alloc(zfs_opt_t *zfs, uint64_t type);
135240afd8cSMark Johnston off_t objset_space_alloc(zfs_opt_t *, zfs_objset_t *, off_t *);
136240afd8cSMark Johnston dnode_phys_t *objset_dnode_alloc(zfs_objset_t *, uint8_t, uint64_t *);
137240afd8cSMark Johnston dnode_phys_t *objset_dnode_bonus_alloc(zfs_objset_t *, uint8_t, uint8_t,
138240afd8cSMark Johnston     uint16_t, uint64_t *);
139240afd8cSMark Johnston dnode_phys_t *objset_dnode_lookup(zfs_objset_t *, uint64_t);
140240afd8cSMark Johnston void objset_root_blkptr_copy(const zfs_objset_t *, blkptr_t *);
141240afd8cSMark Johnston uint64_t objset_space(const zfs_objset_t *);
142240afd8cSMark Johnston void objset_write(zfs_opt_t *zfs, zfs_objset_t *os);
143240afd8cSMark Johnston 
144240afd8cSMark Johnston /* vdev.c */
145240afd8cSMark Johnston void vdev_init(zfs_opt_t *, const char *);
146240afd8cSMark Johnston off_t vdev_space_alloc(zfs_opt_t *zfs, off_t *lenp);
147240afd8cSMark Johnston void vdev_pwrite_data(zfs_opt_t *zfs, uint8_t datatype, uint8_t cksumtype,
148240afd8cSMark Johnston     uint8_t level, uint64_t fill, const void *data, off_t sz, off_t loc,
149240afd8cSMark Johnston     blkptr_t *bp);
150240afd8cSMark Johnston void vdev_pwrite_dnode_indir(zfs_opt_t *zfs, dnode_phys_t *dnode, uint8_t level,
151240afd8cSMark Johnston     uint64_t fill, const void *data, off_t sz, off_t loc, blkptr_t *bp);
152240afd8cSMark Johnston void vdev_pwrite_dnode_data(zfs_opt_t *zfs, dnode_phys_t *dnode, const void *data,
153240afd8cSMark Johnston     off_t sz, off_t loc);
154240afd8cSMark Johnston void vdev_label_write(zfs_opt_t *zfs, int ind, const vdev_label_t *labelp);
155240afd8cSMark Johnston void vdev_spacemap_write(zfs_opt_t *);
156240afd8cSMark Johnston void vdev_fini(zfs_opt_t *zfs);
157240afd8cSMark Johnston 
158240afd8cSMark Johnston /* zap.c */
159240afd8cSMark Johnston zfs_zap_t *zap_alloc(zfs_objset_t *, dnode_phys_t *);
160240afd8cSMark Johnston void zap_add(zfs_zap_t *, const char *, size_t, size_t, const uint8_t *);
161240afd8cSMark Johnston void zap_add_uint64(zfs_zap_t *, const char *, uint64_t);
162240afd8cSMark Johnston void zap_add_string(zfs_zap_t *, const char *, const char *);
163240afd8cSMark Johnston bool zap_entry_exists(zfs_zap_t *, const char *);
164240afd8cSMark Johnston void zap_write(zfs_opt_t *, zfs_zap_t *);
165240afd8cSMark Johnston 
166240afd8cSMark Johnston /* zfs.c */
167240afd8cSMark Johnston struct dnode_cursor *dnode_cursor_init(zfs_opt_t *, zfs_objset_t *,
168240afd8cSMark Johnston     dnode_phys_t *, off_t, off_t);
169240afd8cSMark Johnston blkptr_t *dnode_cursor_next(zfs_opt_t *, struct dnode_cursor *, off_t);
170240afd8cSMark Johnston void dnode_cursor_finish(zfs_opt_t *, struct dnode_cursor *);
17114c5cf3aSMark Johnston uint64_t randomguid(void);
172240afd8cSMark Johnston 
173240afd8cSMark Johnston #endif /* !_MAKEFS_ZFS_H_ */
174