xref: /freebsd/sys/contrib/openzfs/tests/zfs-tests/cmd/badsend.c (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * CDDL HEADER START
4  *
5  * The contents of this file are subject to the terms of the
6  * Common Development and Distribution License (the "License").
7  * You may not use this file except in compliance with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or https://opensource.org/licenses/CDDL-1.0.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Portions Copyright 2020 iXsystems, Inc.
25  */
26 
27 /*
28  * Test some invalid send operations with libzfs/libzfs_core.
29  *
30  * Specifying the to and from snaps in the wrong order should return EXDEV.
31  * We are checking that the early return doesn't accidentally leave any
32  * references held, so this test is designed to trigger a panic when asserts
33  * are verified with the bug present.
34  */
35 
36 #include <libzfs.h>
37 #include <libzfs_core.h>
38 
39 #include <fcntl.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include <sysexits.h>
44 #include <err.h>
45 
46 static void
usage(const char * name)47 usage(const char *name)
48 {
49 	fprintf(stderr, "usage: %s snap0 snap1\n", name);
50 	exit(EX_USAGE);
51 }
52 
53 int
main(int argc,char const * const argv[])54 main(int argc, char const * const argv[])
55 {
56 	sendflags_t flags = { 0 };
57 	libzfs_handle_t *zhdl;
58 	zfs_handle_t *zhp;
59 	const char *fromfull, *tofull, *fsname, *fromsnap, *tosnap, *p;
60 	uint64_t size;
61 	int fd, error;
62 
63 	if (argc != 3)
64 		usage(argv[0]);
65 
66 	fromfull = argv[1];
67 	tofull = argv[2];
68 
69 	p = strchr(fromfull, '@');
70 	if (p == NULL)
71 		usage(argv[0]);
72 	fromsnap = p + 1;
73 
74 	p = strchr(tofull, '@');
75 	if (p == NULL)
76 		usage(argv[0]);
77 	tosnap = p + 1;
78 
79 	fsname = strndup(tofull, p - tofull);
80 	if (fsname == NULL) {
81 		perror("strndup");
82 		exit(EXIT_FAILURE);
83 	}
84 	if (strncmp(fsname, fromfull, p - tofull) != 0)
85 		usage(argv[0]);
86 
87 	fd = open("/dev/null", O_WRONLY);
88 	if (fd == -1)
89 		err(EX_OSERR, "open(\"/dev/null\", O_WRONLY)");
90 
91 	zhdl = libzfs_init();
92 	if (zhdl == NULL)
93 		errx(EX_OSERR, "libzfs_init(): %s", libzfs_error_init(errno));
94 
95 	zhp = zfs_open(zhdl, fsname, ZFS_TYPE_FILESYSTEM);
96 	if (zhp == NULL)
97 		err(EX_OSERR, "zfs_open(\"%s\")", fsname);
98 
99 	/*
100 	 * Exercise EXDEV in dmu_send_obj.  The error gets translated to
101 	 * EZFS_CROSSTARGET in libzfs.
102 	 */
103 	error = zfs_send(zhp, tosnap, fromsnap, &flags, fd, NULL, NULL, NULL);
104 	if (error == 0 || libzfs_errno(zhdl) != EZFS_CROSSTARGET)
105 		errx(EX_OSERR, "zfs_send(\"%s\", \"%s\") should have failed "
106 		    "with EZFS_CROSSTARGET, not %d",
107 		    tofull, fromfull, libzfs_errno(zhdl));
108 	printf("zfs_send(\"%s\", \"%s\"): %s\n",
109 	    tofull, fromfull, libzfs_error_description(zhdl));
110 
111 	zfs_close(zhp);
112 
113 	/*
114 	 * Exercise EXDEV in dmu_send.
115 	 */
116 	error = lzc_send_resume_redacted(fromfull, tofull, fd, 0, 0, 0, NULL);
117 	if (error != EXDEV)
118 		errx(EX_OSERR, "lzc_send_resume_redacted(\"%s\", \"%s\")"
119 		    " should have failed with EXDEV, not %d",
120 		    fromfull, tofull, error);
121 	printf("lzc_send_resume_redacted(\"%s\", \"%s\"): %s\n",
122 	    fromfull, tofull, strerror(error));
123 
124 	/*
125 	 * Exercise EXDEV in dmu_send_estimate_fast.
126 	 */
127 	error = lzc_send_space_resume_redacted(fromfull, tofull, 0, 0, 0, 0,
128 	    NULL, fd, &size);
129 	if (error != EXDEV)
130 		errx(EX_OSERR, "lzc_send_space_resume_redacted(\"%s\", \"%s\")"
131 		    " should have failed with EXDEV, not %d",
132 		    fromfull, tofull, error);
133 	printf("lzc_send_space_resume_redacted(\"%s\", \"%s\"): %s\n",
134 	    fromfull, tofull, strerror(error));
135 
136 	close(fd);
137 	libzfs_fini(zhdl);
138 	free((void *)fsname);
139 
140 	return (EXIT_SUCCESS);
141 }
142