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