1*240afd8cSMark Johnston /*- 2*240afd8cSMark Johnston * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3*240afd8cSMark Johnston * 4*240afd8cSMark Johnston * Copyright (c) 2022 The FreeBSD Foundation 5*240afd8cSMark Johnston * 6*240afd8cSMark Johnston * This software was developed by Mark Johnston under sponsorship from 7*240afd8cSMark Johnston * the FreeBSD Foundation. 8*240afd8cSMark Johnston * 9*240afd8cSMark Johnston * Redistribution and use in source and binary forms, with or without 10*240afd8cSMark Johnston * modification, are permitted provided that the following conditions are 11*240afd8cSMark Johnston * met: 12*240afd8cSMark Johnston * 1. Redistributions of source code must retain the above copyright 13*240afd8cSMark Johnston * notice, this list of conditions and the following disclaimer. 14*240afd8cSMark Johnston * 2. Redistributions in binary form must reproduce the above copyright 15*240afd8cSMark Johnston * notice, this list of conditions and the following disclaimer in 16*240afd8cSMark Johnston * the documentation and/or other materials provided with the distribution. 17*240afd8cSMark Johnston * 18*240afd8cSMark Johnston * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19*240afd8cSMark Johnston * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20*240afd8cSMark Johnston * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21*240afd8cSMark Johnston * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22*240afd8cSMark Johnston * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23*240afd8cSMark Johnston * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24*240afd8cSMark Johnston * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25*240afd8cSMark Johnston * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26*240afd8cSMark Johnston * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27*240afd8cSMark Johnston * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28*240afd8cSMark Johnston * SUCH DAMAGE. 29*240afd8cSMark Johnston */ 30*240afd8cSMark Johnston 31*240afd8cSMark Johnston #ifndef _MAKEFS_ZFS_H_ 32*240afd8cSMark Johnston #define _MAKEFS_ZFS_H_ 33*240afd8cSMark Johnston 34*240afd8cSMark Johnston #include <sys/types.h> 35*240afd8cSMark Johnston #include <sys/queue.h> 36*240afd8cSMark Johnston 37*240afd8cSMark Johnston #include <bitstring.h> 38*240afd8cSMark Johnston #include <stdbool.h> 39*240afd8cSMark Johnston 40*240afd8cSMark Johnston #include "makefs.h" 41*240afd8cSMark Johnston 42*240afd8cSMark Johnston #include "zfs/nvlist.h" 43*240afd8cSMark Johnston #define ASSERT assert 44*240afd8cSMark Johnston #include "zfs/zfsimpl.h" 45*240afd8cSMark Johnston 46*240afd8cSMark Johnston #define MAXBLOCKSHIFT 17 /* 128KB */ 47*240afd8cSMark Johnston #define MAXBLOCKSIZE ((off_t)(1 << MAXBLOCKSHIFT)) 48*240afd8cSMark Johnston _Static_assert(MAXBLOCKSIZE == SPA_OLDMAXBLOCKSIZE, ""); 49*240afd8cSMark Johnston #define MINBLOCKSHIFT 9 /* 512B */ 50*240afd8cSMark Johnston #define MINBLOCKSIZE ((off_t)(1 << MINBLOCKSHIFT)) 51*240afd8cSMark Johnston _Static_assert(MINBLOCKSIZE == SPA_MINBLOCKSIZE, ""); 52*240afd8cSMark Johnston #define MINDEVSIZE ((off_t)SPA_MINDEVSIZE) 53*240afd8cSMark Johnston 54*240afd8cSMark Johnston /* All data was written in this transaction group. */ 55*240afd8cSMark Johnston #define TXG 4 56*240afd8cSMark Johnston 57*240afd8cSMark Johnston typedef struct zfs_dsl_dataset zfs_dsl_dataset_t; 58*240afd8cSMark Johnston typedef struct zfs_dsl_dir zfs_dsl_dir_t; 59*240afd8cSMark Johnston typedef struct zfs_objset zfs_objset_t; 60*240afd8cSMark Johnston typedef struct zfs_zap zfs_zap_t; 61*240afd8cSMark Johnston 62*240afd8cSMark Johnston struct dataset_desc { 63*240afd8cSMark Johnston char *params; 64*240afd8cSMark Johnston STAILQ_ENTRY(dataset_desc) next; 65*240afd8cSMark Johnston }; 66*240afd8cSMark Johnston 67*240afd8cSMark Johnston typedef struct { 68*240afd8cSMark Johnston bool nowarn; 69*240afd8cSMark Johnston 70*240afd8cSMark Johnston /* I/O buffer, just for convenience. */ 71*240afd8cSMark Johnston char filebuf[MAXBLOCKSIZE]; 72*240afd8cSMark Johnston 73*240afd8cSMark Johnston /* Pool parameters. */ 74*240afd8cSMark Johnston const char *poolname; 75*240afd8cSMark Johnston char *rootpath; /* implicit mount point prefix */ 76*240afd8cSMark Johnston char *bootfs; /* bootable dataset, pool property */ 77*240afd8cSMark Johnston int ashift; /* vdev block size */ 78*240afd8cSMark Johnston uint64_t mssize; /* metaslab size */ 79*240afd8cSMark Johnston STAILQ_HEAD(, dataset_desc) datasetdescs; /* non-root dataset descrs */ 80*240afd8cSMark Johnston 81*240afd8cSMark Johnston /* Pool state. */ 82*240afd8cSMark Johnston uint64_t poolguid; /* pool and root vdev GUID */ 83*240afd8cSMark Johnston zfs_zap_t *poolprops; 84*240afd8cSMark Johnston 85*240afd8cSMark Johnston /* MOS state. */ 86*240afd8cSMark Johnston zfs_objset_t *mos; /* meta object set */ 87*240afd8cSMark Johnston uint64_t objarrid; /* space map object array */ 88*240afd8cSMark Johnston 89*240afd8cSMark Johnston /* DSL state. */ 90*240afd8cSMark Johnston zfs_dsl_dir_t *rootdsldir; /* root DSL directory */ 91*240afd8cSMark Johnston zfs_dsl_dataset_t *rootds; 92*240afd8cSMark Johnston zfs_dsl_dir_t *origindsldir; /* $ORIGIN */ 93*240afd8cSMark Johnston zfs_dsl_dataset_t *originds; 94*240afd8cSMark Johnston zfs_dsl_dataset_t *snapds; 95*240afd8cSMark Johnston zfs_zap_t *cloneszap; 96*240afd8cSMark Johnston zfs_dsl_dir_t *freedsldir; /* $FREE */ 97*240afd8cSMark Johnston zfs_dsl_dir_t *mosdsldir; /* $MOS */ 98*240afd8cSMark Johnston 99*240afd8cSMark Johnston /* vdev state. */ 100*240afd8cSMark Johnston int fd; /* vdev disk fd */ 101*240afd8cSMark Johnston uint64_t vdevguid; /* disk vdev GUID */ 102*240afd8cSMark Johnston off_t vdevsize; /* vdev size, including labels */ 103*240afd8cSMark Johnston off_t asize; /* vdev size, excluding labels */ 104*240afd8cSMark Johnston bitstr_t *spacemap; /* space allocation tracking */ 105*240afd8cSMark Johnston int spacemapbits; /* one bit per ashift-sized block */ 106*240afd8cSMark Johnston uint64_t msshift; /* log2(metaslab size) */ 107*240afd8cSMark Johnston uint64_t mscount; /* number of metaslabs for this vdev */ 108*240afd8cSMark Johnston } zfs_opt_t; 109*240afd8cSMark Johnston 110*240afd8cSMark Johnston /* dsl.c */ 111*240afd8cSMark Johnston void dsl_init(zfs_opt_t *); 112*240afd8cSMark Johnston const char *dsl_dir_fullname(const zfs_dsl_dir_t *); 113*240afd8cSMark Johnston uint64_t dsl_dir_id(zfs_dsl_dir_t *); 114*240afd8cSMark Johnston uint64_t dsl_dir_dataset_id(zfs_dsl_dir_t *); 115*240afd8cSMark Johnston void dsl_dir_foreach(zfs_opt_t *, zfs_dsl_dir_t *, 116*240afd8cSMark Johnston void (*)(zfs_opt_t *, zfs_dsl_dir_t *, void *), void *); 117*240afd8cSMark Johnston int dsl_dir_get_canmount(zfs_dsl_dir_t *, uint64_t *); 118*240afd8cSMark Johnston char *dsl_dir_get_mountpoint(zfs_opt_t *, zfs_dsl_dir_t *); 119*240afd8cSMark Johnston bool dsl_dir_has_dataset(zfs_dsl_dir_t *); 120*240afd8cSMark Johnston bool dsl_dir_dataset_has_objset(zfs_dsl_dir_t *); 121*240afd8cSMark Johnston void dsl_dir_dataset_write(zfs_opt_t *, zfs_objset_t *, zfs_dsl_dir_t *); 122*240afd8cSMark Johnston void dsl_dir_size_set(zfs_dsl_dir_t *, uint64_t); 123*240afd8cSMark Johnston void dsl_write(zfs_opt_t *); 124*240afd8cSMark Johnston 125*240afd8cSMark Johnston /* fs.c */ 126*240afd8cSMark Johnston void fs_build(zfs_opt_t *, int, fsnode *); 127*240afd8cSMark Johnston 128*240afd8cSMark Johnston /* objset.c */ 129*240afd8cSMark Johnston zfs_objset_t *objset_alloc(zfs_opt_t *zfs, uint64_t type); 130*240afd8cSMark Johnston off_t objset_space_alloc(zfs_opt_t *, zfs_objset_t *, off_t *); 131*240afd8cSMark Johnston dnode_phys_t *objset_dnode_alloc(zfs_objset_t *, uint8_t, uint64_t *); 132*240afd8cSMark Johnston dnode_phys_t *objset_dnode_bonus_alloc(zfs_objset_t *, uint8_t, uint8_t, 133*240afd8cSMark Johnston uint16_t, uint64_t *); 134*240afd8cSMark Johnston dnode_phys_t *objset_dnode_lookup(zfs_objset_t *, uint64_t); 135*240afd8cSMark Johnston void objset_root_blkptr_copy(const zfs_objset_t *, blkptr_t *); 136*240afd8cSMark Johnston uint64_t objset_space(const zfs_objset_t *); 137*240afd8cSMark Johnston void objset_write(zfs_opt_t *zfs, zfs_objset_t *os); 138*240afd8cSMark Johnston 139*240afd8cSMark Johnston /* vdev.c */ 140*240afd8cSMark Johnston void vdev_init(zfs_opt_t *, const char *); 141*240afd8cSMark Johnston off_t vdev_space_alloc(zfs_opt_t *zfs, off_t *lenp); 142*240afd8cSMark Johnston void vdev_pwrite_data(zfs_opt_t *zfs, uint8_t datatype, uint8_t cksumtype, 143*240afd8cSMark Johnston uint8_t level, uint64_t fill, const void *data, off_t sz, off_t loc, 144*240afd8cSMark Johnston blkptr_t *bp); 145*240afd8cSMark Johnston void vdev_pwrite_dnode_indir(zfs_opt_t *zfs, dnode_phys_t *dnode, uint8_t level, 146*240afd8cSMark Johnston uint64_t fill, const void *data, off_t sz, off_t loc, blkptr_t *bp); 147*240afd8cSMark Johnston void vdev_pwrite_dnode_data(zfs_opt_t *zfs, dnode_phys_t *dnode, const void *data, 148*240afd8cSMark Johnston off_t sz, off_t loc); 149*240afd8cSMark Johnston void vdev_label_write(zfs_opt_t *zfs, int ind, const vdev_label_t *labelp); 150*240afd8cSMark Johnston void vdev_spacemap_write(zfs_opt_t *); 151*240afd8cSMark Johnston void vdev_fini(zfs_opt_t *zfs); 152*240afd8cSMark Johnston 153*240afd8cSMark Johnston /* zap.c */ 154*240afd8cSMark Johnston zfs_zap_t *zap_alloc(zfs_objset_t *, dnode_phys_t *); 155*240afd8cSMark Johnston void zap_add(zfs_zap_t *, const char *, size_t, size_t, const uint8_t *); 156*240afd8cSMark Johnston void zap_add_uint64(zfs_zap_t *, const char *, uint64_t); 157*240afd8cSMark Johnston void zap_add_string(zfs_zap_t *, const char *, const char *); 158*240afd8cSMark Johnston bool zap_entry_exists(zfs_zap_t *, const char *); 159*240afd8cSMark Johnston void zap_write(zfs_opt_t *, zfs_zap_t *); 160*240afd8cSMark Johnston 161*240afd8cSMark Johnston /* zfs.c */ 162*240afd8cSMark Johnston struct dnode_cursor *dnode_cursor_init(zfs_opt_t *, zfs_objset_t *, 163*240afd8cSMark Johnston dnode_phys_t *, off_t, off_t); 164*240afd8cSMark Johnston blkptr_t *dnode_cursor_next(zfs_opt_t *, struct dnode_cursor *, off_t); 165*240afd8cSMark Johnston void dnode_cursor_finish(zfs_opt_t *, struct dnode_cursor *); 166*240afd8cSMark Johnston 167*240afd8cSMark Johnston #endif /* !_MAKEFS_ZFS_H_ */ 168