xref: /freebsd/sbin/ggate/ggatel/ggatel.c (revision 069ac184)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2004 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <stdint.h>
32 #include <fcntl.h>
33 #include <unistd.h>
34 #include <string.h>
35 #include <err.h>
36 #include <errno.h>
37 #include <assert.h>
38 #include <sys/param.h>
39 #include <sys/time.h>
40 #include <sys/bio.h>
41 #include <sys/disk.h>
42 #include <sys/ioctl.h>
43 #include <sys/stat.h>
44 #include <sys/syslog.h>
45 
46 #include <geom/gate/g_gate.h>
47 #include "ggate.h"
48 
49 
50 static enum { UNSET, CREATE, DESTROY, LIST, RESCUE } action = UNSET;
51 
52 static const char *path = NULL;
53 static int unit = G_GATE_UNIT_AUTO;
54 static unsigned flags = 0;
55 static int force = 0;
56 static unsigned sectorsize = 0;
57 static unsigned timeout = G_GATE_TIMEOUT;
58 
59 static void
60 usage(void)
61 {
62 
63 	fprintf(stderr, "usage: %s create [-v] [-o <ro|wo|rw>] "
64 	    "[-s sectorsize] [-t timeout] [-u unit] <path>\n", getprogname());
65 	fprintf(stderr, "       %s rescue [-v] [-o <ro|wo|rw>] <-u unit> "
66 	    "<path>\n", getprogname());
67 	fprintf(stderr, "       %s destroy [-f] <-u unit>\n", getprogname());
68 	fprintf(stderr, "       %s list [-v] [-u unit]\n", getprogname());
69 	exit(EXIT_FAILURE);
70 }
71 
72 static int
73 g_gate_openflags(unsigned ggflags)
74 {
75 
76 	if ((ggflags & G_GATE_FLAG_READONLY) != 0)
77 		return (O_RDONLY);
78 	else if ((ggflags & G_GATE_FLAG_WRITEONLY) != 0)
79 		return (O_WRONLY);
80 	return (O_RDWR);
81 }
82 
83 static void
84 g_gatel_serve(int fd)
85 {
86 	struct g_gate_ctl_io ggio;
87 	size_t bsize;
88 
89 	if (g_gate_verbose == 0) {
90 		if (daemon(0, 0) == -1) {
91 			g_gate_destroy(unit, 1);
92 			err(EXIT_FAILURE, "Cannot daemonize");
93 		}
94 	}
95 	g_gate_log(LOG_DEBUG, "Worker created: %u.", getpid());
96 	ggio.gctl_version = G_GATE_VERSION;
97 	ggio.gctl_unit = unit;
98 	bsize = sectorsize;
99 	ggio.gctl_data = malloc(bsize);
100 	for (;;) {
101 		int error;
102 once_again:
103 		ggio.gctl_length = bsize;
104 		ggio.gctl_error = 0;
105 		g_gate_ioctl(G_GATE_CMD_START, &ggio);
106 		error = ggio.gctl_error;
107 		switch (error) {
108 		case 0:
109 			break;
110 		case ECANCELED:
111 			/* Exit gracefully. */
112 			free(ggio.gctl_data);
113 			g_gate_close_device();
114 			close(fd);
115 			exit(EXIT_SUCCESS);
116 		case ENOMEM:
117 			/* Buffer too small. */
118 			assert(ggio.gctl_cmd == BIO_DELETE ||
119 			    ggio.gctl_cmd == BIO_WRITE);
120 			ggio.gctl_data = realloc(ggio.gctl_data,
121 			    ggio.gctl_length);
122 			if (ggio.gctl_data != NULL) {
123 				bsize = ggio.gctl_length;
124 				goto once_again;
125 			}
126 			/* FALLTHROUGH */
127 		case ENXIO:
128 		default:
129 			g_gate_xlog("ioctl(/dev/%s): %s.", G_GATE_CTL_NAME,
130 			    strerror(error));
131 		}
132 
133 		error = 0;
134 		switch (ggio.gctl_cmd) {
135 		case BIO_READ:
136 			if ((size_t)ggio.gctl_length > bsize) {
137 				ggio.gctl_data = realloc(ggio.gctl_data,
138 				    ggio.gctl_length);
139 				if (ggio.gctl_data != NULL)
140 					bsize = ggio.gctl_length;
141 				else
142 					error = ENOMEM;
143 			}
144 			if (error == 0) {
145 				if (pread(fd, ggio.gctl_data, ggio.gctl_length,
146 				    ggio.gctl_offset) == -1) {
147 					error = errno;
148 				}
149 			}
150 			break;
151 		case BIO_DELETE:
152 		case BIO_WRITE:
153 			if (pwrite(fd, ggio.gctl_data, ggio.gctl_length,
154 			    ggio.gctl_offset) == -1) {
155 				error = errno;
156 			}
157 			break;
158 		default:
159 			error = EOPNOTSUPP;
160 		}
161 
162 		ggio.gctl_error = error;
163 		g_gate_ioctl(G_GATE_CMD_DONE, &ggio);
164 	}
165 }
166 
167 static void
168 g_gatel_create(void)
169 {
170 	struct g_gate_ctl_create ggioc;
171 	int fd;
172 
173 	fd = open(path, g_gate_openflags(flags) | O_DIRECT | O_FSYNC);
174 	if (fd == -1)
175 		err(EXIT_FAILURE, "Cannot open %s", path);
176 	memset(&ggioc, 0, sizeof(ggioc));
177 	ggioc.gctl_version = G_GATE_VERSION;
178 	ggioc.gctl_unit = unit;
179 	ggioc.gctl_mediasize = g_gate_mediasize(fd);
180 	if (sectorsize == 0)
181 		sectorsize = g_gate_sectorsize(fd);
182 	ggioc.gctl_sectorsize = sectorsize;
183 	ggioc.gctl_timeout = timeout;
184 	ggioc.gctl_flags = flags;
185 	ggioc.gctl_maxcount = 0;
186 	strlcpy(ggioc.gctl_info, path, sizeof(ggioc.gctl_info));
187 	g_gate_ioctl(G_GATE_CMD_CREATE, &ggioc);
188 	if (unit == -1)
189 		printf("%s%u\n", G_GATE_PROVIDER_NAME, ggioc.gctl_unit);
190 	unit = ggioc.gctl_unit;
191 	g_gatel_serve(fd);
192 }
193 
194 static void
195 g_gatel_rescue(void)
196 {
197 	struct g_gate_ctl_cancel ggioc;
198 	int fd;
199 
200 	fd = open(path, g_gate_openflags(flags));
201 	if (fd == -1)
202 		err(EXIT_FAILURE, "Cannot open %s", path);
203 
204 	ggioc.gctl_version = G_GATE_VERSION;
205 	ggioc.gctl_unit = unit;
206 	ggioc.gctl_seq = 0;
207 	g_gate_ioctl(G_GATE_CMD_CANCEL, &ggioc);
208 
209 	g_gatel_serve(fd);
210 }
211 
212 int
213 main(int argc, char *argv[])
214 {
215 
216 	if (argc < 2)
217 		usage();
218 	if (strcasecmp(argv[1], "create") == 0)
219 		action = CREATE;
220 	else if (strcasecmp(argv[1], "rescue") == 0)
221 		action = RESCUE;
222 	else if (strcasecmp(argv[1], "destroy") == 0)
223 		action = DESTROY;
224 	else if (strcasecmp(argv[1], "list") == 0)
225 		action = LIST;
226 	else
227 		usage();
228 	argc -= 1;
229 	argv += 1;
230 	for (;;) {
231 		int ch;
232 
233 		ch = getopt(argc, argv, "fo:s:t:u:v");
234 		if (ch == -1)
235 			break;
236 		switch (ch) {
237 		case 'f':
238 			if (action != DESTROY)
239 				usage();
240 			force = 1;
241 			break;
242 		case 'o':
243 			if (action != CREATE && action != RESCUE)
244 				usage();
245 			if (strcasecmp("ro", optarg) == 0)
246 				flags = G_GATE_FLAG_READONLY;
247 			else if (strcasecmp("wo", optarg) == 0)
248 				flags = G_GATE_FLAG_WRITEONLY;
249 			else if (strcasecmp("rw", optarg) == 0)
250 				flags = 0;
251 			else {
252 				errx(EXIT_FAILURE,
253 				    "Invalid argument for '-o' option.");
254 			}
255 			break;
256 		case 's':
257 			if (action != CREATE)
258 				usage();
259 			errno = 0;
260 			sectorsize = strtoul(optarg, NULL, 10);
261 			if (sectorsize == 0 && errno != 0)
262 				errx(EXIT_FAILURE, "Invalid sectorsize.");
263 			break;
264 		case 't':
265 			if (action != CREATE)
266 				usage();
267 			errno = 0;
268 			timeout = strtoul(optarg, NULL, 10);
269 			if (timeout == 0 && errno != 0)
270 				errx(EXIT_FAILURE, "Invalid timeout.");
271 			break;
272 		case 'u':
273 			errno = 0;
274 			unit = strtol(optarg, NULL, 10);
275 			if (unit == 0 && errno != 0)
276 				errx(EXIT_FAILURE, "Invalid unit number.");
277 			break;
278 		case 'v':
279 			if (action == DESTROY)
280 				usage();
281 			g_gate_verbose++;
282 			break;
283 		default:
284 			usage();
285 		}
286 	}
287 	argc -= optind;
288 	argv += optind;
289 
290 	switch (action) {
291 	case CREATE:
292 		if (argc != 1)
293 			usage();
294 		g_gate_load_module();
295 		g_gate_open_device();
296 		path = argv[0];
297 		g_gatel_create();
298 		break;
299 	case RESCUE:
300 		if (argc != 1)
301 			usage();
302 		if (unit == -1) {
303 			fprintf(stderr, "Required unit number.\n");
304 			usage();
305 		}
306 		g_gate_open_device();
307 		path = argv[0];
308 		g_gatel_rescue();
309 		break;
310 	case DESTROY:
311 		if (unit == -1) {
312 			fprintf(stderr, "Required unit number.\n");
313 			usage();
314 		}
315 		g_gate_verbose = 1;
316 		g_gate_open_device();
317 		g_gate_destroy(unit, force);
318 		break;
319 	case LIST:
320 		g_gate_list(unit, g_gate_verbose);
321 		break;
322 	case UNSET:
323 	default:
324 		usage();
325 	}
326 	g_gate_close_device();
327 	exit(EXIT_SUCCESS);
328 }
329