client.c (54186B)
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 37enum { 38 Opt_msize, 39 Opt_trans, 40 Opt_legacy, 41 Opt_version, 42 Opt_err, 43}; 44 45static 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 53inline int p9_is_proto_dotl(struct p9_client *clnt) 54{ 55 return clnt->proto_version == p9_proto_2000L; 56} 57EXPORT_SYMBOL(p9_is_proto_dotl); 58 59inline int p9_is_proto_dotu(struct p9_client *clnt) 60{ 61 return clnt->proto_version == p9_proto_2000u; 62} 63EXPORT_SYMBOL(p9_is_proto_dotu); 64 65int 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} 87EXPORT_SYMBOL(p9_show_client_options); 88 89/* Some error codes are taken directly from the server replies, 90 * make sure they are valid. 91 */ 92static 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 */ 102static 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 130static 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 214free_and_return: 215 if (ret) 216 v9fs_put_trans(clnt->trans_mod); 217 kfree(tmp_options); 218 return ret; 219} 220 221static 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 237void 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} 250EXPORT_SYMBOL(p9_fcall_fini); 251 252static 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 */ 263static struct p9_req_t * 264p9_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 312free: 313 p9_fcall_fini(&req->tc); 314 p9_fcall_fini(&req->rc); 315free_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 */ 328struct p9_req_t *p9_tag_lookup(struct p9_client *c, u16 tag) 329{ 330 struct p9_req_t *req; 331 332 rcu_read_lock(); 333again: 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} 352EXPORT_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 */ 361static 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 373static 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 382int p9_req_put(struct p9_req_t *r) 383{ 384 return kref_put(&r->refcount, p9_req_free); 385} 386EXPORT_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 */ 395static 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 */ 417void 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} 431EXPORT_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 442int 443p9_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 476rewind_and_exit: 477 if (rewind) 478 pdu->offset = offset; 479 return err; 480} 481EXPORT_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 494static 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 547out_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 566static 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 632out_err: 633 p9_debug(P9_DEBUG_ERROR, "couldn't parse error%d\n", err); 634 return err; 635} 636 637static struct p9_req_t * 638p9_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 652static 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 680static 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; 709reterr: 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 725static struct p9_req_t * 726p9_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 } 754again: 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 } 786recalc_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; 799reterr: 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 */ 817static 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 } 868recalc_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; 881reterr: 882 p9_tag_remove(c, req); 883 return ERR_PTR(safe_errno(err)); 884} 885 886static 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 917static 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 931static 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 991error: 992 kfree(version); 993 p9_tag_remove(c, req); 994 995 return err; 996} 997 998struct 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 pr_info("Limiting 'msize' to %d as this is the maximum " 1044 "supported by transport %s\n", 1045 clnt->msize, clnt->trans_mod->name 1046 ); 1047 } 1048 1049 if (clnt->msize < 4096) { 1050 p9_debug(P9_DEBUG_ERROR, 1051 "Please specify a msize of at least 4k\n"); 1052 err = -EINVAL; 1053 goto close_trans; 1054 } 1055 1056 err = p9_client_version(clnt); 1057 if (err) 1058 goto close_trans; 1059 1060 /* P9_HDRSZ + 4 is the smallest packet header we can have that is 1061 * followed by data accessed from userspace by read 1062 */ 1063 clnt->fcall_cache = 1064 kmem_cache_create_usercopy("9p-fcall-cache", clnt->msize, 1065 0, 0, P9_HDRSZ + 4, 1066 clnt->msize - (P9_HDRSZ + 4), 1067 NULL); 1068 1069 return clnt; 1070 1071close_trans: 1072 clnt->trans_mod->close(clnt); 1073put_trans: 1074 v9fs_put_trans(clnt->trans_mod); 1075free_client: 1076 kfree(clnt); 1077 return ERR_PTR(err); 1078} 1079EXPORT_SYMBOL(p9_client_create); 1080 1081void p9_client_destroy(struct p9_client *clnt) 1082{ 1083 struct p9_fid *fid; 1084 int id; 1085 1086 p9_debug(P9_DEBUG_MUX, "clnt %p\n", clnt); 1087 1088 if (clnt->trans_mod) 1089 clnt->trans_mod->close(clnt); 1090 1091 v9fs_put_trans(clnt->trans_mod); 1092 1093 idr_for_each_entry(&clnt->fids, fid, id) { 1094 pr_info("Found fid %d not clunked\n", fid->fid); 1095 p9_fid_destroy(fid); 1096 } 1097 1098 p9_tag_cleanup(clnt); 1099 1100 kmem_cache_destroy(clnt->fcall_cache); 1101 kfree(clnt); 1102} 1103EXPORT_SYMBOL(p9_client_destroy); 1104 1105void p9_client_disconnect(struct p9_client *clnt) 1106{ 1107 p9_debug(P9_DEBUG_9P, "clnt %p\n", clnt); 1108 clnt->status = Disconnected; 1109} 1110EXPORT_SYMBOL(p9_client_disconnect); 1111 1112void p9_client_begin_disconnect(struct p9_client *clnt) 1113{ 1114 p9_debug(P9_DEBUG_9P, "clnt %p\n", clnt); 1115 clnt->status = BeginDisconnect; 1116} 1117EXPORT_SYMBOL(p9_client_begin_disconnect); 1118 1119struct p9_fid *p9_client_attach(struct p9_client *clnt, struct p9_fid *afid, 1120 const char *uname, kuid_t n_uname, 1121 const char *aname) 1122{ 1123 int err = 0; 1124 struct p9_req_t *req; 1125 struct p9_fid *fid; 1126 struct p9_qid qid; 1127 1128 p9_debug(P9_DEBUG_9P, ">>> TATTACH afid %d uname %s aname %s\n", 1129 afid ? afid->fid : -1, uname, aname); 1130 fid = p9_fid_create(clnt); 1131 if (!fid) { 1132 err = -ENOMEM; 1133 goto error; 1134 } 1135 fid->uid = n_uname; 1136 1137 req = p9_client_rpc(clnt, P9_TATTACH, "ddss?u", fid->fid, 1138 afid ? afid->fid : P9_NOFID, uname, aname, n_uname); 1139 if (IS_ERR(req)) { 1140 err = PTR_ERR(req); 1141 goto error; 1142 } 1143 1144 err = p9pdu_readf(&req->rc, clnt->proto_version, "Q", &qid); 1145 if (err) { 1146 trace_9p_protocol_dump(clnt, &req->rc); 1147 p9_tag_remove(clnt, req); 1148 goto error; 1149 } 1150 1151 p9_debug(P9_DEBUG_9P, "<<< RATTACH qid %x.%llx.%x\n", 1152 qid.type, qid.path, qid.version); 1153 1154 memmove(&fid->qid, &qid, sizeof(struct p9_qid)); 1155 1156 p9_tag_remove(clnt, req); 1157 return fid; 1158 1159error: 1160 if (fid) 1161 p9_fid_destroy(fid); 1162 return ERR_PTR(err); 1163} 1164EXPORT_SYMBOL(p9_client_attach); 1165 1166struct p9_fid *p9_client_walk(struct p9_fid *oldfid, uint16_t nwname, 1167 const unsigned char * const *wnames, int clone) 1168{ 1169 int err; 1170 struct p9_client *clnt; 1171 struct p9_fid *fid; 1172 struct p9_qid *wqids; 1173 struct p9_req_t *req; 1174 u16 nwqids, count; 1175 1176 err = 0; 1177 wqids = NULL; 1178 clnt = oldfid->clnt; 1179 if (clone) { 1180 fid = p9_fid_create(clnt); 1181 if (!fid) { 1182 err = -ENOMEM; 1183 goto error; 1184 } 1185 1186 fid->uid = oldfid->uid; 1187 } else { 1188 fid = oldfid; 1189 } 1190 1191 p9_debug(P9_DEBUG_9P, ">>> TWALK fids %d,%d nwname %ud wname[0] %s\n", 1192 oldfid->fid, fid->fid, nwname, wnames ? wnames[0] : NULL); 1193 req = p9_client_rpc(clnt, P9_TWALK, "ddT", oldfid->fid, fid->fid, 1194 nwname, wnames); 1195 if (IS_ERR(req)) { 1196 err = PTR_ERR(req); 1197 goto error; 1198 } 1199 1200 err = p9pdu_readf(&req->rc, clnt->proto_version, "R", &nwqids, &wqids); 1201 if (err) { 1202 trace_9p_protocol_dump(clnt, &req->rc); 1203 p9_tag_remove(clnt, req); 1204 goto clunk_fid; 1205 } 1206 p9_tag_remove(clnt, req); 1207 1208 p9_debug(P9_DEBUG_9P, "<<< RWALK nwqid %d:\n", nwqids); 1209 1210 if (nwqids != nwname) { 1211 err = -ENOENT; 1212 goto clunk_fid; 1213 } 1214 1215 for (count = 0; count < nwqids; count++) 1216 p9_debug(P9_DEBUG_9P, "<<< [%d] %x.%llx.%x\n", 1217 count, wqids[count].type, 1218 wqids[count].path, 1219 wqids[count].version); 1220 1221 if (nwname) 1222 memmove(&fid->qid, &wqids[nwqids - 1], sizeof(struct p9_qid)); 1223 else 1224 memmove(&fid->qid, &oldfid->qid, sizeof(struct p9_qid)); 1225 1226 kfree(wqids); 1227 return fid; 1228 1229clunk_fid: 1230 kfree(wqids); 1231 p9_client_clunk(fid); 1232 fid = NULL; 1233 1234error: 1235 if (fid && fid != oldfid) 1236 p9_fid_destroy(fid); 1237 1238 return ERR_PTR(err); 1239} 1240EXPORT_SYMBOL(p9_client_walk); 1241 1242int p9_client_open(struct p9_fid *fid, int mode) 1243{ 1244 int err; 1245 struct p9_client *clnt; 1246 struct p9_req_t *req; 1247 struct p9_qid qid; 1248 int iounit; 1249 1250 clnt = fid->clnt; 1251 p9_debug(P9_DEBUG_9P, ">>> %s fid %d mode %d\n", 1252 p9_is_proto_dotl(clnt) ? "TLOPEN" : "TOPEN", fid->fid, mode); 1253 err = 0; 1254 1255 if (fid->mode != -1) 1256 return -EINVAL; 1257 1258 if (p9_is_proto_dotl(clnt)) 1259 req = p9_client_rpc(clnt, P9_TLOPEN, "dd", fid->fid, mode); 1260 else 1261 req = p9_client_rpc(clnt, P9_TOPEN, "db", fid->fid, mode); 1262 if (IS_ERR(req)) { 1263 err = PTR_ERR(req); 1264 goto error; 1265 } 1266 1267 err = p9pdu_readf(&req->rc, clnt->proto_version, "Qd", &qid, &iounit); 1268 if (err) { 1269 trace_9p_protocol_dump(clnt, &req->rc); 1270 goto free_and_error; 1271 } 1272 1273 p9_debug(P9_DEBUG_9P, "<<< %s qid %x.%llx.%x iounit %x\n", 1274 p9_is_proto_dotl(clnt) ? "RLOPEN" : "ROPEN", qid.type, 1275 qid.path, qid.version, iounit); 1276 1277 memmove(&fid->qid, &qid, sizeof(struct p9_qid)); 1278 fid->mode = mode; 1279 fid->iounit = iounit; 1280 1281free_and_error: 1282 p9_tag_remove(clnt, req); 1283error: 1284 return err; 1285} 1286EXPORT_SYMBOL(p9_client_open); 1287 1288int p9_client_create_dotl(struct p9_fid *ofid, const char *name, u32 flags, 1289 u32 mode, kgid_t gid, struct p9_qid *qid) 1290{ 1291 int err = 0; 1292 struct p9_client *clnt; 1293 struct p9_req_t *req; 1294 int iounit; 1295 1296 p9_debug(P9_DEBUG_9P, 1297 ">>> TLCREATE fid %d name %s flags %d mode %d gid %d\n", 1298 ofid->fid, name, flags, mode, 1299 from_kgid(&init_user_ns, gid)); 1300 clnt = ofid->clnt; 1301 1302 if (ofid->mode != -1) 1303 return -EINVAL; 1304 1305 req = p9_client_rpc(clnt, P9_TLCREATE, "dsddg", ofid->fid, name, flags, 1306 mode, gid); 1307 if (IS_ERR(req)) { 1308 err = PTR_ERR(req); 1309 goto error; 1310 } 1311 1312 err = p9pdu_readf(&req->rc, clnt->proto_version, "Qd", qid, &iounit); 1313 if (err) { 1314 trace_9p_protocol_dump(clnt, &req->rc); 1315 goto free_and_error; 1316 } 1317 1318 p9_debug(P9_DEBUG_9P, "<<< RLCREATE qid %x.%llx.%x iounit %x\n", 1319 qid->type, qid->path, qid->version, iounit); 1320 1321 memmove(&ofid->qid, qid, sizeof(struct p9_qid)); 1322 ofid->mode = mode; 1323 ofid->iounit = iounit; 1324 1325free_and_error: 1326 p9_tag_remove(clnt, req); 1327error: 1328 return err; 1329} 1330EXPORT_SYMBOL(p9_client_create_dotl); 1331 1332int p9_client_fcreate(struct p9_fid *fid, const char *name, u32 perm, int mode, 1333 char *extension) 1334{ 1335 int err; 1336 struct p9_client *clnt; 1337 struct p9_req_t *req; 1338 struct p9_qid qid; 1339 int iounit; 1340 1341 p9_debug(P9_DEBUG_9P, ">>> TCREATE fid %d name %s perm %d mode %d\n", 1342 fid->fid, name, perm, mode); 1343 err = 0; 1344 clnt = fid->clnt; 1345 1346 if (fid->mode != -1) 1347 return -EINVAL; 1348 1349 req = p9_client_rpc(clnt, P9_TCREATE, "dsdb?s", fid->fid, name, perm, 1350 mode, extension); 1351 if (IS_ERR(req)) { 1352 err = PTR_ERR(req); 1353 goto error; 1354 } 1355 1356 err = p9pdu_readf(&req->rc, clnt->proto_version, "Qd", &qid, &iounit); 1357 if (err) { 1358 trace_9p_protocol_dump(clnt, &req->rc); 1359 goto free_and_error; 1360 } 1361 1362 p9_debug(P9_DEBUG_9P, "<<< RCREATE qid %x.%llx.%x iounit %x\n", 1363 qid.type, qid.path, qid.version, iounit); 1364 1365 memmove(&fid->qid, &qid, sizeof(struct p9_qid)); 1366 fid->mode = mode; 1367 fid->iounit = iounit; 1368 1369free_and_error: 1370 p9_tag_remove(clnt, req); 1371error: 1372 return err; 1373} 1374EXPORT_SYMBOL(p9_client_fcreate); 1375 1376int p9_client_symlink(struct p9_fid *dfid, const char *name, 1377 const char *symtgt, kgid_t gid, struct p9_qid *qid) 1378{ 1379 int err = 0; 1380 struct p9_client *clnt; 1381 struct p9_req_t *req; 1382 1383 p9_debug(P9_DEBUG_9P, ">>> TSYMLINK dfid %d name %s symtgt %s\n", 1384 dfid->fid, name, symtgt); 1385 clnt = dfid->clnt; 1386 1387 req = p9_client_rpc(clnt, P9_TSYMLINK, "dssg", dfid->fid, name, symtgt, 1388 gid); 1389 if (IS_ERR(req)) { 1390 err = PTR_ERR(req); 1391 goto error; 1392 } 1393 1394 err = p9pdu_readf(&req->rc, clnt->proto_version, "Q", qid); 1395 if (err) { 1396 trace_9p_protocol_dump(clnt, &req->rc); 1397 goto free_and_error; 1398 } 1399 1400 p9_debug(P9_DEBUG_9P, "<<< RSYMLINK qid %x.%llx.%x\n", 1401 qid->type, qid->path, qid->version); 1402 1403free_and_error: 1404 p9_tag_remove(clnt, req); 1405error: 1406 return err; 1407} 1408EXPORT_SYMBOL(p9_client_symlink); 1409 1410int p9_client_link(struct p9_fid *dfid, struct p9_fid *oldfid, const char *newname) 1411{ 1412 struct p9_client *clnt; 1413 struct p9_req_t *req; 1414 1415 p9_debug(P9_DEBUG_9P, ">>> TLINK dfid %d oldfid %d newname %s\n", 1416 dfid->fid, oldfid->fid, newname); 1417 clnt = dfid->clnt; 1418 req = p9_client_rpc(clnt, P9_TLINK, "dds", dfid->fid, oldfid->fid, 1419 newname); 1420 if (IS_ERR(req)) 1421 return PTR_ERR(req); 1422 1423 p9_debug(P9_DEBUG_9P, "<<< RLINK\n"); 1424 p9_tag_remove(clnt, req); 1425 return 0; 1426} 1427EXPORT_SYMBOL(p9_client_link); 1428 1429int p9_client_fsync(struct p9_fid *fid, int datasync) 1430{ 1431 int err; 1432 struct p9_client *clnt; 1433 struct p9_req_t *req; 1434 1435 p9_debug(P9_DEBUG_9P, ">>> TFSYNC fid %d datasync:%d\n", 1436 fid->fid, datasync); 1437 err = 0; 1438 clnt = fid->clnt; 1439 1440 req = p9_client_rpc(clnt, P9_TFSYNC, "dd", fid->fid, datasync); 1441 if (IS_ERR(req)) { 1442 err = PTR_ERR(req); 1443 goto error; 1444 } 1445 1446 p9_debug(P9_DEBUG_9P, "<<< RFSYNC fid %d\n", fid->fid); 1447 1448 p9_tag_remove(clnt, req); 1449 1450error: 1451 return err; 1452} 1453EXPORT_SYMBOL(p9_client_fsync); 1454 1455int p9_client_clunk(struct p9_fid *fid) 1456{ 1457 int err; 1458 struct p9_client *clnt; 1459 struct p9_req_t *req; 1460 int retries = 0; 1461 1462 if (!fid || IS_ERR(fid)) { 1463 pr_warn("%s (%d): Trying to clunk with invalid fid\n", 1464 __func__, task_pid_nr(current)); 1465 dump_stack(); 1466 return 0; 1467 } 1468 if (!refcount_dec_and_test(&fid->count)) 1469 return 0; 1470 1471again: 1472 p9_debug(P9_DEBUG_9P, ">>> TCLUNK fid %d (try %d)\n", 1473 fid->fid, retries); 1474 err = 0; 1475 clnt = fid->clnt; 1476 1477 req = p9_client_rpc(clnt, P9_TCLUNK, "d", fid->fid); 1478 if (IS_ERR(req)) { 1479 err = PTR_ERR(req); 1480 goto error; 1481 } 1482 1483 p9_debug(P9_DEBUG_9P, "<<< RCLUNK fid %d\n", fid->fid); 1484 1485 p9_tag_remove(clnt, req); 1486error: 1487 /* Fid is not valid even after a failed clunk 1488 * If interrupted, retry once then give up and 1489 * leak fid until umount. 1490 */ 1491 if (err == -ERESTARTSYS) { 1492 if (retries++ == 0) 1493 goto again; 1494 } else { 1495 p9_fid_destroy(fid); 1496 } 1497 return err; 1498} 1499EXPORT_SYMBOL(p9_client_clunk); 1500 1501int p9_client_remove(struct p9_fid *fid) 1502{ 1503 int err; 1504 struct p9_client *clnt; 1505 struct p9_req_t *req; 1506 1507 p9_debug(P9_DEBUG_9P, ">>> TREMOVE fid %d\n", fid->fid); 1508 err = 0; 1509 clnt = fid->clnt; 1510 1511 req = p9_client_rpc(clnt, P9_TREMOVE, "d", fid->fid); 1512 if (IS_ERR(req)) { 1513 err = PTR_ERR(req); 1514 goto error; 1515 } 1516 1517 p9_debug(P9_DEBUG_9P, "<<< RREMOVE fid %d\n", fid->fid); 1518 1519 p9_tag_remove(clnt, req); 1520error: 1521 if (err == -ERESTARTSYS) 1522 p9_client_clunk(fid); 1523 else 1524 p9_fid_destroy(fid); 1525 return err; 1526} 1527EXPORT_SYMBOL(p9_client_remove); 1528 1529int p9_client_unlinkat(struct p9_fid *dfid, const char *name, int flags) 1530{ 1531 int err = 0; 1532 struct p9_req_t *req; 1533 struct p9_client *clnt; 1534 1535 p9_debug(P9_DEBUG_9P, ">>> TUNLINKAT fid %d %s %d\n", 1536 dfid->fid, name, flags); 1537 1538 clnt = dfid->clnt; 1539 req = p9_client_rpc(clnt, P9_TUNLINKAT, "dsd", dfid->fid, name, flags); 1540 if (IS_ERR(req)) { 1541 err = PTR_ERR(req); 1542 goto error; 1543 } 1544 p9_debug(P9_DEBUG_9P, "<<< RUNLINKAT fid %d %s\n", dfid->fid, name); 1545 1546 p9_tag_remove(clnt, req); 1547error: 1548 return err; 1549} 1550EXPORT_SYMBOL(p9_client_unlinkat); 1551 1552int 1553p9_client_read(struct p9_fid *fid, u64 offset, struct iov_iter *to, int *err) 1554{ 1555 int total = 0; 1556 *err = 0; 1557 1558 while (iov_iter_count(to)) { 1559 int count; 1560 1561 count = p9_client_read_once(fid, offset, to, err); 1562 if (!count || *err) 1563 break; 1564 offset += count; 1565 total += count; 1566 } 1567 return total; 1568} 1569EXPORT_SYMBOL(p9_client_read); 1570 1571int 1572p9_client_read_once(struct p9_fid *fid, u64 offset, struct iov_iter *to, 1573 int *err) 1574{ 1575 struct p9_client *clnt = fid->clnt; 1576 struct p9_req_t *req; 1577 int count = iov_iter_count(to); 1578 int rsize, non_zc = 0; 1579 char *dataptr; 1580 1581 *err = 0; 1582 p9_debug(P9_DEBUG_9P, ">>> TREAD fid %d offset %llu %zu\n", 1583 fid->fid, offset, iov_iter_count(to)); 1584 1585 rsize = fid->iounit; 1586 if (!rsize || rsize > clnt->msize - P9_IOHDRSZ) 1587 rsize = clnt->msize - P9_IOHDRSZ; 1588 1589 if (count < rsize) 1590 rsize = count; 1591 1592 /* Don't bother zerocopy for small IO (< 1024) */ 1593 if (clnt->trans_mod->zc_request && rsize > 1024) { 1594 /* response header len is 11 1595 * PDU Header(7) + IO Size (4) 1596 */ 1597 req = p9_client_zc_rpc(clnt, P9_TREAD, to, NULL, rsize, 1598 0, 11, "dqd", fid->fid, 1599 offset, rsize); 1600 } else { 1601 non_zc = 1; 1602 req = p9_client_rpc(clnt, P9_TREAD, "dqd", fid->fid, offset, 1603 rsize); 1604 } 1605 if (IS_ERR(req)) { 1606 *err = PTR_ERR(req); 1607 return 0; 1608 } 1609 1610 *err = p9pdu_readf(&req->rc, clnt->proto_version, 1611 "D", &count, &dataptr); 1612 if (*err) { 1613 trace_9p_protocol_dump(clnt, &req->rc); 1614 p9_tag_remove(clnt, req); 1615 return 0; 1616 } 1617 if (rsize < count) { 1618 pr_err("bogus RREAD count (%d > %d)\n", count, rsize); 1619 count = rsize; 1620 } 1621 1622 p9_debug(P9_DEBUG_9P, "<<< RREAD count %d\n", count); 1623 1624 if (non_zc) { 1625 int n = copy_to_iter(dataptr, count, to); 1626 1627 if (n != count) { 1628 *err = -EFAULT; 1629 p9_tag_remove(clnt, req); 1630 return n; 1631 } 1632 } else { 1633 iov_iter_advance(to, count); 1634 } 1635 p9_tag_remove(clnt, req); 1636 return count; 1637} 1638EXPORT_SYMBOL(p9_client_read_once); 1639 1640int 1641p9_client_write(struct p9_fid *fid, u64 offset, struct iov_iter *from, int *err) 1642{ 1643 struct p9_client *clnt = fid->clnt; 1644 struct p9_req_t *req; 1645 int total = 0; 1646 *err = 0; 1647 1648 p9_debug(P9_DEBUG_9P, ">>> TWRITE fid %d offset %llu count %zd\n", 1649 fid->fid, offset, iov_iter_count(from)); 1650 1651 while (iov_iter_count(from)) { 1652 int count = iov_iter_count(from); 1653 int rsize = fid->iounit; 1654 1655 if (!rsize || rsize > clnt->msize - P9_IOHDRSZ) 1656 rsize = clnt->msize - P9_IOHDRSZ; 1657 1658 if (count < rsize) 1659 rsize = count; 1660 1661 /* Don't bother zerocopy for small IO (< 1024) */ 1662 if (clnt->trans_mod->zc_request && rsize > 1024) { 1663 req = p9_client_zc_rpc(clnt, P9_TWRITE, NULL, from, 0, 1664 rsize, P9_ZC_HDR_SZ, "dqd", 1665 fid->fid, offset, rsize); 1666 } else { 1667 req = p9_client_rpc(clnt, P9_TWRITE, "dqV", fid->fid, 1668 offset, rsize, from); 1669 } 1670 if (IS_ERR(req)) { 1671 *err = PTR_ERR(req); 1672 break; 1673 } 1674 1675 *err = p9pdu_readf(&req->rc, clnt->proto_version, "d", &count); 1676 if (*err) { 1677 trace_9p_protocol_dump(clnt, &req->rc); 1678 p9_tag_remove(clnt, req); 1679 break; 1680 } 1681 if (rsize < count) { 1682 pr_err("bogus RWRITE count (%d > %d)\n", count, rsize); 1683 count = rsize; 1684 } 1685 1686 p9_debug(P9_DEBUG_9P, "<<< RWRITE count %d\n", count); 1687 1688 p9_tag_remove(clnt, req); 1689 iov_iter_advance(from, count); 1690 total += count; 1691 offset += count; 1692 } 1693 return total; 1694} 1695EXPORT_SYMBOL(p9_client_write); 1696 1697struct p9_wstat *p9_client_stat(struct p9_fid *fid) 1698{ 1699 int err; 1700 struct p9_client *clnt; 1701 struct p9_wstat *ret; 1702 struct p9_req_t *req; 1703 u16 ignored; 1704 1705 p9_debug(P9_DEBUG_9P, ">>> TSTAT fid %d\n", fid->fid); 1706 1707 ret = kmalloc(sizeof(*ret), GFP_KERNEL); 1708 if (!ret) 1709 return ERR_PTR(-ENOMEM); 1710 1711 err = 0; 1712 clnt = fid->clnt; 1713 1714 req = p9_client_rpc(clnt, P9_TSTAT, "d", fid->fid); 1715 if (IS_ERR(req)) { 1716 err = PTR_ERR(req); 1717 goto error; 1718 } 1719 1720 err = p9pdu_readf(&req->rc, clnt->proto_version, "wS", &ignored, ret); 1721 if (err) { 1722 trace_9p_protocol_dump(clnt, &req->rc); 1723 p9_tag_remove(clnt, req); 1724 goto error; 1725 } 1726 1727 p9_debug(P9_DEBUG_9P, 1728 "<<< RSTAT sz=%x type=%x dev=%x qid=%x.%llx.%x\n" 1729 "<<< mode=%8.8x atime=%8.8x mtime=%8.8x length=%llx\n" 1730 "<<< name=%s uid=%s gid=%s muid=%s extension=(%s)\n" 1731 "<<< uid=%d gid=%d n_muid=%d\n", 1732 ret->size, ret->type, ret->dev, ret->qid.type, ret->qid.path, 1733 ret->qid.version, ret->mode, 1734 ret->atime, ret->mtime, ret->length, 1735 ret->name, ret->uid, ret->gid, ret->muid, ret->extension, 1736 from_kuid(&init_user_ns, ret->n_uid), 1737 from_kgid(&init_user_ns, ret->n_gid), 1738 from_kuid(&init_user_ns, ret->n_muid)); 1739 1740 p9_tag_remove(clnt, req); 1741 return ret; 1742 1743error: 1744 kfree(ret); 1745 return ERR_PTR(err); 1746} 1747EXPORT_SYMBOL(p9_client_stat); 1748 1749struct p9_stat_dotl *p9_client_getattr_dotl(struct p9_fid *fid, 1750 u64 request_mask) 1751{ 1752 int err; 1753 struct p9_client *clnt; 1754 struct p9_stat_dotl *ret; 1755 struct p9_req_t *req; 1756 1757 p9_debug(P9_DEBUG_9P, ">>> TGETATTR fid %d, request_mask %lld\n", 1758 fid->fid, request_mask); 1759 1760 ret = kmalloc(sizeof(*ret), GFP_KERNEL); 1761 if (!ret) 1762 return ERR_PTR(-ENOMEM); 1763 1764 err = 0; 1765 clnt = fid->clnt; 1766 1767 req = p9_client_rpc(clnt, P9_TGETATTR, "dq", fid->fid, request_mask); 1768 if (IS_ERR(req)) { 1769 err = PTR_ERR(req); 1770 goto error; 1771 } 1772 1773 err = p9pdu_readf(&req->rc, clnt->proto_version, "A", ret); 1774 if (err) { 1775 trace_9p_protocol_dump(clnt, &req->rc); 1776 p9_tag_remove(clnt, req); 1777 goto error; 1778 } 1779 1780 p9_debug(P9_DEBUG_9P, "<<< RGETATTR st_result_mask=%lld\n" 1781 "<<< qid=%x.%llx.%x\n" 1782 "<<< st_mode=%8.8x st_nlink=%llu\n" 1783 "<<< st_uid=%d st_gid=%d\n" 1784 "<<< st_rdev=%llx st_size=%llx st_blksize=%llu st_blocks=%llu\n" 1785 "<<< st_atime_sec=%lld st_atime_nsec=%lld\n" 1786 "<<< st_mtime_sec=%lld st_mtime_nsec=%lld\n" 1787 "<<< st_ctime_sec=%lld st_ctime_nsec=%lld\n" 1788 "<<< st_btime_sec=%lld st_btime_nsec=%lld\n" 1789 "<<< st_gen=%lld st_data_version=%lld\n", 1790 ret->st_result_mask, 1791 ret->qid.type, ret->qid.path, ret->qid.version, 1792 ret->st_mode, ret->st_nlink, 1793 from_kuid(&init_user_ns, ret->st_uid), 1794 from_kgid(&init_user_ns, ret->st_gid), 1795 ret->st_rdev, ret->st_size, ret->st_blksize, ret->st_blocks, 1796 ret->st_atime_sec, ret->st_atime_nsec, 1797 ret->st_mtime_sec, ret->st_mtime_nsec, 1798 ret->st_ctime_sec, ret->st_ctime_nsec, 1799 ret->st_btime_sec, ret->st_btime_nsec, 1800 ret->st_gen, ret->st_data_version); 1801 1802 p9_tag_remove(clnt, req); 1803 return ret; 1804 1805error: 1806 kfree(ret); 1807 return ERR_PTR(err); 1808} 1809EXPORT_SYMBOL(p9_client_getattr_dotl); 1810 1811static int p9_client_statsize(struct p9_wstat *wst, int proto_version) 1812{ 1813 int ret; 1814 1815 /* NOTE: size shouldn't include its own length */ 1816 /* size[2] type[2] dev[4] qid[13] */ 1817 /* mode[4] atime[4] mtime[4] length[8]*/ 1818 /* name[s] uid[s] gid[s] muid[s] */ 1819 ret = 2 + 4 + 13 + 4 + 4 + 4 + 8 + 2 + 2 + 2 + 2; 1820 1821 if (wst->name) 1822 ret += strlen(wst->name); 1823 if (wst->uid) 1824 ret += strlen(wst->uid); 1825 if (wst->gid) 1826 ret += strlen(wst->gid); 1827 if (wst->muid) 1828 ret += strlen(wst->muid); 1829 1830 if (proto_version == p9_proto_2000u || 1831 proto_version == p9_proto_2000L) { 1832 /* extension[s] n_uid[4] n_gid[4] n_muid[4] */ 1833 ret += 2 + 4 + 4 + 4; 1834 if (wst->extension) 1835 ret += strlen(wst->extension); 1836 } 1837 1838 return ret; 1839} 1840 1841int p9_client_wstat(struct p9_fid *fid, struct p9_wstat *wst) 1842{ 1843 int err; 1844 struct p9_req_t *req; 1845 struct p9_client *clnt; 1846 1847 err = 0; 1848 clnt = fid->clnt; 1849 wst->size = p9_client_statsize(wst, clnt->proto_version); 1850 p9_debug(P9_DEBUG_9P, ">>> TWSTAT fid %d\n", 1851 fid->fid); 1852 p9_debug(P9_DEBUG_9P, 1853 " sz=%x type=%x dev=%x qid=%x.%llx.%x\n" 1854 " mode=%8.8x atime=%8.8x mtime=%8.8x length=%llx\n" 1855 " name=%s uid=%s gid=%s muid=%s extension=(%s)\n" 1856 " uid=%d gid=%d n_muid=%d\n", 1857 wst->size, wst->type, wst->dev, wst->qid.type, 1858 wst->qid.path, wst->qid.version, 1859 wst->mode, wst->atime, wst->mtime, wst->length, 1860 wst->name, wst->uid, wst->gid, wst->muid, wst->extension, 1861 from_kuid(&init_user_ns, wst->n_uid), 1862 from_kgid(&init_user_ns, wst->n_gid), 1863 from_kuid(&init_user_ns, wst->n_muid)); 1864 1865 req = p9_client_rpc(clnt, P9_TWSTAT, "dwS", 1866 fid->fid, wst->size + 2, wst); 1867 if (IS_ERR(req)) { 1868 err = PTR_ERR(req); 1869 goto error; 1870 } 1871 1872 p9_debug(P9_DEBUG_9P, "<<< RWSTAT fid %d\n", fid->fid); 1873 1874 p9_tag_remove(clnt, req); 1875error: 1876 return err; 1877} 1878EXPORT_SYMBOL(p9_client_wstat); 1879 1880int p9_client_setattr(struct p9_fid *fid, struct p9_iattr_dotl *p9attr) 1881{ 1882 int err; 1883 struct p9_req_t *req; 1884 struct p9_client *clnt; 1885 1886 err = 0; 1887 clnt = fid->clnt; 1888 p9_debug(P9_DEBUG_9P, ">>> TSETATTR fid %d\n", fid->fid); 1889 p9_debug(P9_DEBUG_9P, " valid=%x mode=%x uid=%d gid=%d size=%lld\n", 1890 p9attr->valid, p9attr->mode, 1891 from_kuid(&init_user_ns, p9attr->uid), 1892 from_kgid(&init_user_ns, p9attr->gid), 1893 p9attr->size); 1894 p9_debug(P9_DEBUG_9P, " atime_sec=%lld atime_nsec=%lld\n", 1895 p9attr->atime_sec, p9attr->atime_nsec); 1896 p9_debug(P9_DEBUG_9P, " mtime_sec=%lld mtime_nsec=%lld\n", 1897 p9attr->mtime_sec, p9attr->mtime_nsec); 1898 1899 req = p9_client_rpc(clnt, P9_TSETATTR, "dI", fid->fid, p9attr); 1900 1901 if (IS_ERR(req)) { 1902 err = PTR_ERR(req); 1903 goto error; 1904 } 1905 p9_debug(P9_DEBUG_9P, "<<< RSETATTR fid %d\n", fid->fid); 1906 p9_tag_remove(clnt, req); 1907error: 1908 return err; 1909} 1910EXPORT_SYMBOL(p9_client_setattr); 1911 1912int p9_client_statfs(struct p9_fid *fid, struct p9_rstatfs *sb) 1913{ 1914 int err; 1915 struct p9_req_t *req; 1916 struct p9_client *clnt; 1917 1918 err = 0; 1919 clnt = fid->clnt; 1920 1921 p9_debug(P9_DEBUG_9P, ">>> TSTATFS fid %d\n", fid->fid); 1922 1923 req = p9_client_rpc(clnt, P9_TSTATFS, "d", fid->fid); 1924 if (IS_ERR(req)) { 1925 err = PTR_ERR(req); 1926 goto error; 1927 } 1928 1929 err = p9pdu_readf(&req->rc, clnt->proto_version, "ddqqqqqqd", &sb->type, 1930 &sb->bsize, &sb->blocks, &sb->bfree, &sb->bavail, 1931 &sb->files, &sb->ffree, &sb->fsid, &sb->namelen); 1932 if (err) { 1933 trace_9p_protocol_dump(clnt, &req->rc); 1934 p9_tag_remove(clnt, req); 1935 goto error; 1936 } 1937 1938 p9_debug(P9_DEBUG_9P, 1939 "<<< RSTATFS fid %d type 0x%x bsize %u blocks %llu bfree %llu bavail %llu files %llu ffree %llu fsid %llu namelen %u\n", 1940 fid->fid, sb->type, sb->bsize, sb->blocks, sb->bfree, 1941 sb->bavail, sb->files, sb->ffree, sb->fsid, sb->namelen); 1942 1943 p9_tag_remove(clnt, req); 1944error: 1945 return err; 1946} 1947EXPORT_SYMBOL(p9_client_statfs); 1948 1949int p9_client_rename(struct p9_fid *fid, 1950 struct p9_fid *newdirfid, const char *name) 1951{ 1952 int err; 1953 struct p9_req_t *req; 1954 struct p9_client *clnt; 1955 1956 err = 0; 1957 clnt = fid->clnt; 1958 1959 p9_debug(P9_DEBUG_9P, ">>> TRENAME fid %d newdirfid %d name %s\n", 1960 fid->fid, newdirfid->fid, name); 1961 1962 req = p9_client_rpc(clnt, P9_TRENAME, "dds", fid->fid, 1963 newdirfid->fid, name); 1964 if (IS_ERR(req)) { 1965 err = PTR_ERR(req); 1966 goto error; 1967 } 1968 1969 p9_debug(P9_DEBUG_9P, "<<< RRENAME fid %d\n", fid->fid); 1970 1971 p9_tag_remove(clnt, req); 1972error: 1973 return err; 1974} 1975EXPORT_SYMBOL(p9_client_rename); 1976 1977int p9_client_renameat(struct p9_fid *olddirfid, const char *old_name, 1978 struct p9_fid *newdirfid, const char *new_name) 1979{ 1980 int err; 1981 struct p9_req_t *req; 1982 struct p9_client *clnt; 1983 1984 err = 0; 1985 clnt = olddirfid->clnt; 1986 1987 p9_debug(P9_DEBUG_9P, 1988 ">>> TRENAMEAT olddirfid %d old name %s newdirfid %d new name %s\n", 1989 olddirfid->fid, old_name, newdirfid->fid, new_name); 1990 1991 req = p9_client_rpc(clnt, P9_TRENAMEAT, "dsds", olddirfid->fid, 1992 old_name, newdirfid->fid, new_name); 1993 if (IS_ERR(req)) { 1994 err = PTR_ERR(req); 1995 goto error; 1996 } 1997 1998 p9_debug(P9_DEBUG_9P, "<<< RRENAMEAT newdirfid %d new name %s\n", 1999 newdirfid->fid, new_name); 2000 2001 p9_tag_remove(clnt, req); 2002error: 2003 return err; 2004} 2005EXPORT_SYMBOL(p9_client_renameat); 2006 2007/* An xattrwalk without @attr_name gives the fid for the lisxattr namespace 2008 */ 2009struct p9_fid *p9_client_xattrwalk(struct p9_fid *file_fid, 2010 const char *attr_name, u64 *attr_size) 2011{ 2012 int err; 2013 struct p9_req_t *req; 2014 struct p9_client *clnt; 2015 struct p9_fid *attr_fid; 2016 2017 err = 0; 2018 clnt = file_fid->clnt; 2019 attr_fid = p9_fid_create(clnt); 2020 if (!attr_fid) { 2021 err = -ENOMEM; 2022 goto error; 2023 } 2024 p9_debug(P9_DEBUG_9P, 2025 ">>> TXATTRWALK file_fid %d, attr_fid %d name %s\n", 2026 file_fid->fid, attr_fid->fid, attr_name); 2027 2028 req = p9_client_rpc(clnt, P9_TXATTRWALK, "dds", 2029 file_fid->fid, attr_fid->fid, attr_name); 2030 if (IS_ERR(req)) { 2031 err = PTR_ERR(req); 2032 goto error; 2033 } 2034 err = p9pdu_readf(&req->rc, clnt->proto_version, "q", attr_size); 2035 if (err) { 2036 trace_9p_protocol_dump(clnt, &req->rc); 2037 p9_tag_remove(clnt, req); 2038 goto clunk_fid; 2039 } 2040 p9_tag_remove(clnt, req); 2041 p9_debug(P9_DEBUG_9P, "<<< RXATTRWALK fid %d size %llu\n", 2042 attr_fid->fid, *attr_size); 2043 return attr_fid; 2044clunk_fid: 2045 p9_client_clunk(attr_fid); 2046 attr_fid = NULL; 2047error: 2048 if (attr_fid && attr_fid != file_fid) 2049 p9_fid_destroy(attr_fid); 2050 2051 return ERR_PTR(err); 2052} 2053EXPORT_SYMBOL_GPL(p9_client_xattrwalk); 2054 2055int p9_client_xattrcreate(struct p9_fid *fid, const char *name, 2056 u64 attr_size, int flags) 2057{ 2058 int err; 2059 struct p9_req_t *req; 2060 struct p9_client *clnt; 2061 2062 p9_debug(P9_DEBUG_9P, 2063 ">>> TXATTRCREATE fid %d name %s size %llu flag %d\n", 2064 fid->fid, name, attr_size, flags); 2065 err = 0; 2066 clnt = fid->clnt; 2067 req = p9_client_rpc(clnt, P9_TXATTRCREATE, "dsqd", 2068 fid->fid, name, attr_size, flags); 2069 if (IS_ERR(req)) { 2070 err = PTR_ERR(req); 2071 goto error; 2072 } 2073 p9_debug(P9_DEBUG_9P, "<<< RXATTRCREATE fid %d\n", fid->fid); 2074 p9_tag_remove(clnt, req); 2075error: 2076 return err; 2077} 2078EXPORT_SYMBOL_GPL(p9_client_xattrcreate); 2079 2080int p9_client_readdir(struct p9_fid *fid, char *data, u32 count, u64 offset) 2081{ 2082 int err, rsize, non_zc = 0; 2083 struct p9_client *clnt; 2084 struct p9_req_t *req; 2085 char *dataptr; 2086 struct kvec kv = {.iov_base = data, .iov_len = count}; 2087 struct iov_iter to; 2088 2089 iov_iter_kvec(&to, READ, &kv, 1, count); 2090 2091 p9_debug(P9_DEBUG_9P, ">>> TREADDIR fid %d offset %llu count %d\n", 2092 fid->fid, offset, count); 2093 2094 err = 0; 2095 clnt = fid->clnt; 2096 2097 rsize = fid->iounit; 2098 if (!rsize || rsize > clnt->msize - P9_READDIRHDRSZ) 2099 rsize = clnt->msize - P9_READDIRHDRSZ; 2100 2101 if (count < rsize) 2102 rsize = count; 2103 2104 /* Don't bother zerocopy for small IO (< 1024) */ 2105 if (clnt->trans_mod->zc_request && rsize > 1024) { 2106 /* response header len is 11 2107 * PDU Header(7) + IO Size (4) 2108 */ 2109 req = p9_client_zc_rpc(clnt, P9_TREADDIR, &to, NULL, rsize, 0, 2110 11, "dqd", fid->fid, offset, rsize); 2111 } else { 2112 non_zc = 1; 2113 req = p9_client_rpc(clnt, P9_TREADDIR, "dqd", fid->fid, 2114 offset, rsize); 2115 } 2116 if (IS_ERR(req)) { 2117 err = PTR_ERR(req); 2118 goto error; 2119 } 2120 2121 err = p9pdu_readf(&req->rc, clnt->proto_version, "D", &count, &dataptr); 2122 if (err) { 2123 trace_9p_protocol_dump(clnt, &req->rc); 2124 goto free_and_error; 2125 } 2126 if (rsize < count) { 2127 pr_err("bogus RREADDIR count (%d > %d)\n", count, rsize); 2128 count = rsize; 2129 } 2130 2131 p9_debug(P9_DEBUG_9P, "<<< RREADDIR count %d\n", count); 2132 2133 if (non_zc) 2134 memmove(data, dataptr, count); 2135 2136 p9_tag_remove(clnt, req); 2137 return count; 2138 2139free_and_error: 2140 p9_tag_remove(clnt, req); 2141error: 2142 return err; 2143} 2144EXPORT_SYMBOL(p9_client_readdir); 2145 2146int p9_client_mknod_dotl(struct p9_fid *fid, const char *name, int mode, 2147 dev_t rdev, kgid_t gid, struct p9_qid *qid) 2148{ 2149 int err; 2150 struct p9_client *clnt; 2151 struct p9_req_t *req; 2152 2153 err = 0; 2154 clnt = fid->clnt; 2155 p9_debug(P9_DEBUG_9P, 2156 ">>> TMKNOD fid %d name %s mode %d major %d minor %d\n", 2157 fid->fid, name, mode, MAJOR(rdev), MINOR(rdev)); 2158 req = p9_client_rpc(clnt, P9_TMKNOD, "dsdddg", fid->fid, name, mode, 2159 MAJOR(rdev), MINOR(rdev), gid); 2160 if (IS_ERR(req)) 2161 return PTR_ERR(req); 2162 2163 err = p9pdu_readf(&req->rc, clnt->proto_version, "Q", qid); 2164 if (err) { 2165 trace_9p_protocol_dump(clnt, &req->rc); 2166 goto error; 2167 } 2168 p9_debug(P9_DEBUG_9P, "<<< RMKNOD qid %x.%llx.%x\n", 2169 qid->type, qid->path, qid->version); 2170 2171error: 2172 p9_tag_remove(clnt, req); 2173 return err; 2174} 2175EXPORT_SYMBOL(p9_client_mknod_dotl); 2176 2177int p9_client_mkdir_dotl(struct p9_fid *fid, const char *name, int mode, 2178 kgid_t gid, struct p9_qid *qid) 2179{ 2180 int err; 2181 struct p9_client *clnt; 2182 struct p9_req_t *req; 2183 2184 err = 0; 2185 clnt = fid->clnt; 2186 p9_debug(P9_DEBUG_9P, ">>> TMKDIR fid %d name %s mode %d gid %d\n", 2187 fid->fid, name, mode, from_kgid(&init_user_ns, gid)); 2188 req = p9_client_rpc(clnt, P9_TMKDIR, "dsdg", 2189 fid->fid, name, mode, gid); 2190 if (IS_ERR(req)) 2191 return PTR_ERR(req); 2192 2193 err = p9pdu_readf(&req->rc, clnt->proto_version, "Q", qid); 2194 if (err) { 2195 trace_9p_protocol_dump(clnt, &req->rc); 2196 goto error; 2197 } 2198 p9_debug(P9_DEBUG_9P, "<<< RMKDIR qid %x.%llx.%x\n", qid->type, 2199 qid->path, qid->version); 2200 2201error: 2202 p9_tag_remove(clnt, req); 2203 return err; 2204} 2205EXPORT_SYMBOL(p9_client_mkdir_dotl); 2206 2207int p9_client_lock_dotl(struct p9_fid *fid, struct p9_flock *flock, u8 *status) 2208{ 2209 int err; 2210 struct p9_client *clnt; 2211 struct p9_req_t *req; 2212 2213 err = 0; 2214 clnt = fid->clnt; 2215 p9_debug(P9_DEBUG_9P, 2216 ">>> TLOCK fid %d type %i flags %d start %lld length %lld proc_id %d client_id %s\n", 2217 fid->fid, flock->type, flock->flags, flock->start, 2218 flock->length, flock->proc_id, flock->client_id); 2219 2220 req = p9_client_rpc(clnt, P9_TLOCK, "dbdqqds", fid->fid, flock->type, 2221 flock->flags, flock->start, flock->length, 2222 flock->proc_id, flock->client_id); 2223 2224 if (IS_ERR(req)) 2225 return PTR_ERR(req); 2226 2227 err = p9pdu_readf(&req->rc, clnt->proto_version, "b", status); 2228 if (err) { 2229 trace_9p_protocol_dump(clnt, &req->rc); 2230 goto error; 2231 } 2232 p9_debug(P9_DEBUG_9P, "<<< RLOCK status %i\n", *status); 2233error: 2234 p9_tag_remove(clnt, req); 2235 return err; 2236} 2237EXPORT_SYMBOL(p9_client_lock_dotl); 2238 2239int p9_client_getlock_dotl(struct p9_fid *fid, struct p9_getlock *glock) 2240{ 2241 int err; 2242 struct p9_client *clnt; 2243 struct p9_req_t *req; 2244 2245 err = 0; 2246 clnt = fid->clnt; 2247 p9_debug(P9_DEBUG_9P, 2248 ">>> TGETLOCK fid %d, type %i start %lld length %lld proc_id %d client_id %s\n", 2249 fid->fid, glock->type, glock->start, glock->length, 2250 glock->proc_id, glock->client_id); 2251 2252 req = p9_client_rpc(clnt, P9_TGETLOCK, "dbqqds", fid->fid, 2253 glock->type, glock->start, glock->length, 2254 glock->proc_id, glock->client_id); 2255 2256 if (IS_ERR(req)) 2257 return PTR_ERR(req); 2258 2259 err = p9pdu_readf(&req->rc, clnt->proto_version, "bqqds", &glock->type, 2260 &glock->start, &glock->length, &glock->proc_id, 2261 &glock->client_id); 2262 if (err) { 2263 trace_9p_protocol_dump(clnt, &req->rc); 2264 goto error; 2265 } 2266 p9_debug(P9_DEBUG_9P, 2267 "<<< RGETLOCK type %i start %lld length %lld proc_id %d client_id %s\n", 2268 glock->type, glock->start, glock->length, 2269 glock->proc_id, glock->client_id); 2270error: 2271 p9_tag_remove(clnt, req); 2272 return err; 2273} 2274EXPORT_SYMBOL(p9_client_getlock_dotl); 2275 2276int p9_client_readlink(struct p9_fid *fid, char **target) 2277{ 2278 int err; 2279 struct p9_client *clnt; 2280 struct p9_req_t *req; 2281 2282 err = 0; 2283 clnt = fid->clnt; 2284 p9_debug(P9_DEBUG_9P, ">>> TREADLINK fid %d\n", fid->fid); 2285 2286 req = p9_client_rpc(clnt, P9_TREADLINK, "d", fid->fid); 2287 if (IS_ERR(req)) 2288 return PTR_ERR(req); 2289 2290 err = p9pdu_readf(&req->rc, clnt->proto_version, "s", target); 2291 if (err) { 2292 trace_9p_protocol_dump(clnt, &req->rc); 2293 goto error; 2294 } 2295 p9_debug(P9_DEBUG_9P, "<<< RREADLINK target %s\n", *target); 2296error: 2297 p9_tag_remove(clnt, req); 2298 return err; 2299} 2300EXPORT_SYMBOL(p9_client_readlink); 2301 2302int __init p9_client_init(void) 2303{ 2304 p9_req_cache = KMEM_CACHE(p9_req_t, SLAB_TYPESAFE_BY_RCU); 2305 return p9_req_cache ? 0 : -ENOMEM; 2306} 2307 2308void __exit p9_client_exit(void) 2309{ 2310 kmem_cache_destroy(p9_req_cache); 2311}