1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
  24  * Copyright (c) 2013 Steven Hartland. All rights reserved.
  25  * Copyright 2015 RackTop Systems.
  26  */
  27 
  28 /*
  29  * LibZFS_Core (lzc) is intended to replace most functionality in libzfs.
  30  * It has the following characteristics:
  31  *
  32  *  - Thread Safe.  libzfs_core is accessible concurrently from multiple
  33  *  threads.  This is accomplished primarily by avoiding global data
  34  *  (e.g. caching).  Since it's thread-safe, there is no reason for a
  35  *  process to have multiple libzfs "instances".  Therefore, we store
  36  *  our few pieces of data (e.g. the file descriptor) in global
  37  *  variables.  The fd is reference-counted so that the libzfs_core
  38  *  library can be "initialized" multiple times (e.g. by different
  39  *  consumers within the same process).
  40  *
  41  *  - Committed Interface.  The libzfs_core interface will be committed,
  42  *  therefore consumers can compile against it and be confident that
  43  *  their code will continue to work on future releases of this code.
  44  *  Currently, the interface is Evolving (not Committed), but we intend
  45  *  to commit to it once it is more complete and we determine that it
  46  *  meets the needs of all consumers.
  47  *
  48  *  - Programatic Error Handling.  libzfs_core communicates errors with
  49  *  defined error numbers, and doesn't print anything to stdout/stderr.
  50  *
  51  *  - Thin Layer.  libzfs_core is a thin layer, marshaling arguments
  52  *  to/from the kernel ioctls.  There is generally a 1:1 correspondence
  53  *  between libzfs_core functions and ioctls to /dev/zfs.
  54  *
  55  *  - Clear Atomicity.  Because libzfs_core functions are generally 1:1
  56  *  with kernel ioctls, and kernel ioctls are general atomic, each
  57  *  libzfs_core function is atomic.  For example, creating multiple
  58  *  snapshots with a single call to lzc_snapshot() is atomic -- it
  59  *  can't fail with only some of the requested snapshots created, even
  60  *  in the event of power loss or system crash.
  61  *
  62  *  - Continued libzfs Support.  Some higher-level operations (e.g.
  63  *  support for "zfs send -R") are too complicated to fit the scope of
  64  *  libzfs_core.  This functionality will continue to live in libzfs.
  65  *  Where appropriate, libzfs will use the underlying atomic operations
  66  *  of libzfs_core.  For example, libzfs may implement "zfs send -R |
  67  *  zfs receive" by using individual "send one snapshot", rename,
  68  *  destroy, and "receive one snapshot" operations in libzfs_core.
  69  *  /sbin/zfs and /zbin/zpool will link with both libzfs and
  70  *  libzfs_core.  Other consumers should aim to use only libzfs_core,
  71  *  since that will be the supported, stable interface going forwards.
  72  */
  73 
  74 #include <libzfs_core.h>
  75 #include <ctype.h>
  76 #include <unistd.h>
  77 #include <stdlib.h>
  78 #include <string.h>
  79 #include <errno.h>
  80 #include <fcntl.h>
  81 #include <pthread.h>
  82 #include <sys/nvpair.h>
  83 #include <sys/param.h>
  84 #include <sys/types.h>
  85 #include <sys/stat.h>
  86 #include <sys/zfs_ioctl.h>
  87 
  88 static int g_fd;
  89 static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER;
  90 static int g_refcount;
  91 
  92 int
  93 libzfs_core_init(void)
  94 {
  95         (void) pthread_mutex_lock(&g_lock);
  96         if (g_refcount == 0) {
  97                 g_fd = open("/dev/zfs", O_RDWR);
  98                 if (g_fd < 0) {
  99                         (void) pthread_mutex_unlock(&g_lock);
 100                         return (errno);
 101                 }
 102         }
 103         g_refcount++;
 104         (void) pthread_mutex_unlock(&g_lock);
 105         return (0);
 106 }
 107 
 108 void
 109 libzfs_core_fini(void)
 110 {
 111         (void) pthread_mutex_lock(&g_lock);
 112         ASSERT3S(g_refcount, >, 0);
 113         g_refcount--;
 114         if (g_refcount == 0)
 115                 (void) close(g_fd);
 116         (void) pthread_mutex_unlock(&g_lock);
 117 }
 118 
 119 static int
 120 lzc_ioctl(zfs_ioc_t ioc, const char *name,
 121     nvlist_t *source, nvlist_t **resultp)
 122 {
 123         zfs_cmd_t zc = { 0 };
 124         int error = 0;
 125         char *packed;
 126         size_t size;
 127 
 128         ASSERT3S(g_refcount, >, 0);
 129 
 130         (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
 131 
 132         packed = fnvlist_pack(source, &size);
 133         zc.zc_nvlist_src = (uint64_t)(uintptr_t)packed;
 134         zc.zc_nvlist_src_size = size;
 135 
 136         if (resultp != NULL) {
 137                 *resultp = NULL;
 138                 zc.zc_nvlist_dst_size = MAX(size * 2, 128 * 1024);
 139                 zc.zc_nvlist_dst = (uint64_t)(uintptr_t)
 140                     malloc(zc.zc_nvlist_dst_size);
 141                 if (zc.zc_nvlist_dst == NULL) {
 142                         error = ENOMEM;
 143                         goto out;
 144                 }
 145         }
 146 
 147         while (ioctl(g_fd, ioc, &zc) != 0) {
 148                 if (errno == ENOMEM && resultp != NULL) {
 149                         free((void *)(uintptr_t)zc.zc_nvlist_dst);
 150                         zc.zc_nvlist_dst_size *= 2;
 151                         zc.zc_nvlist_dst = (uint64_t)(uintptr_t)
 152                             malloc(zc.zc_nvlist_dst_size);
 153                         if (zc.zc_nvlist_dst == NULL) {
 154                                 error = ENOMEM;
 155                                 goto out;
 156                         }
 157                 } else {
 158                         error = errno;
 159                         break;
 160                 }
 161         }
 162         if (zc.zc_nvlist_dst_filled) {
 163                 *resultp = fnvlist_unpack((void *)(uintptr_t)zc.zc_nvlist_dst,
 164                     zc.zc_nvlist_dst_size);
 165         }
 166 
 167 out:
 168         fnvlist_pack_free(packed, size);
 169         free((void *)(uintptr_t)zc.zc_nvlist_dst);
 170         return (error);
 171 }
 172 
 173 int
 174 lzc_create(const char *fsname, dmu_objset_type_t type, nvlist_t *props)
 175 {
 176         int error;
 177         nvlist_t *args = fnvlist_alloc();
 178         fnvlist_add_int32(args, "type", type);
 179         if (props != NULL)
 180                 fnvlist_add_nvlist(args, "props", props);
 181         error = lzc_ioctl(ZFS_IOC_CREATE, fsname, args, NULL);
 182         nvlist_free(args);
 183         return (error);
 184 }
 185 
 186 int
 187 lzc_clone(const char *fsname, const char *origin,
 188     nvlist_t *props)
 189 {
 190         int error;
 191         nvlist_t *args = fnvlist_alloc();
 192         fnvlist_add_string(args, "origin", origin);
 193         if (props != NULL)
 194                 fnvlist_add_nvlist(args, "props", props);
 195         error = lzc_ioctl(ZFS_IOC_CLONE, fsname, args, NULL);
 196         nvlist_free(args);
 197         return (error);
 198 }
 199 
 200 /*
 201  * Creates snapshots.
 202  *
 203  * The keys in the snaps nvlist are the snapshots to be created.
 204  * They must all be in the same pool.
 205  *
 206  * The props nvlist is properties to set.  Currently only user properties
 207  * are supported.  { user:prop_name -> string value }
 208  *
 209  * The returned results nvlist will have an entry for each snapshot that failed.
 210  * The value will be the (int32) error code.
 211  *
 212  * The return value will be 0 if all snapshots were created, otherwise it will
 213  * be the errno of a (unspecified) snapshot that failed.
 214  */
 215 int
 216 lzc_snapshot(nvlist_t *snaps, nvlist_t *props, nvlist_t **errlist)
 217 {
 218         nvpair_t *elem;
 219         nvlist_t *args;
 220         int error;
 221         char pool[MAXNAMELEN];
 222 
 223         *errlist = NULL;
 224 
 225         /* determine the pool name */
 226         elem = nvlist_next_nvpair(snaps, NULL);
 227         if (elem == NULL)
 228                 return (0);
 229         (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
 230         pool[strcspn(pool, "/@")] = '\0';
 231 
 232         args = fnvlist_alloc();
 233         fnvlist_add_nvlist(args, "snaps", snaps);
 234         if (props != NULL)
 235                 fnvlist_add_nvlist(args, "props", props);
 236 
 237         error = lzc_ioctl(ZFS_IOC_SNAPSHOT, pool, args, errlist);
 238         nvlist_free(args);
 239 
 240         return (error);
 241 }
 242 
 243 /*
 244  * Destroys snapshots.
 245  *
 246  * The keys in the snaps nvlist are the snapshots to be destroyed.
 247  * They must all be in the same pool.
 248  *
 249  * Snapshots that do not exist will be silently ignored.
 250  *
 251  * If 'defer' is not set, and a snapshot has user holds or clones, the
 252  * destroy operation will fail and none of the snapshots will be
 253  * destroyed.
 254  *
 255  * If 'defer' is set, and a snapshot has user holds or clones, it will be
 256  * marked for deferred destruction, and will be destroyed when the last hold
 257  * or clone is removed/destroyed.
 258  *
 259  * The return value will be 0 if all snapshots were destroyed (or marked for
 260  * later destruction if 'defer' is set) or didn't exist to begin with.
 261  *
 262  * Otherwise the return value will be the errno of a (unspecified) snapshot
 263  * that failed, no snapshots will be destroyed, and the errlist will have an
 264  * entry for each snapshot that failed.  The value in the errlist will be
 265  * the (int32) error code.
 266  */
 267 int
 268 lzc_destroy_snaps(nvlist_t *snaps, boolean_t defer, nvlist_t **errlist)
 269 {
 270         nvpair_t *elem;
 271         nvlist_t *args;
 272         int error;
 273         char pool[MAXNAMELEN];
 274 
 275         /* determine the pool name */
 276         elem = nvlist_next_nvpair(snaps, NULL);
 277         if (elem == NULL)
 278                 return (0);
 279         (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
 280         pool[strcspn(pool, "/@")] = '\0';
 281 
 282         args = fnvlist_alloc();
 283         fnvlist_add_nvlist(args, "snaps", snaps);
 284         if (defer)
 285                 fnvlist_add_boolean(args, "defer");
 286 
 287         error = lzc_ioctl(ZFS_IOC_DESTROY_SNAPS, pool, args, errlist);
 288         nvlist_free(args);
 289 
 290         return (error);
 291 }
 292 
 293 int
 294 lzc_snaprange_space(const char *firstsnap, const char *lastsnap,
 295     uint64_t *usedp)
 296 {
 297         nvlist_t *args;
 298         nvlist_t *result;
 299         int err;
 300         char fs[MAXNAMELEN];
 301         char *atp;
 302 
 303         /* determine the fs name */
 304         (void) strlcpy(fs, firstsnap, sizeof (fs));
 305         atp = strchr(fs, '@');
 306         if (atp == NULL)
 307                 return (EINVAL);
 308         *atp = '\0';
 309 
 310         args = fnvlist_alloc();
 311         fnvlist_add_string(args, "firstsnap", firstsnap);
 312 
 313         err = lzc_ioctl(ZFS_IOC_SPACE_SNAPS, lastsnap, args, &result);
 314         nvlist_free(args);
 315         if (err == 0)
 316                 *usedp = fnvlist_lookup_uint64(result, "used");
 317         fnvlist_free(result);
 318 
 319         return (err);
 320 }
 321 
 322 boolean_t
 323 lzc_exists(const char *dataset)
 324 {
 325         /*
 326          * The objset_stats ioctl is still legacy, so we need to construct our
 327          * own zfs_cmd_t rather than using zfsc_ioctl().
 328          */
 329         zfs_cmd_t zc = { 0 };
 330 
 331         (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
 332         return (ioctl(g_fd, ZFS_IOC_OBJSET_STATS, &zc) == 0);
 333 }
 334 
 335 /*
 336  * Create "user holds" on snapshots.  If there is a hold on a snapshot,
 337  * the snapshot can not be destroyed.  (However, it can be marked for deletion
 338  * by lzc_destroy_snaps(defer=B_TRUE).)
 339  *
 340  * The keys in the nvlist are snapshot names.
 341  * The snapshots must all be in the same pool.
 342  * The value is the name of the hold (string type).
 343  *
 344  * If cleanup_fd is not -1, it must be the result of open("/dev/zfs", O_EXCL).
 345  * In this case, when the cleanup_fd is closed (including on process
 346  * termination), the holds will be released.  If the system is shut down
 347  * uncleanly, the holds will be released when the pool is next opened
 348  * or imported.
 349  *
 350  * Holds for snapshots which don't exist will be skipped and have an entry
 351  * added to errlist, but will not cause an overall failure.
 352  *
 353  * The return value will be 0 if all holds, for snapshots that existed,
 354  * were succesfully created.
 355  *
 356  * Otherwise the return value will be the errno of a (unspecified) hold that
 357  * failed and no holds will be created.
 358  *
 359  * In all cases the errlist will have an entry for each hold that failed
 360  * (name = snapshot), with its value being the error code (int32).
 361  */
 362 int
 363 lzc_hold(nvlist_t *holds, int cleanup_fd, nvlist_t **errlist)
 364 {
 365         char pool[MAXNAMELEN];
 366         nvlist_t *args;
 367         nvpair_t *elem;
 368         int error;
 369 
 370         /* determine the pool name */
 371         elem = nvlist_next_nvpair(holds, NULL);
 372         if (elem == NULL)
 373                 return (0);
 374         (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
 375         pool[strcspn(pool, "/@")] = '\0';
 376 
 377         args = fnvlist_alloc();
 378         fnvlist_add_nvlist(args, "holds", holds);
 379         if (cleanup_fd != -1)
 380                 fnvlist_add_int32(args, "cleanup_fd", cleanup_fd);
 381 
 382         error = lzc_ioctl(ZFS_IOC_HOLD, pool, args, errlist);
 383         nvlist_free(args);
 384         return (error);
 385 }
 386 
 387 /*
 388  * Release "user holds" on snapshots.  If the snapshot has been marked for
 389  * deferred destroy (by lzc_destroy_snaps(defer=B_TRUE)), it does not have
 390  * any clones, and all the user holds are removed, then the snapshot will be
 391  * destroyed.
 392  *
 393  * The keys in the nvlist are snapshot names.
 394  * The snapshots must all be in the same pool.
 395  * The value is a nvlist whose keys are the holds to remove.
 396  *
 397  * Holds which failed to release because they didn't exist will have an entry
 398  * added to errlist, but will not cause an overall failure.
 399  *
 400  * The return value will be 0 if the nvl holds was empty or all holds that
 401  * existed, were successfully removed.
 402  *
 403  * Otherwise the return value will be the errno of a (unspecified) hold that
 404  * failed to release and no holds will be released.
 405  *
 406  * In all cases the errlist will have an entry for each hold that failed to
 407  * to release.
 408  */
 409 int
 410 lzc_release(nvlist_t *holds, nvlist_t **errlist)
 411 {
 412         char pool[MAXNAMELEN];
 413         nvpair_t *elem;
 414 
 415         /* determine the pool name */
 416         elem = nvlist_next_nvpair(holds, NULL);
 417         if (elem == NULL)
 418                 return (0);
 419         (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
 420         pool[strcspn(pool, "/@")] = '\0';
 421 
 422         return (lzc_ioctl(ZFS_IOC_RELEASE, pool, holds, errlist));
 423 }
 424 
 425 /*
 426  * Retrieve list of user holds on the specified snapshot.
 427  *
 428  * On success, *holdsp will be set to a nvlist which the caller must free.
 429  * The keys are the names of the holds, and the value is the creation time
 430  * of the hold (uint64) in seconds since the epoch.
 431  */
 432 int
 433 lzc_get_holds(const char *snapname, nvlist_t **holdsp)
 434 {
 435         int error;
 436         nvlist_t *innvl = fnvlist_alloc();
 437         error = lzc_ioctl(ZFS_IOC_GET_HOLDS, snapname, innvl, holdsp);
 438         fnvlist_free(innvl);
 439         return (error);
 440 }
 441 
 442 /*
 443  * Generate a zfs send stream for the specified snapshot and write it to
 444  * the specified file descriptor.
 445  *
 446  * "snapname" is the full name of the snapshot to send (e.g. "pool/fs@snap")
 447  *
 448  * If "from" is NULL, a full (non-incremental) stream will be sent.
 449  * If "from" is non-NULL, it must be the full name of a snapshot or
 450  * bookmark to send an incremental from (e.g. "pool/fs@earlier_snap" or
 451  * "pool/fs#earlier_bmark").  If non-NULL, the specified snapshot or
 452  * bookmark must represent an earlier point in the history of "snapname").
 453  * It can be an earlier snapshot in the same filesystem or zvol as "snapname",
 454  * or it can be the origin of "snapname"'s filesystem, or an earlier
 455  * snapshot in the origin, etc.
 456  *
 457  * "fd" is the file descriptor to write the send stream to.
 458  *
 459  * If "flags" contains LZC_SEND_FLAG_LARGE_BLOCK, the stream is permitted
 460  * to contain DRR_WRITE records with drr_length > 128K, and DRR_OBJECT
 461  * records with drr_blksz > 128K.
 462  *
 463  * If "flags" contains LZC_SEND_FLAG_EMBED_DATA, the stream is permitted
 464  * to contain DRR_WRITE_EMBEDDED records with drr_etype==BP_EMBEDDED_TYPE_DATA,
 465  * which the receiving system must support (as indicated by support
 466  * for the "embedded_data" feature).
 467  */
 468 int
 469 lzc_send(const char *snapname, const char *from, int fd,
 470     enum lzc_send_flags flags)
 471 {
 472         return (lzc_send_resume(snapname, from, fd, flags, 0, 0));
 473 }
 474 
 475 int
 476 lzc_send_resume(const char *snapname, const char *from, int fd,
 477     enum lzc_send_flags flags, uint64_t resumeobj, uint64_t resumeoff)
 478 {
 479         nvlist_t *args;
 480         int err;
 481 
 482         args = fnvlist_alloc();
 483         fnvlist_add_int32(args, "fd", fd);
 484         if (from != NULL)
 485                 fnvlist_add_string(args, "fromsnap", from);
 486         if (flags & LZC_SEND_FLAG_LARGE_BLOCK)
 487                 fnvlist_add_boolean(args, "largeblockok");
 488         if (flags & LZC_SEND_FLAG_EMBED_DATA)
 489                 fnvlist_add_boolean(args, "embedok");
 490         if (flags & LZC_SEND_FLAG_SKIP_FREE)
 491                 fnvlist_add_boolean(args, "skip_free");
 492         if (resumeobj != 0 || resumeoff != 0) {
 493                 fnvlist_add_uint64(args, "resume_object", resumeobj);
 494                 fnvlist_add_uint64(args, "resume_offset", resumeoff);
 495         }
 496         err = lzc_ioctl(ZFS_IOC_SEND_NEW, snapname, args, NULL);
 497         nvlist_free(args);
 498         return (err);
 499 }
 500 
 501 /*
 502  * "from" can be NULL, a snapshot, or a bookmark.
 503  *
 504  * If from is NULL, a full (non-incremental) stream will be estimated.  This
 505  * is calculated very efficiently.
 506  *
 507  * If from is a snapshot, lzc_send_space uses the deadlists attached to
 508  * each snapshot to efficiently estimate the stream size.
 509  *
 510  * If from is a bookmark, the indirect blocks in the destination snapshot
 511  * are traversed, looking for blocks with a birth time since the creation TXG of
 512  * the snapshot this bookmark was created from.  This will result in
 513  * significantly more I/O and be less efficient than a send space estimation on
 514  * an equivalent snapshot.
 515  */
 516 int
 517 lzc_send_space(const char *snapname, const char *from, uint64_t *spacep)
 518 {
 519         nvlist_t *args;
 520         nvlist_t *result;
 521         int err;
 522 
 523         args = fnvlist_alloc();
 524         if (from != NULL)
 525                 fnvlist_add_string(args, "from", from);
 526         err = lzc_ioctl(ZFS_IOC_SEND_SPACE, snapname, args, &result);
 527         nvlist_free(args);
 528         if (err == 0)
 529                 *spacep = fnvlist_lookup_uint64(result, "space");
 530         nvlist_free(result);
 531         return (err);
 532 }
 533 
 534 static int
 535 recv_read(int fd, void *buf, int ilen)
 536 {
 537         char *cp = buf;
 538         int rv;
 539         int len = ilen;
 540 
 541         do {
 542                 rv = read(fd, cp, len);
 543                 cp += rv;
 544                 len -= rv;
 545         } while (rv > 0);
 546 
 547         if (rv < 0 || len != 0)
 548                 return (EIO);
 549 
 550         return (0);
 551 }
 552 
 553 static int
 554 recv_impl(const char *snapname, nvlist_t *props, const char *origin,
 555     boolean_t force, boolean_t resumable, int fd,
 556     const dmu_replay_record_t *begin_record)
 557 {
 558         /*
 559          * The receive ioctl is still legacy, so we need to construct our own
 560          * zfs_cmd_t rather than using zfsc_ioctl().
 561          */
 562         zfs_cmd_t zc = { 0 };
 563         char *atp;
 564         char *packed = NULL;
 565         size_t size;
 566         int error;
 567 
 568         ASSERT3S(g_refcount, >, 0);
 569 
 570         /* zc_name is name of containing filesystem */
 571         (void) strlcpy(zc.zc_name, snapname, sizeof (zc.zc_name));
 572         atp = strchr(zc.zc_name, '@');
 573         if (atp == NULL)
 574                 return (EINVAL);
 575         *atp = '\0';
 576 
 577         /* if the fs does not exist, try its parent. */
 578         if (!lzc_exists(zc.zc_name)) {
 579                 char *slashp = strrchr(zc.zc_name, '/');
 580                 if (slashp == NULL)
 581                         return (ENOENT);
 582                 *slashp = '\0';
 583 
 584         }
 585 
 586         /* zc_value is full name of the snapshot to create */
 587         (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
 588 
 589         if (props != NULL) {
 590                 /* zc_nvlist_src is props to set */
 591                 packed = fnvlist_pack(props, &size);
 592                 zc.zc_nvlist_src = (uint64_t)(uintptr_t)packed;
 593                 zc.zc_nvlist_src_size = size;
 594         }
 595 
 596         /* zc_string is name of clone origin (if DRR_FLAG_CLONE) */
 597         if (origin != NULL)
 598                 (void) strlcpy(zc.zc_string, origin, sizeof (zc.zc_string));
 599 
 600         /* zc_begin_record is non-byteswapped BEGIN record */
 601         if (begin_record == NULL) {
 602                 error = recv_read(fd, &zc.zc_begin_record,
 603                     sizeof (zc.zc_begin_record));
 604                 if (error != 0)
 605                         goto out;
 606         } else {
 607                 zc.zc_begin_record = *begin_record;
 608         }
 609 
 610         /* zc_cookie is fd to read from */
 611         zc.zc_cookie = fd;
 612 
 613         /* zc guid is force flag */
 614         zc.zc_guid = force;
 615 
 616         zc.zc_resumable = resumable;
 617 
 618         /* zc_cleanup_fd is unused */
 619         zc.zc_cleanup_fd = -1;
 620 
 621         error = ioctl(g_fd, ZFS_IOC_RECV, &zc);
 622         if (error != 0)
 623                 error = errno;
 624 
 625 out:
 626         if (packed != NULL)
 627                 fnvlist_pack_free(packed, size);
 628         free((void*)(uintptr_t)zc.zc_nvlist_dst);
 629         return (error);
 630 }
 631 
 632 /*
 633  * The simplest receive case: receive from the specified fd, creating the
 634  * specified snapshot.  Apply the specified properties as "received" properties
 635  * (which can be overridden by locally-set properties).  If the stream is a
 636  * clone, its origin snapshot must be specified by 'origin'.  The 'force'
 637  * flag will cause the target filesystem to be rolled back or destroyed if
 638  * necessary to receive.
 639  *
 640  * Return 0 on success or an errno on failure.
 641  *
 642  * Note: this interface does not work on dedup'd streams
 643  * (those with DMU_BACKUP_FEATURE_DEDUP).
 644  */
 645 int
 646 lzc_receive(const char *snapname, nvlist_t *props, const char *origin,
 647     boolean_t force, int fd)
 648 {
 649         return (recv_impl(snapname, props, origin, force, B_FALSE, fd, NULL));
 650 }
 651 
 652 /*
 653  * Like lzc_receive, but if the receive fails due to premature stream
 654  * termination, the intermediate state will be preserved on disk.  In this
 655  * case, ECKSUM will be returned.  The receive may subsequently be resumed
 656  * with a resuming send stream generated by lzc_send_resume().
 657  */
 658 int
 659 lzc_receive_resumable(const char *snapname, nvlist_t *props, const char *origin,
 660     boolean_t force, int fd)
 661 {
 662         return (recv_impl(snapname, props, origin, force, B_TRUE, fd, NULL));
 663 }
 664 
 665 /*
 666  * Like lzc_receive, but allows the caller to read the begin record and then to
 667  * pass it in.  That could be useful if the caller wants to derive, for example,
 668  * the snapname or the origin parameters based on the information contained in
 669  * the begin record.
 670  * The begin record must be in its original form as read from the stream,
 671  * in other words, it should not be byteswapped.
 672  *
 673  * The 'resumable' parameter allows to obtain the same behavior as with
 674  * lzc_receive_resumable.
 675  */
 676 int
 677 lzc_receive_with_header(const char *snapname, nvlist_t *props,
 678     const char *origin, boolean_t force, boolean_t resumable, int fd,
 679     const dmu_replay_record_t *begin_record)
 680 {
 681         if (begin_record == NULL)
 682                 return (EINVAL);
 683         return (recv_impl(snapname, props, origin, force, resumable, fd,
 684             begin_record));
 685 }
 686 
 687 /*
 688  * Roll back this filesystem or volume to its most recent snapshot.
 689  * If snapnamebuf is not NULL, it will be filled in with the name
 690  * of the most recent snapshot.
 691  *
 692  * Return 0 on success or an errno on failure.
 693  */
 694 int
 695 lzc_rollback(const char *fsname, char *snapnamebuf, int snapnamelen)
 696 {
 697         nvlist_t *args;
 698         nvlist_t *result;
 699         int err;
 700 
 701         args = fnvlist_alloc();
 702         err = lzc_ioctl(ZFS_IOC_ROLLBACK, fsname, args, &result);
 703         nvlist_free(args);
 704         if (err == 0 && snapnamebuf != NULL) {
 705                 const char *snapname = fnvlist_lookup_string(result, "target");
 706                 (void) strlcpy(snapnamebuf, snapname, snapnamelen);
 707         }
 708         return (err);
 709 }
 710 
 711 /*
 712  * Creates bookmarks.
 713  *
 714  * The bookmarks nvlist maps from name of the bookmark (e.g. "pool/fs#bmark") to
 715  * the name of the snapshot (e.g. "pool/fs@snap").  All the bookmarks and
 716  * snapshots must be in the same pool.
 717  *
 718  * The returned results nvlist will have an entry for each bookmark that failed.
 719  * The value will be the (int32) error code.
 720  *
 721  * The return value will be 0 if all bookmarks were created, otherwise it will
 722  * be the errno of a (undetermined) bookmarks that failed.
 723  */
 724 int
 725 lzc_bookmark(nvlist_t *bookmarks, nvlist_t **errlist)
 726 {
 727         nvpair_t *elem;
 728         int error;
 729         char pool[MAXNAMELEN];
 730 
 731         /* determine the pool name */
 732         elem = nvlist_next_nvpair(bookmarks, NULL);
 733         if (elem == NULL)
 734                 return (0);
 735         (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
 736         pool[strcspn(pool, "/#")] = '\0';
 737 
 738         error = lzc_ioctl(ZFS_IOC_BOOKMARK, pool, bookmarks, errlist);
 739 
 740         return (error);
 741 }
 742 
 743 /*
 744  * Retrieve bookmarks.
 745  *
 746  * Retrieve the list of bookmarks for the given file system. The props
 747  * parameter is an nvlist of property names (with no values) that will be
 748  * returned for each bookmark.
 749  *
 750  * The following are valid properties on bookmarks, all of which are numbers
 751  * (represented as uint64 in the nvlist)
 752  *
 753  * "guid" - globally unique identifier of the snapshot it refers to
 754  * "createtxg" - txg when the snapshot it refers to was created
 755  * "creation" - timestamp when the snapshot it refers to was created
 756  *
 757  * The format of the returned nvlist as follows:
 758  * <short name of bookmark> -> {
 759  *     <name of property> -> {
 760  *         "value" -> uint64
 761  *     }
 762  *  }
 763  */
 764 int
 765 lzc_get_bookmarks(const char *fsname, nvlist_t *props, nvlist_t **bmarks)
 766 {
 767         return (lzc_ioctl(ZFS_IOC_GET_BOOKMARKS, fsname, props, bmarks));
 768 }
 769 
 770 /*
 771  * Destroys bookmarks.
 772  *
 773  * The keys in the bmarks nvlist are the bookmarks to be destroyed.
 774  * They must all be in the same pool.  Bookmarks are specified as
 775  * <fs>#<bmark>.
 776  *
 777  * Bookmarks that do not exist will be silently ignored.
 778  *
 779  * The return value will be 0 if all bookmarks that existed were destroyed.
 780  *
 781  * Otherwise the return value will be the errno of a (undetermined) bookmark
 782  * that failed, no bookmarks will be destroyed, and the errlist will have an
 783  * entry for each bookmarks that failed.  The value in the errlist will be
 784  * the (int32) error code.
 785  */
 786 int
 787 lzc_destroy_bookmarks(nvlist_t *bmarks, nvlist_t **errlist)
 788 {
 789         nvpair_t *elem;
 790         int error;
 791         char pool[MAXNAMELEN];
 792 
 793         /* determine the pool name */
 794         elem = nvlist_next_nvpair(bmarks, NULL);
 795         if (elem == NULL)
 796                 return (0);
 797         (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
 798         pool[strcspn(pool, "/#")] = '\0';
 799 
 800         error = lzc_ioctl(ZFS_IOC_DESTROY_BOOKMARKS, pool, bmarks, errlist);
 801 
 802         return (error);
 803 }