| 1 |
/* |
| 2 |
* Copyright (C) 2006 by Latchesar Ionkov <[email protected]> |
| 3 |
* Copyright (C) 2010-2014 by Lawrence Livermore National Security, LLC |
| 4 |
* |
| 5 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 6 |
* copy of this software and associated documentation files (the "Software"), |
| 7 |
* to deal in the Software without restriction, including without limitation |
| 8 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 9 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 10 |
* Software is furnished to do so, subject to the following conditions: |
| 11 |
* |
| 12 |
* The above copyright notice and this permission notice (including the next |
| 13 |
* paragraph) shall be included in all copies or substantial portions of the |
| 14 |
* Software. |
| 15 |
* |
| 16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 |
* PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 20 |
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 21 |
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 22 |
* DEALINGS IN THE SOFTWARE. |
| 23 |
*/ |
| 24 |
|
| 25 |
/* mtfsys.c - pretty much Lucho's original fsys.c */ |
| 26 |
|
| 27 |
#if HAVE_CONFIG_H |
| 28 |
#include "config.h" |
| 29 |
#endif |
| 30 |
#include <stdlib.h> |
| 31 |
#include <stdio.h> |
| 32 |
#include <string.h> |
| 33 |
#include <unistd.h> |
| 34 |
#include <stdarg.h> |
| 35 |
#include <pthread.h> |
| 36 |
#include <sys/socket.h> |
| 37 |
#include <errno.h> |
| 38 |
#include <stdint.h> |
| 39 |
#include <inttypes.h> |
| 40 |
|
| 41 |
#include "9p.h" |
| 42 |
#include "npfs.h" |
| 43 |
#include "npclient.h" |
| 44 |
#include "xpthread.h" |
| 45 |
#include "npcimpl.h" |
| 46 |
|
| 47 |
typedef struct Npcrpc Npcrpc; |
| 48 |
struct Npcrpc { |
| 49 |
pthread_mutex_t lock; |
| 50 |
pthread_cond_t cond; |
| 51 |
u32 ecode; |
| 52 |
Npfcall* tc; |
| 53 |
Npfcall* rc; |
| 54 |
}; |
| 55 |
|
| 56 |
static Npcreq *npc_reqalloc(); |
| 57 |
static void npc_reqfree(Npcreq *req); |
| 58 |
static void *npc_read_proc(void *a); |
| 59 |
static void *npc_write_proc(void *a); |
| 60 |
static void npc_incref_fsys(Npcfsys *fs); |
| 61 |
static void npc_decref_fsys(Npcfsys *fs); |
| 62 |
static int npc_rpc(Npcfsys *fs, Npfcall *tc, Npfcall **rc); |
| 63 |
static void npc_disconnect_fsys(Npcfsys *fs); |
| 64 |
|
| 65 |
Npcfsys * |
| 66 |
npc_create_mtfsys(int rfd, int wfd, int msize, int flags) |
| 67 |
{ |
| 68 |
Npcfsys *fs; |
| 69 |
int err; |
| 70 |
|
| 71 |
fs = malloc(sizeof(*fs)); |
| 72 |
if (!fs) { |
| 73 |
np_uerror(ENOMEM); |
| 74 |
return NULL; |
| 75 |
} |
| 76 |
|
| 77 |
np_uerror (0); |
| 78 |
pthread_mutex_init(&fs->lock, NULL); |
| 79 |
pthread_cond_init(&fs->cond, NULL); |
| 80 |
fs->msize = msize; |
| 81 |
fs->trans = NULL; |
| 82 |
fs->tagpool = NULL; |
| 83 |
fs->fidpool = NULL; |
| 84 |
fs->unsent_first = NULL; |
| 85 |
fs->unsent_last = NULL; |
| 86 |
fs->pend_first = NULL; |
| 87 |
fs->readproc = 0; |
| 88 |
fs->writeproc = 0; |
| 89 |
fs->refcount = 1; |
| 90 |
fs->rfd = rfd; |
| 91 |
fs->wfd = wfd; |
| 92 |
fs->rpc = npc_rpc; |
| 93 |
fs->incref = npc_incref_fsys; |
| 94 |
fs->decref = npc_decref_fsys; |
| 95 |
fs->disconnect = npc_disconnect_fsys; |
| 96 |
fs->flags = flags; |
| 97 |
|
| 98 |
fs->trans = np_fdtrans_create(rfd, wfd); |
| 99 |
if (!fs->trans) |
| 100 |
goto error; |
| 101 |
fs->tagpool = npc_create_pool(P9_NOTAG); |
| 102 |
if (!fs->tagpool) |
| 103 |
goto error; |
| 104 |
fs->fidpool = npc_create_pool(P9_NOFID); |
| 105 |
if (!fs->fidpool) |
| 106 |
goto error; |
| 107 |
|
| 108 |
err = pthread_create(&fs->readproc, NULL, npc_read_proc, fs); |
| 109 |
if (err) { |
| 110 |
np_uerror (err); |
| 111 |
goto error; |
| 112 |
} |
| 113 |
err = pthread_create(&fs->writeproc, NULL, npc_write_proc, fs); |
| 114 |
if (err) { |
| 115 |
np_uerror (err); |
| 116 |
goto error; |
| 117 |
} |
| 118 |
|
| 119 |
return fs; |
| 120 |
|
| 121 |
error: |
| 122 |
npc_disconnect_fsys(fs); |
| 123 |
npc_decref_fsys(fs); |
| 124 |
return NULL; |
| 125 |
} |
| 126 |
|
| 127 |
static void |
| 128 |
npc_disconnect_fsys(Npcfsys *fs) |
| 129 |
{ |
| 130 |
void *v; |
| 131 |
|
| 132 |
xpthread_mutex_lock(&fs->lock); |
| 133 |
if (fs->wfd >= 0) { |
| 134 |
shutdown(fs->wfd, 2); |
| 135 |
close(fs->wfd); |
| 136 |
fs->wfd = -1; |
| 137 |
} |
| 138 |
xpthread_mutex_unlock(&fs->lock); |
| 139 |
|
| 140 |
if (fs->readproc) |
| 141 |
pthread_join(fs->readproc, &v); |
| 142 |
|
| 143 |
pthread_cond_broadcast(&fs->cond); |
| 144 |
if (fs->writeproc) |
| 145 |
pthread_join(fs->writeproc, &v); |
| 146 |
|
| 147 |
xpthread_mutex_lock(&fs->lock); |
| 148 |
if (fs->trans) { |
| 149 |
np_trans_destroy(fs->trans); |
| 150 |
fs->trans = NULL; |
| 151 |
} |
| 152 |
xpthread_mutex_unlock(&fs->lock); |
| 153 |
} |
| 154 |
|
| 155 |
static void |
| 156 |
npc_incref_fsys(Npcfsys *fs) |
| 157 |
{ |
| 158 |
xpthread_mutex_lock(&fs->lock); |
| 159 |
fs->refcount++; |
| 160 |
xpthread_mutex_unlock(&fs->lock); |
| 161 |
} |
| 162 |
|
| 163 |
static void |
| 164 |
npc_decref_fsys(Npcfsys *fs) |
| 165 |
{ |
| 166 |
xpthread_mutex_lock(&fs->lock); |
| 167 |
fs->refcount--; |
| 168 |
if (fs->refcount) { |
| 169 |
xpthread_mutex_unlock(&fs->lock); |
| 170 |
return; |
| 171 |
} |
| 172 |
|
| 173 |
NP_ASSERT(fs->wfd<0 && fs->trans==NULL); |
| 174 |
if (fs->tagpool) { |
| 175 |
npc_destroy_pool(fs->tagpool); |
| 176 |
fs->tagpool = NULL; |
| 177 |
} |
| 178 |
|
| 179 |
if (fs->fidpool) { |
| 180 |
npc_destroy_pool(fs->fidpool); |
| 181 |
fs->fidpool = NULL; |
| 182 |
} |
| 183 |
xpthread_mutex_unlock(&fs->lock); |
| 184 |
|
| 185 |
pthread_mutex_destroy(&fs->lock); |
| 186 |
pthread_cond_destroy(&fs->cond); |
| 187 |
|
| 188 |
free(fs); |
| 189 |
} |
| 190 |
|
| 191 |
#if 0 |
| 192 |
int |
| 193 |
npc_cancel_fid_requests(Npcfid *fid) |
| 194 |
{ |
| 195 |
int i, n; |
| 196 |
u16 *ftags; |
| 197 |
Npfcall *tc, *rc; |
| 198 |
Npcfsys *fs; |
| 199 |
Npcreq *ureqs, *req, *req1, *preq; |
| 200 |
|
| 201 |
fs = fid->fsys; |
| 202 |
xpthread_mutex_lock(&fs->lock); |
| 203 |
ureqs = NULL; |
| 204 |
req = fs->unsent_first; |
| 205 |
while (req != NULL) { |
| 206 |
req1 = req->next; |
| 207 |
if (req->tc->fid == fid->fid) { |
| 208 |
if (req->prev) |
| 209 |
req->prev->next = req->next; |
| 210 |
if (req->next) |
| 211 |
req->next->prev = req->prev; |
| 212 |
if (req == fs->unsent_last) |
| 213 |
fs->unsent_last = req->prev; |
| 214 |
if (req == fs->unsent_first); |
| 215 |
fs->unsent_first = req->next; |
| 216 |
|
| 217 |
req->next = ureqs; |
| 218 |
ureqs = req; |
| 219 |
} |
| 220 |
req = req1; |
| 221 |
} |
| 222 |
|
| 223 |
for(n = 0, req = fs->pend_first; req != NULL; req = req->next) |
| 224 |
if (req->tc->fid == fid->fid) { |
| 225 |
req->flushed = 1; |
| 226 |
n++; |
| 227 |
} |
| 228 |
|
| 229 |
ftags = malloc(n * sizeof(u16)); |
| 230 |
if (!ftags) { |
| 231 |
np_uerror(ENOMEM); |
| 232 |
xpthread_mutex_unlock(&fs->lock); |
| 233 |
return -1; |
| 234 |
} |
| 235 |
|
| 236 |
for(n = 0, req = fs->pend_first; req != NULL; req = req->next) |
| 237 |
if (req->tc->fid == fid->fid) |
| 238 |
ftags[n++] = req->tag; |
| 239 |
xpthread_mutex_unlock(&fs->lock); |
| 240 |
|
| 241 |
/* report error on the unsent requests */ |
| 242 |
req = ureqs; |
| 243 |
while (req != NULL) { |
| 244 |
req1 = req->next; |
| 245 |
req->ecode = EIO; |
| 246 |
(*req->cb)(req, req->cba); |
| 247 |
npc_put_id(fs->tagpool, req->tag); |
| 248 |
npc_reqfree(req); |
| 249 |
req = req1; |
| 250 |
} |
| 251 |
|
| 252 |
/* flush the pending ones */ |
| 253 |
for(i = 0; i < n; i++) { |
| 254 |
tc = np_create_tflush(ftags[i]); |
| 255 |
if (npc_rpc(fs, tc, &rc) > 0) { |
| 256 |
/* if the request didn't receive a response, reply */ |
| 257 |
xpthread_mutex_lock(&fs->lock); |
| 258 |
for(preq = NULL, req = fs->pend_first; req != NULL; preq = req, req = req->next) { |
| 259 |
if (req->tag == ftags[i]) { |
| 260 |
if (preq) |
| 261 |
preq->next = req->next; |
| 262 |
else |
| 263 |
fs->pend_first = req->next; |
| 264 |
break; |
| 265 |
} |
| 266 |
} |
| 267 |
xpthread_mutex_unlock(&fs->lock); |
| 268 |
|
| 269 |
if (req) { |
| 270 |
req->ecode = EIO; |
| 271 |
if (req->cb) |
| 272 |
(*req->cb)(req, req->cba); |
| 273 |
npc_reqfree(req); |
| 274 |
} |
| 275 |
|
| 276 |
npc_put_id(fs->tagpool, ftags[i]); |
| 277 |
} |
| 278 |
|
| 279 |
free(tc); |
| 280 |
free(rc); |
| 281 |
} |
| 282 |
free(ftags); |
| 283 |
|
| 284 |
return 0; |
| 285 |
} |
| 286 |
#endif |
| 287 |
|
| 288 |
static void* |
| 289 |
npc_read_proc(void *a) |
| 290 |
{ |
| 291 |
Npcfsys *fs = (Npcfsys *)a; |
| 292 |
Npfcall *fc = NULL; |
| 293 |
Npcreq *req, *req1, *unsent, *pend, *preq; |
| 294 |
|
| 295 |
while (fs->trans) { |
| 296 |
if (np_trans_recv (fs->trans, &fc, fs->msize) < 0) |
| 297 |
break; |
| 298 |
if (fc == NULL) |
| 299 |
break; |
| 300 |
|
| 301 |
xpthread_mutex_lock(&fs->lock); |
| 302 |
for(preq = NULL, req = fs->pend_first; |
| 303 |
req != NULL; preq = req, req = req->next) { |
| 304 |
if (req->tag == fc->tag) { |
| 305 |
if (preq) |
| 306 |
preq->next = req->next; |
| 307 |
else |
| 308 |
fs->pend_first = req->next; |
| 309 |
|
| 310 |
xpthread_mutex_unlock(&fs->lock); |
| 311 |
req->rc = fc; |
| 312 |
if (fc->type == P9_RLERROR) { |
| 313 |
req->ecode = fc->u.rlerror.ecode; |
| 314 |
} else if (fc->type != req->tc->type+1) { |
| 315 |
req->ecode = EIO; |
| 316 |
} |
| 317 |
fc = NULL; |
| 318 |
if (req->cb) |
| 319 |
(*req->cb)(req, req->cba); |
| 320 |
|
| 321 |
if (!req->flushed) |
| 322 |
npc_put_id(fs->tagpool, req->tag); |
| 323 |
npc_reqfree(req); |
| 324 |
break; |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
if (!req) { |
| 329 |
xpthread_mutex_unlock(&fs->lock); |
| 330 |
free(fc); |
| 331 |
fc = NULL; |
| 332 |
} |
| 333 |
} |
| 334 |
if (fc) |
| 335 |
free(fc); |
| 336 |
xpthread_mutex_lock(&fs->lock); |
| 337 |
unsent = fs->unsent_first; |
| 338 |
fs->unsent_first = NULL; |
| 339 |
pend = fs->pend_first; |
| 340 |
fs->pend_first = NULL; |
| 341 |
if (fs->trans) |
| 342 |
np_trans_destroy(fs->trans); |
| 343 |
fs->trans = NULL; |
| 344 |
xpthread_mutex_unlock(&fs->lock); |
| 345 |
|
| 346 |
req = unsent; |
| 347 |
while (req != NULL) { |
| 348 |
req->ecode = ECONNABORTED; |
| 349 |
req1 = req->next; |
| 350 |
|
| 351 |
if (req->cb) |
| 352 |
(*req->cb)(req, req->cba); |
| 353 |
|
| 354 |
npc_reqfree(req); |
| 355 |
req = req1; |
| 356 |
} |
| 357 |
|
| 358 |
req = pend; |
| 359 |
while (req != NULL) { |
| 360 |
req->ecode = ECONNABORTED; |
| 361 |
req1 = req->next; |
| 362 |
|
| 363 |
if (req->cb) |
| 364 |
(*req->cb)(req, req->cba); |
| 365 |
|
| 366 |
npc_reqfree(req); |
| 367 |
req = req1; |
| 368 |
} |
| 369 |
|
| 370 |
return NULL; |
| 371 |
} |
| 372 |
|
| 373 |
static void* |
| 374 |
npc_write_proc(void *a) |
| 375 |
{ |
| 376 |
int n; |
| 377 |
Npcreq *req; |
| 378 |
Npcfsys *fs; |
| 379 |
|
| 380 |
fs = a; |
| 381 |
xpthread_mutex_lock(&fs->lock); |
| 382 |
while (fs->trans) { |
| 383 |
if (!fs->unsent_first) { |
| 384 |
pthread_cond_wait(&fs->cond, &fs->lock); |
| 385 |
continue; |
| 386 |
} |
| 387 |
|
| 388 |
req = fs->unsent_first; |
| 389 |
fs->unsent_first = req->next; |
| 390 |
req->prev = NULL; |
| 391 |
if (fs->unsent_last == req) |
| 392 |
fs->unsent_last = NULL; |
| 393 |
|
| 394 |
req->prev = NULL; |
| 395 |
req->next = fs->pend_first; |
| 396 |
if (fs->pend_first) |
| 397 |
fs->pend_first->prev = req; |
| 398 |
|
| 399 |
fs->pend_first = req; |
| 400 |
xpthread_mutex_unlock(&fs->lock); |
| 401 |
|
| 402 |
if (fs->trans) { |
| 403 |
n = np_trans_send(fs->trans, req->tc); |
| 404 |
if (n < 0) { |
| 405 |
xpthread_mutex_lock(&fs->lock); |
| 406 |
if (fs->trans) |
| 407 |
np_trans_destroy(fs->trans); |
| 408 |
fs->trans = NULL; |
| 409 |
xpthread_mutex_unlock(&fs->lock); |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
xpthread_mutex_lock(&fs->lock); |
| 414 |
} |
| 415 |
|
| 416 |
xpthread_mutex_unlock(&fs->lock); |
| 417 |
return NULL; |
| 418 |
} |
| 419 |
|
| 420 |
static int |
| 421 |
npc_rpcnb(Npcfsys *fs, Npfcall *tc, void (*cb)(Npcreq *, void *), void *cba) |
| 422 |
{ |
| 423 |
Npcreq *req; |
| 424 |
|
| 425 |
if (!fs->trans) { |
| 426 |
np_uerror(ECONNABORTED); |
| 427 |
return -1; |
| 428 |
} |
| 429 |
|
| 430 |
req = npc_reqalloc(); |
| 431 |
if (!req) |
| 432 |
return -1; |
| 433 |
|
| 434 |
if (tc->type != P9_TVERSION) { |
| 435 |
tc->tag = npc_get_id(fs->tagpool); |
| 436 |
np_set_tag(tc, tc->tag); |
| 437 |
} |
| 438 |
|
| 439 |
req->tag = tc->tag; |
| 440 |
req->tc = tc; |
| 441 |
req->cb = cb; |
| 442 |
req->cba = cba; |
| 443 |
|
| 444 |
xpthread_mutex_lock(&fs->lock); |
| 445 |
if (fs->unsent_last) |
| 446 |
fs->unsent_last->next = req; |
| 447 |
req->prev = fs->unsent_last; |
| 448 |
fs->unsent_last = req; |
| 449 |
|
| 450 |
if (!fs->unsent_first) |
| 451 |
fs->unsent_first = req; |
| 452 |
|
| 453 |
pthread_cond_broadcast(&fs->cond); |
| 454 |
xpthread_mutex_unlock(&fs->lock); |
| 455 |
|
| 456 |
return 0; |
| 457 |
} |
| 458 |
|
| 459 |
static void |
| 460 |
npc_rpc_cb(Npcreq *req, void *cba) |
| 461 |
{ |
| 462 |
Npcrpc *r; |
| 463 |
|
| 464 |
r = cba; |
| 465 |
xpthread_mutex_lock(&r->lock); |
| 466 |
r->ecode = req->ecode; |
| 467 |
r->rc = req->rc; |
| 468 |
pthread_cond_broadcast(&r->cond); |
| 469 |
xpthread_mutex_unlock(&r->lock); |
| 470 |
} |
| 471 |
|
| 472 |
static int |
| 473 |
npc_rpc(Npcfsys *fs, Npfcall *tc, Npfcall **rc) |
| 474 |
{ |
| 475 |
int n; |
| 476 |
Npcrpc r; |
| 477 |
|
| 478 |
if (rc) |
| 479 |
*rc = NULL; |
| 480 |
|
| 481 |
r.ecode = 0; |
| 482 |
r.tc = tc; |
| 483 |
r.rc = NULL; |
| 484 |
pthread_mutex_init(&r.lock, NULL); |
| 485 |
pthread_cond_init(&r.cond, NULL); |
| 486 |
|
| 487 |
n = npc_rpcnb(fs, tc, npc_rpc_cb, &r); |
| 488 |
if (n < 0) |
| 489 |
return n; |
| 490 |
|
| 491 |
xpthread_mutex_lock(&r.lock); |
| 492 |
while (!r.ecode && !r.rc) |
| 493 |
pthread_cond_wait(&r.cond, &r.lock); |
| 494 |
xpthread_mutex_unlock(&r.lock); |
| 495 |
|
| 496 |
if (r.rc == NULL) { |
| 497 |
np_uerror (EPROTO); /* premature EOF */ |
| 498 |
return -1; |
| 499 |
} |
| 500 |
|
| 501 |
/* N.B. allow for auth returning error with ecode == 0 */ |
| 502 |
if (r.ecode || r.rc->type == P9_RLERROR) { |
| 503 |
np_uerror(r.ecode); |
| 504 |
free(r.rc); |
| 505 |
return -1; |
| 506 |
} |
| 507 |
|
| 508 |
if (rc) |
| 509 |
*rc = r.rc; |
| 510 |
else |
| 511 |
free(r.rc); |
| 512 |
|
| 513 |
return 0; |
| 514 |
} |
| 515 |
|
| 516 |
static Npcreq * |
| 517 |
npc_reqalloc() |
| 518 |
{ |
| 519 |
Npcreq *req; |
| 520 |
|
| 521 |
req = malloc(sizeof(*req)); |
| 522 |
if (!req) { |
| 523 |
np_uerror(ENOMEM); |
| 524 |
return NULL; |
| 525 |
} |
| 526 |
|
| 527 |
req->fsys = NULL; |
| 528 |
req->tag = P9_NOTAG; |
| 529 |
req->tc = NULL; |
| 530 |
req->rc = NULL; |
| 531 |
req->ecode = 0; |
| 532 |
req->cb = NULL; |
| 533 |
req->cba = NULL; |
| 534 |
req->next = NULL; |
| 535 |
req->prev = NULL; |
| 536 |
req->flushed = 0; |
| 537 |
|
| 538 |
return req; |
| 539 |
} |
| 540 |
|
| 541 |
static void |
| 542 |
npc_reqfree(Npcreq *req) |
| 543 |
{ |
| 544 |
free(req); |
| 545 |
} |