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 <assert.h> 32c6890399SJessica Clarke #include <stdlib.h> 33240afd8cSMark Johnston #include <string.h> 34240afd8cSMark Johnston 35240afd8cSMark Johnston #include <util.h> 36240afd8cSMark Johnston 37240afd8cSMark Johnston #include "makefs.h" 38240afd8cSMark Johnston #include "zfs.h" 39240afd8cSMark Johnston 40240afd8cSMark Johnston typedef struct zfs_dsl_dataset { 41240afd8cSMark Johnston zfs_objset_t *os; /* referenced objset, may be null */ 42240afd8cSMark Johnston dsl_dataset_phys_t *phys; /* on-disk representation */ 43240afd8cSMark Johnston uint64_t dsid; /* DSL dataset dnode */ 44240afd8cSMark Johnston 45240afd8cSMark Johnston struct zfs_dsl_dir *dir; /* containing parent */ 46240afd8cSMark Johnston } zfs_dsl_dataset_t; 47240afd8cSMark Johnston 48240afd8cSMark Johnston typedef STAILQ_HEAD(zfs_dsl_dir_list, zfs_dsl_dir) zfs_dsl_dir_list_t; 49240afd8cSMark Johnston 50240afd8cSMark Johnston typedef struct zfs_dsl_dir { 51240afd8cSMark Johnston char *fullname; /* full dataset name */ 52240afd8cSMark Johnston char *name; /* basename(fullname) */ 53240afd8cSMark Johnston dsl_dir_phys_t *phys; /* on-disk representation */ 54240afd8cSMark Johnston nvlist_t *propsnv; /* properties saved in propszap */ 55240afd8cSMark Johnston 56240afd8cSMark Johnston zfs_dsl_dataset_t *headds; /* principal dataset, may be null */ 57240afd8cSMark Johnston 58240afd8cSMark Johnston uint64_t dirid; /* DSL directory dnode */ 59240afd8cSMark Johnston zfs_zap_t *propszap; /* dataset properties */ 60240afd8cSMark Johnston zfs_zap_t *childzap; /* child directories */ 61240afd8cSMark Johnston 62240afd8cSMark Johnston /* DSL directory tree linkage. */ 63240afd8cSMark Johnston struct zfs_dsl_dir *parent; 64240afd8cSMark Johnston zfs_dsl_dir_list_t children; 65240afd8cSMark Johnston STAILQ_ENTRY(zfs_dsl_dir) next; 66240afd8cSMark Johnston } zfs_dsl_dir_t; 67240afd8cSMark Johnston 68240afd8cSMark Johnston static zfs_dsl_dir_t *dsl_dir_alloc(zfs_opt_t *zfs, const char *name); 69240afd8cSMark Johnston static zfs_dsl_dataset_t *dsl_dataset_alloc(zfs_opt_t *zfs, zfs_dsl_dir_t *dir); 70240afd8cSMark Johnston 71240afd8cSMark Johnston static int 72240afd8cSMark Johnston nvlist_find_string(nvlist_t *nvl, const char *key, char **retp) 73240afd8cSMark Johnston { 74240afd8cSMark Johnston char *str; 75240afd8cSMark Johnston int error, len; 76240afd8cSMark Johnston 77240afd8cSMark Johnston error = nvlist_find(nvl, key, DATA_TYPE_STRING, NULL, &str, &len); 78*752ba100SMark Johnston if (error == 0) { 79*752ba100SMark Johnston *retp = ecalloc(1, len + 1); 80*752ba100SMark Johnston memcpy(*retp, str, len); 81*752ba100SMark Johnston } 82240afd8cSMark Johnston return (error); 83240afd8cSMark Johnston } 84240afd8cSMark Johnston 85240afd8cSMark Johnston static int 86240afd8cSMark Johnston nvlist_find_uint64(nvlist_t *nvl, const char *key, uint64_t *retp) 87240afd8cSMark Johnston { 88240afd8cSMark Johnston return (nvlist_find(nvl, key, DATA_TYPE_UINT64, NULL, retp, NULL)); 89240afd8cSMark Johnston } 90240afd8cSMark Johnston 91240afd8cSMark Johnston /* 92240afd8cSMark Johnston * Return an allocated string containing the head dataset's mountpoint, 93240afd8cSMark Johnston * including the root path prefix. 94240afd8cSMark Johnston * 95240afd8cSMark Johnston * If the dataset has a mountpoint property, it is returned. Otherwise we have 96240afd8cSMark Johnston * to follow ZFS' inheritance rules. 97240afd8cSMark Johnston */ 98240afd8cSMark Johnston char * 99240afd8cSMark Johnston dsl_dir_get_mountpoint(zfs_opt_t *zfs, zfs_dsl_dir_t *dir) 100240afd8cSMark Johnston { 101240afd8cSMark Johnston zfs_dsl_dir_t *pdir; 102*752ba100SMark Johnston char *mountpoint; 103240afd8cSMark Johnston 104240afd8cSMark Johnston if (nvlist_find_string(dir->propsnv, "mountpoint", &mountpoint) == 0) { 105240afd8cSMark Johnston if (strcmp(mountpoint, "none") == 0) 106240afd8cSMark Johnston return (NULL); 107240afd8cSMark Johnston } else { 108240afd8cSMark Johnston /* 109240afd8cSMark Johnston * If we don't have a mountpoint, it's inherited from one of our 110240afd8cSMark Johnston * ancestors. Walk up the hierarchy until we find it, building 111240afd8cSMark Johnston * up our mountpoint along the way. The mountpoint property is 112240afd8cSMark Johnston * always set for the root dataset. 113240afd8cSMark Johnston */ 114*752ba100SMark Johnston for (pdir = dir->parent, mountpoint = estrdup(dir->name);; 115*752ba100SMark Johnston pdir = pdir->parent) { 116*752ba100SMark Johnston char *origmountpoint, *tmp; 117*752ba100SMark Johnston 118240afd8cSMark Johnston origmountpoint = mountpoint; 119240afd8cSMark Johnston 120240afd8cSMark Johnston if (nvlist_find_string(pdir->propsnv, "mountpoint", 121*752ba100SMark Johnston &tmp) == 0) { 122*752ba100SMark Johnston easprintf(&mountpoint, "%s%s%s", tmp, 123*752ba100SMark Johnston tmp[strlen(tmp) - 1] == '/' ? "" : "/", 124*752ba100SMark Johnston origmountpoint); 125*752ba100SMark Johnston free(tmp); 126240afd8cSMark Johnston free(origmountpoint); 127240afd8cSMark Johnston break; 128240afd8cSMark Johnston } 129240afd8cSMark Johnston 130240afd8cSMark Johnston easprintf(&mountpoint, "%s/%s", pdir->name, 131240afd8cSMark Johnston origmountpoint); 132240afd8cSMark Johnston free(origmountpoint); 133240afd8cSMark Johnston } 134240afd8cSMark Johnston } 135240afd8cSMark Johnston assert(mountpoint[0] == '/'); 136240afd8cSMark Johnston assert(strstr(mountpoint, zfs->rootpath) == mountpoint); 137240afd8cSMark Johnston 138240afd8cSMark Johnston return (mountpoint); 139240afd8cSMark Johnston } 140240afd8cSMark Johnston 141240afd8cSMark Johnston int 142240afd8cSMark Johnston dsl_dir_get_canmount(zfs_dsl_dir_t *dir, uint64_t *canmountp) 143240afd8cSMark Johnston { 144240afd8cSMark Johnston return (nvlist_find_uint64(dir->propsnv, "canmount", canmountp)); 145240afd8cSMark Johnston } 146240afd8cSMark Johnston 147240afd8cSMark Johnston /* 148240afd8cSMark Johnston * Handle dataset properties that we know about; stash them into an nvlist to be 149240afd8cSMark Johnston * written later to the properties ZAP object. 150240afd8cSMark Johnston * 151240afd8cSMark Johnston * If the set of properties we handle grows too much, we should probably explore 152240afd8cSMark Johnston * using libzfs to manage them. 153240afd8cSMark Johnston */ 154240afd8cSMark Johnston static void 155240afd8cSMark Johnston dsl_dir_set_prop(zfs_opt_t *zfs, zfs_dsl_dir_t *dir, const char *key, 156240afd8cSMark Johnston const char *val) 157240afd8cSMark Johnston { 158240afd8cSMark Johnston nvlist_t *nvl; 159240afd8cSMark Johnston 160240afd8cSMark Johnston nvl = dir->propsnv; 161240afd8cSMark Johnston if (val == NULL || val[0] == '\0') 162240afd8cSMark Johnston errx(1, "missing value for property `%s'", key); 163240afd8cSMark Johnston if (nvpair_find(nvl, key) != NULL) 164240afd8cSMark Johnston errx(1, "property `%s' already set", key); 165240afd8cSMark Johnston 166240afd8cSMark Johnston if (strcmp(key, "mountpoint") == 0) { 167240afd8cSMark Johnston if (strcmp(val, "none") != 0) { 168240afd8cSMark Johnston if (val[0] != '/') 169240afd8cSMark Johnston errx(1, "mountpoint `%s' is not absolute", val); 170240afd8cSMark Johnston if (strcmp(val, zfs->rootpath) != 0 && 171240afd8cSMark Johnston strcmp(zfs->rootpath, "/") != 0 && 172240afd8cSMark Johnston (strstr(val, zfs->rootpath) != val || 173240afd8cSMark Johnston val[strlen(zfs->rootpath)] != '/')) { 174240afd8cSMark Johnston errx(1, "mountpoint `%s' is not prefixed by " 175240afd8cSMark Johnston "the root path `%s'", val, zfs->rootpath); 176240afd8cSMark Johnston } 177240afd8cSMark Johnston } 178240afd8cSMark Johnston nvlist_add_string(nvl, key, val); 179240afd8cSMark Johnston } else if (strcmp(key, "atime") == 0 || strcmp(key, "exec") == 0 || 180240afd8cSMark Johnston strcmp(key, "setuid") == 0) { 181240afd8cSMark Johnston if (strcmp(val, "on") == 0) 182240afd8cSMark Johnston nvlist_add_uint64(nvl, key, 1); 183240afd8cSMark Johnston else if (strcmp(val, "off") == 0) 184240afd8cSMark Johnston nvlist_add_uint64(nvl, key, 0); 185240afd8cSMark Johnston else 186240afd8cSMark Johnston errx(1, "invalid value `%s' for %s", val, key); 187240afd8cSMark Johnston } else if (strcmp(key, "canmount") == 0) { 188240afd8cSMark Johnston if (strcmp(val, "noauto") == 0) 189240afd8cSMark Johnston nvlist_add_uint64(nvl, key, 2); 190240afd8cSMark Johnston else if (strcmp(val, "on") == 0) 191240afd8cSMark Johnston nvlist_add_uint64(nvl, key, 1); 192240afd8cSMark Johnston else if (strcmp(val, "off") == 0) 193240afd8cSMark Johnston nvlist_add_uint64(nvl, key, 0); 194240afd8cSMark Johnston else 195240afd8cSMark Johnston errx(1, "invalid value `%s' for %s", val, key); 196240afd8cSMark Johnston } else { 197240afd8cSMark Johnston errx(1, "unknown property `%s'", key); 198240afd8cSMark Johnston } 199240afd8cSMark Johnston } 200240afd8cSMark Johnston 201240afd8cSMark Johnston static zfs_dsl_dir_t * 202240afd8cSMark Johnston dsl_metadir_alloc(zfs_opt_t *zfs, const char *name) 203240afd8cSMark Johnston { 204240afd8cSMark Johnston zfs_dsl_dir_t *dir; 205240afd8cSMark Johnston char *path; 206240afd8cSMark Johnston 207240afd8cSMark Johnston easprintf(&path, "%s/%s", zfs->poolname, name); 208240afd8cSMark Johnston dir = dsl_dir_alloc(zfs, path); 209240afd8cSMark Johnston free(path); 210240afd8cSMark Johnston return (dir); 211240afd8cSMark Johnston } 212240afd8cSMark Johnston 213240afd8cSMark Johnston static void 214240afd8cSMark Johnston dsl_origindir_init(zfs_opt_t *zfs) 215240afd8cSMark Johnston { 216240afd8cSMark Johnston dnode_phys_t *clones; 217240afd8cSMark Johnston uint64_t clonesid; 218240afd8cSMark Johnston 219240afd8cSMark Johnston zfs->origindsldir = dsl_metadir_alloc(zfs, "$ORIGIN"); 220240afd8cSMark Johnston zfs->originds = dsl_dataset_alloc(zfs, zfs->origindsldir); 221240afd8cSMark Johnston zfs->snapds = dsl_dataset_alloc(zfs, zfs->origindsldir); 222240afd8cSMark Johnston 223240afd8cSMark Johnston clones = objset_dnode_alloc(zfs->mos, DMU_OT_DSL_CLONES, &clonesid); 224240afd8cSMark Johnston zfs->cloneszap = zap_alloc(zfs->mos, clones); 225240afd8cSMark Johnston zfs->origindsldir->phys->dd_clones = clonesid; 226240afd8cSMark Johnston } 227240afd8cSMark Johnston 228240afd8cSMark Johnston void 229240afd8cSMark Johnston dsl_init(zfs_opt_t *zfs) 230240afd8cSMark Johnston { 231240afd8cSMark Johnston zfs_dsl_dir_t *dir; 232240afd8cSMark Johnston struct dataset_desc *d; 233240afd8cSMark Johnston const char *dspropdelim; 234240afd8cSMark Johnston 235240afd8cSMark Johnston dspropdelim = ";"; 236240afd8cSMark Johnston 237240afd8cSMark Johnston zfs->rootdsldir = dsl_dir_alloc(zfs, NULL); 238240afd8cSMark Johnston 239240afd8cSMark Johnston nvlist_add_uint64(zfs->rootdsldir->propsnv, "compression", 240240afd8cSMark Johnston ZIO_COMPRESS_OFF); 241240afd8cSMark Johnston 242240afd8cSMark Johnston zfs->rootds = dsl_dataset_alloc(zfs, zfs->rootdsldir); 243240afd8cSMark Johnston zfs->rootdsldir->headds = zfs->rootds; 244240afd8cSMark Johnston 245240afd8cSMark Johnston zfs->mosdsldir = dsl_metadir_alloc(zfs, "$MOS"); 246240afd8cSMark Johnston zfs->freedsldir = dsl_metadir_alloc(zfs, "$FREE"); 247240afd8cSMark Johnston dsl_origindir_init(zfs); 248240afd8cSMark Johnston 249240afd8cSMark Johnston /* 250240afd8cSMark Johnston * Go through the list of user-specified datasets and create DSL objects 251240afd8cSMark Johnston * for them. 252240afd8cSMark Johnston */ 253240afd8cSMark Johnston STAILQ_FOREACH(d, &zfs->datasetdescs, next) { 254240afd8cSMark Johnston char *dsname, *next, *params, *param, *nextparam; 255240afd8cSMark Johnston 256240afd8cSMark Johnston params = d->params; 257240afd8cSMark Johnston dsname = strsep(¶ms, dspropdelim); 258240afd8cSMark Johnston 259240afd8cSMark Johnston if (strcmp(dsname, zfs->poolname) == 0) { 260240afd8cSMark Johnston /* 261240afd8cSMark Johnston * This is the root dataset; it's already created, so 262240afd8cSMark Johnston * we're just setting options. 263240afd8cSMark Johnston */ 264240afd8cSMark Johnston dir = zfs->rootdsldir; 265240afd8cSMark Johnston } else { 266240afd8cSMark Johnston /* 267240afd8cSMark Johnston * This dataset must be a child of the root dataset. 268240afd8cSMark Johnston */ 269240afd8cSMark Johnston if (strstr(dsname, zfs->poolname) != dsname || 270240afd8cSMark Johnston (next = strchr(dsname, '/')) == NULL || 271240afd8cSMark Johnston (size_t)(next - dsname) != strlen(zfs->poolname)) { 272240afd8cSMark Johnston errx(1, "dataset `%s' must be a child of `%s'", 273240afd8cSMark Johnston dsname, zfs->poolname); 274240afd8cSMark Johnston } 275240afd8cSMark Johnston dir = dsl_dir_alloc(zfs, dsname); 276240afd8cSMark Johnston dir->headds = dsl_dataset_alloc(zfs, dir); 277240afd8cSMark Johnston } 278240afd8cSMark Johnston 279240afd8cSMark Johnston for (nextparam = param = params; nextparam != NULL;) { 280240afd8cSMark Johnston char *key, *val; 281240afd8cSMark Johnston 282240afd8cSMark Johnston param = strsep(&nextparam, dspropdelim); 283240afd8cSMark Johnston 284240afd8cSMark Johnston key = val = param; 285240afd8cSMark Johnston key = strsep(&val, "="); 286240afd8cSMark Johnston dsl_dir_set_prop(zfs, dir, key, val); 287240afd8cSMark Johnston } 288240afd8cSMark Johnston } 289240afd8cSMark Johnston 290240afd8cSMark Johnston /* 291240afd8cSMark Johnston * Set the root dataset's mount point if the user didn't override the 292240afd8cSMark Johnston * default. 293240afd8cSMark Johnston */ 294240afd8cSMark Johnston if (nvpair_find(zfs->rootdsldir->propsnv, "mountpoint") == NULL) { 295240afd8cSMark Johnston nvlist_add_string(zfs->rootdsldir->propsnv, "mountpoint", 296240afd8cSMark Johnston zfs->rootpath); 297240afd8cSMark Johnston } 298240afd8cSMark Johnston } 299240afd8cSMark Johnston 300240afd8cSMark Johnston uint64_t 301240afd8cSMark Johnston dsl_dir_id(zfs_dsl_dir_t *dir) 302240afd8cSMark Johnston { 303240afd8cSMark Johnston return (dir->dirid); 304240afd8cSMark Johnston } 305240afd8cSMark Johnston 306240afd8cSMark Johnston uint64_t 307240afd8cSMark Johnston dsl_dir_dataset_id(zfs_dsl_dir_t *dir) 308240afd8cSMark Johnston { 309240afd8cSMark Johnston return (dir->headds->dsid); 310240afd8cSMark Johnston } 311240afd8cSMark Johnston 312240afd8cSMark Johnston static void 313240afd8cSMark Johnston dsl_dir_foreach_post(zfs_opt_t *zfs, zfs_dsl_dir_t *dsldir, 314240afd8cSMark Johnston void (*cb)(zfs_opt_t *, zfs_dsl_dir_t *, void *), void *arg) 315240afd8cSMark Johnston { 316240afd8cSMark Johnston zfs_dsl_dir_t *cdsldir; 317240afd8cSMark Johnston 318240afd8cSMark Johnston STAILQ_FOREACH(cdsldir, &dsldir->children, next) { 319240afd8cSMark Johnston dsl_dir_foreach_post(zfs, cdsldir, cb, arg); 320240afd8cSMark Johnston } 321240afd8cSMark Johnston cb(zfs, dsldir, arg); 322240afd8cSMark Johnston } 323240afd8cSMark Johnston 324240afd8cSMark Johnston /* 325240afd8cSMark Johnston * Used when the caller doesn't care about the order one way or another. 326240afd8cSMark Johnston */ 327240afd8cSMark Johnston void 328240afd8cSMark Johnston dsl_dir_foreach(zfs_opt_t *zfs, zfs_dsl_dir_t *dsldir, 329240afd8cSMark Johnston void (*cb)(zfs_opt_t *, zfs_dsl_dir_t *, void *), void *arg) 330240afd8cSMark Johnston { 331240afd8cSMark Johnston dsl_dir_foreach_post(zfs, dsldir, cb, arg); 332240afd8cSMark Johnston } 333240afd8cSMark Johnston 334240afd8cSMark Johnston const char * 335240afd8cSMark Johnston dsl_dir_fullname(const zfs_dsl_dir_t *dir) 336240afd8cSMark Johnston { 337240afd8cSMark Johnston return (dir->fullname); 338240afd8cSMark Johnston } 339240afd8cSMark Johnston 340240afd8cSMark Johnston /* 341240afd8cSMark Johnston * Create a DSL directory, which is effectively an entry in the ZFS namespace. 342240afd8cSMark Johnston * We always create a root DSL directory, whose name is the pool's name, and 343240afd8cSMark Johnston * several metadata directories. 344240afd8cSMark Johnston * 345240afd8cSMark Johnston * Each directory has two ZAP objects, one pointing to child directories, and 346240afd8cSMark Johnston * one for properties (which are inherited by children unless overridden). 347240afd8cSMark Johnston * Directories typically reference a DSL dataset, the "head dataset", which 348240afd8cSMark Johnston * points to an object set. 349240afd8cSMark Johnston */ 350240afd8cSMark Johnston static zfs_dsl_dir_t * 351240afd8cSMark Johnston dsl_dir_alloc(zfs_opt_t *zfs, const char *name) 352240afd8cSMark Johnston { 353240afd8cSMark Johnston zfs_dsl_dir_list_t l, *lp; 354240afd8cSMark Johnston zfs_dsl_dir_t *dir, *parent; 355240afd8cSMark Johnston dnode_phys_t *dnode; 356240afd8cSMark Johnston char *dirname, *nextdir, *origname; 357240afd8cSMark Johnston uint64_t childid, propsid; 358240afd8cSMark Johnston 359240afd8cSMark Johnston dir = ecalloc(1, sizeof(*dir)); 360240afd8cSMark Johnston 361240afd8cSMark Johnston dnode = objset_dnode_bonus_alloc(zfs->mos, DMU_OT_DSL_DIR, 362240afd8cSMark Johnston DMU_OT_DSL_DIR, sizeof(dsl_dir_phys_t), &dir->dirid); 363240afd8cSMark Johnston dir->phys = (dsl_dir_phys_t *)DN_BONUS(dnode); 364240afd8cSMark Johnston 365240afd8cSMark Johnston dnode = objset_dnode_alloc(zfs->mos, DMU_OT_DSL_PROPS, &propsid); 366240afd8cSMark Johnston dir->propszap = zap_alloc(zfs->mos, dnode); 367240afd8cSMark Johnston 368240afd8cSMark Johnston dnode = objset_dnode_alloc(zfs->mos, DMU_OT_DSL_DIR_CHILD_MAP, 369240afd8cSMark Johnston &childid); 370240afd8cSMark Johnston dir->childzap = zap_alloc(zfs->mos, dnode); 371240afd8cSMark Johnston 372240afd8cSMark Johnston dir->propsnv = nvlist_create(NV_UNIQUE_NAME); 373240afd8cSMark Johnston STAILQ_INIT(&dir->children); 374240afd8cSMark Johnston 375240afd8cSMark Johnston dir->phys->dd_child_dir_zapobj = childid; 376240afd8cSMark Johnston dir->phys->dd_props_zapobj = propsid; 377240afd8cSMark Johnston 378240afd8cSMark Johnston if (name == NULL) { 379240afd8cSMark Johnston /* 380240afd8cSMark Johnston * This is the root DSL directory. 381240afd8cSMark Johnston */ 382240afd8cSMark Johnston dir->name = estrdup(zfs->poolname); 383240afd8cSMark Johnston dir->fullname = estrdup(zfs->poolname); 384240afd8cSMark Johnston dir->parent = NULL; 385240afd8cSMark Johnston dir->phys->dd_parent_obj = 0; 386240afd8cSMark Johnston 387240afd8cSMark Johnston assert(zfs->rootdsldir == NULL); 388240afd8cSMark Johnston zfs->rootdsldir = dir; 389240afd8cSMark Johnston return (dir); 390240afd8cSMark Johnston } 391240afd8cSMark Johnston 392240afd8cSMark Johnston /* 393240afd8cSMark Johnston * Insert the new directory into the hierarchy. Currently this must be 394240afd8cSMark Johnston * done in order, e.g., when creating pool/a/b, pool/a must already 395240afd8cSMark Johnston * exist. 396240afd8cSMark Johnston */ 397240afd8cSMark Johnston STAILQ_INIT(&l); 398240afd8cSMark Johnston STAILQ_INSERT_HEAD(&l, zfs->rootdsldir, next); 399240afd8cSMark Johnston origname = dirname = nextdir = estrdup(name); 400240afd8cSMark Johnston for (lp = &l;; lp = &parent->children) { 401240afd8cSMark Johnston dirname = strsep(&nextdir, "/"); 402240afd8cSMark Johnston if (nextdir == NULL) 403240afd8cSMark Johnston break; 404240afd8cSMark Johnston 405240afd8cSMark Johnston STAILQ_FOREACH(parent, lp, next) { 406240afd8cSMark Johnston if (strcmp(parent->name, dirname) == 0) 407240afd8cSMark Johnston break; 408240afd8cSMark Johnston } 409240afd8cSMark Johnston if (parent == NULL) { 410240afd8cSMark Johnston errx(1, "no parent at `%s' for filesystem `%s'", 411240afd8cSMark Johnston dirname, name); 412240afd8cSMark Johnston } 413240afd8cSMark Johnston } 414240afd8cSMark Johnston 415240afd8cSMark Johnston dir->fullname = estrdup(name); 416240afd8cSMark Johnston dir->name = estrdup(dirname); 417240afd8cSMark Johnston free(origname); 418240afd8cSMark Johnston STAILQ_INSERT_TAIL(lp, dir, next); 419240afd8cSMark Johnston zap_add_uint64(parent->childzap, dir->name, dir->dirid); 420240afd8cSMark Johnston 421240afd8cSMark Johnston dir->parent = parent; 422240afd8cSMark Johnston dir->phys->dd_parent_obj = parent->dirid; 423240afd8cSMark Johnston return (dir); 424240afd8cSMark Johnston } 425240afd8cSMark Johnston 426240afd8cSMark Johnston void 4274f816f5bSMark Johnston dsl_dir_size_add(zfs_dsl_dir_t *dir, uint64_t bytes) 428240afd8cSMark Johnston { 4294f816f5bSMark Johnston dir->phys->dd_used_bytes += bytes; 4304f816f5bSMark Johnston dir->phys->dd_compressed_bytes += bytes; 4314f816f5bSMark Johnston dir->phys->dd_uncompressed_bytes += bytes; 432240afd8cSMark Johnston } 433240afd8cSMark Johnston 434240afd8cSMark Johnston /* 435240afd8cSMark Johnston * Convert dataset properties into entries in the DSL directory's properties 436240afd8cSMark Johnston * ZAP. 437240afd8cSMark Johnston */ 438240afd8cSMark Johnston static void 439240afd8cSMark Johnston dsl_dir_finalize_props(zfs_dsl_dir_t *dir) 440240afd8cSMark Johnston { 441240afd8cSMark Johnston for (nvp_header_t *nvh = NULL; 442240afd8cSMark Johnston (nvh = nvlist_next_nvpair(dir->propsnv, nvh)) != NULL;) { 443240afd8cSMark Johnston nv_string_t *nvname; 444240afd8cSMark Johnston nv_pair_data_t *nvdata; 445e2259837SMark Johnston char *name; 446240afd8cSMark Johnston 447240afd8cSMark Johnston nvname = (nv_string_t *)(nvh + 1); 448240afd8cSMark Johnston nvdata = (nv_pair_data_t *)(&nvname->nv_data[0] + 449240afd8cSMark Johnston NV_ALIGN4(nvname->nv_size)); 450240afd8cSMark Johnston 451240afd8cSMark Johnston name = nvstring_get(nvname); 452240afd8cSMark Johnston switch (nvdata->nv_type) { 453240afd8cSMark Johnston case DATA_TYPE_UINT64: { 454240afd8cSMark Johnston uint64_t val; 455240afd8cSMark Johnston 456240afd8cSMark Johnston memcpy(&val, &nvdata->nv_data[0], sizeof(uint64_t)); 457240afd8cSMark Johnston zap_add_uint64(dir->propszap, name, val); 458240afd8cSMark Johnston break; 459240afd8cSMark Johnston } 460240afd8cSMark Johnston case DATA_TYPE_STRING: { 461240afd8cSMark Johnston nv_string_t *nvstr; 462e2259837SMark Johnston char *val; 463240afd8cSMark Johnston 464240afd8cSMark Johnston nvstr = (nv_string_t *)&nvdata->nv_data[0]; 465e2259837SMark Johnston val = nvstring_get(nvstr); 466e2259837SMark Johnston zap_add_string(dir->propszap, name, val); 467e2259837SMark Johnston free(val); 468240afd8cSMark Johnston break; 469240afd8cSMark Johnston } 470240afd8cSMark Johnston default: 471240afd8cSMark Johnston assert(0); 472240afd8cSMark Johnston } 473e2259837SMark Johnston free(name); 474240afd8cSMark Johnston } 475240afd8cSMark Johnston } 476240afd8cSMark Johnston 477240afd8cSMark Johnston static void 478240afd8cSMark Johnston dsl_dir_finalize(zfs_opt_t *zfs, zfs_dsl_dir_t *dir, void *arg __unused) 479240afd8cSMark Johnston { 480240afd8cSMark Johnston char key[32]; 481240afd8cSMark Johnston zfs_dsl_dir_t *cdir; 482240afd8cSMark Johnston dnode_phys_t *snapnames; 483240afd8cSMark Johnston zfs_dsl_dataset_t *headds; 484240afd8cSMark Johnston zfs_objset_t *os; 485240afd8cSMark Johnston uint64_t bytes, snapnamesid; 486240afd8cSMark Johnston 487240afd8cSMark Johnston dsl_dir_finalize_props(dir); 488240afd8cSMark Johnston zap_write(zfs, dir->propszap); 489240afd8cSMark Johnston zap_write(zfs, dir->childzap); 490240afd8cSMark Johnston 491240afd8cSMark Johnston headds = dir->headds; 492240afd8cSMark Johnston if (headds == NULL) 493240afd8cSMark Johnston return; 494240afd8cSMark Johnston os = headds->os; 495240afd8cSMark Johnston if (os == NULL) 496240afd8cSMark Johnston return; 497240afd8cSMark Johnston 498240afd8cSMark Johnston snapnames = objset_dnode_alloc(zfs->mos, DMU_OT_DSL_DS_SNAP_MAP, 499240afd8cSMark Johnston &snapnamesid); 500240afd8cSMark Johnston zap_write(zfs, zap_alloc(zfs->mos, snapnames)); 501240afd8cSMark Johnston 502240afd8cSMark Johnston dir->phys->dd_head_dataset_obj = headds->dsid; 503240afd8cSMark Johnston dir->phys->dd_clone_parent_obj = zfs->snapds->dsid; 504240afd8cSMark Johnston headds->phys->ds_prev_snap_obj = zfs->snapds->dsid; 505240afd8cSMark Johnston headds->phys->ds_snapnames_zapobj = snapnamesid; 506240afd8cSMark Johnston objset_root_blkptr_copy(os, &headds->phys->ds_bp); 507240afd8cSMark Johnston 508240afd8cSMark Johnston zfs->snapds->phys->ds_num_children++; 509240afd8cSMark Johnston snprintf(key, sizeof(key), "%jx", (uintmax_t)headds->dsid); 510240afd8cSMark Johnston zap_add_uint64(zfs->cloneszap, key, headds->dsid); 511240afd8cSMark Johnston 512240afd8cSMark Johnston bytes = objset_space(os); 513240afd8cSMark Johnston headds->phys->ds_used_bytes = bytes; 514240afd8cSMark Johnston headds->phys->ds_uncompressed_bytes = bytes; 515240afd8cSMark Johnston headds->phys->ds_compressed_bytes = bytes; 516240afd8cSMark Johnston 5174f816f5bSMark Johnston STAILQ_FOREACH(cdir, &dir->children, next) { 5184f816f5bSMark Johnston /* 5194f816f5bSMark Johnston * The root directory needs a special case: the amount of 5204f816f5bSMark Johnston * space used for the MOS isn't known until everything else is 5214f816f5bSMark Johnston * finalized, so it can't be accounted in the MOS directory's 5224f816f5bSMark Johnston * parent until then. 5234f816f5bSMark Johnston */ 5244f816f5bSMark Johnston if (dir == zfs->rootdsldir && cdir == zfs->mosdsldir) 5254f816f5bSMark Johnston continue; 526240afd8cSMark Johnston bytes += cdir->phys->dd_used_bytes; 5274f816f5bSMark Johnston } 5284f816f5bSMark Johnston dsl_dir_size_add(dir, bytes); 529240afd8cSMark Johnston } 530240afd8cSMark Johnston 531240afd8cSMark Johnston void 532240afd8cSMark Johnston dsl_write(zfs_opt_t *zfs) 533240afd8cSMark Johnston { 534240afd8cSMark Johnston zfs_zap_t *snapnameszap; 535240afd8cSMark Johnston dnode_phys_t *snapnames; 536240afd8cSMark Johnston uint64_t snapmapid; 537240afd8cSMark Johnston 538240afd8cSMark Johnston /* 539240afd8cSMark Johnston * Perform accounting, starting from the leaves of the DSL directory 540240afd8cSMark Johnston * tree. Accounting for $MOS is done later, once we've finished 541240afd8cSMark Johnston * allocating space. 542240afd8cSMark Johnston */ 543240afd8cSMark Johnston dsl_dir_foreach_post(zfs, zfs->rootdsldir, dsl_dir_finalize, NULL); 544240afd8cSMark Johnston 545240afd8cSMark Johnston snapnames = objset_dnode_alloc(zfs->mos, DMU_OT_DSL_DS_SNAP_MAP, 546240afd8cSMark Johnston &snapmapid); 547240afd8cSMark Johnston snapnameszap = zap_alloc(zfs->mos, snapnames); 548240afd8cSMark Johnston zap_add_uint64(snapnameszap, "$ORIGIN", zfs->snapds->dsid); 549240afd8cSMark Johnston zap_write(zfs, snapnameszap); 550240afd8cSMark Johnston 551240afd8cSMark Johnston zfs->origindsldir->phys->dd_head_dataset_obj = zfs->originds->dsid; 552240afd8cSMark Johnston zfs->originds->phys->ds_prev_snap_obj = zfs->snapds->dsid; 553240afd8cSMark Johnston zfs->originds->phys->ds_snapnames_zapobj = snapmapid; 554240afd8cSMark Johnston 555240afd8cSMark Johnston zfs->snapds->phys->ds_next_snap_obj = zfs->originds->dsid; 556240afd8cSMark Johnston assert(zfs->snapds->phys->ds_num_children > 0); 557240afd8cSMark Johnston zfs->snapds->phys->ds_num_children++; 558240afd8cSMark Johnston 559240afd8cSMark Johnston zap_write(zfs, zfs->cloneszap); 560240afd8cSMark Johnston 561240afd8cSMark Johnston /* XXX-MJ dirs and datasets are leaked */ 562240afd8cSMark Johnston } 563240afd8cSMark Johnston 564240afd8cSMark Johnston void 565240afd8cSMark Johnston dsl_dir_dataset_write(zfs_opt_t *zfs, zfs_objset_t *os, zfs_dsl_dir_t *dir) 566240afd8cSMark Johnston { 567240afd8cSMark Johnston dir->headds->os = os; 568240afd8cSMark Johnston objset_write(zfs, os); 569240afd8cSMark Johnston } 570240afd8cSMark Johnston 571240afd8cSMark Johnston bool 572240afd8cSMark Johnston dsl_dir_has_dataset(zfs_dsl_dir_t *dir) 573240afd8cSMark Johnston { 574240afd8cSMark Johnston return (dir->headds != NULL); 575240afd8cSMark Johnston } 576240afd8cSMark Johnston 577240afd8cSMark Johnston bool 578240afd8cSMark Johnston dsl_dir_dataset_has_objset(zfs_dsl_dir_t *dir) 579240afd8cSMark Johnston { 580240afd8cSMark Johnston return (dsl_dir_has_dataset(dir) && dir->headds->os != NULL); 581240afd8cSMark Johnston } 582240afd8cSMark Johnston 583240afd8cSMark Johnston static zfs_dsl_dataset_t * 584240afd8cSMark Johnston dsl_dataset_alloc(zfs_opt_t *zfs, zfs_dsl_dir_t *dir) 585240afd8cSMark Johnston { 586240afd8cSMark Johnston zfs_dsl_dataset_t *ds; 587240afd8cSMark Johnston dnode_phys_t *dnode; 588240afd8cSMark Johnston uint64_t deadlistid; 589240afd8cSMark Johnston 590240afd8cSMark Johnston ds = ecalloc(1, sizeof(*ds)); 591240afd8cSMark Johnston 592240afd8cSMark Johnston dnode = objset_dnode_bonus_alloc(zfs->mos, DMU_OT_DSL_DATASET, 593240afd8cSMark Johnston DMU_OT_DSL_DATASET, sizeof(dsl_dataset_phys_t), &ds->dsid); 594240afd8cSMark Johnston ds->phys = (dsl_dataset_phys_t *)DN_BONUS(dnode); 595240afd8cSMark Johnston 596240afd8cSMark Johnston dnode = objset_dnode_bonus_alloc(zfs->mos, DMU_OT_DEADLIST, 597240afd8cSMark Johnston DMU_OT_DEADLIST_HDR, sizeof(dsl_deadlist_phys_t), &deadlistid); 598240afd8cSMark Johnston zap_write(zfs, zap_alloc(zfs->mos, dnode)); 599240afd8cSMark Johnston 600240afd8cSMark Johnston ds->phys->ds_dir_obj = dir->dirid; 601240afd8cSMark Johnston ds->phys->ds_deadlist_obj = deadlistid; 602240afd8cSMark Johnston ds->phys->ds_creation_txg = TXG - 1; 603240afd8cSMark Johnston if (ds != zfs->snapds) 604240afd8cSMark Johnston ds->phys->ds_prev_snap_txg = TXG - 1; 605240afd8cSMark Johnston ds->phys->ds_guid = ((uint64_t)random() << 32) | random(); 606240afd8cSMark Johnston ds->dir = dir; 607240afd8cSMark Johnston 608240afd8cSMark Johnston return (ds); 609240afd8cSMark Johnston } 610