1 // SPDX-License-Identifier: GPL-2.0-only
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;
1044 if (clnt->msize < 4096) {
1045 p9_debug(P9_DEBUG_ERROR,
1046 "Please specify a msize of at least 4k\n");
1051 err = p9_client_version(clnt);
1055 /* P9_HDRSZ + 4 is the smallest packet header we can have that is
1056 * followed by data accessed from userspace by read
1059 kmem_cache_create_usercopy("9p-fcall-cache", clnt->msize,
1061 clnt->msize - (P9_HDRSZ + 4),
1067 clnt->trans_mod->close(clnt);
1069 v9fs_put_trans(clnt->trans_mod);
1072 return ERR_PTR(err);
1074 EXPORT_SYMBOL(p9_client_create);
1076 void p9_client_destroy(struct p9_client *clnt)
1081 p9_debug(P9_DEBUG_MUX, "clnt %p\n", clnt);
1083 if (clnt->trans_mod)
1084 clnt->trans_mod->close(clnt);
1086 v9fs_put_trans(clnt->trans_mod);
1088 idr_for_each_entry(&clnt->fids, fid, id) {
1089 pr_info("Found fid %d not clunked\n", fid->fid);
1090 p9_fid_destroy(fid);
1093 p9_tag_cleanup(clnt);
1095 kmem_cache_destroy(clnt->fcall_cache);
1098 EXPORT_SYMBOL(p9_client_destroy);
1100 void p9_client_disconnect(struct p9_client *clnt)
1102 p9_debug(P9_DEBUG_9P, "clnt %p\n", clnt);
1103 clnt->status = Disconnected;
1105 EXPORT_SYMBOL(p9_client_disconnect);
1107 void p9_client_begin_disconnect(struct p9_client *clnt)
1109 p9_debug(P9_DEBUG_9P, "clnt %p\n", clnt);
1110 clnt->status = BeginDisconnect;
1112 EXPORT_SYMBOL(p9_client_begin_disconnect);
1114 struct p9_fid *p9_client_attach(struct p9_client *clnt, struct p9_fid *afid,
1115 const char *uname, kuid_t n_uname,
1119 struct p9_req_t *req;
1123 p9_debug(P9_DEBUG_9P, ">>> TATTACH afid %d uname %s aname %s\n",
1124 afid ? afid->fid : -1, uname, aname);
1125 fid = p9_fid_create(clnt);
1132 req = p9_client_rpc(clnt, P9_TATTACH, "ddss?u", fid->fid,
1133 afid ? afid->fid : P9_NOFID, uname, aname, n_uname);
1139 err = p9pdu_readf(&req->rc, clnt->proto_version, "Q", &qid);
1141 trace_9p_protocol_dump(clnt, &req->rc);
1142 p9_tag_remove(clnt, req);
1146 p9_debug(P9_DEBUG_9P, "<<< RATTACH qid %x.%llx.%x\n",
1147 qid.type, qid.path, qid.version);
1149 memmove(&fid->qid, &qid, sizeof(struct p9_qid));
1151 p9_tag_remove(clnt, req);
1156 p9_fid_destroy(fid);
1157 return ERR_PTR(err);
1159 EXPORT_SYMBOL(p9_client_attach);
1161 struct p9_fid *p9_client_walk(struct p9_fid *oldfid, uint16_t nwname,
1162 const unsigned char * const *wnames, int clone)
1165 struct p9_client *clnt;
1167 struct p9_qid *wqids;
1168 struct p9_req_t *req;
1173 clnt = oldfid->clnt;
1175 fid = p9_fid_create(clnt);
1181 fid->uid = oldfid->uid;
1186 p9_debug(P9_DEBUG_9P, ">>> TWALK fids %d,%d nwname %ud wname[0] %s\n",
1187 oldfid->fid, fid->fid, nwname, wnames ? wnames[0] : NULL);
1188 req = p9_client_rpc(clnt, P9_TWALK, "ddT", oldfid->fid, fid->fid,
1195 err = p9pdu_readf(&req->rc, clnt->proto_version, "R", &nwqids, &wqids);
1197 trace_9p_protocol_dump(clnt, &req->rc);
1198 p9_tag_remove(clnt, req);
1201 p9_tag_remove(clnt, req);
1203 p9_debug(P9_DEBUG_9P, "<<< RWALK nwqid %d:\n", nwqids);
1205 if (nwqids != nwname) {
1210 for (count = 0; count < nwqids; count++)
1211 p9_debug(P9_DEBUG_9P, "<<< [%d] %x.%llx.%x\n",
1212 count, wqids[count].type,
1214 wqids[count].version);
1217 memmove(&fid->qid, &wqids[nwqids - 1], sizeof(struct p9_qid));
1219 memmove(&fid->qid, &oldfid->qid, sizeof(struct p9_qid));
1226 p9_client_clunk(fid);
1230 if (fid && fid != oldfid)
1231 p9_fid_destroy(fid);
1233 return ERR_PTR(err);
1235 EXPORT_SYMBOL(p9_client_walk);
1237 int p9_client_open(struct p9_fid *fid, int mode)
1240 struct p9_client *clnt;
1241 struct p9_req_t *req;
1246 p9_debug(P9_DEBUG_9P, ">>> %s fid %d mode %d\n",
1247 p9_is_proto_dotl(clnt) ? "TLOPEN" : "TOPEN", fid->fid, mode);
1250 if (fid->mode != -1)
1253 if (p9_is_proto_dotl(clnt))
1254 req = p9_client_rpc(clnt, P9_TLOPEN, "dd", fid->fid, mode);
1256 req = p9_client_rpc(clnt, P9_TOPEN, "db", fid->fid, mode);
1262 err = p9pdu_readf(&req->rc, clnt->proto_version, "Qd", &qid, &iounit);
1264 trace_9p_protocol_dump(clnt, &req->rc);
1265 goto free_and_error;
1268 p9_debug(P9_DEBUG_9P, "<<< %s qid %x.%llx.%x iounit %x\n",
1269 p9_is_proto_dotl(clnt) ? "RLOPEN" : "ROPEN", qid.type,
1270 qid.path, qid.version, iounit);
1272 memmove(&fid->qid, &qid, sizeof(struct p9_qid));
1274 fid->iounit = iounit;
1277 p9_tag_remove(clnt, req);
1281 EXPORT_SYMBOL(p9_client_open);
1283 int p9_client_create_dotl(struct p9_fid *ofid, const char *name, u32 flags,
1284 u32 mode, kgid_t gid, struct p9_qid *qid)
1287 struct p9_client *clnt;
1288 struct p9_req_t *req;
1291 p9_debug(P9_DEBUG_9P,
1292 ">>> TLCREATE fid %d name %s flags %d mode %d gid %d\n",
1293 ofid->fid, name, flags, mode,
1294 from_kgid(&init_user_ns, gid));
1297 if (ofid->mode != -1)
1300 req = p9_client_rpc(clnt, P9_TLCREATE, "dsddg", ofid->fid, name, flags,
1307 err = p9pdu_readf(&req->rc, clnt->proto_version, "Qd", qid, &iounit);
1309 trace_9p_protocol_dump(clnt, &req->rc);
1310 goto free_and_error;
1313 p9_debug(P9_DEBUG_9P, "<<< RLCREATE qid %x.%llx.%x iounit %x\n",
1314 qid->type, qid->path, qid->version, iounit);
1316 memmove(&ofid->qid, qid, sizeof(struct p9_qid));
1318 ofid->iounit = iounit;
1321 p9_tag_remove(clnt, req);
1325 EXPORT_SYMBOL(p9_client_create_dotl);
1327 int p9_client_fcreate(struct p9_fid *fid, const char *name, u32 perm, int mode,
1331 struct p9_client *clnt;
1332 struct p9_req_t *req;
1336 p9_debug(P9_DEBUG_9P, ">>> TCREATE fid %d name %s perm %d mode %d\n",
1337 fid->fid, name, perm, mode);
1341 if (fid->mode != -1)
1344 req = p9_client_rpc(clnt, P9_TCREATE, "dsdb?s", fid->fid, name, perm,
1351 err = p9pdu_readf(&req->rc, clnt->proto_version, "Qd", &qid, &iounit);
1353 trace_9p_protocol_dump(clnt, &req->rc);
1354 goto free_and_error;
1357 p9_debug(P9_DEBUG_9P, "<<< RCREATE qid %x.%llx.%x iounit %x\n",
1358 qid.type, qid.path, qid.version, iounit);
1360 memmove(&fid->qid, &qid, sizeof(struct p9_qid));
1362 fid->iounit = iounit;
1365 p9_tag_remove(clnt, req);
1369 EXPORT_SYMBOL(p9_client_fcreate);
1371 int p9_client_symlink(struct p9_fid *dfid, const char *name,
1372 const char *symtgt, kgid_t gid, struct p9_qid *qid)
1375 struct p9_client *clnt;
1376 struct p9_req_t *req;
1378 p9_debug(P9_DEBUG_9P, ">>> TSYMLINK dfid %d name %s symtgt %s\n",
1379 dfid->fid, name, symtgt);
1382 req = p9_client_rpc(clnt, P9_TSYMLINK, "dssg", dfid->fid, name, symtgt,
1389 err = p9pdu_readf(&req->rc, clnt->proto_version, "Q", qid);
1391 trace_9p_protocol_dump(clnt, &req->rc);
1392 goto free_and_error;
1395 p9_debug(P9_DEBUG_9P, "<<< RSYMLINK qid %x.%llx.%x\n",
1396 qid->type, qid->path, qid->version);
1399 p9_tag_remove(clnt, req);
1403 EXPORT_SYMBOL(p9_client_symlink);
1405 int p9_client_link(struct p9_fid *dfid, struct p9_fid *oldfid, const char *newname)
1407 struct p9_client *clnt;
1408 struct p9_req_t *req;
1410 p9_debug(P9_DEBUG_9P, ">>> TLINK dfid %d oldfid %d newname %s\n",
1411 dfid->fid, oldfid->fid, newname);
1413 req = p9_client_rpc(clnt, P9_TLINK, "dds", dfid->fid, oldfid->fid,
1416 return PTR_ERR(req);
1418 p9_debug(P9_DEBUG_9P, "<<< RLINK\n");
1419 p9_tag_remove(clnt, req);
1422 EXPORT_SYMBOL(p9_client_link);
1424 int p9_client_fsync(struct p9_fid *fid, int datasync)
1427 struct p9_client *clnt;
1428 struct p9_req_t *req;
1430 p9_debug(P9_DEBUG_9P, ">>> TFSYNC fid %d datasync:%d\n",
1431 fid->fid, datasync);
1435 req = p9_client_rpc(clnt, P9_TFSYNC, "dd", fid->fid, datasync);
1441 p9_debug(P9_DEBUG_9P, "<<< RFSYNC fid %d\n", fid->fid);
1443 p9_tag_remove(clnt, req);
1448 EXPORT_SYMBOL(p9_client_fsync);
1450 int p9_client_clunk(struct p9_fid *fid)
1453 struct p9_client *clnt;
1454 struct p9_req_t *req;
1457 if (!fid || IS_ERR(fid)) {
1458 pr_warn("%s (%d): Trying to clunk with invalid fid\n",
1459 __func__, task_pid_nr(current));
1463 if (!refcount_dec_and_test(&fid->count))
1467 p9_debug(P9_DEBUG_9P, ">>> TCLUNK fid %d (try %d)\n",
1472 req = p9_client_rpc(clnt, P9_TCLUNK, "d", fid->fid);
1478 p9_debug(P9_DEBUG_9P, "<<< RCLUNK fid %d\n", fid->fid);
1480 p9_tag_remove(clnt, req);
1482 /* Fid is not valid even after a failed clunk
1483 * If interrupted, retry once then give up and
1484 * leak fid until umount.
1486 if (err == -ERESTARTSYS) {
1490 p9_fid_destroy(fid);
1494 EXPORT_SYMBOL(p9_client_clunk);
1496 int p9_client_remove(struct p9_fid *fid)
1499 struct p9_client *clnt;
1500 struct p9_req_t *req;
1502 p9_debug(P9_DEBUG_9P, ">>> TREMOVE fid %d\n", fid->fid);
1506 req = p9_client_rpc(clnt, P9_TREMOVE, "d", fid->fid);
1512 p9_debug(P9_DEBUG_9P, "<<< RREMOVE fid %d\n", fid->fid);
1514 p9_tag_remove(clnt, req);
1516 if (err == -ERESTARTSYS)
1517 p9_client_clunk(fid);
1519 p9_fid_destroy(fid);
1522 EXPORT_SYMBOL(p9_client_remove);
1524 int p9_client_unlinkat(struct p9_fid *dfid, const char *name, int flags)
1527 struct p9_req_t *req;
1528 struct p9_client *clnt;
1530 p9_debug(P9_DEBUG_9P, ">>> TUNLINKAT fid %d %s %d\n",
1531 dfid->fid, name, flags);
1534 req = p9_client_rpc(clnt, P9_TUNLINKAT, "dsd", dfid->fid, name, flags);
1539 p9_debug(P9_DEBUG_9P, "<<< RUNLINKAT fid %d %s\n", dfid->fid, name);
1541 p9_tag_remove(clnt, req);
1545 EXPORT_SYMBOL(p9_client_unlinkat);
1548 p9_client_read(struct p9_fid *fid, u64 offset, struct iov_iter *to, int *err)
1553 while (iov_iter_count(to)) {
1556 count = p9_client_read_once(fid, offset, to, err);
1564 EXPORT_SYMBOL(p9_client_read);
1567 p9_client_read_once(struct p9_fid *fid, u64 offset, struct iov_iter *to,
1570 struct p9_client *clnt = fid->clnt;
1571 struct p9_req_t *req;
1572 int count = iov_iter_count(to);
1573 int rsize, non_zc = 0;
1577 p9_debug(P9_DEBUG_9P, ">>> TREAD fid %d offset %llu %zu\n",
1578 fid->fid, offset, iov_iter_count(to));
1580 rsize = fid->iounit;
1581 if (!rsize || rsize > clnt->msize - P9_IOHDRSZ)
1582 rsize = clnt->msize - P9_IOHDRSZ;
1587 /* Don't bother zerocopy for small IO (< 1024) */
1588 if (clnt->trans_mod->zc_request && rsize > 1024) {
1589 /* response header len is 11
1590 * PDU Header(7) + IO Size (4)
1592 req = p9_client_zc_rpc(clnt, P9_TREAD, to, NULL, rsize,
1593 0, 11, "dqd", fid->fid,
1597 req = p9_client_rpc(clnt, P9_TREAD, "dqd", fid->fid, offset,
1601 *err = PTR_ERR(req);
1605 *err = p9pdu_readf(&req->rc, clnt->proto_version,
1606 "D", &count, &dataptr);
1608 trace_9p_protocol_dump(clnt, &req->rc);
1609 p9_tag_remove(clnt, req);
1612 if (rsize < count) {
1613 pr_err("bogus RREAD count (%d > %d)\n", count, rsize);
1617 p9_debug(P9_DEBUG_9P, "<<< RREAD count %d\n", count);
1620 int n = copy_to_iter(dataptr, count, to);
1624 p9_tag_remove(clnt, req);
1628 iov_iter_advance(to, count);
1630 p9_tag_remove(clnt, req);
1633 EXPORT_SYMBOL(p9_client_read_once);
1636 p9_client_write(struct p9_fid *fid, u64 offset, struct iov_iter *from, int *err)
1638 struct p9_client *clnt = fid->clnt;
1639 struct p9_req_t *req;
1643 p9_debug(P9_DEBUG_9P, ">>> TWRITE fid %d offset %llu count %zd\n",
1644 fid->fid, offset, iov_iter_count(from));
1646 while (iov_iter_count(from)) {
1647 int count = iov_iter_count(from);
1648 int rsize = fid->iounit;
1650 if (!rsize || rsize > clnt->msize - P9_IOHDRSZ)
1651 rsize = clnt->msize - P9_IOHDRSZ;
1656 /* Don't bother zerocopy for small IO (< 1024) */
1657 if (clnt->trans_mod->zc_request && rsize > 1024) {
1658 req = p9_client_zc_rpc(clnt, P9_TWRITE, NULL, from, 0,
1659 rsize, P9_ZC_HDR_SZ, "dqd",
1660 fid->fid, offset, rsize);
1662 req = p9_client_rpc(clnt, P9_TWRITE, "dqV", fid->fid,
1663 offset, rsize, from);
1666 *err = PTR_ERR(req);
1670 *err = p9pdu_readf(&req->rc, clnt->proto_version, "d", &count);
1672 trace_9p_protocol_dump(clnt, &req->rc);
1673 p9_tag_remove(clnt, req);
1676 if (rsize < count) {
1677 pr_err("bogus RWRITE count (%d > %d)\n", count, rsize);
1681 p9_debug(P9_DEBUG_9P, "<<< RWRITE count %d\n", count);
1683 p9_tag_remove(clnt, req);
1684 iov_iter_advance(from, count);
1690 EXPORT_SYMBOL(p9_client_write);
1692 struct p9_wstat *p9_client_stat(struct p9_fid *fid)
1695 struct p9_client *clnt;
1696 struct p9_wstat *ret;
1697 struct p9_req_t *req;
1700 p9_debug(P9_DEBUG_9P, ">>> TSTAT fid %d\n", fid->fid);
1702 ret = kmalloc(sizeof(*ret), GFP_KERNEL);
1704 return ERR_PTR(-ENOMEM);
1709 req = p9_client_rpc(clnt, P9_TSTAT, "d", fid->fid);
1715 err = p9pdu_readf(&req->rc, clnt->proto_version, "wS", &ignored, ret);
1717 trace_9p_protocol_dump(clnt, &req->rc);
1718 p9_tag_remove(clnt, req);
1722 p9_debug(P9_DEBUG_9P,
1723 "<<< RSTAT sz=%x type=%x dev=%x qid=%x.%llx.%x\n"
1724 "<<< mode=%8.8x atime=%8.8x mtime=%8.8x length=%llx\n"
1725 "<<< name=%s uid=%s gid=%s muid=%s extension=(%s)\n"
1726 "<<< uid=%d gid=%d n_muid=%d\n",
1727 ret->size, ret->type, ret->dev, ret->qid.type, ret->qid.path,
1728 ret->qid.version, ret->mode,
1729 ret->atime, ret->mtime, ret->length,
1730 ret->name, ret->uid, ret->gid, ret->muid, ret->extension,
1731 from_kuid(&init_user_ns, ret->n_uid),
1732 from_kgid(&init_user_ns, ret->n_gid),
1733 from_kuid(&init_user_ns, ret->n_muid));
1735 p9_tag_remove(clnt, req);
1740 return ERR_PTR(err);
1742 EXPORT_SYMBOL(p9_client_stat);
1744 struct p9_stat_dotl *p9_client_getattr_dotl(struct p9_fid *fid,
1748 struct p9_client *clnt;
1749 struct p9_stat_dotl *ret;
1750 struct p9_req_t *req;
1752 p9_debug(P9_DEBUG_9P, ">>> TGETATTR fid %d, request_mask %lld\n",
1753 fid->fid, request_mask);
1755 ret = kmalloc(sizeof(*ret), GFP_KERNEL);
1757 return ERR_PTR(-ENOMEM);
1762 req = p9_client_rpc(clnt, P9_TGETATTR, "dq", fid->fid, request_mask);
1768 err = p9pdu_readf(&req->rc, clnt->proto_version, "A", ret);
1770 trace_9p_protocol_dump(clnt, &req->rc);
1771 p9_tag_remove(clnt, req);
1775 p9_debug(P9_DEBUG_9P, "<<< RGETATTR st_result_mask=%lld\n"
1776 "<<< qid=%x.%llx.%x\n"
1777 "<<< st_mode=%8.8x st_nlink=%llu\n"
1778 "<<< st_uid=%d st_gid=%d\n"
1779 "<<< st_rdev=%llx st_size=%llx st_blksize=%llu st_blocks=%llu\n"
1780 "<<< st_atime_sec=%lld st_atime_nsec=%lld\n"
1781 "<<< st_mtime_sec=%lld st_mtime_nsec=%lld\n"
1782 "<<< st_ctime_sec=%lld st_ctime_nsec=%lld\n"
1783 "<<< st_btime_sec=%lld st_btime_nsec=%lld\n"
1784 "<<< st_gen=%lld st_data_version=%lld\n",
1785 ret->st_result_mask,
1786 ret->qid.type, ret->qid.path, ret->qid.version,
1787 ret->st_mode, ret->st_nlink,
1788 from_kuid(&init_user_ns, ret->st_uid),
1789 from_kgid(&init_user_ns, ret->st_gid),
1790 ret->st_rdev, ret->st_size, ret->st_blksize, ret->st_blocks,
1791 ret->st_atime_sec, ret->st_atime_nsec,
1792 ret->st_mtime_sec, ret->st_mtime_nsec,
1793 ret->st_ctime_sec, ret->st_ctime_nsec,
1794 ret->st_btime_sec, ret->st_btime_nsec,
1795 ret->st_gen, ret->st_data_version);
1797 p9_tag_remove(clnt, req);
1802 return ERR_PTR(err);
1804 EXPORT_SYMBOL(p9_client_getattr_dotl);
1806 static int p9_client_statsize(struct p9_wstat *wst, int proto_version)
1810 /* NOTE: size shouldn't include its own length */
1811 /* size[2] type[2] dev[4] qid[13] */
1812 /* mode[4] atime[4] mtime[4] length[8]*/
1813 /* name[s] uid[s] gid[s] muid[s] */
1814 ret = 2 + 4 + 13 + 4 + 4 + 4 + 8 + 2 + 2 + 2 + 2;
1817 ret += strlen(wst->name);
1819 ret += strlen(wst->uid);
1821 ret += strlen(wst->gid);
1823 ret += strlen(wst->muid);
1825 if (proto_version == p9_proto_2000u ||
1826 proto_version == p9_proto_2000L) {
1827 /* extension[s] n_uid[4] n_gid[4] n_muid[4] */
1828 ret += 2 + 4 + 4 + 4;
1830 ret += strlen(wst->extension);
1836 int p9_client_wstat(struct p9_fid *fid, struct p9_wstat *wst)
1839 struct p9_req_t *req;
1840 struct p9_client *clnt;
1844 wst->size = p9_client_statsize(wst, clnt->proto_version);
1845 p9_debug(P9_DEBUG_9P, ">>> TWSTAT fid %d\n",
1847 p9_debug(P9_DEBUG_9P,
1848 " sz=%x type=%x dev=%x qid=%x.%llx.%x\n"
1849 " mode=%8.8x atime=%8.8x mtime=%8.8x length=%llx\n"
1850 " name=%s uid=%s gid=%s muid=%s extension=(%s)\n"
1851 " uid=%d gid=%d n_muid=%d\n",
1852 wst->size, wst->type, wst->dev, wst->qid.type,
1853 wst->qid.path, wst->qid.version,
1854 wst->mode, wst->atime, wst->mtime, wst->length,
1855 wst->name, wst->uid, wst->gid, wst->muid, wst->extension,
1856 from_kuid(&init_user_ns, wst->n_uid),
1857 from_kgid(&init_user_ns, wst->n_gid),
1858 from_kuid(&init_user_ns, wst->n_muid));
1860 req = p9_client_rpc(clnt, P9_TWSTAT, "dwS",
1861 fid->fid, wst->size + 2, wst);
1867 p9_debug(P9_DEBUG_9P, "<<< RWSTAT fid %d\n", fid->fid);
1869 p9_tag_remove(clnt, req);
1873 EXPORT_SYMBOL(p9_client_wstat);
1875 int p9_client_setattr(struct p9_fid *fid, struct p9_iattr_dotl *p9attr)
1878 struct p9_req_t *req;
1879 struct p9_client *clnt;
1883 p9_debug(P9_DEBUG_9P, ">>> TSETATTR fid %d\n", fid->fid);
1884 p9_debug(P9_DEBUG_9P, " valid=%x mode=%x uid=%d gid=%d size=%lld\n",
1885 p9attr->valid, p9attr->mode,
1886 from_kuid(&init_user_ns, p9attr->uid),
1887 from_kgid(&init_user_ns, p9attr->gid),
1889 p9_debug(P9_DEBUG_9P, " atime_sec=%lld atime_nsec=%lld\n",
1890 p9attr->atime_sec, p9attr->atime_nsec);
1891 p9_debug(P9_DEBUG_9P, " mtime_sec=%lld mtime_nsec=%lld\n",
1892 p9attr->mtime_sec, p9attr->mtime_nsec);
1894 req = p9_client_rpc(clnt, P9_TSETATTR, "dI", fid->fid, p9attr);
1900 p9_debug(P9_DEBUG_9P, "<<< RSETATTR fid %d\n", fid->fid);
1901 p9_tag_remove(clnt, req);
1905 EXPORT_SYMBOL(p9_client_setattr);
1907 int p9_client_statfs(struct p9_fid *fid, struct p9_rstatfs *sb)
1910 struct p9_req_t *req;
1911 struct p9_client *clnt;
1916 p9_debug(P9_DEBUG_9P, ">>> TSTATFS fid %d\n", fid->fid);
1918 req = p9_client_rpc(clnt, P9_TSTATFS, "d", fid->fid);
1924 err = p9pdu_readf(&req->rc, clnt->proto_version, "ddqqqqqqd", &sb->type,
1925 &sb->bsize, &sb->blocks, &sb->bfree, &sb->bavail,
1926 &sb->files, &sb->ffree, &sb->fsid, &sb->namelen);
1928 trace_9p_protocol_dump(clnt, &req->rc);
1929 p9_tag_remove(clnt, req);
1933 p9_debug(P9_DEBUG_9P,
1934 "<<< RSTATFS fid %d type 0x%x bsize %u blocks %llu bfree %llu bavail %llu files %llu ffree %llu fsid %llu namelen %u\n",
1935 fid->fid, sb->type, sb->bsize, sb->blocks, sb->bfree,
1936 sb->bavail, sb->files, sb->ffree, sb->fsid, sb->namelen);
1938 p9_tag_remove(clnt, req);
1942 EXPORT_SYMBOL(p9_client_statfs);
1944 int p9_client_rename(struct p9_fid *fid,
1945 struct p9_fid *newdirfid, const char *name)
1948 struct p9_req_t *req;
1949 struct p9_client *clnt;
1954 p9_debug(P9_DEBUG_9P, ">>> TRENAME fid %d newdirfid %d name %s\n",
1955 fid->fid, newdirfid->fid, name);
1957 req = p9_client_rpc(clnt, P9_TRENAME, "dds", fid->fid,
1958 newdirfid->fid, name);
1964 p9_debug(P9_DEBUG_9P, "<<< RRENAME fid %d\n", fid->fid);
1966 p9_tag_remove(clnt, req);
1970 EXPORT_SYMBOL(p9_client_rename);
1972 int p9_client_renameat(struct p9_fid *olddirfid, const char *old_name,
1973 struct p9_fid *newdirfid, const char *new_name)
1976 struct p9_req_t *req;
1977 struct p9_client *clnt;
1980 clnt = olddirfid->clnt;
1982 p9_debug(P9_DEBUG_9P,
1983 ">>> TRENAMEAT olddirfid %d old name %s newdirfid %d new name %s\n",
1984 olddirfid->fid, old_name, newdirfid->fid, new_name);
1986 req = p9_client_rpc(clnt, P9_TRENAMEAT, "dsds", olddirfid->fid,
1987 old_name, newdirfid->fid, new_name);
1993 p9_debug(P9_DEBUG_9P, "<<< RRENAMEAT newdirfid %d new name %s\n",
1994 newdirfid->fid, new_name);
1996 p9_tag_remove(clnt, req);
2000 EXPORT_SYMBOL(p9_client_renameat);
2002 /* An xattrwalk without @attr_name gives the fid for the lisxattr namespace
2004 struct p9_fid *p9_client_xattrwalk(struct p9_fid *file_fid,
2005 const char *attr_name, u64 *attr_size)
2008 struct p9_req_t *req;
2009 struct p9_client *clnt;
2010 struct p9_fid *attr_fid;
2013 clnt = file_fid->clnt;
2014 attr_fid = p9_fid_create(clnt);
2019 p9_debug(P9_DEBUG_9P,
2020 ">>> TXATTRWALK file_fid %d, attr_fid %d name %s\n",
2021 file_fid->fid, attr_fid->fid, attr_name);
2023 req = p9_client_rpc(clnt, P9_TXATTRWALK, "dds",
2024 file_fid->fid, attr_fid->fid, attr_name);
2029 err = p9pdu_readf(&req->rc, clnt->proto_version, "q", attr_size);
2031 trace_9p_protocol_dump(clnt, &req->rc);
2032 p9_tag_remove(clnt, req);
2035 p9_tag_remove(clnt, req);
2036 p9_debug(P9_DEBUG_9P, "<<< RXATTRWALK fid %d size %llu\n",
2037 attr_fid->fid, *attr_size);
2040 p9_client_clunk(attr_fid);
2043 if (attr_fid && attr_fid != file_fid)
2044 p9_fid_destroy(attr_fid);
2046 return ERR_PTR(err);
2048 EXPORT_SYMBOL_GPL(p9_client_xattrwalk);
2050 int p9_client_xattrcreate(struct p9_fid *fid, const char *name,
2051 u64 attr_size, int flags)
2054 struct p9_req_t *req;
2055 struct p9_client *clnt;
2057 p9_debug(P9_DEBUG_9P,
2058 ">>> TXATTRCREATE fid %d name %s size %llu flag %d\n",
2059 fid->fid, name, attr_size, flags);
2062 req = p9_client_rpc(clnt, P9_TXATTRCREATE, "dsqd",
2063 fid->fid, name, attr_size, flags);
2068 p9_debug(P9_DEBUG_9P, "<<< RXATTRCREATE fid %d\n", fid->fid);
2069 p9_tag_remove(clnt, req);
2073 EXPORT_SYMBOL_GPL(p9_client_xattrcreate);
2075 int p9_client_readdir(struct p9_fid *fid, char *data, u32 count, u64 offset)
2077 int err, rsize, non_zc = 0;
2078 struct p9_client *clnt;
2079 struct p9_req_t *req;
2081 struct kvec kv = {.iov_base = data, .iov_len = count};
2084 iov_iter_kvec(&to, READ, &kv, 1, count);
2086 p9_debug(P9_DEBUG_9P, ">>> TREADDIR fid %d offset %llu count %d\n",
2087 fid->fid, offset, count);
2092 rsize = fid->iounit;
2093 if (!rsize || rsize > clnt->msize - P9_READDIRHDRSZ)
2094 rsize = clnt->msize - P9_READDIRHDRSZ;
2099 /* Don't bother zerocopy for small IO (< 1024) */
2100 if (clnt->trans_mod->zc_request && rsize > 1024) {
2101 /* response header len is 11
2102 * PDU Header(7) + IO Size (4)
2104 req = p9_client_zc_rpc(clnt, P9_TREADDIR, &to, NULL, rsize, 0,
2105 11, "dqd", fid->fid, offset, rsize);
2108 req = p9_client_rpc(clnt, P9_TREADDIR, "dqd", fid->fid,
2116 err = p9pdu_readf(&req->rc, clnt->proto_version, "D", &count, &dataptr);
2118 trace_9p_protocol_dump(clnt, &req->rc);
2119 goto free_and_error;
2121 if (rsize < count) {
2122 pr_err("bogus RREADDIR count (%d > %d)\n", count, rsize);
2126 p9_debug(P9_DEBUG_9P, "<<< RREADDIR count %d\n", count);
2129 memmove(data, dataptr, count);
2131 p9_tag_remove(clnt, req);
2135 p9_tag_remove(clnt, req);
2139 EXPORT_SYMBOL(p9_client_readdir);
2141 int p9_client_mknod_dotl(struct p9_fid *fid, const char *name, int mode,
2142 dev_t rdev, kgid_t gid, struct p9_qid *qid)
2145 struct p9_client *clnt;
2146 struct p9_req_t *req;
2150 p9_debug(P9_DEBUG_9P,
2151 ">>> TMKNOD fid %d name %s mode %d major %d minor %d\n",
2152 fid->fid, name, mode, MAJOR(rdev), MINOR(rdev));
2153 req = p9_client_rpc(clnt, P9_TMKNOD, "dsdddg", fid->fid, name, mode,
2154 MAJOR(rdev), MINOR(rdev), gid);
2156 return PTR_ERR(req);
2158 err = p9pdu_readf(&req->rc, clnt->proto_version, "Q", qid);
2160 trace_9p_protocol_dump(clnt, &req->rc);
2163 p9_debug(P9_DEBUG_9P, "<<< RMKNOD qid %x.%llx.%x\n",
2164 qid->type, qid->path, qid->version);
2167 p9_tag_remove(clnt, req);
2170 EXPORT_SYMBOL(p9_client_mknod_dotl);
2172 int p9_client_mkdir_dotl(struct p9_fid *fid, const char *name, int mode,
2173 kgid_t gid, struct p9_qid *qid)
2176 struct p9_client *clnt;
2177 struct p9_req_t *req;
2181 p9_debug(P9_DEBUG_9P, ">>> TMKDIR fid %d name %s mode %d gid %d\n",
2182 fid->fid, name, mode, from_kgid(&init_user_ns, gid));
2183 req = p9_client_rpc(clnt, P9_TMKDIR, "dsdg",
2184 fid->fid, name, mode, gid);
2186 return PTR_ERR(req);
2188 err = p9pdu_readf(&req->rc, clnt->proto_version, "Q", qid);
2190 trace_9p_protocol_dump(clnt, &req->rc);
2193 p9_debug(P9_DEBUG_9P, "<<< RMKDIR qid %x.%llx.%x\n", qid->type,
2194 qid->path, qid->version);
2197 p9_tag_remove(clnt, req);
2200 EXPORT_SYMBOL(p9_client_mkdir_dotl);
2202 int p9_client_lock_dotl(struct p9_fid *fid, struct p9_flock *flock, u8 *status)
2205 struct p9_client *clnt;
2206 struct p9_req_t *req;
2210 p9_debug(P9_DEBUG_9P,
2211 ">>> TLOCK fid %d type %i flags %d start %lld length %lld proc_id %d client_id %s\n",
2212 fid->fid, flock->type, flock->flags, flock->start,
2213 flock->length, flock->proc_id, flock->client_id);
2215 req = p9_client_rpc(clnt, P9_TLOCK, "dbdqqds", fid->fid, flock->type,
2216 flock->flags, flock->start, flock->length,
2217 flock->proc_id, flock->client_id);
2220 return PTR_ERR(req);
2222 err = p9pdu_readf(&req->rc, clnt->proto_version, "b", status);
2224 trace_9p_protocol_dump(clnt, &req->rc);
2227 p9_debug(P9_DEBUG_9P, "<<< RLOCK status %i\n", *status);
2229 p9_tag_remove(clnt, req);
2232 EXPORT_SYMBOL(p9_client_lock_dotl);
2234 int p9_client_getlock_dotl(struct p9_fid *fid, struct p9_getlock *glock)
2237 struct p9_client *clnt;
2238 struct p9_req_t *req;
2242 p9_debug(P9_DEBUG_9P,
2243 ">>> TGETLOCK fid %d, type %i start %lld length %lld proc_id %d client_id %s\n",
2244 fid->fid, glock->type, glock->start, glock->length,
2245 glock->proc_id, glock->client_id);
2247 req = p9_client_rpc(clnt, P9_TGETLOCK, "dbqqds", fid->fid,
2248 glock->type, glock->start, glock->length,
2249 glock->proc_id, glock->client_id);
2252 return PTR_ERR(req);
2254 err = p9pdu_readf(&req->rc, clnt->proto_version, "bqqds", &glock->type,
2255 &glock->start, &glock->length, &glock->proc_id,
2258 trace_9p_protocol_dump(clnt, &req->rc);
2261 p9_debug(P9_DEBUG_9P,
2262 "<<< RGETLOCK type %i start %lld length %lld proc_id %d client_id %s\n",
2263 glock->type, glock->start, glock->length,
2264 glock->proc_id, glock->client_id);
2266 p9_tag_remove(clnt, req);
2269 EXPORT_SYMBOL(p9_client_getlock_dotl);
2271 int p9_client_readlink(struct p9_fid *fid, char **target)
2274 struct p9_client *clnt;
2275 struct p9_req_t *req;
2279 p9_debug(P9_DEBUG_9P, ">>> TREADLINK fid %d\n", fid->fid);
2281 req = p9_client_rpc(clnt, P9_TREADLINK, "d", fid->fid);
2283 return PTR_ERR(req);
2285 err = p9pdu_readf(&req->rc, clnt->proto_version, "s", target);
2287 trace_9p_protocol_dump(clnt, &req->rc);
2290 p9_debug(P9_DEBUG_9P, "<<< RREADLINK target %s\n", *target);
2292 p9_tag_remove(clnt, req);
2295 EXPORT_SYMBOL(p9_client_readlink);
2297 int __init p9_client_init(void)
2299 p9_req_cache = KMEM_CACHE(p9_req_t, SLAB_TYPESAFE_BY_RCU);
2300 return p9_req_cache ? 0 : -ENOMEM;
2303 void __exit p9_client_exit(void)
2305 kmem_cache_destroy(p9_req_cache);