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