View | Details | Raw Unified | Return to bug 132798
Collapse All | Expand All

(-)b/ggatec/ggatec.c (-18 / +10 lines)
Lines 71-77 Link Here
71
static off_t mediasize;
71
static off_t mediasize;
72
static unsigned sectorsize = 0;
72
static unsigned sectorsize = 0;
73
static unsigned timeout = G_GATE_TIMEOUT;
73
static unsigned timeout = G_GATE_TIMEOUT;
74
static int sendfd, recvfd;
74
static int fd = -1;
75
static uint32_t token;
75
static uint32_t token;
76
static pthread_t sendtd, recvtd;
76
static pthread_t sendtd, recvtd;
77
static int reconnect;
77
static int reconnect;
Lines 183-189 Link Here
183
		hdr.gh_error = 0;
183
		hdr.gh_error = 0;
184
		g_gate_swap2n_hdr(&hdr);
184
		g_gate_swap2n_hdr(&hdr);
185
185
186
		data = g_gate_send(sendfd, &hdr, sizeof(hdr), MSG_NOSIGNAL);
186
		data = g_gate_send(fd, &hdr, sizeof(hdr), MSG_NOSIGNAL);
187
		g_gate_swap2h_hdr(&hdr);
187
		g_gate_swap2h_hdr(&hdr);
188
		g_gate_log(LOG_DEBUG, "Sent hdr packet (seq=%llu).", hdr.gh_seq);
188
		g_gate_log(LOG_DEBUG, "Sent hdr packet (seq=%llu).", hdr.gh_seq);
189
		send_seq = hdr.gh_seq;
189
		send_seq = hdr.gh_seq;
Lines 198-204 Link Here
198
		}
198
		}
199
199
200
		if (hdr.gh_cmd == GGATE_CMD_WRITE) {
200
		if (hdr.gh_cmd == GGATE_CMD_WRITE) {
201
			data = g_gate_send(sendfd, ggio.gctl_data,
201
			data = g_gate_send(fd, ggio.gctl_data,
202
			    ggio.gctl_length, MSG_NOSIGNAL);
202
			    ggio.gctl_length, MSG_NOSIGNAL);
203
			if (reconnect)
203
			if (reconnect)
204
				break;
204
				break;
Lines 231-237 Link Here
231
	ggio.gctl_data = buf;
231
	ggio.gctl_data = buf;
232
232
233
	for (;;) {
233
	for (;;) {
234
		data = g_gate_recv(recvfd, &hdr, sizeof(hdr), MSG_WAITALL);
234
		data = g_gate_recv(fd, &hdr, sizeof(hdr), MSG_WAITALL);
235
		if (reconnect)
235
		if (reconnect)
236
			break;
236
			break;
237
		g_gate_swap2h_hdr(&hdr);
237
		g_gate_swap2h_hdr(&hdr);
Lines 254-260 Link Here
254
254
255
		if (ggio.gctl_error == 0 && ggio.gctl_cmd == GGATE_CMD_READ) {
255
		if (ggio.gctl_error == 0 && ggio.gctl_cmd == GGATE_CMD_READ) {
256
g_gate_log(LOG_DEBUG, "Received READ packet.");
256
g_gate_log(LOG_DEBUG, "Received READ packet.");
257
			data = g_gate_recv(recvfd, ggio.gctl_data,
257
			data = g_gate_recv(fd, ggio.gctl_data,
258
			    ggio.gctl_length, MSG_WAITALL);
258
			    ggio.gctl_length, MSG_WAITALL);
259
			if (reconnect)
259
			if (reconnect)
260
				break;
260
				break;
Lines 277-283 Link Here
277
}
277
}
278
278
279
static int
279
static int
280
handshake(int dir)
280
handshake(void)
281
{
281
{
282
	struct g_gate_version ver;
282
	struct g_gate_version ver;
283
	struct g_gate_cinit cinit;
283
	struct g_gate_cinit cinit;
Lines 367-373 Link Here
367
		close(sfd);
367
		close(sfd);
368
		return (-1);
368
		return (-1);
369
	}
369
	}
370
	cinit.gc_flags = flags | dir;
370
	cinit.gc_flags = flags;
371
	cinit.gc_token = token;
371
	cinit.gc_token = token;
372
	cinit.gc_nconn = 2;
372
	cinit.gc_nconn = 2;
373
	g_gate_swap2n_cinit(&cinit);
373
	g_gate_swap2n_cinit(&cinit);
Lines 426-440 Link Here
426
	 * Our receive descriptor is connected to the send descriptor on the
426
	 * Our receive descriptor is connected to the send descriptor on the
427
	 * server side.
427
	 * server side.
428
	 */
428
	 */
429
	recvfd = handshake(GGATE_FLAG_SEND);
429
	fd = handshake();
430
	if (recvfd == -1)
430
	if (fd == -1)
431
		return (0);
432
	/*
433
	 * Our send descriptor is connected to the receive descriptor on the
434
	 * server side.
435
	 */
436
	sendfd = handshake(GGATE_FLAG_RECV);
437
	if (sendfd == -1)
438
		return (0);
431
		return (0);
439
	return (1);
432
	return (1);
440
}
433
}
Lines 454-461 Link Here
454
	sendtd = pthread_self();
447
	sendtd = pthread_self();
455
	send_thread(NULL);
448
	send_thread(NULL);
456
	/* Disconnected. */
449
	/* Disconnected. */
457
	close(sendfd);
450
	close(fd);
458
	close(recvfd);
459
}
451
}
460
452
461
static void
453
static void
(-)b/ggated/ggated.c (-93 / +30 lines)
Lines 65-72 Link Here
65
	unsigned	 c_sectorsize;
