2 * Copyright (c) 2004 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
38 #include <sys/param.h>
42 #include <sys/ioctl.h>
44 #include <sys/syslog.h>
46 #include <geom/gate/g_gate.h>
50 enum { UNSET, CREATE, DESTROY, LIST, RESCUE } action = UNSET;
52 static const char *path = NULL;
53 static int unit = G_GATE_UNIT_AUTO;
54 static unsigned flags = 0;
56 static unsigned sectorsize = 0;
57 static unsigned timeout = G_GATE_TIMEOUT;
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());
73 g_gate_openflags(unsigned ggflags)
76 if ((ggflags & G_GATE_FLAG_READONLY) != 0)
78 else if ((ggflags & G_GATE_FLAG_WRITEONLY) != 0)
86 struct g_gate_ctl_io ggio;
89 if (g_gate_verbose == 0) {
90 if (daemon(0, 0) == -1) {
91 g_gate_destroy(unit, 1);
92 err(EXIT_FAILURE, "Cannot daemonize");
95 g_gate_log(LOG_DEBUG, "Worker created: %u.", getpid());
96 ggio.gctl_version = G_GATE_VERSION;
97 ggio.gctl_unit = unit;
99 ggio.gctl_data = malloc(bsize);
103 ggio.gctl_length = bsize;
105 g_gate_ioctl(G_GATE_CMD_START, &ggio);
106 error = ggio.gctl_error;
111 /* Exit gracefully. */
112 free(ggio.gctl_data);
113 g_gate_close_device();
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,
122 if (ggio.gctl_data != NULL) {
123 bsize = ggio.gctl_length;
129 g_gate_xlog("ioctl(/dev/%s): %s.", G_GATE_CTL_NAME,
134 switch (ggio.gctl_cmd) {
136 if ((size_t)ggio.gctl_length > bsize) {
137 ggio.gctl_data = realloc(ggio.gctl_data,
139 if (ggio.gctl_data != NULL)
140 bsize = ggio.gctl_length;
145 if (pread(fd, ggio.gctl_data, ggio.gctl_length,
146 ggio.gctl_offset) == -1) {
153 if (pwrite(fd, ggio.gctl_data, ggio.gctl_length,
154 ggio.gctl_offset) == -1) {
162 ggio.gctl_error = error;
163 g_gate_ioctl(G_GATE_CMD_DONE, &ggio);
170 struct g_gate_ctl_create ggioc;
173 fd = open(path, g_gate_openflags(flags) | O_DIRECT | O_FSYNC);
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);
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);
188 printf("%s%u\n", G_GATE_PROVIDER_NAME, ggioc.gctl_unit);
189 unit = ggioc.gctl_unit;
196 struct g_gate_ctl_cancel ggioc;
199 fd = open(path, g_gate_openflags(flags));
201 err(EXIT_FAILURE, "Cannot open %s", path);
203 ggioc.gctl_version = G_GATE_VERSION;
204 ggioc.gctl_unit = unit;
206 g_gate_ioctl(G_GATE_CMD_CANCEL, &ggioc);
212 main(int argc, char *argv[])
217 if (strcasecmp(argv[1], "create") == 0)
219 else if (strcasecmp(argv[1], "rescue") == 0)
221 else if (strcasecmp(argv[1], "destroy") == 0)
223 else if (strcasecmp(argv[1], "list") == 0)
232 ch = getopt(argc, argv, "fo:s:t:u:v");
237 if (action != DESTROY)
242 if (action != CREATE && action != RESCUE)
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)
252 "Invalid argument for '-o' option.");
256 if (action != CREATE)
259 sectorsize = strtoul(optarg, NULL, 10);
260 if (sectorsize == 0 && errno != 0)
261 errx(EXIT_FAILURE, "Invalid sectorsize.");
264 if (action != CREATE)
267 timeout = strtoul(optarg, NULL, 10);
268 if (timeout == 0 && errno != 0)
269 errx(EXIT_FAILURE, "Invalid timeout.");
273 unit = strtol(optarg, NULL, 10);
274 if (unit == 0 && errno != 0)
275 errx(EXIT_FAILURE, "Invalid unit number.");
278 if (action == DESTROY)
293 g_gate_load_module();
294 g_gate_open_device();
302 fprintf(stderr, "Required unit number.\n");
305 g_gate_open_device();
311 fprintf(stderr, "Required unit number.\n");
315 g_gate_open_device();
316 g_gate_destroy(unit, force);
319 g_gate_list(unit, g_gate_verbose);
325 g_gate_close_device();