geom_gate userland utility improvements
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 

337 行
7.9 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 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 rescue [-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 int
  66. g_gate_openflags(unsigned ggflags)
  67. {
  68. if ((ggflags & G_GATE_FLAG_READONLY) != 0)
  69. return (O_RDONLY);
  70. else if ((ggflags & G_GATE_FLAG_WRITEONLY) != 0)
  71. return (O_WRONLY);
  72. return (O_RDWR);
  73. }
  74. static void
  75. g_gatel_serve(int fd)
  76. {
  77. struct g_gate_ctl_io ggio;
  78. size_t bsize;
  79. if (g_gate_verbose == 0) {
  80. if (daemon(0, 0) == -1) {
  81. g_gate_destroy(unit, 1);
  82. err(EXIT_FAILURE, "Cannot daemonize");
  83. }
  84. }
  85. g_gate_log(LOG_DEBUG, "Worker created: %u.", getpid());
  86. ggio.gctl_version = G_GATE_VERSION;
  87. ggio.gctl_unit = unit;
  88. bsize = sectorsize;
  89. ggio.gctl_data = malloc(bsize);
  90. for (;;) {
  91. int error;
  92. once_again:
  93. ggio.gctl_length = bsize;
  94. ggio.gctl_error = 0;
  95. g_gate_ioctl(G_GATE_CMD_START, &ggio);
  96. error = ggio.gctl_error;
  97. switch (error) {
  98. case 0:
  99. break;
  100. case ECANCELED:
  101. /* Exit gracefully. */
  102. free(ggio.gctl_data);
  103. g_gate_close_device();
  104. close(fd);
  105. exit(EXIT_SUCCESS);
  106. case ENOMEM:
  107. /* Buffer too small. */
  108. assert(ggio.gctl_cmd == BIO_DELETE ||
  109. ggio.gctl_cmd == BIO_WRITE);
  110. ggio.gctl_data = realloc(ggio.gctl_data,
  111. ggio.gctl_length);
  112. if (ggio.gctl_data != NULL) {
  113. bsize = ggio.gctl_length;
  114. goto once_again;
  115. }
  116. /* FALLTHROUGH */
  117. case ENXIO:
  118. default:
  119. g_gate_xlog("ioctl(/dev/%s): %s.", G_GATE_CTL_NAME,
  120. strerror(error));
  121. }
  122. error = 0;
  123. switch (ggio.gctl_cmd) {
  124. case BIO_READ:
  125. if ((size_t)ggio.gctl_length > bsize) {
  126. ggio.gctl_data = realloc(ggio.gctl_data,
  127. ggio.gctl_length);
  128. if (ggio.gctl_data != NULL)
  129. bsize = ggio.gctl_length;
  130. else
  131. error = ENOMEM;
  132. }
  133. if (error == 0) {
  134. if (pread(fd, ggio.gctl_data, ggio.gctl_length,
  135. ggio.gctl_offset) == -1) {
  136. error = errno;
  137. }
  138. }
  139. break;
  140. case BIO_DELETE:
  141. case BIO_WRITE:
  142. if (pwrite(fd, ggio.gctl_data, ggio.gctl_length,
  143. ggio.gctl_offset) == -1) {
  144. error = errno;
  145. }
  146. break;
  147. default:
  148. error = EOPNOTSUPP;
  149. }
  150. ggio.gctl_error = error;
  151. g_gate_ioctl(G_GATE_CMD_DONE, &ggio);
  152. }
  153. }
  154. static void
  155. g_gatel_create(void)
  156. {
  157. struct g_gate_ctl_create ggioc;
  158. int fd;
  159. fd = open(path, g_gate_openflags(flags) | O_DIRECT | O_FSYNC);
  160. if (fd == -1)
  161. err(EXIT_FAILURE, "Cannot open %s", path);
  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 = queue_size;
  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:q: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 'q':
  235. if (action != CREATE)
  236. usage();
  237. errno = 0;
  238. queue_size = strtoul(optarg, NULL, 10);
  239. if (queue_size == 0 && errno != 0)
  240. errx(EXIT_FAILURE, "Invalid queue_size.");
  241. break;
  242. case 's':
  243. if (action != CREATE)
  244. usage();
  245. errno = 0;
  246. sectorsize = strtoul(optarg, NULL, 10);
  247. if (sectorsize == 0 && errno != 0)
  248. errx(EXIT_FAILURE, "Invalid sectorsize.");
  249. break;
  250. case 't':
  251. if (action != CREATE)
  252. usage();
  253. errno = 0;
  254. timeout = strtoul(optarg, NULL, 10);
  255. if (timeout == 0 && errno != 0)
  256. errx(EXIT_FAILURE, "Invalid timeout.");
  257. break;
  258. case 'u':
  259. errno = 0;
  260. unit = strtol(optarg, NULL, 10);
  261. if (unit == 0 && errno != 0)
  262. errx(EXIT_FAILURE, "Invalid unit number.");
  263. break;
  264. case 'v':
  265. if (action == DESTROY)
  266. usage();
  267. g_gate_verbose++;
  268. break;
  269. default:
  270. usage();
  271. }
  272. }
  273. argc -= optind;
  274. argv += optind;
  275. switch (action) {
  276. case CREATE:
  277. if (argc != 1)
  278. usage();
  279. g_gate_load_module();
  280. g_gate_open_device();
  281. path = argv[0];
  282. g_gatel_create();
  283. break;
  284. case RESCUE:
  285. if (argc != 1)
  286. usage();
  287. if (unit == -1) {
  288. fprintf(stderr, "Required unit number.\n");
  289. usage();
  290. }
  291. g_gate_open_device();
  292. path = argv[0];
  293. g_gatel_rescue();
  294. break;
  295. case DESTROY:
  296. if (unit == -1) {
  297. fprintf(stderr, "Required unit number.\n");
  298. usage();
  299. }
  300. g_gate_verbose = 1;
  301. g_gate_open_device();
  302. g_gate_destroy(unit, force);
  303. break;
  304. case LIST:
  305. g_gate_list(unit, g_gate_verbose);
  306. break;
  307. case UNSET:
  308. default:
  309. usage();
  310. }
  311. g_gate_close_device();
  312. exit(EXIT_SUCCESS);
  313. }