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 (c) 2014 Integros [integros.com]
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 = -1;
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
114 if (g_refcount > 0)
115 g_refcount--;
116
117 if (g_refcount == 0 && g_fd != -1) {
118 (void) close(g_fd);
119 g_fd = -1;
120 }
121 (void) pthread_mutex_unlock(&g_lock);
122 }
123
124 static int
125 lzc_ioctl(zfs_ioc_t ioc, const char *name,
126 nvlist_t *source, nvlist_t **resultp)
127 {
128 zfs_cmd_t zc = { 0 };
129 int error = 0;
130 char *packed;
131 size_t size;
132
133 ASSERT3S(g_refcount, >, 0);
134 VERIFY3S(g_fd, !=, -1);
135
136 (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
137
138 packed = fnvlist_pack(source, &size);
139 zc.zc_nvlist_src = (uint64_t)(uintptr_t)packed;
140 zc.zc_nvlist_src_size = size;
141
142 if (resultp != NULL) {
143 *resultp = NULL;
144 zc.zc_nvlist_dst_size = MAX(size * 2, 128 * 1024);
145 zc.zc_nvlist_dst = (uint64_t)(uintptr_t)
146 malloc(zc.zc_nvlist_dst_size);
147 if (zc.zc_nvlist_dst == NULL) {
148 error = ENOMEM;
149 goto out;
150 }
151 }
152
153 while (ioctl(g_fd, ioc, &zc) != 0) {
154 if (errno == ENOMEM && resultp != NULL) {
155 free((void *)(uintptr_t)zc.zc_nvlist_dst);
156 zc.zc_nvlist_dst_size *= 2;
157 zc.zc_nvlist_dst = (uint64_t)(uintptr_t)
158 malloc(zc.zc_nvlist_dst_size);
159 if (zc.zc_nvlist_dst == NULL) {
160 error = ENOMEM;
161 goto out;
162 }
163 } else {
164 error = errno;
165 break;
166 }
167 }
168 if (zc.zc_nvlist_dst_filled) {
169 *resultp = fnvlist_unpack((void *)(uintptr_t)zc.zc_nvlist_dst,
170 zc.zc_nvlist_dst_size);
171 }
172
173 out:
174 fnvlist_pack_free(packed, size);
175 free((void *)(uintptr_t)zc.zc_nvlist_dst);
176 return (error);
177 }
178
179 int
180 lzc_create(const char *fsname, enum lzc_dataset_type type, nvlist_t *props)
181 {
182 int error;
183 nvlist_t *args = fnvlist_alloc();
184 fnvlist_add_int32(args, "type", (dmu_objset_type_t)type);
185 if (props != NULL)
186 fnvlist_add_nvlist(args, "props", props);
187 error = lzc_ioctl(ZFS_IOC_CREATE, fsname, args, NULL);
188 nvlist_free(args);
189 return (error);
190 }
191
192 int
193 lzc_clone(const char *fsname, const char *origin,
194 nvlist_t *props)
195 {
196 int error;
197 nvlist_t *args = fnvlist_alloc();
198 fnvlist_add_string(args, "origin", origin);
199 if (props != NULL)
200 fnvlist_add_nvlist(args, "props", props);
201 error = lzc_ioctl(ZFS_IOC_CLONE, fsname, args, NULL);
202 nvlist_free(args);
203 return (error);
204 }
205
206 /*
207 * Creates snapshots.
208 *
209 * The keys in the snaps nvlist are the snapshots to be created.
210 * They must all be in the same pool.
211 *
212 * The props nvlist is properties to set. Currently only user properties
213 * are supported. { user:prop_name -> string value }
214 *
215 * The returned results nvlist will have an entry for each snapshot that failed.
216 * The value will be the (int32) error code.
217 *
218 * The return value will be 0 if all snapshots were created, otherwise it will
219 * be the errno of a (unspecified) snapshot that failed.
220 */
221 int
222 lzc_snapshot(nvlist_t *snaps, nvlist_t *props, nvlist_t **errlist)
223 {
224 nvpair_t *elem;
225 nvlist_t *args;
226 int error;
227 char pool[ZFS_MAX_DATASET_NAME_LEN];
228
229 *errlist = NULL;
230
231 /* determine the pool name */
232 elem = nvlist_next_nvpair(snaps, NULL);
233 if (elem == NULL)
234 return (0);
235 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
236 pool[strcspn(pool, "/@")] = '\0';
237
238 args = fnvlist_alloc();
239 fnvlist_add_nvlist(args, "snaps", snaps);
240 if (props != NULL)
241 fnvlist_add_nvlist(args, "props", props);
242
243 error = lzc_ioctl(ZFS_IOC_SNAPSHOT, pool, args, errlist);
244 nvlist_free(args);
245
246 return (error);
247 }
248
249 /*
250 * Destroys snapshots.
251 *
252 * The keys in the snaps nvlist are the snapshots to be destroyed.
253 * They must all be in the same pool.
254 *
255 * Snapshots that do not exist will be silently ignored.
256 *
257 * If 'defer' is not set, and a snapshot has user holds or clones, the
258 * destroy operation will fail and none of the snapshots will be
259 * destroyed.
260 *
261 * If 'defer' is set, and a snapshot has user holds or clones, it will be
262 * marked for deferred destruction, and will be destroyed when the last hold
263 * or clone is removed/destroyed.
264 *
265 * The return value will be 0 if all snapshots were destroyed (or marked for
266 * later destruction if 'defer' is set) or didn't exist to begin with.
267 *
268 * Otherwise the return value will be the errno of a (unspecified) snapshot
269 * that failed, no snapshots will be destroyed, and the errlist will have an
270 * entry for each snapshot that failed. The value in the errlist will be
271 * the (int32) error code.
272 */
273 int
274 lzc_destroy_snaps(nvlist_t *snaps, boolean_t defer, nvlist_t **errlist)
275 {
276 nvpair_t *elem;
277 nvlist_t *args;
278 int error;
279 char pool[ZFS_MAX_DATASET_NAME_LEN];
280
281 /* determine the pool name */
282 elem = nvlist_next_nvpair(snaps, NULL);
283 if (elem == NULL)
284 return (0);
285 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
286 pool[strcspn(pool, "/@")] = '\0';
287
288 args = fnvlist_alloc();
289 fnvlist_add_nvlist(args, "snaps", snaps);
290 if (defer)
291 fnvlist_add_boolean(args, "defer");
292
293 error = lzc_ioctl(ZFS_IOC_DESTROY_SNAPS, pool, args, errlist);
294 nvlist_free(args);
295
296 return (error);
297 }
298
299 int
300 lzc_snaprange_space(const char *firstsnap, const char *lastsnap,
301 uint64_t *usedp)
302 {
303 nvlist_t *args;
304 nvlist_t *result;
305 int err;
306 char fs[ZFS_MAX_DATASET_NAME_LEN];
307 char *atp;
308
309 /* determine the fs name */
310 (void) strlcpy(fs, firstsnap, sizeof (fs));
311 atp = strchr(fs, '@');
312 if (atp == NULL)
313 return (EINVAL);
314 *atp = '\0';
315
316 args = fnvlist_alloc();
317 fnvlist_add_string(args, "firstsnap", firstsnap);
318
319 err = lzc_ioctl(ZFS_IOC_SPACE_SNAPS, lastsnap, args, &result);
320 nvlist_free(args);
321 if (err == 0)
322 *usedp = fnvlist_lookup_uint64(result, "used");
323 fnvlist_free(result);
324
325 return (err);
326 }
327
328 boolean_t
329 lzc_exists(const char *dataset)
330 {
331 /*
332 * The objset_stats ioctl is still legacy, so we need to construct our
333 * own zfs_cmd_t rather than using zfsc_ioctl().
334 */
335 zfs_cmd_t zc = { 0 };
336
337 ASSERT3S(g_refcount, >, 0);
338 VERIFY3S(g_fd, !=, -1);
339
340 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
341 return (ioctl(g_fd, ZFS_IOC_OBJSET_STATS, &zc) == 0);
342 }
343
344 /*
345 * Create "user holds" on snapshots. If there is a hold on a snapshot,
346 * the snapshot can not be destroyed. (However, it can be marked for deletion
347 * by lzc_destroy_snaps(defer=B_TRUE).)
348 *
349 * The keys in the nvlist are snapshot names.
350 * The snapshots must all be in the same pool.
351 * The value is the name of the hold (string type).
352 *
353 * If cleanup_fd is not -1, it must be the result of open("/dev/zfs", O_EXCL).
354 * In this case, when the cleanup_fd is closed (including on process
355 * termination), the holds will be released. If the system is shut down
356 * uncleanly, the holds will be released when the pool is next opened
357 * or imported.
358 *
359 * Holds for snapshots which don't exist will be skipped and have an entry
360 * added to errlist, but will not cause an overall failure.
361 *
362 * The return value will be 0 if all holds, for snapshots that existed,
363 * were succesfully created.
364 *
365 * Otherwise the return value will be the errno of a (unspecified) hold that
366 * failed and no holds will be created.
367 *
368 * In all cases the errlist will have an entry for each hold that failed
369 * (name = snapshot), with its value being the error code (int32).
370 */
371 int
372 lzc_hold(nvlist_t *holds, int cleanup_fd, nvlist_t **errlist)
373 {
374 char pool[ZFS_MAX_DATASET_NAME_LEN];
375 nvlist_t *args;
376 nvpair_t *elem;
377 int error;
378
379 /* determine the pool name */
380 elem = nvlist_next_nvpair(holds, NULL);
381 if (elem == NULL)
382 return (0);
383 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
384 pool[strcspn(pool, "/@")] = '\0';
385
386 args = fnvlist_alloc();
387 fnvlist_add_nvlist(args, "holds", holds);
388 if (cleanup_fd != -1)
389 fnvlist_add_int32(args, "cleanup_fd", cleanup_fd);
390
391 error = lzc_ioctl(ZFS_IOC_HOLD, pool, args, errlist);
392 nvlist_free(args);
393 return (error);
394 }
395
396 /*
397 * Release "user holds" on snapshots. If the snapshot has been marked for
398 * deferred destroy (by lzc_destroy_snaps(defer=B_TRUE)), it does not have
399 * any clones, and all the user holds are removed, then the snapshot will be
400 * destroyed.
401 *
402 * The keys in the nvlist are snapshot names.
403 * The snapshots must all be in the same pool.
404 * The value is a nvlist whose keys are the holds to remove.
405 *
406 * Holds which failed to release because they didn't exist will have an entry
407 * added to errlist, but will not cause an overall failure.
408 *
409 * The return value will be 0 if the nvl holds was empty or all holds that
410 * existed, were successfully 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[ZFS_MAX_DATASET_NAME_LEN];
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 * Generate a zfs send stream for the specified snapshot and write it to
453 * the specified file descriptor.
454 *
455 * "snapname" is the full name of the snapshot to send (e.g. "pool/fs@snap")
456 *
457 * If "from" is NULL, a full (non-incremental) stream will be sent.
458 * If "from" is non-NULL, it must be the full name of a snapshot or
459 * bookmark to send an incremental from (e.g. "pool/fs@earlier_snap" or
460 * "pool/fs#earlier_bmark"). If non-NULL, the specified snapshot or
461 * bookmark must represent an earlier point in the history of "snapname").
462 * It can be an earlier snapshot in the same filesystem or zvol as "snapname",
463 * or it can be the origin of "snapname"'s filesystem, or an earlier
464 * snapshot in the origin, etc.
465 *
466 * "fd" is the file descriptor to write the send stream to.
467 *
468 * If "flags" contains LZC_SEND_FLAG_LARGE_BLOCK, the stream is permitted
469 * to contain DRR_WRITE records with drr_length > 128K, and DRR_OBJECT
470 * records with drr_blksz > 128K.
471 *
472 * If "flags" contains LZC_SEND_FLAG_EMBED_DATA, the stream is permitted
473 * to contain DRR_WRITE_EMBEDDED records with drr_etype==BP_EMBEDDED_TYPE_DATA,
474 * which the receiving system must support (as indicated by support
475 * for the "embedded_data" feature).
476 */
477 int
478 lzc_send(const char *snapname, const char *from, int fd,
479 enum lzc_send_flags flags)
480 {
481 return (lzc_send_resume(snapname, from, fd, flags, 0, 0));
482 }
483
484 int
485 lzc_send_resume(const char *snapname, const char *from, int fd,
486 enum lzc_send_flags flags, uint64_t resumeobj, uint64_t resumeoff)
487 {
488 nvlist_t *args;
489 int err;
490
491 args = fnvlist_alloc();
492 fnvlist_add_int32(args, "fd", fd);
493 if (from != NULL)
494 fnvlist_add_string(args, "fromsnap", from);
495 if (flags & LZC_SEND_FLAG_LARGE_BLOCK)
496 fnvlist_add_boolean(args, "largeblockok");
497 if (flags & LZC_SEND_FLAG_EMBED_DATA)
498 fnvlist_add_boolean(args, "embedok");
499 if (flags & LZC_SEND_FLAG_COMPRESS)
500 fnvlist_add_boolean(args, "compressok");
501 if (resumeobj != 0 || resumeoff != 0) {
502 fnvlist_add_uint64(args, "resume_object", resumeobj);
503 fnvlist_add_uint64(args, "resume_offset", resumeoff);
504 }
505 err = lzc_ioctl(ZFS_IOC_SEND_NEW, snapname, args, NULL);
506 nvlist_free(args);
507 return (err);
508 }
509
510 /*
511 * "from" can be NULL, a snapshot, or a bookmark.
512 *
513 * If from is NULL, a full (non-incremental) stream will be estimated. This
514 * is calculated very efficiently.
515 *
516 * If from is a snapshot, lzc_send_space uses the deadlists attached to
517 * each snapshot to efficiently estimate the stream size.
518 *
519 * If from is a bookmark, the indirect blocks in the destination snapshot
520 * are traversed, looking for blocks with a birth time since the creation TXG of
521 * the snapshot this bookmark was created from. This will result in
522 * significantly more I/O and be less efficient than a send space estimation on
523 * an equivalent snapshot.
524 */
525 int
526 lzc_send_space(const char *snapname, const char *from,
527 enum lzc_send_flags flags, uint64_t *spacep)
528 {
529 nvlist_t *args;
530 nvlist_t *result;
531 int err;
532
533 args = fnvlist_alloc();
534 if (from != NULL)
535 fnvlist_add_string(args, "from", from);
536 if (flags & LZC_SEND_FLAG_LARGE_BLOCK)
537 fnvlist_add_boolean(args, "largeblockok");
538 if (flags & LZC_SEND_FLAG_EMBED_DATA)
539 fnvlist_add_boolean(args, "embedok");
540 if (flags & LZC_SEND_FLAG_COMPRESS)
541 fnvlist_add_boolean(args, "compressok");
542 err = lzc_ioctl(ZFS_IOC_SEND_SPACE, snapname, args, &result);
543 nvlist_free(args);
544 if (err == 0)
545 *spacep = fnvlist_lookup_uint64(result, "space");
546 nvlist_free(result);
547 return (err);
548 }
549
550 static int
551 recv_read(int fd, void *buf, int ilen)
552 {
553 char *cp = buf;
554 int rv;
555 int len = ilen;
556
557 do {
558 rv = read(fd, cp, len);
559 cp += rv;
560 len -= rv;
561 } while (rv > 0);
562
563 if (rv < 0 || len != 0)
564 return (EIO);
565
566 return (0);
567 }
568
569 static int
570 recv_impl(const char *snapname, nvlist_t *props, const char *origin,
571 boolean_t force, boolean_t resumable, int fd,
572 const dmu_replay_record_t *begin_record)
573 {
574 /*
575 * The receive ioctl is still legacy, so we need to construct our own
576 * zfs_cmd_t rather than using zfsc_ioctl().
577 */
578 zfs_cmd_t zc = { 0 };
579 char *atp;
580 char *packed = NULL;
581 size_t size;
582 int error;
583
584 ASSERT3S(g_refcount, >, 0);
585 VERIFY3S(g_fd, !=, -1);
586
587 /* zc_name is name of containing filesystem */
588 (void) strlcpy(zc.zc_name, snapname, sizeof (zc.zc_name));
589 atp = strchr(zc.zc_name, '@');
590 if (atp == NULL)
591 return (EINVAL);
592 *atp = '\0';
593
594 /* if the fs does not exist, try its parent. */
595 if (!lzc_exists(zc.zc_name)) {
596 char *slashp = strrchr(zc.zc_name, '/');
597 if (slashp == NULL)
598 return (ENOENT);
599 *slashp = '\0';
600
601 }
602
603 /* zc_value is full name of the snapshot to create */
604 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
605
606 if (props != NULL) {
607 /* zc_nvlist_src is props to set */
608 packed = fnvlist_pack(props, &size);
609 zc.zc_nvlist_src = (uint64_t)(uintptr_t)packed;
610 zc.zc_nvlist_src_size = size;
611 }
612
613 /* zc_string is name of clone origin (if DRR_FLAG_CLONE) */
614 if (origin != NULL)
615 (void) strlcpy(zc.zc_string, origin, sizeof (zc.zc_string));
616
617 /* zc_begin_record is non-byteswapped BEGIN record */
618 if (begin_record == NULL) {
619 error = recv_read(fd, &zc.zc_begin_record,
620 sizeof (zc.zc_begin_record));
621 if (error != 0)
622 goto out;
623 } else {
624 zc.zc_begin_record = *begin_record;
625 }
626
627 /* zc_cookie is fd to read from */
628 zc.zc_cookie = fd;
629
630 /* zc guid is force flag */
631 zc.zc_guid = force;
632
633 zc.zc_resumable = resumable;
634
635 /* zc_cleanup_fd is unused */
636 zc.zc_cleanup_fd = -1;
637
638 error = ioctl(g_fd, ZFS_IOC_RECV, &zc);
639 if (error != 0)
640 error = errno;
641
642 out:
643 if (packed != NULL)
644 fnvlist_pack_free(packed, size);
645 free((void*)(uintptr_t)zc.zc_nvlist_dst);
646 return (error);
647 }
648
649 /*
650 * The simplest receive case: receive from the specified fd, creating the
651 * specified snapshot. Apply the specified properties as "received" properties
652 * (which can be overridden by locally-set properties). If the stream is a
653 * clone, its origin snapshot must be specified by 'origin'. The 'force'
654 * flag will cause the target filesystem to be rolled back or destroyed if
655 * necessary to receive.
656 *
657 * Return 0 on success or an errno on failure.
658 *
659 * Note: this interface does not work on dedup'd streams
660 * (those with DMU_BACKUP_FEATURE_DEDUP).
661 */
662 int
663 lzc_receive(const char *snapname, nvlist_t *props, const char *origin,
664 boolean_t force, int fd)
665 {
666 return (recv_impl(snapname, props, origin, force, B_FALSE, fd, NULL));
667 }
668
669 /*
670 * Like lzc_receive, but if the receive fails due to premature stream
671 * termination, the intermediate state will be preserved on disk. In this
672 * case, ECKSUM will be returned. The receive may subsequently be resumed
673 * with a resuming send stream generated by lzc_send_resume().
674 */
675 int
676 lzc_receive_resumable(const char *snapname, nvlist_t *props, const char *origin,
677 boolean_t force, int fd)
678 {
679 return (recv_impl(snapname, props, origin, force, B_TRUE, fd, NULL));
680 }
681
682 /*
683 * Like lzc_receive, but allows the caller to read the begin record and then to
684 * pass it in. That could be useful if the caller wants to derive, for example,
685 * the snapname or the origin parameters based on the information contained in
686 * the begin record.
687 * The begin record must be in its original form as read from the stream,
688 * in other words, it should not be byteswapped.
689 *
690 * The 'resumable' parameter allows to obtain the same behavior as with
691 * lzc_receive_resumable.
692 */
693 int
694 lzc_receive_with_header(const char *snapname, nvlist_t *props,
695 const char *origin, boolean_t force, boolean_t resumable, int fd,
696 const dmu_replay_record_t *begin_record)
697 {
698 if (begin_record == NULL)
699 return (EINVAL);
700 return (recv_impl(snapname, props, origin, force, resumable, fd,
701 begin_record));
702 }
703
704 /*
705 * Roll back this filesystem or volume to its most recent snapshot.
706 * If snapnamebuf is not NULL, it will be filled in with the name
707 * of the most recent snapshot.
708 *
709 * Return 0 on success or an errno on failure.
710 */
711 int
712 lzc_rollback(const char *fsname, char *snapnamebuf, int snapnamelen)
713 {
714 nvlist_t *args;
715 nvlist_t *result;
716 int err;
717
718 args = fnvlist_alloc();
719 err = lzc_ioctl(ZFS_IOC_ROLLBACK, fsname, args, &result);
720 nvlist_free(args);
721 if (err == 0 && snapnamebuf != NULL) {
722 const char *snapname = fnvlist_lookup_string(result, "target");
723 (void) strlcpy(snapnamebuf, snapname, snapnamelen);
724 }
725 nvlist_free(result);
726
727 return (err);
728 }
729
730 /*
731 * Creates bookmarks.
732 *
733 * The bookmarks nvlist maps from name of the bookmark (e.g. "pool/fs#bmark") to
734 * the name of the snapshot (e.g. "pool/fs@snap"). All the bookmarks and
735 * snapshots must be in the same pool.
736 *
737 * The returned results nvlist will have an entry for each bookmark that failed.
738 * The value will be the (int32) error code.
739 *
740 * The return value will be 0 if all bookmarks were created, otherwise it will
741 * be the errno of a (undetermined) bookmarks that failed.
742 */
743 int
744 lzc_bookmark(nvlist_t *bookmarks, nvlist_t **errlist)
745 {
746 nvpair_t *elem;
747 int error;
748 char pool[ZFS_MAX_DATASET_NAME_LEN];
749
750 /* determine the pool name */
751 elem = nvlist_next_nvpair(bookmarks, NULL);
752 if (elem == NULL)
753 return (0);
754 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
755 pool[strcspn(pool, "/#")] = '\0';
756
757 error = lzc_ioctl(ZFS_IOC_BOOKMARK, pool, bookmarks, errlist);
758
759 return (error);
760 }
761
762 /*
763 * Retrieve bookmarks.
764 *
765 * Retrieve the list of bookmarks for the given file system. The props
766 * parameter is an nvlist of property names (with no values) that will be
767 * returned for each bookmark.
768 *
769 * The following are valid properties on bookmarks, all of which are numbers
770 * (represented as uint64 in the nvlist)
771 *
772 * "guid" - globally unique identifier of the snapshot it refers to
773 * "createtxg" - txg when the snapshot it refers to was created
774 * "creation" - timestamp when the snapshot it refers to was created
775 *
776 * The format of the returned nvlist as follows:
777 * <short name of bookmark> -> {
778 * <name of property> -> {
779 * "value" -> uint64
780 * }
781 * }
782 */
783 int
784 lzc_get_bookmarks(const char *fsname, nvlist_t *props, nvlist_t **bmarks)
785 {
786 return (lzc_ioctl(ZFS_IOC_GET_BOOKMARKS, fsname, props, bmarks));
787 }
788
789 /*
790 * Destroys bookmarks.
791 *
792 * The keys in the bmarks nvlist are the bookmarks to be destroyed.
793 * They must all be in the same pool. Bookmarks are specified as
794 * <fs>#<bmark>.
795 *
796 * Bookmarks that do not exist will be silently ignored.
797 *
798 * The return value will be 0 if all bookmarks that existed were destroyed.
799 *
800 * Otherwise the return value will be the errno of a (undetermined) bookmark
801 * that failed, no bookmarks will be destroyed, and the errlist will have an
802 * entry for each bookmarks that failed. The value in the errlist will be
803 * the (int32) error code.
804 */
805 int
806 lzc_destroy_bookmarks(nvlist_t *bmarks, nvlist_t **errlist)
807 {
808 nvpair_t *elem;
809 int error;
810 char pool[ZFS_MAX_DATASET_NAME_LEN];
811
812 /* determine the pool name */
813 elem = nvlist_next_nvpair(bmarks, NULL);
814 if (elem == NULL)
815 return (0);
816 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
817 pool[strcspn(pool, "/#")] = '\0';
818
819 error = lzc_ioctl(ZFS_IOC_DESTROY_BOOKMARKS, pool, bmarks, errlist);
820
821 return (error);
822 }