forked from conclusiveeng/lib9p
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.c
215 lines (184 loc) · 5.65 KB
/
connection.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
* Copyright 2016 Jakub Klama <jceel@FreeBSD.org>
* All rights reserved
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted providing that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <sys/queue.h>
#include "lib9p.h"
#include "lib9p_impl.h"
#include "fid.h"
#include "hashtable.h"
#include "log.h"
#include "threadpool.h"
#include "backend/backend.h"
int
l9p_server_init(struct l9p_server **serverp, struct l9p_backend *backend)
{
struct l9p_server *server;
server = l9p_calloc(1, sizeof (*server));
server->ls_max_version = L9P_2000L;
server->ls_backend = backend;
LIST_INIT(&server->ls_conns);
*serverp = server;
return (0);
}
int
l9p_connection_init(struct l9p_server *server, struct l9p_connection **conn)
{
struct l9p_connection *newconn;
assert(server != NULL);
assert(conn != NULL);
newconn = calloc(1, sizeof (*newconn));
if (newconn == NULL)
return (-1);
newconn->lc_server = server;
newconn->lc_msize = L9P_DEFAULT_MSIZE;
if (l9p_threadpool_init(&newconn->lc_tp, L9P_NUMTHREADS)) {
free(newconn);
return (-1);
}
ht_init(&newconn->lc_files, 100);
ht_init(&newconn->lc_requests, 100);
LIST_INSERT_HEAD(&server->ls_conns, newconn, lc_link);
*conn = newconn;
return (0);
}
void
l9p_connection_free(struct l9p_connection *conn)
{
LIST_REMOVE(conn, lc_link);
free(conn);
}
void
l9p_connection_recv(struct l9p_connection *conn, const struct iovec *iov,
const size_t niov, void *aux)
{
struct l9p_request *req;
int error;
req = l9p_calloc(1, sizeof (struct l9p_request));
req->lr_aux = aux;
req->lr_conn = conn;
req->lr_req_msg.lm_mode = L9P_UNPACK;
req->lr_req_msg.lm_niov = niov;
memcpy(req->lr_req_msg.lm_iov, iov, sizeof (struct iovec) * niov);
req->lr_resp_msg.lm_mode = L9P_PACK;
if (l9p_pufcall(&req->lr_req_msg, &req->lr_req, conn->lc_version) != 0) {
L9P_LOG(L9P_WARNING, "cannot unpack received message");
l9p_freefcall(&req->lr_req);
free(req);
return;
}
if (ht_add(&conn->lc_requests, req->lr_req.hdr.tag, req)) {
L9P_LOG(L9P_WARNING, "client reusing outstanding tag %d",
req->lr_req.hdr.tag);
l9p_freefcall(&req->lr_req);
free(req);
return;
}
error = conn->lc_lt.lt_get_response_buffer(req,
req->lr_resp_msg.lm_iov,
&req->lr_resp_msg.lm_niov,
conn->lc_lt.lt_aux);
if (error) {
L9P_LOG(L9P_WARNING, "cannot obtain buffers for response");
ht_remove(&conn->lc_requests, req->lr_req.hdr.tag);
l9p_freefcall(&req->lr_req);
free(req);
return;
}
/*
* NB: it's up to l9p_threadpool_run to decide whether
* to queue the work or to run it immediately and wait
* (it must do the latter for Tflush requests).
*/
l9p_threadpool_run(&conn->lc_tp, req);
}
void
l9p_connection_close(struct l9p_connection *conn)
{
struct ht_iter iter;
struct l9p_fid *fid;
struct l9p_request *req;
L9P_LOG(L9P_DEBUG, "waiting for thread pool to shut down");
l9p_threadpool_shutdown(&conn->lc_tp);
/* Drain pending requests (if any) */
L9P_LOG(L9P_DEBUG, "draining pending requests");
ht_iter(&conn->lc_requests, &iter);
while ((req = ht_next(&iter)) != NULL) {
#ifdef notyet
/* XXX would be good to know if there is anyone listening */
if (anyone listening) {
/* XXX crude - ops like Tclunk should succeed */
req->lr_error = EINTR;
l9p_respond(req, false, false);
} else
#endif
l9p_respond(req, true, false); /* use no-answer path */
ht_remove_at_iter(&iter);
}
/* Close opened files (if any) */
L9P_LOG(L9P_DEBUG, "closing opened files");
ht_iter(&conn->lc_files, &iter);
while ((fid = ht_next(&iter)) != NULL) {
conn->lc_server->ls_backend->freefid(
conn->lc_server->ls_backend->softc, fid);
free(fid);
ht_remove_at_iter(&iter);
}
ht_destroy(&conn->lc_requests);
ht_destroy(&conn->lc_files);
}
struct l9p_fid *
l9p_connection_alloc_fid(struct l9p_connection *conn, uint32_t fid)
{
struct l9p_fid *file;
file = l9p_calloc(1, sizeof (struct l9p_fid));
file->lo_fid = fid;
/*
* Note that the new fid is not marked valid yet.
* The insert here will fail if the fid number is
* in use, otherwise we have an invalid fid in the
* table (as desired).
*/
if (ht_add(&conn->lc_files, fid, file) != 0) {
free(file);
return (NULL);
}
return (file);
}
void
l9p_connection_remove_fid(struct l9p_connection *conn, struct l9p_fid *fid)
{
struct l9p_backend *be;
/* fid should be marked invalid by this point */
assert(!l9p_fid_isvalid(fid));
be = conn->lc_server->ls_backend;
be->freefid(be->softc, fid);
ht_remove(&conn->lc_files, fid->lo_fid);
free(fid);
}