1 // SPDX-License-Identifier: GPL-2.0-only
7 * Copyright (C) 2008 by Eric Van Hensbergen <ericvh@gmail.com>
8 * Copyright (C) 2007 by Latchesar Ionkov <lucho@ionkov.net>
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/module.h>
14 #include <linux/errno.h>
16 #include <linux/poll.h>
17 #include <linux/idr.h>
18 #include <linux/mutex.h>
19 #include <linux/slab.h>
20 #include <linux/sched/signal.h>
21 #include <linux/uaccess.h>
22 #include <linux/uio.h>
23 #include <net/9p/9p.h>
24 #include <linux/parser.h>
25 #include <linux/seq_file.h>
26 #include <net/9p/client.h>
27 #include <net/9p/transport.h>
30 #define CREATE_TRACE_POINTS
31 #include <trace/events/9p.h>
33 #define DEFAULT_MSIZE (128 * 1024)
35 /* Client Option Parsing (code inspired by NFS code)
36 * - a little lazy - parse all client options
47 static const match_table_t tokens = {
48 {Opt_msize, "msize=%u"},
49 {Opt_legacy, "noextend"},
50 {Opt_trans, "trans=%s"},
51 {Opt_version, "version=%s"},
55 inline int p9_is_proto_dotl(struct p9_client *clnt)
57 return clnt->proto_version == p9_proto_2000L;
59 EXPORT_SYMBOL(p9_is_proto_dotl);
61 inline int p9_is_proto_dotu(struct p9_client *clnt)
63 return clnt->proto_version == p9_proto_2000u;
65 EXPORT_SYMBOL(p9_is_proto_dotu);
67 int p9_show_client_options(struct seq_file *m, struct p9_client *clnt)
69 if (clnt->msize != DEFAULT_MSIZE)
70 seq_printf(m, ",msize=%u", clnt->msize);
71 seq_printf(m, ",trans=%s", clnt->trans_mod->name);
73 switch (clnt->proto_version) {
75 seq_puts(m, ",noextend");
78 seq_puts(m, ",version=9p2000.u");
85 if (clnt->trans_mod->show_options)
86 return clnt->trans_mod->show_options(m, clnt);
89 EXPORT_SYMBOL(p9_show_client_options);
91 /* Some error codes are taken directly from the server replies,
92 * make sure they are valid.
94 static int safe_errno(int err)
96 if (err > 0 || err < -MAX_ERRNO) {
97 p9_debug(P9_DEBUG_ERROR, "Invalid error code %d\n", err);
103 /* Interpret mount option for protocol version */
104 static int get_protocol_version(char *s)
106 int version = -EINVAL;
108 if (!strcmp(s, "9p2000")) {
109 version = p9_proto_legacy;
110 p9_debug(P9_DEBUG_9P, "Protocol version: Legacy\n");
111 } else if (!strcmp(s, "9p2000.u")) {
112 version = p9_proto_2000u;
113 p9_debug(P9_DEBUG_9P, "Protocol version: 9P2000.u\n");
114 } else if (!strcmp(s, "9p2000.L")) {
115 version = p9_proto_2000L;
116 p9_debug(P9_DEBUG_9P, "Protocol version: 9P2000.L\n");
118 pr_info("Unknown protocol version %s\n", s);
125 * parse_opts - parse mount options into client structure
126 * @opts: options string passed from mount
127 * @clnt: existing v9fs client information
129 * Return 0 upon success, -ERRNO upon failure
132 static int parse_opts(char *opts, struct p9_client *clnt)
134 char *options, *tmp_options;
136 substring_t args[MAX_OPT_ARGS];
141 clnt->proto_version = p9_proto_2000L;
142 clnt->msize = DEFAULT_MSIZE;
147 tmp_options = kstrdup(opts, GFP_KERNEL);
150 options = tmp_options;
152 while ((p = strsep(&options, ",")) != NULL) {
157 token = match_token(p, tokens, args);
160 r = match_int(&args[0], &option);
162 p9_debug(P9_DEBUG_ERROR,
163 "integer field, but no integer?\n");
168 p9_debug(P9_DEBUG_ERROR,
169 "msize should be at least 4k\n");
173 clnt->msize = option;
176 s = match_strdup(&args[0]);
179 p9_debug(P9_DEBUG_ERROR,
180 "problem allocating copy of trans arg\n");
181 goto free_and_return;
184 v9fs_put_trans(clnt->trans_mod);
185 clnt->trans_mod = v9fs_get_trans_by_name(s);
186 if (!clnt->trans_mod) {
187 pr_info("Could not find request transport: %s\n",
194 clnt->proto_version = p9_proto_legacy;
197 s = match_strdup(&args[0]);
200 p9_debug(P9_DEBUG_ERROR,
201 "problem allocating copy of version arg\n");
202 goto free_and_return;
204 r = get_protocol_version(s);
208 clnt->proto_version = r;
218 v9fs_put_trans(clnt->trans_mod);
223 static int p9_fcall_init(struct p9_client *c, struct p9_fcall *fc,
226 if (likely(c->fcall_cache) && alloc_msize == c->msize) {
227 fc->sdata = kmem_cache_alloc(c->fcall_cache, GFP_NOFS);
228 fc->cache = c->fcall_cache;
230 fc->sdata = kmalloc(alloc_msize, GFP_NOFS);
235 fc->capacity = alloc_msize;
239 void p9_fcall_fini(struct p9_fcall *fc)
241 /* sdata can be NULL for interrupted requests in trans_rdma,
242 * and kmem_cache_free does not do NULL-check for us
244 if (unlikely(!fc->sdata))
248 kmem_cache_free(fc->cache, fc->sdata);
252 EXPORT_SYMBOL(p9_fcall_fini);
254 static struct kmem_cache *p9_req_cache;
257 * p9_tag_alloc - Allocate a new request.
258 * @c: Client session.
259 * @type: Transaction type.
260 * @max_size: Maximum packet size for this request.
262 * Context: Process context.
263 * Return: Pointer to new request.
265 static struct p9_req_t *
266 p9_tag_alloc(struct p9_client *c, int8_t type, unsigned int max_size)
268 struct p9_req_t *req = kmem_cache_alloc(p9_req_cache, GFP_NOFS);
269 int alloc_msize = min(c->msize, max_size);
273 return ERR_PTR(-ENOMEM);
275 if (p9_fcall_init(c, &req->tc, alloc_msize))
277 if (p9_fcall_init(c, &req->rc, alloc_msize))
280 p9pdu_reset(&req->tc);
281 p9pdu_reset(&req->rc);
283 req->status = REQ_STATUS_ALLOC;
284 init_waitqueue_head(&req->wq);
285 INIT_LIST_HEAD(&req->req_list);
287 idr_preload(GFP_NOFS);
288 spin_lock_irq(&c->lock);
289 if (type == P9_TVERSION)
290 tag = idr_alloc(&c->reqs, req, P9_NOTAG, P9_NOTAG + 1,
293 tag = idr_alloc(&c->reqs, req, 0, P9_NOTAG, GFP_NOWAIT);
295 spin_unlock_irq(&c->lock);
300 /* Init ref to two because in the general case there is one ref
301 * that is put asynchronously by a writer thread, one ref
302 * temporarily given by p9_tag_lookup and put by p9_client_cb
303 * in the recv thread, and one ref put by p9_tag_remove in the
304 * main thread. The only exception is virtio that does not use
305 * p9_tag_lookup but does not have a writer thread either
306 * (the write happens synchronously in the request/zc_request
307 * callback), so p9_client_cb eats the second ref there
308 * as the pointer is duplicated directly by virtqueue_add_sgs()
310 refcount_set(&req->refcount, 2);
315 p9_fcall_fini(&req->tc);
316 p9_fcall_fini(&req->rc);
318 kmem_cache_free(p9_req_cache, req);
319 return ERR_PTR(-ENOMEM);
323 * p9_tag_lookup - Look up a request by tag.
324 * @c: Client session.
325 * @tag: Transaction ID.
327 * Context: Any context.
328 * Return: A request, or %NULL if there is no request with that tag.
330 struct p9_req_t *p9_tag_lookup(struct p9_client *c, u16 tag)
332 struct p9_req_t *req;
336 req = idr_find(&c->reqs, tag);
338 /* We have to be careful with the req found under rcu_read_lock
339 * Thanks to SLAB_TYPESAFE_BY_RCU we can safely try to get the
340 * ref again without corrupting other data, then check again
341 * that the tag matches once we have the ref
343 if (!p9_req_try_get(req))
345 if (req->tc.tag != tag) {
354 EXPORT_SYMBOL(p9_tag_lookup);
357 * p9_tag_remove - Remove a tag.
358 * @c: Client session.
359 * @r: Request of reference.
361 * Context: Any context.
363 static int p9_tag_remove(struct p9_client *c, struct p9_req_t *r)
368 p9_debug(P9_DEBUG_MUX, "clnt %p req %p tag: %d\n", c, r, tag);
369 spin_lock_irqsave(&c->lock, flags);
370 idr_remove(&c->reqs, tag);
371 spin_unlock_irqrestore(&c->lock, flags);
372 return p9_req_put(c, r);
375 int p9_req_put(struct p9_client *c, struct p9_req_t *r)
377 if (refcount_dec_and_test(&r->refcount)) {
378 p9_fcall_fini(&r->tc);
379 p9_fcall_fini(&r->rc);
380 kmem_cache_free(p9_req_cache, r);
385 EXPORT_SYMBOL(p9_req_put);
388 * p9_tag_cleanup - cleans up tags structure and reclaims resources
389 * @c: v9fs client struct
391 * This frees resources associated with the tags structure
394 static void p9_tag_cleanup(struct p9_client *c)
396 struct p9_req_t *req;
400 idr_for_each_entry(&c->reqs, req, id) {
401 pr_info("Tag %d still in use\n", id);
402 if (p9_tag_remove(c, req) == 0)
403 pr_warn("Packet with tag %d has still references",
410 * p9_client_cb - call back from transport to client
412 * @req: request received
413 * @status: request status, one of REQ_STATUS_*
416 void p9_client_cb(struct p9_client *c, struct p9_req_t *req, int status)
418 p9_debug(P9_DEBUG_MUX, " tag %d\n", req->tc.tag);
420 /* This barrier is needed to make sure any change made to req before
421 * the status change is visible to another thread
424 req->status = status;
427 p9_debug(P9_DEBUG_MUX, "wakeup: %d\n", req->tc.tag);
430 EXPORT_SYMBOL(p9_client_cb);
433 * p9_parse_header - parse header arguments out of a packet
434 * @pdu: packet to parse
435 * @size: size of packet
436 * @type: type of request
437 * @tag: tag of packet
438 * @rewind: set if we need to rewind offset afterwards
442 p9_parse_header(struct p9_fcall *pdu, int32_t *size, int8_t *type,
443 int16_t *tag, int rewind)
448 int offset = pdu->offset;
453 err = p9pdu_readf(pdu, 0, "dbw", &r_size, &r_type, &r_tag);
455 goto rewind_and_exit;
464 if (pdu->size != r_size || r_size < 7) {
466 goto rewind_and_exit;
472 p9_debug(P9_DEBUG_9P, "<<< size=%d type: %d tag: %d\n",
473 pdu->size, pdu->id, pdu->tag);
477 pdu->offset = offset;
480 EXPORT_SYMBOL(p9_parse_header);
483 * p9_check_errors - check 9p packet for error return and process it
484 * @c: current client instance
485 * @req: request to parse and check for error conditions
487 * returns error code if one is discovered, otherwise returns 0
489 * this will have to be more complicated if we have multiple
493 static int p9_check_errors(struct p9_client *c, struct p9_req_t *req)
499 err = p9_parse_header(&req->rc, NULL, &type, NULL, 0);
500 if (req->rc.size >= c->msize) {
501 p9_debug(P9_DEBUG_ERROR,
502 "requested packet size too big: %d\n",
506 /* dump the response from server
507 * This should be after check errors which poplulate pdu_fcall.
509 trace_9p_protocol_dump(c, &req->rc);
511 p9_debug(P9_DEBUG_ERROR, "couldn't parse header %d\n", err);
514 if (type != P9_RERROR && type != P9_RLERROR)
517 if (!p9_is_proto_dotl(c)) {
520 err = p9pdu_readf(&req->rc, c->proto_version, "s?d",
525 if (p9_is_proto_dotu(c) && ecode < 512)
529 err = p9_errstr2errno(ename, strlen(ename));
531 p9_debug(P9_DEBUG_9P, "<<< RERROR (%d) %s\n",
536 err = p9pdu_readf(&req->rc, c->proto_version, "d", &ecode);
541 p9_debug(P9_DEBUG_9P, "<<< RLERROR (%d)\n", -ecode);
547 p9_debug(P9_DEBUG_ERROR, "couldn't parse error%d\n", err);
553 * p9_check_zc_errors - check 9p packet for error return and process it
554 * @c: current client instance
555 * @req: request to parse and check for error conditions
556 * @uidata: external buffer containing error
557 * @in_hdrlen: Size of response protocol buffer.
559 * returns error code if one is discovered, otherwise returns 0
561 * this will have to be more complicated if we have multiple
565 static int p9_check_zc_errors(struct p9_client *c, struct p9_req_t *req,
566 struct iov_iter *uidata, int in_hdrlen)
573 err = p9_parse_header(&req->rc, NULL, &type, NULL, 0);
574 /* dump the response from server
575 * This should be after parse_header which poplulate pdu_fcall.
577 trace_9p_protocol_dump(c, &req->rc);
579 p9_debug(P9_DEBUG_ERROR, "couldn't parse header %d\n", err);
583 if (type != P9_RERROR && type != P9_RLERROR)
586 if (!p9_is_proto_dotl(c)) {
587 /* Error is reported in string format */
589 /* 7 = header size for RERROR; */
590 int inline_len = in_hdrlen - 7;
592 len = req->rc.size - req->rc.offset;
593 if (len > (P9_ZC_HDR_SZ - 7)) {
598 ename = &req->rc.sdata[req->rc.offset];
599 if (len > inline_len) {
600 /* We have error in external buffer */
601 if (!copy_from_iter_full(ename + inline_len,
602 len - inline_len, uidata)) {
608 err = p9pdu_readf(&req->rc, c->proto_version, "s?d",
613 if (p9_is_proto_dotu(c) && ecode < 512)
617 err = p9_errstr2errno(ename, strlen(ename));
619 p9_debug(P9_DEBUG_9P, "<<< RERROR (%d) %s\n",
624 err = p9pdu_readf(&req->rc, c->proto_version, "d", &ecode);
627 p9_debug(P9_DEBUG_9P, "<<< RLERROR (%d)\n", -ecode);
632 p9_debug(P9_DEBUG_ERROR, "couldn't parse error%d\n", err);
636 static struct p9_req_t *
637 p9_client_rpc(struct p9_client *c, int8_t type, const char *fmt, ...);
640 * p9_client_flush - flush (cancel) a request
642 * @oldreq: request to cancel
644 * This sents a flush for a particular request and links
645 * the flush request to the original request. The current
646 * code only supports a single flush request although the protocol
647 * allows for multiple flush requests to be sent for a single request.
651 static int p9_client_flush(struct p9_client *c, struct p9_req_t *oldreq)
653 struct p9_req_t *req;
657 err = p9_parse_header(&oldreq->tc, NULL, NULL, &oldtag, 1);
661 p9_debug(P9_DEBUG_9P, ">>> TFLUSH tag %d\n", oldtag);
663 req = p9_client_rpc(c, P9_TFLUSH, "w", oldtag);
667 /* if we haven't received a response for oldreq,
668 * remove it from the list
670 if (oldreq->status == REQ_STATUS_SENT) {
671 if (c->trans_mod->cancelled)
672 c->trans_mod->cancelled(c, oldreq);
675 p9_tag_remove(c, req);
679 static struct p9_req_t *p9_client_prepare_req(struct p9_client *c,
680 int8_t type, int req_size,
681 const char *fmt, va_list ap)
684 struct p9_req_t *req;
686 p9_debug(P9_DEBUG_MUX, "client %p op %d\n", c, type);
688 /* we allow for any status other than disconnected */
689 if (c->status == Disconnected)
690 return ERR_PTR(-EIO);
692 /* if status is begin_disconnected we allow only clunk request */
693 if (c->status == BeginDisconnect && type != P9_TCLUNK)
694 return ERR_PTR(-EIO);
696 req = p9_tag_alloc(c, type, req_size);
700 /* marshall the data */
701 p9pdu_prepare(&req->tc, req->tc.tag, type);
702 err = p9pdu_vwritef(&req->tc, c->proto_version, fmt, ap);
705 p9pdu_finalize(c, &req->tc);
706 trace_9p_client_req(c, type, req->tc.tag);
709 p9_tag_remove(c, req);
710 /* We have to put also the 2nd reference as it won't be used */
716 * p9_client_rpc - issue a request and wait for a response
718 * @type: type of request
719 * @fmt: protocol format string (see protocol.c)
721 * Returns request structure (which client must free using p9_tag_remove)
724 static struct p9_req_t *
725 p9_client_rpc(struct p9_client *c, int8_t type, const char *fmt, ...)
730 struct p9_req_t *req;
733 req = p9_client_prepare_req(c, type, c->msize, fmt, ap);
738 if (signal_pending(current)) {
740 clear_thread_flag(TIF_SIGPENDING);
745 err = c->trans_mod->request(c, req);
747 /* write won't happen */
749 if (err != -ERESTARTSYS && err != -EFAULT)
750 c->status = Disconnected;
751 goto recalc_sigpending;
754 /* Wait for the response */
755 err = wait_event_killable(req->wq, req->status >= REQ_STATUS_RCVD);
757 /* Make sure our req is coherent with regard to updates in other
758 * threads - echoes to wmb() in the callback
762 if (err == -ERESTARTSYS && c->status == Connected &&
765 clear_thread_flag(TIF_SIGPENDING);
769 if (req->status == REQ_STATUS_ERROR) {
770 p9_debug(P9_DEBUG_ERROR, "req_status error %d\n", req->t_err);
773 if (err == -ERESTARTSYS && c->status == Connected) {
774 p9_debug(P9_DEBUG_MUX, "flushing\n");
776 clear_thread_flag(TIF_SIGPENDING);
778 if (c->trans_mod->cancel(c, req))
779 p9_client_flush(c, req);
781 /* if we received the response anyway, don't signal error */
782 if (req->status == REQ_STATUS_RCVD)
787 spin_lock_irqsave(¤t->sighand->siglock, flags);
789 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
794 err = p9_check_errors(c, req);
795 trace_9p_client_res(c, type, req->rc.tag, err);
799 p9_tag_remove(c, req);
800 return ERR_PTR(safe_errno(err));
804 * p9_client_zc_rpc - issue a request and wait for a response
806 * @type: type of request
807 * @uidata: destination for zero copy read
808 * @uodata: source for zero copy write
809 * @inlen: read buffer size
810 * @olen: write buffer size
811 * @in_hdrlen: reader header size, This is the size of response protocol data
812 * @fmt: protocol format string (see protocol.c)
814 * Returns request structure (which client must free using p9_tag_remove)
816 static struct p9_req_t *p9_client_zc_rpc(struct p9_client *c, int8_t type,
817 struct iov_iter *uidata,
818 struct iov_iter *uodata,
819 int inlen, int olen, int in_hdrlen,
820 const char *fmt, ...)
825 struct p9_req_t *req;
828 /* We allocate a inline protocol data of only 4k bytes.
829 * The actual content is passed in zero-copy fashion.
831 req = p9_client_prepare_req(c, type, P9_ZC_HDR_SZ, fmt, ap);
836 if (signal_pending(current)) {
838 clear_thread_flag(TIF_SIGPENDING);
843 err = c->trans_mod->zc_request(c, req, uidata, uodata,
844 inlen, olen, in_hdrlen);
847 c->status = Disconnected;
848 if (err != -ERESTARTSYS)
849 goto recalc_sigpending;
851 if (req->status == REQ_STATUS_ERROR) {
852 p9_debug(P9_DEBUG_ERROR, "req_status error %d\n", req->t_err);
855 if (err == -ERESTARTSYS && c->status == Connected) {
856 p9_debug(P9_DEBUG_MUX, "flushing\n");
858 clear_thread_flag(TIF_SIGPENDING);
860 if (c->trans_mod->cancel(c, req))
861 p9_client_flush(c, req);
863 /* if we received the response anyway, don't signal error */
864 if (req->status == REQ_STATUS_RCVD)
869 spin_lock_irqsave(¤t->sighand->siglock, flags);
871 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
876 err = p9_check_zc_errors(c, req, uidata, in_hdrlen);
877 trace_9p_client_res(c, type, req->rc.tag, err);
881 p9_tag_remove(c, req);
882 return ERR_PTR(safe_errno(err));
885 static struct p9_fid *p9_fid_create(struct p9_client *clnt)
890 p9_debug(P9_DEBUG_FID, "clnt %p\n", clnt);
891 fid = kzalloc(sizeof(*fid), GFP_KERNEL);
896 fid->uid = current_fsuid();
898 refcount_set(&fid->count, 1);
900 idr_preload(GFP_KERNEL);
901 spin_lock_irq(&clnt->lock);
902 ret = idr_alloc_u32(&clnt->fids, fid, &fid->fid, P9_NOFID - 1,
904 spin_unlock_irq(&clnt->lock);
913 static void p9_fid_destroy(struct p9_fid *fid)
915 struct p9_client *clnt;
918 p9_debug(P9_DEBUG_FID, "fid %d\n", fid->fid);
920 spin_lock_irqsave(&clnt->lock, flags);
921 idr_remove(&clnt->fids, fid->fid);
922 spin_unlock_irqrestore(&clnt->lock, flags);
927 static int p9_client_version(struct p9_client *c)
930 struct p9_req_t *req;
931 char *version = NULL;
934 p9_debug(P9_DEBUG_9P, ">>> TVERSION msize %d protocol %d\n",
935 c->msize, c->proto_version);
937 switch (c->proto_version) {
939 req = p9_client_rpc(c, P9_TVERSION, "ds",
940 c->msize, "9P2000.L");
943 req = p9_client_rpc(c, P9_TVERSION, "ds",
944 c->msize, "9P2000.u");
946 case p9_proto_legacy:
947 req = p9_client_rpc(c, P9_TVERSION, "ds",
957 err = p9pdu_readf(&req->rc, c->proto_version, "ds", &msize, &version);
959 p9_debug(P9_DEBUG_9P, "version error %d\n", err);
960 trace_9p_protocol_dump(c, &req->rc);
964 p9_debug(P9_DEBUG_9P, "<<< RVERSION msize %d %s\n", msize, version);
965 if (!strncmp(version, "9P2000.L", 8)) {
966 c->proto_version = p9_proto_2000L;
967 } else if (!strncmp(version, "9P2000.u", 8)) {
968 c->proto_version = p9_proto_2000u;
969 } else if (!strncmp(version, "9P2000", 6)) {
970 c->proto_version = p9_proto_legacy;
972 p9_debug(P9_DEBUG_ERROR,
973 "server returned an unknown version: %s\n", version);
979 p9_debug(P9_DEBUG_ERROR,
980 "server returned a msize < 4096: %d\n", msize);
984 if (msize < c->msize)
989 p9_tag_remove(c, req);
994 struct p9_client *p9_client_create(const char *dev_name, char *options)
997 struct p9_client *clnt;
1001 clnt = kmalloc(sizeof(*clnt), GFP_KERNEL);
1003 return ERR_PTR(-ENOMEM);
1005 clnt->trans_mod = NULL;
1007 clnt->fcall_cache = NULL;
1009 client_id = utsname()->nodename;
1010 memcpy(clnt->name, client_id, strlen(client_id) + 1);
1012 spin_lock_init(&clnt->lock);
1013 idr_init(&clnt->fids);
1014 idr_init(&clnt->reqs);
1016 err = parse_opts(options, clnt);
1020 if (!clnt->trans_mod)
1021 clnt->trans_mod = v9fs_get_default_trans();
1023 if (!clnt->trans_mod) {
1024 err = -EPROTONOSUPPORT;
1025 p9_debug(P9_DEBUG_ERROR,
1026 "No transport defined or default transport\n");
1030 p9_debug(P9_DEBUG_MUX, "clnt %p trans %p msize %d protocol %d\n",
1031 clnt, clnt->trans_mod, clnt->msize, clnt->proto_version);
1033 err = clnt->trans_mod->create(clnt, dev_name, options);
1037 if (clnt->msize > clnt->trans_mod->maxsize)
1038 clnt->msize = clnt->trans_mod->maxsize;
1040 if (clnt->msize < 4096) {
1041 p9_debug(P9_DEBUG_ERROR,
1042 "Please specify a msize of at least 4k\n");
1047 err = p9_client_version(clnt);
1051 /* P9_HDRSZ + 4 is the smallest packet header we can have that is
1052 * followed by data accessed from userspace by read
1055 kmem_cache_create_usercopy("9p-fcall-cache", clnt->msize,
1057 clnt->msize - (P9_HDRSZ + 4),
1063 clnt->trans_mod->close(clnt);
1065 v9fs_put_trans(clnt->trans_mod);
1068 return ERR_PTR(err);
1070 EXPORT_SYMBOL(p9_client_create);
1072 void p9_client_destroy(struct p9_client *clnt)
1077 p9_debug(P9_DEBUG_MUX, "clnt %p\n", clnt);
1079 if (clnt->trans_mod)
1080 clnt->trans_mod->close(clnt);
1082 v9fs_put_trans(clnt->trans_mod);
1084 idr_for_each_entry(&clnt->fids, fid, id) {
1085 pr_info("Found fid %d not clunked\n", fid->fid);
1086 p9_fid_destroy(fid);
1089 p9_tag_cleanup(clnt);
1091 kmem_cache_destroy(clnt->fcall_cache);
1094 EXPORT_SYMBOL(p9_client_destroy);
1096 void p9_client_disconnect(struct p9_client *clnt)
1098 p9_debug(P9_DEBUG_9P, "clnt %p\n", clnt);
1099 clnt->status = Disconnected;
1101 EXPORT_SYMBOL(p9_client_disconnect);
1103 void p9_client_begin_disconnect(struct p9_client *clnt)
1105 p9_debug(P9_DEBUG_9P, "clnt %p\n", clnt);
1106 clnt->status = BeginDisconnect;
1108 EXPORT_SYMBOL(p9_client_begin_disconnect);
1110 struct p9_fid *p9_client_attach(struct p9_client *clnt, struct p9_fid *afid,
1111 const char *uname, kuid_t n_uname,
1115 struct p9_req_t *req;
1119 p9_debug(P9_DEBUG_9P, ">>> TATTACH afid %d uname %s aname %s\n",
1120 afid ? afid->fid : -1, uname, aname);
1121 fid = p9_fid_create(clnt);
1128 req = p9_client_rpc(clnt, P9_TATTACH, "ddss?u", fid->fid,
1129 afid ? afid->fid : P9_NOFID, uname, aname, n_uname);
1135 err = p9pdu_readf(&req->rc, clnt->proto_version, "Q", &qid);
1137 trace_9p_protocol_dump(clnt, &req->rc);
1138 p9_tag_remove(clnt, req);
1142 p9_debug(P9_DEBUG_9P, "<<< RATTACH qid %x.%llx.%x\n",
1143 qid.type, qid.path, qid.version);
1145 memmove(&fid->qid, &qid, sizeof(struct p9_qid));
1147 p9_tag_remove(clnt, req);
1152 p9_fid_destroy(fid);
1153 return ERR_PTR(err);
1155 EXPORT_SYMBOL(p9_client_attach);
1157 struct p9_fid *p9_client_walk(struct p9_fid *oldfid, uint16_t nwname,
1158 const unsigned char * const *wnames, int clone)
1161 struct p9_client *clnt;
1163 struct p9_qid *wqids;
1164 struct p9_req_t *req;
1169 clnt = oldfid->clnt;
1171 fid = p9_fid_create(clnt);
1177 fid->uid = oldfid->uid;
1182 p9_debug(P9_DEBUG_9P, ">>> TWALK fids %d,%d nwname %ud wname[0] %s\n",
1183 oldfid->fid, fid->fid, nwname, wnames ? wnames[0] : NULL);
1184 req = p9_client_rpc(clnt, P9_TWALK, "ddT", oldfid->fid, fid->fid,
1191 err = p9pdu_readf(&req->rc, clnt->proto_version, "R", &nwqids, &wqids);
1193 trace_9p_protocol_dump(clnt, &req->rc);
1194 p9_tag_remove(clnt, req);
1197 p9_tag_remove(clnt, req);
1199 p9_debug(P9_DEBUG_9P, "<<< RWALK nwqid %d:\n", nwqids);
1201 if (nwqids != nwname) {
1206 for (count = 0; count < nwqids; count++)
1207 p9_debug(P9_DEBUG_9P, "<<< [%d] %x.%llx.%x\n",
1208 count, wqids[count].type,
1210 wqids[count].version);
1213 memmove(&fid->qid, &wqids[nwqids - 1], sizeof(struct p9_qid));
1215 memmove(&fid->qid, &oldfid->qid, sizeof(struct p9_qid));
1222 p9_client_clunk(fid);
1226 if (fid && fid != oldfid)
1227 p9_fid_destroy(fid);
1229 return ERR_PTR(err);
1231 EXPORT_SYMBOL(p9_client_walk);
1233 int p9_client_open(struct p9_fid *fid, int mode)
1236 struct p9_client *clnt;
1237 struct p9_req_t *req;
1242 p9_debug(P9_DEBUG_9P, ">>> %s fid %d mode %d\n",
1243 p9_is_proto_dotl(clnt) ? "TLOPEN" : "TOPEN", fid->fid, mode);
1246 if (fid->mode != -1)
1249 if (p9_is_proto_dotl(clnt))
1250 req = p9_client_rpc(clnt, P9_TLOPEN, "dd", fid->fid, mode);
1252 req = p9_client_rpc(clnt, P9_TOPEN, "db", fid->fid, mode);
1258 err = p9pdu_readf(&req->rc, clnt->proto_version, "Qd", &qid, &iounit);
1260 trace_9p_protocol_dump(clnt, &req->rc);
1261 goto free_and_error;
1264 p9_debug(P9_DEBUG_9P, "<<< %s qid %x.%llx.%x iounit %x\n",
1265 p9_is_proto_dotl(clnt) ? "RLOPEN" : "ROPEN", qid.type,
1266 qid.path, qid.version, iounit);
1268 memmove(&fid->qid, &qid, sizeof(struct p9_qid));
1270 fid->iounit = iounit;
1273 p9_tag_remove(clnt, req);
1277 EXPORT_SYMBOL(p9_client_open);
1279 int p9_client_create_dotl(struct p9_fid *ofid, const char *name, u32 flags,
1280 u32 mode, kgid_t gid, struct p9_qid *qid)
1283 struct p9_client *clnt;
1284 struct p9_req_t *req;
1287 p9_debug(P9_DEBUG_9P,
1288 ">>> TLCREATE fid %d name %s flags %d mode %d gid %d\n",
1289 ofid->fid, name, flags, mode,
1290 from_kgid(&init_user_ns, gid));
1293 if (ofid->mode != -1)
1296 req = p9_client_rpc(clnt, P9_TLCREATE, "dsddg", ofid->fid, name, flags,
1303 err = p9pdu_readf(&req->rc, clnt->proto_version, "Qd", qid, &iounit);
1305 trace_9p_protocol_dump(clnt, &req->rc);
1306 goto free_and_error;
1309 p9_debug(P9_DEBUG_9P, "<<< RLCREATE qid %x.%llx.%x iounit %x\n",
1310 qid->type, qid->path, qid->version, iounit);
1312 memmove(&ofid->qid, qid, sizeof(struct p9_qid));
1314 ofid->iounit = iounit;
1317 p9_tag_remove(clnt, req);
1321 EXPORT_SYMBOL(p9_client_create_dotl);
1323 int p9_client_fcreate(struct p9_fid *fid, const char *name, u32 perm, int mode,
1327 struct p9_client *clnt;
1328 struct p9_req_t *req;
1332 p9_debug(P9_DEBUG_9P, ">>> TCREATE fid %d name %s perm %d mode %d\n",
1333 fid->fid, name, perm, mode);
1337 if (fid->mode != -1)
1340 req = p9_client_rpc(clnt, P9_TCREATE, "dsdb?s", fid->fid, name, perm,
1347 err = p9pdu_readf(&req->rc, clnt->proto_version, "Qd", &qid, &iounit);
1349 trace_9p_protocol_dump(clnt, &req->rc);
1350 goto free_and_error;
1353 p9_debug(P9_DEBUG_9P, "<<< RCREATE qid %x.%llx.%x iounit %x\n",
1354 qid.type, qid.path, qid.version, iounit);
1356 memmove(&fid->qid, &qid, sizeof(struct p9_qid));
1358 fid->iounit = iounit;
1361 p9_tag_remove(clnt, req);
1365 EXPORT_SYMBOL(p9_client_fcreate);
1367 int p9_client_symlink(struct p9_fid *dfid, const char *name,
1368 const char *symtgt, kgid_t gid, struct p9_qid *qid)
1371 struct p9_client *clnt;
1372 struct p9_req_t *req;
1374 p9_debug(P9_DEBUG_9P, ">>> TSYMLINK dfid %d name %s symtgt %s\n",
1375 dfid->fid, name, symtgt);
1378 req = p9_client_rpc(clnt, P9_TSYMLINK, "dssg", dfid->fid, name, symtgt,
1385 err = p9pdu_readf(&req->rc, clnt->proto_version, "Q", qid);
1387 trace_9p_protocol_dump(clnt, &req->rc);
1388 goto free_and_error;
1391 p9_debug(P9_DEBUG_9P, "<<< RSYMLINK qid %x.%llx.%x\n",
1392 qid->type, qid->path, qid->version);
1395 p9_tag_remove(clnt, req);
1399 EXPORT_SYMBOL(p9_client_symlink);
1401 int p9_client_link(struct p9_fid *dfid, struct p9_fid *oldfid, const char *newname)
1403 struct p9_client *clnt;
1404 struct p9_req_t *req;
1406 p9_debug(P9_DEBUG_9P, ">>> TLINK dfid %d oldfid %d newname %s\n",
1407 dfid->fid, oldfid->fid, newname);
1409 req = p9_client_rpc(clnt, P9_TLINK, "dds", dfid->fid, oldfid->fid,
1412 return PTR_ERR(req);
1414 p9_debug(P9_DEBUG_9P, "<<< RLINK\n");
1415 p9_tag_remove(clnt, req);
1418 EXPORT_SYMBOL(p9_client_link);
1420 int p9_client_fsync(struct p9_fid *fid, int datasync)
1423 struct p9_client *clnt;
1424 struct p9_req_t *req;
1426 p9_debug(P9_DEBUG_9P, ">>> TFSYNC fid %d datasync:%d\n",
1427 fid->fid, datasync);
1431 req = p9_client_rpc(clnt, P9_TFSYNC, "dd", fid->fid, datasync);
1437 p9_debug(P9_DEBUG_9P, "<<< RFSYNC fid %d\n", fid->fid);
1439 p9_tag_remove(clnt, req);
1444 EXPORT_SYMBOL(p9_client_fsync);
1446 int p9_client_clunk(struct p9_fid *fid)
1449 struct p9_client *clnt;
1450 struct p9_req_t *req;
1453 if (!fid || IS_ERR(fid)) {
1454 pr_warn("%s (%d): Trying to clunk with invalid fid\n",
1455 __func__, task_pid_nr(current));
1459 if (!refcount_dec_and_test(&fid->count))
1463 p9_debug(P9_DEBUG_9P, ">>> TCLUNK fid %d (try %d)\n",
1468 req = p9_client_rpc(clnt, P9_TCLUNK, "d", fid->fid);
1474 p9_debug(P9_DEBUG_9P, "<<< RCLUNK fid %d\n", fid->fid);
1476 p9_tag_remove(clnt, req);
1478 /* Fid is not valid even after a failed clunk
1479 * If interrupted, retry once then give up and
1480 * leak fid until umount.
1482 if (err == -ERESTARTSYS) {
1486 p9_fid_destroy(fid);
1490 EXPORT_SYMBOL(p9_client_clunk);
1492 int p9_client_remove(struct p9_fid *fid)
1495 struct p9_client *clnt;
1496 struct p9_req_t *req;
1498 p9_debug(P9_DEBUG_9P, ">>> TREMOVE fid %d\n", fid->fid);
1502 req = p9_client_rpc(clnt, P9_TREMOVE, "d", fid->fid);
1508 p9_debug(P9_DEBUG_9P, "<<< RREMOVE fid %d\n", fid->fid);
1510 p9_tag_remove(clnt, req);
1512 if (err == -ERESTARTSYS)
1513 p9_client_clunk(fid);
1515 p9_fid_destroy(fid);
1518 EXPORT_SYMBOL(p9_client_remove);
1520 int p9_client_unlinkat(struct p9_fid *dfid, const char *name, int flags)
1523 struct p9_req_t *req;
1524 struct p9_client *clnt;
1526 p9_debug(P9_DEBUG_9P, ">>> TUNLINKAT fid %d %s %d\n",
1527 dfid->fid, name, flags);
1530 req = p9_client_rpc(clnt, P9_TUNLINKAT, "dsd", dfid->fid, name, flags);
1535 p9_debug(P9_DEBUG_9P, "<<< RUNLINKAT fid %d %s\n", dfid->fid, name);
1537 p9_tag_remove(clnt, req);
1541 EXPORT_SYMBOL(p9_client_unlinkat);
1544 p9_client_read(struct p9_fid *fid, u64 offset, struct iov_iter *to, int *err)
1549 while (iov_iter_count(to)) {
1552 count = p9_client_read_once(fid, offset, to, err);
1560 EXPORT_SYMBOL(p9_client_read);
1563 p9_client_read_once(struct p9_fid *fid, u64 offset, struct iov_iter *to,
1566 struct p9_client *clnt = fid->clnt;
1567 struct p9_req_t *req;
1568 int count = iov_iter_count(to);
1569 int rsize, non_zc = 0;
1573 p9_debug(P9_DEBUG_9P, ">>> TREAD fid %d offset %llu %zu\n",
1574 fid->fid, offset, iov_iter_count(to));
1576 rsize = fid->iounit;
1577 if (!rsize || rsize > clnt->msize - P9_IOHDRSZ)
1578 rsize = clnt->msize - P9_IOHDRSZ;
1583 /* Don't bother zerocopy for small IO (< 1024) */
1584 if (clnt->trans_mod->zc_request && rsize > 1024) {
1585 /* response header len is 11
1586 * PDU Header(7) + IO Size (4)
1588 req = p9_client_zc_rpc(clnt, P9_TREAD, to, NULL, rsize,
1589 0, 11, "dqd", fid->fid,
1593 req = p9_client_rpc(clnt, P9_TREAD, "dqd", fid->fid, offset,
1597 *err = PTR_ERR(req);
1601 *err = p9pdu_readf(&req->rc, clnt->proto_version,
1602 "D", &count, &dataptr);
1604 trace_9p_protocol_dump(clnt, &req->rc);
1605 p9_tag_remove(clnt, req);
1608 if (rsize < count) {
1609 pr_err("bogus RREAD count (%d > %d)\n", count, rsize);
1613 p9_debug(P9_DEBUG_9P, "<<< RREAD count %d\n", count);
1616 int n = copy_to_iter(dataptr, count, to);
1620 p9_tag_remove(clnt, req);
1624 iov_iter_advance(to, count);
1626 p9_tag_remove(clnt, req);
1629 EXPORT_SYMBOL(p9_client_read_once);
1632 p9_client_write(struct p9_fid *fid, u64 offset, struct iov_iter *from, int *err)
1634 struct p9_client *clnt = fid->clnt;
1635 struct p9_req_t *req;
1639 p9_debug(P9_DEBUG_9P, ">>> TWRITE fid %d offset %llu count %zd\n",
1640 fid->fid, offset, iov_iter_count(from));
1642 while (iov_iter_count(from)) {
1643 int count = iov_iter_count(from);
1644 int rsize = fid->iounit;
1646 if (!rsize || rsize > clnt->msize - P9_IOHDRSZ)
1647 rsize = clnt->msize - P9_IOHDRSZ;
1652 /* Don't bother zerocopy for small IO (< 1024) */
1653 if (clnt->trans_mod->zc_request && rsize > 1024) {
1654 req = p9_client_zc_rpc(clnt, P9_TWRITE, NULL, from, 0,
1655 rsize, P9_ZC_HDR_SZ, "dqd",
1656 fid->fid, offset, rsize);
1658 req = p9_client_rpc(clnt, P9_TWRITE, "dqV", fid->fid,
1659 offset, rsize, from);
1662 *err = PTR_ERR(req);
1666 *err = p9pdu_readf(&req->rc, clnt->proto_version, "d", &count);
1668 trace_9p_protocol_dump(clnt, &req->rc);
1669 p9_tag_remove(clnt, req);
1672 if (rsize < count) {
1673 pr_err("bogus RWRITE count (%d > %d)\n", count, rsize);
1677 p9_debug(P9_DEBUG_9P, "<<< RWRITE count %d\n", count);
1679 p9_tag_remove(clnt, req);
1680 iov_iter_advance(from, count);
1686 EXPORT_SYMBOL(p9_client_write);
1688 struct p9_wstat *p9_client_stat(struct p9_fid *fid)
1691 struct p9_client *clnt;
1692 struct p9_wstat *ret;
1693 struct p9_req_t *req;
1696 p9_debug(P9_DEBUG_9P, ">>> TSTAT fid %d\n", fid->fid);
1698 ret = kmalloc(sizeof(*ret), GFP_KERNEL);
1700 return ERR_PTR(-ENOMEM);
1705 req = p9_client_rpc(clnt, P9_TSTAT, "d", fid->fid);
1711 err = p9pdu_readf(&req->rc, clnt->proto_version, "wS", &ignored, ret);
1713 trace_9p_protocol_dump(clnt, &req->rc);
1714 p9_tag_remove(clnt, req);
1718 p9_debug(P9_DEBUG_9P,
1719 "<<< RSTAT sz=%x type=%x dev=%x qid=%x.%llx.%x\n"
1720 "<<< mode=%8.8x atime=%8.8x mtime=%8.8x length=%llx\n"
1721 "<<< name=%s uid=%s gid=%s muid=%s extension=(%s)\n"
1722 "<<< uid=%d gid=%d n_muid=%d\n",
1723 ret->size, ret->type, ret->dev, ret->qid.type, ret->qid.path,
1724 ret->qid.version, ret->mode,
1725 ret->atime, ret->mtime, ret->length,
1726 ret->name, ret->uid, ret->gid, ret->muid, ret->extension,
1727 from_kuid(&init_user_ns, ret->n_uid),
1728 from_kgid(&init_user_ns, ret->n_gid),
1729 from_kuid(&init_user_ns, ret->n_muid));
1731 p9_tag_remove(clnt, req);
1736 return ERR_PTR(err);
1738 EXPORT_SYMBOL(p9_client_stat);
1740 struct p9_stat_dotl *p9_client_getattr_dotl(struct p9_fid *fid,
1744 struct p9_client *clnt;
1745 struct p9_stat_dotl *ret;
1746 struct p9_req_t *req;
1748 p9_debug(P9_DEBUG_9P, ">>> TGETATTR fid %d, request_mask %lld\n",
1749 fid->fid, request_mask);
1751 ret = kmalloc(sizeof(*ret), GFP_KERNEL);
1753 return ERR_PTR(-ENOMEM);
1758 req = p9_client_rpc(clnt, P9_TGETATTR, "dq", fid->fid, request_mask);
1764 err = p9pdu_readf(&req->rc, clnt->proto_version, "A", ret);
1766 trace_9p_protocol_dump(clnt, &req->rc);
1767 p9_tag_remove(clnt, req);
1771 p9_debug(P9_DEBUG_9P, "<<< RGETATTR st_result_mask=%lld\n"
1772 "<<< qid=%x.%llx.%x\n"
1773 "<<< st_mode=%8.8x st_nlink=%llu\n"
1774 "<<< st_uid=%d st_gid=%d\n"
1775 "<<< st_rdev=%llx st_size=%llx st_blksize=%llu st_blocks=%llu\n"
1776 "<<< st_atime_sec=%lld st_atime_nsec=%lld\n"
1777 "<<< st_mtime_sec=%lld st_mtime_nsec=%lld\n"
1778 "<<< st_ctime_sec=%lld st_ctime_nsec=%lld\n"
1779 "<<< st_btime_sec=%lld st_btime_nsec=%lld\n"
1780 "<<< st_gen=%lld st_data_version=%lld\n",
1781 ret->st_result_mask,
1782 ret->qid.type, ret->qid.path, ret->qid.version,
1783 ret->st_mode, ret->st_nlink,
1784 from_kuid(&init_user_ns, ret->st_uid),
1785 from_kgid(&init_user_ns, ret->st_gid),
1786 ret->st_rdev, ret->st_size, ret->st_blksize, ret->st_blocks,
1787 ret->st_atime_sec, ret->st_atime_nsec,
1788 ret->st_mtime_sec, ret->st_mtime_nsec,
1789 ret->st_ctime_sec, ret->st_ctime_nsec,
1790 ret->st_btime_sec, ret->st_btime_nsec,
1791 ret->st_gen, ret->st_data_version);
1793 p9_tag_remove(clnt, req);
1798 return ERR_PTR(err);
1800 EXPORT_SYMBOL(p9_client_getattr_dotl);
1802 static int p9_client_statsize(struct p9_wstat *wst, int proto_version)
1806 /* NOTE: size shouldn't include its own length */
1807 /* size[2] type[2] dev[4] qid[13] */
1808 /* mode[4] atime[4] mtime[4] length[8]*/
1809 /* name[s] uid[s] gid[s] muid[s] */
1810 ret = 2 + 4 + 13 + 4 + 4 + 4 + 8 + 2 + 2 + 2 + 2;
1813 ret += strlen(wst->name);
1815 ret += strlen(wst->uid);
1817 ret += strlen(wst->gid);
1819 ret += strlen(wst->muid);
1821 if (proto_version == p9_proto_2000u ||
1822 proto_version == p9_proto_2000L) {
1823 /* extension[s] n_uid[4] n_gid[4] n_muid[4] */
1824 ret += 2 + 4 + 4 + 4;
1826 ret += strlen(wst->extension);
1832 int p9_client_wstat(struct p9_fid *fid, struct p9_wstat *wst)
1835 struct p9_req_t *req;
1836 struct p9_client *clnt;
1840 wst->size = p9_client_statsize(wst, clnt->proto_version);
1841 p9_debug(P9_DEBUG_9P, ">>> TWSTAT fid %d\n",
1843 p9_debug(P9_DEBUG_9P,
1844 " sz=%x type=%x dev=%x qid=%x.%llx.%x\n"
1845 " mode=%8.8x atime=%8.8x mtime=%8.8x length=%llx\n"
1846 " name=%s uid=%s gid=%s muid=%s extension=(%s)\n"
1847 " uid=%d gid=%d n_muid=%d\n",
1848 wst->size, wst->type, wst->dev, wst->qid.type,
1849 wst->qid.path, wst->qid.version,
1850 wst->mode, wst->atime, wst->mtime, wst->length,
1851 wst->name, wst->uid, wst->gid, wst->muid, wst->extension,
1852 from_kuid(&init_user_ns, wst->n_uid),
1853 from_kgid(&init_user_ns, wst->n_gid),
1854 from_kuid(&init_user_ns, wst->n_muid));
1856 req = p9_client_rpc(clnt, P9_TWSTAT, "dwS",
1857 fid->fid, wst->size + 2, wst);
1863 p9_debug(P9_DEBUG_9P, "<<< RWSTAT fid %d\n", fid->fid);
1865 p9_tag_remove(clnt, req);
1869 EXPORT_SYMBOL(p9_client_wstat);
1871 int p9_client_setattr(struct p9_fid *fid, struct p9_iattr_dotl *p9attr)
1874 struct p9_req_t *req;
1875 struct p9_client *clnt;
1879 p9_debug(P9_DEBUG_9P, ">>> TSETATTR fid %d\n", fid->fid);
1880 p9_debug(P9_DEBUG_9P, " valid=%x mode=%x uid=%d gid=%d size=%lld\n",
1881 p9attr->valid, p9attr->mode,
1882 from_kuid(&init_user_ns, p9attr->uid),
1883 from_kgid(&init_user_ns, p9attr->gid),
1885 p9_debug(P9_DEBUG_9P, " atime_sec=%lld atime_nsec=%lld\n",
1886 p9attr->atime_sec, p9attr->atime_nsec);
1887 p9_debug(P9_DEBUG_9P, " mtime_sec=%lld mtime_nsec=%lld\n",
1888 p9attr->mtime_sec, p9attr->mtime_nsec);
1890 req = p9_client_rpc(clnt, P9_TSETATTR, "dI", fid->fid, p9attr);
1896 p9_debug(P9_DEBUG_9P, "<<< RSETATTR fid %d\n", fid->fid);
1897 p9_tag_remove(clnt, req);
1901 EXPORT_SYMBOL(p9_client_setattr);
1903 int p9_client_statfs(struct p9_fid *fid, struct p9_rstatfs *sb)
1906 struct p9_req_t *req;
1907 struct p9_client *clnt;
1912 p9_debug(P9_DEBUG_9P, ">>> TSTATFS fid %d\n", fid->fid);
1914 req = p9_client_rpc(clnt, P9_TSTATFS, "d", fid->fid);
1920 err = p9pdu_readf(&req->rc, clnt->proto_version, "ddqqqqqqd", &sb->type,
1921 &sb->bsize, &sb->blocks, &sb->bfree, &sb->bavail,
1922 &sb->files, &sb->ffree, &sb->fsid, &sb->namelen);
1924 trace_9p_protocol_dump(clnt, &req->rc);
1925 p9_tag_remove(clnt, req);
1929 p9_debug(P9_DEBUG_9P,
1930 "<<< RSTATFS fid %d type 0x%x bsize %u blocks %llu bfree %llu bavail %llu files %llu ffree %llu fsid %llu namelen %u\n",
1931 fid->fid, sb->type, sb->bsize, sb->blocks, sb->bfree,
1932 sb->bavail, sb->files, sb->ffree, sb->fsid, sb->namelen);
1934 p9_tag_remove(clnt, req);
1938 EXPORT_SYMBOL(p9_client_statfs);
1940 int p9_client_rename(struct p9_fid *fid,
1941 struct p9_fid *newdirfid, const char *name)
1944 struct p9_req_t *req;
1945 struct p9_client *clnt;
1950 p9_debug(P9_DEBUG_9P, ">>> TRENAME fid %d newdirfid %d name %s\n",
1951 fid->fid, newdirfid->fid, name);
1953 req = p9_client_rpc(clnt, P9_TRENAME, "dds", fid->fid,
1954 newdirfid->fid, name);
1960 p9_debug(P9_DEBUG_9P, "<<< RRENAME fid %d\n", fid->fid);
1962 p9_tag_remove(clnt, req);
1966 EXPORT_SYMBOL(p9_client_rename);
1968 int p9_client_renameat(struct p9_fid *olddirfid, const char *old_name,
1969 struct p9_fid *newdirfid, const char *new_name)
1972 struct p9_req_t *req;
1973 struct p9_client *clnt;
1976 clnt = olddirfid->clnt;
1978 p9_debug(P9_DEBUG_9P,
1979 ">>> TRENAMEAT olddirfid %d old name %s newdirfid %d new name %s\n",
1980 olddirfid->fid, old_name, newdirfid->fid, new_name);
1982 req = p9_client_rpc(clnt, P9_TRENAMEAT, "dsds", olddirfid->fid,
1983 old_name, newdirfid->fid, new_name);
1989 p9_debug(P9_DEBUG_9P, "<<< RRENAMEAT newdirfid %d new name %s\n",
1990 newdirfid->fid, new_name);
1992 p9_tag_remove(clnt, req);
1996 EXPORT_SYMBOL(p9_client_renameat);
1998 /* An xattrwalk without @attr_name gives the fid for the lisxattr namespace
2000 struct p9_fid *p9_client_xattrwalk(struct p9_fid *file_fid,
2001 const char *attr_name, u64 *attr_size)
2004 struct p9_req_t *req;
2005 struct p9_client *clnt;
2006 struct p9_fid *attr_fid;
2009 clnt = file_fid->clnt;
2010 attr_fid = p9_fid_create(clnt);
2015 p9_debug(P9_DEBUG_9P,
2016 ">>> TXATTRWALK file_fid %d, attr_fid %d name %s\n",
2017 file_fid->fid, attr_fid->fid, attr_name);
2019 req = p9_client_rpc(clnt, P9_TXATTRWALK, "dds",
2020 file_fid->fid, attr_fid->fid, attr_name);
2025 err = p9pdu_readf(&req->rc, clnt->proto_version, "q", attr_size);
2027 trace_9p_protocol_dump(clnt, &req->rc);
2028 p9_tag_remove(clnt, req);
2031 p9_tag_remove(clnt, req);
2032 p9_debug(P9_DEBUG_9P, "<<< RXATTRWALK fid %d size %llu\n",
2033 attr_fid->fid, *attr_size);
2036 p9_client_clunk(attr_fid);
2039 if (attr_fid && attr_fid != file_fid)
2040 p9_fid_destroy(attr_fid);
2042 return ERR_PTR(err);
2044 EXPORT_SYMBOL_GPL(p9_client_xattrwalk);
2046 int p9_client_xattrcreate(struct p9_fid *fid, const char *name,
2047 u64 attr_size, int flags)
2050 struct p9_req_t *req;
2051 struct p9_client *clnt;
2053 p9_debug(P9_DEBUG_9P,
2054 ">>> TXATTRCREATE fid %d name %s size %llu flag %d\n",
2055 fid->fid, name, attr_size, flags);
2058 req = p9_client_rpc(clnt, P9_TXATTRCREATE, "dsqd",
2059 fid->fid, name, attr_size, flags);
2064 p9_debug(P9_DEBUG_9P, "<<< RXATTRCREATE fid %d\n", fid->fid);
2065 p9_tag_remove(clnt, req);
2069 EXPORT_SYMBOL_GPL(p9_client_xattrcreate);
2071 int p9_client_readdir(struct p9_fid *fid, char *data, u32 count, u64 offset)
2073 int err, rsize, non_zc = 0;
2074 struct p9_client *clnt;
2075 struct p9_req_t *req;
2077 struct kvec kv = {.iov_base = data, .iov_len = count};
2080 iov_iter_kvec(&to, READ, &kv, 1, count);
2082 p9_debug(P9_DEBUG_9P, ">>> TREADDIR fid %d offset %llu count %d\n",
2083 fid->fid, offset, count);
2088 rsize = fid->iounit;
2089 if (!rsize || rsize > clnt->msize - P9_READDIRHDRSZ)
2090 rsize = clnt->msize - P9_READDIRHDRSZ;
2095 /* Don't bother zerocopy for small IO (< 1024) */
2096 if (clnt->trans_mod->zc_request && rsize > 1024) {
2097 /* response header len is 11
2098 * PDU Header(7) + IO Size (4)
2100 req = p9_client_zc_rpc(clnt, P9_TREADDIR, &to, NULL, rsize, 0,
2101 11, "dqd", fid->fid, offset, rsize);
2104 req = p9_client_rpc(clnt, P9_TREADDIR, "dqd", fid->fid,
2112 err = p9pdu_readf(&req->rc, clnt->proto_version, "D", &count, &dataptr);
2114 trace_9p_protocol_dump(clnt, &req->rc);
2115 goto free_and_error;
2117 if (rsize < count) {
2118 pr_err("bogus RREADDIR count (%d > %d)\n", count, rsize);
2122 p9_debug(P9_DEBUG_9P, "<<< RREADDIR count %d\n", count);
2125 memmove(data, dataptr, count);
2127 p9_tag_remove(clnt, req);
2131 p9_tag_remove(clnt, req);
2135 EXPORT_SYMBOL(p9_client_readdir);
2137 int p9_client_mknod_dotl(struct p9_fid *fid, const char *name, int mode,
2138 dev_t rdev, kgid_t gid, struct p9_qid *qid)
2141 struct p9_client *clnt;
2142 struct p9_req_t *req;
2146 p9_debug(P9_DEBUG_9P,
2147 ">>> TMKNOD fid %d name %s mode %d major %d minor %d\n",
2148 fid->fid, name, mode, MAJOR(rdev), MINOR(rdev));
2149 req = p9_client_rpc(clnt, P9_TMKNOD, "dsdddg", fid->fid, name, mode,
2150 MAJOR(rdev), MINOR(rdev), gid);
2152 return PTR_ERR(req);
2154 err = p9pdu_readf(&req->rc, clnt->proto_version, "Q", qid);
2156 trace_9p_protocol_dump(clnt, &req->rc);
2159 p9_debug(P9_DEBUG_9P, "<<< RMKNOD qid %x.%llx.%x\n",
2160 qid->type, qid->path, qid->version);
2163 p9_tag_remove(clnt, req);
2166 EXPORT_SYMBOL(p9_client_mknod_dotl);
2168 int p9_client_mkdir_dotl(struct p9_fid *fid, const char *name, int mode,
2169 kgid_t gid, struct p9_qid *qid)
2172 struct p9_client *clnt;
2173 struct p9_req_t *req;
2177 p9_debug(P9_DEBUG_9P, ">>> TMKDIR fid %d name %s mode %d gid %d\n",
2178 fid->fid, name, mode, from_kgid(&init_user_ns, gid));
2179 req = p9_client_rpc(clnt, P9_TMKDIR, "dsdg",
2180 fid->fid, name, mode, gid);
2182 return PTR_ERR(req);
2184 err = p9pdu_readf(&req->rc, clnt->proto_version, "Q", qid);
2186 trace_9p_protocol_dump(clnt, &req->rc);
2189 p9_debug(P9_DEBUG_9P, "<<< RMKDIR qid %x.%llx.%x\n", qid->type,
2190 qid->path, qid->version);
2193 p9_tag_remove(clnt, req);
2196 EXPORT_SYMBOL(p9_client_mkdir_dotl);
2198 int p9_client_lock_dotl(struct p9_fid *fid, struct p9_flock *flock, u8 *status)
2201 struct p9_client *clnt;
2202 struct p9_req_t *req;
2206 p9_debug(P9_DEBUG_9P,
2207 ">>> TLOCK fid %d type %i flags %d start %lld length %lld proc_id %d client_id %s\n",
2208 fid->fid, flock->type, flock->flags, flock->start,
2209 flock->length, flock->proc_id, flock->client_id);
2211 req = p9_client_rpc(clnt, P9_TLOCK, "dbdqqds", fid->fid, flock->type,
2212 flock->flags, flock->start, flock->length,
2213 flock->proc_id, flock->client_id);
2216 return PTR_ERR(req);
2218 err = p9pdu_readf(&req->rc, clnt->proto_version, "b", status);
2220 trace_9p_protocol_dump(clnt, &req->rc);
2223 p9_debug(P9_DEBUG_9P, "<<< RLOCK status %i\n", *status);
2225 p9_tag_remove(clnt, req);
2228 EXPORT_SYMBOL(p9_client_lock_dotl);
2230 int p9_client_getlock_dotl(struct p9_fid *fid, struct p9_getlock *glock)
2233 struct p9_client *clnt;
2234 struct p9_req_t *req;
2238 p9_debug(P9_DEBUG_9P,
2239 ">>> TGETLOCK fid %d, type %i start %lld length %lld proc_id %d client_id %s\n",
2240 fid->fid, glock->type, glock->start, glock->length,
2241 glock->proc_id, glock->client_id);
2243 req = p9_client_rpc(clnt, P9_TGETLOCK, "dbqqds", fid->fid,
2244 glock->type, glock->start, glock->length,
2245 glock->proc_id, glock->client_id);
2248 return PTR_ERR(req);
2250 err = p9pdu_readf(&req->rc, clnt->proto_version, "bqqds", &glock->type,
2251 &glock->start, &glock->length, &glock->proc_id,
2254 trace_9p_protocol_dump(clnt, &req->rc);
2257 p9_debug(P9_DEBUG_9P,
2258 "<<< RGETLOCK type %i start %lld length %lld proc_id %d client_id %s\n",
2259 glock->type, glock->start, glock->length,
2260 glock->proc_id, glock->client_id);
2262 p9_tag_remove(clnt, req);
2265 EXPORT_SYMBOL(p9_client_getlock_dotl);
2267 int p9_client_readlink(struct p9_fid *fid, char **target)
2270 struct p9_client *clnt;
2271 struct p9_req_t *req;
2275 p9_debug(P9_DEBUG_9P, ">>> TREADLINK fid %d\n", fid->fid);
2277 req = p9_client_rpc(clnt, P9_TREADLINK, "d", fid->fid);
2279 return PTR_ERR(req);
2281 err = p9pdu_readf(&req->rc, clnt->proto_version, "s", target);
2283 trace_9p_protocol_dump(clnt, &req->rc);
2286 p9_debug(P9_DEBUG_9P, "<<< RREADLINK target %s\n", *target);
2288 p9_tag_remove(clnt, req);
2291 EXPORT_SYMBOL(p9_client_readlink);
2293 int __init p9_client_init(void)
2295 p9_req_cache = KMEM_CACHE(p9_req_t, SLAB_TYPESAFE_BY_RCU);
2296 return p9_req_cache ? 0 : -ENOMEM;
2299 void __exit p9_client_exit(void)
2301 kmem_cache_destroy(p9_req_cache);