geom_gate userland utility improvements
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 

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