65
	unsigned	 c_sectorsize;
66
	unsigned	 c_flags;	/* flags (RO/RW) */
66
	unsigned	 c_flags;	/* flags (RO/RW) */
67
	int		 c_diskfd;
67
	int		 c_diskfd;
68
	int		 c_sendfd;
68
	int		 c_netfd;
69
	int		 c_recvfd;
70
	time_t		 c_birthtime;
69
	time_t		 c_birthtime;
71
	char		*c_path;
70
	char		*c_path;
72
	uint64_t	 c_token;
71
	uint64_t	 c_token;
Lines 138-147 Link Here
138
	/* Use snprintf(3) so that we don't reenter stdio(3). */
137
	/* Use snprintf(3) so that we don't reenter stdio(3). */
139
	LIST_FOREACH_SAFE(conn, &connections, c_next, tconn) {
138
	LIST_FOREACH_SAFE(conn, &connections, c_next, tconn) {
140
		snprintf(buf, sizeof(buf),
139
		snprintf(buf, sizeof(buf),
140
			"token %llu\n"
141
			"path %s\n"
141
			"path %s\n"
142
			"send_seq %llu\n"
142
			"send_seq %llu\n"
143
			"recv_seq %llu\n"
143
			"recv_seq %llu\n"
144
			"",
144
			"",
145
			conn->c_token,
145
			conn->c_path,
146
			conn->c_path,
146
			conn->c_send_seq, conn->c_recv_seq);
147
			conn->c_send_seq, conn->c_recv_seq);
147
		write(STDERR_FILENO, buf, strlen(buf));
148
		write(STDERR_FILENO, buf, strlen(buf));
Lines 527-550 Link Here
527
			    ip2str((struct sockaddr*)&conn->c_srcaddr),
528
			    ip2str((struct sockaddr*)&conn->c_srcaddr),
528
			    conn->c_path);
529
			    conn->c_path);
529
			close(conn->c_diskfd);
530
			close(conn->c_diskfd);
530
			close(conn->c_sendfd);
531
			close(conn->c_netfd);
531
			close(conn->c_recvfd);
532
			free(conn->c_path);
532
			free(conn->c_path);
533
			free(conn);
533
			free(conn);
534
		}
534
		}
535
	}
535
	}
536
}
537
538
static struct ggd_connection *
539
connection_find(struct g_gate_cinit *cinit)
540
{
541
	struct ggd_connection *conn;
542
543
	LIST_FOREACH(conn, &connections, c_next) {
544
		if (conn->c_token == cinit->gc_token)
545
			break;
546
	}
547
	return (conn);
548
}
536
}
549
537
550
static struct ggd_connection *
538
static struct ggd_connection *
Lines 570-580 Link Here
570
	}
558
	}
571
	conn->c_token = cinit->gc_token;
559
	conn->c_token = cinit->gc_token;
572
	memcpy(&conn->c_srcaddr, s, s->sa_len);
560
	memcpy(&conn->c_srcaddr, s, s->sa_len);
573
	conn->c_sendfd = conn->c_recvfd = -1;
561
	conn->c_diskfd = -1;
574
	if ((cinit->gc_flags & GGATE_FLAG_SEND) != 0)
562
	conn->c_netfd = sfd;
575
		conn->c_sendfd = sfd;
576
	else
577
		conn->c_recvfd = sfd;
578
	conn->c_mediasize = 0;
563
	conn->c_mediasize = 0;
