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 * Portions Copyright 2020 iXsystems, Inc.
24 */
25
26/*
27 * Test some invalid send operations with libzfs/libzfs_core.
28 *
29 * Specifying the to and from snaps in the wrong order should return EXDEV.
30 * We are checking that the early return doesn't accidentally leave any
31 * references held, so this test is designed to trigger a panic when asserts
32 * are verified with the bug present.
33 */
34
35#include <libzfs.h>
36#include <libzfs_core.h>
37
38#include <fcntl.h>
39#include <stdlib.h>
40#include <string.h>
41#include <unistd.h>
42#include <sysexits.h>
43#include <err.h>
44
45static void
46usage(const char *name)
47{
48	fprintf(stderr, "usage: %s snap0 snap1\n", name);
49	exit(EX_USAGE);
50}
51
52int
53main(int argc, char const * const argv[])
54{
55	sendflags_t flags = { 0 };
56	libzfs_handle_t *zhdl;
57	zfs_handle_t *zhp;
58	const char *fromfull, *tofull, *fsname, *fromsnap, *tosnap, *p;
59	uint64_t size;
60	int fd, error;
61
62	if (argc != 3)
63		usage(argv[0]);
64
65	fromfull = argv[1];
66	tofull = argv[2];
67
68	p = strchr(fromfull, '@');
69	if (p == NULL)
70		usage(argv[0]);
71	fromsnap = p + 1;
72
73	p = strchr(tofull, '@');
74	if (p == NULL)
75		usage(argv[0]);
76	tosnap = p + 1;
77
78	fsname = strndup(tofull, p - tofull);
79	if (strncmp(fsname, fromfull, p - tofull) != 0)
80		usage(argv[0]);
81
82	fd = open("/dev/null", O_WRONLY);
83	if (fd == -1)
84		err(EX_OSERR, "open(\"/dev/null\", O_WRONLY)");
85
86	zhdl = libzfs_init();
87	if (zhdl == NULL)
88		errx(EX_OSERR, "libzfs_init(): %s", libzfs_error_init(errno));
89
90	zhp = zfs_open(zhdl, fsname, ZFS_TYPE_FILESYSTEM);
91	if (zhp == NULL)
92		err(EX_OSERR, "zfs_open(\"%s\")", fsname);
93
94	/*
95	 * Exercise EXDEV in dmu_send_obj.  The error gets translated to
96	 * EZFS_CROSSTARGET in libzfs.
97	 */
98	error = zfs_send(zhp, tosnap, fromsnap, &flags, fd, NULL, NULL, NULL);
99	if (error == 0 || libzfs_errno(zhdl) != EZFS_CROSSTARGET)
100		errx(EX_OSERR, "zfs_send(\"%s\", \"%s\") should have failed "
101		    "with EZFS_CROSSTARGET, not %d",
102		    tofull, fromfull, libzfs_errno(zhdl));
103	printf("zfs_send(\"%s\", \"%s\"): %s\n",
104	    tofull, fromfull, libzfs_error_description(zhdl));
105
106	zfs_close(zhp);
107
108	/*
109	 * Exercise EXDEV in dmu_send.
110	 */
111	error = lzc_send_resume_redacted(fromfull, tofull, fd, 0, 0, 0, NULL);
112	if (error != EXDEV)
113		errx(EX_OSERR, "lzc_send_resume_redacted(\"%s\", \"%s\")"
114		    " should have failed with EXDEV, not %d",
115		    fromfull, tofull, error);
116	printf("lzc_send_resume_redacted(\"%s\", \"%s\"): %s\n",
117	    fromfull, tofull, strerror(error));
118
119	/*
120	 * Exercise EXDEV in dmu_send_estimate_fast.
121	 */
122	error = lzc_send_space_resume_redacted(fromfull, tofull, 0, 0, 0, 0,
123	    NULL, fd, &size);
124	if (error != EXDEV)
125		errx(EX_OSERR, "lzc_send_space_resume_redacted(\"%s\", \"%s\")"
126		    " should have failed with EXDEV, not %d",
127		    fromfull, tofull, error);
128	printf("lzc_send_space_resume_redacted(\"%s\", \"%s\"): %s\n",
129	    fromfull, tofull, strerror(error));
130
131	close(fd);
132	libzfs_fini(zhdl);
133	free((void *)fsname);
134
135	return (EXIT_SUCCESS);
136}
137