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 0 if all snapshots were destroyed (or marked for
 258  * later destruction if 'defer' is set) or didn't exist to begin with.
 259  *
 260  * Otherwise the return value will be the errno of a (unspecified) snapshot
 261  * that failed, no snapshots will be destroyed, and the errlist will have an
 262  * entry for each snapshot that failed.  The value in the errlist will be
 263  * the (int32) error code.
 264  */
 265 int
 266 lzc_destroy_snaps(nvlist_t *snaps, boolean_t defer, nvlist_t **errlist)
 267 {
 268         nvpair_t *elem;
 269         nvlist_t *args;
 270         int error;
 271         char pool[MAXNAMELEN];
 272 
 273         /* determine the pool name */
 274         elem = nvlist_next_nvpair(snaps, NULL);
 275         if (elem == NULL)
 276                 return (0);
 277         (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
 278         pool[strcspn(pool, "/@")] = '\0';
 279 
 280         args = fnvlist_alloc();
 281         fnvlist_add_nvlist(args, "snaps", snaps);
 282         if (defer)
 283                 fnvlist_add_boolean(args, "defer");
 284 
 285         error = lzc_ioctl(ZFS_IOC_DESTROY_SNAPS, pool, args, errlist);
 286         nvlist_free(args);
 287 
 288         return (error);
 289 
 290 }
 291 
 292 int
 293 lzc_snaprange_space(const char *firstsnap, const char *lastsnap,
 294     uint64_t *usedp)
 295 {
 296         nvlist_t *args;
 297         nvlist_t *result;
 298         int err;
 299         char fs[MAXNAMELEN];
 300         char *atp;
 301 
 302         /* determine the fs name */
 303         (void) strlcpy(fs, firstsnap, sizeof (fs));
 304         atp = strchr(fs, '@');
 305         if (atp == NULL)
 306                 return (EINVAL);
 307         *atp = '\0';
 308 
 309         args = fnvlist_alloc();
 310         fnvlist_add_string(args, "firstsnap", firstsnap);
 311 
 312         err = lzc_ioctl(ZFS_IOC_SPACE_SNAPS, lastsnap, args, &result);
 313         nvlist_free(args);
 314         if (err == 0)
 315                 *usedp = fnvlist_lookup_uint64(result, "used");
 316         fnvlist_free(result);
 317 
 318         return (err);
 319 }
 320 
 321 boolean_t
 322 lzc_exists(const char *dataset)
 323 {
 324         /*
 325          * The objset_stats ioctl is still legacy, so we need to construct our
 326          * own zfs_cmd_t rather than using zfsc_ioctl().
 327          */
 328         zfs_cmd_t zc = { 0 };
 329 
 330         (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
 331         return (ioctl(g_fd, ZFS_IOC_OBJSET_STATS, &zc) == 0);
 332 }
 333 
 334 /*
 335  * Create "user holds" on snapshots.  If there is a hold on a snapshot,
 336  * the snapshot can not be destroyed.  (However, it can be marked for deletion
 337  * by lzc_destroy_snaps(defer=B_TRUE).)
 338  *
 339  * The keys in the nvlist are snapshot names.
 340  * The snapshots must all be in the same pool.
 341  * The value is the name of the hold (string type).
 342  *
 343  * If cleanup_fd is not -1, it must be the result of open("/dev/zfs", O_EXCL).
 344  * In this case, when the cleanup_fd is closed (including on process
 345  * termination), the holds will be released.  If the system is shut down
 346  * uncleanly, the holds will be released when the pool is next opened
 347  * or imported.
 348  *
 349  * The return value will be 0 if all holds were created. Otherwise the return
 350  * value will be the errno of a (unspecified) hold that failed, no holds will
 351  * be created, and the errlist will have an entry for each hold that
 352  * failed (name = snapshot).  The value in the errlist will be the error
 353  * code (int32).
 354  */
 355 int
 356 lzc_hold(nvlist_t *holds, int cleanup_fd, nvlist_t **errlist)
 357 {
 358         char pool[MAXNAMELEN];
 359         nvlist_t *args;
 360         nvpair_t *elem;
 361         int error;
 362 
 363         /* determine the pool name */
 364         elem = nvlist_next_nvpair(holds, NULL);
 365         if (elem == NULL)
 366                 return (0);
 367         (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
 368         pool[strcspn(pool, "/@")] = '\0';
 369 
 370         args = fnvlist_alloc();
 371         fnvlist_add_nvlist(args, "holds", holds);
 372         if (cleanup_fd != -1)
 373                 fnvlist_add_int32(args, "cleanup_fd", cleanup_fd);
 374 
 375         error = lzc_ioctl(ZFS_IOC_HOLD, pool, args, errlist);
 376         nvlist_free(args);
 377         return (error);
 378 }
 379 
 380 /*
 381  * Release "user holds" on snapshots.  If the snapshot has been marked for
 382  * deferred destroy (by lzc_destroy_snaps(defer=B_TRUE)), it does not have
 383  * any clones, and all the user holds are removed, then the snapshot will be
 384  * destroyed.
 385  *
 386  * The keys in the nvlist are snapshot names.
 387  * The snapshots must all be in the same pool.
 388  * The value is a nvlist whose keys are the holds to remove.
 389  *
 390  * The return value will be 0 if all holds were removed.
 391  * Otherwise the return value will be the errno of a (unspecified) release
 392  * that failed, no holds will be released, and the errlist will have an
 393  * entry for each snapshot that has failed releases (name = snapshot).
 394  * The value in the errlist will be the error code (int32) of a failed release.
 395  */
 396 int
 397 lzc_release(nvlist_t *holds, nvlist_t **errlist)
 398 {
 399         char pool[MAXNAMELEN];
 400         nvpair_t *elem;
 401 
 402         /* determine the pool name */
 403         elem = nvlist_next_nvpair(holds, NULL);
 404         if (elem == NULL)
 405                 return (0);
 406         (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
 407         pool[strcspn(pool, "/@")] = '\0';
 408 
 409         return (lzc_ioctl(ZFS_IOC_RELEASE, pool, holds, errlist));
 410 }
 411 
 412 /*
 413  * Retrieve list of user holds on the specified snapshot.
 414  *
 415  * On success, *holdsp will be set to a nvlist which the caller must free.
 416  * The keys are the names of the holds, and the value is the creation time
 417  * of the hold (uint64) in seconds since the epoch.
 418  */
 419 int
 420 lzc_get_holds(const char *snapname, nvlist_t **holdsp)
 421 {
 422         int error;
 423         nvlist_t *innvl = fnvlist_alloc();
 424         error = lzc_ioctl(ZFS_IOC_GET_HOLDS, snapname, innvl, holdsp);
 425         fnvlist_free(innvl);
 426         return (error);
 427 }
 428 
 429 /*
 430  * If fromsnap is NULL, a full (non-incremental) stream will be sent.
 431  */
 432 int
 433 lzc_send(const char *snapname, const char *fromsnap, int fd)
 434 {
 435         nvlist_t *args;
 436         int err;
 437 
 438         args = fnvlist_alloc();
 439         fnvlist_add_int32(args, "fd", fd);
 440         if (fromsnap != NULL)
 441                 fnvlist_add_string(args, "fromsnap", fromsnap);
 442         err = lzc_ioctl(ZFS_IOC_SEND_NEW, snapname, args, NULL);
 443         nvlist_free(args);
 444         return (err);
 445 }
 446 
 447 /*
 448  * If fromsnap is NULL, a full (non-incremental) stream will be estimated.
 449  */
 450 int
 451 lzc_send_space(const char *snapname, const char *fromsnap, uint64_t *spacep)
 452 {
 453         nvlist_t *args;
 454         nvlist_t *result;
 455         int err;
 456 
 457         args = fnvlist_alloc();
 458         if (fromsnap != NULL)
 459                 fnvlist_add_string(args, "fromsnap", fromsnap);
 460         err = lzc_ioctl(ZFS_IOC_SEND_SPACE, snapname, args, &result);
 461         nvlist_free(args);
 462         if (err == 0)
 463                 *spacep = fnvlist_lookup_uint64(result, "space");
 464         nvlist_free(result);
 465         return (err);
 466 }
 467 
 468 static int
 469 recv_read(int fd, void *buf, int ilen)
 470 {
 471         char *cp = buf;
 472         int rv;
 473         int len = ilen;
 474 
 475         do {
 476                 rv = read(fd, cp, len);
 477                 cp += rv;
 478                 len -= rv;
 479         } while (rv > 0);
 480 
 481         if (rv < 0 || len != 0)
 482                 return (EIO);
 483 
 484         return (0);
 485 }
 486 
 487 /*
 488  * The simplest receive case: receive from the specified fd, creating the
 489  * specified snapshot.  Apply the specified properties a "received" properties
 490  * (which can be overridden by locally-set properties).  If the stream is a
 491  * clone, its origin snapshot must be specified by 'origin'.  The 'force'
 492  * flag will cause the target filesystem to be rolled back or destroyed if
 493  * necessary to receive.
 494  *
 495  * Return 0 on success or an errno on failure.
 496  *
 497  * Note: this interface does not work on dedup'd streams
 498  * (those with DMU_BACKUP_FEATURE_DEDUP).
 499  */
 500 int
 501 lzc_receive(const char *snapname, nvlist_t *props, const char *origin,
 502     boolean_t force, int fd)
 503 {
 504         /*
 505          * The receive ioctl is still legacy, so we need to construct our own
 506          * zfs_cmd_t rather than using zfsc_ioctl().
 507          */
 508         zfs_cmd_t zc = { 0 };
 509         char *atp;
 510         char *packed = NULL;
 511         size_t size;
 512         dmu_replay_record_t drr;
 513         int error;
 514 
 515         ASSERT3S(g_refcount, >, 0);
 516 
 517         /* zc_name is name of containing filesystem */
 518         (void) strlcpy(zc.zc_name, snapname, sizeof (zc.zc_name));
 519         atp = strchr(zc.zc_name, '@');
 520         if (atp == NULL)
 521                 return (EINVAL);
 522         *atp = '\0';
 523 
 524         /* if the fs does not exist, try its parent. */
 525         if (!lzc_exists(zc.zc_name)) {
 526                 char *slashp = strrchr(zc.zc_name, '/');
 527                 if (slashp == NULL)
 528                         return (ENOENT);
 529                 *slashp = '\0';
 530 
 531         }
 532 
 533         /* zc_value is full name of the snapshot to create */
 534         (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
 535 
 536         if (props != NULL) {
 537                 /* zc_nvlist_src is props to set */
 538                 packed = fnvlist_pack(props, &size);
 539                 zc.zc_nvlist_src = (uint64_t)(uintptr_t)packed;
 540                 zc.zc_nvlist_src_size = size;
 541         }
 542 
 543         /* zc_string is name of clone origin (if DRR_FLAG_CLONE) */
 544         if (origin != NULL)
 545                 (void) strlcpy(zc.zc_string, origin, sizeof (zc.zc_string));
 546 
 547         /* zc_begin_record is non-byteswapped BEGIN record */
 548         error = recv_read(fd, &drr, sizeof (drr));
 549         if (error != 0)
 550                 goto out;
 551         zc.zc_begin_record = drr.drr_u.drr_begin;
 552 
 553         /* zc_cookie is fd to read from */
 554         zc.zc_cookie = fd;
 555 
 556         /* zc guid is force flag */
 557         zc.zc_guid = force;
 558 
 559         /* zc_cleanup_fd is unused */
 560         zc.zc_cleanup_fd = -1;
 561 
 562         error = ioctl(g_fd, ZFS_IOC_RECV, &zc);
 563         if (error != 0)
 564                 error = errno;
 565 
 566 out:
 567         if (packed != NULL)
 568                 fnvlist_pack_free(packed, size);
 569         free((void*)(uintptr_t)zc.zc_nvlist_dst);
 570         return (error);
 571 }