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.
 
 
 
 

328 lines
7.6 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. 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. ggioc.gctl_version = G_GATE_VERSION;
  162. ggioc.gctl_unit = unit;
  163. ggioc.gctl_mediasize = g_gate_mediasize(fd);
  164. if (sectorsize == 0)
  165. sectorsize = g_gate_sectorsize(fd);
  166. ggioc.gctl_sectorsize = sectorsize;
  167. ggioc.gctl_timeout = timeout;
  168. ggioc.gctl_flags = flags;
  169. ggioc.gctl_maxcount = 0;
  170. strlcpy(ggioc.gctl_info, path, sizeof(ggioc.gctl_info));
  171. g_gate_ioctl(G_GATE_CMD_CREATE, &ggioc);
  172. if (unit == -1)
  173. printf("%s%u\n", G_GATE_PROVIDER_NAME, ggioc.gctl_unit);
  174. unit = ggioc.gctl_unit;
  175. g_gatel_serve(fd);
  176. }
  177. static void
  178. g_gatel_rescue(void)
  179. {
  180. struct g_gate_ctl_cancel ggioc;
  181. int fd;
  182. fd = open(path, g_gate_openflags(flags));
  183. if (fd == -1)
  184. err(EXIT_FAILURE, "Cannot open %s", path);
  185. ggioc.gctl_version = G_GATE_VERSION;
  186. ggioc.gctl_unit = unit;
  187. ggioc.gctl_seq = 0;
  188. g_gate_ioctl(G_GATE_CMD_CANCEL, &ggioc);
  189. g_gatel_serve(fd);
  190. }
  191. int
  192. main(int argc, char *argv[])
  193. {
  194. if (argc < 2)
  195. usage();
  196. if (strcasecmp(argv[1], "create") == 0)
  197. action = CREATE;
  198. else if (strcasecmp(argv[1], "rescue") == 0)
  199. action = RESCUE;
  200. else if (strcasecmp(argv[1], "destroy") == 0)
  201. action = DESTROY;
  202. else if (strcasecmp(argv[1], "list") == 0)
  203. action = LIST;
  204. else
  205. usage();
  206. argc -= 1;
  207. argv += 1;
  208. for (;;) {
  209. int ch;
  210. ch = getopt(argc, argv, "fo:s:t:u:v");
  211. if (ch == -1)
  212. break;
  213. switch (ch) {
  214. case 'f':
  215. if (action != DESTROY)
  216. usage();
  217. force = 1;
  218. break;
  219. case 'o':
  220. if (action != CREATE && action != RESCUE)
  221. usage();
  222. if (strcasecmp("ro", optarg) == 0)
  223. flags = G_GATE_FLAG_READONLY;
  224. else if (strcasecmp("wo", optarg) == 0)
  225. flags = G_GATE_FLAG_WRITEONLY;
  226. else if (strcasecmp("rw", optarg) == 0)
  227. flags = 0;
  228. else {
  229. errx(EXIT_FAILURE,
  230. "Invalid argument for '-o' option.");
  231. }
  232. break;
  233. case 's':
  234. if (action != CREATE)
  235. usage();
  236. errno = 0;
  237. sectorsize = strtoul(optarg, NULL, 10);
  238. if (sectorsize == 0 && errno != 0)
  239. errx(EXIT_FAILURE, "Invalid sectorsize.");
  240. break;
  241. case 't':
  242. if (action != CREATE)
  243. usage();
  244. errno = 0;
  245. timeout = strtoul(optarg, NULL, 10);
  246. if (timeout == 0 && errno != 0)
  247. errx(EXIT_FAILURE, "Invalid timeout.");
  248. break;
  249. case 'u':
  250. errno = 0;
  251. unit = strtol(optarg, NULL, 10);
  252. if (unit == 0 && errno != 0)
  253. errx(EXIT_FAILURE, "Invalid unit number.");
  254. break;
  255. case 'v':
  256. if (action == DESTROY)
  257. usage();
  258. g_gate_verbose++;
  259. break;
  260. default:
  261. usage();
  262. }
  263. }
  264. argc -= optind;
  265. argv += optind;
  266. switch (action) {
  267. case CREATE:
  268. if (argc != 1)
  269. usage();
  270. g_gate_load_module();
  271. g_gate_open_device();
  272. path = argv[0];
  273. g_gatel_create();
  274. break;
  275. case RESCUE:
  276. if (argc != 1)
  277. usage();
  278. if (unit == -1) {
  279. fprintf(stderr, "Required unit number.\n");
  280. usage();
  281. }
  282. g_gate_open_device();
  283. path = argv[0];
  284. g_gatel_rescue();
  285. break;
  286. case DESTROY:
  287. if (unit == -1) {
  288. fprintf(stderr, "Required unit number.\n");
  289. usage();
  290. }
  291. g_gate_verbose = 1;
  292. g_gate_open_device();
  293. g_gate_destroy(unit, force);
  294. break;
  295. case LIST:
  296. g_gate_list(unit, g_gate_verbose);
  297. break;
  298. case UNSET:
  299. default:
  300. usage();
  301. }
  302. g_gate_close_device();
  303. exit(EXIT_SUCCESS);
  304. }