1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2004 Pawel Jakub Dawidek <pjd@FreeBSD.org> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 * $FreeBSD$ 29 */ 30 31 #include <stdio.h> 32 #include <stdlib.h> 33 #include <stdint.h> 34 #include <fcntl.h> 35 #include <unistd.h> 36 #include <string.h> 37 #include <ctype.h> 38 #include <inttypes.h> 39 #include <libgen.h> 40 #include <pthread.h> 41 #include <signal.h> 42 #include <err.h> 43 #include <errno.h> 44 #include <assert.h> 45 46 #include <sys/param.h> 47 #include <sys/ioctl.h> 48 #include <sys/socket.h> 49 #include <sys/sysctl.h> 50 #include <sys/syslog.h> 51 #include <sys/time.h> 52 #include <sys/bio.h> 53 #include <netinet/in.h> 54 #include <netinet/tcp.h> 55 #include <arpa/inet.h> 56 57 #include <geom/gate/g_gate.h> 58 #include "ggate.h" 59 60 61 static enum { UNSET, CREATE, DESTROY, LIST, RESCUE } action = UNSET; 62 63 static const char *path = NULL; 64 static const char *host = NULL; 65 static int unit = G_GATE_UNIT_AUTO; 66 static unsigned flags = 0; 67 static int force = 0; 68 static unsigned queue_size = G_GATE_QUEUE_SIZE; 69 static unsigned port = G_GATE_PORT; 70 static off_t mediasize; 71 static unsigned sectorsize = 0; 72 static unsigned timeout = G_GATE_TIMEOUT; 73 static int sendfd, recvfd; 74 static uint32_t token; 75 static pthread_t sendtd, recvtd; 76 static int reconnect; 77 78 static void 79 usage(void) 80 { 81 82 fprintf(stderr, "usage: %s create [-nv] [-o <ro|wo|rw>] [-p port] " 83 "[-q queue_size] [-R rcvbuf] [-S sndbuf] [-s sectorsize] " 84 "[-t timeout] [-u unit] <host> <path>\n", getprogname()); 85 fprintf(stderr, " %s rescue [-nv] [-o <ro|wo|rw>] [-p port] " 86 "[-R rcvbuf] [-S sndbuf] <-u unit> <host> <path>\n", getprogname()); 87 fprintf(stderr, " %s destroy [-f] <-u unit>\n", getprogname()); 88 fprintf(stderr, " %s list [-v] [-u unit]\n", getprogname()); 89 exit(EXIT_FAILURE); 90 } 91 92 static void * 93 send_thread(void *arg __unused) 94 { 95 struct g_gate_ctl_io ggio; 96 struct g_gate_hdr hdr; 97 char buf[MAXPHYS]; 98 ssize_t data; 99 int error; 100 101 g_gate_log(LOG_NOTICE, "%s: started!", __func__); 102 103 ggio.gctl_version = G_GATE_VERSION; 104 ggio.gctl_unit = unit; 105 ggio.gctl_data = buf; 106 107 for (;;) { 108 ggio.gctl_length = sizeof(buf); 109 ggio.gctl_error = 0; 110 g_gate_ioctl(G_GATE_CMD_START, &ggio); 111 error = ggio.gctl_error; 112 switch (error) { 113 case 0: 114 break; 115 case ECANCELED: 116 if (reconnect) 117 break; 118 /* Exit gracefully. */ 119 g_gate_close_device(); 120 exit(EXIT_SUCCESS); 121 #if 0 122 case ENOMEM: 123 /* Buffer too small. */ 124 ggio.gctl_data = realloc(ggio.gctl_data, 125 ggio.gctl_length); 126 if (ggio.gctl_data != NULL) { 127 bsize = ggio.gctl_length; 128 goto once_again; 129 } 130 /* FALLTHROUGH */ 131 #endif 132 case ENXIO: 133 default: 134 g_gate_xlog("ioctl(/dev/%s): %s.", G_GATE_CTL_NAME, 135 strerror(error)); 136 } 137 138 if (reconnect) 139 break; 140 141 switch (ggio.gctl_cmd) { 142 case BIO_READ: 143 hdr.gh_cmd = GGATE_CMD_READ; 144 break; 145 case BIO_WRITE: 146 hdr.gh_cmd = GGATE_CMD_WRITE; 147 break; 148 default: 149 g_gate_log(LOG_NOTICE, "Unknown gctl_cmd: %i", ggio.gctl_cmd); 150 ggio.gctl_error = EOPNOTSUPP; 151 g_gate_ioctl(G_GATE_CMD_DONE, &ggio); 152 continue; 153 } 154 155 /* Don't send requests for more data than we can handle the response for! */ 156 if (ggio.gctl_length > MAXPHYS) { 157 g_gate_log(LOG_ERR, "Request too big: %zd", ggio.gctl_length); 158 ggio.gctl_error = EOPNOTSUPP; 159 g_gate_ioctl(G_GATE_CMD_DONE, &ggio); 160 continue; 161 } 162 163 hdr.gh_seq = ggio.gctl_seq; 164 hdr.gh_offset = ggio.gctl_offset; 165 hdr.gh_length = ggio.gctl_length; 166 hdr.gh_error = 0; 167 g_gate_swap2n_hdr(&hdr); 168 169 data = g_gate_send(sendfd, &hdr, sizeof(hdr), MSG_NOSIGNAL); 170 g_gate_log(LOG_DEBUG, "Sent hdr packet."); 171 g_gate_swap2h_hdr(&hdr); 172 if (reconnect) 173 break; 174 if (data != sizeof(hdr)) { 175 g_gate_log(LOG_ERR, "Lost connection 1."); 176 reconnect = 1; 177 pthread_kill(recvtd, SIGUSR1); 178 break; 179 } 180 181 if (hdr.gh_cmd == GGATE_CMD_WRITE) { 182 data = g_gate_send(sendfd, ggio.gctl_data, 183 ggio.gctl_length, MSG_NOSIGNAL); 184 if (reconnect) 185 break; 186 if (data != ggio.gctl_length) { 187 g_gate_log(LOG_ERR, "Lost connection 2 (%zd != %zd).", data, (ssize_t)ggio.gctl_length); 188 reconnect = 1; 189 pthread_kill(recvtd, SIGUSR1); 190 break; 191 } 192 g_gate_log(LOG_DEBUG, "Sent %zd bytes (offset=%" 193 PRIu64 ", length=%" PRIu32 ").", data, 194 hdr.gh_offset, hdr.gh_length); 195 } 196 } 197 g_gate_log(LOG_DEBUG, "%s: Died.", __func__); 198 return (NULL); 199 } 200 201 static void * 202 recv_thread(void *arg __unused) 203 { 204 struct g_gate_ctl_io ggio; 205 struct g_gate_hdr hdr; 206 char buf[MAXPHYS]; 207 ssize_t data; 208 209 g_gate_log(LOG_NOTICE, "%s: started!", __func__); 210 211 ggio.gctl_version = G_GATE_VERSION; 212 ggio.gctl_unit = unit; 213 ggio.gctl_data = buf; 214 215 for (;;) { 216 data = g_gate_recv(recvfd, &hdr, sizeof(hdr), MSG_WAITALL); 217 if (reconnect) 218 break; 219 g_gate_swap2h_hdr(&hdr); 220 if (data != sizeof(hdr)) { 221 if (data == -1 && errno == EAGAIN) 222 continue; 223 g_gate_log(LOG_ERR, "Lost connection 3."); 224 reconnect = 1; 225 pthread_kill(sendtd, SIGUSR1); 226 break; 227 } 228 g_gate_log(LOG_DEBUG, "Received hdr packet."); 229 230 ggio.gctl_seq = hdr.gh_seq; 231 ggio.gctl_cmd = hdr.gh_cmd; 232 ggio.gctl_offset = hdr.gh_offset; 233 ggio.gctl_length = hdr.gh_length; 234 ggio.gctl_error = hdr.gh_error; 235 236 /* Do not overflow our buffer if there is a bogus response. */ 237 if (ggio.gctl_length > (off_t) sizeof(buf)) { 238 g_gate_log(LOG_ERR, "Received too big response: %zd", ggio.gctl_length); 239 break; 240 } 241 242 if (ggio.gctl_error == 0 && ggio.gctl_cmd == GGATE_CMD_READ) { 243 data = g_gate_recv(recvfd, ggio.gctl_data, 244 ggio.gctl_length, MSG_WAITALL); 245 if (reconnect) 246 break; 247 g_gate_log(LOG_DEBUG, "Received data packet."); 248 if (data != ggio.gctl_length) { 249 g_gate_log(LOG_ERR, "Lost connection 4."); 250 reconnect = 1; 251 pthread_kill(sendtd, SIGUSR1); 252 break; 253 } 254 g_gate_log(LOG_DEBUG, "Received %d bytes (offset=%" 255 PRIu64 ", length=%" PRIu32 ").", data, 256 hdr.gh_offset, hdr.gh_length); 257 } 258 259 g_gate_ioctl(G_GATE_CMD_DONE, &ggio); 260 } 261 g_gate_log(LOG_DEBUG, "%s: Died.", __func__); 262 pthread_exit(NULL); 263 } 264 265 static int 266 handshake(int dir) 267 { 268 struct g_gate_version ver; 269 struct g_gate_cinit cinit; 270 struct g_gate_sinit sinit; 271 struct sockaddr_in serv; 272 int sfd; 273 274 /* 275 * Do the network stuff. 276 */ 277 bzero(&serv, sizeof(serv)); 278 serv.sin_family = AF_INET; 279 serv.sin_addr.s_addr = g_gate_str2ip(host); 280 if (serv.sin_addr.s_addr == INADDR_NONE) { 281 g_gate_log(LOG_DEBUG, "Invalid IP/host name: %s.", host); 282 return (-1); 283 } 284 serv.sin_port = htons(port); 285 sfd = socket(AF_INET, SOCK_STREAM, 0); 286 if (sfd == -1) { 287 g_gate_log(LOG_DEBUG, "Cannot open socket: %s.", 288 strerror(errno)); 289 return (-1); 290 } 291 292 g_gate_socket_settings(sfd); 293 294 if (connect(sfd, (struct sockaddr *)&serv, sizeof(serv)) == -1) { 295 g_gate_log(LOG_DEBUG, "Cannot connect to server: %s.", 296 strerror(errno)); 297 close(sfd); 298 return (-1); 299 } 300 301 g_gate_log(LOG_INFO, "Connected to the server: %s:%d.", host, port); 302 303 /* 304 * Create and send version packet. 305 */ 306 g_gate_log(LOG_DEBUG, "Sending version packet."); 307 assert(strlen(GGATE_MAGIC) == sizeof(ver.gv_magic)); 308 bcopy(GGATE_MAGIC, ver.gv_magic, sizeof(ver.gv_magic)); 309 ver.gv_version = GGATE_VERSION; 310 ver.gv_error = 0; 311 g_gate_swap2n_version(&ver); 312 if (g_gate_send(sfd, &ver, sizeof(ver), MSG_NOSIGNAL) == -1) { 313 g_gate_log(LOG_DEBUG, "Error while sending version packet: %s.", 314 strerror(errno)); 315 close(sfd); 316 return (-1); 317 } 318 bzero(&ver, sizeof(ver)); 319 if (g_gate_recv(sfd, &ver, sizeof(ver), MSG_WAITALL) == -1) { 320 g_gate_log(LOG_DEBUG, "Error while receiving data: %s.", 321 strerror(errno)); 322 close(sfd); 323 return (-1); 324 } 325 if (ver.gv_error != 0) { 326 g_gate_log(LOG_DEBUG, "Version verification problem: %s.", 327 strerror(errno)); 328 close(sfd); 329 return (-1); 330 } 331 332 /* 333 * Create and send initial packet. 334 */ 335 g_gate_log(LOG_DEBUG, "Sending initial packet."); 336 if (strlcpy(cinit.gc_path, path, sizeof(cinit.gc_path)) >= 337 sizeof(cinit.gc_path)) { 338 g_gate_log(LOG_DEBUG, "Path name too long."); 339 close(sfd); 340 return (-1); 341 } 342 cinit.gc_flags = flags | dir; 343 cinit.gc_token = token; 344 cinit.gc_nconn = 2; 345 g_gate_swap2n_cinit(&cinit); 346 if (g_gate_send(sfd, &cinit, sizeof(cinit), MSG_NOSIGNAL) == -1) { 347 g_gate_log(LOG_DEBUG, "Error while sending initial packet: %s.", 348 strerror(errno)); 349 close(sfd); 350 return (-1); 351 } 352 g_gate_swap2h_cinit(&cinit); 353 354 /* 355 * Receiving initial packet from server. 356 */ 357 g_gate_log(LOG_DEBUG, "Receiving initial packet."); 358 if (g_gate_recv(sfd, &sinit, sizeof(sinit), MSG_WAITALL) == -1) { 359 g_gate_log(LOG_DEBUG, "Error while receiving data: %s.", 360 strerror(errno)); 361 close(sfd); 362 return (-1); 363 } 364 g_gate_swap2h_sinit(&sinit); 365 if (sinit.gs_error != 0) { 366 g_gate_log(LOG_DEBUG, "Error from server: %s.", 367 strerror(sinit.gs_error)); 368 close(sfd); 369 return (-1); 370 } 371 g_gate_log(LOG_DEBUG, "Received initial packet."); 372 373 mediasize = sinit.gs_mediasize; 374 if (sectorsize == 0) 375 sectorsize = sinit.gs_sectorsize; 376 377 return (sfd); 378 } 379 380 static void 381 mydaemon(void) 382 { 383 384 if (g_gate_verbose > 0) 385 return; 386 if (daemon(0, 0) == 0) 387 return; 388 if (action == CREATE) 389 g_gate_destroy(unit, 1); 390 err(EXIT_FAILURE, "Cannot daemonize"); 391 } 392 393 static int 394 g_gatec_connect(void) 395 { 396 397 token = arc4random(); 398 /* 399 * Our receive descriptor is connected to the send descriptor on the 400 * server side. 401 */ 402 recvfd = handshake(GGATE_FLAG_SEND); 403 if (recvfd == -1) 404 return (0); 405 /* 406 * Our send descriptor is connected to the receive descriptor on the 407 * server side. 408 */ 409 sendfd = handshake(GGATE_FLAG_RECV); 410 if (sendfd == -1) 411 return (0); 412 return (1); 413 } 414 415 static void 416 g_gatec_start(void) 417 { 418 int error; 419 420 reconnect = 0; 421 error = pthread_create(&recvtd, NULL, recv_thread, NULL); 422 if (error != 0) { 423 g_gate_destroy(unit, 1); 424 g_gate_xlog("pthread_create(recv_thread): %s.", 425 strerror(error)); 426 } 427 sendtd = pthread_self(); 428 send_thread(NULL); 429 /* Disconnected. */ 430 close(sendfd); 431 close(recvfd); 432 } 433 434 static void 435 signop(int sig __unused) 436 { 437 438 /* Do nothing. */ 439 } 440 441 static void 442 g_gatec_loop(void) 443 { 444 struct g_gate_ctl_cancel ggioc; 445 446 signal(SIGUSR1, signop); 447 for (;;) { 448 g_gatec_start(); 449 g_gate_log(LOG_NOTICE, "Disconnected [%s %s]. Connecting...", 450 host, path); 451 while (!g_gatec_connect()) { 452 sleep(2); 453 g_gate_log(LOG_NOTICE, "Connecting [%s %s]...", host, 454 path); 455 } 456 ggioc.gctl_version = G_GATE_VERSION; 457 ggioc.gctl_unit = unit; 458 ggioc.gctl_seq = 0; 459 g_gate_ioctl(G_GATE_CMD_CANCEL, &ggioc); 460 } 461 } 462 463 static void 464 g_gatec_create(void) 465 { 466 struct g_gate_ctl_create ggioc; 467 468 if (!g_gatec_connect()) 469 g_gate_xlog("Cannot connect: %s.", strerror(errno)); 470 471 /* 472 * Ok, got both sockets, time to create provider. 473 */ 474 memset(&ggioc, 0, sizeof(ggioc)); 475 ggioc.gctl_version = G_GATE_VERSION; 476 ggioc.gctl_mediasize = mediasize; 477 ggioc.gctl_sectorsize = sectorsize; 478 ggioc.gctl_flags = flags; 479 ggioc.gctl_maxcount = queue_size; 480 ggioc.gctl_timeout = timeout; 481 ggioc.gctl_unit = unit; 482 snprintf(ggioc.gctl_info, sizeof(ggioc.gctl_info), "%s:%u %s", host, 483 port, path); 484 g_gate_ioctl(G_GATE_CMD_CREATE, &ggioc); 485 if (unit == -1) { 486 printf("%s%u\n", G_GATE_PROVIDER_NAME, ggioc.gctl_unit); 487 fflush(stdout); 488 } 489 unit = ggioc.gctl_unit; 490 491 mydaemon(); 492 g_gatec_loop(); 493 } 494 495 static void 496 g_gatec_rescue(void) 497 { 498 struct g_gate_ctl_cancel ggioc; 499 500 if (!g_gatec_connect()) 501 g_gate_xlog("Cannot connect: %s.", strerror(errno)); 502 503 ggioc.gctl_version = G_GATE_VERSION; 504 ggioc.gctl_unit = unit; 505 ggioc.gctl_seq = 0; 506 g_gate_ioctl(G_GATE_CMD_CANCEL, &ggioc); 507 508 mydaemon(); 509 g_gatec_loop(); 510 } 511 512 int 513 main(int argc, char *argv[]) 514 { 515 516 if (argc < 2) 517 usage(); 518 if (strcasecmp(argv[1], "create") == 0) 519 action = CREATE; 520 else if (strcasecmp(argv[1], "destroy") == 0) 521 action = DESTROY; 522 else if (strcasecmp(argv[1], "list") == 0) 523 action = LIST; 524 else if (strcasecmp(argv[1], "rescue") == 0) 525 action = RESCUE; 526 else 527 usage(); 528 argc -= 1; 529 argv += 1; 530 for (;;) { 531 int ch; 532 533 ch = getopt(argc, argv, "fno:p:q:R:S:s:t:u:v"); 534 if (ch == -1) 535 break; 536 switch (ch) { 537 case 'f': 538 if (action != DESTROY) 539 usage(); 540 force = 1; 541 break; 542 case 'n': 543 if (action != CREATE && action != RESCUE) 544 usage(); 545 nagle = 0; 546 break; 547 case 'o': 548 if (action != CREATE && action != RESCUE) 549 usage(); 550 if (strcasecmp("ro", optarg) == 0) 551 flags = G_GATE_FLAG_READONLY; 552 else if (strcasecmp("wo", optarg) == 0) 553 flags = G_GATE_FLAG_WRITEONLY; 554 else if (strcasecmp("rw", optarg) == 0) 555 flags = 0; 556 else { 557 errx(EXIT_FAILURE, 558 "Invalid argument for '-o' option."); 559 } 560 break; 561 case 'p': 562 if (action != CREATE && action != RESCUE) 563 usage(); 564 errno = 0; 565 port = strtoul(optarg, NULL, 10); 566 if (port == 0 && errno != 0) 567 errx(EXIT_FAILURE, "Invalid port."); 568 break; 569 case 'q': 570 if (action != CREATE) 571 usage(); 572 errno = 0; 573 queue_size = strtoul(optarg, NULL, 10); 574 if (queue_size == 0 && errno != 0) 575 errx(EXIT_FAILURE, "Invalid queue_size."); 576 break; 577 case 'R': 578 if (action != CREATE && action != RESCUE) 579 usage(); 580 errno = 0; 581 rcvbuf = strtoul(optarg, NULL, 10); 582 if (rcvbuf == 0 && errno != 0) 583 errx(EXIT_FAILURE, "Invalid rcvbuf."); 584 break; 585 case 'S': 586 if (action != CREATE && action != RESCUE) 587 usage(); 588 errno = 0; 589 sndbuf = strtoul(optarg, NULL, 10); 590 if (sndbuf == 0 && errno != 0) 591 errx(EXIT_FAILURE, "Invalid sndbuf."); 592 break; 593 case 's': 594 if (action != CREATE) 595 usage(); 596 errno = 0; 597 sectorsize = strtoul(optarg, NULL, 10); 598 if (sectorsize == 0 && errno != 0) 599 errx(EXIT_FAILURE, "Invalid sectorsize."); 600 break; 601 case 't': 602 if (action != CREATE) 603 usage(); 604 errno = 0; 605 timeout = strtoul(optarg, NULL, 10); 606 if (timeout == 0 && errno != 0) 607 errx(EXIT_FAILURE, "Invalid timeout."); 608 break; 609 case 'u': 610 errno = 0; 611 unit = strtol(optarg, NULL, 10); 612 if (unit == 0 && errno != 0) 613 errx(EXIT_FAILURE, "Invalid unit number."); 614 break; 615 case 'v': 616 if (action == DESTROY) 617 usage(); 618 g_gate_verbose++; 619 break; 620 default: 621 usage(); 622 } 623 } 624 argc -= optind; 625 argv += optind; 626 627 switch (action) { 628 case CREATE: 629 if (argc != 2) 630 usage(); 631 g_gate_load_module(); 632 g_gate_open_device(); 633 host = argv[0]; 634 path = argv[1]; 635 g_gatec_create(); 636 break; 637 case DESTROY: 638 if (unit == -1) { 639 fprintf(stderr, "Required unit number.\n"); 640 usage(); 641 } 642 g_gate_verbose = 1; 643 g_gate_open_device(); 644 g_gate_destroy(unit, force); 645 break; 646 case LIST: 647 g_gate_list(unit, g_gate_verbose); 648 break; 649 case RESCUE: 650 if (argc != 2) 651 usage(); 652 if (unit == -1) { 653 fprintf(stderr, "Required unit number.\n"); 654 usage(); 655 } 656 g_gate_open_device(); 657 host = argv[0]; 658 path = argv[1]; 659 g_gatec_rescue(); 660 break; 661 case UNSET: 662 default: 663 usage(); 664 } 665 g_gate_close_device(); 666 exit(EXIT_SUCCESS); 667 } 668