579
	conn->c_sectorsize = 0;
564
	conn->c_sectorsize = 0;
580
	time(&conn->c_birthtime);
565
	time(&conn->c_birthtime);
Lines 583-614 Link Here
583
	g_gate_log(LOG_DEBUG, "Connection created [%s, %s].", ip2str(s),
568
	g_gate_log(LOG_DEBUG, "Connection created [%s, %s].", ip2str(s),
584
	    conn->c_path);
569
	    conn->c_path);
585
	return (conn);
570
	return (conn);
586
}
587
588
static int
589
connection_add(struct ggd_connection *conn, struct g_gate_cinit *cinit,
590
    struct sockaddr *s, int sfd)
591
{
592
	if ((cinit->gc_flags & GGATE_FLAG_SEND) != 0) {
593
		if (conn->c_sendfd != -1) {
594
			g_gate_log(LOG_WARNING,
595
			    "Send socket already exists [%s, %s].", ip2str(s),
596
			    conn->c_path);
597
			return (EEXIST);
598
		}
599
		conn->c_sendfd = sfd;
600
	} else {
601
		if (conn->c_recvfd != -1) {
602
			g_gate_log(LOG_WARNING,
603
			    "Receive socket already exists [%s, %s].",
604
			    ip2str(s), conn->c_path);
605
			return (EEXIST);
606
		}
607
		conn->c_recvfd = sfd;
608
	}
609
	g_gate_log(LOG_DEBUG, "Connection added [%s, %s].", ip2str(s),
610
	    conn->c_path);
611
	return (0);
612
}
571
}
613
572
614
/*
573
/*
Lines 622-641 Link Here
622
	LIST_REMOVE(conn, c_next);
581
	LIST_REMOVE(conn, c_next);
623
	g_gate_log(LOG_DEBUG, "Connection removed [%s %s].",
582
	g_gate_log(LOG_DEBUG, "Connection removed [%s %s].",
624
	    ip2str((struct sockaddr*)&conn->c_srcaddr), conn->c_path);
583
	    ip2str((struct sockaddr*)&conn->c_srcaddr), conn->c_path);
625
	if (conn->c_sendfd != -1)
584
	close(conn->c_netfd);
626
		close(conn->c_sendfd);
585
	if(conn->c_diskfd == -1)
627
	if (conn->c_recvfd != -1)
586
		close(conn->c_diskfd);
628
		close(conn->c_recvfd);
629
	close(conn->c_diskfd);
630
	free(conn->c_path);
587
	free(conn->c_path);
631
	free(conn);
588
	free(conn);
632
}
633
634
static int
635
connection_ready(struct ggd_connection *conn)
636
{
637
638
	return (conn->c_sendfd != -1 && conn->c_recvfd != -1);
639
}
589
}
640
590
641
static void
591
static void
Lines 742-748 Link Here
742
692
743
	conn = arg;
693
	conn = arg;
744
	g_gate_log(LOG_NOTICE, "%s: started [%s]!", __func__, conn->c_path);
694
	g_gate_log(LOG_NOTICE, "%s: started [%s]!", __func__, conn->c_path);
745
	fd = conn->c_recvfd;
695
	fd = conn->c_netfd;
746
	for (;;) {
696
	for (;;) {
747
		/*
697
		/*
748
		 * Get header packet.
698
		 * Get header packet.
Lines 886-892 Link Here
886
836
887
	conn = arg;
837
	conn = arg;
888
	g_gate_log(LOG_NOTICE, "%s: started [%s]!", __func__, conn->c_path);
838
	g_gate_log(LOG_NOTICE, "%s: started [%s]!", __func__, conn->c_path);
889
	fd = conn->c_sendfd;
839
	fd = conn->c_netfd;
890
	for (;;) {
840
	for (;;) {
891
		/*
841
		/*
892
		 * Get a request from the outgoing queue.
842
		 * Get a request from the outgoing queue.
Lines 937-942 Link Here
937
	g_gate_log(LOG_INFO, "Connection from: %s.", ip2str(from));
887
	g_gate_log(LOG_INFO, "Connection from: %s.", ip2str(from));
938
}
888
}
939
889
890
/*
891
 * handshake return 1 if sfd is closed; otherwise 0 so that caller has to close.
892
 */
940
static int
893
static int
941
handshake(struct sockaddr *from, int sfd)
894
handshake(struct sockaddr *from, int sfd)
942
{
895
{
Lines 989-1024 Link Here
989
		return (0);
942
		return (0);
990
	}
943
	}
991
	g_gate_log(LOG_DEBUG, "Initial packet received.");
