client.c revision 45abdf1c7be80d6ec3b0b14e59ee75a0d5d9fb37
1/* 2 * net/9p/clnt.c 3 * 4 * 9P Client 5 * 6 * Copyright (C) 2008 by Eric Van Hensbergen <ericvh@gmail.com> 7 * Copyright (C) 2007 by Latchesar Ionkov <lucho@ionkov.net> 8 * 9 * This program is free software; you can redistribute it and/or modify 10 * it under the terms of the GNU General Public License version 2 11 * as published by the Free Software Foundation. 12 * 13 * This program is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU General Public License for more details. 17 * 18 * You should have received a copy of the GNU General Public License 19 * along with this program; if not, write to: 20 * Free Software Foundation 21 * 51 Franklin Street, Fifth Floor 22 * Boston, MA 02111-1301 USA 23 * 24 */ 25 26#include <linux/module.h> 27#include <linux/errno.h> 28#include <linux/fs.h> 29#include <linux/poll.h> 30#include <linux/idr.h> 31#include <linux/mutex.h> 32#include <linux/sched.h> 33#include <linux/uaccess.h> 34#include <net/9p/9p.h> 35#include <linux/parser.h> 36#include <net/9p/client.h> 37#include <net/9p/transport.h> 38#include "protocol.h" 39 40/* 41 * Client Option Parsing (code inspired by NFS code) 42 * - a little lazy - parse all client options 43 */ 44 45enum { 46 Opt_msize, 47 Opt_trans, 48 Opt_legacy, 49 Opt_err, 50}; 51 52static const match_table_t tokens = { 53 {Opt_msize, "msize=%u"}, 54 {Opt_legacy, "noextend"}, 55 {Opt_trans, "trans=%s"}, 56 {Opt_err, NULL}, 57}; 58 59static struct p9_req_t * 60p9_client_rpc(struct p9_client *c, int8_t type, const char *fmt, ...); 61 62/** 63 * v9fs_parse_options - parse mount options into session structure 64 * @options: options string passed from mount 65 * @v9ses: existing v9fs session information 66 * 67 * Return 0 upon success, -ERRNO upon failure 68 */ 69 70static int parse_opts(char *opts, struct p9_client *clnt) 71{ 72 char *options; 73 char *p; 74 substring_t args[MAX_OPT_ARGS]; 75 int option; 76 int ret = 0; 77 78 clnt->dotu = 1; 79 clnt->msize = 8192; 80 81 if (!opts) 82 return 0; 83 84 options = kstrdup(opts, GFP_KERNEL); 85 if (!options) { 86 P9_DPRINTK(P9_DEBUG_ERROR, 87 "failed to allocate copy of option string\n"); 88 return -ENOMEM; 89 } 90 91 while ((p = strsep(&options, ",")) != NULL) { 92 int token; 93 if (!*p) 94 continue; 95 token = match_token(p, tokens, args); 96 if (token < Opt_trans) { 97 int r = match_int(&args[0], &option); 98 if (r < 0) { 99 P9_DPRINTK(P9_DEBUG_ERROR, 100 "integer field, but no integer?\n"); 101 ret = r; 102 continue; 103 } 104 } 105 switch (token) { 106 case Opt_msize: 107 clnt->msize = option; 108 break; 109 case Opt_trans: 110 clnt->trans_mod = v9fs_get_trans_by_name(&args[0]); 111 break; 112 case Opt_legacy: 113 clnt->dotu = 0; 114 break; 115 default: 116 continue; 117 } 118 } 119 120 if (!clnt->trans_mod) 121 clnt->trans_mod = v9fs_get_default_trans(); 122 123 kfree(options); 124 return ret; 125} 126 127/** 128 * p9_tag_alloc - lookup/allocate a request by tag 129 * @c: client session to lookup tag within 130 * @tag: numeric id for transaction 131 * 132 * this is a simple array lookup, but will grow the 133 * request_slots as necessary to accomodate transaction 134 * ids which did not previously have a slot. 135 * 136 * this code relies on the client spinlock to manage locks, its 137 * possible we should switch to something else, but I'd rather 138 * stick with something low-overhead for the common case. 139 * 140 */ 141 142static struct p9_req_t *p9_tag_alloc(struct p9_client *c, u16 tag) 143{ 144 unsigned long flags; 145 int row, col; 146 struct p9_req_t *req; 147 148 /* This looks up the original request by tag so we know which 149 * buffer to read the data into */ 150 tag++; 151 152 if (tag >= c->max_tag) { 153 spin_lock_irqsave(&c->lock, flags); 154 /* check again since original check was outside of lock */ 155 while (tag >= c->max_tag) { 156 row = (tag / P9_ROW_MAXTAG); 157 c->reqs[row] = kcalloc(P9_ROW_MAXTAG, 158 sizeof(struct p9_req_t), GFP_ATOMIC); 159 160 if (!c->reqs[row]) { 161 printk(KERN_ERR "Couldn't grow tag array\n"); 162 spin_unlock_irqrestore(&c->lock, flags); 163 return ERR_PTR(-ENOMEM); 164 } 165 for (col = 0; col < P9_ROW_MAXTAG; col++) { 166 c->reqs[row][col].status = REQ_STATUS_IDLE; 167 c->reqs[row][col].tc = NULL; 168 } 169 c->max_tag += P9_ROW_MAXTAG; 170 } 171 spin_unlock_irqrestore(&c->lock, flags); 172 } 173 row = tag / P9_ROW_MAXTAG; 174 col = tag % P9_ROW_MAXTAG; 175 176 req = &c->reqs[row][col]; 177 if (!req->tc) { 178 req->wq = kmalloc(sizeof(wait_queue_head_t), GFP_KERNEL); 179 if (!req->wq) { 180 printk(KERN_ERR "Couldn't grow tag array\n"); 181 return ERR_PTR(-ENOMEM); 182 } 183 init_waitqueue_head(req->wq); 184 req->tc = kmalloc(sizeof(struct p9_fcall)+c->msize, 185 GFP_KERNEL); 186 req->rc = kmalloc(sizeof(struct p9_fcall)+c->msize, 187 GFP_KERNEL); 188 if ((!req->tc) || (!req->rc)) { 189 printk(KERN_ERR "Couldn't grow tag array\n"); 190 kfree(req->tc); 191 kfree(req->rc); 192 kfree(req->wq); 193 req->tc = req->rc = NULL; 194 req->wq = NULL; 195 return ERR_PTR(-ENOMEM); 196 } 197 req->tc->sdata = (char *) req->tc + sizeof(struct p9_fcall); 198 req->tc->capacity = c->msize; 199 req->rc->sdata = (char *) req->rc + sizeof(struct p9_fcall); 200 req->rc->capacity = c->msize; 201 } 202 203 p9pdu_reset(req->tc); 204 p9pdu_reset(req->rc); 205 206 req->flush_tag = 0; 207 req->tc->tag = tag-1; 208 req->status = REQ_STATUS_ALLOC; 209 210 return &c->reqs[row][col]; 211} 212 213/** 214 * p9_tag_lookup - lookup a request by tag 215 * @c: client session to lookup tag within 216 * @tag: numeric id for transaction 217 * 218 */ 219 220struct p9_req_t *p9_tag_lookup(struct p9_client *c, u16 tag) 221{ 222 int row, col; 223 224 /* This looks up the original request by tag so we know which 225 * buffer to read the data into */ 226 tag++; 227 228 BUG_ON(tag >= c->max_tag); 229 230 row = tag / P9_ROW_MAXTAG; 231 col = tag % P9_ROW_MAXTAG; 232 233 return &c->reqs[row][col]; 234} 235EXPORT_SYMBOL(p9_tag_lookup); 236 237/** 238 * p9_tag_init - setup tags structure and contents 239 * @tags: tags structure from the client struct 240 * 241 * This initializes the tags structure for each client instance. 242 * 243 */ 244 245static int p9_tag_init(struct p9_client *c) 246{ 247 int err = 0; 248 249 c->tagpool = p9_idpool_create(); 250 if (IS_ERR(c->tagpool)) { 251 err = PTR_ERR(c->tagpool); 252 c->tagpool = NULL; 253 goto error; 254 } 255 256 p9_idpool_get(c->tagpool); /* reserve tag 0 */ 257 258 c->max_tag = 0; 259error: 260 return err; 261} 262 263/** 264 * p9_tag_cleanup - cleans up tags structure and reclaims resources 265 * @tags: tags structure from the client struct 266 * 267 * This frees resources associated with the tags structure 268 * 269 */ 270static void p9_tag_cleanup(struct p9_client *c) 271{ 272 int row, col; 273 274 /* check to insure all requests are idle */ 275 for (row = 0; row < (c->max_tag/P9_ROW_MAXTAG); row++) { 276 for (col = 0; col < P9_ROW_MAXTAG; col++) { 277 if (c->reqs[row][col].status != REQ_STATUS_IDLE) { 278 P9_DPRINTK(P9_DEBUG_MUX, 279 "Attempting to cleanup non-free tag %d,%d\n", 280 row, col); 281 /* TODO: delay execution of cleanup */ 282 return; 283 } 284 } 285 } 286 287 if (c->tagpool) 288 p9_idpool_destroy(c->tagpool); 289 290 /* free requests associated with tags */ 291 for (row = 0; row < (c->max_tag/P9_ROW_MAXTAG); row++) { 292 for (col = 0; col < P9_ROW_MAXTAG; col++) { 293 kfree(c->reqs[row][col].wq); 294 kfree(c->reqs[row][col].tc); 295 kfree(c->reqs[row][col].rc); 296 } 297 kfree(c->reqs[row]); 298 } 299 c->max_tag = 0; 300} 301 302/** 303 * p9_free_req - free a request and clean-up as necessary 304 * c: client state 305 * r: request to release 306 * 307 */ 308 309static void p9_free_req(struct p9_client *c, struct p9_req_t *r) 310{ 311 int tag = r->tc->tag; 312 P9_DPRINTK(P9_DEBUG_MUX, "clnt %p req %p tag: %d\n", c, r, tag); 313 314 r->status = REQ_STATUS_IDLE; 315 if (tag != P9_NOTAG && p9_idpool_check(tag, c->tagpool)) 316 p9_idpool_put(tag, c->tagpool); 317} 318 319/** 320 * p9_client_cb - call back from transport to client 321 * c: client state 322 * req: request received 323 * 324 */ 325void p9_client_cb(struct p9_client *c, struct p9_req_t *req) 326{ 327 struct p9_req_t *other_req; 328 unsigned long flags; 329 330 P9_DPRINTK(P9_DEBUG_MUX, " tag %d\n", req->tc->tag); 331 332 if (req->status == REQ_STATUS_ERROR) 333 wake_up(req->wq); 334 335 if (req->flush_tag) { /* flush receive path */ 336 P9_DPRINTK(P9_DEBUG_9P, "<<< RFLUSH %d\n", req->tc->tag); 337 spin_lock_irqsave(&c->lock, flags); 338 other_req = p9_tag_lookup(c, req->flush_tag); 339 if (other_req->status != REQ_STATUS_FLSH) /* stale flush */ 340 spin_unlock_irqrestore(&c->lock, flags); 341 else { 342 other_req->status = REQ_STATUS_FLSHD; 343 spin_unlock_irqrestore(&c->lock, flags); 344 wake_up(other_req->wq); 345 } 346 p9_free_req(c, req); 347 } else { /* normal receive path */ 348 P9_DPRINTK(P9_DEBUG_MUX, "normal: tag %d\n", req->tc->tag); 349 spin_lock_irqsave(&c->lock, flags); 350 if (req->status != REQ_STATUS_FLSHD) 351 req->status = REQ_STATUS_RCVD; 352 spin_unlock_irqrestore(&c->lock, flags); 353 wake_up(req->wq); 354 P9_DPRINTK(P9_DEBUG_MUX, "wakeup: %d\n", req->tc->tag); 355 } 356} 357EXPORT_SYMBOL(p9_client_cb); 358 359/** 360 * p9_parse_header - parse header arguments out of a packet 361 * @pdu: packet to parse 362 * @size: size of packet 363 * @type: type of request 364 * @tag: tag of packet 365 * @rewind: set if we need to rewind offset afterwards 366 */ 367 368int 369p9_parse_header(struct p9_fcall *pdu, int32_t *size, int8_t *type, int16_t *tag, 370 int rewind) 371{ 372 int8_t r_type; 373 int16_t r_tag; 374 int32_t r_size; 375 int offset = pdu->offset; 376 int err; 377 378 pdu->offset = 0; 379 if (pdu->size == 0) 380 pdu->size = 7; 381 382 err = p9pdu_readf(pdu, 0, "dbw", &r_size, &r_type, &r_tag); 383 if (err) 384 goto rewind_and_exit; 385 386 pdu->size = r_size; 387 pdu->id = r_type; 388 pdu->tag = r_tag; 389 390 P9_DPRINTK(P9_DEBUG_9P, "<<< size=%d type: %d tag: %d\n", pdu->size, 391 pdu->id, pdu->tag); 392 393 if (type) 394 *type = r_type; 395 if (tag) 396 *tag = r_tag; 397 if (size) 398 *size = r_size; 399 400 401rewind_and_exit: 402 if (rewind) 403 pdu->offset = offset; 404 return err; 405} 406EXPORT_SYMBOL(p9_parse_header); 407 408/** 409 * p9_check_errors - check 9p packet for error return and process it 410 * @c: current client instance 411 * @req: request to parse and check for error conditions 412 * 413 * returns error code if one is discovered, otherwise returns 0 414 * 415 * this will have to be more complicated if we have multiple 416 * error packet types 417 */ 418 419static int p9_check_errors(struct p9_client *c, struct p9_req_t *req) 420{ 421 int8_t type; 422 int err; 423 424 err = p9_parse_header(req->rc, NULL, &type, NULL, 0); 425 if (err) { 426 P9_DPRINTK(P9_DEBUG_ERROR, "couldn't parse header %d\n", err); 427 return err; 428 } 429 430 if (type == P9_RERROR) { 431 int ecode; 432 char *ename; 433 434 err = p9pdu_readf(req->rc, c->dotu, "s?d", &ename, &ecode); 435 if (err) { 436 P9_DPRINTK(P9_DEBUG_ERROR, "couldn't parse error%d\n", 437 err); 438 return err; 439 } 440 441 if (c->dotu) 442 err = -ecode; 443 444 if (!err) { 445 err = p9_errstr2errno(ename, strlen(ename)); 446 447 /* string match failed */ 448 if (!err) 449 err = -ESERVERFAULT; 450 } 451 452 P9_DPRINTK(P9_DEBUG_9P, "<<< RERROR (%d) %s\n", -ecode, ename); 453 454 kfree(ename); 455 } else 456 err = 0; 457 458 return err; 459} 460 461/** 462 * p9_client_flush - flush (cancel) a request 463 * c: client state 464 * req: request to cancel 465 * 466 * This sents a flush for a particular requests and links 467 * the flush request to the original request. The current 468 * code only supports a single flush request although the protocol 469 * allows for multiple flush requests to be sent for a single request. 470 * 471 */ 472 473static int p9_client_flush(struct p9_client *c, struct p9_req_t *oldreq) 474{ 475 struct p9_req_t *req; 476 int16_t oldtag; 477 int err; 478 479 err = p9_parse_header(oldreq->tc, NULL, NULL, &oldtag, 1); 480 if (err) 481 return err; 482 483 P9_DPRINTK(P9_DEBUG_9P, ">>> TFLUSH tag %d\n", oldtag); 484 485 req = p9_client_rpc(c, P9_TFLUSH, "w", oldtag); 486 if (IS_ERR(req)) 487 return PTR_ERR(req); 488 489 req->flush_tag = oldtag; 490 491 /* we don't free anything here because RPC isn't complete */ 492 return 0; 493} 494 495/** 496 * p9_client_rpc - issue a request and wait for a response 497 * @c: client session 498 * @type: type of request 499 * @fmt: protocol format string (see protocol.c) 500 * 501 * Returns request structure (which client must free using p9_free_req) 502 */ 503 504static struct p9_req_t * 505p9_client_rpc(struct p9_client *c, int8_t type, const char *fmt, ...) 506{ 507 va_list ap; 508 int tag, err; 509 struct p9_req_t *req; 510 unsigned long flags; 511 int sigpending; 512 int flushed = 0; 513 514 P9_DPRINTK(P9_DEBUG_MUX, "client %p op %d\n", c, type); 515 516 if (c->status != Connected) 517 return ERR_PTR(-EIO); 518 519 if (signal_pending(current)) { 520 sigpending = 1; 521 clear_thread_flag(TIF_SIGPENDING); 522 } else 523 sigpending = 0; 524 525 tag = P9_NOTAG; 526 if (type != P9_TVERSION) { 527 tag = p9_idpool_get(c->tagpool); 528 if (tag < 0) 529 return ERR_PTR(-ENOMEM); 530 } 531 532 req = p9_tag_alloc(c, tag); 533 if (IS_ERR(req)) 534 return req; 535 536 /* marshall the data */ 537 p9pdu_prepare(req->tc, tag, type); 538 va_start(ap, fmt); 539 err = p9pdu_vwritef(req->tc, c->dotu, fmt, ap); 540 va_end(ap); 541 p9pdu_finalize(req->tc); 542 543 err = c->trans_mod->request(c, req); 544 if (err < 0) { 545 c->status = Disconnected; 546 goto reterr; 547 } 548 549 /* if it was a flush we just transmitted, return our tag */ 550 if (type == P9_TFLUSH) 551 return req; 552again: 553 P9_DPRINTK(P9_DEBUG_MUX, "wait %p tag: %d\n", req->wq, tag); 554 err = wait_event_interruptible(*req->wq, 555 req->status >= REQ_STATUS_RCVD); 556 P9_DPRINTK(P9_DEBUG_MUX, "wait %p tag: %d returned %d (flushed=%d)\n", 557 req->wq, tag, err, flushed); 558 559 if (req->status == REQ_STATUS_ERROR) { 560 P9_DPRINTK(P9_DEBUG_ERROR, "req_status error %d\n", req->t_err); 561 err = req->t_err; 562 } else if (err == -ERESTARTSYS && flushed) { 563 P9_DPRINTK(P9_DEBUG_MUX, "flushed - going again\n"); 564 goto again; 565 } else if (req->status == REQ_STATUS_FLSHD) { 566 P9_DPRINTK(P9_DEBUG_MUX, "flushed - erestartsys\n"); 567 err = -ERESTARTSYS; 568 } 569 570 if ((err == -ERESTARTSYS) && (c->status == Connected) && (!flushed)) { 571 P9_DPRINTK(P9_DEBUG_MUX, "flushing\n"); 572 spin_lock_irqsave(&c->lock, flags); 573 if (req->status == REQ_STATUS_SENT) 574 req->status = REQ_STATUS_FLSH; 575 spin_unlock_irqrestore(&c->lock, flags); 576 sigpending = 1; 577 flushed = 1; 578 clear_thread_flag(TIF_SIGPENDING); 579 580 if (c->trans_mod->cancel(c, req)) { 581 err = p9_client_flush(c, req); 582 if (err == 0) 583 goto again; 584 } 585 } 586 587 if (sigpending) { 588 spin_lock_irqsave(¤t->sighand->siglock, flags); 589 recalc_sigpending(); 590 spin_unlock_irqrestore(¤t->sighand->siglock, flags); 591 } 592 593 if (err < 0) 594 goto reterr; 595 596 err = p9_check_errors(c, req); 597 if (!err) { 598 P9_DPRINTK(P9_DEBUG_MUX, "exit: client %p op %d\n", c, type); 599 return req; 600 } 601 602reterr: 603 P9_DPRINTK(P9_DEBUG_MUX, "exit: client %p op %d error: %d\n", c, type, 604 err); 605 p9_free_req(c, req); 606 return ERR_PTR(err); 607} 608 609static struct p9_fid *p9_fid_create(struct p9_client *clnt) 610{ 611 int err; 612 struct p9_fid *fid; 613 unsigned long flags; 614 615 P9_DPRINTK(P9_DEBUG_FID, "clnt %p\n", clnt); 616 fid = kmalloc(sizeof(struct p9_fid), GFP_KERNEL); 617 if (!fid) 618 return ERR_PTR(-ENOMEM); 619 620 fid->fid = p9_idpool_get(clnt->fidpool); 621 if (fid->fid < 0) { 622 err = -ENOSPC; 623 goto error; 624 } 625 626 memset(&fid->qid, 0, sizeof(struct p9_qid)); 627 fid->mode = -1; 628 fid->rdir_fpos = 0; 629 fid->uid = current->fsuid; 630 fid->clnt = clnt; 631 fid->aux = NULL; 632 633 spin_lock_irqsave(&clnt->lock, flags); 634 list_add(&fid->flist, &clnt->fidlist); 635 spin_unlock_irqrestore(&clnt->lock, flags); 636 637 return fid; 638 639error: 640 kfree(fid); 641 return ERR_PTR(err); 642} 643 644static void p9_fid_destroy(struct p9_fid *fid) 645{ 646 struct p9_client *clnt; 647 unsigned long flags; 648 649 P9_DPRINTK(P9_DEBUG_FID, "fid %d\n", fid->fid); 650 clnt = fid->clnt; 651 p9_idpool_put(fid->fid, clnt->fidpool); 652 spin_lock_irqsave(&clnt->lock, flags); 653 list_del(&fid->flist); 654 spin_unlock_irqrestore(&clnt->lock, flags); 655 kfree(fid); 656} 657 658int p9_client_version(struct p9_client *c) 659{ 660 int err = 0; 661 struct p9_req_t *req; 662 char *version; 663 int msize; 664 665 P9_DPRINTK(P9_DEBUG_9P, ">>> TVERSION msize %d extended %d\n", 666 c->msize, c->dotu); 667 req = p9_client_rpc(c, P9_TVERSION, "ds", c->msize, 668 c->dotu ? "9P2000.u" : "9P2000"); 669 if (IS_ERR(req)) 670 return PTR_ERR(req); 671 672 err = p9pdu_readf(req->rc, c->dotu, "ds", &msize, &version); 673 if (err) { 674 P9_DPRINTK(P9_DEBUG_9P, "version error %d\n", err); 675 p9pdu_dump(1, req->rc); 676 goto error; 677 } 678 679 P9_DPRINTK(P9_DEBUG_9P, "<<< RVERSION msize %d %s\n", msize, version); 680 if (!memcmp(version, "9P2000.u", 8)) 681 c->dotu = 1; 682 else if (!memcmp(version, "9P2000", 6)) 683 c->dotu = 0; 684 else { 685 err = -EREMOTEIO; 686 goto error; 687 } 688 689 if (msize < c->msize) 690 c->msize = msize; 691 692error: 693 kfree(version); 694 p9_free_req(c, req); 695 696 return err; 697} 698EXPORT_SYMBOL(p9_client_version); 699 700struct p9_client *p9_client_create(const char *dev_name, char *options) 701{ 702 int err; 703 struct p9_client *clnt; 704 705 err = 0; 706 clnt = kmalloc(sizeof(struct p9_client), GFP_KERNEL); 707 if (!clnt) 708 return ERR_PTR(-ENOMEM); 709 710 clnt->trans_mod = NULL; 711 clnt->trans = NULL; 712 spin_lock_init(&clnt->lock); 713 INIT_LIST_HEAD(&clnt->fidlist); 714 clnt->fidpool = p9_idpool_create(); 715 if (IS_ERR(clnt->fidpool)) { 716 err = PTR_ERR(clnt->fidpool); 717 clnt->fidpool = NULL; 718 goto error; 719 } 720 721 p9_tag_init(clnt); 722 723 err = parse_opts(options, clnt); 724 if (err < 0) 725 goto error; 726 727 if (clnt->trans_mod == NULL) { 728 err = -EPROTONOSUPPORT; 729 P9_DPRINTK(P9_DEBUG_ERROR, 730 "No transport defined or default transport\n"); 731 goto error; 732 } 733 734 P9_DPRINTK(P9_DEBUG_MUX, "clnt %p trans %p msize %d dotu %d\n", 735 clnt, clnt->trans_mod, clnt->msize, clnt->dotu); 736 737 err = clnt->trans_mod->create(clnt, dev_name, options); 738 if (err) 739 goto error; 740 741 if ((clnt->msize+P9_IOHDRSZ) > clnt->trans_mod->maxsize) 742 clnt->msize = clnt->trans_mod->maxsize-P9_IOHDRSZ; 743 744 err = p9_client_version(clnt); 745 if (err) 746 goto error; 747 748 return clnt; 749 750error: 751 p9_client_destroy(clnt); 752 return ERR_PTR(err); 753} 754EXPORT_SYMBOL(p9_client_create); 755 756void p9_client_destroy(struct p9_client *clnt) 757{ 758 struct p9_fid *fid, *fidptr; 759 760 P9_DPRINTK(P9_DEBUG_MUX, "clnt %p\n", clnt); 761 762 if (clnt->trans_mod) 763 clnt->trans_mod->close(clnt); 764 765 v9fs_put_trans(clnt->trans_mod); 766 767 list_for_each_entry_safe(fid, fidptr, &clnt->fidlist, flist) 768 p9_fid_destroy(fid); 769 770 if (clnt->fidpool) 771 p9_idpool_destroy(clnt->fidpool); 772 773 p9_tag_cleanup(clnt); 774 775 kfree(clnt); 776} 777EXPORT_SYMBOL(p9_client_destroy); 778 779void p9_client_disconnect(struct p9_client *clnt) 780{ 781 P9_DPRINTK(P9_DEBUG_9P, "clnt %p\n", clnt); 782 clnt->status = Disconnected; 783} 784EXPORT_SYMBOL(p9_client_disconnect); 785 786struct p9_fid *p9_client_attach(struct p9_client *clnt, struct p9_fid *afid, 787 char *uname, u32 n_uname, char *aname) 788{ 789 int err; 790 struct p9_req_t *req; 791 struct p9_fid *fid; 792 struct p9_qid qid; 793 794 P9_DPRINTK(P9_DEBUG_9P, ">>> TATTACH afid %d uname %s aname %s\n", 795 afid ? afid->fid : -1, uname, aname); 796 err = 0; 797 798 fid = p9_fid_create(clnt); 799 if (IS_ERR(fid)) { 800 err = PTR_ERR(fid); 801 fid = NULL; 802 goto error; 803 } 804 805 req = p9_client_rpc(clnt, P9_TATTACH, "ddss?d", fid->fid, 806 afid ? afid->fid : P9_NOFID, uname, aname, n_uname); 807 if (IS_ERR(req)) { 808 err = PTR_ERR(req); 809 goto error; 810 } 811 812 err = p9pdu_readf(req->rc, clnt->dotu, "Q", &qid); 813 if (err) { 814 p9pdu_dump(1, req->rc); 815 p9_free_req(clnt, req); 816 goto error; 817 } 818 819 P9_DPRINTK(P9_DEBUG_9P, "<<< RATTACH qid %x.%llx.%x\n", 820 qid.type, qid.path, qid.version); 821 822 memmove(&fid->qid, &qid, sizeof(struct p9_qid)); 823 824 p9_free_req(clnt, req); 825 return fid; 826 827error: 828 if (fid) 829 p9_fid_destroy(fid); 830 return ERR_PTR(err); 831} 832EXPORT_SYMBOL(p9_client_attach); 833 834struct p9_fid * 835p9_client_auth(struct p9_client *clnt, char *uname, u32 n_uname, char *aname) 836{ 837 int err; 838 struct p9_req_t *req; 839 struct p9_qid qid; 840 struct p9_fid *afid; 841 842 P9_DPRINTK(P9_DEBUG_9P, ">>> TAUTH uname %s aname %s\n", uname, aname); 843 err = 0; 844 845 afid = p9_fid_create(clnt); 846 if (IS_ERR(afid)) { 847 err = PTR_ERR(afid); 848 afid = NULL; 849 goto error; 850 } 851 852 req = p9_client_rpc(clnt, P9_TAUTH, "dss?d", 853 afid ? afid->fid : P9_NOFID, uname, aname, n_uname); 854 if (IS_ERR(req)) { 855 err = PTR_ERR(req); 856 goto error; 857 } 858 859 err = p9pdu_readf(req->rc, clnt->dotu, "Q", &qid); 860 if (err) { 861 p9pdu_dump(1, req->rc); 862 p9_free_req(clnt, req); 863 goto error; 864 } 865 866 P9_DPRINTK(P9_DEBUG_9P, "<<< RAUTH qid %x.%llx.%x\n", 867 qid.type, qid.path, qid.version); 868 869 memmove(&afid->qid, &qid, sizeof(struct p9_qid)); 870 p9_free_req(clnt, req); 871 return afid; 872 873error: 874 if (afid) 875 p9_fid_destroy(afid); 876 return ERR_PTR(err); 877} 878EXPORT_SYMBOL(p9_client_auth); 879 880struct p9_fid *p9_client_walk(struct p9_fid *oldfid, int nwname, char **wnames, 881 int clone) 882{ 883 int err; 884 struct p9_client *clnt; 885 struct p9_fid *fid; 886 struct p9_qid *wqids; 887 struct p9_req_t *req; 888 int16_t nwqids, count; 889 890 err = 0; 891 clnt = oldfid->clnt; 892 if (clone) { 893 fid = p9_fid_create(clnt); 894 if (IS_ERR(fid)) { 895 err = PTR_ERR(fid); 896 fid = NULL; 897 goto error; 898 } 899 900 fid->uid = oldfid->uid; 901 } else 902 fid = oldfid; 903 904 905 P9_DPRINTK(P9_DEBUG_9P, ">>> TWALK fids %d,%d nwname %d wname[0] %s\n", 906 oldfid->fid, fid->fid, nwname, wnames ? wnames[0] : NULL); 907 908 req = p9_client_rpc(clnt, P9_TWALK, "ddT", oldfid->fid, fid->fid, 909 nwname, wnames); 910 if (IS_ERR(req)) { 911 err = PTR_ERR(req); 912 goto error; 913 } 914 915 err = p9pdu_readf(req->rc, clnt->dotu, "R", &nwqids, &wqids); 916 if (err) { 917 p9pdu_dump(1, req->rc); 918 p9_free_req(clnt, req); 919 goto clunk_fid; 920 } 921 p9_free_req(clnt, req); 922 923 P9_DPRINTK(P9_DEBUG_9P, "<<< RWALK nwqid %d:\n", nwqids); 924 925 if (nwqids != nwname) { 926 err = -ENOENT; 927 goto clunk_fid; 928 } 929 930 for (count = 0; count < nwqids; count++) 931 P9_DPRINTK(P9_DEBUG_9P, "<<< [%d] %x.%llx.%x\n", 932 count, wqids[count].type, wqids[count].path, 933 wqids[count].version); 934 935 if (nwname) 936 memmove(&fid->qid, &wqids[nwqids - 1], sizeof(struct p9_qid)); 937 else 938 fid->qid = oldfid->qid; 939 940 return fid; 941 942clunk_fid: 943 p9_client_clunk(fid); 944 fid = NULL; 945 946error: 947 if (fid && (fid != oldfid)) 948 p9_fid_destroy(fid); 949 950 return ERR_PTR(err); 951} 952EXPORT_SYMBOL(p9_client_walk); 953 954int p9_client_open(struct p9_fid *fid, int mode) 955{ 956 int err; 957 struct p9_client *clnt; 958 struct p9_req_t *req; 959 struct p9_qid qid; 960 int iounit; 961 962 P9_DPRINTK(P9_DEBUG_9P, ">>> TOPEN fid %d mode %d\n", fid->fid, mode); 963 err = 0; 964 clnt = fid->clnt; 965 966 if (fid->mode != -1) 967 return -EINVAL; 968 969 req = p9_client_rpc(clnt, P9_TOPEN, "db", fid->fid, mode); 970 if (IS_ERR(req)) { 971 err = PTR_ERR(req); 972 goto error; 973 } 974 975 err = p9pdu_readf(req->rc, clnt->dotu, "Qd", &qid, &iounit); 976 if (err) { 977 p9pdu_dump(1, req->rc); 978 goto free_and_error; 979 } 980 981 P9_DPRINTK(P9_DEBUG_9P, "<<< ROPEN qid %x.%llx.%x iounit %x\n", 982 qid.type, qid.path, qid.version, iounit); 983 984 fid->mode = mode; 985 fid->iounit = iounit; 986 987free_and_error: 988 p9_free_req(clnt, req); 989error: 990 return err; 991} 992EXPORT_SYMBOL(p9_client_open); 993 994int p9_client_fcreate(struct p9_fid *fid, char *name, u32 perm, int mode, 995 char *extension) 996{ 997 int err; 998 struct p9_client *clnt; 999 struct p9_req_t *req; 1000 struct p9_qid qid; 1001 int iounit; 1002 1003 P9_DPRINTK(P9_DEBUG_9P, ">>> TCREATE fid %d name %s perm %d mode %d\n", 1004 fid->fid, name, perm, mode); 1005 err = 0; 1006 clnt = fid->clnt; 1007 1008 if (fid->mode != -1) 1009 return -EINVAL; 1010 1011 req = p9_client_rpc(clnt, P9_TCREATE, "dsdb?s", fid->fid, name, perm, 1012 mode, extension); 1013 if (IS_ERR(req)) { 1014 err = PTR_ERR(req); 1015 goto error; 1016 } 1017 1018 err = p9pdu_readf(req->rc, clnt->dotu, "Qd", &qid, &iounit); 1019 if (err) { 1020 p9pdu_dump(1, req->rc); 1021 goto free_and_error; 1022 } 1023 1024 P9_DPRINTK(P9_DEBUG_9P, "<<< RCREATE qid %x.%llx.%x iounit %x\n", 1025 qid.type, qid.path, qid.version, iounit); 1026 1027 fid->mode = mode; 1028 fid->iounit = iounit; 1029 1030free_and_error: 1031 p9_free_req(clnt, req); 1032error: 1033 return err; 1034} 1035EXPORT_SYMBOL(p9_client_fcreate); 1036 1037int p9_client_clunk(struct p9_fid *fid) 1038{ 1039 int err; 1040 struct p9_client *clnt; 1041 struct p9_req_t *req; 1042 1043 P9_DPRINTK(P9_DEBUG_9P, ">>> TCLUNK fid %d\n", fid->fid); 1044 err = 0; 1045 clnt = fid->clnt; 1046 1047 req = p9_client_rpc(clnt, P9_TCLUNK, "d", fid->fid); 1048 if (IS_ERR(req)) { 1049 err = PTR_ERR(req); 1050 goto error; 1051 } 1052 1053 P9_DPRINTK(P9_DEBUG_9P, "<<< RCLUNK fid %d\n", fid->fid); 1054 1055 p9_free_req(clnt, req); 1056 p9_fid_destroy(fid); 1057 1058error: 1059 return err; 1060} 1061EXPORT_SYMBOL(p9_client_clunk); 1062 1063int p9_client_remove(struct p9_fid *fid) 1064{ 1065 int err; 1066 struct p9_client *clnt; 1067 struct p9_req_t *req; 1068 1069 P9_DPRINTK(P9_DEBUG_9P, ">>> TREMOVE fid %d\n", fid->fid); 1070 err = 0; 1071 clnt = fid->clnt; 1072 1073 req = p9_client_rpc(clnt, P9_TREMOVE, "d", fid->fid); 1074 if (IS_ERR(req)) { 1075 err = PTR_ERR(req); 1076 goto error; 1077 } 1078 1079 P9_DPRINTK(P9_DEBUG_9P, "<<< RREMOVE fid %d\n", fid->fid); 1080 1081 p9_free_req(clnt, req); 1082 p9_fid_destroy(fid); 1083 1084error: 1085 return err; 1086} 1087EXPORT_SYMBOL(p9_client_remove); 1088 1089int 1090p9_client_read(struct p9_fid *fid, char *data, char __user *udata, u64 offset, 1091 u32 count) 1092{ 1093 int err, rsize, total; 1094 struct p9_client *clnt; 1095 struct p9_req_t *req; 1096 char *dataptr; 1097 1098 P9_DPRINTK(P9_DEBUG_9P, ">>> TREAD fid %d offset %llu %d\n", fid->fid, 1099 (long long unsigned) offset, count); 1100 err = 0; 1101 clnt = fid->clnt; 1102 total = 0; 1103 1104 rsize = fid->iounit; 1105 if (!rsize || rsize > clnt->msize-P9_IOHDRSZ) 1106 rsize = clnt->msize - P9_IOHDRSZ; 1107 1108 if (count < rsize) 1109 rsize = count; 1110 1111 req = p9_client_rpc(clnt, P9_TREAD, "dqd", fid->fid, offset, rsize); 1112 if (IS_ERR(req)) { 1113 err = PTR_ERR(req); 1114 goto error; 1115 } 1116 1117 err = p9pdu_readf(req->rc, clnt->dotu, "D", &count, &dataptr); 1118 if (err) { 1119 p9pdu_dump(1, req->rc); 1120 goto free_and_error; 1121 } 1122 1123 P9_DPRINTK(P9_DEBUG_9P, "<<< RREAD count %d\n", count); 1124 1125 if (data) { 1126 memmove(data, dataptr, count); 1127 data += count; 1128 } 1129 1130 if (udata) { 1131 err = copy_to_user(udata, dataptr, count); 1132 if (err) { 1133 err = -EFAULT; 1134 goto free_and_error; 1135 } 1136 } 1137 1138 p9_free_req(clnt, req); 1139 return count; 1140 1141free_and_error: 1142 p9_free_req(clnt, req); 1143error: 1144 return err; 1145} 1146EXPORT_SYMBOL(p9_client_read); 1147 1148int 1149p9_client_write(struct p9_fid *fid, char *data, const char __user *udata, 1150 u64 offset, u32 count) 1151{ 1152 int err, rsize, total; 1153 struct p9_client *clnt; 1154 struct p9_req_t *req; 1155 1156 P9_DPRINTK(P9_DEBUG_9P, ">>> TWRITE fid %d offset %llu count %d\n", 1157 fid->fid, (long long unsigned) offset, count); 1158 err = 0; 1159 clnt = fid->clnt; 1160 total = 0; 1161 1162 rsize = fid->iounit; 1163 if (!rsize || rsize > clnt->msize-P9_IOHDRSZ) 1164 rsize = clnt->msize - P9_IOHDRSZ; 1165 1166 if (count < rsize) 1167 rsize = count; 1168 if (data) 1169 req = p9_client_rpc(clnt, P9_TWRITE, "dqD", fid->fid, offset, 1170 rsize, data); 1171 else 1172 req = p9_client_rpc(clnt, P9_TWRITE, "dqU", fid->fid, offset, 1173 rsize, udata); 1174 if (IS_ERR(req)) { 1175 err = PTR_ERR(req); 1176 goto error; 1177 } 1178 1179 err = p9pdu_readf(req->rc, clnt->dotu, "d", &count); 1180 if (err) { 1181 p9pdu_dump(1, req->rc); 1182 goto free_and_error; 1183 } 1184 1185 P9_DPRINTK(P9_DEBUG_9P, "<<< RWRITE count %d\n", count); 1186 1187 p9_free_req(clnt, req); 1188 return count; 1189 1190free_and_error: 1191 p9_free_req(clnt, req); 1192error: 1193 return err; 1194} 1195EXPORT_SYMBOL(p9_client_write); 1196 1197struct p9_wstat *p9_client_stat(struct p9_fid *fid) 1198{ 1199 int err; 1200 struct p9_client *clnt; 1201 struct p9_wstat *ret = kmalloc(sizeof(struct p9_wstat), GFP_KERNEL); 1202 struct p9_req_t *req; 1203 u16 ignored; 1204 1205 P9_DPRINTK(P9_DEBUG_9P, ">>> TSTAT fid %d\n", fid->fid); 1206 1207 if (!ret) 1208 return ERR_PTR(-ENOMEM); 1209 1210 err = 0; 1211 clnt = fid->clnt; 1212 1213 req = p9_client_rpc(clnt, P9_TSTAT, "d", fid->fid); 1214 if (IS_ERR(req)) { 1215 err = PTR_ERR(req); 1216 goto error; 1217 } 1218 1219 err = p9pdu_readf(req->rc, clnt->dotu, "wS", &ignored, ret); 1220 if (err) { 1221 ret = ERR_PTR(err); 1222 p9pdu_dump(1, req->rc); 1223 goto free_and_error; 1224 } 1225 1226 P9_DPRINTK(P9_DEBUG_9P, 1227 "<<< RSTAT sz=%x type=%x dev=%x qid=%x.%llx.%x\n" 1228 "<<< mode=%8.8x atime=%8.8x mtime=%8.8x length=%llx\n" 1229 "<<< name=%s uid=%s gid=%s muid=%s extension=(%s)\n" 1230 "<<< uid=%d gid=%d n_muid=%d\n", 1231 ret->size, ret->type, ret->dev, ret->qid.type, 1232 ret->qid.path, ret->qid.version, ret->mode, 1233 ret->atime, ret->mtime, ret->length, ret->name, 1234 ret->uid, ret->gid, ret->muid, ret->extension, 1235 ret->n_uid, ret->n_gid, ret->n_muid); 1236 1237free_and_error: 1238 p9_free_req(clnt, req); 1239error: 1240 return ret; 1241} 1242EXPORT_SYMBOL(p9_client_stat); 1243 1244int p9_client_wstat(struct p9_fid *fid, struct p9_wstat *wst) 1245{ 1246 int err; 1247 struct p9_req_t *req; 1248 struct p9_client *clnt; 1249 1250 P9_DPRINTK(P9_DEBUG_9P, ">>> TWSTAT fid %d\n", fid->fid); 1251 P9_DPRINTK(P9_DEBUG_9P, 1252 " sz=%x type=%x dev=%x qid=%x.%llx.%x\n" 1253 " mode=%8.8x atime=%8.8x mtime=%8.8x length=%llx\n" 1254 " name=%s uid=%s gid=%s muid=%s extension=(%s)\n" 1255 " uid=%d gid=%d n_muid=%d\n", 1256 wst->size, wst->type, wst->dev, wst->qid.type, 1257 wst->qid.path, wst->qid.version, wst->mode, 1258 wst->atime, wst->mtime, wst->length, wst->name, 1259 wst->uid, wst->gid, wst->muid, wst->extension, 1260 wst->n_uid, wst->n_gid, wst->n_muid); 1261 err = 0; 1262 clnt = fid->clnt; 1263 1264 req = p9_client_rpc(clnt, P9_TWSTAT, "dwS", fid->fid, 0, wst); 1265 if (IS_ERR(req)) { 1266 err = PTR_ERR(req); 1267 goto error; 1268 } 1269 1270 P9_DPRINTK(P9_DEBUG_9P, "<<< RWSTAT fid %d\n", fid->fid); 1271 1272 p9_free_req(clnt, req); 1273error: 1274 return err; 1275} 1276EXPORT_SYMBOL(p9_client_wstat); 1277