static void dsl_dataset_user_release_sync(void *arg, dmu_tx_t *tx) { dsl_dataset_user_release_arg_t *ddura = arg; dsl_holdfunc_t *holdfunc = ddura->ddura_holdfunc; dsl_pool_t *dp = dmu_tx_pool(tx); ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock)); for (nvpair_t *pair = nvlist_next_nvpair(ddura->ddura_chkholds, NULL); pair != NULL; pair = nvlist_next_nvpair(ddura->ddura_chkholds, pair)) { dsl_dataset_t *ds; const char *name = nvpair_name(pair); VERIFY0(holdfunc(dp, name, FTAG, &ds)); dsl_dataset_user_release_sync_one(ds, fnvpair_value_nvlist(pair), tx); if (nvlist_exists(ddura->ddura_todelete, name)) { ASSERT(ds->ds_userrefs == 0 && ds->ds_phys->ds_num_children == 1 && DS_IS_DEFER_DESTROY(ds)); dsl_destroy_snapshot_sync_impl(ds, B_FALSE, tx); } dsl_dataset_rele(ds, FTAG); } }
static int zcp_props_iter(lua_State *state) { char *source, *val; nvlist_t *nvprop; nvlist_t **props = lua_touserdata(state, lua_upvalueindex(1)); nvpair_t *pair = lua_touserdata(state, lua_upvalueindex(2)); do { pair = nvlist_next_nvpair(*props, pair); if (pair == NULL) { fnvlist_free(*props); *props = NULL; return (0); } } while (!zfs_prop_user(nvpair_name(pair))); lua_pushlightuserdata(state, pair); lua_replace(state, lua_upvalueindex(2)); nvprop = fnvpair_value_nvlist(pair); val = fnvlist_lookup_string(nvprop, ZPROP_VALUE); source = fnvlist_lookup_string(nvprop, ZPROP_SOURCE); (void) lua_pushstring(state, nvpair_name(pair)); (void) lua_pushstring(state, val); (void) lua_pushstring(state, source); return (3); }
static boolean_t nvlist_equal(nvlist_t *nvla, nvlist_t *nvlb) { if (fnvlist_num_pairs(nvla) != fnvlist_num_pairs(nvlb)) return (B_FALSE); /* * The nvlists have the same number of pairs and keys are unique, so * if every key in A is also in B and assigned to the same value, the * lists are identical. */ for (nvpair_t *pair = nvlist_next_nvpair(nvla, NULL); pair != NULL; pair = nvlist_next_nvpair(nvla, pair)) { char *key = nvpair_name(pair); if (!nvlist_exists(nvlb, key)) return (B_FALSE); if (nvpair_type(pair) != nvpair_type(fnvlist_lookup_nvpair(nvlb, key))) return (B_FALSE); switch (nvpair_type(pair)) { case DATA_TYPE_BOOLEAN_VALUE: if (fnvpair_value_boolean_value(pair) != fnvlist_lookup_boolean_value(nvlb, key)) { return (B_FALSE); } break; case DATA_TYPE_STRING: if (strcmp(fnvpair_value_string(pair), fnvlist_lookup_string(nvlb, key))) { return (B_FALSE); } break; case DATA_TYPE_INT64: if (fnvpair_value_int64(pair) != fnvlist_lookup_int64(nvlb, key)) { return (B_FALSE); } break; case DATA_TYPE_NVLIST: if (!nvlist_equal(fnvpair_value_nvlist(pair), fnvlist_lookup_nvlist(nvlb, key))) { return (B_FALSE); } break; default: (void) printf("Unexpected type for nvlist_equal\n"); return (B_FALSE); } } return (B_TRUE); }
/* * Iterate over all bookmarks */ int zfs_iter_bookmarks(zfs_handle_t *zhp, zfs_iter_f func, void *data) { zfs_handle_t *nzhp; nvlist_t *props = NULL; nvlist_t *bmarks = NULL; int err; nvpair_t *pair; if ((zfs_get_type(zhp) & (ZFS_TYPE_SNAPSHOT | ZFS_TYPE_BOOKMARK)) != 0) return (0); /* Setup the requested properties nvlist. */ props = fnvlist_alloc(); fnvlist_add_boolean(props, zfs_prop_to_name(ZFS_PROP_GUID)); fnvlist_add_boolean(props, zfs_prop_to_name(ZFS_PROP_CREATETXG)); fnvlist_add_boolean(props, zfs_prop_to_name(ZFS_PROP_CREATION)); /* Allocate an nvlist to hold the bookmarks. */ bmarks = fnvlist_alloc(); if ((err = lzc_get_bookmarks(zhp->zfs_name, props, &bmarks)) != 0) goto out; for (pair = nvlist_next_nvpair(bmarks, NULL); pair != NULL; pair = nvlist_next_nvpair(bmarks, pair)) { char name[ZFS_MAXNAMELEN]; char *bmark_name; nvlist_t *bmark_props; bmark_name = nvpair_name(pair); bmark_props = fnvpair_value_nvlist(pair); (void) snprintf(name, sizeof (name), "%s#%s", zhp->zfs_name, bmark_name); nzhp = make_bookmark_handle(zhp, name, bmark_props); if (nzhp == NULL) continue; if ((err = func(nzhp, data)) != 0) goto out; } out: fnvlist_free(props); fnvlist_free(bmarks); return (err); }
static void dsl_deleg_set_sync(void *arg, dmu_tx_t *tx) { dsl_deleg_arg_t *dda = arg; dsl_dir_t *dd; dsl_pool_t *dp = dmu_tx_pool(tx); objset_t *mos = dp->dp_meta_objset; nvpair_t *whopair = NULL; uint64_t zapobj; VERIFY0(dsl_dir_hold(dp, dda->dda_name, FTAG, &dd, NULL)); zapobj = dd->dd_phys->dd_deleg_zapobj; if (zapobj == 0) { dmu_buf_will_dirty(dd->dd_dbuf, tx); zapobj = dd->dd_phys->dd_deleg_zapobj = zap_create(mos, DMU_OT_DSL_PERMS, DMU_OT_NONE, 0, tx); } while ((whopair = nvlist_next_nvpair(dda->dda_nvlist, whopair))) { const char *whokey = nvpair_name(whopair); nvlist_t *perms; nvpair_t *permpair = NULL; uint64_t jumpobj; perms = fnvpair_value_nvlist(whopair); if (zap_lookup(mos, zapobj, whokey, 8, 1, &jumpobj) != 0) { jumpobj = zap_create_link(mos, DMU_OT_DSL_PERMS, zapobj, whokey, tx); } while ((permpair = nvlist_next_nvpair(perms, permpair))) { const char *perm = nvpair_name(permpair); uint64_t n = 0; VERIFY(zap_update(mos, jumpobj, perm, 8, 1, &n, tx) == 0); spa_history_log_internal_dd(dd, "permission update", tx, "%s %s", whokey, perm); } } dsl_dir_rele(dd, FTAG); }
static void dsl_dataset_user_release_sync(void *arg, dmu_tx_t *tx) { dsl_dataset_user_release_arg_t *ddura = arg; dsl_pool_t *dp = dmu_tx_pool(tx); nvpair_t *pair; for (pair = nvlist_next_nvpair(ddura->ddura_holds, NULL); pair != NULL; pair = nvlist_next_nvpair(ddura->ddura_holds, pair)) { dsl_dataset_t *ds; VERIFY0(dsl_dataset_hold(dp, nvpair_name(pair), FTAG, &ds)); dsl_dataset_user_release_sync_one(ds, fnvpair_value_nvlist(pair), tx); if (nvlist_exists(ddura->ddura_todelete, nvpair_name(pair))) { ASSERT(ds->ds_userrefs == 0 && ds->ds_phys->ds_num_children == 1 && DS_IS_DEFER_DESTROY(ds)); dsl_destroy_snapshot_sync_impl(ds, B_FALSE, tx); } dsl_dataset_rele(ds, FTAG); } }
/* * Given a cache file, return the contents as a list of importable pools. * poolname or guid (but not both) are provided by the caller when trying * to import a specific pool. */ nvlist_t * zpool_find_import_cached(libzfs_handle_t *hdl, const char *cachefile, char *poolname, uint64_t guid) { char *buf; int fd; struct stat statbuf; nvlist_t *raw, *src, *dst; nvlist_t *pools; nvpair_t *elem; char *name; uint64_t this_guid; boolean_t active; verify(poolname == NULL || guid == 0); if ((fd = open(cachefile, O_RDONLY)) < 0) { zfs_error_aux(hdl, "%s", strerror(errno)); (void) zfs_error(hdl, EZFS_BADCACHE, dgettext(TEXT_DOMAIN, "failed to open cache file")); return (NULL); } if (fstat(fd, &statbuf) != 0) { zfs_error_aux(hdl, "%s", strerror(errno)); (void) close(fd); (void) zfs_error(hdl, EZFS_BADCACHE, dgettext(TEXT_DOMAIN, "failed to get size of cache file")); return (NULL); } if ((buf = zfs_alloc(hdl, statbuf.st_size)) == NULL) { (void) close(fd); return (NULL); } if (read(fd, buf, statbuf.st_size) != statbuf.st_size) { (void) close(fd); free(buf); (void) zfs_error(hdl, EZFS_BADCACHE, dgettext(TEXT_DOMAIN, "failed to read cache file contents")); return (NULL); } (void) close(fd); if (nvlist_unpack(buf, statbuf.st_size, &raw, 0) != 0) { free(buf); (void) zfs_error(hdl, EZFS_BADCACHE, dgettext(TEXT_DOMAIN, "invalid or corrupt cache file contents")); return (NULL); } free(buf); /* * Go through and get the current state of the pools and refresh their * state. */ if (nvlist_alloc(&pools, 0, 0) != 0) { (void) no_memory(hdl); nvlist_free(raw); return (NULL); } elem = NULL; while ((elem = nvlist_next_nvpair(raw, elem)) != NULL) { src = fnvpair_value_nvlist(elem); name = fnvlist_lookup_string(src, ZPOOL_CONFIG_POOL_NAME); if (poolname != NULL && strcmp(poolname, name) != 0) continue; this_guid = fnvlist_lookup_uint64(src, ZPOOL_CONFIG_POOL_GUID); if (guid != 0 && guid != this_guid) continue; if (pool_active(hdl, name, this_guid, &active) != 0) { nvlist_free(raw); nvlist_free(pools); return (NULL); } if (active) continue; if (nvlist_add_string(src, ZPOOL_CONFIG_CACHEFILE, cachefile) != 0) { (void) no_memory(hdl); nvlist_free(raw); nvlist_free(pools); return (NULL); } if ((dst = refresh_config(hdl, src)) == NULL) { nvlist_free(raw); nvlist_free(pools); return (NULL); } if (nvlist_add_nvlist(pools, nvpair_name(elem), dst) != 0) { (void) no_memory(hdl); nvlist_free(dst); nvlist_free(raw); nvlist_free(pools); return (NULL); } nvlist_free(dst); } nvlist_free(raw); return (pools); }
/* * Called when the module is first loaded, this routine loads the configuration * file into the SPA namespace. It does not actually open or load the pools; it * only populates the namespace. */ void spa_config_load(void) { void *buf = NULL; nvlist_t *nvlist, *child; nvpair_t *nvpair; char *pathname; struct _buf *file; uint64_t fsize; #ifdef _KERNEL if (zfs_autoimport_disable) return; #endif /* * Open the configuration file. */ pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP); (void) snprintf(pathname, MAXPATHLEN, "%s%s", (rootdir != NULL) ? "./" : "", spa_config_path); file = kobj_open_file(pathname); kmem_free(pathname, MAXPATHLEN); if (file == (struct _buf *)-1) return; if (kobj_get_filesize(file, &fsize) != 0) goto out; buf = kmem_alloc(fsize, KM_SLEEP); /* * Read the nvlist from the file. */ if (kobj_read_file(file, buf, fsize, 0) < 0) goto out; /* * Unpack the nvlist. */ if (nvlist_unpack(buf, fsize, &nvlist, KM_SLEEP) != 0) goto out; /* * Iterate over all elements in the nvlist, creating a new spa_t for * each one with the specified configuration. */ mutex_enter(&spa_namespace_lock); nvpair = NULL; while ((nvpair = nvlist_next_nvpair(nvlist, nvpair)) != NULL) { if (nvpair_type(nvpair) != DATA_TYPE_NVLIST) continue; child = fnvpair_value_nvlist(nvpair); if (spa_lookup(nvpair_name(nvpair)) != NULL) continue; (void) spa_add(nvpair_name(nvpair), child, NULL); } mutex_exit(&spa_namespace_lock); nvlist_free(nvlist); out: if (buf != NULL) kmem_free(buf, fsize); kobj_close_file(file); }
/* * Dump a JSON-formatted representation of an nvlist to the provided FILE *. * This routine does not output any new-lines or additional whitespace other * than that contained in strings, nor does it call fflush(3C). */ int bunyan_nvlist_print_json(FILE *fp, nvlist_t *nvl) { nvpair_t *curr; boolean_t first = B_TRUE; FPRINTF(fp, "{"); for (curr = nvlist_next_nvpair(nvl, NULL); curr; curr = nvlist_next_nvpair(nvl, curr)) { data_type_t type = nvpair_type(curr); if (!first) FPRINTF(fp, ","); else first = B_FALSE; if (bunyan_nvlist_print_json_string(fp, nvpair_name(curr)) == -1) { return (-1); } FPRINTF(fp, ":"); switch (type) { case DATA_TYPE_STRING: { char *string = fnvpair_value_string(curr); if (bunyan_nvlist_print_json_string(fp, string) == -1) return (-1); break; } case DATA_TYPE_BOOLEAN: { FPRINTF(fp, "true"); break; } case DATA_TYPE_BOOLEAN_VALUE: { FPRINTF(fp, "%s", fnvpair_value_boolean_value(curr) == B_TRUE ? "true" : "false"); break; } case DATA_TYPE_BYTE: { FPRINTF(fp, "%hhu", fnvpair_value_byte(curr)); break; } case DATA_TYPE_INT8: { FPRINTF(fp, "%hhd", fnvpair_value_int8(curr)); break; } case DATA_TYPE_UINT8: { FPRINTF(fp, "%hhu", fnvpair_value_uint8_t(curr)); break; } case DATA_TYPE_INT16: { FPRINTF(fp, "%hd", fnvpair_value_int16(curr)); break; } case DATA_TYPE_UINT16: { FPRINTF(fp, "%hu", fnvpair_value_uint16(curr)); break; } case DATA_TYPE_INT32: { FPRINTF(fp, "%d", fnvpair_value_int32(curr)); break; } case DATA_TYPE_UINT32: { FPRINTF(fp, "%u", fnvpair_value_uint32(curr)); break; } case DATA_TYPE_INT64: { FPRINTF(fp, "%lld", (long long)fnvpair_value_int64(curr)); break; } case DATA_TYPE_UINT64: { FPRINTF(fp, "%llu", (unsigned long long)fnvpair_value_uint64(curr)); break; } case DATA_TYPE_HRTIME: { hrtime_t val; VERIFY0(nvpair_value_hrtime(curr, &val)); FPRINTF(fp, "%llu", (unsigned long long)val); break; } case DATA_TYPE_DOUBLE: { double val; VERIFY0(nvpair_value_double(curr, &val)); FPRINTF(fp, "%f", val); break; } case DATA_TYPE_NVLIST: { if (nvlist_print_json(fp, fnvpair_value_nvlist(curr)) == -1) return (-1); break; } case DATA_TYPE_STRING_ARRAY: { char **val; uint_t valsz, i; VERIFY0(nvpair_value_string_array(curr, &val, &valsz)); FPRINTF(fp, "["); for (i = 0; i < valsz; i++) { if (i > 0) FPRINTF(fp, ","); if (bunyan_nvlist_print_json_string(fp, val[i]) == -1) { return (-1); } } FPRINTF(fp, "]"); break; } case DATA_TYPE_NVLIST_ARRAY: { nvlist_t **val; uint_t valsz, i; VERIFY0(nvpair_value_nvlist_array(curr, &val, &valsz)); FPRINTF(fp, "["); for (i = 0; i < valsz; i++) { if (i > 0) FPRINTF(fp, ","); if (nvlist_print_json(fp, val[i]) == -1) return (-1); } FPRINTF(fp, "]"); break; } case DATA_TYPE_BOOLEAN_ARRAY: { boolean_t *val; uint_t valsz, i; VERIFY0(nvpair_value_boolean_array(curr, &val, &valsz)); FPRINTF(fp, "["); for (i = 0; i < valsz; i++) { if (i > 0) FPRINTF(fp, ","); FPRINTF(fp, val[i] == B_TRUE ? "true" : "false"); } FPRINTF(fp, "]"); break; } case DATA_TYPE_BYTE_ARRAY: { uchar_t *val; uint_t valsz, i; VERIFY0(nvpair_value_byte_array(curr, &val, &valsz)); FPRINTF(fp, "["); for (i = 0; i < valsz; i++) { if (i > 0) FPRINTF(fp, ","); FPRINTF(fp, "%hhu", val[i]); } FPRINTF(fp, "]"); break; } case DATA_TYPE_UINT8_ARRAY: { uint8_t *val; uint_t valsz, i; VERIFY0(nvpair_value_uint8_array(curr, &val, &valsz)); FPRINTF(fp, "["); for (i = 0; i < valsz; i++) { if (i > 0) FPRINTF(fp, ","); FPRINTF(fp, "%hhu", val[i]); } FPRINTF(fp, "]"); break; } case DATA_TYPE_INT8_ARRAY: { int8_t *val; uint_t valsz, i; VERIFY0(nvpair_value_int8_array(curr, &val, &valsz)); FPRINTF(fp, "["); for (i = 0; i < valsz; i++) { if (i > 0) FPRINTF(fp, ","); FPRINTF(fp, "%hd", val[i]); } FPRINTF(fp, "]"); break; } case DATA_TYPE_UINT16_ARRAY: { uint16_t *val; uint_t valsz, i; VERIFY0(nvpair_value_uint16_array(curr, &val, &valsz)); FPRINTF(fp, "["); for (i = 0; i < valsz; i++) { if (i > 0) FPRINTF(fp, ","); FPRINTF(fp, "%hu", val[i]); } FPRINTF(fp, "]"); break; } case DATA_TYPE_INT16_ARRAY: { int16_t *val; uint_t valsz, i; VERIFY0(nvpair_value_int16_array(curr, &val, &valsz)); FPRINTF(fp, "["); for (i = 0; i < valsz; i++) { if (i > 0) FPRINTF(fp, ","); FPRINTF(fp, "%hhd", val[i]); } FPRINTF(fp, "]"); break; } case DATA_TYPE_UINT32_ARRAY: { uint32_t *val; uint_t valsz, i; VERIFY0(nvpair_value_uint32_array(curr, &val, &valsz)); FPRINTF(fp, "["); for (i = 0; i < valsz; i++) { if (i > 0) FPRINTF(fp, ","); FPRINTF(fp, "%u", val[i]); } FPRINTF(fp, "]"); break; } case DATA_TYPE_INT32_ARRAY: { int32_t *val; uint_t valsz, i; VERIFY0(nvpair_value_int32_array(curr, &val, &valsz)); FPRINTF(fp, "["); for (i = 0; i < valsz; i++) { if (i > 0) FPRINTF(fp, ","); FPRINTF(fp, "%d", val[i]); } FPRINTF(fp, "]"); break; } case DATA_TYPE_UINT64_ARRAY: { uint64_t *val; uint_t valsz, i; VERIFY0(nvpair_value_uint64_array(curr, &val, &valsz)); FPRINTF(fp, "["); for (i = 0; i < valsz; i++) { if (i > 0) FPRINTF(fp, ","); FPRINTF(fp, "%llu", (unsigned long long)val[i]); } FPRINTF(fp, "]"); break; } case DATA_TYPE_INT64_ARRAY: { int64_t *val; uint_t valsz, i; VERIFY0(nvpair_value_int64_array(curr, &val, &valsz)); FPRINTF(fp, "["); for (i = 0; i < valsz; i++) { if (i > 0) FPRINTF(fp, ","); FPRINTF(fp, "%lld", (long long)val[i]); } FPRINTF(fp, "]"); break; } case DATA_TYPE_UNKNOWN: return (-1); } } FPRINTF(fp, "}"); return (0); }
/* * Push a Lua object representing the value of "pair" onto the stack. * * Only understands boolean_value, string, int64, nvlist, * string_array, and int64_array type values. For other * types, returns EINVAL, fills in errbuf, and pushes nothing. */ static int zcp_nvpair_value_to_lua(lua_State *state, nvpair_t *pair, char *errbuf, int errbuf_len) { int err = 0; if (pair == NULL) { lua_pushnil(state); return (0); } switch (nvpair_type(pair)) { case DATA_TYPE_BOOLEAN_VALUE: (void) lua_pushboolean(state, fnvpair_value_boolean_value(pair)); break; case DATA_TYPE_STRING: (void) lua_pushstring(state, fnvpair_value_string(pair)); break; case DATA_TYPE_INT64: (void) lua_pushinteger(state, fnvpair_value_int64(pair)); break; case DATA_TYPE_NVLIST: err = zcp_nvlist_to_lua(state, fnvpair_value_nvlist(pair), errbuf, errbuf_len); break; case DATA_TYPE_STRING_ARRAY: { char **strarr; uint_t nelem; (void) nvpair_value_string_array(pair, &strarr, &nelem); lua_newtable(state); for (int i = 0; i < nelem; i++) { (void) lua_pushinteger(state, i + 1); (void) lua_pushstring(state, strarr[i]); (void) lua_settable(state, -3); } break; } case DATA_TYPE_UINT64_ARRAY: { uint64_t *intarr; uint_t nelem; (void) nvpair_value_uint64_array(pair, &intarr, &nelem); lua_newtable(state); for (int i = 0; i < nelem; i++) { (void) lua_pushinteger(state, i + 1); (void) lua_pushinteger(state, intarr[i]); (void) lua_settable(state, -3); } break; } case DATA_TYPE_INT64_ARRAY: { int64_t *intarr; uint_t nelem; (void) nvpair_value_int64_array(pair, &intarr, &nelem); lua_newtable(state); for (int i = 0; i < nelem; i++) { (void) lua_pushinteger(state, i + 1); (void) lua_pushinteger(state, intarr[i]); (void) lua_settable(state, -3); } break; } default: { if (errbuf != NULL) { (void) snprintf(errbuf, errbuf_len, "Unhandled nvpair type %d for key '%s'", nvpair_type(pair), nvpair_name(pair)); } return (EINVAL); } } return (err); }