1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2008 by Eric Van Hensbergen <ericvh@gmail.com>
6 * Copyright (C) 2007 by Latchesar Ionkov <lucho@ionkov.net>
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/module.h>
12 #include <linux/errno.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>
28 #define CREATE_TRACE_POINTS
29 #include <trace/events/9p.h>
31 #define DEFAULT_MSIZE (128 * 1024)
33 /* Client Option Parsing (code inspired by NFS code)
34 * - a little lazy - parse all client options
45 static const match_table_t tokens = {
46 {Opt_msize, "msize=%u"},
47 {Opt_legacy, "noextend"},
48 {Opt_trans, "trans=%s"},
49 {Opt_version, "version=%s"},
53 inline int p9_is_proto_dotl(struct p9_client *clnt)
55 return clnt->proto_version == p9_proto_2000L;
57 EXPORT_SYMBOL(p9_is_proto_dotl);
59 inline int p9_is_proto_dotu(struct p9_client *clnt)
61 return clnt->proto_version == p9_proto_2000u;
63 EXPORT_SYMBOL(p9_is_proto_dotu);
65 int p9_show_client_options(struct seq_file *m, struct p9_client *clnt)
67 if (clnt->msize != DEFAULT_MSIZE)
68 seq_printf(m, ",msize=%u", clnt->msize);
69 seq_printf(m, ",trans=%s", clnt->trans_mod->name);
71 switch (clnt->proto_version) {
73 seq_puts(m, ",noextend");
76 seq_puts(m, ",version=9p2000.u");
83 if (clnt->trans_mod->show_options)
84 return clnt->trans_mod->show_options(m, clnt);
87 EXPORT_SYMBOL(p9_show_client_options);
89 /* Some error codes are taken directly from the server replies,
90 * make sure they are valid.
92 static int safe_errno(int err)
94 if (err > 0 || err < -MAX_ERRNO) {
95 p9_debug(P9_DEBUG_ERROR, "Invalid error code %d\n", err);
101 /* Interpret mount option for protocol version */
102 static int get_protocol_version(char *s)
104 int version = -EINVAL;
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");
116 pr_info("Unknown protocol version %s\n", s);
123 * parse_opts - parse mount options into client structure
124 * @opts: options string passed from mount
125 * @clnt: existing v9fs client information
127 * Return 0 upon success, -ERRNO upon failure
130 static int parse_opts(char *opts, struct p9_client *clnt)
132 char *options, *tmp_options;
134 substring_t args[MAX_OPT_ARGS];
139 clnt->proto_version = p9_proto_2000L;
140 clnt->msize = DEFAULT_MSIZE;
145 tmp_options = kstrdup(opts, GFP_KERNEL);
148 options = tmp_options;
150 while ((p = strsep(&options, ",")) != NULL) {
155 token = match_token(p, tokens, args);
158 r = match_int(&args[0], &option);
160 p9_debug(P9_DEBUG_ERROR,
161 "integer field, but no integer?\n");
166 p9_debug(P9_DEBUG_ERROR,
167 "msize should be at least 4k\n");
171 clnt->msize = option;
174 s = match_strdup(&args[0]);
177 p9_debug(P9_DEBUG_ERROR,
178 "problem allocating copy of trans arg\n");
179 goto free_and_return;
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",
192 clnt->proto_version = p9_proto_legacy;
195 s = match_strdup(&args[0]);
198 p9_debug(P9_DEBUG_ERROR,
199 "problem allocating copy of version arg\n");
200 goto free_and_return;
202 r = get_protocol_version(s);
206 clnt->proto_version = r;
216 v9fs_put_trans(clnt->trans_mod);
221 static int p9_fcall_init(struct p9_client *c, struct p9_fcall *fc,
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;
228 fc->sdata = kmalloc(alloc_msize, GFP_NOFS);
233 fc->capacity = alloc_msize;
237 void p9_fcall_fini(struct p9_fcall *fc)
239 /* sdata can be NULL for interrupted requests in trans_rdma,
240 * and kmem_cache_free does not do NULL-check for us
242 if (unlikely(!fc->sdata))
246 kmem_cache_free(fc->cache, fc->sdata);
250 EXPORT_SYMBOL(p9_fcall_fini);
252 static struct kmem_cache *p9_req_cache;
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.
260 * Context: Process context.
261 * Return: Pointer to new request.
263 static struct p9_req_t *
264 p9_tag_alloc(struct p9_client *c, int8_t type, unsigned int max_size)
266 struct p9_req_t *req = kmem_cache_alloc(p9_req_cache, GFP_NOFS);
267 int alloc_msize = min(c->msize, max_size);
271 return ERR_PTR(-ENOMEM);
273 if (p9_fcall_init(c, &req->tc, alloc_msize))
275 if (p9_fcall_init(c, &req->rc, alloc_msize))
278 p9pdu_reset(&req->tc);
279 p9pdu_reset(&req->rc);
281 req->status = REQ_STATUS_ALLOC;
282 init_waitqueue_head(&req->wq);
283 INIT_LIST_HEAD(&req->req_list);
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,
291 tag = idr_alloc(&c->reqs, req, 0, P9_NOTAG, GFP_NOWAIT);
293 spin_unlock_irq(&c->lock);
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()
308 refcount_set(&req->refcount.refcount, 2);
313 p9_fcall_fini(&req->tc);
314 p9_fcall_fini(&req->rc);
316 kmem_cache_free(p9_req_cache, req);
317 return ERR_PTR(-ENOMEM);
321 * p9_tag_lookup - Look up a request by tag.
322 * @c: Client session.
323 * @tag: Transaction ID.
325 * Context: Any context.
326 * Return: A request, or %NULL if there is no request with that tag.
328 struct p9_req_t *p9_tag_lookup(struct p9_client *c, u16 tag)
330 struct p9_req_t *req;
334 req = idr_find(&c->reqs, tag);
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
341 if (!p9_req_try_get(req))
343 if (req->tc.tag != tag) {
352 EXPORT_SYMBOL(p9_tag_lookup);
355 * p9_tag_remove - Remove a tag.
356 * @c: Client session.
357 * @r: Request of reference.
359 * Context: Any context.
361 static int p9_tag_remove(struct p9_client *c, struct p9_req_t *r)
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);
373 static void p9_req_free(struct kref *ref)
375 struct p9_req_t *r = container_of(ref, struct p9_req_t, refcount);
377 p9_fcall_fini(&r->tc);
378 p9_fcall_fini(&r->rc);
379 kmem_cache_free(p9_req_cache, r);
382 int p9_req_put(struct p9_req_t *r)
384 return kref_put(&r->refcount, p9_req_free);
386 EXPORT_SYMBOL(p9_req_put);
389 * p9_tag_cleanup - cleans up tags structure and reclaims resources
390 * @c: v9fs client struct
392 * This frees resources associated with the tags structure
395 static void p9_tag_cleanup(struct p9_client *c)
397 struct p9_req_t *req;
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",
411 * p9_client_cb - call back from transport to client
413 * @req: request received
414 * @status: request status, one of REQ_STATUS_*
417 void p9_client_cb(struct p9_client *c, struct p9_req_t *req, int status)
419 p9_debug(P9_DEBUG_MUX, " tag %d\n", req->tc.tag);
421 /* This barrier is needed to make sure any change made to req before
422 * the status change is visible to another thread
425 req->status = status;
428 p9_debug(P9_DEBUG_MUX, "wakeup: %d\n", req->tc.tag);
431 EXPORT_SYMBOL(p9_client_cb);
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
443 p9_parse_header(struct p9_fcall *pdu, int32_t *size, int8_t *type,
444 int16_t *tag, int rewind)
449 int offset = pdu->offset;
454 err = p9pdu_readf(pdu, 0, "dbw", &r_size, &r_type, &r_tag);
456 goto rewind_and_exit;
465 if (pdu->size != r_size || r_size < 7) {
467 goto rewind_and_exit;
473 p9_debug(P9_DEBUG_9P, "<<< size=%d type: %d tag: %d\n",
474 pdu->size, pdu->id, pdu->tag);
478 pdu->offset = offset;
481 EXPORT_SYMBOL(p9_parse_header);
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
488 * returns error code if one is discovered, otherwise returns 0
490 * this will have to be more complicated if we have multiple
494 static int p9_check_errors(struct p9_client *c, struct p9_req_t *req)
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",
507 /* dump the response from server
508 * This should be after check errors which poplulate pdu_fcall.
510 trace_9p_protocol_dump(c, &req->rc);
512 p9_debug(P9_DEBUG_ERROR, "couldn't parse header %d\n", err);
515 if (type != P9_RERROR && type != P9_RLERROR)
518 if (!p9_is_proto_dotl(c)) {
521 err = p9pdu_readf(&req->rc, c->proto_version, "s?d",
526 if (p9_is_proto_dotu(c) && ecode < 512)
530 err = p9_errstr2errno(ename, strlen(ename));
532 p9_debug(P9_DEBUG_9P, "<<< RERROR (%d) %s\n",
537 err = p9pdu_readf(&req->rc, c->proto_version, "d", &ecode);
542 p9_debug(P9_DEBUG_9P, "<<< RLERROR (%d)\n", -ecode);
548 p9_debug(P9_DEBUG_ERROR, "couldn't parse error%d\n", err);
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.
560 * returns error code if one is discovered, otherwise returns 0
562 * this will have to be more complicated if we have multiple
566 static int p9_check_zc_errors(struct p9_client *c, struct p9_req_t *req,
567 struct iov_iter *uidata, int in_hdrlen)
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.
578 trace_9p_protocol_dump(c, &req->rc);
580 p9_debug(P9_DEBUG_ERROR, "couldn't parse header %d\n", err);
584 if (type != P9_RERROR && type != P9_RLERROR)
587 if (!p9_is_proto_dotl(c)) {
588 /* Error is reported in string format */
590 /* 7 = header size for RERROR; */
591 int inline_len = in_hdrlen - 7;
593 len = req->rc.size - req->rc.offset;
594 if (len > (P9_ZC_HDR_SZ - 7)) {
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)) {
609 err = p9pdu_readf(&req->rc, c->proto_version, "s?d",
614 if (p9_is_proto_dotu(c) && ecode < 512)
618 err = p9_errstr2errno(ename, strlen(ename));
620 p9_debug(P9_DEBUG_9P, "<<< RERROR (%d) %s\n",
625 err = p9pdu_readf(&req->rc, c->proto_version, "d", &ecode);
628 p9_debug(P9_DEBUG_9P, "<<< RLERROR (%d)\n", -ecode);
633 p9_debug(P9_DEBUG_ERROR, "couldn't parse error%d\n", err);
637 static struct p9_req_t *
638 p9_client_rpc(struct p9_client *c, int8_t type, const char *fmt, ...);
641 * p9_client_flush - flush (cancel) a request
643 * @oldreq: request to cancel
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.
652 static int p9_client_flush(struct p9_client *c, struct p9_req_t *oldreq)
654 struct p9_req_t *req;
658 err = p9_parse_header(&oldreq->tc, NULL, NULL, &oldtag, 1);
662 p9_debug(P9_DEBUG_9P, ">>> TFLUSH tag %d\n", oldtag);
664 req = p9_client_rpc(c, P9_TFLUSH, "w", oldtag);
668 /* if we haven't received a response for oldreq,
669 * remove it from the list
671 if (oldreq->status == REQ_STATUS_SENT) {
672 if (c->trans_mod->cancelled)
673 c->trans_mod->cancelled(c, oldreq);
676 p9_tag_remove(c, req);
680 static struct p9_req_t *p9_client_prepare_req(struct p9_client *c,
681 int8_t type, int req_size,
682 const char *fmt, va_list ap)
685 struct p9_req_t *req;
687 p9_debug(P9_DEBUG_MUX, "client %p op %d\n", c, type);
689 /* we allow for any status other than disconnected */
690 if (c->status == Disconnected)
691 return ERR_PTR(-EIO);
693 /* if status is begin_disconnected we allow only clunk request */
694 if (c->status == BeginDisconnect && type != P9_TCLUNK)
695 return ERR_PTR(-EIO);
697 req = p9_tag_alloc(c, type, req_size);
701 /* marshall the data */
702 p9pdu_prepare(&req->tc, req->tc.tag, type);
703 err = p9pdu_vwritef(&req->tc, c->proto_version, fmt, ap);
706 p9pdu_finalize(c, &req->tc);
707 trace_9p_client_req(c, type, req->tc.tag);
710 p9_tag_remove(c, req);
711 /* We have to put also the 2nd reference as it won't be used */
717 * p9_client_rpc - issue a request and wait for a response
719 * @type: type of request
720 * @fmt: protocol format string (see protocol.c)
722 * Returns request structure (which client must free using p9_tag_remove)
725 static struct p9_req_t *
726 p9_client_rpc(struct p9_client *c, int8_t type, const char *fmt, ...)
731 struct p9_req_t *req;
734 req = p9_client_prepare_req(c, type, c->msize, fmt, ap);
739 if (signal_pending(current)) {
741 clear_thread_flag(TIF_SIGPENDING);
746 err = c->trans_mod->request(c, req);
748 /* write won't happen */
750 if (err != -ERESTARTSYS && err != -EFAULT)
751 c->status = Disconnected;
752 goto recalc_sigpending;
755 /* Wait for the response */
756 err = wait_event_killable(req->wq, req->status >= REQ_STATUS_RCVD);
758 /* Make sure our req is coherent with regard to updates in other
759 * threads - echoes to wmb() in the callback
763 if (err == -ERESTARTSYS && c->status == Connected &&
766 clear_thread_flag(TIF_SIGPENDING);
770 if (req->status == REQ_STATUS_ERROR) {
771 p9_debug(P9_DEBUG_ERROR, "req_status error %d\n", req->t_err);
774 if (err == -ERESTARTSYS && c->status == Connected) {
775 p9_debug(P9_DEBUG_MUX, "flushing\n");
777 clear_thread_flag(TIF_SIGPENDING);
779 if (c->trans_mod->cancel(c, req))
780 p9_client_flush(c, req);
782 /* if we received the response anyway, don't signal error */
783 if (req->status == REQ_STATUS_RCVD)
788 spin_lock_irqsave(¤t->sighand->siglock, flags);
790 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
795 err = p9_check_errors(c, req);
796 trace_9p_client_res(c, type, req->rc.tag, err);
800 p9_tag_remove(c, req);
801 return ERR_PTR(safe_errno(err));
805 * p9_client_zc_rpc - issue a request and wait for a response
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)
815 * Returns request structure (which client must free using p9_tag_remove)
817 static struct p9_req_t *p9_client_zc_rpc(struct p9_client *c, int8_t type,
818 struct iov_iter *uidata,
819 struct iov_iter *uodata,
820 int inlen, int olen, int in_hdrlen,
821 const char *fmt, ...)
826 struct p9_req_t *req;
829 /* We allocate a inline protocol data of only 4k bytes.
830 * The actual content is passed in zero-copy fashion.
832 req = p9_client_prepare_req(c, type, P9_ZC_HDR_SZ, fmt, ap);
837 if (signal_pending(current)) {
839 clear_thread_flag(TIF_SIGPENDING);
844 err = c->trans_mod->zc_request(c, req, uidata, uodata,
845 inlen, olen, in_hdrlen);
848 c->status = Disconnected;
849 if (err != -ERESTARTSYS)
850 goto recalc_sigpending;
852 if (req->status == REQ_STATUS_ERROR) {
853 p9_debug(P9_DEBUG_ERROR, "req_status error %d\n", req->t_err);
856 if (err == -ERESTARTSYS && c->status == Connected) {
857 p9_debug(P9_DEBUG_MUX, "flushing\n");
859 clear_thread_flag(TIF_SIGPENDING);
861 if (c->trans_mod->cancel(c, req))
862 p9_client_flush(c, req);
864 /* if we received the response anyway, don't signal error */
865 if (req->status == REQ_STATUS_RCVD)
870 spin_lock_irqsave(¤t->sighand->siglock, flags);
872 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
877 err = p9_check_zc_errors(c, req, uidata, in_hdrlen);
878 trace_9p_client_res(c, type, req->rc.tag, err);
882 p9_tag_remove(c, req);
883 return ERR_PTR(safe_errno(err));
886 static struct p9_fid *p9_fid_create(struct p9_client *clnt)
891 p9_debug(P9_DEBUG_FID, "clnt %p\n", clnt);
892 fid = kmalloc(sizeof(*fid), GFP_KERNEL);
896 memset(&fid->qid, 0, sizeof(fid->qid));
898 fid->uid = current_fsuid();
902 refcount_set(&fid->count, 1);
904 idr_preload(GFP_KERNEL);
905 spin_lock_irq(&clnt->lock);
906 ret = idr_alloc_u32(&clnt->fids, fid, &fid->fid, P9_NOFID - 1,
908 spin_unlock_irq(&clnt->lock);
917 static void p9_fid_destroy(struct p9_fid *fid)
919 struct p9_client *clnt;
922 p9_debug(P9_DEBUG_FID, "fid %d\n", fid->fid);
924 spin_lock_irqsave(&clnt->lock, flags);
925 idr_remove(&clnt->fids, fid->fid);
926 spin_unlock_irqrestore(&clnt->lock, flags);
931 static int p9_client_version(struct p9_client *c)
934 struct p9_req_t *req;
935 char *version = NULL;
938 p9_debug(P9_DEBUG_9P, ">>> TVERSION msize %d protocol %d\n",
939 c->msize, c->proto_version);
941 switch (c->proto_version) {
943 req = p9_client_rpc(c, P9_TVERSION, "ds",
944 c->msize, "9P2000.L");
947 req = p9_client_rpc(c, P9_TVERSION, "ds",
948 c->msize, "9P2000.u");
950 case p9_proto_legacy:
951 req = p9_client_rpc(c, P9_TVERSION, "ds",
961 err = p9pdu_readf(&req->rc, c->proto_version, "ds", &msize, &version);
963 p9_debug(P9_DEBUG_9P, "version error %d\n", err);
964 trace_9p_protocol_dump(c, &req->rc);
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;
976 p9_debug(P9_DEBUG_ERROR,
977 "server returned an unknown version: %s\n", version);
983 p9_debug(P9_DEBUG_ERROR,
984 "server returned a msize < 4096: %d\n", msize);
988 if (msize < c->msize)
993 p9_tag_remove(c, req);
998 struct p9_client *p9_client_create(const char *dev_name, char *options)
1001 struct p9_client *clnt;
1005 clnt = kmalloc(sizeof(*clnt), GFP_KERNEL);
1007 return ERR_PTR(-ENOMEM);
1009 clnt->trans_mod = NULL;
1011 clnt->fcall_cache = NULL;
1013 client_id = utsname()->nodename;
1014 memcpy(clnt->name, client_id, strlen(client_id) + 1);
1016 spin_lock_init(&clnt->lock);
1017 idr_init(&clnt->fids);
1018 idr_init(&clnt->reqs);
1020 err = parse_opts(options, clnt);
1024 if (!clnt->trans_mod)
1025 clnt->trans_mod = v9fs_get_default_trans();
1027 if (!clnt->trans_mod) {
1028 err = -EPROTONOSUPPORT;
1029 p9_debug(P9_DEBUG_ERROR,
1030 "No transport defined or default transport\n");
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);
1037 err = clnt->trans_mod->create(clnt, dev_name, options);
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
1049 if (clnt->msize < 4096) {
1050 p9_debug(P9_DEBUG_ERROR,
1051 "Please specify a msize of at least 4k\n");
1056 err = p9_client_version(clnt);
1060 /* P9_HDRSZ + 4 is the smallest packet header we can have that is
1061 * followed by data accessed from userspace by read
1064 kmem_cache_create_usercopy("9p-fcall-cache", clnt->msize,
1066 clnt->msize - (P9_HDRSZ + 4),
1072 clnt->trans_mod->close(clnt);
1074 v9fs_put_trans(clnt->trans_mod);
1077 return ERR_PTR(err);
1079 EXPORT_SYMBOL(p9_client_create);
1081 void p9_client_destroy(struct p9_client *clnt)
1086 p9_debug(P9_DEBUG_MUX, "clnt %p\n", clnt);
1088 if (clnt->trans_mod)
1089 clnt->trans_mod->close(clnt);
1091 v9fs_put_trans(clnt->trans_mod);
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);
1098 p9_tag_cleanup(clnt);
1100 kmem_cache_destroy(clnt->fcall_cache);
1103 EXPORT_SYMBOL(p9_client_destroy);
1105 void p9_client_disconnect(struct p9_client *clnt)
1107 p9_debug(P9_DEBUG_9P, "clnt %p\n", clnt);
1108 clnt->status = Disconnected;
1110 EXPORT_SYMBOL(p9_client_disconnect);
1112 void p9_client_begin_disconnect(struct p9_client *clnt)
1114 p9_debug(P9_DEBUG_9P, "clnt %p\n", clnt);
1115 clnt->status = BeginDisconnect;
1117 EXPORT_SYMBOL(p9_client_begin_disconnect);
1119 struct p9_fid *p9_client_attach(struct p9_client *clnt, struct p9_fid *afid,
1120 const char *uname, kuid_t n_uname,
1124 struct p9_req_t *req;
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);
1137 req = p9_client_rpc(clnt, P9_TATTACH, "ddss?u", fid->fid,
1138 afid ? afid->fid : P9_NOFID, uname, aname, n_uname);
1144 err = p9pdu_readf(&req->rc, clnt->proto_version, "Q", &qid);
1146 trace_9p_protocol_dump(clnt, &req->rc);
1147 p9_tag_remove(clnt, req);
1151 p9_debug(P9_DEBUG_9P, "<<< RATTACH qid %x.%llx.%x\n",
1152 qid.type, qid.path, qid.version);
1154 memmove(&fid->qid, &qid, sizeof(struct p9_qid));
1156 p9_tag_remove(clnt, req);
1161 p9_fid_destroy(fid);
1162 return ERR_PTR(err);
1164 EXPORT_SYMBOL(p9_client_attach);
1166 struct p9_fid *p9_client_walk(struct p9_fid *oldfid, uint16_t nwname,
1167 const unsigned char * const *wnames, int clone)
1170 struct p9_client *clnt;
1172 struct p9_qid *wqids;
1173 struct p9_req_t *req;
1178 clnt = oldfid->clnt;
1180 fid = p9_fid_create(clnt);
1186 fid->uid = oldfid->uid;
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,
1200 err = p9pdu_readf(&req->rc, clnt->proto_version, "R", &nwqids, &wqids);
1202 trace_9p_protocol_dump(clnt, &req->rc);
1203 p9_tag_remove(clnt, req);
1206 p9_tag_remove(clnt, req);
1208 p9_debug(P9_DEBUG_9P, "<<< RWALK nwqid %d:\n", nwqids);
1210 if (nwqids != nwname) {
1215 for (count = 0; count < nwqids; count++)
1216 p9_debug(P9_DEBUG_9P, "<<< [%d] %x.%llx.%x\n",
1217 count, wqids[count].type,
1219 wqids[count].version);
1222 memmove(&fid->qid, &wqids[nwqids - 1], sizeof(struct p9_qid));
1224 memmove(&fid->qid, &oldfid->qid, sizeof(struct p9_qid));
1235 if (fid && fid != oldfid)
1236 p9_fid_destroy(fid);
1238 return ERR_PTR(err);
1240 EXPORT_SYMBOL(p9_client_walk);
1242 int p9_client_open(struct p9_fid *fid, int mode)
1245 struct p9_client *clnt;
1246 struct p9_req_t *req;
1251 p9_debug(P9_DEBUG_9P, ">>> %s fid %d mode %d\n",
1252 p9_is_proto_dotl(clnt) ? "TLOPEN" : "TOPEN", fid->fid, mode);
1255 if (fid->mode != -1)
1258 if (p9_is_proto_dotl(clnt))
1259 req = p9_client_rpc(clnt, P9_TLOPEN, "dd", fid->fid, mode);
1261 req = p9_client_rpc(clnt, P9_TOPEN, "db", fid->fid, mode);
1267 err = p9pdu_readf(&req->rc, clnt->proto_version, "Qd", &qid, &iounit);
1269 trace_9p_protocol_dump(clnt, &req->rc);
1270 goto free_and_error;
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);
1277 memmove(&fid->qid, &qid, sizeof(struct p9_qid));
1279 fid->iounit = iounit;
1282 p9_tag_remove(clnt, req);
1286 EXPORT_SYMBOL(p9_client_open);
1288 int p9_client_create_dotl(struct p9_fid *ofid, const char *name, u32 flags,
1289 u32 mode, kgid_t gid, struct p9_qid *qid)
1292 struct p9_client *clnt;
1293 struct p9_req_t *req;
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));
1302 if (ofid->mode != -1)
1305 req = p9_client_rpc(clnt, P9_TLCREATE, "dsddg", ofid->fid, name, flags,
1312 err = p9pdu_readf(&req->rc, clnt->proto_version, "Qd", qid, &iounit);
1314 trace_9p_protocol_dump(clnt, &req->rc);
1315 goto free_and_error;
1318 p9_debug(P9_DEBUG_9P, "<<< RLCREATE qid %x.%llx.%x iounit %x\n",
1319 qid->type, qid->path, qid->version, iounit);
1321 memmove(&ofid->qid, qid, sizeof(struct p9_qid));
1323 ofid->iounit = iounit;
1326 p9_tag_remove(clnt, req);
1330 EXPORT_SYMBOL(p9_client_create_dotl);
1332 int p9_client_fcreate(struct p9_fid *fid, const char *name, u32 perm, int mode,
1336 struct p9_client *clnt;
1337 struct p9_req_t *req;
1341 p9_debug(P9_DEBUG_9P, ">>> TCREATE fid %d name %s perm %d mode %d\n",
1342 fid->fid, name, perm, mode);
1346 if (fid->mode != -1)
1349 req = p9_client_rpc(clnt, P9_TCREATE, "dsdb?s", fid->fid, name, perm,
1356 err = p9pdu_readf(&req->rc, clnt->proto_version, "Qd", &qid, &iounit);
1358 trace_9p_protocol_dump(clnt, &req->rc);
1359 goto free_and_error;
1362 p9_debug(P9_DEBUG_9P, "<<< RCREATE qid %x.%llx.%x iounit %x\n",
1363 qid.type, qid.path, qid.version, iounit);
1365 memmove(&fid->qid, &qid, sizeof(struct p9_qid));
1367 fid->iounit = iounit;
1370 p9_tag_remove(clnt, req);
1374 EXPORT_SYMBOL(p9_client_fcreate);
1376 int p9_client_symlink(struct p9_fid *dfid, const char *name,
1377 const char *symtgt, kgid_t gid, struct p9_qid *qid)
1380 struct p9_client *clnt;
1381 struct p9_req_t *req;
1383 p9_debug(P9_DEBUG_9P, ">>> TSYMLINK dfid %d name %s symtgt %s\n",
1384 dfid->fid, name, symtgt);
1387 req = p9_client_rpc(clnt, P9_TSYMLINK, "dssg", dfid->fid, name, symtgt,
1394 err = p9pdu_readf(&req->rc, clnt->proto_version, "Q", qid);
1396 trace_9p_protocol_dump(clnt, &req->rc);
1397 goto free_and_error;
1400 p9_debug(P9_DEBUG_9P, "<<< RSYMLINK qid %x.%llx.%x\n",
1401 qid->type, qid->path, qid->version);
1404 p9_tag_remove(clnt, req);
1408 EXPORT_SYMBOL(p9_client_symlink);
1410 int p9_client_link(struct p9_fid *dfid, struct p9_fid *oldfid, const char *newname)
1412 struct p9_client *clnt;
1413 struct p9_req_t *req;
1415 p9_debug(P9_DEBUG_9P, ">>> TLINK dfid %d oldfid %d newname %s\n",
1416 dfid->fid, oldfid->fid, newname);
1418 req = p9_client_rpc(clnt, P9_TLINK, "dds", dfid->fid, oldfid->fid,
1421 return PTR_ERR(req);
1423 p9_debug(P9_DEBUG_9P, "<<< RLINK\n");
1424 p9_tag_remove(clnt, req);
1427 EXPORT_SYMBOL(p9_client_link);
1429 int p9_client_fsync(struct p9_fid *fid, int datasync)
1432 struct p9_client *clnt;
1433 struct p9_req_t *req;
1435 p9_debug(P9_DEBUG_9P, ">>> TFSYNC fid %d datasync:%d\n",
1436 fid->fid, datasync);
1440 req = p9_client_rpc(clnt, P9_TFSYNC, "dd", fid->fid, datasync);
1446 p9_debug(P9_DEBUG_9P, "<<< RFSYNC fid %d\n", fid->fid);
1448 p9_tag_remove(clnt, req);
1453 EXPORT_SYMBOL(p9_client_fsync);
1455 int p9_client_clunk(struct p9_fid *fid)
1458 struct p9_client *clnt;
1459 struct p9_req_t *req;
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)
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_fid_put(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);