1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * 9P Client 4 * 5 * Copyright (C) 2008 by Eric Van Hensbergen <ericvh@gmail.com> 6 * Copyright (C) 2007 by Latchesar Ionkov <lucho@ionkov.net> 7 */ 8 9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 10 11 #include <linux/module.h> 12 #include <linux/errno.h> 13 #include <linux/fs.h> 14 #include <linux/poll.h> 15 #include <linux/idr.h> 16 #include <linux/mutex.h> 17 #include <linux/slab.h> 18 #include <linux/sched/signal.h> 19 #include <linux/uaccess.h> 20 #include <linux/uio.h> 21 #include <net/9p/9p.h> 22 #include <linux/parser.h> 23 #include <linux/seq_file.h> 24 #include <net/9p/client.h> 25 #include <net/9p/transport.h> 26 #include "protocol.h" 27 28 #define CREATE_TRACE_POINTS 29 #include <trace/events/9p.h> 30 31 #define DEFAULT_MSIZE (128 * 1024) 32 33 /* Client Option Parsing (code inspired by NFS code) 34 * - a little lazy - parse all client options 35 */ 36 37 enum { 38 Opt_msize, 39 Opt_trans, 40 Opt_legacy, 41 Opt_version, 42 Opt_err, 43 }; 44 45 static const match_table_t tokens = { 46 {Opt_msize, "msize=%u"}, 47 {Opt_legacy, "noextend"}, 48 {Opt_trans, "trans=%s"}, 49 {Opt_version, "version=%s"}, 50 {Opt_err, NULL}, 51 }; 52 53 inline int p9_is_proto_dotl(struct p9_client *clnt) 54 { 55 return clnt->proto_version == p9_proto_2000L; 56 } 57 EXPORT_SYMBOL(p9_is_proto_dotl); 58 59 inline int p9_is_proto_dotu(struct p9_client *clnt) 60 { 61 return clnt->proto_version == p9_proto_2000u; 62 } 63 EXPORT_SYMBOL(p9_is_proto_dotu); 64 65 int p9_show_client_options(struct seq_file *m, struct p9_client *clnt) 66 { 67 if (clnt->msize != DEFAULT_MSIZE) 68 seq_printf(m, ",msize=%u", clnt->msize); 69 seq_printf(m, ",trans=%s", clnt->trans_mod->name); 70 71 switch (clnt->proto_version) { 72 case p9_proto_legacy: 73 seq_puts(m, ",noextend"); 74 break; 75 case p9_proto_2000u: 76 seq_puts(m, ",version=9p2000.u"); 77 break; 78 case p9_proto_2000L: 79 /* Default */ 80 break; 81 } 82 83 if (clnt->trans_mod->show_options) 84 return clnt->trans_mod->show_options(m, clnt); 85 return 0; 86 } 87 EXPORT_SYMBOL(p9_show_client_options); 88 89 /* Some error codes are taken directly from the server replies, 90 * make sure they are valid. 91 */ 92 static int safe_errno(int err) 93 { 94 if (err > 0 || err < -MAX_ERRNO) { 95 p9_debug(P9_DEBUG_ERROR, "Invalid error code %d\n", err); 96 return -EPROTO; 97 } 98 return err; 99 } 100 101 /* Interpret mount option for protocol version */ 102 static int get_protocol_version(char *s) 103 { 104 int version = -EINVAL; 105 106 if (!strcmp(s, "9p2000")) { 107 version = p9_proto_legacy; 108 p9_debug(P9_DEBUG_9P, "Protocol version: Legacy\n"); 109 } else if (!strcmp(s, "9p2000.u")) { 110 version = p9_proto_2000u; 111 p9_debug(P9_DEBUG_9P, "Protocol version: 9P2000.u\n"); 112 } else if (!strcmp(s, "9p2000.L")) { 113 version = p9_proto_2000L; 114 p9_debug(P9_DEBUG_9P, "Protocol version: 9P2000.L\n"); 115 } else { 116 pr_info("Unknown protocol version %s\n", s); 117 } 118 119 return version; 120 } 121 122 /** 123 * parse_opts - parse mount options into client structure 124 * @opts: options string passed from mount 125 * @clnt: existing v9fs client information 126 * 127 * Return 0 upon success, -ERRNO upon failure 128 */ 129 130 static int parse_opts(char *opts, struct p9_client *clnt) 131 { 132 char *options, *tmp_options; 133 char *p; 134 substring_t args[MAX_OPT_ARGS]; 135 int option; 136 char *s; 137 int ret = 0; 138 139 clnt->proto_version = p9_proto_2000L; 140 clnt->msize = DEFAULT_MSIZE; 141 142 if (!opts) 143 return 0; 144 145 tmp_options = kstrdup(opts, GFP_KERNEL); 146 if (!tmp_options) 147 return -ENOMEM; 148 options = tmp_options; 149 150 while ((p = strsep(&options, ",")) != NULL) { 151 int token, r; 152 153 if (!*p) 154 continue; 155 token = match_token(p, tokens, args); 156 switch (token) { 157 case Opt_msize: 158 r = match_int(&args[0], &option); 159 if (r < 0) { 160 p9_debug(P9_DEBUG_ERROR, 161 "integer field, but no integer?\n"); 162 ret = r; 163 continue; 164 } 165 if (option < 4096) { 166 p9_debug(P9_DEBUG_ERROR, 167 "msize should be at least 4k\n"); 168 ret = -EINVAL; 169 continue; 170 } 171 clnt->msize = option; 172 break; 173 case Opt_trans: 174 s = match_strdup(&args[0]); 175 if (!s) { 176 ret = -ENOMEM; 177 p9_debug(P9_DEBUG_ERROR, 178 "problem allocating copy of trans arg\n"); 179 goto free_and_return; 180 } 181 182 v9fs_put_trans(clnt->trans_mod); 183 clnt->trans_mod = v9fs_get_trans_by_name(s); 184 if (!clnt->trans_mod) { 185 pr_info("Could not find request transport: %s\n", 186 s); 187 ret = -EINVAL; 188 } 189 kfree(s); 190 break; 191 case Opt_legacy: 192 clnt->proto_version = p9_proto_legacy; 193 break; 194 case Opt_version: 195 s = match_strdup(&args[0]); 196 if (!s) { 197 ret = -ENOMEM; 198 p9_debug(P9_DEBUG_ERROR, 199 "problem allocating copy of version arg\n"); 200 goto free_and_return; 201 } 202 r = get_protocol_version(s); 203 if (r < 0) 204 ret = r; 205 else 206 clnt->proto_version = r; 207 kfree(s); 208 break; 209 default: 210 continue; 211 } 212 } 213 214 free_and_return: 215 if (ret) 216 v9fs_put_trans(clnt->trans_mod); 217 kfree(tmp_options); 218 return ret; 219 } 220 221 static int p9_fcall_init(struct p9_client *c, struct p9_fcall *fc, 222 int alloc_msize) 223 { 224 if (likely(c->fcall_cache) && alloc_msize == c->msize) { 225 fc->sdata = kmem_cache_alloc(c->fcall_cache, GFP_NOFS); 226 fc->cache = c->fcall_cache; 227 } else { 228 fc->sdata = kmalloc(alloc_msize, GFP_NOFS); 229 fc->cache = NULL; 230 } 231 if (!fc->sdata) 232 return -ENOMEM; 233 fc->capacity = alloc_msize; 234 return 0; 235 } 236 237 void p9_fcall_fini(struct p9_fcall *fc) 238 { 239 /* sdata can be NULL for interrupted requests in trans_rdma, 240 * and kmem_cache_free does not do NULL-check for us 241 */ 242 if (unlikely(!fc->sdata)) 243 return; 244 245 if (fc->cache) 246 kmem_cache_free(fc->cache, fc->sdata); 247 else 248 kfree(fc->sdata); 249 } 250 EXPORT_SYMBOL(p9_fcall_fini); 251 252 static struct kmem_cache *p9_req_cache; 253 254 /** 255 * p9_tag_alloc - Allocate a new request. 256 * @c: Client session. 257 * @type: Transaction type. 258 * @max_size: Maximum packet size for this request. 259 * 260 * Context: Process context. 261 * Return: Pointer to new request. 262 */ 263 static struct p9_req_t * 264 p9_tag_alloc(struct p9_client *c, int8_t type, unsigned int max_size) 265 { 266 struct p9_req_t *req = kmem_cache_alloc(p9_req_cache, GFP_NOFS); 267 int alloc_msize = min(c->msize, max_size); 268 int tag; 269 270 if (!req) 271 return ERR_PTR(-ENOMEM); 272 273 if (p9_fcall_init(c, &req->tc, alloc_msize)) 274 goto free_req; 275 if (p9_fcall_init(c, &req->rc, alloc_msize)) 276 goto free; 277 278 p9pdu_reset(&req->tc); 279 p9pdu_reset(&req->rc); 280 req->t_err = 0; 281 req->status = REQ_STATUS_ALLOC; 282 init_waitqueue_head(&req->wq); 283 INIT_LIST_HEAD(&req->req_list); 284 285 idr_preload(GFP_NOFS); 286 spin_lock_irq(&c->lock); 287 if (type == P9_TVERSION) 288 tag = idr_alloc(&c->reqs, req, P9_NOTAG, P9_NOTAG + 1, 289 GFP_NOWAIT); 290 else 291 tag = idr_alloc(&c->reqs, req, 0, P9_NOTAG, GFP_NOWAIT); 292 req->tc.tag = tag; 293 spin_unlock_irq(&c->lock); 294 idr_preload_end(); 295 if (tag < 0) 296 goto free; 297 298 /* Init ref to two because in the general case there is one ref 299 * that is put asynchronously by a writer thread, one ref 300 * temporarily given by p9_tag_lookup and put by p9_client_cb 301 * in the recv thread, and one ref put by p9_tag_remove in the 302 * main thread. The only exception is virtio that does not use 303 * p9_tag_lookup but does not have a writer thread either 304 * (the write happens synchronously in the request/zc_request 305 * callback), so p9_client_cb eats the second ref there 306 * as the pointer is duplicated directly by virtqueue_add_sgs() 307 */ 308 refcount_set(&req->refcount.refcount, 2); 309 310 return req; 311 312 free: 313 p9_fcall_fini(&req->tc); 314 p9_fcall_fini(&req->rc); 315 free_req: 316 kmem_cache_free(p9_req_cache, req); 317 return ERR_PTR(-ENOMEM); 318 } 319 320 /** 321 * p9_tag_lookup - Look up a request by tag. 322 * @c: Client session. 323 * @tag: Transaction ID. 324 * 325 * Context: Any context. 326 * Return: A request, or %NULL if there is no request with that tag. 327 */ 328 struct p9_req_t *p9_tag_lookup(struct p9_client *c, u16 tag) 329 { 330 struct p9_req_t *req; 331 332 rcu_read_lock(); 333 again: 334 req = idr_find(&c->reqs, tag); 335 if (req) { 336 /* We have to be careful with the req found under rcu_read_lock 337 * Thanks to SLAB_TYPESAFE_BY_RCU we can safely try to get the 338 * ref again without corrupting other data, then check again 339 * that the tag matches once we have the ref 340 */ 341 if (!p9_req_try_get(req)) 342 goto again; 343 if (req->tc.tag != tag) { 344 p9_req_put(req); 345 goto again; 346 } 347 } 348 rcu_read_unlock(); 349 350 return req; 351 } 352 EXPORT_SYMBOL(p9_tag_lookup); 353 354 /** 355 * p9_tag_remove - Remove a tag. 356 * @c: Client session. 357 * @r: Request of reference. 358 * 359 * Context: Any context. 360 */ 361 static int p9_tag_remove(struct p9_client *c, struct p9_req_t *r) 362 { 363 unsigned long flags; 364 u16 tag = r->tc.tag; 365 366 p9_debug(P9_DEBUG_MUX, "clnt %p req %p tag: %d\n", c, r, tag); 367 spin_lock_irqsave(&c->lock, flags); 368 idr_remove(&c->reqs, tag); 369 spin_unlock_irqrestore(&c->lock, flags); 370 return p9_req_put(r); 371 } 372 373 static void p9_req_free(struct kref *ref) 374 { 375 struct p9_req_t *r = container_of(ref, struct p9_req_t, refcount); 376 377 p9_fcall_fini(&r->tc); 378 p9_fcall_fini(&r->rc); 379 kmem_cache_free(p9_req_cache, r); 380 } 381 382 int p9_req_put(struct p9_req_t *r) 383 { 384 return kref_put(&r->refcount, p9_req_free); 385 } 386 EXPORT_SYMBOL(p9_req_put); 387 388 /** 389 * p9_tag_cleanup - cleans up tags structure and reclaims resources 390 * @c: v9fs client struct 391 * 392 * This frees resources associated with the tags structure 393 * 394 */ 395 static void p9_tag_cleanup(struct p9_client *c) 396 { 397 struct p9_req_t *req; 398 int id; 399 400 rcu_read_lock(); 401 idr_for_each_entry(&c->reqs, req, id) { 402 pr_info("Tag %d still in use\n", id); 403 if (p9_tag_remove(c, req) == 0) 404 pr_warn("Packet with tag %d has still references", 405 req->tc.tag); 406 } 407 rcu_read_unlock(); 408 } 409 410 /** 411 * p9_client_cb - call back from transport to client 412 * @c: client state 413 * @req: request received 414 * @status: request status, one of REQ_STATUS_* 415 * 416 */ 417 void p9_client_cb(struct p9_client *c, struct p9_req_t *req, int status) 418 { 419 p9_debug(P9_DEBUG_MUX, " tag %d\n", req->tc.tag); 420 421 /* This barrier is needed to make sure any change made to req before 422 * the status change is visible to another thread 423 */ 424 smp_wmb(); 425 req->status = status; 426 427 wake_up(&req->wq); 428 p9_debug(P9_DEBUG_MUX, "wakeup: %d\n", req->tc.tag); 429 p9_req_put(req); 430 } 431 EXPORT_SYMBOL(p9_client_cb); 432 433 /** 434 * p9_parse_header - parse header arguments out of a packet 435 * @pdu: packet to parse 436 * @size: size of packet 437 * @type: type of request 438 * @tag: tag of packet 439 * @rewind: set if we need to rewind offset afterwards 440 */ 441 442 int 443 p9_parse_header(struct p9_fcall *pdu, int32_t *size, int8_t *type, 444 int16_t *tag, int rewind) 445 { 446 s8 r_type; 447 s16 r_tag; 448 s32 r_size; 449 int offset = pdu->offset; 450 int err; 451 452 pdu->offset = 0; 453 454 err = p9pdu_readf(pdu, 0, "dbw", &r_size, &r_type, &r_tag); 455 if (err) 456 goto rewind_and_exit; 457 458 if (type) 459 *type = r_type; 460 if (tag) 461 *tag = r_tag; 462 if (size) 463 *size = r_size; 464 465 if (pdu->size != r_size || r_size < 7) { 466 err = -EINVAL; 467 goto rewind_and_exit; 468 } 469 470 pdu->id = r_type; 471 pdu->tag = r_tag; 472 473 p9_debug(P9_DEBUG_9P, "<<< size=%d type: %d tag: %d\n", 474 pdu->size, pdu->id, pdu->tag); 475 476 rewind_and_exit: 477 if (rewind) 478 pdu->offset = offset; 479 return err; 480 } 481 EXPORT_SYMBOL(p9_parse_header); 482 483 /** 484 * p9_check_errors - check 9p packet for error return and process it 485 * @c: current client instance 486 * @req: request to parse and check for error conditions 487 * 488 * returns error code if one is discovered, otherwise returns 0 489 * 490 * this will have to be more complicated if we have multiple 491 * error packet types 492 */ 493 494 static int p9_check_errors(struct p9_client *c, struct p9_req_t *req) 495 { 496 s8 type; 497 int err; 498 int ecode; 499 500 err = p9_parse_header(&req->rc, NULL, &type, NULL, 0); 501 if (req->rc.size >= c->msize) { 502 p9_debug(P9_DEBUG_ERROR, 503 "requested packet size too big: %d\n", 504 req->rc.size); 505 return -EIO; 506 } 507 /* dump the response from server 508 * This should be after check errors which poplulate pdu_fcall. 509 */ 510 trace_9p_protocol_dump(c, &req->rc); 511 if (err) { 512 p9_debug(P9_DEBUG_ERROR, "couldn't parse header %d\n", err); 513 return err; 514 } 515 if (type != P9_RERROR && type != P9_RLERROR) 516 return 0; 517 518 if (!p9_is_proto_dotl(c)) { 519 char *ename; 520 521 err = p9pdu_readf(&req->rc, c->proto_version, "s?d", 522 &ename, &ecode); 523 if (err) 524 goto out_err; 525 526 if (p9_is_proto_dotu(c) && ecode < 512) 527 err = -ecode; 528 529 if (!err) { 530 err = p9_errstr2errno(ename, strlen(ename)); 531 532 p9_debug(P9_DEBUG_9P, "<<< RERROR (%d) %s\n", 533 -ecode, ename); 534 } 535 kfree(ename); 536 } else { 537 err = p9pdu_readf(&req->rc, c->proto_version, "d", &ecode); 538 if (err) 539 goto out_err; 540 err = -ecode; 541 542 p9_debug(P9_DEBUG_9P, "<<< RLERROR (%d)\n", -ecode); 543 } 544 545 return err; 546 547 out_err: 548 p9_debug(P9_DEBUG_ERROR, "couldn't parse error%d\n", err); 549 550 return err; 551 } 552 553 /** 554 * p9_check_zc_errors - check 9p packet for error return and process it 555 * @c: current client instance 556 * @req: request to parse and check for error conditions 557 * @uidata: external buffer containing error 558 * @in_hdrlen: Size of response protocol buffer. 559 * 560 * returns error code if one is discovered, otherwise returns 0 561 * 562 * this will have to be more complicated if we have multiple 563 * error packet types 564 */ 565 566 static int p9_check_zc_errors(struct p9_client *c, struct p9_req_t *req, 567 struct iov_iter *uidata, int in_hdrlen) 568 { 569 int err; 570 int ecode; 571 s8 type; 572 char *ename = NULL; 573 574 err = p9_parse_header(&req->rc, NULL, &type, NULL, 0); 575 /* dump the response from server 576 * This should be after parse_header which poplulate pdu_fcall. 577 */ 578 trace_9p_protocol_dump(c, &req->rc); 579 if (err) { 580 p9_debug(P9_DEBUG_ERROR, "couldn't parse header %d\n", err); 581 return err; 582 } 583 584 if (type != P9_RERROR && type != P9_RLERROR) 585 return 0; 586 587 if (!p9_is_proto_dotl(c)) { 588 /* Error is reported in string format */ 589 int len; 590 /* 7 = header size for RERROR; */ 591 int inline_len = in_hdrlen - 7; 592 593 len = req->rc.size - req->rc.offset; 594 if (len > (P9_ZC_HDR_SZ - 7)) { 595 err = -EFAULT; 596 goto out_err; 597 } 598 599 ename = &req->rc.sdata[req->rc.offset]; 600 if (len > inline_len) { 601 /* We have error in external buffer */ 602 if (!copy_from_iter_full(ename + inline_len, 603 len - inline_len, uidata)) { 604 err = -EFAULT; 605 goto out_err; 606 } 607 } 608 ename = NULL; 609 err = p9pdu_readf(&req->rc, c->proto_version, "s?d", 610 &ename, &ecode); 611 if (err) 612 goto out_err; 613 614 if (p9_is_proto_dotu(c) && ecode < 512) 615 err = -ecode; 616 617 if (!err) { 618 err = p9_errstr2errno(ename, strlen(ename)); 619 620 p9_debug(P9_DEBUG_9P, "<<< RERROR (%d) %s\n", 621 -ecode, ename); 622 } 623 kfree(ename); 624 } else { 625 err = p9pdu_readf(&req->rc, c->proto_version, "d", &ecode); 626 err = -ecode; 627 628 p9_debug(P9_DEBUG_9P, "<<< RLERROR (%d)\n", -ecode); 629 } 630 return err; 631 632 out_err: 633 p9_debug(P9_DEBUG_ERROR, "couldn't parse error%d\n", err); 634 return err; 635 } 636 637 static struct p9_req_t * 638 p9_client_rpc(struct p9_client *c, int8_t type, const char *fmt, ...); 639 640 /** 641 * p9_client_flush - flush (cancel) a request 642 * @c: client state 643 * @oldreq: request to cancel 644 * 645 * This sents a flush for a particular request and links 646 * the flush request to the original request. The current 647 * code only supports a single flush request although the protocol 648 * allows for multiple flush requests to be sent for a single request. 649 * 650 */ 651 652 static int p9_client_flush(struct p9_client *c, struct p9_req_t *oldreq) 653 { 654 struct p9_req_t *req; 655 s16 oldtag; 656 int err; 657 658 err = p9_parse_header(&oldreq->tc, NULL, NULL, &oldtag, 1); 659 if (err) 660 return err; 661 662 p9_debug(P9_DEBUG_9P, ">>> TFLUSH tag %d\n", oldtag); 663 664 req = p9_client_rpc(c, P9_TFLUSH, "w", oldtag); 665 if (IS_ERR(req)) 666 return PTR_ERR(req); 667 668 /* if we haven't received a response for oldreq, 669 * remove it from the list 670 */ 671 if (oldreq->status == REQ_STATUS_SENT) { 672 if (c->trans_mod->cancelled) 673 c->trans_mod->cancelled(c, oldreq); 674 } 675 676 p9_tag_remove(c, req); 677 return 0; 678 } 679 680 static struct p9_req_t *p9_client_prepare_req(struct p9_client *c, 681 int8_t type, int req_size, 682 const char *fmt, va_list ap) 683 { 684 int err; 685 struct p9_req_t *req; 686 687 p9_debug(P9_DEBUG_MUX, "client %p op %d\n", c, type); 688 689 /* we allow for any status other than disconnected */ 690 if (c->status == Disconnected) 691 return ERR_PTR(-EIO); 692 693 /* if status is begin_disconnected we allow only clunk request */ 694 if (c->status == BeginDisconnect && type != P9_TCLUNK) 695 return ERR_PTR(-EIO); 696 697 req = p9_tag_alloc(c, type, req_size); 698 if (IS_ERR(req)) 699 return req; 700 701 /* marshall the data */ 702 p9pdu_prepare(&req->tc, req->tc.tag, type); 703 err = p9pdu_vwritef(&req->tc, c->proto_version, fmt, ap); 704 if (err) 705 goto reterr; 706 p9pdu_finalize(c, &req->tc); 707 trace_9p_client_req(c, type, req->tc.tag); 708 return req; 709 reterr: 710 p9_tag_remove(c, req); 711 /* We have to put also the 2nd reference as it won't be used */ 712 p9_req_put(req); 713 return ERR_PTR(err); 714 } 715 716 /** 717 * p9_client_rpc - issue a request and wait for a response 718 * @c: client session 719 * @type: type of request 720 * @fmt: protocol format string (see protocol.c) 721 * 722 * Returns request structure (which client must free using p9_tag_remove) 723 */ 724 725 static struct p9_req_t * 726 p9_client_rpc(struct p9_client *c, int8_t type, const char *fmt, ...) 727 { 728 va_list ap; 729 int sigpending, err; 730 unsigned long flags; 731 struct p9_req_t *req; 732 733 va_start(ap, fmt); 734 req = p9_client_prepare_req(c, type, c->msize, fmt, ap); 735 va_end(ap); 736 if (IS_ERR(req)) 737 return req; 738 739 if (signal_pending(current)) { 740 sigpending = 1; 741 clear_thread_flag(TIF_SIGPENDING); 742 } else { 743 sigpending = 0; 744 } 745 746 err = c->trans_mod->request(c, req); 747 if (err < 0) { 748 /* write won't happen */ 749 p9_req_put(req); 750 if (err != -ERESTARTSYS && err != -EFAULT) 751 c->status = Disconnected; 752 goto recalc_sigpending; 753 } 754 again: 755 /* Wait for the response */ 756 err = wait_event_killable(req->wq, req->status >= REQ_STATUS_RCVD); 757 758 /* Make sure our req is coherent with regard to updates in other 759 * threads - echoes to wmb() in the callback 760 */ 761 smp_rmb(); 762 763 if (err == -ERESTARTSYS && c->status == Connected && 764 type == P9_TFLUSH) { 765 sigpending = 1; 766 clear_thread_flag(TIF_SIGPENDING); 767 goto again; 768 } 769 770 if (req->status == REQ_STATUS_ERROR) { 771 p9_debug(P9_DEBUG_ERROR, "req_status error %d\n", req->t_err); 772 err = req->t_err; 773 } 774 if (err == -ERESTARTSYS && c->status == Connected) { 775 p9_debug(P9_DEBUG_MUX, "flushing\n"); 776 sigpending = 1; 777 clear_thread_flag(TIF_SIGPENDING); 778 779 if (c->trans_mod->cancel(c, req)) 780 p9_client_flush(c, req); 781 782 /* if we received the response anyway, don't signal error */ 783 if (req->status == REQ_STATUS_RCVD) 784 err = 0; 785 } 786 recalc_sigpending: 787 if (sigpending) { 788 spin_lock_irqsave(¤t->sighand->siglock, flags); 789 recalc_sigpending(); 790 spin_unlock_irqrestore(¤t->sighand->siglock, flags); 791 } 792 if (err < 0) 793 goto reterr; 794 795 err = p9_check_errors(c, req); 796 trace_9p_client_res(c, type, req->rc.tag, err); 797 if (!err) 798 return req; 799 reterr: 800 p9_tag_remove(c, req); 801 return ERR_PTR(safe_errno(err)); 802 } 803 804 /** 805 * p9_client_zc_rpc - issue a request and wait for a response 806 * @c: client session 807 * @type: type of request 808 * @uidata: destination for zero copy read 809 * @uodata: source for zero copy write 810 * @inlen: read buffer size 811 * @olen: write buffer size 812 * @in_hdrlen: reader header size, This is the size of response protocol data 813 * @fmt: protocol format string (see protocol.c) 814 * 815 * Returns request structure (which client must free using p9_tag_remove) 816 */ 817 static struct p9_req_t *p9_client_zc_rpc(struct p9_client *c, int8_t type, 818 struct iov_iter *uidata, 819 struct iov_iter *uodata, 820 int inlen, int olen, int in_hdrlen, 821 const char *fmt, ...) 822 { 823 va_list ap; 824 int sigpending, err; 825 unsigned long flags; 826 struct p9_req_t *req; 827 828 va_start(ap, fmt); 829 /* We allocate a inline protocol data of only 4k bytes. 830 * The actual content is passed in zero-copy fashion. 831 */ 832 req = p9_client_prepare_req(c, type, P9_ZC_HDR_SZ, fmt, ap); 833 va_end(ap); 834 if (IS_ERR(req)) 835 return req; 836 837 if (signal_pending(current)) { 838 sigpending = 1; 839 clear_thread_flag(TIF_SIGPENDING); 840 } else { 841 sigpending = 0; 842 } 843 844 err = c->trans_mod->zc_request(c, req, uidata, uodata, 845 inlen, olen, in_hdrlen); 846 if (err < 0) { 847 if (err == -EIO) 848 c->status = Disconnected; 849 if (err != -ERESTARTSYS) 850 goto recalc_sigpending; 851 } 852 if (req->status == REQ_STATUS_ERROR) { 853 p9_debug(P9_DEBUG_ERROR, "req_status error %d\n", req->t_err); 854 err = req->t_err; 855 } 856 if (err == -ERESTARTSYS && c->status == Connected) { 857 p9_debug(P9_DEBUG_MUX, "flushing\n"); 858 sigpending = 1; 859 clear_thread_flag(TIF_SIGPENDING); 860 861 if (c->trans_mod->cancel(c, req)) 862 p9_client_flush(c, req); 863 864 /* if we received the response anyway, don't signal error */ 865 if (req->status == REQ_STATUS_RCVD) 866 err = 0; 867 } 868 recalc_sigpending: 869 if (sigpending) { 870 spin_lock_irqsave(¤t->sighand->siglock, flags); 871 recalc_sigpending(); 872 spin_unlock_irqrestore(¤t->sighand->siglock, flags); 873 } 874 if (err < 0) 875 goto reterr; 876 877 err = p9_check_zc_errors(c, req, uidata, in_hdrlen); 878 trace_9p_client_res(c, type, req->rc.tag, err); 879 if (!err) 880 return req; 881 reterr: 882 p9_tag_remove(c, req); 883 return ERR_PTR(safe_errno(err)); 884 } 885 886 static struct p9_fid *p9_fid_create(struct p9_client *clnt) 887 { 888 int ret; 889 struct p9_fid *fid; 890 891 p9_debug(P9_DEBUG_FID, "clnt %p\n", clnt); 892 fid = kmalloc(sizeof(*fid), GFP_KERNEL); 893 if (!fid) 894 return NULL; 895 896 memset(&fid->qid, 0, sizeof(fid->qid)); 897 fid->mode = -1; 898 fid->uid = current_fsuid(); 899 fid->clnt = clnt; 900 fid->rdir = NULL; 901 fid->fid = 0; 902 refcount_set(&fid->count, 1); 903 904 idr_preload(GFP_KERNEL); 905 spin_lock_irq(&clnt->lock); 906 ret = idr_alloc_u32(&clnt->fids, fid, &fid->fid, P9_NOFID - 1, 907 GFP_NOWAIT); 908 spin_unlock_irq(&clnt->lock); 909 idr_preload_end(); 910 if (!ret) 911 return fid; 912 913 kfree(fid); 914 return NULL; 915 } 916 917 static void p9_fid_destroy(struct p9_fid *fid) 918 { 919 struct p9_client *clnt; 920 unsigned long flags; 921 922 p9_debug(P9_DEBUG_FID, "fid %d\n", fid->fid); 923 clnt = fid->clnt; 924 spin_lock_irqsave(&clnt->lock, flags); 925 idr_remove(&clnt->fids, fid->fid); 926 spin_unlock_irqrestore(&clnt->lock, flags); 927 kfree(fid->rdir); 928 kfree(fid); 929 } 930 931 static int p9_client_version(struct p9_client *c) 932 { 933 int err = 0; 934 struct p9_req_t *req; 935 char *version = NULL; 936 int msize; 937 938 p9_debug(P9_DEBUG_9P, ">>> TVERSION msize %d protocol %d\n", 939 c->msize, c->proto_version); 940 941 switch (c->proto_version) { 942 case p9_proto_2000L: 943 req = p9_client_rpc(c, P9_TVERSION, "ds", 944 c->msize, "9P2000.L"); 945 break; 946 case p9_proto_2000u: 947 req = p9_client_rpc(c, P9_TVERSION, "ds", 948 c->msize, "9P2000.u"); 949 break; 950 case p9_proto_legacy: 951 req = p9_client_rpc(c, P9_TVERSION, "ds", 952 c->msize, "9P2000"); 953 break; 954 default: 955 return -EINVAL; 956 } 957 958 if (IS_ERR(req)) 959 return PTR_ERR(req); 960 961 err = p9pdu_readf(&req->rc, c->proto_version, "ds", &msize, &version); 962 if (err) { 963 p9_debug(P9_DEBUG_9P, "version error %d\n", err); 964 trace_9p_protocol_dump(c, &req->rc); 965 goto error; 966 } 967 968 p9_debug(P9_DEBUG_9P, "<<< RVERSION msize %d %s\n", msize, version); 969 if (!strncmp(version, "9P2000.L", 8)) { 970 c->proto_version = p9_proto_2000L; 971 } else if (!strncmp(version, "9P2000.u", 8)) { 972 c->proto_version = p9_proto_2000u; 973 } else if (!strncmp(version, "9P2000", 6)) { 974 c->proto_version = p9_proto_legacy; 975 } else { 976 p9_debug(P9_DEBUG_ERROR, 977 "server returned an unknown version: %s\n", version); 978 err = -EREMOTEIO; 979 goto error; 980 } 981 982 if (msize < 4096) { 983 p9_debug(P9_DEBUG_ERROR, 984 "server returned a msize < 4096: %d\n", msize); 985 err = -EREMOTEIO; 986 goto error; 987 } 988 if (msize < c->msize) 989 c->msize = msize; 990 991 error: 992 kfree(version); 993 p9_tag_remove(c, req); 994 995 return err; 996 } 997 998 struct p9_client *p9_client_create(const char *dev_name, char *options) 999 { 1000 int err; 1001 struct p9_client *clnt; 1002 char *client_id; 1003 1004 err = 0; 1005 clnt = kmalloc(sizeof(*clnt), GFP_KERNEL); 1006 if (!clnt) 1007 return ERR_PTR(-ENOMEM); 1008 1009 clnt->trans_mod = NULL; 1010 clnt->trans = NULL; 1011 clnt->fcall_cache = NULL; 1012 1013 client_id = utsname()->nodename; 1014 memcpy(clnt->name, client_id, strlen(client_id) + 1); 1015 1016 spin_lock_init(&clnt->lock); 1017 idr_init(&clnt->fids); 1018 idr_init(&clnt->reqs); 1019 1020 err = parse_opts(options, clnt); 1021 if (err < 0) 1022 goto free_client; 1023 1024 if (!clnt->trans_mod) 1025 clnt->trans_mod = v9fs_get_default_trans(); 1026 1027 if (!clnt->trans_mod) { 1028 err = -EPROTONOSUPPORT; 1029 p9_debug(P9_DEBUG_ERROR, 1030 "No transport defined or default transport\n"); 1031 goto free_client; 1032 } 1033 1034 p9_debug(P9_DEBUG_MUX, "clnt %p trans %p msize %d protocol %d\n", 1035 clnt, clnt->trans_mod, clnt->msize, clnt->proto_version); 1036 1037 err = clnt->trans_mod->create(clnt, dev_name, options); 1038 if (err) 1039 goto put_trans; 1040 1041 if (clnt->msize > clnt->trans_mod->maxsize) 1042 clnt->msize = clnt->trans_mod->maxsize; 1043 1044 if (clnt->msize < 4096) { 1045 p9_debug(P9_DEBUG_ERROR, 1046 "Please specify a msize of at least 4k\n"); 1047 err = -EINVAL; 1048 goto close_trans; 1049 } 1050 1051 err = p9_client_version(clnt); 1052 if (err) 1053 goto close_trans; 1054 1055 /* P9_HDRSZ + 4 is the smallest packet header we can have that is 1056 * followed by data accessed from userspace by read 1057 */ 1058 clnt->fcall_cache = 1059 kmem_cache_create_usercopy("9p-fcall-cache", clnt->msize, 1060 0, 0, P9_HDRSZ + 4, 1061 clnt->msize - (P9_HDRSZ + 4), 1062 NULL); 1063 1064 return clnt; 1065 1066 close_trans: 1067 clnt->trans_mod->close(clnt); 1068 put_trans: 1069 v9fs_put_trans(clnt->trans_mod); 1070 free_client: 1071 kfree(clnt); 1072 return ERR_PTR(err); 1073 } 1074 EXPORT_SYMBOL(p9_client_create); 1075 1076 void p9_client_destroy(struct p9_client *clnt) 1077 { 1078 struct p9_fid *fid; 1079 int id; 1080 1081 p9_debug(P9_DEBUG_MUX, "clnt %p\n", clnt); 1082 1083 if (clnt->trans_mod) 1084 clnt->trans_mod->close(clnt); 1085 1086 v9fs_put_trans(clnt->trans_mod); 1087 1088 idr_for_each_entry(&clnt->fids, fid, id) { 1089 pr_info("Found fid %d not clunked\n", fid->fid); 1090 p9_fid_destroy(fid); 1091 } 1092 1093 p9_tag_cleanup(clnt); 1094 1095 kmem_cache_destroy(clnt->fcall_cache); 1096 kfree(clnt); 1097 } 1098 EXPORT_SYMBOL(p9_client_destroy); 1099 1100 void p9_client_disconnect(struct p9_client *clnt) 1101 { 1102 p9_debug(P9_DEBUG_9P, "clnt %p\n", clnt); 1103 clnt->status = Disconnected; 1104 } 1105 EXPORT_SYMBOL(p9_client_disconnect); 1106 1107 void p9_client_begin_disconnect(struct p9_client *clnt) 1108 { 1109 p9_debug(P9_DEBUG_9P, "clnt %p\n", clnt); 1110 clnt->status = BeginDisconnect; 1111 } 1112 EXPORT_SYMBOL(p9_client_begin_disconnect); 1113 1114 struct p9_fid *p9_client_attach(struct p9_client *clnt, struct p9_fid *afid, 1115 const char *uname, kuid_t n_uname, 1116 const char *aname) 1117 { 1118 int err = 0; 1119 struct p9_req_t *req; 1120 struct p9_fid *fid; 1121 struct p9_qid qid; 1122 1123 p9_debug(P9_DEBUG_9P, ">>> TATTACH afid %d uname %s aname %s\n", 1124 afid ? afid->fid : -1, uname, aname); 1125 fid = p9_fid_create(clnt); 1126 if (!fid) { 1127 err = -ENOMEM; 1128 goto error; 1129 } 1130 fid->uid = n_uname; 1131 1132 req = p9_client_rpc(clnt, P9_TATTACH, "ddss?u", fid->fid, 1133 afid ? afid->fid : P9_NOFID, uname, aname, n_uname); 1134 if (IS_ERR(req)) { 1135 err = PTR_ERR(req); 1136 goto error; 1137 } 1138 1139 err = p9pdu_readf(&req->rc, clnt->proto_version, "Q", &qid); 1140 if (err) { 1141 trace_9p_protocol_dump(clnt, &req->rc); 1142 p9_tag_remove(clnt, req); 1143 goto error; 1144 } 1145 1146 p9_debug(P9_DEBUG_9P, "<<< RATTACH qid %x.%llx.%x\n", 1147 qid.type, qid.path, qid.version); 1148 1149 memmove(&fid->qid, &qid, sizeof(struct p9_qid)); 1150 1151 p9_tag_remove(clnt, req); 1152 return fid; 1153 1154 error: 1155 if (fid) 1156 p9_fid_destroy(fid); 1157 return ERR_PTR(err); 1158 } 1159 EXPORT_SYMBOL(p9_client_attach); 1160 1161 struct p9_fid *p9_client_walk(struct p9_fid *oldfid, uint16_t nwname, 1162 const unsigned char * const *wnames, int clone) 1163 { 1164 int err; 1165 struct p9_client *clnt; 1166 struct p9_fid *fid; 1167 struct p9_qid *wqids; 1168 struct p9_req_t *req; 1169 u16 nwqids, count; 1170 1171 err = 0; 1172 wqids = NULL; 1173 clnt = oldfid->clnt; 1174 if (clone) { 1175 fid = p9_fid_create(clnt); 1176 if (!fid) { 1177 err = -ENOMEM; 1178 goto error; 1179 } 1180 1181 fid->uid = oldfid->uid; 1182 } else { 1183 fid = oldfid; 1184 } 1185 1186 p9_debug(P9_DEBUG_9P, ">>> TWALK fids %d,%d nwname %ud wname[0] %s\n", 1187 oldfid->fid, fid->fid, nwname, wnames ? wnames[0] : NULL); 1188 req = p9_client_rpc(clnt, P9_TWALK, "ddT", oldfid->fid, fid->fid, 1189 nwname, wnames); 1190 if (IS_ERR(req)) { 1191 err = PTR_ERR(req); 1192 goto error; 1193 } 1194 1195 err = p9pdu_readf(&req->rc, clnt->proto_version, "R", &nwqids, &wqids); 1196 if (err) { 1197 trace_9p_protocol_dump(clnt, &req->rc); 1198 p9_tag_remove(clnt, req); 1199 goto clunk_fid; 1200 } 1201 p9_tag_remove(clnt, req); 1202 1203 p9_debug(P9_DEBUG_9P, "<<< RWALK nwqid %d:\n", nwqids); 1204 1205 if (nwqids != nwname) { 1206 err = -ENOENT; 1207 goto clunk_fid; 1208 } 1209 1210 for (count = 0; count < nwqids; count++) 1211 p9_debug(P9_DEBUG_9P, "<<< [%d] %x.%llx.%x\n", 1212 count, wqids[count].type, 1213 wqids[count].path, 1214 wqids[count].version); 1215 1216 if (nwname) 1217 memmove(&fid->qid, &wqids[nwqids - 1], sizeof(struct p9_qid)); 1218 else 1219 memmove(&fid->qid, &oldfid->qid, sizeof(struct p9_qid)); 1220 1221 kfree(wqids); 1222 return fid; 1223 1224 clunk_fid: 1225 kfree(wqids); 1226 p9_client_clunk(fid); 1227 fid = NULL; 1228 1229 error: 1230 if (fid && fid != oldfid) 1231 p9_fid_destroy(fid); 1232 1233 return ERR_PTR(err); 1234 } 1235 EXPORT_SYMBOL(p9_client_walk); 1236 1237 int p9_client_open(struct p9_fid *fid, int mode) 1238 { 1239 int err; 1240 struct p9_client *clnt; 1241 struct p9_req_t *req; 1242 struct p9_qid qid; 1243 int iounit; 1244 1245 clnt = fid->clnt; 1246 p9_debug(P9_DEBUG_9P, ">>> %s fid %d mode %d\n", 1247 p9_is_proto_dotl(clnt) ? "TLOPEN" : "TOPEN", fid->fid, mode); 1248 err = 0; 1249 1250 if (fid->mode != -1) 1251 return -EINVAL; 1252 1253 if (p9_is_proto_dotl(clnt)) 1254 req = p9_client_rpc(clnt, P9_TLOPEN, "dd", fid->fid, mode); 1255 else 1256 req = p9_client_rpc(clnt, P9_TOPEN, "db", fid->fid, mode); 1257 if (IS_ERR(req)) { 1258 err = PTR_ERR(req); 1259 goto error; 1260 } 1261 1262 err = p9pdu_readf(&req->rc, clnt->proto_version, "Qd", &qid, &iounit); 1263 if (err) { 1264 trace_9p_protocol_dump(clnt, &req->rc); 1265 goto free_and_error; 1266 } 1267 1268 p9_debug(P9_DEBUG_9P, "<<< %s qid %x.%llx.%x iounit %x\n", 1269 p9_is_proto_dotl(clnt) ? "RLOPEN" : "ROPEN", qid.type, 1270 qid.path, qid.version, iounit); 1271 1272 memmove(&fid->qid, &qid, sizeof(struct p9_qid)); 1273 fid->mode = mode; 1274 fid->iounit = iounit; 1275 1276 free_and_error: 1277 p9_tag_remove(clnt, req); 1278 error: 1279 return err; 1280 } 1281 EXPORT_SYMBOL(p9_client_open); 1282 1283 int p9_client_create_dotl(struct p9_fid *ofid, const char *name, u32 flags, 1284 u32 mode, kgid_t gid, struct p9_qid *qid) 1285 { 1286 int err = 0; 1287 struct p9_client *clnt; 1288 struct p9_req_t *req; 1289 int iounit; 1290 1291 p9_debug(P9_DEBUG_9P, 1292 ">>> TLCREATE fid %d name %s flags %d mode %d gid %d\n", 1293 ofid->fid, name, flags, mode, 1294 from_kgid(&init_user_ns, gid)); 1295 clnt = ofid->clnt; 1296 1297 if (ofid->mode != -1) 1298 return -EINVAL; 1299 1300 req = p9_client_rpc(clnt, P9_TLCREATE, "dsddg", ofid->fid, name, flags, 1301 mode, gid); 1302 if (IS_ERR(req)) { 1303 err = PTR_ERR(req); 1304 goto error; 1305 } 1306 1307 err = p9pdu_readf(&req->rc, clnt->proto_version, "Qd", qid, &iounit); 1308 if (err) { 1309 trace_9p_protocol_dump(clnt, &req->rc); 1310 goto free_and_error; 1311 } 1312 1313 p9_debug(P9_DEBUG_9P, "<<< RLCREATE qid %x.%llx.%x iounit %x\n", 1314 qid->type, qid->path, qid->version, iounit); 1315 1316 memmove(&ofid->qid, qid, sizeof(struct p9_qid)); 1317 ofid->mode = mode; 1318 ofid->iounit = iounit; 1319 1320 free_and_error: 1321 p9_tag_remove(clnt, req); 1322 error: 1323 return err; 1324 } 1325 EXPORT_SYMBOL(p9_client_create_dotl); 1326 1327 int p9_client_fcreate(struct p9_fid *fid, const char *name, u32 perm, int mode, 1328 char *extension) 1329 { 1330 int err; 1331 struct p9_client *clnt; 1332 struct p9_req_t *req; 1333 struct p9_qid qid; 1334 int iounit; 1335 1336 p9_debug(P9_DEBUG_9P, ">>> TCREATE fid %d name %s perm %d mode %d\n", 1337 fid->fid, name, perm, mode); 1338 err = 0; 1339 clnt = fid->clnt; 1340 1341 if (fid->mode != -1) 1342 return -EINVAL; 1343 1344 req = p9_client_rpc(clnt, P9_TCREATE, "dsdb?s", fid->fid, name, perm, 1345 mode, extension); 1346 if (IS_ERR(req)) { 1347 err = PTR_ERR(req); 1348 goto error; 1349 } 1350 1351 err = p9pdu_readf(&req->rc, clnt->proto_version, "Qd", &qid, &iounit); 1352 if (err) { 1353 trace_9p_protocol_dump(clnt, &req->rc); 1354 goto free_and_error; 1355 } 1356 1357 p9_debug(P9_DEBUG_9P, "<<< RCREATE qid %x.%llx.%x iounit %x\n", 1358 qid.type, qid.path, qid.version, iounit); 1359 1360 memmove(&fid->qid, &qid, sizeof(struct p9_qid)); 1361 fid->mode = mode; 1362 fid->iounit = iounit; 1363 1364 free_and_error: 1365 p9_tag_remove(clnt, req); 1366 error: 1367 return err; 1368 } 1369 EXPORT_SYMBOL(p9_client_fcreate); 1370 1371 int p9_client_symlink(struct p9_fid *dfid, const char *name, 1372 const char *symtgt, kgid_t gid, struct p9_qid *qid) 1373 { 1374 int err = 0; 1375 struct p9_client *clnt; 1376 struct p9_req_t *req; 1377 1378 p9_debug(P9_DEBUG_9P, ">>> TSYMLINK dfid %d name %s symtgt %s\n", 1379 dfid->fid, name, symtgt); 1380 clnt = dfid->clnt; 1381 1382 req = p9_client_rpc(clnt, P9_TSYMLINK, "dssg", dfid->fid, name, symtgt, 1383 gid); 1384 if (IS_ERR(req)) { 1385 err = PTR_ERR(req); 1386 goto error; 1387 } 1388 1389 err = p9pdu_readf(&req->rc, clnt->proto_version, "Q", qid); 1390 if (err) { 1391 trace_9p_protocol_dump(clnt, &req->rc); 1392 goto free_and_error; 1393 } 1394 1395 p9_debug(P9_DEBUG_9P, "<<< RSYMLINK qid %x.%llx.%x\n", 1396 qid->type, qid->path, qid->version); 1397 1398 free_and_error: 1399 p9_tag_remove(clnt, req); 1400 error: 1401 return err; 1402 } 1403 EXPORT_SYMBOL(p9_client_symlink); 1404 1405 int p9_client_link(struct p9_fid *dfid, struct p9_fid *oldfid, const char *newname) 1406 { 1407 struct p9_client *clnt; 1408 struct p9_req_t *req; 1409 1410 p9_debug(P9_DEBUG_9P, ">>> TLINK dfid %d oldfid %d newname %s\n", 1411 dfid->fid, oldfid->fid, newname); 1412 clnt = dfid->clnt; 1413 req = p9_client_rpc(clnt, P9_TLINK, "dds", dfid->fid, oldfid->fid, 1414 newname); 1415 if (IS_ERR(req)) 1416 return PTR_ERR(req); 1417 1418 p9_debug(P9_DEBUG_9P, "<<< RLINK\n"); 1419 p9_tag_remove(clnt, req); 1420 return 0; 1421 } 1422 EXPORT_SYMBOL(p9_client_link); 1423 1424 int p9_client_fsync(struct p9_fid *fid, int datasync) 1425 { 1426 int err; 1427 struct p9_client *clnt; 1428 struct p9_req_t *req; 1429 1430 p9_debug(P9_DEBUG_9P, ">>> TFSYNC fid %d datasync:%d\n", 1431 fid->fid, datasync); 1432 err = 0; 1433 clnt = fid->clnt; 1434 1435 req = p9_client_rpc(clnt, P9_TFSYNC, "dd", fid->fid, datasync); 1436 if (IS_ERR(req)) { 1437 err = PTR_ERR(req); 1438 goto error; 1439 } 1440 1441 p9_debug(P9_DEBUG_9P, "<<< RFSYNC fid %d\n", fid->fid); 1442 1443 p9_tag_remove(clnt, req); 1444 1445 error: 1446 return err; 1447 } 1448 EXPORT_SYMBOL(p9_client_fsync); 1449 1450 int p9_client_clunk(struct p9_fid *fid) 1451 { 1452 int err; 1453 struct p9_client *clnt; 1454 struct p9_req_t *req; 1455 int retries = 0; 1456 1457 if (!fid || IS_ERR(fid)) { 1458 pr_warn("%s (%d): Trying to clunk with invalid fid\n", 1459 __func__, task_pid_nr(current)); 1460 dump_stack(); 1461 return 0; 1462 } 1463 if (!refcount_dec_and_test(&fid->count)) 1464 return 0; 1465 1466 again: 1467 p9_debug(P9_DEBUG_9P, ">>> TCLUNK fid %d (try %d)\n", 1468 fid->fid, retries); 1469 err = 0; 1470 clnt = fid->clnt; 1471 1472 req = p9_client_rpc(clnt, P9_TCLUNK, "d", fid->fid); 1473 if (IS_ERR(req)) { 1474 err = PTR_ERR(req); 1475 goto error; 1476 } 1477 1478 p9_debug(P9_DEBUG_9P, "<<< RCLUNK fid %d\n", fid->fid); 1479 1480 p9_tag_remove(clnt, req); 1481 error: 1482 /* Fid is not valid even after a failed clunk 1483 * If interrupted, retry once then give up and 1484 * leak fid until umount. 1485 */ 1486 if (err == -ERESTARTSYS) { 1487 if (retries++ == 0) 1488 goto again; 1489 } else { 1490 p9_fid_destroy(fid); 1491 } 1492 return err; 1493 } 1494 EXPORT_SYMBOL(p9_client_clunk); 1495 1496 int p9_client_remove(struct p9_fid *fid) 1497 { 1498 int err; 1499 struct p9_client *clnt; 1500 struct p9_req_t *req; 1501 1502 p9_debug(P9_DEBUG_9P, ">>> TREMOVE fid %d\n", fid->fid); 1503 err = 0; 1504 clnt = fid->clnt; 1505 1506 req = p9_client_rpc(clnt, P9_TREMOVE, "d", fid->fid); 1507 if (IS_ERR(req)) { 1508 err = PTR_ERR(req); 1509 goto error; 1510 } 1511 1512 p9_debug(P9_DEBUG_9P, "<<< RREMOVE fid %d\n", fid->fid); 1513 1514 p9_tag_remove(clnt, req); 1515 error: 1516 if (err == -ERESTARTSYS) 1517 p9_client_clunk(fid); 1518 else 1519 p9_fid_destroy(fid); 1520 return err; 1521 } 1522 EXPORT_SYMBOL(p9_client_remove); 1523 1524 int p9_client_unlinkat(struct p9_fid *dfid, const char *name, int flags) 1525 { 1526 int err = 0; 1527 struct p9_req_t *req; 1528 struct p9_client *clnt; 1529 1530 p9_debug(P9_DEBUG_9P, ">>> TUNLINKAT fid %d %s %d\n", 1531 dfid->fid, name, flags); 1532 1533 clnt = dfid->clnt; 1534 req = p9_client_rpc(clnt, P9_TUNLINKAT, "dsd", dfid->fid, name, flags); 1535 if (IS_ERR(req)) { 1536 err = PTR_ERR(req); 1537 goto error; 1538 } 1539 p9_debug(P9_DEBUG_9P, "<<< RUNLINKAT fid %d %s\n", dfid->fid, name); 1540 1541 p9_tag_remove(clnt, req); 1542 error: 1543 return err; 1544 } 1545 EXPORT_SYMBOL(p9_client_unlinkat); 1546 1547 int 1548 p9_client_read(struct p9_fid *fid, u64 offset, struct iov_iter *to, int *err) 1549 { 1550 int total = 0; 1551 *err = 0; 1552 1553 while (iov_iter_count(to)) { 1554 int count; 1555 1556 count = p9_client_read_once(fid, offset, to, err); 1557 if (!count || *err) 1558 break; 1559 offset += count; 1560 total += count; 1561 } 1562 return total; 1563 } 1564 EXPORT_SYMBOL(p9_client_read); 1565 1566 int 1567 p9_client_read_once(struct p9_fid *fid, u64 offset, struct iov_iter *to, 1568 int *err) 1569 { 1570 struct p9_client *clnt = fid->clnt; 1571 struct p9_req_t *req; 1572 int count = iov_iter_count(to); 1573 int rsize, non_zc = 0; 1574 char *dataptr; 1575 1576 *err = 0; 1577 p9_debug(P9_DEBUG_9P, ">>> TREAD fid %d offset %llu %zu\n", 1578 fid->fid, offset, iov_iter_count(to)); 1579 1580 rsize = fid->iounit; 1581 if (!rsize || rsize > clnt->msize - P9_IOHDRSZ) 1582 rsize = clnt->msize - P9_IOHDRSZ; 1583 1584 if (count < rsize) 1585 rsize = count; 1586 1587 /* Don't bother zerocopy for small IO (< 1024) */ 1588 if (clnt->trans_mod->zc_request && rsize > 1024) { 1589 /* response header len is 11 1590 * PDU Header(7) + IO Size (4) 1591 */ 1592 req = p9_client_zc_rpc(clnt, P9_TREAD, to, NULL, rsize, 1593 0, 11, "dqd", fid->fid, 1594 offset, rsize); 1595 } else { 1596 non_zc = 1; 1597 req = p9_client_rpc(clnt, P9_TREAD, "dqd", fid->fid, offset, 1598 rsize); 1599 } 1600 if (IS_ERR(req)) { 1601 *err = PTR_ERR(req); 1602 return 0; 1603 } 1604 1605 *err = p9pdu_readf(&req->rc, clnt->proto_version, 1606 "D", &count, &dataptr); 1607 if (*err) { 1608 trace_9p_protocol_dump(clnt, &req->rc); 1609 p9_tag_remove(clnt, req); 1610 return 0; 1611 } 1612 if (rsize < count) { 1613 pr_err("bogus RREAD count (%d > %d)\n", count, rsize); 1614 count = rsize; 1615 } 1616 1617 p9_debug(P9_DEBUG_9P, "<<< RREAD count %d\n", count); 1618 1619 if (non_zc) { 1620 int n = copy_to_iter(dataptr, count, to); 1621 1622 if (n != count) { 1623 *err = -EFAULT; 1624 p9_tag_remove(clnt, req); 1625 return n; 1626 } 1627 } else { 1628 iov_iter_advance(to, count); 1629 } 1630 p9_tag_remove(clnt, req); 1631 return count; 1632 } 1633 EXPORT_SYMBOL(p9_client_read_once); 1634 1635 int 1636 p9_client_write(struct p9_fid *fid, u64 offset, struct iov_iter *from, int *err) 1637 { 1638 struct p9_client *clnt = fid->clnt; 1639 struct p9_req_t *req; 1640 int total = 0; 1641 *err = 0; 1642 1643 p9_debug(P9_DEBUG_9P, ">>> TWRITE fid %d offset %llu count %zd\n", 1644 fid->fid, offset, iov_iter_count(from)); 1645 1646 while (iov_iter_count(from)) { 1647 int count = iov_iter_count(from); 1648 int rsize = fid->iounit; 1649 1650 if (!rsize || rsize > clnt->msize - P9_IOHDRSZ) 1651 rsize = clnt->msize - P9_IOHDRSZ; 1652 1653 if (count < rsize) 1654 rsize = count; 1655 1656 /* Don't bother zerocopy for small IO (< 1024) */ 1657 if (clnt->trans_mod->zc_request && rsize > 1024) { 1658 req = p9_client_zc_rpc(clnt, P9_TWRITE, NULL, from, 0, 1659 rsize, P9_ZC_HDR_SZ, "dqd", 1660 fid->fid, offset, rsize); 1661 } else { 1662 req = p9_client_rpc(clnt, P9_TWRITE, "dqV", fid->fid, 1663 offset, rsize, from); 1664 } 1665 if (IS_ERR(req)) { 1666 *err = PTR_ERR(req); 1667 break; 1668 } 1669 1670 *err = p9pdu_readf(&req->rc, clnt->proto_version, "d", &count); 1671 if (*err) { 1672 trace_9p_protocol_dump(clnt, &req->rc); 1673 p9_tag_remove(clnt, req); 1674 break; 1675 } 1676 if (rsize < count) { 1677 pr_err("bogus RWRITE count (%d > %d)\n", count, rsize); 1678 count = rsize; 1679 } 1680 1681 p9_debug(P9_DEBUG_9P, "<<< RWRITE count %d\n", count); 1682 1683 p9_tag_remove(clnt, req); 1684 iov_iter_advance(from, count); 1685 total += count; 1686 offset += count; 1687 } 1688 return total; 1689 } 1690 EXPORT_SYMBOL(p9_client_write); 1691 1692 struct p9_wstat *p9_client_stat(struct p9_fid *fid) 1693 { 1694 int err; 1695 struct p9_client *clnt; 1696 struct p9_wstat *ret; 1697 struct p9_req_t *req; 1698 u16 ignored; 1699 1700 p9_debug(P9_DEBUG_9P, ">>> TSTAT fid %d\n", fid->fid); 1701 1702 ret = kmalloc(sizeof(*ret), GFP_KERNEL); 1703 if (!ret) 1704 return ERR_PTR(-ENOMEM); 1705 1706 err = 0; 1707 clnt = fid->clnt; 1708 1709 req = p9_client_rpc(clnt, P9_TSTAT, "d", fid->fid); 1710 if (IS_ERR(req)) { 1711 err = PTR_ERR(req); 1712 goto error; 1713 } 1714 1715 err = p9pdu_readf(&req->rc, clnt->proto_version, "wS", &ignored, ret); 1716 if (err) { 1717 trace_9p_protocol_dump(clnt, &req->rc); 1718 p9_tag_remove(clnt, req); 1719 goto error; 1720 } 1721 1722 p9_debug(P9_DEBUG_9P, 1723 "<<< RSTAT sz=%x type=%x dev=%x qid=%x.%llx.%x\n" 1724 "<<< mode=%8.8x atime=%8.8x mtime=%8.8x length=%llx\n" 1725 "<<< name=%s uid=%s gid=%s muid=%s extension=(%s)\n" 1726 "<<< uid=%d gid=%d n_muid=%d\n", 1727 ret->size, ret->type, ret->dev, ret->qid.type, ret->qid.path, 1728 ret->qid.version, ret->mode, 1729 ret->atime, ret->mtime, ret->length, 1730 ret->name, ret->uid, ret->gid, ret->muid, ret->extension, 1731 from_kuid(&init_user_ns, ret->n_uid), 1732 from_kgid(&init_user_ns, ret->n_gid), 1733 from_kuid(&init_user_ns, ret->n_muid)); 1734 1735 p9_tag_remove(clnt, req); 1736 return ret; 1737 1738 error: 1739 kfree(ret); 1740 return ERR_PTR(err); 1741 } 1742 EXPORT_SYMBOL(p9_client_stat); 1743 1744 struct p9_stat_dotl *p9_client_getattr_dotl(struct p9_fid *fid, 1745 u64 request_mask) 1746 { 1747 int err; 1748 struct p9_client *clnt; 1749 struct p9_stat_dotl *ret; 1750 struct p9_req_t *req; 1751 1752 p9_debug(P9_DEBUG_9P, ">>> TGETATTR fid %d, request_mask %lld\n", 1753 fid->fid, request_mask); 1754 1755 ret = kmalloc(sizeof(*ret), GFP_KERNEL); 1756 if (!ret) 1757 return ERR_PTR(-ENOMEM); 1758 1759 err = 0; 1760 clnt = fid->clnt; 1761 1762 req = p9_client_rpc(clnt, P9_TGETATTR, "dq", fid->fid, request_mask); 1763 if (IS_ERR(req)) { 1764 err = PTR_ERR(req); 1765 goto error; 1766 } 1767 1768 err = p9pdu_readf(&req->rc, clnt->proto_version, "A", ret); 1769 if (err) { 1770 trace_9p_protocol_dump(clnt, &req->rc); 1771 p9_tag_remove(clnt, req); 1772 goto error; 1773 } 1774 1775 p9_debug(P9_DEBUG_9P, "<<< RGETATTR st_result_mask=%lld\n" 1776 "<<< qid=%x.%llx.%x\n" 1777 "<<< st_mode=%8.8x st_nlink=%llu\n" 1778 "<<< st_uid=%d st_gid=%d\n" 1779 "<<< st_rdev=%llx st_size=%llx st_blksize=%llu st_blocks=%llu\n" 1780 "<<< st_atime_sec=%lld st_atime_nsec=%lld\n" 1781 "<<< st_mtime_sec=%lld st_mtime_nsec=%lld\n" 1782 "<<< st_ctime_sec=%lld st_ctime_nsec=%lld\n" 1783 "<<< st_btime_sec=%lld st_btime_nsec=%lld\n" 1784 "<<< st_gen=%lld st_data_version=%lld\n", 1785 ret->st_result_mask, 1786 ret->qid.type, ret->qid.path, ret->qid.version, 1787 ret->st_mode, ret->st_nlink, 1788 from_kuid(&init_user_ns, ret->st_uid), 1789 from_kgid(&init_user_ns, ret->st_gid), 1790 ret->st_rdev, ret->st_size, ret->st_blksize, ret->st_blocks, 1791 ret->st_atime_sec, ret->st_atime_nsec, 1792 ret->st_mtime_sec, ret->st_mtime_nsec, 1793 ret->st_ctime_sec, ret->st_ctime_nsec, 1794 ret->st_btime_sec, ret->st_btime_nsec, 1795 ret->st_gen, ret->st_data_version); 1796 1797 p9_tag_remove(clnt, req); 1798 return ret; 1799 1800 error: 1801 kfree(ret); 1802 return ERR_PTR(err); 1803 } 1804 EXPORT_SYMBOL(p9_client_getattr_dotl); 1805 1806 static int p9_client_statsize(struct p9_wstat *wst, int proto_version) 1807 { 1808 int ret; 1809 1810 /* NOTE: size shouldn't include its own length */ 1811 /* size[2] type[2] dev[4] qid[13] */ 1812 /* mode[4] atime[4] mtime[4] length[8]*/ 1813 /* name[s] uid[s] gid[s] muid[s] */ 1814 ret = 2 + 4 + 13 + 4 + 4 + 4 + 8 + 2 + 2 + 2 + 2; 1815 1816 if (wst->name) 1817 ret += strlen(wst->name); 1818 if (wst->uid) 1819 ret += strlen(wst->uid); 1820 if (wst->gid) 1821 ret += strlen(wst->gid); 1822 if (wst->muid) 1823 ret += strlen(wst->muid); 1824 1825 if (proto_version == p9_proto_2000u || 1826 proto_version == p9_proto_2000L) { 1827 /* extension[s] n_uid[4] n_gid[4] n_muid[4] */ 1828 ret += 2 + 4 + 4 + 4; 1829 if (wst->extension) 1830 ret += strlen(wst->extension); 1831 } 1832 1833 return ret; 1834 } 1835 1836 int p9_client_wstat(struct p9_fid *fid, struct p9_wstat *wst) 1837 { 1838 int err; 1839 struct p9_req_t *req; 1840 struct p9_client *clnt; 1841 1842 err = 0; 1843 clnt = fid->clnt; 1844 wst->size = p9_client_statsize(wst, clnt->proto_version); 1845 p9_debug(P9_DEBUG_9P, ">>> TWSTAT fid %d\n", 1846 fid->fid); 1847 p9_debug(P9_DEBUG_9P, 1848 " sz=%x type=%x dev=%x qid=%x.%llx.%x\n" 1849 " mode=%8.8x atime=%8.8x mtime=%8.8x length=%llx\n" 1850 " name=%s uid=%s gid=%s muid=%s extension=(%s)\n" 1851 " uid=%d gid=%d n_muid=%d\n", 1852 wst->size, wst->type, wst->dev, wst->qid.type, 1853 wst->qid.path, wst->qid.version, 1854 wst->mode, wst->atime, wst->mtime, wst->length, 1855 wst->name, wst->uid, wst->gid, wst->muid, wst->extension, 1856 from_kuid(&init_user_ns, wst->n_uid), 1857 from_kgid(&init_user_ns, wst->n_gid), 1858 from_kuid(&init_user_ns, wst->n_muid)); 1859 1860 req = p9_client_rpc(clnt, P9_TWSTAT, "dwS", 1861 fid->fid, wst->size + 2, wst); 1862 if (IS_ERR(req)) { 1863 err = PTR_ERR(req); 1864 goto error; 1865 } 1866 1867 p9_debug(P9_DEBUG_9P, "<<< RWSTAT fid %d\n", fid->fid); 1868 1869 p9_tag_remove(clnt, req); 1870 error: 1871 return err; 1872 } 1873 EXPORT_SYMBOL(p9_client_wstat); 1874 1875 int p9_client_setattr(struct p9_fid *fid, struct p9_iattr_dotl *p9attr) 1876 { 1877 int err; 1878 struct p9_req_t *req; 1879 struct p9_client *clnt; 1880 1881 err = 0; 1882 clnt = fid->clnt; 1883 p9_debug(P9_DEBUG_9P, ">>> TSETATTR fid %d\n", fid->fid); 1884 p9_debug(P9_DEBUG_9P, " valid=%x mode=%x uid=%d gid=%d size=%lld\n", 1885 p9attr->valid, p9attr->mode, 1886 from_kuid(&init_user_ns, p9attr->uid), 1887 from_kgid(&init_user_ns, p9attr->gid), 1888 p9attr->size); 1889 p9_debug(P9_DEBUG_9P, " atime_sec=%lld atime_nsec=%lld\n", 1890 p9attr->atime_sec, p9attr->atime_nsec); 1891 p9_debug(P9_DEBUG_9P, " mtime_sec=%lld mtime_nsec=%lld\n", 1892 p9attr->mtime_sec, p9attr->mtime_nsec); 1893 1894 req = p9_client_rpc(clnt, P9_TSETATTR, "dI", fid->fid, p9attr); 1895 1896 if (IS_ERR(req)) { 1897 err = PTR_ERR(req); 1898 goto error; 1899 } 1900 p9_debug(P9_DEBUG_9P, "<<< RSETATTR fid %d\n", fid->fid); 1901 p9_tag_remove(clnt, req); 1902 error: 1903 return err; 1904 } 1905 EXPORT_SYMBOL(p9_client_setattr); 1906 1907 int p9_client_statfs(struct p9_fid *fid, struct p9_rstatfs *sb) 1908 { 1909 int err; 1910 struct p9_req_t *req; 1911 struct p9_client *clnt; 1912 1913 err = 0; 1914 clnt = fid->clnt; 1915 1916 p9_debug(P9_DEBUG_9P, ">>> TSTATFS fid %d\n", fid->fid); 1917 1918 req = p9_client_rpc(clnt, P9_TSTATFS, "d", fid->fid); 1919 if (IS_ERR(req)) { 1920 err = PTR_ERR(req); 1921 goto error; 1922 } 1923 1924 err = p9pdu_readf(&req->rc, clnt->proto_version, "ddqqqqqqd", &sb->type, 1925 &sb->bsize, &sb->blocks, &sb->bfree, &sb->bavail, 1926 &sb->files, &sb->ffree, &sb->fsid, &sb->namelen); 1927 if (err) { 1928 trace_9p_protocol_dump(clnt, &req->rc); 1929 p9_tag_remove(clnt, req); 1930 goto error; 1931 } 1932 1933 p9_debug(P9_DEBUG_9P, 1934 "<<< RSTATFS fid %d type 0x%x bsize %u blocks %llu bfree %llu bavail %llu files %llu ffree %llu fsid %llu namelen %u\n", 1935 fid->fid, sb->type, sb->bsize, sb->blocks, sb->bfree, 1936 sb->bavail, sb->files, sb->ffree, sb->fsid, sb->namelen); 1937 1938 p9_tag_remove(clnt, req); 1939 error: 1940 return err; 1941 } 1942 EXPORT_SYMBOL(p9_client_statfs); 1943 1944 int p9_client_rename(struct p9_fid *fid, 1945 struct p9_fid *newdirfid, const char *name) 1946 { 1947 int err; 1948 struct p9_req_t *req; 1949 struct p9_client *clnt; 1950 1951 err = 0; 1952 clnt = fid->clnt; 1953 1954 p9_debug(P9_DEBUG_9P, ">>> TRENAME fid %d newdirfid %d name %s\n", 1955 fid->fid, newdirfid->fid, name); 1956 1957 req = p9_client_rpc(clnt, P9_TRENAME, "dds", fid->fid, 1958 newdirfid->fid, name); 1959 if (IS_ERR(req)) { 1960 err = PTR_ERR(req); 1961 goto error; 1962 } 1963 1964 p9_debug(P9_DEBUG_9P, "<<< RRENAME fid %d\n", fid->fid); 1965 1966 p9_tag_remove(clnt, req); 1967 error: 1968 return err; 1969 } 1970 EXPORT_SYMBOL(p9_client_rename); 1971 1972 int p9_client_renameat(struct p9_fid *olddirfid, const char *old_name, 1973 struct p9_fid *newdirfid, const char *new_name) 1974 { 1975 int err; 1976 struct p9_req_t *req; 1977 struct p9_client *clnt; 1978 1979 err = 0; 1980 clnt = olddirfid->clnt; 1981 1982 p9_debug(P9_DEBUG_9P, 1983 ">>> TRENAMEAT olddirfid %d old name %s newdirfid %d new name %s\n", 1984 olddirfid->fid, old_name, newdirfid->fid, new_name); 1985 1986 req = p9_client_rpc(clnt, P9_TRENAMEAT, "dsds", olddirfid->fid, 1987 old_name, newdirfid->fid, new_name); 1988 if (IS_ERR(req)) { 1989 err = PTR_ERR(req); 1990 goto error; 1991 } 1992 1993 p9_debug(P9_DEBUG_9P, "<<< RRENAMEAT newdirfid %d new name %s\n", 1994 newdirfid->fid, new_name); 1995 1996 p9_tag_remove(clnt, req); 1997 error: 1998 return err; 1999 } 2000 EXPORT_SYMBOL(p9_client_renameat); 2001 2002 /* An xattrwalk without @attr_name gives the fid for the lisxattr namespace 2003 */ 2004 struct p9_fid *p9_client_xattrwalk(struct p9_fid *file_fid, 2005 const char *attr_name, u64 *attr_size) 2006 { 2007 int err; 2008 struct p9_req_t *req; 2009 struct p9_client *clnt; 2010 struct p9_fid *attr_fid; 2011 2012 err = 0; 2013 clnt = file_fid->clnt; 2014 attr_fid = p9_fid_create(clnt); 2015 if (!attr_fid) { 2016 err = -ENOMEM; 2017 goto error; 2018 } 2019 p9_debug(P9_DEBUG_9P, 2020 ">>> TXATTRWALK file_fid %d, attr_fid %d name %s\n", 2021 file_fid->fid, attr_fid->fid, attr_name); 2022 2023 req = p9_client_rpc(clnt, P9_TXATTRWALK, "dds", 2024 file_fid->fid, attr_fid->fid, attr_name); 2025 if (IS_ERR(req)) { 2026 err = PTR_ERR(req); 2027 goto error; 2028 } 2029 err = p9pdu_readf(&req->rc, clnt->proto_version, "q", attr_size); 2030 if (err) { 2031 trace_9p_protocol_dump(clnt, &req->rc); 2032 p9_tag_remove(clnt, req); 2033 goto clunk_fid; 2034 } 2035 p9_tag_remove(clnt, req); 2036 p9_debug(P9_DEBUG_9P, "<<< RXATTRWALK fid %d size %llu\n", 2037 attr_fid->fid, *attr_size); 2038 return attr_fid; 2039 clunk_fid: 2040 p9_client_clunk(attr_fid); 2041 attr_fid = NULL; 2042 error: 2043 if (attr_fid && attr_fid != file_fid) 2044 p9_fid_destroy(attr_fid); 2045 2046 return ERR_PTR(err); 2047 } 2048 EXPORT_SYMBOL_GPL(p9_client_xattrwalk); 2049 2050 int p9_client_xattrcreate(struct p9_fid *fid, const char *name, 2051 u64 attr_size, int flags) 2052 { 2053 int err; 2054 struct p9_req_t *req; 2055 struct p9_client *clnt; 2056 2057 p9_debug(P9_DEBUG_9P, 2058 ">>> TXATTRCREATE fid %d name %s size %llu flag %d\n", 2059 fid->fid, name, attr_size, flags); 2060 err = 0; 2061 clnt = fid->clnt; 2062 req = p9_client_rpc(clnt, P9_TXATTRCREATE, "dsqd", 2063 fid->fid, name, attr_size, flags); 2064 if (IS_ERR(req)) { 2065 err = PTR_ERR(req); 2066 goto error; 2067 } 2068 p9_debug(P9_DEBUG_9P, "<<< RXATTRCREATE fid %d\n", fid->fid); 2069 p9_tag_remove(clnt, req); 2070 error: 2071 return err; 2072 } 2073 EXPORT_SYMBOL_GPL(p9_client_xattrcreate); 2074 2075 int p9_client_readdir(struct p9_fid *fid, char *data, u32 count, u64 offset) 2076 { 2077 int err, rsize, non_zc = 0; 2078 struct p9_client *clnt; 2079 struct p9_req_t *req; 2080 char *dataptr; 2081 struct kvec kv = {.iov_base = data, .iov_len = count}; 2082 struct iov_iter to; 2083 2084 iov_iter_kvec(&to, READ, &kv, 1, count); 2085 2086 p9_debug(P9_DEBUG_9P, ">>> TREADDIR fid %d offset %llu count %d\n", 2087 fid->fid, offset, count); 2088 2089 err = 0; 2090 clnt = fid->clnt; 2091 2092 rsize = fid->iounit; 2093 if (!rsize || rsize > clnt->msize - P9_READDIRHDRSZ) 2094 rsize = clnt->msize - P9_READDIRHDRSZ; 2095 2096 if (count < rsize) 2097 rsize = count; 2098 2099 /* Don't bother zerocopy for small IO (< 1024) */ 2100 if (clnt->trans_mod->zc_request && rsize > 1024) { 2101 /* response header len is 11 2102 * PDU Header(7) + IO Size (4) 2103 */ 2104 req = p9_client_zc_rpc(clnt, P9_TREADDIR, &to, NULL, rsize, 0, 2105 11, "dqd", fid->fid, offset, rsize); 2106 } else { 2107 non_zc = 1; 2108 req = p9_client_rpc(clnt, P9_TREADDIR, "dqd", fid->fid, 2109 offset, rsize); 2110 } 2111 if (IS_ERR(req)) { 2112 err = PTR_ERR(req); 2113 goto error; 2114 } 2115 2116 err = p9pdu_readf(&req->rc, clnt->proto_version, "D", &count, &dataptr); 2117 if (err) { 2118 trace_9p_protocol_dump(clnt, &req->rc); 2119 goto free_and_error; 2120 } 2121 if (rsize < count) { 2122 pr_err("bogus RREADDIR count (%d > %d)\n", count, rsize); 2123 count = rsize; 2124 } 2125 2126 p9_debug(P9_DEBUG_9P, "<<< RREADDIR count %d\n", count); 2127 2128 if (non_zc) 2129 memmove(data, dataptr, count); 2130 2131 p9_tag_remove(clnt, req); 2132 return count; 2133 2134 free_and_error: 2135 p9_tag_remove(clnt, req); 2136 error: 2137 return err; 2138 } 2139 EXPORT_SYMBOL(p9_client_readdir); 2140 2141 int p9_client_mknod_dotl(struct p9_fid *fid, const char *name, int mode, 2142 dev_t rdev, kgid_t gid, struct p9_qid *qid) 2143 { 2144 int err; 2145 struct p9_client *clnt; 2146 struct p9_req_t *req; 2147 2148 err = 0; 2149 clnt = fid->clnt; 2150 p9_debug(P9_DEBUG_9P, 2151 ">>> TMKNOD fid %d name %s mode %d major %d minor %d\n", 2152 fid->fid, name, mode, MAJOR(rdev), MINOR(rdev)); 2153 req = p9_client_rpc(clnt, P9_TMKNOD, "dsdddg", fid->fid, name, mode, 2154 MAJOR(rdev), MINOR(rdev), gid); 2155 if (IS_ERR(req)) 2156 return PTR_ERR(req); 2157 2158 err = p9pdu_readf(&req->rc, clnt->proto_version, "Q", qid); 2159 if (err) { 2160 trace_9p_protocol_dump(clnt, &req->rc); 2161 goto error; 2162 } 2163 p9_debug(P9_DEBUG_9P, "<<< RMKNOD qid %x.%llx.%x\n", 2164 qid->type, qid->path, qid->version); 2165 2166 error: 2167 p9_tag_remove(clnt, req); 2168 return err; 2169 } 2170 EXPORT_SYMBOL(p9_client_mknod_dotl); 2171 2172 int p9_client_mkdir_dotl(struct p9_fid *fid, const char *name, int mode, 2173 kgid_t gid, struct p9_qid *qid) 2174 { 2175 int err; 2176 struct p9_client *clnt; 2177 struct p9_req_t *req; 2178 2179 err = 0; 2180 clnt = fid->clnt; 2181 p9_debug(P9_DEBUG_9P, ">>> TMKDIR fid %d name %s mode %d gid %d\n", 2182 fid->fid, name, mode, from_kgid(&init_user_ns, gid)); 2183 req = p9_client_rpc(clnt, P9_TMKDIR, "dsdg", 2184 fid->fid, name, mode, gid); 2185 if (IS_ERR(req)) 2186 return PTR_ERR(req); 2187 2188 err = p9pdu_readf(&req->rc, clnt->proto_version, "Q", qid); 2189 if (err) { 2190 trace_9p_protocol_dump(clnt, &req->rc); 2191 goto error; 2192 } 2193 p9_debug(P9_DEBUG_9P, "<<< RMKDIR qid %x.%llx.%x\n", qid->type, 2194 qid->path, qid->version); 2195 2196 error: 2197 p9_tag_remove(clnt, req); 2198 return err; 2199 } 2200 EXPORT_SYMBOL(p9_client_mkdir_dotl); 2201 2202 int p9_client_lock_dotl(struct p9_fid *fid, struct p9_flock *flock, u8 *status) 2203 { 2204 int err; 2205 struct p9_client *clnt; 2206 struct p9_req_t *req; 2207 2208 err = 0; 2209 clnt = fid->clnt; 2210 p9_debug(P9_DEBUG_9P, 2211 ">>> TLOCK fid %d type %i flags %d start %lld length %lld proc_id %d client_id %s\n", 2212 fid->fid, flock->type, flock->flags, flock->start, 2213 flock->length, flock->proc_id, flock->client_id); 2214 2215 req = p9_client_rpc(clnt, P9_TLOCK, "dbdqqds", fid->fid, flock->type, 2216 flock->flags, flock->start, flock->length, 2217 flock->proc_id, flock->client_id); 2218 2219 if (IS_ERR(req)) 2220 return PTR_ERR(req); 2221 2222 err = p9pdu_readf(&req->rc, clnt->proto_version, "b", status); 2223 if (err) { 2224 trace_9p_protocol_dump(clnt, &req->rc); 2225 goto error; 2226 } 2227 p9_debug(P9_DEBUG_9P, "<<< RLOCK status %i\n", *status); 2228 error: 2229 p9_tag_remove(clnt, req); 2230 return err; 2231 } 2232 EXPORT_SYMBOL(p9_client_lock_dotl); 2233 2234 int p9_client_getlock_dotl(struct p9_fid *fid, struct p9_getlock *glock) 2235 { 2236 int err; 2237 struct p9_client *clnt; 2238 struct p9_req_t *req; 2239 2240 err = 0; 2241 clnt = fid->clnt; 2242 p9_debug(P9_DEBUG_9P, 2243 ">>> TGETLOCK fid %d, type %i start %lld length %lld proc_id %d client_id %s\n", 2244 fid->fid, glock->type, glock->start, glock->length, 2245 glock->proc_id, glock->client_id); 2246 2247 req = p9_client_rpc(clnt, P9_TGETLOCK, "dbqqds", fid->fid, 2248 glock->type, glock->start, glock->length, 2249 glock->proc_id, glock->client_id); 2250 2251 if (IS_ERR(req)) 2252 return PTR_ERR(req); 2253 2254 err = p9pdu_readf(&req->rc, clnt->proto_version, "bqqds", &glock->type, 2255 &glock->start, &glock->length, &glock->proc_id, 2256 &glock->client_id); 2257 if (err) { 2258 trace_9p_protocol_dump(clnt, &req->rc); 2259 goto error; 2260 } 2261 p9_debug(P9_DEBUG_9P, 2262 "<<< RGETLOCK type %i start %lld length %lld proc_id %d client_id %s\n", 2263 glock->type, glock->start, glock->length, 2264 glock->proc_id, glock->client_id); 2265 error: 2266 p9_tag_remove(clnt, req); 2267 return err; 2268 } 2269 EXPORT_SYMBOL(p9_client_getlock_dotl); 2270 2271 int p9_client_readlink(struct p9_fid *fid, char **target) 2272 { 2273 int err; 2274 struct p9_client *clnt; 2275 struct p9_req_t *req; 2276 2277 err = 0; 2278 clnt = fid->clnt; 2279 p9_debug(P9_DEBUG_9P, ">>> TREADLINK fid %d\n", fid->fid); 2280 2281 req = p9_client_rpc(clnt, P9_TREADLINK, "d", fid->fid); 2282 if (IS_ERR(req)) 2283 return PTR_ERR(req); 2284 2285 err = p9pdu_readf(&req->rc, clnt->proto_version, "s", target); 2286 if (err) { 2287 trace_9p_protocol_dump(clnt, &req->rc); 2288 goto error; 2289 } 2290 p9_debug(P9_DEBUG_9P, "<<< RREADLINK target %s\n", *target); 2291 error: 2292 p9_tag_remove(clnt, req); 2293 return err; 2294 } 2295 EXPORT_SYMBOL(p9_client_readlink); 2296 2297 int __init p9_client_init(void) 2298 { 2299 p9_req_cache = KMEM_CACHE(p9_req_t, SLAB_TYPESAFE_BY_RCU); 2300 return p9_req_cache ? 0 : -ENOMEM; 2301 } 2302 2303 void __exit p9_client_exit(void) 2304 { 2305 kmem_cache_destroy(p9_req_cache); 2306 } 2307