ggatel.c revision 285748
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: stable/10/sbin/ggate/ggatel/ggatel.c 285748 2015-07-21 14:25:21Z brueffer $
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
50static enum { UNSET, CREATE, DESTROY, LIST, RESCUE } action = UNSET;
51
52static const char *path = NULL;
53static int unit = G_GATE_UNIT_AUTO;
54static unsigned flags = 0;
55static int force = 0;
56static unsigned sectorsize = 0;
57static unsigned timeout = G_GATE_TIMEOUT;
58
59static void
60usage(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
72static int
73g_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
83static void
84g_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;
102once_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
167static void
168g_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
194static void
195g_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
212int
213main(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