2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2004 Pawel Jakub Dawidek <pjd@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
40 #include <sys/param.h>
44 #include <sys/ioctl.h>
46 #include <sys/syslog.h>
48 #include <geom/gate/g_gate.h>
52 static enum { UNSET, CREATE, DESTROY, LIST, RESCUE } action = UNSET;
54 static const char *path = NULL;
55 static int unit = G_GATE_UNIT_AUTO;
56 static unsigned flags = 0;
58 static unsigned sectorsize = 0;
59 static unsigned timeout = G_GATE_TIMEOUT;
65 fprintf(stderr, "usage: %s create [-v] [-o <ro|wo|rw>] "
66 "[-s sectorsize] [-t timeout] [-u unit] <path>\n", getprogname());
67 fprintf(stderr, " %s rescue [-v] [-o <ro|wo|rw>] <-u unit> "
68 "<path>\n", getprogname());
69 fprintf(stderr, " %s destroy [-f] <-u unit>\n", getprogname());
70 fprintf(stderr, " %s list [-v] [-u unit]\n", getprogname());
75 g_gate_openflags(unsigned ggflags)
78 if ((ggflags & G_GATE_FLAG_READONLY) != 0)
80 else if ((ggflags & G_GATE_FLAG_WRITEONLY) != 0)
88 struct g_gate_ctl_io ggio;
91 if (g_gate_verbose == 0) {
92 if (daemon(0, 0) == -1) {
93 g_gate_destroy(unit, 1);
94 err(EXIT_FAILURE, "Cannot daemonize");
97 g_gate_log(LOG_DEBUG, "Worker created: %u.", getpid());
98 ggio.gctl_version = G_GATE_VERSION;
99 ggio.gctl_unit = unit;
101 ggio.gctl_data = malloc(bsize);
105 ggio.gctl_length = bsize;
107 g_gate_ioctl(G_GATE_CMD_START, &ggio);
108 error = ggio.gctl_error;
113 /* Exit gracefully. */
114 free(ggio.gctl_data);
115 g_gate_close_device();
119 /* Buffer too small. */
120 assert(ggio.gctl_cmd == BIO_DELETE ||
121 ggio.gctl_cmd == BIO_WRITE);
122 ggio.gctl_data = realloc(ggio.gctl_data,
124 if (ggio.gctl_data != NULL) {
125 bsize = ggio.gctl_length;
131 g_gate_xlog("ioctl(/dev/%s): %s.", G_GATE_CTL_NAME,
136 switch (ggio.gctl_cmd) {
138 if ((size_t)ggio.gctl_length > bsize) {
139 ggio.gctl_data = realloc(ggio.gctl_data,
141 if (ggio.gctl_data != NULL)
142 bsize = ggio.gctl_length;
147 if (pread(fd, ggio.gctl_data, ggio.gctl_length,
148 ggio.gctl_offset) == -1) {
155 if (pwrite(fd, ggio.gctl_data, ggio.gctl_length,
156 ggio.gctl_offset) == -1) {
164 ggio.gctl_error = error;
165 g_gate_ioctl(G_GATE_CMD_DONE, &ggio);
172 struct g_gate_ctl_create ggioc;
175 fd = open(path, g_gate_openflags(flags) | O_DIRECT | O_FSYNC);
177 err(EXIT_FAILURE, "Cannot open %s", path);
178 memset(&ggioc, 0, sizeof(ggioc));
179 ggioc.gctl_version = G_GATE_VERSION;
180 ggioc.gctl_unit = unit;
181 ggioc.gctl_mediasize = g_gate_mediasize(fd);
183 sectorsize = g_gate_sectorsize(fd);
184 ggioc.gctl_sectorsize = sectorsize;
185 ggioc.gctl_timeout = timeout;
186 ggioc.gctl_flags = flags;
187 ggioc.gctl_maxcount = 0;
188 strlcpy(ggioc.gctl_info, path, sizeof(ggioc.gctl_info));
189 g_gate_ioctl(G_GATE_CMD_CREATE, &ggioc);
191 printf("%s%u\n", G_GATE_PROVIDER_NAME, ggioc.gctl_unit);
192 unit = ggioc.gctl_unit;
199 struct g_gate_ctl_cancel ggioc;
202 fd = open(path, g_gate_openflags(flags));
204 err(EXIT_FAILURE, "Cannot open %s", path);
206 ggioc.gctl_version = G_GATE_VERSION;
207 ggioc.gctl_unit = unit;
209 g_gate_ioctl(G_GATE_CMD_CANCEL, &ggioc);
215 main(int argc, char *argv[])
220 if (strcasecmp(argv[1], "create") == 0)
222 else if (strcasecmp(argv[1], "rescue") == 0)
224 else if (strcasecmp(argv[1], "destroy") == 0)
226 else if (strcasecmp(argv[1], "list") == 0)
235 ch = getopt(argc, argv, "fo:s:t:u:v");
240 if (action != DESTROY)
245 if (action != CREATE && action != RESCUE)
247 if (strcasecmp("ro", optarg) == 0)
248 flags = G_GATE_FLAG_READONLY;
249 else if (strcasecmp("wo", optarg) == 0)
250 flags = G_GATE_FLAG_WRITEONLY;
251 else if (strcasecmp("rw", optarg) == 0)
255 "Invalid argument for '-o' option.");
259 if (action != CREATE)
262 sectorsize = strtoul(optarg, NULL, 10);
263 if (sectorsize == 0 && errno != 0)
264 errx(EXIT_FAILURE, "Invalid sectorsize.");
267 if (action != CREATE)
270 timeout = strtoul(optarg, NULL, 10);
271 if (timeout == 0 && errno != 0)
272 errx(EXIT_FAILURE, "Invalid timeout.");
276 unit = strtol(optarg, NULL, 10);
277 if (unit == 0 && errno != 0)
278 errx(EXIT_FAILURE, "Invalid unit number.");
281 if (action == DESTROY)
296 g_gate_load_module();
297 g_gate_open_device();
305 fprintf(stderr, "Required unit number.\n");
308 g_gate_open_device();
314 fprintf(stderr, "Required unit number.\n");
318 g_gate_open_device();
319 g_gate_destroy(unit, force);
322 g_gate_list(unit, g_gate_verbose);
328 g_gate_close_device();