944
	g_gate_log(LOG_DEBUG, "Initial packet received.");
992
	conn = connection_find(&cinit);
945
993
	if (conn != NULL) {
946
	/*
994
		/*
947
	 * New connection, allocate space.
995
		 * Connection should already exists.
948
	 */
996
		 */
949
	conn = connection_new(&cinit, from, sfd);
997
		g_gate_log(LOG_DEBUG, "Found existing connection (token=%lu).",
950
	if (conn == NULL) {
998
		    (unsigned long)conn->c_token);
951
		sendfail(sfd, ENOMEM,
999
		if (connection_add(conn, &cinit, from, sfd) == -1) {
952
		    "Cannot allocate new connection.");
1000
			connection_remove(conn);
953
		return (0);
1001
			return (0);
1002
		}
1003
	} else {
1004
		/*
1005
		 * New connection, allocate space.
1006
		 */
1007
		conn = connection_new(&cinit, from, sfd);
1008
		if (conn == NULL) {
1009
			sendfail(sfd, ENOMEM,
1010
			    "Cannot allocate new connection.");
1011
			return (0);
1012
		}
1013
		g_gate_log(LOG_DEBUG, "New connection created (token=%lu).",
1014
		    (unsigned long)conn->c_token);
1015
	}
954
	}
955
	g_gate_log(LOG_DEBUG, "New connection created (token=%lu).",
956
	    (unsigned long)conn->c_token);
1016
957
1017
	ex = exports_find(from, &cinit, conn);
958
	ex = exports_find(from, &cinit, conn);
1018
	if (ex == NULL) {
959
	if (ex == NULL) {
1019
		connection_remove(conn);
1020
		sendfail(sfd, errno, NULL);
960
		sendfail(sfd, errno, NULL);
1021
		return (0);
961
		goto out;
1022
	}
962
	}
1023
	if (conn->c_mediasize == 0) {
963
	if (conn->c_mediasize == 0) {
1024
		conn->c_mediasize = g_gate_mediasize(conn->c_diskfd);
964
		conn->c_mediasize = g_gate_mediasize(conn->c_diskfd);
Lines 1033-1048 Link Here
1033
	g_gate_swap2n_sinit(&sinit);
973
	g_gate_swap2n_sinit(&sinit);
1034
	data = g_gate_send(sfd, &sinit, sizeof(sinit), 0);
974
	data = g_gate_send(sfd, &sinit, sizeof(sinit), 0);
1035
	g_gate_swap2h_sinit(&sinit);
975
	g_gate_swap2h_sinit(&sinit);
1036
	if (data == -1) {
976
	if (data == -1)
1037
		sendfail(sfd, errno, "Error while sending initial packet: %s.",
977
		sendfail(sfd, errno, "Error while sending initial packet: %s.",
1038
		    strerror(errno));
978
		    strerror(errno));
1039
		return (0);
979
	else
1040
	}
1041
1042
	if (connection_ready(conn)) {
1043
		connection_launch(conn);
980
		connection_launch(conn);
1044
		connection_remove(conn);
981
out:
1045
	}
982
	connection_remove(conn);
1046
	return (1);
983
	return (1);
1047
}
984
}
1048
985
(-)b/shared/ggate.h (-9 / +1 lines)
Lines 41-58 Link Here
41
#define	G_GATE_TIMEOUT		0
41
#define	G_GATE_TIMEOUT		0
42
42
43
#define	GGATE_MAGIC		"GEOM_GATE       "
43
#define	GGATE_MAGIC		"GEOM_GATE       "
44
#define	GGATE_VERSION		0
44
#define	GGATE_VERSION		1
45
45
46
#define	GGATE_FLAG_RDONLY	0x0001
46
#define	GGATE_FLAG_RDONLY	0x0001
47
#define	GGATE_FLAG_WRONLY	0x0002
47
#define	GGATE_FLAG_WRONLY	0x0002
48
/*
49
 * If GGATE_FLAG_SEND not GGATE_FLAG_RECV flag is set, this is initial
50
 * connection.
51
 * If GGATE_FLAG_SEND flag is set - this is socket to send data.
52
 * If GGATE_FLAG_RECV flag is set - this is socket to receive data.
53
 */
54
#define	GGATE_FLAG_SEND		0x0004
55
#define	GGATE_FLAG_RECV		0x0008
56
48
57
#define	GGATE_CMD_READ		0
49
#define	GGATE_CMD_READ		0
58
#define	GGATE_CMD_WRITE		1
50
#define	GGATE_CMD_WRITE		1

Return to bug 132798