]> git.proxmox.com Git - mirror_zfs.git/blob - lib/libzfs_core/libzfs_core.c
Added no_scrub_restart flag to zpool reopen
[mirror_zfs.git] / lib / libzfs_core / libzfs_core.c
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) 2017 Datto Inc.
26 * Copyright 2017 RackTop Systems.
27 * Copyright (c) 2017 Open-E, Inc. All Rights Reserved.
28 */
29
30 /*
31 * LibZFS_Core (lzc) is intended to replace most functionality in libzfs.
32 * It has the following characteristics:
33 *
34 * - Thread Safe. libzfs_core is accessible concurrently from multiple
35 * threads. This is accomplished primarily by avoiding global data
36 * (e.g. caching). Since it's thread-safe, there is no reason for a
37 * process to have multiple libzfs "instances". Therefore, we store
38 * our few pieces of data (e.g. the file descriptor) in global
39 * variables. The fd is reference-counted so that the libzfs_core
40 * library can be "initialized" multiple times (e.g. by different
41 * consumers within the same process).
42 *
43 * - Committed Interface. The libzfs_core interface will be committed,
44 * therefore consumers can compile against it and be confident that
45 * their code will continue to work on future releases of this code.
46 * Currently, the interface is Evolving (not Committed), but we intend
47 * to commit to it once it is more complete and we determine that it
48 * meets the needs of all consumers.
49 *
50 * - Programmatic Error Handling. libzfs_core communicates errors with
51 * defined error numbers, and doesn't print anything to stdout/stderr.
52 *
53 * - Thin Layer. libzfs_core is a thin layer, marshaling arguments
54 * to/from the kernel ioctls. There is generally a 1:1 correspondence
55 * between libzfs_core functions and ioctls to /dev/zfs.
56 *
57 * - Clear Atomicity. Because libzfs_core functions are generally 1:1
58 * with kernel ioctls, and kernel ioctls are general atomic, each
59 * libzfs_core function is atomic. For example, creating multiple
60 * snapshots with a single call to lzc_snapshot() is atomic -- it
61 * can't fail with only some of the requested snapshots created, even
62 * in the event of power loss or system crash.
63 *
64 * - Continued libzfs Support. Some higher-level operations (e.g.
65 * support for "zfs send -R") are too complicated to fit the scope of
66 * libzfs_core. This functionality will continue to live in libzfs.
67 * Where appropriate, libzfs will use the underlying atomic operations
68 * of libzfs_core. For example, libzfs may implement "zfs send -R |
69 * zfs receive" by using individual "send one snapshot", rename,
70 * destroy, and "receive one snapshot" operations in libzfs_core.
71 * /sbin/zfs and /zbin/zpool will link with both libzfs and
72 * libzfs_core. Other consumers should aim to use only libzfs_core,
73 * since that will be the supported, stable interface going forwards.
74 */
75
76 #include <libzfs_core.h>
77 #include <ctype.h>
78 #include <unistd.h>
79 #include <stdlib.h>
80 #include <string.h>
81 #include <errno.h>
82 #include <fcntl.h>
83 #include <pthread.h>
84 #include <sys/nvpair.h>
85 #include <sys/param.h>
86 #include <sys/types.h>
87 #include <sys/stat.h>
88 #include <sys/zfs_ioctl.h>
89
90 static int g_fd = -1;
91 static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER;
92 static int g_refcount;
93
94 int
95 libzfs_core_init(void)
96 {
97 (void) pthread_mutex_lock(&g_lock);
98 if (g_refcount == 0) {
99 g_fd = open("/dev/zfs", O_RDWR);
100 if (g_fd < 0) {
101 (void) pthread_mutex_unlock(&g_lock);
102 return (errno);
103 }
104 }
105 g_refcount++;
106 (void) pthread_mutex_unlock(&g_lock);
107 return (0);
108 }
109
110 void
111 libzfs_core_fini(void)
112 {
113 (void) pthread_mutex_lock(&g_lock);
114 ASSERT3S(g_refcount, >, 0);
115
116 if (g_refcount > 0)
117 g_refcount--;
118
119 if (g_refcount == 0 && g_fd != -1) {
120 (void) close(g_fd);
121 g_fd = -1;
122 }
123 (void) pthread_mutex_unlock(&g_lock);
124 }
125
126 static int
127 lzc_ioctl(zfs_ioc_t ioc, const char *name,
128 nvlist_t *source, nvlist_t **resultp)
129 {
130 zfs_cmd_t zc = {"\0"};
131 int error = 0;
132 char *packed = NULL;
133 size_t size = 0;
134
135 ASSERT3S(g_refcount, >, 0);
136 VERIFY3S(g_fd, !=, -1);
137
138 if (name != NULL)
139 (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
140
141 if (source != NULL) {
142 packed = fnvlist_pack(source, &size);
143 zc.zc_nvlist_src = (uint64_t)(uintptr_t)packed;
144 zc.zc_nvlist_src_size = size;
145 }
146
147 if (resultp != NULL) {
148 *resultp = NULL;
149 zc.zc_nvlist_dst_size = MAX(size * 2, 128 * 1024);
150 zc.zc_nvlist_dst = (uint64_t)(uintptr_t)
151 malloc(zc.zc_nvlist_dst_size);
152 if (zc.zc_nvlist_dst == (uint64_t)0) {
153 error = ENOMEM;
154 goto out;
155 }
156 }
157
158 while (ioctl(g_fd, ioc, &zc) != 0) {
159 if (errno == ENOMEM && resultp != NULL) {
160 free((void *)(uintptr_t)zc.zc_nvlist_dst);
161 zc.zc_nvlist_dst_size *= 2;
162 zc.zc_nvlist_dst = (uint64_t)(uintptr_t)
163 malloc(zc.zc_nvlist_dst_size);
164 if (zc.zc_nvlist_dst == (uint64_t)0) {
165 error = ENOMEM;
166 goto out;
167 }
168 } else {
169 error = errno;
170 break;
171 }
172 }
173 if (zc.zc_nvlist_dst_filled) {
174 *resultp = fnvlist_unpack((void *)(uintptr_t)zc.zc_nvlist_dst,
175 zc.zc_nvlist_dst_size);
176 }
177
178 out:
179 if (packed != NULL)
180 fnvlist_pack_free(packed, size);
181 free((void *)(uintptr_t)zc.zc_nvlist_dst);
182 return (error);
183 }
184
185 int
186 lzc_create(const char *fsname, enum lzc_dataset_type type, nvlist_t *props,
187 uint8_t *wkeydata, uint_t wkeylen)
188 {
189 int error;
190 nvlist_t *hidden_args = NULL;
191 nvlist_t *args = fnvlist_alloc();
192
193 fnvlist_add_int32(args, "type", (dmu_objset_type_t)type);
194 if (props != NULL)
195 fnvlist_add_nvlist(args, "props", props);
196
197 if (wkeydata != NULL) {
198 hidden_args = fnvlist_alloc();
199 fnvlist_add_uint8_array(hidden_args, "wkeydata", wkeydata,
200 wkeylen);
201 fnvlist_add_nvlist(args, ZPOOL_HIDDEN_ARGS, hidden_args);
202 }
203
204 error = lzc_ioctl(ZFS_IOC_CREATE, fsname, args, NULL);
205 nvlist_free(hidden_args);
206 nvlist_free(args);
207 return (error);
208 }
209
210 int
211 lzc_clone(const char *fsname, const char *origin, nvlist_t *props)
212 {
213 int error;
214 nvlist_t *hidden_args = NULL;
215 nvlist_t *args = fnvlist_alloc();
216
217 fnvlist_add_string(args, "origin", origin);
218 if (props != NULL)
219 fnvlist_add_nvlist(args, "props", props);
220 error = lzc_ioctl(ZFS_IOC_CLONE, fsname, args, NULL);
221 nvlist_free(hidden_args);
222 nvlist_free(args);
223 return (error);
224 }
225
226 int
227 lzc_promote(const char *fsname, char *snapnamebuf, int snapnamelen)
228 {
229 /*
230 * The promote ioctl is still legacy, so we need to construct our
231 * own zfs_cmd_t rather than using lzc_ioctl().
232 */
233 zfs_cmd_t zc = { "\0" };
234
235 ASSERT3S(g_refcount, >, 0);
236 VERIFY3S(g_fd, !=, -1);
237
238 (void) strlcpy(zc.zc_name, fsname, sizeof (zc.zc_name));
239 if (ioctl(g_fd, ZFS_IOC_PROMOTE, &zc) != 0) {
240 int error = errno;
241 if (error == EEXIST && snapnamebuf != NULL)
242 (void) strlcpy(snapnamebuf, zc.zc_string, snapnamelen);
243 return (error);
244 }
245 return (0);
246 }
247
248 /*
249 * Creates snapshots.
250 *
251 * The keys in the snaps nvlist are the snapshots to be created.
252 * They must all be in the same pool.
253 *
254 * The props nvlist is properties to set. Currently only user properties
255 * are supported. { user:prop_name -> string value }
256 *
257 * The returned results nvlist will have an entry for each snapshot that failed.
258 * The value will be the (int32) error code.
259 *
260 * The return value will be 0 if all snapshots were created, otherwise it will
261 * be the errno of a (unspecified) snapshot that failed.
262 */
263 int
264 lzc_snapshot(nvlist_t *snaps, nvlist_t *props, nvlist_t **errlist)
265 {
266 nvpair_t *elem;
267 nvlist_t *args;
268 int error;
269 char pool[ZFS_MAX_DATASET_NAME_LEN];
270
271 *errlist = NULL;
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 (props != NULL)
283 fnvlist_add_nvlist(args, "props", props);
284
285 error = lzc_ioctl(ZFS_IOC_SNAPSHOT, pool, args, errlist);
286 nvlist_free(args);
287
288 return (error);
289 }
290
291 /*
292 * Destroys snapshots.
293 *
294 * The keys in the snaps nvlist are the snapshots to be destroyed.
295 * They must all be in the same pool.
296 *
297 * Snapshots that do not exist will be silently ignored.
298 *
299 * If 'defer' is not set, and a snapshot has user holds or clones, the
300 * destroy operation will fail and none of the snapshots will be
301 * destroyed.
302 *
303 * If 'defer' is set, and a snapshot has user holds or clones, it will be
304 * marked for deferred destruction, and will be destroyed when the last hold
305 * or clone is removed/destroyed.
306 *
307 * The return value will be 0 if all snapshots were destroyed (or marked for
308 * later destruction if 'defer' is set) or didn't exist to begin with.
309 *
310 * Otherwise the return value will be the errno of a (unspecified) snapshot
311 * that failed, no snapshots will be destroyed, and the errlist will have an
312 * entry for each snapshot that failed. The value in the errlist will be
313 * the (int32) error code.
314 */
315 int
316 lzc_destroy_snaps(nvlist_t *snaps, boolean_t defer, nvlist_t **errlist)
317 {
318 nvpair_t *elem;
319 nvlist_t *args;
320 int error;
321 char pool[ZFS_MAX_DATASET_NAME_LEN];
322
323 /* determine the pool name */
324 elem = nvlist_next_nvpair(snaps, NULL);
325 if (elem == NULL)
326 return (0);
327 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
328 pool[strcspn(pool, "/@")] = '\0';
329
330 args = fnvlist_alloc();
331 fnvlist_add_nvlist(args, "snaps", snaps);
332 if (defer)
333 fnvlist_add_boolean(args, "defer");
334
335 error = lzc_ioctl(ZFS_IOC_DESTROY_SNAPS, pool, args, errlist);
336 nvlist_free(args);
337
338 return (error);
339 }
340
341 int
342 lzc_snaprange_space(const char *firstsnap, const char *lastsnap,
343 uint64_t *usedp)
344 {
345 nvlist_t *args;
346 nvlist_t *result;
347 int err;
348 char fs[ZFS_MAX_DATASET_NAME_LEN];
349 char *atp;
350
351 /* determine the fs name */
352 (void) strlcpy(fs, firstsnap, sizeof (fs));
353 atp = strchr(fs, '@');
354 if (atp == NULL)
355 return (EINVAL);
356 *atp = '\0';
357
358 args = fnvlist_alloc();
359 fnvlist_add_string(args, "firstsnap", firstsnap);
360
361 err = lzc_ioctl(ZFS_IOC_SPACE_SNAPS, lastsnap, args, &result);
362 nvlist_free(args);
363 if (err == 0)
364 *usedp = fnvlist_lookup_uint64(result, "used");
365 fnvlist_free(result);
366
367 return (err);
368 }
369
370 boolean_t
371 lzc_exists(const char *dataset)
372 {
373 /*
374 * The objset_stats ioctl is still legacy, so we need to construct our
375 * own zfs_cmd_t rather than using lzc_ioctl().
376 */
377 zfs_cmd_t zc = {"\0"};
378
379 ASSERT3S(g_refcount, >, 0);
380 VERIFY3S(g_fd, !=, -1);
381
382 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
383 return (ioctl(g_fd, ZFS_IOC_OBJSET_STATS, &zc) == 0);
384 }
385
386 /*
387 * outnvl is unused.
388 * It was added to preserve the function signature in case it is
389 * needed in the future.
390 */
391 /*ARGSUSED*/
392 int
393 lzc_sync(const char *pool_name, nvlist_t *innvl, nvlist_t **outnvl)
394 {
395 return (lzc_ioctl(ZFS_IOC_POOL_SYNC, pool_name, innvl, NULL));
396 }
397
398 /*
399 * Create "user holds" on snapshots. If there is a hold on a snapshot,
400 * the snapshot can not be destroyed. (However, it can be marked for deletion
401 * by lzc_destroy_snaps(defer=B_TRUE).)
402 *
403 * The keys in the nvlist are snapshot names.
404 * The snapshots must all be in the same pool.
405 * The value is the name of the hold (string type).
406 *
407 * If cleanup_fd is not -1, it must be the result of open("/dev/zfs", O_EXCL).
408 * In this case, when the cleanup_fd is closed (including on process
409 * termination), the holds will be released. If the system is shut down
410 * uncleanly, the holds will be released when the pool is next opened
411 * or imported.
412 *
413 * Holds for snapshots which don't exist will be skipped and have an entry
414 * added to errlist, but will not cause an overall failure.
415 *
416 * The return value will be 0 if all holds, for snapshots that existed,
417 * were successfully created.
418 *
419 * Otherwise the return value will be the errno of a (unspecified) hold that
420 * failed and no holds will be created.
421 *
422 * In all cases the errlist will have an entry for each hold that failed
423 * (name = snapshot), with its value being the error code (int32).
424 */
425 int
426 lzc_hold(nvlist_t *holds, int cleanup_fd, nvlist_t **errlist)
427 {
428 char pool[ZFS_MAX_DATASET_NAME_LEN];
429 nvlist_t *args;
430 nvpair_t *elem;
431 int error;
432
433 /* determine the pool name */
434 elem = nvlist_next_nvpair(holds, NULL);
435 if (elem == NULL)
436 return (0);
437 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
438 pool[strcspn(pool, "/@")] = '\0';
439
440 args = fnvlist_alloc();
441 fnvlist_add_nvlist(args, "holds", holds);
442 if (cleanup_fd != -1)
443 fnvlist_add_int32(args, "cleanup_fd", cleanup_fd);
444
445 error = lzc_ioctl(ZFS_IOC_HOLD, pool, args, errlist);
446 nvlist_free(args);
447 return (error);
448 }
449
450 /*
451 * Release "user holds" on snapshots. If the snapshot has been marked for
452 * deferred destroy (by lzc_destroy_snaps(defer=B_TRUE)), it does not have
453 * any clones, and all the user holds are removed, then the snapshot will be
454 * destroyed.
455 *
456 * The keys in the nvlist are snapshot names.
457 * The snapshots must all be in the same pool.
458 * The value is an nvlist whose keys are the holds to remove.
459 *
460 * Holds which failed to release because they didn't exist will have an entry
461 * added to errlist, but will not cause an overall failure.
462 *
463 * The return value will be 0 if the nvl holds was empty or all holds that
464 * existed, were successfully removed.
465 *
466 * Otherwise the return value will be the errno of a (unspecified) hold that
467 * failed to release and no holds will be released.
468 *
469 * In all cases the errlist will have an entry for each hold that failed to
470 * to release.
471 */
472 int
473 lzc_release(nvlist_t *holds, nvlist_t **errlist)
474 {
475 char pool[ZFS_MAX_DATASET_NAME_LEN];
476 nvpair_t *elem;
477
478 /* determine the pool name */
479 elem = nvlist_next_nvpair(holds, NULL);
480 if (elem == NULL)
481 return (0);
482 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
483 pool[strcspn(pool, "/@")] = '\0';
484
485 return (lzc_ioctl(ZFS_IOC_RELEASE, pool, holds, errlist));
486 }
487
488 /*
489 * Retrieve list of user holds on the specified snapshot.
490 *
491 * On success, *holdsp will be set to an nvlist which the caller must free.
492 * The keys are the names of the holds, and the value is the creation time
493 * of the hold (uint64) in seconds since the epoch.
494 */
495 int
496 lzc_get_holds(const char *snapname, nvlist_t **holdsp)
497 {
498 return (lzc_ioctl(ZFS_IOC_GET_HOLDS, snapname, NULL, holdsp));
499 }
500
501 /*
502 * Generate a zfs send stream for the specified snapshot and write it to
503 * the specified file descriptor.
504 *
505 * "snapname" is the full name of the snapshot to send (e.g. "pool/fs@snap")
506 *
507 * If "from" is NULL, a full (non-incremental) stream will be sent.
508 * If "from" is non-NULL, it must be the full name of a snapshot or
509 * bookmark to send an incremental from (e.g. "pool/fs@earlier_snap" or
510 * "pool/fs#earlier_bmark"). If non-NULL, the specified snapshot or
511 * bookmark must represent an earlier point in the history of "snapname").
512 * It can be an earlier snapshot in the same filesystem or zvol as "snapname",
513 * or it can be the origin of "snapname"'s filesystem, or an earlier
514 * snapshot in the origin, etc.
515 *
516 * "fd" is the file descriptor to write the send stream to.
517 *
518 * If "flags" contains LZC_SEND_FLAG_LARGE_BLOCK, the stream is permitted
519 * to contain DRR_WRITE records with drr_length > 128K, and DRR_OBJECT
520 * records with drr_blksz > 128K.
521 *
522 * If "flags" contains LZC_SEND_FLAG_EMBED_DATA, the stream is permitted
523 * to contain DRR_WRITE_EMBEDDED records with drr_etype==BP_EMBEDDED_TYPE_DATA,
524 * which the receiving system must support (as indicated by support
525 * for the "embedded_data" feature).
526 */
527 int
528 lzc_send(const char *snapname, const char *from, int fd,
529 enum lzc_send_flags flags)
530 {
531 return (lzc_send_resume(snapname, from, fd, flags, 0, 0));
532 }
533
534 int
535 lzc_send_resume(const char *snapname, const char *from, int fd,
536 enum lzc_send_flags flags, uint64_t resumeobj, uint64_t resumeoff)
537 {
538 nvlist_t *args;
539 int err;
540
541 args = fnvlist_alloc();
542 fnvlist_add_int32(args, "fd", fd);
543 if (from != NULL)
544 fnvlist_add_string(args, "fromsnap", from);
545 if (flags & LZC_SEND_FLAG_LARGE_BLOCK)
546 fnvlist_add_boolean(args, "largeblockok");
547 if (flags & LZC_SEND_FLAG_EMBED_DATA)
548 fnvlist_add_boolean(args, "embedok");
549 if (flags & LZC_SEND_FLAG_COMPRESS)
550 fnvlist_add_boolean(args, "compressok");
551 if (flags & LZC_SEND_FLAG_RAW)
552 fnvlist_add_boolean(args, "rawok");
553 if (resumeobj != 0 || resumeoff != 0) {
554 fnvlist_add_uint64(args, "resume_object", resumeobj);
555 fnvlist_add_uint64(args, "resume_offset", resumeoff);
556 }
557 err = lzc_ioctl(ZFS_IOC_SEND_NEW, snapname, args, NULL);
558 nvlist_free(args);
559 return (err);
560 }
561
562 /*
563 * "from" can be NULL, a snapshot, or a bookmark.
564 *
565 * If from is NULL, a full (non-incremental) stream will be estimated. This
566 * is calculated very efficiently.
567 *
568 * If from is a snapshot, lzc_send_space uses the deadlists attached to
569 * each snapshot to efficiently estimate the stream size.
570 *
571 * If from is a bookmark, the indirect blocks in the destination snapshot
572 * are traversed, looking for blocks with a birth time since the creation TXG of
573 * the snapshot this bookmark was created from. This will result in
574 * significantly more I/O and be less efficient than a send space estimation on
575 * an equivalent snapshot.
576 */
577 int
578 lzc_send_space(const char *snapname, const char *from,
579 enum lzc_send_flags flags, uint64_t *spacep)
580 {
581 nvlist_t *args;
582 nvlist_t *result;
583 int err;
584
585 args = fnvlist_alloc();
586 if (from != NULL)
587 fnvlist_add_string(args, "from", from);
588 if (flags & LZC_SEND_FLAG_LARGE_BLOCK)
589 fnvlist_add_boolean(args, "largeblockok");
590 if (flags & LZC_SEND_FLAG_EMBED_DATA)
591 fnvlist_add_boolean(args, "embedok");
592 if (flags & LZC_SEND_FLAG_COMPRESS)
593 fnvlist_add_boolean(args, "compressok");
594 if (flags & LZC_SEND_FLAG_RAW)
595 fnvlist_add_boolean(args, "rawok");
596 err = lzc_ioctl(ZFS_IOC_SEND_SPACE, snapname, args, &result);
597 nvlist_free(args);
598 if (err == 0)
599 *spacep = fnvlist_lookup_uint64(result, "space");
600 nvlist_free(result);
601 return (err);
602 }
603
604 static int
605 recv_read(int fd, void *buf, int ilen)
606 {
607 char *cp = buf;
608 int rv;
609 int len = ilen;
610
611 do {
612 rv = read(fd, cp, len);
613 cp += rv;
614 len -= rv;
615 } while (rv > 0);
616
617 if (rv < 0 || len != 0)
618 return (EIO);
619
620 return (0);
621 }
622
623 /*
624 * Linux adds ZFS_IOC_RECV_NEW for resumable and raw streams and preserves the
625 * legacy ZFS_IOC_RECV user/kernel interface. The new interface supports all
626 * stream options but is currently only used for resumable streams. This way
627 * updated user space utilities will interoperate with older kernel modules.
628 *
629 * Non-Linux OpenZFS platforms have opted to modify the legacy interface.
630 */
631 static int
632 recv_impl(const char *snapname, nvlist_t *recvdprops, nvlist_t *localprops,
633 const char *origin, boolean_t force, boolean_t resumable, boolean_t raw,
634 int input_fd, const dmu_replay_record_t *begin_record, int cleanup_fd,
635 uint64_t *read_bytes, uint64_t *errflags, uint64_t *action_handle,
636 nvlist_t **errors)
637 {
638 dmu_replay_record_t drr;
639 char fsname[MAXPATHLEN];
640 char *atp;
641 int error;
642
643 ASSERT3S(g_refcount, >, 0);
644 VERIFY3S(g_fd, !=, -1);
645
646 /* Set 'fsname' to the name of containing filesystem */
647 (void) strlcpy(fsname, snapname, sizeof (fsname));
648 atp = strchr(fsname, '@');
649 if (atp == NULL)
650 return (EINVAL);
651 *atp = '\0';
652
653 /* If the fs does not exist, try its parent. */
654 if (!lzc_exists(fsname)) {
655 char *slashp = strrchr(fsname, '/');
656 if (slashp == NULL)
657 return (ENOENT);
658 *slashp = '\0';
659 }
660
661 /*
662 * The begin_record is normally a non-byteswapped BEGIN record.
663 * For resumable streams it may be set to any non-byteswapped
664 * dmu_replay_record_t.
665 */
666 if (begin_record == NULL) {
667 error = recv_read(input_fd, &drr, sizeof (drr));
668 if (error != 0)
669 return (error);
670 } else {
671 drr = *begin_record;
672 }
673
674 if (resumable || raw) {
675 nvlist_t *outnvl = NULL;
676 nvlist_t *innvl = fnvlist_alloc();
677
678 fnvlist_add_string(innvl, "snapname", snapname);
679
680 if (recvdprops != NULL)
681 fnvlist_add_nvlist(innvl, "props", recvdprops);
682
683 if (localprops != NULL)
684 fnvlist_add_nvlist(innvl, "localprops", localprops);
685
686 if (origin != NULL && strlen(origin))
687 fnvlist_add_string(innvl, "origin", origin);
688
689 fnvlist_add_byte_array(innvl, "begin_record",
690 (uchar_t *)&drr, sizeof (drr));
691
692 fnvlist_add_int32(innvl, "input_fd", input_fd);
693
694 if (force)
695 fnvlist_add_boolean(innvl, "force");
696
697 if (resumable)
698 fnvlist_add_boolean(innvl, "resumable");
699
700 if (cleanup_fd >= 0)
701 fnvlist_add_int32(innvl, "cleanup_fd", cleanup_fd);
702
703 if (action_handle != NULL)
704 fnvlist_add_uint64(innvl, "action_handle",
705 *action_handle);
706
707 error = lzc_ioctl(ZFS_IOC_RECV_NEW, fsname, innvl, &outnvl);
708
709 if (error == 0 && read_bytes != NULL)
710 error = nvlist_lookup_uint64(outnvl, "read_bytes",
711 read_bytes);
712
713 if (error == 0 && errflags != NULL)
714 error = nvlist_lookup_uint64(outnvl, "error_flags",
715 errflags);
716
717 if (error == 0 && action_handle != NULL)
718 error = nvlist_lookup_uint64(outnvl, "action_handle",
719 action_handle);
720
721 if (error == 0 && errors != NULL) {
722 nvlist_t *nvl;
723 error = nvlist_lookup_nvlist(outnvl, "errors", &nvl);
724 if (error == 0)
725 *errors = fnvlist_dup(nvl);
726 }
727
728 fnvlist_free(innvl);
729 fnvlist_free(outnvl);
730 } else {
731 zfs_cmd_t zc = {"\0"};
732 char *packed = NULL;
733 size_t size;
734
735 ASSERT3S(g_refcount, >, 0);
736
737 (void) strlcpy(zc.zc_name, fsname, sizeof (zc.zc_value));
738 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
739
740 if (recvdprops != NULL) {
741 packed = fnvlist_pack(recvdprops, &size);
742 zc.zc_nvlist_src = (uint64_t)(uintptr_t)packed;
743 zc.zc_nvlist_src_size = size;
744 }
745
746 if (localprops != NULL) {
747 packed = fnvlist_pack(localprops, &size);
748 zc.zc_nvlist_conf = (uint64_t)(uintptr_t)packed;
749 zc.zc_nvlist_conf_size = size;
750 }
751
752 if (origin != NULL)
753 (void) strlcpy(zc.zc_string, origin,
754 sizeof (zc.zc_string));
755
756 ASSERT3S(drr.drr_type, ==, DRR_BEGIN);
757 zc.zc_begin_record = drr.drr_u.drr_begin;
758 zc.zc_guid = force;
759 zc.zc_cookie = input_fd;
760 zc.zc_cleanup_fd = -1;
761 zc.zc_action_handle = 0;
762
763 if (cleanup_fd >= 0)
764 zc.zc_cleanup_fd = cleanup_fd;
765
766 if (action_handle != NULL)
767 zc.zc_action_handle = *action_handle;
768
769 zc.zc_nvlist_dst_size = 128 * 1024;
770 zc.zc_nvlist_dst = (uint64_t)(uintptr_t)
771 malloc(zc.zc_nvlist_dst_size);
772
773 error = ioctl(g_fd, ZFS_IOC_RECV, &zc);
774 if (error != 0) {
775 error = errno;
776 } else {
777 if (read_bytes != NULL)
778 *read_bytes = zc.zc_cookie;
779
780 if (errflags != NULL)
781 *errflags = zc.zc_obj;
782
783 if (action_handle != NULL)
784 *action_handle = zc.zc_action_handle;
785
786 if (errors != NULL)
787 VERIFY0(nvlist_unpack(
788 (void *)(uintptr_t)zc.zc_nvlist_dst,
789 zc.zc_nvlist_dst_size, errors, KM_SLEEP));
790 }
791
792 if (packed != NULL)
793 fnvlist_pack_free(packed, size);
794 free((void *)(uintptr_t)zc.zc_nvlist_dst);
795 }
796
797 return (error);
798 }
799
800 /*
801 * The simplest receive case: receive from the specified fd, creating the
802 * specified snapshot. Apply the specified properties as "received" properties
803 * (which can be overridden by locally-set properties). If the stream is a
804 * clone, its origin snapshot must be specified by 'origin'. The 'force'
805 * flag will cause the target filesystem to be rolled back or destroyed if
806 * necessary to receive.
807 *
808 * Return 0 on success or an errno on failure.
809 *
810 * Note: this interface does not work on dedup'd streams
811 * (those with DMU_BACKUP_FEATURE_DEDUP).
812 */
813 int
814 lzc_receive(const char *snapname, nvlist_t *props, const char *origin,
815 boolean_t force, boolean_t raw, int fd)
816 {
817 return (recv_impl(snapname, props, NULL, origin, force, B_FALSE, raw,
818 fd, NULL, -1, NULL, NULL, NULL, NULL));
819 }
820
821 /*
822 * Like lzc_receive, but if the receive fails due to premature stream
823 * termination, the intermediate state will be preserved on disk. In this
824 * case, ECKSUM will be returned. The receive may subsequently be resumed
825 * with a resuming send stream generated by lzc_send_resume().
826 */
827 int
828 lzc_receive_resumable(const char *snapname, nvlist_t *props, const char *origin,
829 boolean_t force, boolean_t raw, int fd)
830 {
831 return (recv_impl(snapname, props, NULL, origin, force, B_TRUE, raw,
832 fd, NULL, -1, NULL, NULL, NULL, NULL));
833 }
834
835 /*
836 * Like lzc_receive, but allows the caller to read the begin record and then to
837 * pass it in. That could be useful if the caller wants to derive, for example,
838 * the snapname or the origin parameters based on the information contained in
839 * the begin record.
840 * The begin record must be in its original form as read from the stream,
841 * in other words, it should not be byteswapped.
842 *
843 * The 'resumable' parameter allows to obtain the same behavior as with
844 * lzc_receive_resumable.
845 */
846 int
847 lzc_receive_with_header(const char *snapname, nvlist_t *props,
848 const char *origin, boolean_t force, boolean_t resumable, boolean_t raw,
849 int fd, const dmu_replay_record_t *begin_record)
850 {
851 if (begin_record == NULL)
852 return (EINVAL);
853
854 return (recv_impl(snapname, props, NULL, origin, force, resumable, raw,
855 fd, begin_record, -1, NULL, NULL, NULL, NULL));
856 }
857
858 /*
859 * Like lzc_receive, but allows the caller to pass all supported arguments
860 * and retrieve all values returned. The only additional input parameter
861 * is 'cleanup_fd' which is used to set a cleanup-on-exit file descriptor.
862 *
863 * The following parameters all provide return values. Several may be set
864 * in the failure case and will contain additional information.
865 *
866 * The 'read_bytes' value will be set to the total number of bytes read.
867 *
868 * The 'errflags' value will contain zprop_errflags_t flags which are
869 * used to describe any failures.
870 *
871 * The 'action_handle' is used to pass the handle for this guid/ds mapping.
872 * It should be set to zero on first call and will contain an updated handle
873 * on success, it should be passed in subsequent calls.
874 *
875 * The 'errors' nvlist contains an entry for each unapplied received
876 * property. Callers are responsible for freeing this nvlist.
877 */
878 int lzc_receive_one(const char *snapname, nvlist_t *props,
879 const char *origin, boolean_t force, boolean_t resumable, boolean_t raw,
880 int input_fd, const dmu_replay_record_t *begin_record, int cleanup_fd,
881 uint64_t *read_bytes, uint64_t *errflags, uint64_t *action_handle,
882 nvlist_t **errors)
883 {
884 return (recv_impl(snapname, props, NULL, origin, force, resumable,
885 raw, input_fd, begin_record, cleanup_fd, read_bytes, errflags,
886 action_handle, errors));
887 }
888
889 /*
890 * Like lzc_receive_one, but allows the caller to pass an additional 'cmdprops'
891 * argument.
892 *
893 * The 'cmdprops' nvlist contains both override ('zfs receive -o') and
894 * exclude ('zfs receive -x') properties. Callers are responsible for freeing
895 * this nvlist
896 */
897 int lzc_receive_with_cmdprops(const char *snapname, nvlist_t *props,
898 nvlist_t *cmdprops, const char *origin, boolean_t force,
899 boolean_t resumable, boolean_t raw, int input_fd,
900 const dmu_replay_record_t *begin_record, int cleanup_fd,
901 uint64_t *read_bytes, uint64_t *errflags, uint64_t *action_handle,
902 nvlist_t **errors)
903 {
904 return (recv_impl(snapname, props, cmdprops, origin, force, resumable,
905 raw, input_fd, begin_record, cleanup_fd, read_bytes, errflags,
906 action_handle, errors));
907 }
908
909 /*
910 * Roll back this filesystem or volume to its most recent snapshot.
911 * If snapnamebuf is not NULL, it will be filled in with the name
912 * of the most recent snapshot.
913 * Note that the latest snapshot may change if a new one is concurrently
914 * created or the current one is destroyed. lzc_rollback_to can be used
915 * to roll back to a specific latest snapshot.
916 *
917 * Return 0 on success or an errno on failure.
918 */
919 int
920 lzc_rollback(const char *fsname, char *snapnamebuf, int snapnamelen)
921 {
922 nvlist_t *args;
923 nvlist_t *result;
924 int err;
925
926 args = fnvlist_alloc();
927 err = lzc_ioctl(ZFS_IOC_ROLLBACK, fsname, args, &result);
928 nvlist_free(args);
929 if (err == 0 && snapnamebuf != NULL) {
930 const char *snapname = fnvlist_lookup_string(result, "target");
931 (void) strlcpy(snapnamebuf, snapname, snapnamelen);
932 }
933 nvlist_free(result);
934
935 return (err);
936 }
937
938 /*
939 * Roll back this filesystem or volume to the specified snapshot,
940 * if possible.
941 *
942 * Return 0 on success or an errno on failure.
943 */
944 int
945 lzc_rollback_to(const char *fsname, const char *snapname)
946 {
947 nvlist_t *args;
948 nvlist_t *result;
949 int err;
950
951 args = fnvlist_alloc();
952 fnvlist_add_string(args, "target", snapname);
953 err = lzc_ioctl(ZFS_IOC_ROLLBACK, fsname, args, &result);
954 nvlist_free(args);
955 nvlist_free(result);
956 return (err);
957 }
958
959 /*
960 * Creates bookmarks.
961 *
962 * The bookmarks nvlist maps from name of the bookmark (e.g. "pool/fs#bmark") to
963 * the name of the snapshot (e.g. "pool/fs@snap"). All the bookmarks and
964 * snapshots must be in the same pool.
965 *
966 * The returned results nvlist will have an entry for each bookmark that failed.
967 * The value will be the (int32) error code.
968 *
969 * The return value will be 0 if all bookmarks were created, otherwise it will
970 * be the errno of a (undetermined) bookmarks that failed.
971 */
972 int
973 lzc_bookmark(nvlist_t *bookmarks, nvlist_t **errlist)
974 {
975 nvpair_t *elem;
976 int error;
977 char pool[ZFS_MAX_DATASET_NAME_LEN];
978
979 /* determine the pool name */
980 elem = nvlist_next_nvpair(bookmarks, NULL);
981 if (elem == NULL)
982 return (0);
983 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
984 pool[strcspn(pool, "/#")] = '\0';
985
986 error = lzc_ioctl(ZFS_IOC_BOOKMARK, pool, bookmarks, errlist);
987
988 return (error);
989 }
990
991 /*
992 * Retrieve bookmarks.
993 *
994 * Retrieve the list of bookmarks for the given file system. The props
995 * parameter is an nvlist of property names (with no values) that will be
996 * returned for each bookmark.
997 *
998 * The following are valid properties on bookmarks, all of which are numbers
999 * (represented as uint64 in the nvlist)
1000 *
1001 * "guid" - globally unique identifier of the snapshot it refers to
1002 * "createtxg" - txg when the snapshot it refers to was created
1003 * "creation" - timestamp when the snapshot it refers to was created
1004 *
1005 * The format of the returned nvlist as follows:
1006 * <short name of bookmark> -> {
1007 * <name of property> -> {
1008 * "value" -> uint64
1009 * }
1010 * }
1011 */
1012 int
1013 lzc_get_bookmarks(const char *fsname, nvlist_t *props, nvlist_t **bmarks)
1014 {
1015 return (lzc_ioctl(ZFS_IOC_GET_BOOKMARKS, fsname, props, bmarks));
1016 }
1017
1018 /*
1019 * Destroys bookmarks.
1020 *
1021 * The keys in the bmarks nvlist are the bookmarks to be destroyed.
1022 * They must all be in the same pool. Bookmarks are specified as
1023 * <fs>#<bmark>.
1024 *
1025 * Bookmarks that do not exist will be silently ignored.
1026 *
1027 * The return value will be 0 if all bookmarks that existed were destroyed.
1028 *
1029 * Otherwise the return value will be the errno of a (undetermined) bookmark
1030 * that failed, no bookmarks will be destroyed, and the errlist will have an
1031 * entry for each bookmarks that failed. The value in the errlist will be
1032 * the (int32) error code.
1033 */
1034 int
1035 lzc_destroy_bookmarks(nvlist_t *bmarks, nvlist_t **errlist)
1036 {
1037 nvpair_t *elem;
1038 int error;
1039 char pool[ZFS_MAX_DATASET_NAME_LEN];
1040
1041 /* determine the pool name */
1042 elem = nvlist_next_nvpair(bmarks, NULL);
1043 if (elem == NULL)
1044 return (0);
1045 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
1046 pool[strcspn(pool, "/#")] = '\0';
1047
1048 error = lzc_ioctl(ZFS_IOC_DESTROY_BOOKMARKS, pool, bmarks, errlist);
1049
1050 return (error);
1051 }
1052
1053 /*
1054 * Performs key management functions
1055 *
1056 * crypto_cmd should be a value from zfs_ioc_crypto_cmd_t. If the command
1057 * specifies to load or change a wrapping key, the key should be specified in
1058 * the hidden_args nvlist so that it is not logged
1059 */
1060 int
1061 lzc_load_key(const char *fsname, boolean_t noop, uint8_t *wkeydata,
1062 uint_t wkeylen)
1063 {
1064 int error;
1065 nvlist_t *ioc_args;
1066 nvlist_t *hidden_args;
1067
1068 if (wkeydata == NULL)
1069 return (EINVAL);
1070
1071 ioc_args = fnvlist_alloc();
1072 hidden_args = fnvlist_alloc();
1073 fnvlist_add_uint8_array(hidden_args, "wkeydata", wkeydata, wkeylen);
1074 fnvlist_add_nvlist(ioc_args, ZPOOL_HIDDEN_ARGS, hidden_args);
1075 if (noop)
1076 fnvlist_add_boolean(ioc_args, "noop");
1077 error = lzc_ioctl(ZFS_IOC_LOAD_KEY, fsname, ioc_args, NULL);
1078 nvlist_free(hidden_args);
1079 nvlist_free(ioc_args);
1080
1081 return (error);
1082 }
1083
1084 int
1085 lzc_unload_key(const char *fsname)
1086 {
1087 return (lzc_ioctl(ZFS_IOC_UNLOAD_KEY, fsname, NULL, NULL));
1088 }
1089
1090 int
1091 lzc_change_key(const char *fsname, uint64_t crypt_cmd, nvlist_t *props,
1092 uint8_t *wkeydata, uint_t wkeylen)
1093 {
1094 int error;
1095 nvlist_t *ioc_args = fnvlist_alloc();
1096 nvlist_t *hidden_args = NULL;
1097
1098 fnvlist_add_uint64(ioc_args, "crypt_cmd", crypt_cmd);
1099
1100 if (wkeydata != NULL) {
1101 hidden_args = fnvlist_alloc();
1102 fnvlist_add_uint8_array(hidden_args, "wkeydata", wkeydata,
1103 wkeylen);
1104 fnvlist_add_nvlist(ioc_args, ZPOOL_HIDDEN_ARGS, hidden_args);
1105 }
1106
1107 if (props != NULL)
1108 fnvlist_add_nvlist(ioc_args, "props", props);
1109
1110 error = lzc_ioctl(ZFS_IOC_CHANGE_KEY, fsname, ioc_args, NULL);
1111 nvlist_free(hidden_args);
1112 nvlist_free(ioc_args);
1113 return (error);
1114 }
1115
1116 int
1117 lzc_reopen(const char *pool_name, boolean_t scrub_restart)
1118 {
1119 nvlist_t *args = fnvlist_alloc();
1120 int error;
1121
1122 fnvlist_add_boolean_value(args, "scrub_restart", scrub_restart);
1123
1124 error = lzc_ioctl(ZFS_IOC_POOL_REOPEN, pool_name, args, NULL);
1125 nvlist_free(args);
1126 return (error);
1127 }