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