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 by Delphix. All rights reserved.
  24  */
  25 
  26 /*
  27  * LibZFS_Core (lzc) is intended to replace most functionality in libzfs.
  28  * It has the following characteristics:
  29  *
  30  *  - Thread Safe.  libzfs_core is accessible concurrently from multiple
  31  *  threads.  This is accomplished primarily by avoiding global data
  32  *  (e.g. caching).  Since it's thread-safe, there is no reason for a
  33  *  process to have multiple libzfs "instances".  Therefore, we store
  34  *  our few pieces of data (e.g. the file descriptor) in global
  35  *  variables.  The fd is reference-counted so that the libzfs_core
  36  *  library can be "initialized" multiple times (e.g. by different
  37  *  consumers within the same process).
  38  *
  39  *  - Committed Interface.  The libzfs_core interface will be committed,
  40  *  therefore consumers can compile against it and be confident that
  41  *  their code will continue to work on future releases of this code.
  42  *  Currently, the interface is Evolving (not Committed), but we intend
  43  *  to commit to it once it is more complete and we determine that it
  44  *  meets the needs of all consumers.
  45  *
  46  *  - Programatic Error Handling.  libzfs_core communicates errors with
  47  *  defined error numbers, and doesn't print anything to stdout/stderr.
  48  *
  49  *  - Thin Layer.  libzfs_core is a thin layer, marshaling arguments
  50  *  to/from the kernel ioctls.  There is generally a 1:1 correspondence
  51  *  between libzfs_core functions and ioctls to /dev/zfs.
  52  *
  53  *  - Clear Atomicity.  Because libzfs_core functions are generally 1:1
  54  *  with kernel ioctls, and kernel ioctls are general atomic, each
  55  *  libzfs_core function is atomic.  For example, creating multiple
  56  *  snapshots with a single call to lzc_snapshot() is atomic -- it
  57  *  can't fail with only some of the requested snapshots created, even
  58  *  in the event of power loss or system crash.
  59  *
  60  *  - Continued libzfs Support.  Some higher-level operations (e.g.
  61  *  support for "zfs send -R") are too complicated to fit the scope of
  62  *  libzfs_core.  This functionality will continue to live in libzfs.
  63  *  Where appropriate, libzfs will use the underlying atomic operations
  64  *  of libzfs_core.  For example, libzfs may implement "zfs send -R |
  65  *  zfs receive" by using individual "send one snapshot", rename,
  66  *  destroy, and "receive one snapshot" operations in libzfs_core.
  67  *  /sbin/zfs and /zbin/zpool will link with both libzfs and
  68  *  libzfs_core.  Other consumers should aim to use only libzfs_core,
  69  *  since that will be the supported, stable interface going forwards.
  70  */
  71 
  72 #include <libzfs_core.h>
  73 #include <ctype.h>
  74 #include <unistd.h>
  75 #include <stdlib.h>
  76 #include <string.h>
  77 #include <errno.h>
  78 #include <fcntl.h>
  79 #include <pthread.h>
  80 #include <sys/nvpair.h>
  81 #include <sys/param.h>
  82 #include <sys/types.h>
  83 #include <sys/stat.h>
  84 #include <sys/zfs_ioctl.h>
  85 
  86 static int g_fd;
  87 static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER;
  88 static int g_refcount;
  89 
  90 int
  91 libzfs_core_init(void)
  92 {
  93         (void) pthread_mutex_lock(&g_lock);
  94         if (g_refcount == 0) {
  95                 g_fd = open("/dev/zfs", O_RDWR);
  96                 if (g_fd < 0) {
  97                         (void) pthread_mutex_unlock(&g_lock);
  98                         return (errno);
  99                 }
 100         }
 101         g_refcount++;
 102         (void) pthread_mutex_unlock(&g_lock);
 103         return (0);
 104 }
 105 
 106 void
 107 libzfs_core_fini(void)
 108 {
 109         (void) pthread_mutex_lock(&g_lock);
 110         ASSERT3S(g_refcount, >, 0);
 111         g_refcount--;
 112         if (g_refcount == 0)
 113                 (void) close(g_fd);
 114         (void) pthread_mutex_unlock(&g_lock);
 115 }
 116 
 117 static int
 118 lzc_ioctl(zfs_ioc_t ioc, const char *name,
 119     nvlist_t *source, nvlist_t **resultp)
 120 {
 121         zfs_cmd_t zc = { 0 };
 122         int error = 0;
 123         char *packed;
 124         size_t size;
 125 
 126         ASSERT3S(g_refcount, >, 0);
 127 
 128         (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
 129 
 130         packed = fnvlist_pack(source, &size);
 131         zc.zc_nvlist_src = (uint64_t)(uintptr_t)packed;
 132         zc.zc_nvlist_src_size = size;
 133 
 134         if (resultp != NULL) {
 135                 *resultp = NULL;
 136                 zc.zc_nvlist_dst_size = MAX(size * 2, 128 * 1024);
 137                 zc.zc_nvlist_dst = (uint64_t)(uintptr_t)
 138                     malloc(zc.zc_nvlist_dst_size);
 139                 if (zc.zc_nvlist_dst == NULL) {
 140                         error = ENOMEM;
 141                         goto out;
 142                 }
 143         }
 144 
 145         while (ioctl(g_fd, ioc, &zc) != 0) {
 146                 if (errno == ENOMEM && resultp != NULL) {
 147                         free((void *)(uintptr_t)zc.zc_nvlist_dst);
 148                         zc.zc_nvlist_dst_size *= 2;
 149                         zc.zc_nvlist_dst = (uint64_t)(uintptr_t)
 150                             malloc(zc.zc_nvlist_dst_size);
 151                         if (zc.zc_nvlist_dst == NULL) {
 152                                 error = ENOMEM;
 153                                 goto out;
 154                         }
 155                 } else {
 156                         error = errno;
 157                         break;
 158                 }
 159         }
 160         if (zc.zc_nvlist_dst_filled) {
 161                 *resultp = fnvlist_unpack((void *)(uintptr_t)zc.zc_nvlist_dst,
 162                     zc.zc_nvlist_dst_size);
 163         }
 164 
 165 out:
 166         fnvlist_pack_free(packed, size);
 167         free((void *)(uintptr_t)zc.zc_nvlist_dst);
 168         return (error);
 169 }
 170 
 171 int
 172 lzc_create(const char *fsname, dmu_objset_type_t type, nvlist_t *props)
 173 {
 174         int error;
 175         nvlist_t *args = fnvlist_alloc();
 176         fnvlist_add_int32(args, "type", type);
 177         if (props != NULL)
 178                 fnvlist_add_nvlist(args, "props", props);
 179         error = lzc_ioctl(ZFS_IOC_CREATE, fsname, args, NULL);
 180         nvlist_free(args);
 181         return (error);
 182 }
 183 
 184 int
 185 lzc_clone(const char *fsname, const char *origin,
 186     nvlist_t *props)
 187 {
 188         int error;
 189         nvlist_t *args = fnvlist_alloc();
 190         fnvlist_add_string(args, "origin", origin);
 191         if (props != NULL)
 192                 fnvlist_add_nvlist(args, "props", props);
 193         error = lzc_ioctl(ZFS_IOC_CLONE, fsname, args, NULL);
 194         nvlist_free(args);
 195         return (error);
 196 }
 197 
 198 /*
 199  * Creates snapshots.
 200  *
 201  * The keys in the snaps nvlist are the snapshots to be created.
 202  * They must all be in the same pool.
 203  *
 204  * The props nvlist is properties to set.  Currently only user properties
 205  * are supported.  { user:prop_name -> string value }
 206  *
 207  * The returned results nvlist will have an entry for each snapshot that failed.
 208  * The value will be the (int32) error code.
 209  *
 210  * The return value will be 0 if all snapshots were created, otherwise it will
 211  * be the errno of a (unspecified) snapshot that failed.
 212  */
 213 int
 214 lzc_snapshot(nvlist_t *snaps, nvlist_t *props, nvlist_t **errlist)
 215 {
 216         nvpair_t *elem;
 217         nvlist_t *args;
 218         int error;
 219         char pool[MAXNAMELEN];
 220 
 221         *errlist = NULL;
 222 
 223         /* determine the pool name */
 224         elem = nvlist_next_nvpair(snaps, NULL);
 225         if (elem == NULL)
 226                 return (0);
 227         (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
 228         pool[strcspn(pool, "/@")] = '\0';
 229 
 230         args = fnvlist_alloc();
 231         fnvlist_add_nvlist(args, "snaps", snaps);
 232         if (props != NULL)
 233                 fnvlist_add_nvlist(args, "props", props);
 234 
 235         error = lzc_ioctl(ZFS_IOC_SNAPSHOT, pool, args, errlist);
 236         nvlist_free(args);
 237 
 238         return (error);
 239 }
 240 
 241 /*
 242  * Destroys snapshots.
 243  *
 244  * The keys in the snaps nvlist are the snapshots to be destroyed.
 245  * They must all be in the same pool.
 246  *
 247  * Snapshots that do not exist will be silently ignored.
 248  *
 249  * If 'defer' is not set, and a snapshot has user holds or clones, the
 250  * destroy operation will fail and none of the snapshots will be
 251  * destroyed.
 252  *
 253  * If 'defer' is set, and a snapshot has user holds or clones, it will be
 254  * marked for deferred destruction, and will be destroyed when the last hold
 255  * or clone is removed/destroyed.
 256  *
 257  * The return value will be ENOENT if none of the snapshots existed.
 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 and
 261  * at least one snapshot was destroyed.
 262  *
 263  * Otherwise the return value will be the errno of a (unspecified) snapshot
 264  * that failed, no snapshots will be destroyed, and the errlist will have an
 265  * entry for each snapshot that failed.  The value in the errlist will be
 266  * the (int32) error code.
 267  */
 268 int
 269 lzc_destroy_snaps(nvlist_t *snaps, boolean_t defer, nvlist_t **errlist)
 270 {
 271         nvpair_t *elem;
 272         nvlist_t *args;
 273         int error;
 274         char pool[MAXNAMELEN];
 275 
 276         /* determine the pool name */
 277         elem = nvlist_next_nvpair(snaps, NULL);
 278         if (elem == NULL)
 279                 return (0);
 280         (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
 281         pool[strcspn(pool, "/@")] = '\0';
 282 
 283         args = fnvlist_alloc();
 284         fnvlist_add_nvlist(args, "snaps", snaps);
 285         if (defer)
 286                 fnvlist_add_boolean(args, "defer");
 287 
 288         error = lzc_ioctl(ZFS_IOC_DESTROY_SNAPS, pool, args, errlist);
 289         nvlist_free(args);
 290 
 291         return (error);
 292 }
 293 
 294 int
 295 lzc_snaprange_space(const char *firstsnap, const char *lastsnap,
 296     uint64_t *usedp)
 297 {
 298         nvlist_t *args;
 299         nvlist_t *result;
 300         int err;
 301         char fs[MAXNAMELEN];
 302         char *atp;
 303 
 304         /* determine the fs name */
 305         (void) strlcpy(fs, firstsnap, sizeof (fs));
 306         atp = strchr(fs, '@');
 307         if (atp == NULL)
 308                 return (EINVAL);
 309         *atp = '\0';
 310 
 311         args = fnvlist_alloc();
 312         fnvlist_add_string(args, "firstsnap", firstsnap);
 313 
 314         err = lzc_ioctl(ZFS_IOC_SPACE_SNAPS, lastsnap, args, &result);
 315         nvlist_free(args);
 316         if (err == 0)
 317                 *usedp = fnvlist_lookup_uint64(result, "used");
 318         fnvlist_free(result);
 319 
 320         return (err);
 321 }
 322 
 323 boolean_t
 324 lzc_exists(const char *dataset)
 325 {
 326         /*
 327          * The objset_stats ioctl is still legacy, so we need to construct our
 328          * own zfs_cmd_t rather than using zfsc_ioctl().
 329          */
 330         zfs_cmd_t zc = { 0 };
 331 
 332         (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
 333         return (ioctl(g_fd, ZFS_IOC_OBJSET_STATS, &zc) == 0);
 334 }
 335 
 336 /*
 337  * Create "user holds" on snapshots.  If there is a hold on a snapshot,
 338  * the snapshot can not be destroyed.  (However, it can be marked for deletion
 339  * by lzc_destroy_snaps(defer=B_TRUE).)
 340  *
 341  * The keys in the nvlist are snapshot names.
 342  * The snapshots must all be in the same pool.
 343  * The value is the name of the hold (string type).
 344  *
 345  * If cleanup_fd is not -1, it must be the result of open("/dev/zfs", O_EXCL).
 346  * In this case, when the cleanup_fd is closed (including on process
 347  * termination), the holds will be released.  If the system is shut down
 348  * uncleanly, the holds will be released when the pool is next opened
 349  * or imported.
 350  *
 351  * Holds for snapshots which don't exist will be skipped and have an entry
 352  * added to errlist, but will not cause an overall failure, except in the
 353  * case that all holds where skipped.
 354  *
 355  * The return value will be ENOENT if none of the snapshots for the requested
 356  * holds existed.
 357  *
 358  * The return value will be 0 if the nvl holds was empty or all holds, for
 359  * snapshots that existed, were succesfully created and at least one hold
 360  * was created.
 361  *
 362  * Otherwise the return value will be the errno of a (unspecified) hold that
 363  * failed and no holds will be created.
 364  *
 365  * In all cases the errlist will have an entry for each hold that failed
 366  * (name = snapshot), with its value being the error code (int32).
 367  */
 368 int
 369 lzc_hold(nvlist_t *holds, int cleanup_fd, nvlist_t **errlist)
 370 {
 371         char pool[MAXNAMELEN];
 372         nvlist_t *args;
 373         nvpair_t *elem;
 374         int error;
 375 
 376         /* determine the pool name */
 377         elem = nvlist_next_nvpair(holds, NULL);
 378         if (elem == NULL)
 379                 return (0);
 380         (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
 381         pool[strcspn(pool, "/@")] = '\0';
 382 
 383         args = fnvlist_alloc();
 384         fnvlist_add_nvlist(args, "holds", holds);
 385         if (cleanup_fd != -1)
 386                 fnvlist_add_int32(args, "cleanup_fd", cleanup_fd);
 387 
 388         error = lzc_ioctl(ZFS_IOC_HOLD, pool, args, errlist);
 389         nvlist_free(args);
 390         return (error);
 391 }
 392 
 393 /*
 394  * Release "user holds" on snapshots.  If the snapshot has been marked for
 395  * deferred destroy (by lzc_destroy_snaps(defer=B_TRUE)), it does not have
 396  * any clones, and all the user holds are removed, then the snapshot will be
 397  * destroyed.
 398  *
 399  * The keys in the nvlist are snapshot names.
 400  * The snapshots must all be in the same pool.
 401  * The value is a nvlist whose keys are the holds to remove.
 402  *
 403  * Holds which failed to release because they didn't exist will have an entry
 404  * added to errlist, but will not cause an overall failure, except in the
 405  * case that all releases where skipped.
 406  *
 407  * The return value will be ENOENT if none of the specified holds existed.
 408  *
 409  * The return value will be 0 if the nvl holds was empty or all holds, that
 410  * existed, were succesfully removed and at least one hold was removed.
 411  *
 412  * Otherwise the return value will be the errno of a (unspecified) hold that
 413  * failed to release and no holds will be released.
 414  *
 415  * In all cases the errlist will have an entry for each hold that failed to
 416  * to release.
 417  */
 418 int
 419 lzc_release(nvlist_t *holds, nvlist_t **errlist)
 420 {
 421         char pool[MAXNAMELEN];
 422         nvpair_t *elem;
 423 
 424         /* determine the pool name */
 425         elem = nvlist_next_nvpair(holds, NULL);
 426         if (elem == NULL)
 427                 return (0);
 428         (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
 429         pool[strcspn(pool, "/@")] = '\0';
 430 
 431         return (lzc_ioctl(ZFS_IOC_RELEASE, pool, holds, errlist));
 432 }
 433 
 434 /*
 435  * Retrieve list of user holds on the specified snapshot.
 436  *
 437  * On success, *holdsp will be set to a nvlist which the caller must free.
 438  * The keys are the names of the holds, and the value is the creation time
 439  * of the hold (uint64) in seconds since the epoch.
 440  */
 441 int
 442 lzc_get_holds(const char *snapname, nvlist_t **holdsp)
 443 {
 444         int error;
 445         nvlist_t *innvl = fnvlist_alloc();
 446         error = lzc_ioctl(ZFS_IOC_GET_HOLDS, snapname, innvl, holdsp);
 447         fnvlist_free(innvl);
 448         return (error);
 449 }
 450 
 451 /*
 452  * If fromsnap is NULL, a full (non-incremental) stream will be sent.
 453  */
 454 int
 455 lzc_send(const char *snapname, const char *fromsnap, int fd)
 456 {
 457         nvlist_t *args;
 458         int err;
 459 
 460         args = fnvlist_alloc();
 461         fnvlist_add_int32(args, "fd", fd);
 462         if (fromsnap != NULL)
 463                 fnvlist_add_string(args, "fromsnap", fromsnap);
 464         err = lzc_ioctl(ZFS_IOC_SEND_NEW, snapname, args, NULL);
 465         nvlist_free(args);
 466         return (err);
 467 }
 468 
 469 /*
 470  * If fromsnap is NULL, a full (non-incremental) stream will be estimated.
 471  */
 472 int
 473 lzc_send_space(const char *snapname, const char *fromsnap, uint64_t *spacep)
 474 {
 475         nvlist_t *args;
 476         nvlist_t *result;
 477         int err;
 478 
 479         args = fnvlist_alloc();
 480         if (fromsnap != NULL)
 481                 fnvlist_add_string(args, "fromsnap", fromsnap);
 482         err = lzc_ioctl(ZFS_IOC_SEND_SPACE, snapname, args, &result);
 483         nvlist_free(args);
 484         if (err == 0)
 485                 *spacep = fnvlist_lookup_uint64(result, "space");
 486         nvlist_free(result);
 487         return (err);
 488 }
 489 
 490 static int
 491 recv_read(int fd, void *buf, int ilen)
 492 {
 493         char *cp = buf;
 494         int rv;
 495         int len = ilen;
 496 
 497         do {
 498                 rv = read(fd, cp, len);
 499                 cp += rv;
 500                 len -= rv;
 501         } while (rv > 0);
 502 
 503         if (rv < 0 || len != 0)
 504                 return (EIO);
 505 
 506         return (0);
 507 }
 508 
 509 /*
 510  * The simplest receive case: receive from the specified fd, creating the
 511  * specified snapshot.  Apply the specified properties a "received" properties
 512  * (which can be overridden by locally-set properties).  If the stream is a
 513  * clone, its origin snapshot must be specified by 'origin'.  The 'force'
 514  * flag will cause the target filesystem to be rolled back or destroyed if
 515  * necessary to receive.
 516  *
 517  * Return 0 on success or an errno on failure.
 518  *
 519  * Note: this interface does not work on dedup'd streams
 520  * (those with DMU_BACKUP_FEATURE_DEDUP).
 521  */
 522 int
 523 lzc_receive(const char *snapname, nvlist_t *props, const char *origin,
 524     boolean_t force, int fd)
 525 {
 526         /*
 527          * The receive ioctl is still legacy, so we need to construct our own
 528          * zfs_cmd_t rather than using zfsc_ioctl().
 529          */
 530         zfs_cmd_t zc = { 0 };
 531         char *atp;
 532         char *packed = NULL;
 533         size_t size;
 534         dmu_replay_record_t drr;
 535         int error;
 536 
 537         ASSERT3S(g_refcount, >, 0);
 538 
 539         /* zc_name is name of containing filesystem */
 540         (void) strlcpy(zc.zc_name, snapname, sizeof (zc.zc_name));
 541         atp = strchr(zc.zc_name, '@');
 542         if (atp == NULL)
 543                 return (EINVAL);
 544         *atp = '\0';
 545 
 546         /* if the fs does not exist, try its parent. */
 547         if (!lzc_exists(zc.zc_name)) {
 548                 char *slashp = strrchr(zc.zc_name, '/');
 549                 if (slashp == NULL)
 550                         return (ENOENT);
 551                 *slashp = '\0';
 552 
 553         }
 554 
 555         /* zc_value is full name of the snapshot to create */
 556         (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
 557 
 558         if (props != NULL) {
 559                 /* zc_nvlist_src is props to set */
 560                 packed = fnvlist_pack(props, &size);
 561                 zc.zc_nvlist_src = (uint64_t)(uintptr_t)packed;
 562                 zc.zc_nvlist_src_size = size;
 563         }
 564 
 565         /* zc_string is name of clone origin (if DRR_FLAG_CLONE) */
 566         if (origin != NULL)
 567                 (void) strlcpy(zc.zc_string, origin, sizeof (zc.zc_string));
 568 
 569         /* zc_begin_record is non-byteswapped BEGIN record */
 570         error = recv_read(fd, &drr, sizeof (drr));
 571         if (error != 0)
 572                 goto out;
 573         zc.zc_begin_record = drr.drr_u.drr_begin;
 574 
 575         /* zc_cookie is fd to read from */
 576         zc.zc_cookie = fd;
 577 
 578         /* zc guid is force flag */
 579         zc.zc_guid = force;
 580 
 581         /* zc_cleanup_fd is unused */
 582         zc.zc_cleanup_fd = -1;
 583 
 584         error = ioctl(g_fd, ZFS_IOC_RECV, &zc);
 585         if (error != 0)
 586                 error = errno;
 587 
 588 out:
 589         if (packed != NULL)
 590                 fnvlist_pack_free(packed, size);
 591         free((void*)(uintptr_t)zc.zc_nvlist_dst);
 592         return (error);
 593 }