xref: /freebsd/sbin/ggate/ggatel/ggatel.c (revision bb15ca603fa442c72dde3f3cb8b46db6970e3950)
1 /*-
2  * Copyright (c) 2004 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
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 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 	ggioc.gctl_version = G_GATE_VERSION;
177 	ggioc.gctl_unit = unit;
178 	ggioc.gctl_mediasize = g_gate_mediasize(fd);
179 	if (sectorsize == 0)
180 		sectorsize = g_gate_sectorsize(fd);
181 	ggioc.gctl_sectorsize = sectorsize;
182 	ggioc.gctl_timeout = timeout;
183 	ggioc.gctl_flags = flags;
184 	ggioc.gctl_maxcount = 0;
185 	strlcpy(ggioc.gctl_info, path, sizeof(ggioc.gctl_info));
186 	g_gate_ioctl(G_GATE_CMD_CREATE, &ggioc);
187 	if (unit == -1)
188 		printf("%s%u\n", G_GATE_PROVIDER_NAME, ggioc.gctl_unit);
189 	unit = ggioc.gctl_unit;
190 	g_gatel_serve(fd);
191 }
192 
193 static void
194 g_gatel_rescue(void)
195 {
196 	struct g_gate_ctl_cancel ggioc;
197 	int fd;
198 
199 	fd = open(path, g_gate_openflags(flags));
200 	if (fd == -1)
201 		err(EXIT_FAILURE, "Cannot open %s", path);
202 
203 	ggioc.gctl_version = G_GATE_VERSION;
204 	ggioc.gctl_unit = unit;
205 	ggioc.gctl_seq = 0;
206 	g_gate_ioctl(G_GATE_CMD_CANCEL, &ggioc);
207 
208 	g_gatel_serve(fd);
209 }
210 
211 int
212 main(int argc, char *argv[])
213 {
214 
215 	if (argc < 2)
216 		usage();
217 	if (strcasecmp(argv[1], "create") == 0)
218 		action = CREATE;
219 	else if (strcasecmp(argv[1], "rescue") == 0)
220 		action = RESCUE;
221 	else if (strcasecmp(argv[1], "destroy") == 0)
222 		action = DESTROY;
223 	else if (strcasecmp(argv[1], "list") == 0)
224 		action = LIST;
225 	else
226 		usage();
227 	argc -= 1;
228 	argv += 1;
229 	for (;;) {
230 		int ch;
231 
232 		ch = getopt(argc, argv, "fo:s:t:u:v");
233 		if (ch == -1)
234 			break;
235 		switch (ch) {
236 		case 'f':
237 			if (action != DESTROY)
238 				usage();
239 			force = 1;
240 			break;
241 		case 'o':
242 			if (action != CREATE && action != RESCUE)
243 				usage();
244 			if (strcasecmp("ro", optarg) == 0)
245 				flags = G_GATE_FLAG_READONLY;
246 			else if (strcasecmp("wo", optarg) == 0)
247 				flags = G_GATE_FLAG_WRITEONLY;
248 			else if (strcasecmp("rw", optarg) == 0)
249 				flags = 0;
250 			else {
251 				errx(EXIT_FAILURE,
252 				    "Invalid argument for '-o' option.");
253 			}
254 			break;
255 		case 's':
256 			if (action != CREATE)
257 				usage();
258 			errno = 0;
259 			sectorsize = strtoul(optarg, NULL, 10);
260 			if (sectorsize == 0 && errno != 0)
261 				errx(EXIT_FAILURE, "Invalid sectorsize.");
262 			break;
263 		case 't':
264 			if (action != CREATE)
265 				usage();
266 			errno = 0;
267 			timeout = strtoul(optarg, NULL, 10);
268 			if (timeout == 0 && errno != 0)
269 				errx(EXIT_FAILURE, "Invalid timeout.");
270 			break;
271 		case 'u':
272 			errno = 0;
273 			unit = strtol(optarg, NULL, 10);
274 			if (unit == 0 && errno != 0)
275 				errx(EXIT_FAILURE, "Invalid unit number.");
276 			break;
277 		case 'v':
278 			if (action == DESTROY)
279 				usage();
280 			g_gate_verbose++;
281 			break;
282 		default:
283 			usage();
284 		}
285 	}
286 	argc -= optind;
287 	argv += optind;
288 
289 	switch (action) {
290 	case CREATE:
291 		if (argc != 1)
292 			usage();
293 		g_gate_load_module();
294 		g_gate_open_device();
295 		path = argv[0];
296 		g_gatel_create();
297 		break;
298 	case RESCUE:
299 		if (argc != 1)
300 			usage();
301 		if (unit == -1) {
302 			fprintf(stderr, "Required unit number.\n");
303 			usage();
304 		}
305 		g_gate_open_device();
306 		path = argv[0];
307 		g_gatel_rescue();
308 		break;
309 	case DESTROY:
310 		if (unit == -1) {
311 			fprintf(stderr, "Required unit number.\n");
312 			usage();
313 		}
314 		g_gate_verbose = 1;
315 		g_gate_open_device();
316 		g_gate_destroy(unit, force);
317 		break;
318 	case LIST:
319 		g_gate_list(unit, g_gate_verbose);
320 		break;
321 	case UNSET:
322 	default:
323 		usage();
324 	}
325 	g_gate_close_device();
326 	exit(EXIT_SUCCESS);
327 }
328