14445fffbSMatthew Ahrens /*
24445fffbSMatthew Ahrens * CDDL HEADER START
34445fffbSMatthew Ahrens *
44445fffbSMatthew Ahrens * The contents of this file are subject to the terms of the
54445fffbSMatthew Ahrens * Common Development and Distribution License (the "License").
64445fffbSMatthew Ahrens * You may not use this file except in compliance with the License.
74445fffbSMatthew Ahrens *
84445fffbSMatthew Ahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
94445fffbSMatthew Ahrens * or http://www.opensolaris.org/os/licensing.
104445fffbSMatthew Ahrens * See the License for the specific language governing permissions
114445fffbSMatthew Ahrens * and limitations under the License.
124445fffbSMatthew Ahrens *
134445fffbSMatthew Ahrens * When distributing Covered Code, include this CDDL HEADER in each
144445fffbSMatthew Ahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
154445fffbSMatthew Ahrens * If applicable, add the following below this CDDL HEADER, with the
164445fffbSMatthew Ahrens * fields enclosed by brackets "[]" replaced with your own identifying
174445fffbSMatthew Ahrens * information: Portions Copyright [yyyy] [name of copyright owner]
184445fffbSMatthew Ahrens *
194445fffbSMatthew Ahrens * CDDL HEADER END
204445fffbSMatthew Ahrens */
214445fffbSMatthew Ahrens
224445fffbSMatthew Ahrens /*
23c4ecba8aSPaul Dagnelie * Copyright (c) 2012, 2020 by Delphix. All rights reserved.
24a7a845e4SSteven Hartland * Copyright (c) 2013 Steven Hartland. All rights reserved.
25c3d26abcSMatthew Ahrens * Copyright (c) 2014 Integros [integros.com]
26a4b8c9aaSAndrew Stormont * Copyright 2017 RackTop Systems.
279c2acf00SAlek Pinchuk * Copyright (c) 2017 Datto Inc.
28*d8f839f9SJason King * Copyright 2020 Joyent, Inc.
294445fffbSMatthew Ahrens */
304445fffbSMatthew Ahrens
314445fffbSMatthew Ahrens /*
324445fffbSMatthew Ahrens * LibZFS_Core (lzc) is intended to replace most functionality in libzfs.
334445fffbSMatthew Ahrens * It has the following characteristics:
344445fffbSMatthew Ahrens *
354445fffbSMatthew Ahrens * - Thread Safe. libzfs_core is accessible concurrently from multiple
364445fffbSMatthew Ahrens * threads. This is accomplished primarily by avoiding global data
374445fffbSMatthew Ahrens * (e.g. caching). Since it's thread-safe, there is no reason for a
384445fffbSMatthew Ahrens * process to have multiple libzfs "instances". Therefore, we store
394445fffbSMatthew Ahrens * our few pieces of data (e.g. the file descriptor) in global
404445fffbSMatthew Ahrens * variables. The fd is reference-counted so that the libzfs_core
414445fffbSMatthew Ahrens * library can be "initialized" multiple times (e.g. by different
424445fffbSMatthew Ahrens * consumers within the same process).
434445fffbSMatthew Ahrens *
444445fffbSMatthew Ahrens * - Committed Interface. The libzfs_core interface will be committed,
454445fffbSMatthew Ahrens * therefore consumers can compile against it and be confident that
464445fffbSMatthew Ahrens * their code will continue to work on future releases of this code.
474445fffbSMatthew Ahrens * Currently, the interface is Evolving (not Committed), but we intend
484445fffbSMatthew Ahrens * to commit to it once it is more complete and we determine that it
494445fffbSMatthew Ahrens * meets the needs of all consumers.
504445fffbSMatthew Ahrens *
514445fffbSMatthew Ahrens * - Programatic Error Handling. libzfs_core communicates errors with
524445fffbSMatthew Ahrens * defined error numbers, and doesn't print anything to stdout/stderr.
534445fffbSMatthew Ahrens *
544445fffbSMatthew Ahrens * - Thin Layer. libzfs_core is a thin layer, marshaling arguments
554445fffbSMatthew Ahrens * to/from the kernel ioctls. There is generally a 1:1 correspondence
564445fffbSMatthew Ahrens * between libzfs_core functions and ioctls to /dev/zfs.
574445fffbSMatthew Ahrens *
584445fffbSMatthew Ahrens * - Clear Atomicity. Because libzfs_core functions are generally 1:1
594445fffbSMatthew Ahrens * with kernel ioctls, and kernel ioctls are general atomic, each
604445fffbSMatthew Ahrens * libzfs_core function is atomic. For example, creating multiple
614445fffbSMatthew Ahrens * snapshots with a single call to lzc_snapshot() is atomic -- it
624445fffbSMatthew Ahrens * can't fail with only some of the requested snapshots created, even
634445fffbSMatthew Ahrens * in the event of power loss or system crash.
644445fffbSMatthew Ahrens *
654445fffbSMatthew Ahrens * - Continued libzfs Support. Some higher-level operations (e.g.
664445fffbSMatthew Ahrens * support for "zfs send -R") are too complicated to fit the scope of
674445fffbSMatthew Ahrens * libzfs_core. This functionality will continue to live in libzfs.
684445fffbSMatthew Ahrens * Where appropriate, libzfs will use the underlying atomic operations
694445fffbSMatthew Ahrens * of libzfs_core. For example, libzfs may implement "zfs send -R |
704445fffbSMatthew Ahrens * zfs receive" by using individual "send one snapshot", rename,
714445fffbSMatthew Ahrens * destroy, and "receive one snapshot" operations in libzfs_core.
724445fffbSMatthew Ahrens * /sbin/zfs and /zbin/zpool will link with both libzfs and
734445fffbSMatthew Ahrens * libzfs_core. Other consumers should aim to use only libzfs_core,
744445fffbSMatthew Ahrens * since that will be the supported, stable interface going forwards.
754445fffbSMatthew Ahrens */
764445fffbSMatthew Ahrens
774445fffbSMatthew Ahrens #include <libzfs_core.h>
784445fffbSMatthew Ahrens #include <ctype.h>
794445fffbSMatthew Ahrens #include <unistd.h>
804445fffbSMatthew Ahrens #include <stdlib.h>
814445fffbSMatthew Ahrens #include <string.h>
827ac89354SDon Brady #ifdef ZFS_DEBUG
837ac89354SDon Brady #include <stdio.h>
847ac89354SDon Brady #endif
854445fffbSMatthew Ahrens #include <errno.h>
864445fffbSMatthew Ahrens #include <fcntl.h>
874445fffbSMatthew Ahrens #include <pthread.h>
884445fffbSMatthew Ahrens #include <sys/nvpair.h>
894445fffbSMatthew Ahrens #include <sys/param.h>
904445fffbSMatthew Ahrens #include <sys/types.h>
914445fffbSMatthew Ahrens #include <sys/stat.h>
924445fffbSMatthew Ahrens #include <sys/zfs_ioctl.h>
934445fffbSMatthew Ahrens
947c13517fSSerapheim Dimitropoulos static int g_fd = -1;
954445fffbSMatthew Ahrens static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER;
964445fffbSMatthew Ahrens static int g_refcount;
974445fffbSMatthew Ahrens
987ac89354SDon Brady #ifdef ZFS_DEBUG
997ac89354SDon Brady static zfs_ioc_t fail_ioc_cmd;
1007ac89354SDon Brady static zfs_errno_t fail_ioc_err;
1017ac89354SDon Brady
1027ac89354SDon Brady static void
libzfs_core_debug_ioc(void)1037ac89354SDon Brady libzfs_core_debug_ioc(void)
1047ac89354SDon Brady {
1057ac89354SDon Brady /*
1067ac89354SDon Brady * To test running newer user space binaries with kernel's
1077ac89354SDon Brady * that don't yet support an ioctl or a new ioctl arg we
1087ac89354SDon Brady * provide an override to intentionally fail an ioctl.
1097ac89354SDon Brady *
1107ac89354SDon Brady * USAGE:
1117ac89354SDon Brady * The override variable, ZFS_IOC_TEST, is of the form "cmd:err"
1127ac89354SDon Brady *
1137ac89354SDon Brady * For example, to fail a ZFS_IOC_POOL_CHECKPOINT with a
1147ac89354SDon Brady * ZFS_ERR_IOC_CMD_UNAVAIL, the string would be "0x5a4d:1029"
1157ac89354SDon Brady *
1167ac89354SDon Brady * $ sudo sh -c "ZFS_IOC_TEST=0x5a4d:1029 zpool checkpoint tank"
1177ac89354SDon Brady * cannot checkpoint 'tank': the loaded zfs module does not support
1187ac89354SDon Brady * this operation. A reboot may be required to enable this operation.
1197ac89354SDon Brady */
1207ac89354SDon Brady if (fail_ioc_cmd == 0) {
1217ac89354SDon Brady char *ioc_test = getenv("ZFS_IOC_TEST");
1227ac89354SDon Brady unsigned int ioc_num = 0, ioc_err = 0;
1237ac89354SDon Brady
1247ac89354SDon Brady if (ioc_test != NULL &&
1257ac89354SDon Brady sscanf(ioc_test, "%i:%i", &ioc_num, &ioc_err) == 2 &&
1267ac89354SDon Brady ioc_num < ZFS_IOC_LAST) {
1277ac89354SDon Brady fail_ioc_cmd = ioc_num;
1287ac89354SDon Brady fail_ioc_err = ioc_err;
1297ac89354SDon Brady }
1307ac89354SDon Brady }
1317ac89354SDon Brady }
1327ac89354SDon Brady #endif
1337ac89354SDon Brady
1344445fffbSMatthew Ahrens int
libzfs_core_init(void)1354445fffbSMatthew Ahrens libzfs_core_init(void)
1364445fffbSMatthew Ahrens {
1374445fffbSMatthew Ahrens (void) pthread_mutex_lock(&g_lock);
1384445fffbSMatthew Ahrens if (g_refcount == 0) {
1394445fffbSMatthew Ahrens g_fd = open("/dev/zfs", O_RDWR);
1404445fffbSMatthew Ahrens if (g_fd < 0) {
1414445fffbSMatthew Ahrens (void) pthread_mutex_unlock(&g_lock);
1424445fffbSMatthew Ahrens return (errno);
1434445fffbSMatthew Ahrens }
1444445fffbSMatthew Ahrens }
1454445fffbSMatthew Ahrens g_refcount++;
1467ac89354SDon Brady
1477ac89354SDon Brady #ifdef ZFS_DEBUG
1487ac89354SDon Brady libzfs_core_debug_ioc();
1497ac89354SDon Brady #endif
1504445fffbSMatthew Ahrens (void) pthread_mutex_unlock(&g_lock);
1514445fffbSMatthew Ahrens return (0);
1524445fffbSMatthew Ahrens }
1534445fffbSMatthew Ahrens
1544445fffbSMatthew Ahrens void
libzfs_core_fini(void)1554445fffbSMatthew Ahrens libzfs_core_fini(void)
1564445fffbSMatthew Ahrens {
1574445fffbSMatthew Ahrens (void) pthread_mutex_lock(&g_lock);
1584445fffbSMatthew Ahrens ASSERT3S(g_refcount, >, 0);
1597c13517fSSerapheim Dimitropoulos
1607c13517fSSerapheim Dimitropoulos if (g_refcount > 0)
1617c13517fSSerapheim Dimitropoulos g_refcount--;
1627c13517fSSerapheim Dimitropoulos
1637c13517fSSerapheim Dimitropoulos if (g_refcount == 0 && g_fd != -1) {
1644445fffbSMatthew Ahrens (void) close(g_fd);
1657c13517fSSerapheim Dimitropoulos g_fd = -1;
1667c13517fSSerapheim Dimitropoulos }
1674445fffbSMatthew Ahrens (void) pthread_mutex_unlock(&g_lock);
1684445fffbSMatthew Ahrens }
1694445fffbSMatthew Ahrens
1704445fffbSMatthew Ahrens static int
lzc_ioctl(zfs_ioc_t ioc,const char * name,nvlist_t * source,nvlist_t ** resultp)1714445fffbSMatthew Ahrens lzc_ioctl(zfs_ioc_t ioc, const char *name,
1724445fffbSMatthew Ahrens nvlist_t *source, nvlist_t **resultp)
1734445fffbSMatthew Ahrens {
1744445fffbSMatthew Ahrens zfs_cmd_t zc = { 0 };
1754445fffbSMatthew Ahrens int error = 0;
1769c2acf00SAlek Pinchuk char *packed = NULL;
1779c2acf00SAlek Pinchuk size_t size = 0;
1784445fffbSMatthew Ahrens
1794445fffbSMatthew Ahrens ASSERT3S(g_refcount, >, 0);
1807c13517fSSerapheim Dimitropoulos VERIFY3S(g_fd, !=, -1);
1814445fffbSMatthew Ahrens
1827ac89354SDon Brady #ifdef ZFS_DEBUG
1837ac89354SDon Brady if (ioc == fail_ioc_cmd)
1847ac89354SDon Brady return (fail_ioc_err);
1857ac89354SDon Brady #endif
1867ac89354SDon Brady
1879c2acf00SAlek Pinchuk if (name != NULL)
1889c2acf00SAlek Pinchuk (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
1894445fffbSMatthew Ahrens
1909c2acf00SAlek Pinchuk if (source != NULL) {
1919c2acf00SAlek Pinchuk packed = fnvlist_pack(source, &size);
1929c2acf00SAlek Pinchuk zc.zc_nvlist_src = (uint64_t)(uintptr_t)packed;
1939c2acf00SAlek Pinchuk zc.zc_nvlist_src_size = size;
1949c2acf00SAlek Pinchuk }
1954445fffbSMatthew Ahrens
1964445fffbSMatthew Ahrens if (resultp != NULL) {
1973b2aab18SMatthew Ahrens *resultp = NULL;
1982840dce1SChris Williamson if (ioc == ZFS_IOC_CHANNEL_PROGRAM) {
1992840dce1SChris Williamson zc.zc_nvlist_dst_size = fnvlist_lookup_uint64(source,
2002840dce1SChris Williamson ZCP_ARG_MEMLIMIT);
2012840dce1SChris Williamson } else {
2022840dce1SChris Williamson zc.zc_nvlist_dst_size = MAX(size * 2, 128 * 1024);
2032840dce1SChris Williamson }
2044445fffbSMatthew Ahrens zc.zc_nvlist_dst = (uint64_t)(uintptr_t)
2054445fffbSMatthew Ahrens malloc(zc.zc_nvlist_dst_size);
206dd645855SToomas Soome if (zc.zc_nvlist_dst == 0) {
2074445fffbSMatthew Ahrens error = ENOMEM;
2084445fffbSMatthew Ahrens goto out;
2094445fffbSMatthew Ahrens }
2104445fffbSMatthew Ahrens }
2114445fffbSMatthew Ahrens
2124445fffbSMatthew Ahrens while (ioctl(g_fd, ioc, &zc) != 0) {
213dfc11533SChris Williamson /*
214dfc11533SChris Williamson * If ioctl exited with ENOMEM, we retry the ioctl after
215dfc11533SChris Williamson * increasing the size of the destination nvlist.
216dfc11533SChris Williamson *
2172840dce1SChris Williamson * Channel programs that exit with ENOMEM ran over the
218dfc11533SChris Williamson * lua memory sandbox; they should not be retried.
219dfc11533SChris Williamson */
220dfc11533SChris Williamson if (errno == ENOMEM && resultp != NULL &&
221dfc11533SChris Williamson ioc != ZFS_IOC_CHANNEL_PROGRAM) {
2224445fffbSMatthew Ahrens free((void *)(uintptr_t)zc.zc_nvlist_dst);
2234445fffbSMatthew Ahrens zc.zc_nvlist_dst_size *= 2;
2244445fffbSMatthew Ahrens zc.zc_nvlist_dst = (uint64_t)(uintptr_t)
2254445fffbSMatthew Ahrens malloc(zc.zc_nvlist_dst_size);
226dd645855SToomas Soome if (zc.zc_nvlist_dst == 0) {
2274445fffbSMatthew Ahrens error = ENOMEM;
2284445fffbSMatthew Ahrens goto out;
2294445fffbSMatthew Ahrens }
2304445fffbSMatthew Ahrens } else {
2314445fffbSMatthew Ahrens error = errno;
2324445fffbSMatthew Ahrens break;
2334445fffbSMatthew Ahrens }
2344445fffbSMatthew Ahrens }
2354445fffbSMatthew Ahrens if (zc.zc_nvlist_dst_filled) {
2364445fffbSMatthew Ahrens *resultp = fnvlist_unpack((void *)(uintptr_t)zc.zc_nvlist_dst,
2374445fffbSMatthew Ahrens zc.zc_nvlist_dst_size);
2384445fffbSMatthew Ahrens }
2394445fffbSMatthew Ahrens
2404445fffbSMatthew Ahrens out:
241eb633035STom Caputi if (packed != NULL)
242eb633035STom Caputi fnvlist_pack_free(packed, size);
2434445fffbSMatthew Ahrens free((void *)(uintptr_t)zc.zc_nvlist_dst);
2444445fffbSMatthew Ahrens return (error);
2454445fffbSMatthew Ahrens }
2464445fffbSMatthew Ahrens
2474445fffbSMatthew Ahrens int
lzc_create(const char * fsname,enum lzc_dataset_type type,nvlist_t * props,uint8_t * wkeydata,uint_t wkeylen)248eb633035STom Caputi lzc_create(const char *fsname, enum lzc_dataset_type type, nvlist_t *props,
249eb633035STom Caputi uint8_t *wkeydata, uint_t wkeylen)
2504445fffbSMatthew Ahrens {
2514445fffbSMatthew Ahrens int error;
252eb633035STom Caputi nvlist_t *hidden_args = NULL;
2534445fffbSMatthew Ahrens nvlist_t *args = fnvlist_alloc();
254eb633035STom Caputi
25526455f9eSAndriy Gapon fnvlist_add_int32(args, "type", (dmu_objset_type_t)type);
2564445fffbSMatthew Ahrens if (props != NULL)
2574445fffbSMatthew Ahrens fnvlist_add_nvlist(args, "props", props);
258eb633035STom Caputi
259eb633035STom Caputi if (wkeydata != NULL) {
260eb633035STom Caputi hidden_args = fnvlist_alloc();
261eb633035STom Caputi fnvlist_add_uint8_array(hidden_args, "wkeydata", wkeydata,
262eb633035STom Caputi wkeylen);
263eb633035STom Caputi fnvlist_add_nvlist(args, ZPOOL_HIDDEN_ARGS, hidden_args);
264eb633035STom Caputi }
265eb633035STom Caputi
2664445fffbSMatthew Ahrens error = lzc_ioctl(ZFS_IOC_CREATE, fsname, args, NULL);
267eb633035STom Caputi nvlist_free(hidden_args);
2684445fffbSMatthew Ahrens nvlist_free(args);
2694445fffbSMatthew Ahrens return (error);
2704445fffbSMatthew Ahrens }
2714445fffbSMatthew Ahrens
2724445fffbSMatthew Ahrens int
lzc_clone(const char * fsname,const char * origin,nvlist_t * props)273eb633035STom Caputi lzc_clone(const char *fsname, const char *origin, nvlist_t *props)
2744445fffbSMatthew Ahrens {
2754445fffbSMatthew Ahrens int error;
276eb633035STom Caputi nvlist_t *hidden_args = NULL;
2774445fffbSMatthew Ahrens nvlist_t *args = fnvlist_alloc();
278eb633035STom Caputi
2794445fffbSMatthew Ahrens fnvlist_add_string(args, "origin", origin);
2804445fffbSMatthew Ahrens if (props != NULL)
2814445fffbSMatthew Ahrens fnvlist_add_nvlist(args, "props", props);
2824445fffbSMatthew Ahrens error = lzc_ioctl(ZFS_IOC_CLONE, fsname, args, NULL);
283eb633035STom Caputi nvlist_free(hidden_args);
2844445fffbSMatthew Ahrens nvlist_free(args);
2854445fffbSMatthew Ahrens return (error);
2864445fffbSMatthew Ahrens }
2874445fffbSMatthew Ahrens
288a4b8c9aaSAndrew Stormont int
lzc_promote(const char * fsname,char * snapnamebuf,int snapnamelen)289a4b8c9aaSAndrew Stormont lzc_promote(const char *fsname, char *snapnamebuf, int snapnamelen)
290a4b8c9aaSAndrew Stormont {
291a4b8c9aaSAndrew Stormont /*
292a4b8c9aaSAndrew Stormont * The promote ioctl is still legacy, so we need to construct our
293a4b8c9aaSAndrew Stormont * own zfs_cmd_t rather than using lzc_ioctl().
294a4b8c9aaSAndrew Stormont */
295a4b8c9aaSAndrew Stormont zfs_cmd_t zc = { 0 };
296a4b8c9aaSAndrew Stormont
297a4b8c9aaSAndrew Stormont ASSERT3S(g_refcount, >, 0);
298a4b8c9aaSAndrew Stormont VERIFY3S(g_fd, !=, -1);
299a4b8c9aaSAndrew Stormont
300a4b8c9aaSAndrew Stormont (void) strlcpy(zc.zc_name, fsname, sizeof (zc.zc_name));
301a4b8c9aaSAndrew Stormont if (ioctl(g_fd, ZFS_IOC_PROMOTE, &zc) != 0) {
302a4b8c9aaSAndrew Stormont int error = errno;
303a4b8c9aaSAndrew Stormont if (error == EEXIST && snapnamebuf != NULL)
304a4b8c9aaSAndrew Stormont (void) strlcpy(snapnamebuf, zc.zc_string, snapnamelen);
305a4b8c9aaSAndrew Stormont return (error);
306a4b8c9aaSAndrew Stormont }
307a4b8c9aaSAndrew Stormont return (0);
308a4b8c9aaSAndrew Stormont }
309a4b8c9aaSAndrew Stormont
3105cabbc6bSPrashanth Sreenivasa int
lzc_remap(const char * fsname)3115cabbc6bSPrashanth Sreenivasa lzc_remap(const char *fsname)
3125cabbc6bSPrashanth Sreenivasa {
3135cabbc6bSPrashanth Sreenivasa int error;
3145cabbc6bSPrashanth Sreenivasa nvlist_t *args = fnvlist_alloc();
3155cabbc6bSPrashanth Sreenivasa error = lzc_ioctl(ZFS_IOC_REMAP, fsname, args, NULL);
3165cabbc6bSPrashanth Sreenivasa nvlist_free(args);
3175cabbc6bSPrashanth Sreenivasa return (error);
3185cabbc6bSPrashanth Sreenivasa }
3195cabbc6bSPrashanth Sreenivasa
320049ba636SAndriy Gapon int
lzc_rename(const char * source,const char * target)321049ba636SAndriy Gapon lzc_rename(const char *source, const char *target)
322049ba636SAndriy Gapon {
323049ba636SAndriy Gapon zfs_cmd_t zc = { 0 };
324049ba636SAndriy Gapon int error;
325049ba636SAndriy Gapon
326049ba636SAndriy Gapon ASSERT3S(g_refcount, >, 0);
327049ba636SAndriy Gapon VERIFY3S(g_fd, !=, -1);
328049ba636SAndriy Gapon
329049ba636SAndriy Gapon (void) strlcpy(zc.zc_name, source, sizeof (zc.zc_name));
330049ba636SAndriy Gapon (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
331049ba636SAndriy Gapon error = ioctl(g_fd, ZFS_IOC_RENAME, &zc);
332049ba636SAndriy Gapon if (error != 0)
333049ba636SAndriy Gapon error = errno;
334049ba636SAndriy Gapon return (error);
335049ba636SAndriy Gapon }
336049ba636SAndriy Gapon
337049ba636SAndriy Gapon int
lzc_destroy(const char * fsname)338049ba636SAndriy Gapon lzc_destroy(const char *fsname)
339049ba636SAndriy Gapon {
340049ba636SAndriy Gapon int error;
341049ba636SAndriy Gapon
342049ba636SAndriy Gapon nvlist_t *args = fnvlist_alloc();
343049ba636SAndriy Gapon error = lzc_ioctl(ZFS_IOC_DESTROY, fsname, args, NULL);
344049ba636SAndriy Gapon nvlist_free(args);
345049ba636SAndriy Gapon return (error);
346049ba636SAndriy Gapon }
347049ba636SAndriy Gapon
3484445fffbSMatthew Ahrens /*
3494445fffbSMatthew Ahrens * Creates snapshots.
3504445fffbSMatthew Ahrens *
3514445fffbSMatthew Ahrens * The keys in the snaps nvlist are the snapshots to be created.
3524445fffbSMatthew Ahrens * They must all be in the same pool.
3534445fffbSMatthew Ahrens *
3544445fffbSMatthew Ahrens * The props nvlist is properties to set. Currently only user properties
3554445fffbSMatthew Ahrens * are supported. { user:prop_name -> string value }
3564445fffbSMatthew Ahrens *
3574445fffbSMatthew Ahrens * The returned results nvlist will have an entry for each snapshot that failed.
3584445fffbSMatthew Ahrens * The value will be the (int32) error code.
3594445fffbSMatthew Ahrens *
3604445fffbSMatthew Ahrens * The return value will be 0 if all snapshots were created, otherwise it will
3613b2aab18SMatthew Ahrens * be the errno of a (unspecified) snapshot that failed.
3624445fffbSMatthew Ahrens */
3634445fffbSMatthew Ahrens int
lzc_snapshot(nvlist_t * snaps,nvlist_t * props,nvlist_t ** errlist)3644445fffbSMatthew Ahrens lzc_snapshot(nvlist_t *snaps, nvlist_t *props, nvlist_t **errlist)
3654445fffbSMatthew Ahrens {
3664445fffbSMatthew Ahrens nvpair_t *elem;
3674445fffbSMatthew Ahrens nvlist_t *args;
3684445fffbSMatthew Ahrens int error;
3699adfa60dSMatthew Ahrens char pool[ZFS_MAX_DATASET_NAME_LEN];
3704445fffbSMatthew Ahrens
3714445fffbSMatthew Ahrens *errlist = NULL;
3724445fffbSMatthew Ahrens
3734445fffbSMatthew Ahrens /* determine the pool name */
3744445fffbSMatthew Ahrens elem = nvlist_next_nvpair(snaps, NULL);
3754445fffbSMatthew Ahrens if (elem == NULL)
3764445fffbSMatthew Ahrens return (0);
3774445fffbSMatthew Ahrens (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
3784445fffbSMatthew Ahrens pool[strcspn(pool, "/@")] = '\0';
3794445fffbSMatthew Ahrens
3804445fffbSMatthew Ahrens args = fnvlist_alloc();
3814445fffbSMatthew Ahrens fnvlist_add_nvlist(args, "snaps", snaps);
3824445fffbSMatthew Ahrens if (props != NULL)
3834445fffbSMatthew Ahrens fnvlist_add_nvlist(args, "props", props);
3844445fffbSMatthew Ahrens
3854445fffbSMatthew Ahrens error = lzc_ioctl(ZFS_IOC_SNAPSHOT, pool, args, errlist);
3864445fffbSMatthew Ahrens nvlist_free(args);
3874445fffbSMatthew Ahrens
3884445fffbSMatthew Ahrens return (error);
3894445fffbSMatthew Ahrens }
3904445fffbSMatthew Ahrens
3914445fffbSMatthew Ahrens /*
3924445fffbSMatthew Ahrens * Destroys snapshots.
3934445fffbSMatthew Ahrens *
3944445fffbSMatthew Ahrens * The keys in the snaps nvlist are the snapshots to be destroyed.
3954445fffbSMatthew Ahrens * They must all be in the same pool.
3964445fffbSMatthew Ahrens *
3974445fffbSMatthew Ahrens * Snapshots that do not exist will be silently ignored.
3984445fffbSMatthew Ahrens *
3994445fffbSMatthew Ahrens * If 'defer' is not set, and a snapshot has user holds or clones, the
4004445fffbSMatthew Ahrens * destroy operation will fail and none of the snapshots will be
4014445fffbSMatthew Ahrens * destroyed.
4024445fffbSMatthew Ahrens *
4034445fffbSMatthew Ahrens * If 'defer' is set, and a snapshot has user holds or clones, it will be
4044445fffbSMatthew Ahrens * marked for deferred destruction, and will be destroyed when the last hold
4054445fffbSMatthew Ahrens * or clone is removed/destroyed.
4064445fffbSMatthew Ahrens *
4074445fffbSMatthew Ahrens * The return value will be 0 if all snapshots were destroyed (or marked for
408bb6e7075SMatthew Ahrens * later destruction if 'defer' is set) or didn't exist to begin with.
4094445fffbSMatthew Ahrens *
4103b2aab18SMatthew Ahrens * Otherwise the return value will be the errno of a (unspecified) snapshot
4114445fffbSMatthew Ahrens * that failed, no snapshots will be destroyed, and the errlist will have an
4124445fffbSMatthew Ahrens * entry for each snapshot that failed. The value in the errlist will be
4134445fffbSMatthew Ahrens * the (int32) error code.
4144445fffbSMatthew Ahrens */
4154445fffbSMatthew Ahrens int
lzc_destroy_snaps(nvlist_t * snaps,boolean_t defer,nvlist_t ** errlist)4164445fffbSMatthew Ahrens lzc_destroy_snaps(nvlist_t *snaps, boolean_t defer, nvlist_t **errlist)
4174445fffbSMatthew Ahrens {
4184445fffbSMatthew Ahrens nvpair_t *elem;
4194445fffbSMatthew Ahrens nvlist_t *args;
4204445fffbSMatthew Ahrens int error;
4219adfa60dSMatthew Ahrens char pool[ZFS_MAX_DATASET_NAME_LEN];
4224445fffbSMatthew Ahrens
4234445fffbSMatthew Ahrens /* determine the pool name */
4244445fffbSMatthew Ahrens elem = nvlist_next_nvpair(snaps, NULL);
4254445fffbSMatthew Ahrens if (elem == NULL)
4264445fffbSMatthew Ahrens return (0);
4274445fffbSMatthew Ahrens (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
4284445fffbSMatthew Ahrens pool[strcspn(pool, "/@")] = '\0';
4294445fffbSMatthew Ahrens
4304445fffbSMatthew Ahrens args = fnvlist_alloc();
4314445fffbSMatthew Ahrens fnvlist_add_nvlist(args, "snaps", snaps);
4324445fffbSMatthew Ahrens if (defer)
4334445fffbSMatthew Ahrens fnvlist_add_boolean(args, "defer");
4344445fffbSMatthew Ahrens
4354445fffbSMatthew Ahrens error = lzc_ioctl(ZFS_IOC_DESTROY_SNAPS, pool, args, errlist);
4364445fffbSMatthew Ahrens nvlist_free(args);
4374445fffbSMatthew Ahrens
4384445fffbSMatthew Ahrens return (error);
4394445fffbSMatthew Ahrens }
4404445fffbSMatthew Ahrens
4414445fffbSMatthew Ahrens int
lzc_snaprange_space(const char * firstsnap,const char * lastsnap,uint64_t * usedp)4424445fffbSMatthew Ahrens lzc_snaprange_space(const char *firstsnap, const char *lastsnap,
4434445fffbSMatthew Ahrens uint64_t *usedp)
4444445fffbSMatthew Ahrens {
4454445fffbSMatthew Ahrens nvlist_t *args;
4464445fffbSMatthew Ahrens nvlist_t *result;
4474445fffbSMatthew Ahrens int err;
4489adfa60dSMatthew Ahrens char fs[ZFS_MAX_DATASET_NAME_LEN];
4494445fffbSMatthew Ahrens char *atp;
4504445fffbSMatthew Ahrens
4514445fffbSMatthew Ahrens /* determine the fs name */
4524445fffbSMatthew Ahrens (void) strlcpy(fs, firstsnap, sizeof (fs));
4534445fffbSMatthew Ahrens atp = strchr(fs, '@');
4544445fffbSMatthew Ahrens if (atp == NULL)
4554445fffbSMatthew Ahrens return (EINVAL);
4564445fffbSMatthew Ahrens *atp = '\0';
4574445fffbSMatthew Ahrens
4584445fffbSMatthew Ahrens args = fnvlist_alloc();
4594445fffbSMatthew Ahrens fnvlist_add_string(args, "firstsnap", firstsnap);
4604445fffbSMatthew Ahrens
4614445fffbSMatthew Ahrens err = lzc_ioctl(ZFS_IOC_SPACE_SNAPS, lastsnap, args, &result);
4624445fffbSMatthew Ahrens nvlist_free(args);
4634445fffbSMatthew Ahrens if (err == 0)
4644445fffbSMatthew Ahrens *usedp = fnvlist_lookup_uint64(result, "used");
4654445fffbSMatthew Ahrens fnvlist_free(result);
4664445fffbSMatthew Ahrens
4674445fffbSMatthew Ahrens return (err);
4684445fffbSMatthew Ahrens }
4694445fffbSMatthew Ahrens
4704445fffbSMatthew Ahrens boolean_t
lzc_exists(const char * dataset)4714445fffbSMatthew Ahrens lzc_exists(const char *dataset)
4724445fffbSMatthew Ahrens {
4734445fffbSMatthew Ahrens /*
4744445fffbSMatthew Ahrens * The objset_stats ioctl is still legacy, so we need to construct our
475a4b8c9aaSAndrew Stormont * own zfs_cmd_t rather than using lzc_ioctl().
4764445fffbSMatthew Ahrens */
4774445fffbSMatthew Ahrens zfs_cmd_t zc = { 0 };
4784445fffbSMatthew Ahrens
4797c13517fSSerapheim Dimitropoulos ASSERT3S(g_refcount, >, 0);
4807c13517fSSerapheim Dimitropoulos VERIFY3S(g_fd, !=, -1);
4817c13517fSSerapheim Dimitropoulos
4824445fffbSMatthew Ahrens (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4834445fffbSMatthew Ahrens return (ioctl(g_fd, ZFS_IOC_OBJSET_STATS, &zc) == 0);
4844445fffbSMatthew Ahrens }
4854445fffbSMatthew Ahrens
4869c2acf00SAlek Pinchuk /*
4879c2acf00SAlek Pinchuk * outnvl is unused.
4889c2acf00SAlek Pinchuk * It was added to preserve the function signature in case it is
4899c2acf00SAlek Pinchuk * needed in the future.
4909c2acf00SAlek Pinchuk */
4919c2acf00SAlek Pinchuk /*ARGSUSED*/
4929c2acf00SAlek Pinchuk int
lzc_sync(const char * pool_name,nvlist_t * innvl,nvlist_t ** outnvl)4939c2acf00SAlek Pinchuk lzc_sync(const char *pool_name, nvlist_t *innvl, nvlist_t **outnvl)
4949c2acf00SAlek Pinchuk {
4959c2acf00SAlek Pinchuk return (lzc_ioctl(ZFS_IOC_POOL_SYNC, pool_name, innvl, NULL));
4969c2acf00SAlek Pinchuk }
4979c2acf00SAlek Pinchuk
4983b2aab18SMatthew Ahrens /*
4993b2aab18SMatthew Ahrens * Create "user holds" on snapshots. If there is a hold on a snapshot,
5003b2aab18SMatthew Ahrens * the snapshot can not be destroyed. (However, it can be marked for deletion
5013b2aab18SMatthew Ahrens * by lzc_destroy_snaps(defer=B_TRUE).)
5023b2aab18SMatthew Ahrens *
5033b2aab18SMatthew Ahrens * The keys in the nvlist are snapshot names.
5043b2aab18SMatthew Ahrens * The snapshots must all be in the same pool.
5053b2aab18SMatthew Ahrens * The value is the name of the hold (string type).
5063b2aab18SMatthew Ahrens *
5073b2aab18SMatthew Ahrens * If cleanup_fd is not -1, it must be the result of open("/dev/zfs", O_EXCL).
5083b2aab18SMatthew Ahrens * In this case, when the cleanup_fd is closed (including on process
5093b2aab18SMatthew Ahrens * termination), the holds will be released. If the system is shut down
5103b2aab18SMatthew Ahrens * uncleanly, the holds will be released when the pool is next opened
5113b2aab18SMatthew Ahrens * or imported.
5123b2aab18SMatthew Ahrens *
513a7a845e4SSteven Hartland * Holds for snapshots which don't exist will be skipped and have an entry
514bb6e7075SMatthew Ahrens * added to errlist, but will not cause an overall failure.
515a7a845e4SSteven Hartland *
516bb6e7075SMatthew Ahrens * The return value will be 0 if all holds, for snapshots that existed,
517bb6e7075SMatthew Ahrens * were succesfully created.
518a7a845e4SSteven Hartland *
519a7a845e4SSteven Hartland * Otherwise the return value will be the errno of a (unspecified) hold that
520a7a845e4SSteven Hartland * failed and no holds will be created.
521a7a845e4SSteven Hartland *
522a7a845e4SSteven Hartland * In all cases the errlist will have an entry for each hold that failed
523a7a845e4SSteven Hartland * (name = snapshot), with its value being the error code (int32).
5243b2aab18SMatthew Ahrens */
5253b2aab18SMatthew Ahrens int
lzc_hold(nvlist_t * holds,int cleanup_fd,nvlist_t ** errlist)5263b2aab18SMatthew Ahrens lzc_hold(nvlist_t *holds, int cleanup_fd, nvlist_t **errlist)
5273b2aab18SMatthew Ahrens {
5289adfa60dSMatthew Ahrens char pool[ZFS_MAX_DATASET_NAME_LEN];
5293b2aab18SMatthew Ahrens nvlist_t *args;
5303b2aab18SMatthew Ahrens nvpair_t *elem;
5313b2aab18SMatthew Ahrens int error;
5323b2aab18SMatthew Ahrens
5333b2aab18SMatthew Ahrens /* determine the pool name */
5343b2aab18SMatthew Ahrens elem = nvlist_next_nvpair(holds, NULL);
5353b2aab18SMatthew Ahrens if (elem == NULL)
5363b2aab18SMatthew Ahrens return (0);
5373b2aab18SMatthew Ahrens (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
5383b2aab18SMatthew Ahrens pool[strcspn(pool, "/@")] = '\0';
5393b2aab18SMatthew Ahrens
5403b2aab18SMatthew Ahrens args = fnvlist_alloc();
5413b2aab18SMatthew Ahrens fnvlist_add_nvlist(args, "holds", holds);
5423b2aab18SMatthew Ahrens if (cleanup_fd != -1)
5433b2aab18SMatthew Ahrens fnvlist_add_int32(args, "cleanup_fd", cleanup_fd);
5443b2aab18SMatthew Ahrens
5453b2aab18SMatthew Ahrens error = lzc_ioctl(ZFS_IOC_HOLD, pool, args, errlist);
5463b2aab18SMatthew Ahrens nvlist_free(args);
5473b2aab18SMatthew Ahrens return (error);
5483b2aab18SMatthew Ahrens }
5493b2aab18SMatthew Ahrens
5503b2aab18SMatthew Ahrens /*
5513b2aab18SMatthew Ahrens * Release "user holds" on snapshots. If the snapshot has been marked for
5523b2aab18SMatthew Ahrens * deferred destroy (by lzc_destroy_snaps(defer=B_TRUE)), it does not have
5533b2aab18SMatthew Ahrens * any clones, and all the user holds are removed, then the snapshot will be
5543b2aab18SMatthew Ahrens * destroyed.
5553b2aab18SMatthew Ahrens *
5563b2aab18SMatthew Ahrens * The keys in the nvlist are snapshot names.
5573b2aab18SMatthew Ahrens * The snapshots must all be in the same pool.
5583b2aab18SMatthew Ahrens * The value is a nvlist whose keys are the holds to remove.
5593b2aab18SMatthew Ahrens *
560a7a845e4SSteven Hartland * Holds which failed to release because they didn't exist will have an entry
561bb6e7075SMatthew Ahrens * added to errlist, but will not cause an overall failure.
562a7a845e4SSteven Hartland *
563a7a845e4SSteven Hartland * The return value will be 0 if the nvl holds was empty or all holds that
564bb6e7075SMatthew Ahrens * existed, were successfully removed.
565a7a845e4SSteven Hartland *
566a7a845e4SSteven Hartland * Otherwise the return value will be the errno of a (unspecified) hold that
567a7a845e4SSteven Hartland * failed to release and no holds will be released.
568a7a845e4SSteven Hartland *
569a7a845e4SSteven Hartland * In all cases the errlist will have an entry for each hold that failed to
570a7a845e4SSteven Hartland * to release.
5713b2aab18SMatthew Ahrens */
5723b2aab18SMatthew Ahrens int
lzc_release(nvlist_t * holds,nvlist_t ** errlist)5733b2aab18SMatthew Ahrens lzc_release(nvlist_t *holds, nvlist_t **errlist)
5743b2aab18SMatthew Ahrens {
5759adfa60dSMatthew Ahrens char pool[ZFS_MAX_DATASET_NAME_LEN];
5763b2aab18SMatthew Ahrens nvpair_t *elem;
5773b2aab18SMatthew Ahrens
5783b2aab18SMatthew Ahrens /* determine the pool name */
5793b2aab18SMatthew Ahrens elem = nvlist_next_nvpair(holds, NULL);
5803b2aab18SMatthew Ahrens if (elem == NULL)
5813b2aab18SMatthew Ahrens return (0);
5823b2aab18SMatthew Ahrens (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
5833b2aab18SMatthew Ahrens pool[strcspn(pool, "/@")] = '\0';
5843b2aab18SMatthew Ahrens
5853b2aab18SMatthew Ahrens return (lzc_ioctl(ZFS_IOC_RELEASE, pool, holds, errlist));
5863b2aab18SMatthew Ahrens }
5873b2aab18SMatthew Ahrens
5883b2aab18SMatthew Ahrens /*
5893b2aab18SMatthew Ahrens * Retrieve list of user holds on the specified snapshot.
5903b2aab18SMatthew Ahrens *
5913b2aab18SMatthew Ahrens * On success, *holdsp will be set to a nvlist which the caller must free.
5923b2aab18SMatthew Ahrens * The keys are the names of the holds, and the value is the creation time
5933b2aab18SMatthew Ahrens * of the hold (uint64) in seconds since the epoch.
5943b2aab18SMatthew Ahrens */
5953b2aab18SMatthew Ahrens int
lzc_get_holds(const char * snapname,nvlist_t ** holdsp)5963b2aab18SMatthew Ahrens lzc_get_holds(const char *snapname, nvlist_t **holdsp)
5973b2aab18SMatthew Ahrens {
5989c2acf00SAlek Pinchuk return (lzc_ioctl(ZFS_IOC_GET_HOLDS, snapname, NULL, holdsp));
5993b2aab18SMatthew Ahrens }
6003b2aab18SMatthew Ahrens
6014445fffbSMatthew Ahrens /*
6025d7b4d43SMatthew Ahrens * Generate a zfs send stream for the specified snapshot and write it to
6035d7b4d43SMatthew Ahrens * the specified file descriptor.
60478f17100SMatthew Ahrens *
60578f17100SMatthew Ahrens * "snapname" is the full name of the snapshot to send (e.g. "pool/fs@snap")
60678f17100SMatthew Ahrens *
60778f17100SMatthew Ahrens * If "from" is NULL, a full (non-incremental) stream will be sent.
60878f17100SMatthew Ahrens * If "from" is non-NULL, it must be the full name of a snapshot or
60978f17100SMatthew Ahrens * bookmark to send an incremental from (e.g. "pool/fs@earlier_snap" or
61078f17100SMatthew Ahrens * "pool/fs#earlier_bmark"). If non-NULL, the specified snapshot or
61178f17100SMatthew Ahrens * bookmark must represent an earlier point in the history of "snapname").
61278f17100SMatthew Ahrens * It can be an earlier snapshot in the same filesystem or zvol as "snapname",
61378f17100SMatthew Ahrens * or it can be the origin of "snapname"'s filesystem, or an earlier
61478f17100SMatthew Ahrens * snapshot in the origin, etc.
61578f17100SMatthew Ahrens *
61678f17100SMatthew Ahrens * "fd" is the file descriptor to write the send stream to.
6175d7b4d43SMatthew Ahrens *
618b5152584SMatthew Ahrens * If "flags" contains LZC_SEND_FLAG_LARGE_BLOCK, the stream is permitted
619b5152584SMatthew Ahrens * to contain DRR_WRITE records with drr_length > 128K, and DRR_OBJECT
620b5152584SMatthew Ahrens * records with drr_blksz > 128K.
621b5152584SMatthew Ahrens *
6225d7b4d43SMatthew Ahrens * If "flags" contains LZC_SEND_FLAG_EMBED_DATA, the stream is permitted
6235d7b4d43SMatthew Ahrens * to contain DRR_WRITE_EMBEDDED records with drr_etype==BP_EMBEDDED_TYPE_DATA,
6245d7b4d43SMatthew Ahrens * which the receiving system must support (as indicated by support
6255d7b4d43SMatthew Ahrens * for the "embedded_data" feature).
6264445fffbSMatthew Ahrens */
6274445fffbSMatthew Ahrens int
lzc_send(const char * snapname,const char * from,int fd,enum lzc_send_flags flags)6285d7b4d43SMatthew Ahrens lzc_send(const char *snapname, const char *from, int fd,
6295d7b4d43SMatthew Ahrens enum lzc_send_flags flags)
6309c3fd121SMatthew Ahrens {
6319c3fd121SMatthew Ahrens return (lzc_send_resume(snapname, from, fd, flags, 0, 0));
6329c3fd121SMatthew Ahrens }
6339c3fd121SMatthew Ahrens
6349c3fd121SMatthew Ahrens int
lzc_send_resume(const char * snapname,const char * from,int fd,enum lzc_send_flags flags,uint64_t resumeobj,uint64_t resumeoff)6359c3fd121SMatthew Ahrens lzc_send_resume(const char *snapname, const char *from, int fd,
6369c3fd121SMatthew Ahrens enum lzc_send_flags flags, uint64_t resumeobj, uint64_t resumeoff)
6374445fffbSMatthew Ahrens {
6384445fffbSMatthew Ahrens nvlist_t *args;
6394445fffbSMatthew Ahrens int err;
6404445fffbSMatthew Ahrens
6414445fffbSMatthew Ahrens args = fnvlist_alloc();
6424445fffbSMatthew Ahrens fnvlist_add_int32(args, "fd", fd);
64378f17100SMatthew Ahrens if (from != NULL)
64478f17100SMatthew Ahrens fnvlist_add_string(args, "fromsnap", from);
645b5152584SMatthew Ahrens if (flags & LZC_SEND_FLAG_LARGE_BLOCK)
646b5152584SMatthew Ahrens fnvlist_add_boolean(args, "largeblockok");
6475d7b4d43SMatthew Ahrens if (flags & LZC_SEND_FLAG_EMBED_DATA)
6485d7b4d43SMatthew Ahrens fnvlist_add_boolean(args, "embedok");
6495602294fSDan Kimmel if (flags & LZC_SEND_FLAG_COMPRESS)
6505602294fSDan Kimmel fnvlist_add_boolean(args, "compressok");
651eb633035STom Caputi if (flags & LZC_SEND_FLAG_RAW)
652eb633035STom Caputi fnvlist_add_boolean(args, "rawok");
6539c3fd121SMatthew Ahrens if (resumeobj != 0 || resumeoff != 0) {
6549c3fd121SMatthew Ahrens fnvlist_add_uint64(args, "resume_object", resumeobj);
6559c3fd121SMatthew Ahrens fnvlist_add_uint64(args, "resume_offset", resumeoff);
6569c3fd121SMatthew Ahrens }
6574445fffbSMatthew Ahrens err = lzc_ioctl(ZFS_IOC_SEND_NEW, snapname, args, NULL);
6584445fffbSMatthew Ahrens nvlist_free(args);
6594445fffbSMatthew Ahrens return (err);
6604445fffbSMatthew Ahrens }
6614445fffbSMatthew Ahrens
6624445fffbSMatthew Ahrens /*
663643da460SMax Grossman * "from" can be NULL, a snapshot, or a bookmark.
664643da460SMax Grossman *
665643da460SMax Grossman * If from is NULL, a full (non-incremental) stream will be estimated. This
666643da460SMax Grossman * is calculated very efficiently.
667643da460SMax Grossman *
668643da460SMax Grossman * If from is a snapshot, lzc_send_space uses the deadlists attached to
669643da460SMax Grossman * each snapshot to efficiently estimate the stream size.
670643da460SMax Grossman *
671643da460SMax Grossman * If from is a bookmark, the indirect blocks in the destination snapshot
672643da460SMax Grossman * are traversed, looking for blocks with a birth time since the creation TXG of
673643da460SMax Grossman * the snapshot this bookmark was created from. This will result in
674643da460SMax Grossman * significantly more I/O and be less efficient than a send space estimation on
675643da460SMax Grossman * an equivalent snapshot.
6764445fffbSMatthew Ahrens */
6774445fffbSMatthew Ahrens int
lzc_send_space(const char * snapname,const char * from,enum lzc_send_flags flags,uint64_t * spacep)6785602294fSDan Kimmel lzc_send_space(const char *snapname, const char *from,
6795602294fSDan Kimmel enum lzc_send_flags flags, uint64_t *spacep)
6804445fffbSMatthew Ahrens {
6814445fffbSMatthew Ahrens nvlist_t *args;
6824445fffbSMatthew Ahrens nvlist_t *result;
6834445fffbSMatthew Ahrens int err;
6844445fffbSMatthew Ahrens
6854445fffbSMatthew Ahrens args = fnvlist_alloc();
686643da460SMax Grossman if (from != NULL)
687643da460SMax Grossman fnvlist_add_string(args, "from", from);
6885602294fSDan Kimmel if (flags & LZC_SEND_FLAG_LARGE_BLOCK)
6895602294fSDan Kimmel fnvlist_add_boolean(args, "largeblockok");
6905602294fSDan Kimmel if (flags & LZC_SEND_FLAG_EMBED_DATA)
6915602294fSDan Kimmel fnvlist_add_boolean(args, "embedok");
6925602294fSDan Kimmel if (flags & LZC_SEND_FLAG_COMPRESS)
6935602294fSDan Kimmel fnvlist_add_boolean(args, "compressok");
6944445fffbSMatthew Ahrens err = lzc_ioctl(ZFS_IOC_SEND_SPACE, snapname, args, &result);
6954445fffbSMatthew Ahrens nvlist_free(args);
6964445fffbSMatthew Ahrens if (err == 0)
6974445fffbSMatthew Ahrens *spacep = fnvlist_lookup_uint64(result, "space");
6984445fffbSMatthew Ahrens nvlist_free(result);
6994445fffbSMatthew Ahrens return (err);
7004445fffbSMatthew Ahrens }
7014445fffbSMatthew Ahrens
7024445fffbSMatthew Ahrens static int
recv_read(int fd,void * buf,int ilen)7034445fffbSMatthew Ahrens recv_read(int fd, void *buf, int ilen)
7044445fffbSMatthew Ahrens {
7054445fffbSMatthew Ahrens char *cp = buf;
7064445fffbSMatthew Ahrens int rv;
7074445fffbSMatthew Ahrens int len = ilen;
7084445fffbSMatthew Ahrens
7094445fffbSMatthew Ahrens do {
7104445fffbSMatthew Ahrens rv = read(fd, cp, len);
7114445fffbSMatthew Ahrens cp += rv;
7124445fffbSMatthew Ahrens len -= rv;
7134445fffbSMatthew Ahrens } while (rv > 0);
7144445fffbSMatthew Ahrens
7154445fffbSMatthew Ahrens if (rv < 0 || len != 0)
7164445fffbSMatthew Ahrens return (EIO);
7174445fffbSMatthew Ahrens
7184445fffbSMatthew Ahrens return (0);
7194445fffbSMatthew Ahrens }
7204445fffbSMatthew Ahrens
7219c3fd121SMatthew Ahrens static int
recv_impl(const char * snapname,nvlist_t * recvdprops,nvlist_t * localprops,uint8_t * wkeydata,uint_t wkeylen,const char * origin,boolean_t force,boolean_t resumable,boolean_t raw,int input_fd,const dmu_replay_record_t * begin_record,int cleanup_fd,uint64_t * read_bytes,uint64_t * errflags,uint64_t * action_handle,nvlist_t ** errors)7226ccda740Sloli recv_impl(const char *snapname, nvlist_t *recvdprops, nvlist_t *localprops,
7236ccda740Sloli uint8_t *wkeydata, uint_t wkeylen, const char *origin, boolean_t force,
7246ccda740Sloli boolean_t resumable, boolean_t raw, int input_fd,
7256ccda740Sloli const dmu_replay_record_t *begin_record, int cleanup_fd,
7266ccda740Sloli uint64_t *read_bytes, uint64_t *errflags, uint64_t *action_handle,
7276ccda740Sloli nvlist_t **errors)
7284445fffbSMatthew Ahrens {
7296ccda740Sloli
7304445fffbSMatthew Ahrens /*
7314445fffbSMatthew Ahrens * The receive ioctl is still legacy, so we need to construct our own
7324445fffbSMatthew Ahrens * zfs_cmd_t rather than using zfsc_ioctl().
7334445fffbSMatthew Ahrens */
7344445fffbSMatthew Ahrens zfs_cmd_t zc = { 0 };
7354445fffbSMatthew Ahrens char *packed = NULL;
7364445fffbSMatthew Ahrens size_t size;
7376ccda740Sloli
7386ccda740Sloli dmu_replay_record_t drr;
7396ccda740Sloli char fsname[MAXPATHLEN];
7406ccda740Sloli char *atp;
7414445fffbSMatthew Ahrens int error;
7424445fffbSMatthew Ahrens
7434445fffbSMatthew Ahrens ASSERT3S(g_refcount, >, 0);
7447c13517fSSerapheim Dimitropoulos VERIFY3S(g_fd, !=, -1);
7454445fffbSMatthew Ahrens
7466ccda740Sloli /* Set 'fsname' to the name of containing filesystem */
7476ccda740Sloli (void) strlcpy(fsname, snapname, sizeof (fsname));
7486ccda740Sloli atp = strchr(fsname, '@');
7494445fffbSMatthew Ahrens if (atp == NULL)
7504445fffbSMatthew Ahrens return (EINVAL);
7514445fffbSMatthew Ahrens *atp = '\0';
7524445fffbSMatthew Ahrens
7534445fffbSMatthew Ahrens /* if the fs does not exist, try its parent. */
7546ccda740Sloli if (!lzc_exists(fsname)) {
7556ccda740Sloli char *slashp = strrchr(fsname, '/');
7564445fffbSMatthew Ahrens if (slashp == NULL)
7574445fffbSMatthew Ahrens return (ENOENT);
7584445fffbSMatthew Ahrens *slashp = '\0';
7596ccda740Sloli }
7604445fffbSMatthew Ahrens
7616ccda740Sloli /*
7626ccda740Sloli * The begin_record is normally a non-byteswapped BEGIN record.
7636ccda740Sloli * For resumable streams it may be set to any non-byteswapped
7646ccda740Sloli * dmu_replay_record_t.
7656ccda740Sloli */
7666ccda740Sloli if (begin_record == NULL) {
7676ccda740Sloli error = recv_read(input_fd, &drr, sizeof (drr));
7686ccda740Sloli if (error != 0)
7696ccda740Sloli return (error);
7706ccda740Sloli } else {
7716ccda740Sloli drr = *begin_record;
7724445fffbSMatthew Ahrens }
7734445fffbSMatthew Ahrens
7746ccda740Sloli (void) strlcpy(zc.zc_name, fsname, sizeof (zc.zc_name));
7754445fffbSMatthew Ahrens (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
7764445fffbSMatthew Ahrens
7776ccda740Sloli if (recvdprops != NULL) {
7786ccda740Sloli packed = fnvlist_pack(recvdprops, &size);
7794445fffbSMatthew Ahrens zc.zc_nvlist_src = (uint64_t)(uintptr_t)packed;
7804445fffbSMatthew Ahrens zc.zc_nvlist_src_size = size;
7814445fffbSMatthew Ahrens }
7824445fffbSMatthew Ahrens
7836ccda740Sloli if (localprops != NULL) {
7846ccda740Sloli packed = fnvlist_pack(localprops, &size);
7856ccda740Sloli zc.zc_nvlist_conf = (uint64_t)(uintptr_t)packed;
7866ccda740Sloli zc.zc_nvlist_conf_size = size;
7876ccda740Sloli }
7884445fffbSMatthew Ahrens
7896ccda740Sloli /* Use zc_history_ members for hidden args */
7906ccda740Sloli if (wkeydata != NULL) {
7916ccda740Sloli nvlist_t *hidden_args = fnvlist_alloc();
7926ccda740Sloli fnvlist_add_uint8_array(hidden_args, "wkeydata", wkeydata,
7936ccda740Sloli wkeylen);
7946ccda740Sloli packed = fnvlist_pack(hidden_args, &size);
7956ccda740Sloli zc.zc_history_offset = (uint64_t)(uintptr_t)packed;
7966ccda740Sloli zc.zc_history_len = size;
797620f3225SAndriy Gapon }
7984445fffbSMatthew Ahrens
7996ccda740Sloli if (origin != NULL)
8006ccda740Sloli (void) strlcpy(zc.zc_string, origin, sizeof (zc.zc_string));
8014445fffbSMatthew Ahrens
8026ccda740Sloli ASSERT3S(drr.drr_type, ==, DRR_BEGIN);
8036ccda740Sloli zc.zc_begin_record = drr;
8044445fffbSMatthew Ahrens zc.zc_guid = force;
8056ccda740Sloli zc.zc_cookie = input_fd;
8066ccda740Sloli zc.zc_cleanup_fd = -1;
8076ccda740Sloli zc.zc_action_handle = 0;
8089c3fd121SMatthew Ahrens zc.zc_resumable = resumable;
8099c3fd121SMatthew Ahrens
8106ccda740Sloli if (cleanup_fd >= 0)
8116ccda740Sloli zc.zc_cleanup_fd = cleanup_fd;
8126ccda740Sloli
8136ccda740Sloli if (action_handle != NULL)
8146ccda740Sloli zc.zc_action_handle = *action_handle;
8156ccda740Sloli
8166ccda740Sloli zc.zc_nvlist_dst_size = 128 * 1024;
8176ccda740Sloli zc.zc_nvlist_dst = (uint64_t)(uintptr_t)malloc(zc.zc_nvlist_dst_size);
8184445fffbSMatthew Ahrens
8194445fffbSMatthew Ahrens error = ioctl(g_fd, ZFS_IOC_RECV, &zc);
8206ccda740Sloli if (error != 0) {
8214445fffbSMatthew Ahrens error = errno;
8226ccda740Sloli } else {
8236ccda740Sloli if (read_bytes != NULL)
8246ccda740Sloli *read_bytes = zc.zc_cookie;
8256ccda740Sloli
8266ccda740Sloli if (errflags != NULL)
8276ccda740Sloli *errflags = zc.zc_obj;
8286ccda740Sloli
8296ccda740Sloli if (action_handle != NULL)
8306ccda740Sloli *action_handle = zc.zc_action_handle;
8316ccda740Sloli
8326ccda740Sloli if (errors != NULL)
8336ccda740Sloli VERIFY0(nvlist_unpack(
8346ccda740Sloli (void *)(uintptr_t)zc.zc_nvlist_dst,
8356ccda740Sloli zc.zc_nvlist_dst_size, errors, KM_SLEEP));
8366ccda740Sloli }
8374445fffbSMatthew Ahrens
8384445fffbSMatthew Ahrens if (packed != NULL)
8394445fffbSMatthew Ahrens fnvlist_pack_free(packed, size);
8404445fffbSMatthew Ahrens free((void*)(uintptr_t)zc.zc_nvlist_dst);
8416ccda740Sloli
8424445fffbSMatthew Ahrens return (error);
8434445fffbSMatthew Ahrens }
844a7027df1SMatthew Ahrens
8459c3fd121SMatthew Ahrens /*
8469c3fd121SMatthew Ahrens * The simplest receive case: receive from the specified fd, creating the
8479c3fd121SMatthew Ahrens * specified snapshot. Apply the specified properties as "received" properties
8489c3fd121SMatthew Ahrens * (which can be overridden by locally-set properties). If the stream is a
8499c3fd121SMatthew Ahrens * clone, its origin snapshot must be specified by 'origin'. The 'force'
8509c3fd121SMatthew Ahrens * flag will cause the target filesystem to be rolled back or destroyed if
8519c3fd121SMatthew Ahrens * necessary to receive.
8529c3fd121SMatthew Ahrens *
8539c3fd121SMatthew Ahrens * Return 0 on success or an errno on failure.
8549c3fd121SMatthew Ahrens *
8559c3fd121SMatthew Ahrens * Note: this interface does not work on dedup'd streams
8569c3fd121SMatthew Ahrens * (those with DMU_BACKUP_FEATURE_DEDUP).
8579c3fd121SMatthew Ahrens */
8589c3fd121SMatthew Ahrens int
lzc_receive(const char * snapname,nvlist_t * props,const char * origin,boolean_t raw,boolean_t force,int fd)8599c3fd121SMatthew Ahrens lzc_receive(const char *snapname, nvlist_t *props, const char *origin,
860eb633035STom Caputi boolean_t raw, boolean_t force, int fd)
8619c3fd121SMatthew Ahrens {
8626ccda740Sloli return (recv_impl(snapname, props, NULL, NULL, 0, origin, force,
8636ccda740Sloli B_FALSE, raw, fd, NULL, -1, NULL, NULL, NULL, NULL));
8649c3fd121SMatthew Ahrens }
8659c3fd121SMatthew Ahrens
8669c3fd121SMatthew Ahrens /*
8679c3fd121SMatthew Ahrens * Like lzc_receive, but if the receive fails due to premature stream
8689c3fd121SMatthew Ahrens * termination, the intermediate state will be preserved on disk. In this
8699c3fd121SMatthew Ahrens * case, ECKSUM will be returned. The receive may subsequently be resumed
8709c3fd121SMatthew Ahrens * with a resuming send stream generated by lzc_send_resume().
8719c3fd121SMatthew Ahrens */
8729c3fd121SMatthew Ahrens int
lzc_receive_resumable(const char * snapname,nvlist_t * props,const char * origin,boolean_t force,boolean_t raw,int fd)8739c3fd121SMatthew Ahrens lzc_receive_resumable(const char *snapname, nvlist_t *props, const char *origin,
874eb633035STom Caputi boolean_t force, boolean_t raw, int fd)
8759c3fd121SMatthew Ahrens {
8766ccda740Sloli return (recv_impl(snapname, props, NULL, NULL, 0, origin, force,
8776ccda740Sloli B_TRUE, raw, fd, NULL, -1, NULL, NULL, NULL, NULL));
878620f3225SAndriy Gapon }
879620f3225SAndriy Gapon
880620f3225SAndriy Gapon /*
881620f3225SAndriy Gapon * Like lzc_receive, but allows the caller to read the begin record and then to
882620f3225SAndriy Gapon * pass it in. That could be useful if the caller wants to derive, for example,
883620f3225SAndriy Gapon * the snapname or the origin parameters based on the information contained in
884620f3225SAndriy Gapon * the begin record.
885620f3225SAndriy Gapon * The begin record must be in its original form as read from the stream,
886620f3225SAndriy Gapon * in other words, it should not be byteswapped.
887620f3225SAndriy Gapon *
888620f3225SAndriy Gapon * The 'resumable' parameter allows to obtain the same behavior as with
889620f3225SAndriy Gapon * lzc_receive_resumable.
890620f3225SAndriy Gapon */
891620f3225SAndriy Gapon int
lzc_receive_with_header(const char * snapname,nvlist_t * props,const char * origin,boolean_t force,boolean_t resumable,boolean_t raw,int fd,const dmu_replay_record_t * begin_record)892620f3225SAndriy Gapon lzc_receive_with_header(const char *snapname, nvlist_t *props,
893eb633035STom Caputi const char *origin, boolean_t force, boolean_t resumable, boolean_t raw,
894eb633035STom Caputi int fd, const dmu_replay_record_t *begin_record)
895620f3225SAndriy Gapon {
896620f3225SAndriy Gapon if (begin_record == NULL)
897620f3225SAndriy Gapon return (EINVAL);
8986ccda740Sloli
8996ccda740Sloli return (recv_impl(snapname, props, NULL, NULL, 0, origin, force,
9006ccda740Sloli resumable, raw, fd, begin_record, -1, NULL, NULL, NULL, NULL));
9016ccda740Sloli }
9026ccda740Sloli
9036ccda740Sloli /*
9046ccda740Sloli * Allows the caller to pass an additional 'cmdprops' argument.
9056ccda740Sloli *
9066ccda740Sloli * The 'cmdprops' nvlist contains both override ('zfs receive -o') and
9076ccda740Sloli * exclude ('zfs receive -x') properties. Callers are responsible for freeing
9086ccda740Sloli * this nvlist
9096ccda740Sloli */
lzc_receive_with_cmdprops(const char * snapname,nvlist_t * props,nvlist_t * cmdprops,uint8_t * wkeydata,uint_t wkeylen,const char * origin,boolean_t force,boolean_t resumable,boolean_t raw,int input_fd,const dmu_replay_record_t * begin_record,int cleanup_fd,uint64_t * read_bytes,uint64_t * errflags,uint64_t * action_handle,nvlist_t ** errors)9106ccda740Sloli int lzc_receive_with_cmdprops(const char *snapname, nvlist_t *props,
9116ccda740Sloli nvlist_t *cmdprops, uint8_t *wkeydata, uint_t wkeylen, const char *origin,
9126ccda740Sloli boolean_t force, boolean_t resumable, boolean_t raw, int input_fd,
9136ccda740Sloli const dmu_replay_record_t *begin_record, int cleanup_fd,
9146ccda740Sloli uint64_t *read_bytes, uint64_t *errflags, uint64_t *action_handle,
9156ccda740Sloli nvlist_t **errors)
9166ccda740Sloli {
9176ccda740Sloli return (recv_impl(snapname, props, cmdprops, wkeydata, wkeylen, origin,
9186ccda740Sloli force, resumable, raw, input_fd, begin_record, cleanup_fd,
9196ccda740Sloli read_bytes, errflags, action_handle, errors));
9209c3fd121SMatthew Ahrens }
9219c3fd121SMatthew Ahrens
922a7027df1SMatthew Ahrens /*
923a7027df1SMatthew Ahrens * Roll back this filesystem or volume to its most recent snapshot.
924a7027df1SMatthew Ahrens * If snapnamebuf is not NULL, it will be filled in with the name
925a7027df1SMatthew Ahrens * of the most recent snapshot.
92677b17137SAndriy Gapon * Note that the latest snapshot may change if a new one is concurrently
92777b17137SAndriy Gapon * created or the current one is destroyed. lzc_rollback_to can be used
92877b17137SAndriy Gapon * to roll back to a specific latest snapshot.
929a7027df1SMatthew Ahrens *
930a7027df1SMatthew Ahrens * Return 0 on success or an errno on failure.
931a7027df1SMatthew Ahrens */
932a7027df1SMatthew Ahrens int
lzc_rollback(const char * fsname,char * snapnamebuf,int snapnamelen)933a7027df1SMatthew Ahrens lzc_rollback(const char *fsname, char *snapnamebuf, int snapnamelen)
934a7027df1SMatthew Ahrens {
935a7027df1SMatthew Ahrens nvlist_t *args;
936a7027df1SMatthew Ahrens nvlist_t *result;
937a7027df1SMatthew Ahrens int err;
938a7027df1SMatthew Ahrens
939a7027df1SMatthew Ahrens args = fnvlist_alloc();
940a7027df1SMatthew Ahrens err = lzc_ioctl(ZFS_IOC_ROLLBACK, fsname, args, &result);
941a7027df1SMatthew Ahrens nvlist_free(args);
942a7027df1SMatthew Ahrens if (err == 0 && snapnamebuf != NULL) {
943a7027df1SMatthew Ahrens const char *snapname = fnvlist_lookup_string(result, "target");
944a7027df1SMatthew Ahrens (void) strlcpy(snapnamebuf, snapname, snapnamelen);
945a7027df1SMatthew Ahrens }
946ac428481SYuri Pankov nvlist_free(result);
947ac428481SYuri Pankov
948a7027df1SMatthew Ahrens return (err);
949a7027df1SMatthew Ahrens }
95078f17100SMatthew Ahrens
95177b17137SAndriy Gapon /*
95277b17137SAndriy Gapon * Roll back this filesystem or volume to the specified snapshot,
95377b17137SAndriy Gapon * if possible.
95477b17137SAndriy Gapon *
95577b17137SAndriy Gapon * Return 0 on success or an errno on failure.
95677b17137SAndriy Gapon */
95777b17137SAndriy Gapon int
lzc_rollback_to(const char * fsname,const char * snapname)95877b17137SAndriy Gapon lzc_rollback_to(const char *fsname, const char *snapname)
95977b17137SAndriy Gapon {
96077b17137SAndriy Gapon nvlist_t *args;
96177b17137SAndriy Gapon nvlist_t *result;
96277b17137SAndriy Gapon int err;
96377b17137SAndriy Gapon
96477b17137SAndriy Gapon args = fnvlist_alloc();
96577b17137SAndriy Gapon fnvlist_add_string(args, "target", snapname);
96677b17137SAndriy Gapon err = lzc_ioctl(ZFS_IOC_ROLLBACK, fsname, args, &result);
96777b17137SAndriy Gapon nvlist_free(args);
96877b17137SAndriy Gapon nvlist_free(result);
96977b17137SAndriy Gapon return (err);
97077b17137SAndriy Gapon }
97177b17137SAndriy Gapon
97278f17100SMatthew Ahrens /*
97378f17100SMatthew Ahrens * Creates bookmarks.
97478f17100SMatthew Ahrens *
97578f17100SMatthew Ahrens * The bookmarks nvlist maps from name of the bookmark (e.g. "pool/fs#bmark") to
97678f17100SMatthew Ahrens * the name of the snapshot (e.g. "pool/fs@snap"). All the bookmarks and
97778f17100SMatthew Ahrens * snapshots must be in the same pool.
97878f17100SMatthew Ahrens *
97978f17100SMatthew Ahrens * The returned results nvlist will have an entry for each bookmark that failed.
98078f17100SMatthew Ahrens * The value will be the (int32) error code.
98178f17100SMatthew Ahrens *
98278f17100SMatthew Ahrens * The return value will be 0 if all bookmarks were created, otherwise it will
98378f17100SMatthew Ahrens * be the errno of a (undetermined) bookmarks that failed.
98478f17100SMatthew Ahrens */
98578f17100SMatthew Ahrens int
lzc_bookmark(nvlist_t * bookmarks,nvlist_t ** errlist)98678f17100SMatthew Ahrens lzc_bookmark(nvlist_t *bookmarks, nvlist_t **errlist)
98778f17100SMatthew Ahrens {
98878f17100SMatthew Ahrens nvpair_t *elem;
98978f17100SMatthew Ahrens int error;
9909adfa60dSMatthew Ahrens char pool[ZFS_MAX_DATASET_NAME_LEN];
99178f17100SMatthew Ahrens
99278f17100SMatthew Ahrens /* determine the pool name */
99378f17100SMatthew Ahrens elem = nvlist_next_nvpair(bookmarks, NULL);
99478f17100SMatthew Ahrens if (elem == NULL)
99578f17100SMatthew Ahrens return (0);
99678f17100SMatthew Ahrens (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
99778f17100SMatthew Ahrens pool[strcspn(pool, "/#")] = '\0';
99878f17100SMatthew Ahrens
99978f17100SMatthew Ahrens error = lzc_ioctl(ZFS_IOC_BOOKMARK, pool, bookmarks, errlist);
100078f17100SMatthew Ahrens
100178f17100SMatthew Ahrens return (error);
100278f17100SMatthew Ahrens }
100378f17100SMatthew Ahrens
100478f17100SMatthew Ahrens /*
100578f17100SMatthew Ahrens * Retrieve bookmarks.
100678f17100SMatthew Ahrens *
100778f17100SMatthew Ahrens * Retrieve the list of bookmarks for the given file system. The props
100878f17100SMatthew Ahrens * parameter is an nvlist of property names (with no values) that will be
100978f17100SMatthew Ahrens * returned for each bookmark.
101078f17100SMatthew Ahrens *
101178f17100SMatthew Ahrens * The following are valid properties on bookmarks, all of which are numbers
101278f17100SMatthew Ahrens * (represented as uint64 in the nvlist)
101378f17100SMatthew Ahrens *
101478f17100SMatthew Ahrens * "guid" - globally unique identifier of the snapshot it refers to
101578f17100SMatthew Ahrens * "createtxg" - txg when the snapshot it refers to was created
101678f17100SMatthew Ahrens * "creation" - timestamp when the snapshot it refers to was created
1017eb633035STom Caputi * "ivsetguid" - IVset guid for identifying encrypted snapshots
101878f17100SMatthew Ahrens *
101978f17100SMatthew Ahrens * The format of the returned nvlist as follows:
102078f17100SMatthew Ahrens * <short name of bookmark> -> {
102178f17100SMatthew Ahrens * <name of property> -> {
102278f17100SMatthew Ahrens * "value" -> uint64
102378f17100SMatthew Ahrens * }
102478f17100SMatthew Ahrens * }
102578f17100SMatthew Ahrens */
102678f17100SMatthew Ahrens int
lzc_get_bookmarks(const char * fsname,nvlist_t * props,nvlist_t ** bmarks)102778f17100SMatthew Ahrens lzc_get_bookmarks(const char *fsname, nvlist_t *props, nvlist_t **bmarks)
102878f17100SMatthew Ahrens {
102978f17100SMatthew Ahrens return (lzc_ioctl(ZFS_IOC_GET_BOOKMARKS, fsname, props, bmarks));
103078f17100SMatthew Ahrens }
103178f17100SMatthew Ahrens
103278f17100SMatthew Ahrens /*
103378f17100SMatthew Ahrens * Destroys bookmarks.
103478f17100SMatthew Ahrens *
103578f17100SMatthew Ahrens * The keys in the bmarks nvlist are the bookmarks to be destroyed.
103678f17100SMatthew Ahrens * They must all be in the same pool. Bookmarks are specified as
103778f17100SMatthew Ahrens * <fs>#<bmark>.
103878f17100SMatthew Ahrens *
103978f17100SMatthew Ahrens * Bookmarks that do not exist will be silently ignored.
104078f17100SMatthew Ahrens *
104178f17100SMatthew Ahrens * The return value will be 0 if all bookmarks that existed were destroyed.
104278f17100SMatthew Ahrens *
104378f17100SMatthew Ahrens * Otherwise the return value will be the errno of a (undetermined) bookmark
104478f17100SMatthew Ahrens * that failed, no bookmarks will be destroyed, and the errlist will have an
104578f17100SMatthew Ahrens * entry for each bookmarks that failed. The value in the errlist will be
104678f17100SMatthew Ahrens * the (int32) error code.
104778f17100SMatthew Ahrens */
104878f17100SMatthew Ahrens int
lzc_destroy_bookmarks(nvlist_t * bmarks,nvlist_t ** errlist)104978f17100SMatthew Ahrens lzc_destroy_bookmarks(nvlist_t *bmarks, nvlist_t **errlist)
105078f17100SMatthew Ahrens {
105178f17100SMatthew Ahrens nvpair_t *elem;
105278f17100SMatthew Ahrens int error;
10539adfa60dSMatthew Ahrens char pool[ZFS_MAX_DATASET_NAME_LEN];
105478f17100SMatthew Ahrens
105578f17100SMatthew Ahrens /* determine the pool name */
105678f17100SMatthew Ahrens elem = nvlist_next_nvpair(bmarks, NULL);
105778f17100SMatthew Ahrens if (elem == NULL)
105878f17100SMatthew Ahrens return (0);
105978f17100SMatthew Ahrens (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
106078f17100SMatthew Ahrens pool[strcspn(pool, "/#")] = '\0';
106178f17100SMatthew Ahrens
106278f17100SMatthew Ahrens error = lzc_ioctl(ZFS_IOC_DESTROY_BOOKMARKS, pool, bmarks, errlist);
106378f17100SMatthew Ahrens
106478f17100SMatthew Ahrens return (error);
106578f17100SMatthew Ahrens }
1066dfc11533SChris Williamson
1067a3b28680SSerapheim Dimitropoulos static int
lzc_channel_program_impl(const char * pool,const char * program,boolean_t sync,uint64_t instrlimit,uint64_t memlimit,nvlist_t * argnvl,nvlist_t ** outnvl)1068a3b28680SSerapheim Dimitropoulos lzc_channel_program_impl(const char *pool, const char *program, boolean_t sync,
1069a3b28680SSerapheim Dimitropoulos uint64_t instrlimit, uint64_t memlimit, nvlist_t *argnvl, nvlist_t **outnvl)
1070a3b28680SSerapheim Dimitropoulos {
1071a3b28680SSerapheim Dimitropoulos int error;
1072a3b28680SSerapheim Dimitropoulos nvlist_t *args;
1073*d8f839f9SJason King nvlist_t *hidden_args = NULL;
1074a3b28680SSerapheim Dimitropoulos
1075a3b28680SSerapheim Dimitropoulos args = fnvlist_alloc();
1076a3b28680SSerapheim Dimitropoulos fnvlist_add_string(args, ZCP_ARG_PROGRAM, program);
1077a3b28680SSerapheim Dimitropoulos fnvlist_add_nvlist(args, ZCP_ARG_ARGLIST, argnvl);
1078a3b28680SSerapheim Dimitropoulos fnvlist_add_boolean_value(args, ZCP_ARG_SYNC, sync);
1079a3b28680SSerapheim Dimitropoulos fnvlist_add_uint64(args, ZCP_ARG_INSTRLIMIT, instrlimit);
1080a3b28680SSerapheim Dimitropoulos fnvlist_add_uint64(args, ZCP_ARG_MEMLIMIT, memlimit);
1081*d8f839f9SJason King
1082*d8f839f9SJason King /*
1083*d8f839f9SJason King * If any hidden arguments are passed, we pull them out of 'args'
1084*d8f839f9SJason King * and into a separate nvlist so spa_history_nvl() doesn't log
1085*d8f839f9SJason King * their values.
1086*d8f839f9SJason King */
1087*d8f839f9SJason King if (nvlist_lookup_nvlist(argnvl, ZPOOL_HIDDEN_ARGS,
1088*d8f839f9SJason King &hidden_args) == 0) {
1089*d8f839f9SJason King nvlist_t *argcopy = fnvlist_dup(argnvl);
1090*d8f839f9SJason King
1091*d8f839f9SJason King fnvlist_add_nvlist(args, ZPOOL_HIDDEN_ARGS, hidden_args);
1092*d8f839f9SJason King fnvlist_remove(argcopy, ZPOOL_HIDDEN_ARGS);
1093*d8f839f9SJason King fnvlist_add_nvlist(args, ZCP_ARG_ARGLIST, argcopy);
1094*d8f839f9SJason King nvlist_free(argcopy);
1095*d8f839f9SJason King }
1096*d8f839f9SJason King
1097a3b28680SSerapheim Dimitropoulos error = lzc_ioctl(ZFS_IOC_CHANNEL_PROGRAM, pool, args, outnvl);
1098a3b28680SSerapheim Dimitropoulos fnvlist_free(args);
1099a3b28680SSerapheim Dimitropoulos
1100a3b28680SSerapheim Dimitropoulos return (error);
1101a3b28680SSerapheim Dimitropoulos }
1102a3b28680SSerapheim Dimitropoulos
1103dfc11533SChris Williamson /*
1104dfc11533SChris Williamson * Executes a channel program.
1105dfc11533SChris Williamson *
1106dfc11533SChris Williamson * If this function returns 0 the channel program was successfully loaded and
1107dfc11533SChris Williamson * ran without failing. Note that individual commands the channel program ran
1108dfc11533SChris Williamson * may have failed and the channel program is responsible for reporting such
1109dfc11533SChris Williamson * errors through outnvl if they are important.
1110dfc11533SChris Williamson *
1111dfc11533SChris Williamson * This method may also return:
1112dfc11533SChris Williamson *
1113dfc11533SChris Williamson * EINVAL The program contains syntax errors, or an invalid memory or time
1114dfc11533SChris Williamson * limit was given. No part of the channel program was executed.
1115dfc11533SChris Williamson * If caused by syntax errors, 'outnvl' contains information about the
1116dfc11533SChris Williamson * errors.
1117dfc11533SChris Williamson *
1118dfc11533SChris Williamson * ECHRNG The program was executed, but encountered a runtime error, such as
1119dfc11533SChris Williamson * calling a function with incorrect arguments, invoking the error()
1120dfc11533SChris Williamson * function directly, failing an assert() command, etc. Some portion
1121dfc11533SChris Williamson * of the channel program may have executed and committed changes.
1122dfc11533SChris Williamson * Information about the failure can be found in 'outnvl'.
1123dfc11533SChris Williamson *
1124dfc11533SChris Williamson * ENOMEM The program fully executed, but the output buffer was not large
1125dfc11533SChris Williamson * enough to store the returned value. No output is returned through
1126dfc11533SChris Williamson * 'outnvl'.
1127dfc11533SChris Williamson *
1128dfc11533SChris Williamson * ENOSPC The program was terminated because it exceeded its memory usage
1129dfc11533SChris Williamson * limit. Some portion of the channel program may have executed and
1130dfc11533SChris Williamson * committed changes to disk. No output is returned through 'outnvl'.
1131dfc11533SChris Williamson *
1132dfc11533SChris Williamson * ETIME The program was terminated because it exceeded its Lua instruction
1133dfc11533SChris Williamson * limit. Some portion of the channel program may have executed and
1134dfc11533SChris Williamson * committed changes to disk. No output is returned through 'outnvl'.
1135dfc11533SChris Williamson */
1136dfc11533SChris Williamson int
lzc_channel_program(const char * pool,const char * program,uint64_t instrlimit,uint64_t memlimit,nvlist_t * argnvl,nvlist_t ** outnvl)1137dfc11533SChris Williamson lzc_channel_program(const char *pool, const char *program, uint64_t instrlimit,
1138dfc11533SChris Williamson uint64_t memlimit, nvlist_t *argnvl, nvlist_t **outnvl)
1139dfc11533SChris Williamson {
1140a3b28680SSerapheim Dimitropoulos return (lzc_channel_program_impl(pool, program, B_TRUE, instrlimit,
1141a3b28680SSerapheim Dimitropoulos memlimit, argnvl, outnvl));
1142a3b28680SSerapheim Dimitropoulos }
1143dfc11533SChris Williamson
114486714001SSerapheim Dimitropoulos /*
114586714001SSerapheim Dimitropoulos * Creates a checkpoint for the specified pool.
114686714001SSerapheim Dimitropoulos *
114786714001SSerapheim Dimitropoulos * If this function returns 0 the pool was successfully checkpointed.
114886714001SSerapheim Dimitropoulos *
114986714001SSerapheim Dimitropoulos * This method may also return:
115086714001SSerapheim Dimitropoulos *
115186714001SSerapheim Dimitropoulos * ZFS_ERR_CHECKPOINT_EXISTS
115286714001SSerapheim Dimitropoulos * The pool already has a checkpoint. A pools can only have one
115386714001SSerapheim Dimitropoulos * checkpoint at most, at any given time.
115486714001SSerapheim Dimitropoulos *
115586714001SSerapheim Dimitropoulos * ZFS_ERR_DISCARDING_CHECKPOINT
1156dd645855SToomas Soome * ZFS is in the middle of discarding a checkpoint for this pool.
1157dd645855SToomas Soome * The pool can be checkpointed again once the discard is done.
115886714001SSerapheim Dimitropoulos *
115986714001SSerapheim Dimitropoulos * ZFS_DEVRM_IN_PROGRESS
1160dd645855SToomas Soome * A vdev is currently being removed. The pool cannot be
1161dd645855SToomas Soome * checkpointed until the device removal is done.
116286714001SSerapheim Dimitropoulos *
116386714001SSerapheim Dimitropoulos * ZFS_VDEV_TOO_BIG
1164dd645855SToomas Soome * One or more top-level vdevs exceed the maximum vdev size
1165dd645855SToomas Soome * supported for this feature.
116686714001SSerapheim Dimitropoulos */
116786714001SSerapheim Dimitropoulos int
lzc_pool_checkpoint(const char * pool)116886714001SSerapheim Dimitropoulos lzc_pool_checkpoint(const char *pool)
116986714001SSerapheim Dimitropoulos {
117086714001SSerapheim Dimitropoulos int error;
117186714001SSerapheim Dimitropoulos
117286714001SSerapheim Dimitropoulos nvlist_t *result = NULL;
117386714001SSerapheim Dimitropoulos nvlist_t *args = fnvlist_alloc();
117486714001SSerapheim Dimitropoulos
117586714001SSerapheim Dimitropoulos error = lzc_ioctl(ZFS_IOC_POOL_CHECKPOINT, pool, args, &result);
117686714001SSerapheim Dimitropoulos
117786714001SSerapheim Dimitropoulos fnvlist_free(args);
117886714001SSerapheim Dimitropoulos fnvlist_free(result);
117986714001SSerapheim Dimitropoulos
118086714001SSerapheim Dimitropoulos return (error);
118186714001SSerapheim Dimitropoulos }
118286714001SSerapheim Dimitropoulos
118386714001SSerapheim Dimitropoulos /*
118486714001SSerapheim Dimitropoulos * Discard the checkpoint from the specified pool.
118586714001SSerapheim Dimitropoulos *
118686714001SSerapheim Dimitropoulos * If this function returns 0 the checkpoint was successfully discarded.
118786714001SSerapheim Dimitropoulos *
118886714001SSerapheim Dimitropoulos * This method may also return:
118986714001SSerapheim Dimitropoulos *
119086714001SSerapheim Dimitropoulos * ZFS_ERR_NO_CHECKPOINT
1191dd645855SToomas Soome * The pool does not have a checkpoint.
119286714001SSerapheim Dimitropoulos *
119386714001SSerapheim Dimitropoulos * ZFS_ERR_DISCARDING_CHECKPOINT
1194dd645855SToomas Soome * ZFS is already in the middle of discarding the checkpoint.
119586714001SSerapheim Dimitropoulos */
119686714001SSerapheim Dimitropoulos int
lzc_pool_checkpoint_discard(const char * pool)119786714001SSerapheim Dimitropoulos lzc_pool_checkpoint_discard(const char *pool)
119886714001SSerapheim Dimitropoulos {
119986714001SSerapheim Dimitropoulos int error;
120086714001SSerapheim Dimitropoulos
120186714001SSerapheim Dimitropoulos nvlist_t *result = NULL;
120286714001SSerapheim Dimitropoulos nvlist_t *args = fnvlist_alloc();
120386714001SSerapheim Dimitropoulos
120486714001SSerapheim Dimitropoulos error = lzc_ioctl(ZFS_IOC_POOL_DISCARD_CHECKPOINT, pool, args, &result);
120586714001SSerapheim Dimitropoulos
120686714001SSerapheim Dimitropoulos fnvlist_free(args);
120786714001SSerapheim Dimitropoulos fnvlist_free(result);
120886714001SSerapheim Dimitropoulos
120986714001SSerapheim Dimitropoulos return (error);
121086714001SSerapheim Dimitropoulos }
121186714001SSerapheim Dimitropoulos
1212a3b28680SSerapheim Dimitropoulos /*
1213a3b28680SSerapheim Dimitropoulos * Executes a read-only channel program.
1214a3b28680SSerapheim Dimitropoulos *
1215a3b28680SSerapheim Dimitropoulos * A read-only channel program works programmatically the same way as a
1216a3b28680SSerapheim Dimitropoulos * normal channel program executed with lzc_channel_program(). The only
1217a3b28680SSerapheim Dimitropoulos * difference is it runs exclusively in open-context and therefore can
1218a3b28680SSerapheim Dimitropoulos * return faster. The downside to that, is that the program cannot change
1219a3b28680SSerapheim Dimitropoulos * on-disk state by calling functions from the zfs.sync submodule.
1220a3b28680SSerapheim Dimitropoulos *
1221a3b28680SSerapheim Dimitropoulos * The return values of this function (and their meaning) are exactly the
1222a3b28680SSerapheim Dimitropoulos * same as the ones described in lzc_channel_program().
1223a3b28680SSerapheim Dimitropoulos */
1224a3b28680SSerapheim Dimitropoulos int
lzc_channel_program_nosync(const char * pool,const char * program,uint64_t timeout,uint64_t memlimit,nvlist_t * argnvl,nvlist_t ** outnvl)1225a3b28680SSerapheim Dimitropoulos lzc_channel_program_nosync(const char *pool, const char *program,
1226a3b28680SSerapheim Dimitropoulos uint64_t timeout, uint64_t memlimit, nvlist_t *argnvl, nvlist_t **outnvl)
1227a3b28680SSerapheim Dimitropoulos {
1228a3b28680SSerapheim Dimitropoulos return (lzc_channel_program_impl(pool, program, B_FALSE, timeout,
1229a3b28680SSerapheim Dimitropoulos memlimit, argnvl, outnvl));
1230dfc11533SChris Williamson }
1231094e47e9SGeorge Wilson
1232094e47e9SGeorge Wilson /*
1233094e47e9SGeorge Wilson * Changes initializing state.
1234094e47e9SGeorge Wilson *
1235094e47e9SGeorge Wilson * vdevs should be a list of (<key>, guid) where guid is a uint64 vdev GUID.
1236094e47e9SGeorge Wilson * The key is ignored.
1237094e47e9SGeorge Wilson *
1238094e47e9SGeorge Wilson * If there are errors related to vdev arguments, per-vdev errors are returned
1239094e47e9SGeorge Wilson * in an nvlist with the key "vdevs". Each error is a (guid, errno) pair where
1240094e47e9SGeorge Wilson * guid is stringified with PRIu64, and errno is one of the following as
1241094e47e9SGeorge Wilson * an int64_t:
1242094e47e9SGeorge Wilson * - ENODEV if the device was not found
1243094e47e9SGeorge Wilson * - EINVAL if the devices is not a leaf or is not concrete (e.g. missing)
1244094e47e9SGeorge Wilson * - EROFS if the device is not writeable
1245084fd14fSBrian Behlendorf * - EBUSY start requested but the device is already being either
1246084fd14fSBrian Behlendorf * initialized or trimmed
1247094e47e9SGeorge Wilson * - ESRCH cancel/suspend requested but device is not being initialized
1248094e47e9SGeorge Wilson *
1249094e47e9SGeorge Wilson * If the errlist is empty, then return value will be:
1250094e47e9SGeorge Wilson * - EINVAL if one or more arguments was invalid
1251094e47e9SGeorge Wilson * - Other spa_open failures
1252094e47e9SGeorge Wilson * - 0 if the operation succeeded
1253094e47e9SGeorge Wilson */
1254094e47e9SGeorge Wilson int
lzc_initialize(const char * poolname,pool_initialize_func_t cmd_type,nvlist_t * vdevs,nvlist_t ** errlist)1255094e47e9SGeorge Wilson lzc_initialize(const char *poolname, pool_initialize_func_t cmd_type,
1256094e47e9SGeorge Wilson nvlist_t *vdevs, nvlist_t **errlist)
1257094e47e9SGeorge Wilson {
1258094e47e9SGeorge Wilson int error;
1259084fd14fSBrian Behlendorf
1260094e47e9SGeorge Wilson nvlist_t *args = fnvlist_alloc();
1261094e47e9SGeorge Wilson fnvlist_add_uint64(args, ZPOOL_INITIALIZE_COMMAND, (uint64_t)cmd_type);
1262094e47e9SGeorge Wilson fnvlist_add_nvlist(args, ZPOOL_INITIALIZE_VDEVS, vdevs);
1263094e47e9SGeorge Wilson
1264094e47e9SGeorge Wilson error = lzc_ioctl(ZFS_IOC_POOL_INITIALIZE, poolname, args, errlist);
1265094e47e9SGeorge Wilson
1266094e47e9SGeorge Wilson fnvlist_free(args);
1267094e47e9SGeorge Wilson
1268094e47e9SGeorge Wilson return (error);
1269094e47e9SGeorge Wilson }
1270eb633035STom Caputi
1271084fd14fSBrian Behlendorf /*
1272084fd14fSBrian Behlendorf * Changes TRIM state.
1273084fd14fSBrian Behlendorf *
1274084fd14fSBrian Behlendorf * vdevs should be a list of (<key>, guid) where guid is a uint64 vdev GUID.
1275084fd14fSBrian Behlendorf * The key is ignored.
1276084fd14fSBrian Behlendorf *
1277084fd14fSBrian Behlendorf * If there are errors related to vdev arguments, per-vdev errors are returned
1278084fd14fSBrian Behlendorf * in an nvlist with the key "vdevs". Each error is a (guid, errno) pair where
1279084fd14fSBrian Behlendorf * guid is stringified with PRIu64, and errno is one of the following as
1280084fd14fSBrian Behlendorf * an int64_t:
1281084fd14fSBrian Behlendorf * - ENODEV if the device was not found
1282084fd14fSBrian Behlendorf * - EINVAL if the devices is not a leaf or is not concrete (e.g. missing)
1283084fd14fSBrian Behlendorf * - EROFS if the device is not writeable
1284084fd14fSBrian Behlendorf * - EBUSY start requested but the device is already being either trimmed
1285084fd14fSBrian Behlendorf * or initialized
1286084fd14fSBrian Behlendorf * - ESRCH cancel/suspend requested but device is not being initialized
1287084fd14fSBrian Behlendorf * - EOPNOTSUPP if the device does not support TRIM (or secure TRIM)
1288084fd14fSBrian Behlendorf *
1289084fd14fSBrian Behlendorf * If the errlist is empty, then return value will be:
1290084fd14fSBrian Behlendorf * - EINVAL if one or more arguments was invalid
1291084fd14fSBrian Behlendorf * - Other spa_open failures
1292084fd14fSBrian Behlendorf * - 0 if the operation succeeded
1293084fd14fSBrian Behlendorf */
1294084fd14fSBrian Behlendorf int
lzc_trim(const char * poolname,pool_trim_func_t cmd_type,uint64_t rate,boolean_t secure,nvlist_t * vdevs,nvlist_t ** errlist)1295084fd14fSBrian Behlendorf lzc_trim(const char *poolname, pool_trim_func_t cmd_type, uint64_t rate,
1296084fd14fSBrian Behlendorf boolean_t secure, nvlist_t *vdevs, nvlist_t **errlist)
1297084fd14fSBrian Behlendorf {
1298084fd14fSBrian Behlendorf int error;
1299084fd14fSBrian Behlendorf
1300084fd14fSBrian Behlendorf nvlist_t *args = fnvlist_alloc();
1301084fd14fSBrian Behlendorf fnvlist_add_uint64(args, ZPOOL_TRIM_COMMAND, (uint64_t)cmd_type);
1302084fd14fSBrian Behlendorf fnvlist_add_nvlist(args, ZPOOL_TRIM_VDEVS, vdevs);
1303084fd14fSBrian Behlendorf fnvlist_add_uint64(args, ZPOOL_TRIM_RATE, rate);
1304084fd14fSBrian Behlendorf fnvlist_add_boolean_value(args, ZPOOL_TRIM_SECURE, secure);
1305084fd14fSBrian Behlendorf
1306084fd14fSBrian Behlendorf error = lzc_ioctl(ZFS_IOC_POOL_TRIM, poolname, args, errlist);
1307084fd14fSBrian Behlendorf
1308084fd14fSBrian Behlendorf fnvlist_free(args);
1309084fd14fSBrian Behlendorf
1310084fd14fSBrian Behlendorf return (error);
1311084fd14fSBrian Behlendorf }
1312084fd14fSBrian Behlendorf
1313eb633035STom Caputi /*
1314eb633035STom Caputi * Performs key management functions
1315eb633035STom Caputi *
1316eb633035STom Caputi * crypto_cmd should be a value from zfs_ioc_crypto_cmd_t. If the command
1317eb633035STom Caputi * specifies to load or change a wrapping key, the key should be specified in
1318eb633035STom Caputi * the hidden_args nvlist so that it is not logged
1319eb633035STom Caputi */
1320eb633035STom Caputi int
lzc_load_key(const char * fsname,boolean_t noop,uint8_t * wkeydata,uint_t wkeylen)1321eb633035STom Caputi lzc_load_key(const char *fsname, boolean_t noop, uint8_t *wkeydata,
1322eb633035STom Caputi uint_t wkeylen)
1323eb633035STom Caputi {
1324eb633035STom Caputi int error;
1325eb633035STom Caputi nvlist_t *ioc_args;
1326eb633035STom Caputi nvlist_t *hidden_args;
1327eb633035STom Caputi
1328eb633035STom Caputi if (wkeydata == NULL)
1329eb633035STom Caputi return (EINVAL);
1330eb633035STom Caputi
1331eb633035STom Caputi ioc_args = fnvlist_alloc();
1332eb633035STom Caputi hidden_args = fnvlist_alloc();
1333eb633035STom Caputi fnvlist_add_uint8_array(hidden_args, "wkeydata", wkeydata, wkeylen);
1334eb633035STom Caputi fnvlist_add_nvlist(ioc_args, ZPOOL_HIDDEN_ARGS, hidden_args);
1335eb633035STom Caputi if (noop)
1336eb633035STom Caputi fnvlist_add_boolean(ioc_args, "noop");
1337eb633035STom Caputi error = lzc_ioctl(ZFS_IOC_LOAD_KEY, fsname, ioc_args, NULL);
1338eb633035STom Caputi nvlist_free(hidden_args);
1339eb633035STom Caputi nvlist_free(ioc_args);
1340eb633035STom Caputi
1341eb633035STom Caputi return (error);
1342eb633035STom Caputi }
1343eb633035STom Caputi
1344eb633035STom Caputi int
lzc_unload_key(const char * fsname)1345eb633035STom Caputi lzc_unload_key(const char *fsname)
1346eb633035STom Caputi {
1347eb633035STom Caputi return (lzc_ioctl(ZFS_IOC_UNLOAD_KEY, fsname, NULL, NULL));
1348eb633035STom Caputi }
1349eb633035STom Caputi
1350eb633035STom Caputi int
lzc_change_key(const char * fsname,uint64_t crypt_cmd,nvlist_t * props,uint8_t * wkeydata,uint_t wkeylen)1351eb633035STom Caputi lzc_change_key(const char *fsname, uint64_t crypt_cmd, nvlist_t *props,
1352eb633035STom Caputi uint8_t *wkeydata, uint_t wkeylen)
1353eb633035STom Caputi {
1354eb633035STom Caputi int error;
1355eb633035STom Caputi nvlist_t *ioc_args = fnvlist_alloc();
1356eb633035STom Caputi nvlist_t *hidden_args = NULL;
1357eb633035STom Caputi
1358eb633035STom Caputi fnvlist_add_uint64(ioc_args, "crypt_cmd", crypt_cmd);
1359eb633035STom Caputi
1360eb633035STom Caputi if (wkeydata != NULL) {
1361eb633035STom Caputi hidden_args = fnvlist_alloc();
1362eb633035STom Caputi fnvlist_add_uint8_array(hidden_args, "wkeydata", wkeydata,
1363eb633035STom Caputi wkeylen);
1364eb633035STom Caputi fnvlist_add_nvlist(ioc_args, ZPOOL_HIDDEN_ARGS, hidden_args);
1365eb633035STom Caputi }
1366eb633035STom Caputi
1367eb633035STom Caputi if (props != NULL)
1368eb633035STom Caputi fnvlist_add_nvlist(ioc_args, "props", props);
1369eb633035STom Caputi
1370eb633035STom Caputi error = lzc_ioctl(ZFS_IOC_CHANGE_KEY, fsname, ioc_args, NULL);
1371eb633035STom Caputi nvlist_free(hidden_args);
1372eb633035STom Caputi nvlist_free(ioc_args);
1373eb633035STom Caputi return (error);
1374eb633035STom Caputi }
1375c4ecba8aSPaul Dagnelie
1376c4ecba8aSPaul Dagnelie /*
1377c4ecba8aSPaul Dagnelie * Set the bootenv contents for the given pool.
1378c4ecba8aSPaul Dagnelie */
1379c4ecba8aSPaul Dagnelie int
lzc_set_bootenv(const char * pool,const nvlist_t * env)138009fcda9fSToomas Soome lzc_set_bootenv(const char *pool, const nvlist_t *env)
1381c4ecba8aSPaul Dagnelie {
138209fcda9fSToomas Soome return (lzc_ioctl(ZFS_IOC_SET_BOOTENV, pool, (nvlist_t *)env, NULL));
1383c4ecba8aSPaul Dagnelie }
1384c4ecba8aSPaul Dagnelie
1385c4ecba8aSPaul Dagnelie /*
1386c4ecba8aSPaul Dagnelie * Get the contents of the bootenv of the given pool.
1387c4ecba8aSPaul Dagnelie */
1388c4ecba8aSPaul Dagnelie int
lzc_get_bootenv(const char * pool,nvlist_t ** outnvl)1389c4ecba8aSPaul Dagnelie lzc_get_bootenv(const char *pool, nvlist_t **outnvl)
1390c4ecba8aSPaul Dagnelie {
1391c4ecba8aSPaul Dagnelie return (lzc_ioctl(ZFS_IOC_GET_BOOTENV, pool, NULL, outnvl));
1392c4ecba8aSPaul Dagnelie }
1393