geom_gate userland utility improvements
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

329 lines
7.7 KiB

  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. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <stdint.h>
  31. #include <fcntl.h>
  32. #include <unistd.h>
  33. #include <string.h>
  34. #include <err.h>
  35. #include <errno.h>
  36. #include <assert.h>
  37. #include <sys/param.h>
  38. #include <sys/time.h>
  39. #include <sys/bio.h>
  40. #include <sys/disk.h>
  41. #include <sys/ioctl.h>
  42. #include <sys/stat.h>
  43. #include <sys/syslog.h>
  44. #include <geom/gate/g_gate.h>
  45. #include "ggate.h"
  46. static enum { UNSET, CREATE, DESTROY, LIST, RESCUE } action = UNSET;
  47. static const char *path = NULL;
  48. static int unit = G_GATE_UNIT_AUTO;
  49. static unsigned flags = 0;
  50. static int force = 0;
  51. static unsigned sectorsize = 0;
  52. static unsigned timeout = G_GATE_TIMEOUT;
  53. static void
  54. usage(void)
  55. {
  56. fprintf(stderr, "usage: %s create [-v] [-o <ro|wo|rw>] "
  57. "[-s sectorsize] [-t timeout] [-u unit] <path>\n", getprogname());
  58. fprintf(stderr, " %s rescue [-v] [-o <ro|wo|rw>] <-u unit> "
  59. "<path>\n", getprogname());
  60. fprintf(stderr, " %s destroy [-f] <-u unit>\n", getprogname());
  61. fprintf(stderr, " %s list [-v] [-u unit]\n", getprogname());
  62. exit(EXIT_FAILURE);
  63. }
  64. static int
  65. g_gate_openflags(unsigned ggflags)
  66. {
  67. if ((ggflags & G_GATE_FLAG_READONLY) != 0)
  68. return (O_RDONLY);
  69. else if ((ggflags & G_GATE_FLAG_WRITEONLY) != 0)
  70. return (O_WRONLY);
  71. return (O_RDWR);
  72. }
  73. static void
  74. g_gatel_serve(int fd)
  75. {
  76. struct g_gate_ctl_io ggio;
  77. size_t bsize;
  78. if (g_gate_verbose == 0) {
  79. if (daemon(0, 0) == -1) {
  80. g_gate_destroy(unit, 1);
  81. err(EXIT_FAILURE, "Cannot daemonize");
  82. }
  83. }
  84. g_gate_log(LOG_DEBUG, "Worker created: %u.", getpid());
  85. ggio.gctl_version = G_GATE_VERSION;
  86. ggio.gctl_unit = unit;
  87. bsize = sectorsize;
  88. ggio.gctl_data = malloc(bsize);
  89. for (;;) {
  90. int error;
  91. once_again:
  92. ggio.gctl_length = bsize;
  93. ggio.gctl_error = 0;
  94. g_gate_ioctl(G_GATE_CMD_START, &ggio);
  95. error = ggio.gctl_error;
  96. switch (error) {
  97. case 0:
  98. break;
  99. case ECANCELED:
  100. /* Exit gracefully. */
  101. free(ggio.gctl_data);
  102. g_gate_close_device();
  103. close(fd);
  104. exit(EXIT_SUCCESS);
  105. case ENOMEM:
  106. /* Buffer too small. */
  107. assert(ggio.gctl_cmd == BIO_DELETE ||
  108. ggio.gctl_cmd == BIO_WRITE);
  109. ggio.gctl_data = realloc(ggio.gctl_data,
  110. ggio.gctl_length);
  111. if (ggio.gctl_data != NULL) {
  112. bsize = ggio.gctl_length;
  113. goto once_again;
  114. }
  115. /* FALLTHROUGH */
  116. case ENXIO:
  117. default:
  118. g_gate_xlog("ioctl(/dev/%s): %s.", G_GATE_CTL_NAME,
  119. strerror(error));
  120. }
  121. error = 0;
  122. switch (ggio.gctl_cmd) {
  123. case BIO_READ:
  124. if ((size_t)ggio.gctl_length > bsize) {
  125. ggio.gctl_data = realloc(ggio.gctl_data,
  126. ggio.gctl_length);
  127. if (ggio.gctl_data != NULL)
  128. bsize = ggio.gctl_length;
  129. else
  130. error = ENOMEM;
  131. }
  132. if (error == 0) {
  133. if (pread(fd, ggio.gctl_data, ggio.gctl_length,
  134. ggio.gctl_offset) == -1) {
  135. error = errno;
  136. }
  137. }
  138. break;
  139. case BIO_DELETE:
  140. case BIO_WRITE:
  141. if (pwrite(fd, ggio.gctl_data, ggio.gctl_length,
  142. ggio.gctl_offset) == -1) {
  143. error = errno;
  144. }
  145. break;
  146. default:
  147. error = EOPNOTSUPP;
  148. }
  149. ggio.gctl_error = error;
  150. g_gate_ioctl(G_GATE_CMD_DONE, &ggio);
  151. }
  152. }
  153. static void
  154. g_gatel_create(void)
  155. {
  156. struct g_gate_ctl_create ggioc;
  157. int fd;
  158. fd = open(path, g_gate_openflags(flags) | O_DIRECT | O_FSYNC);
  159. if (fd == -1)
  160. err(EXIT_FAILURE, "Cannot open %s", path);
  161. memset(&ggioc, 0, sizeof(ggioc));
  162. ggioc.gctl_version = G_GATE_VERSION;
  163. ggioc.gctl_unit = unit;
  164. ggioc.gctl_mediasize = g_gate_mediasize(fd);
  165. if (sectorsize == 0)
  166. sectorsize = g_gate_sectorsize(fd);
  167. ggioc.gctl_sectorsize = sectorsize;
  168. ggioc.gctl_timeout = timeout;
  169. ggioc.gctl_flags = flags;
  170. ggioc.gctl_maxcount = 0;
  171. strlcpy(ggioc.gctl_info, path, sizeof(ggioc.gctl_info));
  172. g_gate_ioctl(G_GATE_CMD_CREATE, &ggioc);
  173. if (unit == -1)
  174. printf("%s%u\n", G_GATE_PROVIDER_NAME, ggioc.gctl_unit);
  175. unit = ggioc.gctl_unit;
  176. g_gatel_serve(fd);
  177. }
  178. static void
  179. g_gatel_rescue(void)
  180. {
  181. struct g_gate_ctl_cancel ggioc;
  182. int fd;
  183. fd = open(path, g_gate_openflags(flags));
  184. if (fd == -1)
  185. err(EXIT_FAILURE, "Cannot open %s", path);
  186. ggioc.gctl_version = G_GATE_VERSION;
  187. ggioc.gctl_unit = unit;
  188. ggioc.gctl_seq = 0;
  189. g_gate_ioctl(G_GATE_CMD_CANCEL, &ggioc);
  190. g_gatel_serve(fd);
  191. }
  192. int
  193. main(int argc, char *argv[])
  194. {
  195. if (argc < 2)
  196. usage();
  197. if (strcasecmp(argv[1], "create") == 0)
  198. action = CREATE;
  199. else if (strcasecmp(argv[1], "rescue") == 0)
  200. action = RESCUE;
  201. else if (strcasecmp(argv[1], "destroy") == 0)
  202. action = DESTROY;
  203. else if (strcasecmp(argv[1], "list") == 0)
  204. action = LIST;
  205. else
  206. usage();
  207. argc -= 1;
  208. argv += 1;
  209. for (;;) {
  210. int ch;
  211. ch = getopt(argc, argv, "fo:s:t:u:v");
  212. if (ch == -1)
  213. break;
  214. switch (ch) {
  215. case 'f':
  216. if (action != DESTROY)
  217. usage();
  218. force = 1;
  219. break;
  220. case 'o':
  221. if (action != CREATE && action != RESCUE)
  222. usage();
  223. if (strcasecmp("ro", optarg) == 0)
  224. flags = G_GATE_FLAG_READONLY;
  225. else if (strcasecmp("wo", optarg) == 0)
  226. flags = G_GATE_FLAG_WRITEONLY;
  227. else if (strcasecmp("rw", optarg) == 0)
  228. flags = 0;
  229. else {
  230. errx(EXIT_FAILURE,
  231. "Invalid argument for '-o' option.");
  232. }
  233. break;
  234. case 's':
  235. if (action != CREATE)
  236. usage();
  237. errno = 0;
  238. sectorsize = strtoul(optarg, NULL, 10);
  239. if (sectorsize == 0 && errno != 0)
  240. errx(EXIT_FAILURE, "Invalid sectorsize.");
  241. break;
  242. case 't':
  243. if (action != CREATE)
  244. usage();
  245. errno = 0;
  246. timeout = strtoul(optarg, NULL, 10);
  247. if (timeout == 0 && errno != 0)
  248. errx(EXIT_FAILURE, "Invalid timeout.");
  249. break;
  250. case 'u':
  251. errno = 0;
  252. unit = strtol(optarg, NULL, 10);
  253. if (unit == 0 && errno != 0)
  254. errx(EXIT_FAILURE, "Invalid unit number.");
  255. break;
  256. case 'v':
  257. if (action == DESTROY)
  258. usage();
  259. g_gate_verbose++;
  260. break;
  261. default:
  262. usage();
  263. }
  264. }
  265. argc -= optind;
  266. argv += optind;
  267. switch (action) {
  268. case CREATE:
  269. if (argc != 1)
  270. usage();
  271. g_gate_load_module();
  272. g_gate_open_device();
  273. path = argv[0];
  274. g_gatel_create();
  275. break;
  276. case RESCUE:
  277. if (argc != 1)
  278. usage();
  279. if (unit == -1) {
  280. fprintf(stderr, "Required unit number.\n");
  281. usage();
  282. }
  283. g_gate_open_device();
  284. path = argv[0];
  285. g_gatel_rescue();
  286. break;
  287. case DESTROY:
  288. if (unit == -1) {
  289. fprintf(stderr, "Required unit number.\n");
  290. usage();
  291. }
  292. g_gate_verbose = 1;
  293. g_gate_open_device();
  294. g_gate_destroy(unit, force);
  295. break;
  296. case LIST:
  297. g_gate_list(unit, g_gate_verbose);
  298. break;
  299. case UNSET:
  300. default:
  301. usage();
  302. }
  303. g_gate_close_device();
  304. exit(EXIT_SUCCESS);
  305. }