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 <linux/netfs.h>
22 #include <net/9p/9p.h>
23 #include <linux/parser.h>
24 #include <linux/seq_file.h>
25 #include <net/9p/client.h>
26 #include <net/9p/transport.h>
29 #define CREATE_TRACE_POINTS
30 #include <trace/events/9p.h>
32 /* DEFAULT MSIZE = 32 pages worth of payload + P9_HDRSZ +
33 * room for write (16 extra) or read (11 extra) operands.
36 #define DEFAULT_MSIZE ((128 * 1024) + P9_IOHDRSZ)
38 /* Client Option Parsing (code inspired by NFS code)
39 * - a little lazy - parse all client options
50 static const match_table_t tokens
= {
51 {Opt_msize
, "msize=%u"},
52 {Opt_legacy
, "noextend"},
53 {Opt_trans
, "trans=%s"},
54 {Opt_version
, "version=%s"},
58 inline int p9_is_proto_dotl(struct p9_client
*clnt
)
60 return clnt
->proto_version
== p9_proto_2000L
;
62 EXPORT_SYMBOL(p9_is_proto_dotl
);
64 inline int p9_is_proto_dotu(struct p9_client
*clnt
)
66 return clnt
->proto_version
== p9_proto_2000u
;
68 EXPORT_SYMBOL(p9_is_proto_dotu
);
70 int p9_show_client_options(struct seq_file
*m
, struct p9_client
*clnt
)
72 if (clnt
->msize
!= DEFAULT_MSIZE
)
73 seq_printf(m
, ",msize=%u", clnt
->msize
);
74 seq_printf(m
, ",trans=%s", clnt
->trans_mod
->name
);
76 switch (clnt
->proto_version
) {
78 seq_puts(m
, ",noextend");
81 seq_puts(m
, ",version=9p2000.u");
88 if (clnt
->trans_mod
->show_options
)
89 return clnt
->trans_mod
->show_options(m
, clnt
);
92 EXPORT_SYMBOL(p9_show_client_options
);
94 /* Some error codes are taken directly from the server replies,
95 * make sure they are valid.
97 static int safe_errno(int err
)
99 if (err
> 0 || err
< -MAX_ERRNO
) {
100 p9_debug(P9_DEBUG_ERROR
, "Invalid error code %d\n", err
);
106 /* Interpret mount option for protocol version */
107 static int get_protocol_version(char *s
)
109 int version
= -EINVAL
;
111 if (!strcmp(s
, "9p2000")) {
112 version
= p9_proto_legacy
;
113 p9_debug(P9_DEBUG_9P
, "Protocol version: Legacy\n");
114 } else if (!strcmp(s
, "9p2000.u")) {
115 version
= p9_proto_2000u
;
116 p9_debug(P9_DEBUG_9P
, "Protocol version: 9P2000.u\n");
117 } else if (!strcmp(s
, "9p2000.L")) {
118 version
= p9_proto_2000L
;
119 p9_debug(P9_DEBUG_9P
, "Protocol version: 9P2000.L\n");
121 pr_info("Unknown protocol version %s\n", s
);
128 * parse_opts - parse mount options into client structure
129 * @opts: options string passed from mount
130 * @clnt: existing v9fs client information
132 * Return 0 upon success, -ERRNO upon failure
135 static int parse_opts(char *opts
, struct p9_client
*clnt
)
137 char *options
, *tmp_options
;
139 substring_t args
[MAX_OPT_ARGS
];
144 clnt
->proto_version
= p9_proto_2000L
;
145 clnt
->msize
= DEFAULT_MSIZE
;
150 tmp_options
= kstrdup(opts
, GFP_KERNEL
);
153 options
= tmp_options
;
155 while ((p
= strsep(&options
, ",")) != NULL
) {
160 token
= match_token(p
, tokens
, args
);
163 r
= match_int(&args
[0], &option
);
165 p9_debug(P9_DEBUG_ERROR
,
166 "integer field, but no integer?\n");
171 p9_debug(P9_DEBUG_ERROR
,
172 "msize should be at least 4k\n");
176 clnt
->msize
= option
;
179 s
= match_strdup(&args
[0]);
182 p9_debug(P9_DEBUG_ERROR
,
183 "problem allocating copy of trans arg\n");
184 goto free_and_return
;
187 v9fs_put_trans(clnt
->trans_mod
);
188 clnt
->trans_mod
= v9fs_get_trans_by_name(s
);
189 if (!clnt
->trans_mod
) {
190 pr_info("Could not find request transport: %s\n",
197 clnt
->proto_version
= p9_proto_legacy
;
200 s
= match_strdup(&args
[0]);
203 p9_debug(P9_DEBUG_ERROR
,
204 "problem allocating copy of version arg\n");
205 goto free_and_return
;
207 r
= get_protocol_version(s
);
211 clnt
->proto_version
= r
;
221 v9fs_put_trans(clnt
->trans_mod
);
226 static int p9_fcall_init(struct p9_client
*c
, struct p9_fcall
*fc
,
229 if (likely(c
->fcall_cache
) && alloc_msize
== c
->msize
) {
230 fc
->sdata
= kmem_cache_alloc(c
->fcall_cache
, GFP_NOFS
);
231 fc
->cache
= c
->fcall_cache
;
233 fc
->sdata
= kmalloc(alloc_msize
, GFP_NOFS
);
238 fc
->capacity
= alloc_msize
;
244 void p9_fcall_fini(struct p9_fcall
*fc
)
246 /* sdata can be NULL for interrupted requests in trans_rdma,
247 * and kmem_cache_free does not do NULL-check for us
249 if (unlikely(!fc
->sdata
))
253 kmem_cache_free(fc
->cache
, fc
->sdata
);
257 EXPORT_SYMBOL(p9_fcall_fini
);
259 static struct kmem_cache
*p9_req_cache
;
262 * p9_tag_alloc - Allocate a new request.
263 * @c: Client session.
264 * @type: Transaction type.
265 * @t_size: Buffer size for holding this request
266 * (automatic calculation by format template if 0).
267 * @r_size: Buffer size for holding server's reply on this request
268 * (automatic calculation by format template if 0).
269 * @fmt: Format template for assembling 9p request message
270 * (see p9pdu_vwritef).
271 * @ap: Variable arguments to be fed to passed format template
272 * (see p9pdu_vwritef).
274 * Context: Process context.
275 * Return: Pointer to new request.
277 static struct p9_req_t
*
278 p9_tag_alloc(struct p9_client
*c
, int8_t type
, uint t_size
, uint r_size
,
279 const char *fmt
, va_list ap
)
281 struct p9_req_t
*req
= kmem_cache_alloc(p9_req_cache
, GFP_NOFS
);
288 alloc_tsize
= min_t(size_t, c
->msize
,
289 t_size
?: p9_msg_buf_size(c
, type
, fmt
, apc
));
292 alloc_rsize
= min_t(size_t, c
->msize
,
293 r_size
?: p9_msg_buf_size(c
, type
+ 1, fmt
, ap
));
296 return ERR_PTR(-ENOMEM
);
298 if (p9_fcall_init(c
, &req
->tc
, alloc_tsize
))
300 if (p9_fcall_init(c
, &req
->rc
, alloc_rsize
))
303 p9pdu_reset(&req
->tc
);
304 p9pdu_reset(&req
->rc
);
306 req
->status
= REQ_STATUS_ALLOC
;
307 /* refcount needs to be set to 0 before inserting into the idr
308 * so p9_tag_lookup does not accept a request that is not fully
309 * initialized. refcount_set to 2 below will mark request ready.
311 refcount_set(&req
->refcount
, 0);
312 init_waitqueue_head(&req
->wq
);
313 INIT_LIST_HEAD(&req
->req_list
);
315 idr_preload(GFP_NOFS
);
316 spin_lock_irq(&c
->lock
);
317 if (type
== P9_TVERSION
)
318 tag
= idr_alloc(&c
->reqs
, req
, P9_NOTAG
, P9_NOTAG
+ 1,
321 tag
= idr_alloc(&c
->reqs
, req
, 0, P9_NOTAG
, GFP_NOWAIT
);
323 spin_unlock_irq(&c
->lock
);
328 /* Init ref to two because in the general case there is one ref
329 * that is put asynchronously by a writer thread, one ref
330 * temporarily given by p9_tag_lookup and put by p9_client_cb
331 * in the recv thread, and one ref put by p9_req_put in the
332 * main thread. The only exception is virtio that does not use
333 * p9_tag_lookup but does not have a writer thread either
334 * (the write happens synchronously in the request/zc_request
335 * callback), so p9_client_cb eats the second ref there
336 * as the pointer is duplicated directly by virtqueue_add_sgs()
338 refcount_set(&req
->refcount
, 2);
343 p9_fcall_fini(&req
->tc
);
344 p9_fcall_fini(&req
->rc
);
346 kmem_cache_free(p9_req_cache
, req
);
347 return ERR_PTR(-ENOMEM
);
351 * p9_tag_lookup - Look up a request by tag.
352 * @c: Client session.
353 * @tag: Transaction ID.
355 * Context: Any context.
356 * Return: A request, or %NULL if there is no request with that tag.
358 struct p9_req_t
*p9_tag_lookup(struct p9_client
*c
, u16 tag
)
360 struct p9_req_t
*req
;
364 req
= idr_find(&c
->reqs
, tag
);
366 /* We have to be careful with the req found under rcu_read_lock
367 * Thanks to SLAB_TYPESAFE_BY_RCU we can safely try to get the
368 * ref again without corrupting other data, then check again
369 * that the tag matches once we have the ref
371 if (!p9_req_try_get(req
))
373 if (req
->tc
.tag
!= tag
) {
382 EXPORT_SYMBOL(p9_tag_lookup
);
385 * p9_tag_remove - Remove a tag.
386 * @c: Client session.
387 * @r: Request of reference.
389 * Context: Any context.
391 static void p9_tag_remove(struct p9_client
*c
, struct p9_req_t
*r
)
396 p9_debug(P9_DEBUG_MUX
, "freeing clnt %p req %p tag: %d\n", c
, r
, tag
);
397 spin_lock_irqsave(&c
->lock
, flags
);
398 idr_remove(&c
->reqs
, tag
);
399 spin_unlock_irqrestore(&c
->lock
, flags
);
402 int p9_req_put(struct p9_client
*c
, struct p9_req_t
*r
)
404 if (refcount_dec_and_test(&r
->refcount
)) {
407 p9_fcall_fini(&r
->tc
);
408 p9_fcall_fini(&r
->rc
);
409 kmem_cache_free(p9_req_cache
, r
);
414 EXPORT_SYMBOL(p9_req_put
);
417 * p9_tag_cleanup - cleans up tags structure and reclaims resources
418 * @c: v9fs client struct
420 * This frees resources associated with the tags structure
423 static void p9_tag_cleanup(struct p9_client
*c
)
425 struct p9_req_t
*req
;
429 idr_for_each_entry(&c
->reqs
, req
, id
) {
430 pr_info("Tag %d still in use\n", id
);
431 if (p9_req_put(c
, req
) == 0)
432 pr_warn("Packet with tag %d has still references",
439 * p9_client_cb - call back from transport to client
441 * @req: request received
442 * @status: request status, one of REQ_STATUS_*
445 void p9_client_cb(struct p9_client
*c
, struct p9_req_t
*req
, int status
)
447 p9_debug(P9_DEBUG_MUX
, " tag %d\n", req
->tc
.tag
);
449 /* This barrier is needed to make sure any change made to req before
450 * the status change is visible to another thread
453 WRITE_ONCE(req
->status
, status
);
456 p9_debug(P9_DEBUG_MUX
, "wakeup: %d\n", req
->tc
.tag
);
459 EXPORT_SYMBOL(p9_client_cb
);
462 * p9_parse_header - parse header arguments out of a packet
463 * @pdu: packet to parse
464 * @size: size of packet
465 * @type: type of request
466 * @tag: tag of packet
467 * @rewind: set if we need to rewind offset afterwards
471 p9_parse_header(struct p9_fcall
*pdu
, int32_t *size
, int8_t *type
,
472 int16_t *tag
, int rewind
)
477 int offset
= pdu
->offset
;
482 err
= p9pdu_readf(pdu
, 0, "dbw", &r_size
, &r_type
, &r_tag
);
484 goto rewind_and_exit
;
493 if (pdu
->size
!= r_size
|| r_size
< 7) {
495 goto rewind_and_exit
;
501 p9_debug(P9_DEBUG_9P
, "<<< size=%d type: %d tag: %d\n",
502 pdu
->size
, pdu
->id
, pdu
->tag
);
506 pdu
->offset
= offset
;
509 EXPORT_SYMBOL(p9_parse_header
);
512 * p9_check_errors - check 9p packet for error return and process it
513 * @c: current client instance
514 * @req: request to parse and check for error conditions
516 * returns error code if one is discovered, otherwise returns 0
518 * this will have to be more complicated if we have multiple
522 static int p9_check_errors(struct p9_client
*c
, struct p9_req_t
*req
)
528 err
= p9_parse_header(&req
->rc
, NULL
, &type
, NULL
, 0);
529 if (req
->rc
.size
> req
->rc
.capacity
&& !req
->rc
.zc
) {
530 pr_err("requested packet size too big: %d does not fit %zu (type=%d)\n",
531 req
->rc
.size
, req
->rc
.capacity
, req
->rc
.id
);
534 /* dump the response from server
535 * This should be after check errors which poplulate pdu_fcall.
537 trace_9p_protocol_dump(c
, &req
->rc
);
539 p9_debug(P9_DEBUG_ERROR
, "couldn't parse header %d\n", err
);
542 if (type
!= P9_RERROR
&& type
!= P9_RLERROR
)
545 if (!p9_is_proto_dotl(c
)) {
548 err
= p9pdu_readf(&req
->rc
, c
->proto_version
, "s?d",
555 if (p9_is_proto_dotu(c
) && ecode
< 512)
559 err
= p9_errstr2errno(ename
, strlen(ename
));
561 p9_debug(P9_DEBUG_9P
, "<<< RERROR (%d) %s\n",
566 err
= p9pdu_readf(&req
->rc
, c
->proto_version
, "d", &ecode
);
571 p9_debug(P9_DEBUG_9P
, "<<< RLERROR (%d)\n", -ecode
);
577 p9_debug(P9_DEBUG_ERROR
, "couldn't parse error%d\n", err
);
582 static struct p9_req_t
*
583 p9_client_rpc(struct p9_client
*c
, int8_t type
, const char *fmt
, ...);
586 * p9_client_flush - flush (cancel) a request
588 * @oldreq: request to cancel
590 * This sents a flush for a particular request and links
591 * the flush request to the original request. The current
592 * code only supports a single flush request although the protocol
593 * allows for multiple flush requests to be sent for a single request.
597 static int p9_client_flush(struct p9_client
*c
, struct p9_req_t
*oldreq
)
599 struct p9_req_t
*req
;
603 err
= p9_parse_header(&oldreq
->tc
, NULL
, NULL
, &oldtag
, 1);
607 p9_debug(P9_DEBUG_9P
, ">>> TFLUSH tag %d\n", oldtag
);
609 req
= p9_client_rpc(c
, P9_TFLUSH
, "w", oldtag
);
613 /* if we haven't received a response for oldreq,
614 * remove it from the list
616 if (READ_ONCE(oldreq
->status
) == REQ_STATUS_SENT
) {
617 if (c
->trans_mod
->cancelled
)
618 c
->trans_mod
->cancelled(c
, oldreq
);
625 static struct p9_req_t
*p9_client_prepare_req(struct p9_client
*c
,
626 int8_t type
, uint t_size
, uint r_size
,
627 const char *fmt
, va_list ap
)
630 struct p9_req_t
*req
;
633 p9_debug(P9_DEBUG_MUX
, "client %p op %d\n", c
, type
);
635 /* we allow for any status other than disconnected */
636 if (c
->status
== Disconnected
)
637 return ERR_PTR(-EIO
);
639 /* if status is begin_disconnected we allow only clunk request */
640 if (c
->status
== BeginDisconnect
&& type
!= P9_TCLUNK
)
641 return ERR_PTR(-EIO
);
644 req
= p9_tag_alloc(c
, type
, t_size
, r_size
, fmt
, apc
);
649 /* marshall the data */
650 p9pdu_prepare(&req
->tc
, req
->tc
.tag
, type
);
651 err
= p9pdu_vwritef(&req
->tc
, c
->proto_version
, fmt
, ap
);
654 p9pdu_finalize(c
, &req
->tc
);
655 trace_9p_client_req(c
, type
, req
->tc
.tag
);
659 /* We have to put also the 2nd reference as it won't be used */
665 * p9_client_rpc - issue a request and wait for a response
667 * @type: type of request
668 * @fmt: protocol format string (see protocol.c)
670 * Returns request structure (which client must free using p9_req_put)
673 static struct p9_req_t
*
674 p9_client_rpc(struct p9_client
*c
, int8_t type
, const char *fmt
, ...)
679 struct p9_req_t
*req
;
680 /* Passing zero for tsize/rsize to p9_client_prepare_req() tells it to
681 * auto determine an appropriate (small) request/response size
682 * according to actual message data being sent. Currently RDMA
683 * transport is excluded from this response message size optimization,
684 * as it would not cope with it, due to its pooled response buffers
685 * (using an optimized request size for RDMA as well though).
687 const uint tsize
= 0;
688 const uint rsize
= c
->trans_mod
->pooled_rbuffers
? c
->msize
: 0;
691 req
= p9_client_prepare_req(c
, type
, tsize
, rsize
, fmt
, ap
);
699 if (signal_pending(current
)) {
701 clear_thread_flag(TIF_SIGPENDING
);
706 err
= c
->trans_mod
->request(c
, req
);
708 /* write won't happen */
710 if (err
!= -ERESTARTSYS
&& err
!= -EFAULT
)
711 c
->status
= Disconnected
;
712 goto recalc_sigpending
;
715 /* Wait for the response */
716 err
= wait_event_killable(req
->wq
,
717 READ_ONCE(req
->status
) >= REQ_STATUS_RCVD
);
719 /* Make sure our req is coherent with regard to updates in other
720 * threads - echoes to wmb() in the callback
724 if (err
== -ERESTARTSYS
&& c
->status
== Connected
&&
727 clear_thread_flag(TIF_SIGPENDING
);
731 if (READ_ONCE(req
->status
) == REQ_STATUS_ERROR
) {
732 p9_debug(P9_DEBUG_ERROR
, "req_status error %d\n", req
->t_err
);
735 if (err
== -ERESTARTSYS
&& c
->status
== Connected
) {
736 p9_debug(P9_DEBUG_MUX
, "flushing\n");
738 clear_thread_flag(TIF_SIGPENDING
);
740 if (c
->trans_mod
->cancel(c
, req
))
741 p9_client_flush(c
, req
);
743 /* if we received the response anyway, don't signal error */
744 if (READ_ONCE(req
->status
) == REQ_STATUS_RCVD
)
749 spin_lock_irqsave(¤t
->sighand
->siglock
, flags
);
751 spin_unlock_irqrestore(¤t
->sighand
->siglock
, flags
);
756 err
= p9_check_errors(c
, req
);
757 trace_9p_client_res(c
, type
, req
->rc
.tag
, err
);
762 return ERR_PTR(safe_errno(err
));
766 * p9_client_zc_rpc - issue a request and wait for a response
768 * @type: type of request
769 * @uidata: destination for zero copy read
770 * @uodata: source for zero copy write
771 * @inlen: read buffer size
772 * @olen: write buffer size
773 * @in_hdrlen: reader header size, This is the size of response protocol data
774 * @fmt: protocol format string (see protocol.c)
776 * Returns request structure (which client must free using p9_req_put)
778 static struct p9_req_t
*p9_client_zc_rpc(struct p9_client
*c
, int8_t type
,
779 struct iov_iter
*uidata
,
780 struct iov_iter
*uodata
,
781 int inlen
, int olen
, int in_hdrlen
,
782 const char *fmt
, ...)
787 struct p9_req_t
*req
;
790 /* We allocate a inline protocol data of only 4k bytes.
791 * The actual content is passed in zero-copy fashion.
793 req
= p9_client_prepare_req(c
, type
, P9_ZC_HDR_SZ
, P9_ZC_HDR_SZ
, fmt
, ap
);
801 if (signal_pending(current
)) {
803 clear_thread_flag(TIF_SIGPENDING
);
808 err
= c
->trans_mod
->zc_request(c
, req
, uidata
, uodata
,
809 inlen
, olen
, in_hdrlen
);
812 c
->status
= Disconnected
;
813 if (err
!= -ERESTARTSYS
)
814 goto recalc_sigpending
;
816 if (READ_ONCE(req
->status
) == REQ_STATUS_ERROR
) {
817 p9_debug(P9_DEBUG_ERROR
, "req_status error %d\n", req
->t_err
);
820 if (err
== -ERESTARTSYS
&& c
->status
== Connected
) {
821 p9_debug(P9_DEBUG_MUX
, "flushing\n");
823 clear_thread_flag(TIF_SIGPENDING
);
825 if (c
->trans_mod
->cancel(c
, req
))
826 p9_client_flush(c
, req
);
828 /* if we received the response anyway, don't signal error */
829 if (READ_ONCE(req
->status
) == REQ_STATUS_RCVD
)
834 spin_lock_irqsave(¤t
->sighand
->siglock
, flags
);
836 spin_unlock_irqrestore(¤t
->sighand
->siglock
, flags
);
841 err
= p9_check_errors(c
, req
);
842 trace_9p_client_res(c
, type
, req
->rc
.tag
, err
);
847 return ERR_PTR(safe_errno(err
));
850 static struct p9_fid
*p9_fid_create(struct p9_client
*clnt
)
855 p9_debug(P9_DEBUG_FID
, "clnt %p\n", clnt
);
856 fid
= kzalloc(sizeof(*fid
), GFP_KERNEL
);
861 fid
->uid
= current_fsuid();
863 refcount_set(&fid
->count
, 1);
865 idr_preload(GFP_KERNEL
);
866 spin_lock_irq(&clnt
->lock
);
867 ret
= idr_alloc_u32(&clnt
->fids
, fid
, &fid
->fid
, P9_NOFID
- 1,
869 spin_unlock_irq(&clnt
->lock
);
872 trace_9p_fid_ref(fid
, P9_FID_REF_CREATE
);
880 static void p9_fid_destroy(struct p9_fid
*fid
)
882 struct p9_client
*clnt
;
885 p9_debug(P9_DEBUG_FID
, "fid %d\n", fid
->fid
);
886 trace_9p_fid_ref(fid
, P9_FID_REF_DESTROY
);
888 spin_lock_irqsave(&clnt
->lock
, flags
);
889 idr_remove(&clnt
->fids
, fid
->fid
);
890 spin_unlock_irqrestore(&clnt
->lock
, flags
);
895 /* We also need to export tracepoint symbols for tracepoint_enabled() */
896 EXPORT_TRACEPOINT_SYMBOL(9p_fid_ref
);
898 void do_trace_9p_fid_get(struct p9_fid
*fid
)
900 trace_9p_fid_ref(fid
, P9_FID_REF_GET
);
902 EXPORT_SYMBOL(do_trace_9p_fid_get
);
904 void do_trace_9p_fid_put(struct p9_fid
*fid
)
906 trace_9p_fid_ref(fid
, P9_FID_REF_PUT
);
908 EXPORT_SYMBOL(do_trace_9p_fid_put
);
910 static int p9_client_version(struct p9_client
*c
)
913 struct p9_req_t
*req
;
914 char *version
= NULL
;
917 p9_debug(P9_DEBUG_9P
, ">>> TVERSION msize %d protocol %d\n",
918 c
->msize
, c
->proto_version
);
920 switch (c
->proto_version
) {
922 req
= p9_client_rpc(c
, P9_TVERSION
, "ds",
923 c
->msize
, "9P2000.L");
926 req
= p9_client_rpc(c
, P9_TVERSION
, "ds",
927 c
->msize
, "9P2000.u");
929 case p9_proto_legacy
:
930 req
= p9_client_rpc(c
, P9_TVERSION
, "ds",
940 err
= p9pdu_readf(&req
->rc
, c
->proto_version
, "ds", &msize
, &version
);
942 p9_debug(P9_DEBUG_9P
, "version error %d\n", err
);
943 trace_9p_protocol_dump(c
, &req
->rc
);
947 p9_debug(P9_DEBUG_9P
, "<<< RVERSION msize %d %s\n", msize
, version
);
948 if (!strncmp(version
, "9P2000.L", 8)) {
949 c
->proto_version
= p9_proto_2000L
;
950 } else if (!strncmp(version
, "9P2000.u", 8)) {
951 c
->proto_version
= p9_proto_2000u
;
952 } else if (!strncmp(version
, "9P2000", 6)) {
953 c
->proto_version
= p9_proto_legacy
;
955 p9_debug(P9_DEBUG_ERROR
,
956 "server returned an unknown version: %s\n", version
);
962 p9_debug(P9_DEBUG_ERROR
,
963 "server returned a msize < 4096: %d\n", msize
);
967 if (msize
< c
->msize
)
977 struct p9_client
*p9_client_create(const char *dev_name
, char *options
)
980 static atomic_t seqno
= ATOMIC_INIT(0);
981 struct p9_client
*clnt
;
985 clnt
= kmalloc(sizeof(*clnt
), GFP_KERNEL
);
987 return ERR_PTR(-ENOMEM
);
989 clnt
->trans_mod
= NULL
;
991 clnt
->fcall_cache
= NULL
;
993 client_id
= utsname()->nodename
;
994 memcpy(clnt
->name
, client_id
, strlen(client_id
) + 1);
996 spin_lock_init(&clnt
->lock
);
997 idr_init(&clnt
->fids
);
998 idr_init(&clnt
->reqs
);
1000 err
= parse_opts(options
, clnt
);
1004 if (!clnt
->trans_mod
)
1005 clnt
->trans_mod
= v9fs_get_default_trans();
1007 if (!clnt
->trans_mod
) {
1008 err
= -EPROTONOSUPPORT
;
1009 p9_debug(P9_DEBUG_ERROR
,
1010 "No transport defined or default transport\n");
1014 p9_debug(P9_DEBUG_MUX
, "clnt %p trans %p msize %d protocol %d\n",
1015 clnt
, clnt
->trans_mod
, clnt
->msize
, clnt
->proto_version
);
1017 err
= clnt
->trans_mod
->create(clnt
, dev_name
, options
);
1021 if (clnt
->msize
> clnt
->trans_mod
->maxsize
) {
1022 clnt
->msize
= clnt
->trans_mod
->maxsize
;
1023 pr_info("Limiting 'msize' to %d as this is the maximum "
1024 "supported by transport %s\n",
1025 clnt
->msize
, clnt
->trans_mod
->name
1029 if (clnt
->msize
< 4096) {
1030 p9_debug(P9_DEBUG_ERROR
,
1031 "Please specify a msize of at least 4k\n");
1036 err
= p9_client_version(clnt
);
1040 cache_name
= kasprintf(GFP_KERNEL
,
1041 "9p-fcall-cache-%u", atomic_inc_return(&seqno
));
1047 /* P9_HDRSZ + 4 is the smallest packet header we can have that is
1048 * followed by data accessed from userspace by read
1051 kmem_cache_create_usercopy(cache_name
, clnt
->msize
,
1053 clnt
->msize
- (P9_HDRSZ
+ 4),
1060 clnt
->trans_mod
->close(clnt
);
1062 v9fs_put_trans(clnt
->trans_mod
);
1065 return ERR_PTR(err
);
1067 EXPORT_SYMBOL(p9_client_create
);
1069 void p9_client_destroy(struct p9_client
*clnt
)
1074 p9_debug(P9_DEBUG_MUX
, "clnt %p\n", clnt
);
1076 if (clnt
->trans_mod
)
1077 clnt
->trans_mod
->close(clnt
);
1079 v9fs_put_trans(clnt
->trans_mod
);
1081 idr_for_each_entry(&clnt
->fids
, fid
, id
) {
1082 pr_info("Found fid %d not clunked\n", fid
->fid
);
1083 p9_fid_destroy(fid
);
1086 p9_tag_cleanup(clnt
);
1088 kmem_cache_destroy(clnt
->fcall_cache
);
1091 EXPORT_SYMBOL(p9_client_destroy
);
1093 void p9_client_disconnect(struct p9_client
*clnt
)
1095 p9_debug(P9_DEBUG_9P
, "clnt %p\n", clnt
);
1096 clnt
->status
= Disconnected
;
1098 EXPORT_SYMBOL(p9_client_disconnect
);
1100 void p9_client_begin_disconnect(struct p9_client
*clnt
)
1102 p9_debug(P9_DEBUG_9P
, "clnt %p\n", clnt
);
1103 clnt
->status
= BeginDisconnect
;
1105 EXPORT_SYMBOL(p9_client_begin_disconnect
);
1107 struct p9_fid
*p9_client_attach(struct p9_client
*clnt
, struct p9_fid
*afid
,
1108 const char *uname
, kuid_t n_uname
,
1112 struct p9_req_t
*req
;
1116 p9_debug(P9_DEBUG_9P
, ">>> TATTACH afid %d uname %s aname %s\n",
1117 afid
? afid
->fid
: -1, uname
, aname
);
1118 fid
= p9_fid_create(clnt
);
1125 req
= p9_client_rpc(clnt
, P9_TATTACH
, "ddss?u", fid
->fid
,
1126 afid
? afid
->fid
: P9_NOFID
, uname
, aname
, n_uname
);
1132 err
= p9pdu_readf(&req
->rc
, clnt
->proto_version
, "Q", &qid
);
1134 trace_9p_protocol_dump(clnt
, &req
->rc
);
1135 p9_req_put(clnt
, req
);
1139 p9_debug(P9_DEBUG_9P
, "<<< RATTACH qid %x.%llx.%x\n",
1140 qid
.type
, qid
.path
, qid
.version
);
1142 memmove(&fid
->qid
, &qid
, sizeof(struct p9_qid
));
1144 p9_req_put(clnt
, req
);
1149 p9_fid_destroy(fid
);
1150 return ERR_PTR(err
);
1152 EXPORT_SYMBOL(p9_client_attach
);
1154 struct p9_fid
*p9_client_walk(struct p9_fid
*oldfid
, uint16_t nwname
,
1155 const unsigned char * const *wnames
, int clone
)
1158 struct p9_client
*clnt
;
1160 struct p9_qid
*wqids
;
1161 struct p9_req_t
*req
;
1165 clnt
= oldfid
->clnt
;
1167 fid
= p9_fid_create(clnt
);
1173 fid
->uid
= oldfid
->uid
;
1178 p9_debug(P9_DEBUG_9P
, ">>> TWALK fids %d,%d nwname %ud wname[0] %s\n",
1179 oldfid
->fid
, fid
->fid
, nwname
, wnames
? wnames
[0] : NULL
);
1180 req
= p9_client_rpc(clnt
, P9_TWALK
, "ddT", oldfid
->fid
, fid
->fid
,
1187 err
= p9pdu_readf(&req
->rc
, clnt
->proto_version
, "R", &nwqids
, &wqids
);
1189 trace_9p_protocol_dump(clnt
, &req
->rc
);
1190 p9_req_put(clnt
, req
);
1193 p9_req_put(clnt
, req
);
1195 p9_debug(P9_DEBUG_9P
, "<<< RWALK nwqid %d:\n", nwqids
);
1197 if (nwqids
!= nwname
) {
1202 for (count
= 0; count
< nwqids
; count
++)
1203 p9_debug(P9_DEBUG_9P
, "<<< [%d] %x.%llx.%x\n",
1204 count
, wqids
[count
].type
,
1206 wqids
[count
].version
);
1209 memmove(&fid
->qid
, &wqids
[nwqids
- 1], sizeof(struct p9_qid
));
1211 memmove(&fid
->qid
, &oldfid
->qid
, sizeof(struct p9_qid
));
1222 if (fid
&& fid
!= oldfid
)
1223 p9_fid_destroy(fid
);
1225 return ERR_PTR(err
);
1227 EXPORT_SYMBOL(p9_client_walk
);
1229 int p9_client_open(struct p9_fid
*fid
, int mode
)
1232 struct p9_client
*clnt
;
1233 struct p9_req_t
*req
;
1238 p9_debug(P9_DEBUG_9P
, ">>> %s fid %d mode %d\n",
1239 p9_is_proto_dotl(clnt
) ? "TLOPEN" : "TOPEN", fid
->fid
, mode
);
1241 if (fid
->mode
!= -1)
1244 if (p9_is_proto_dotl(clnt
))
1245 req
= p9_client_rpc(clnt
, P9_TLOPEN
, "dd", fid
->fid
, mode
& P9L_MODE_MASK
);
1247 req
= p9_client_rpc(clnt
, P9_TOPEN
, "db", fid
->fid
, mode
& P9L_MODE_MASK
);
1253 err
= p9pdu_readf(&req
->rc
, clnt
->proto_version
, "Qd", &qid
, &iounit
);
1255 trace_9p_protocol_dump(clnt
, &req
->rc
);
1256 goto free_and_error
;
1259 p9_debug(P9_DEBUG_9P
, "<<< %s qid %x.%llx.%x iounit %x\n",
1260 p9_is_proto_dotl(clnt
) ? "RLOPEN" : "ROPEN", qid
.type
,
1261 qid
.path
, qid
.version
, iounit
);
1263 memmove(&fid
->qid
, &qid
, sizeof(struct p9_qid
));
1265 fid
->iounit
= iounit
;
1268 p9_req_put(clnt
, req
);
1272 EXPORT_SYMBOL(p9_client_open
);
1274 int p9_client_create_dotl(struct p9_fid
*ofid
, const char *name
, u32 flags
,
1275 u32 mode
, kgid_t gid
, struct p9_qid
*qid
)
1278 struct p9_client
*clnt
;
1279 struct p9_req_t
*req
;
1282 p9_debug(P9_DEBUG_9P
,
1283 ">>> TLCREATE fid %d name %s flags %d mode %d gid %d\n",
1284 ofid
->fid
, name
, flags
, mode
,
1285 from_kgid(&init_user_ns
, gid
));
1288 if (ofid
->mode
!= -1)
1291 req
= p9_client_rpc(clnt
, P9_TLCREATE
, "dsddg", ofid
->fid
, name
, flags
,
1292 mode
& P9L_MODE_MASK
, gid
);
1298 err
= p9pdu_readf(&req
->rc
, clnt
->proto_version
, "Qd", qid
, &iounit
);
1300 trace_9p_protocol_dump(clnt
, &req
->rc
);
1301 goto free_and_error
;
1304 p9_debug(P9_DEBUG_9P
, "<<< RLCREATE qid %x.%llx.%x iounit %x\n",
1305 qid
->type
, qid
->path
, qid
->version
, iounit
);
1307 memmove(&ofid
->qid
, qid
, sizeof(struct p9_qid
));
1309 ofid
->iounit
= iounit
;
1312 p9_req_put(clnt
, req
);
1316 EXPORT_SYMBOL(p9_client_create_dotl
);
1318 int p9_client_fcreate(struct p9_fid
*fid
, const char *name
, u32 perm
, int mode
,
1322 struct p9_client
*clnt
;
1323 struct p9_req_t
*req
;
1327 p9_debug(P9_DEBUG_9P
, ">>> TCREATE fid %d name %s perm %d mode %d\n",
1328 fid
->fid
, name
, perm
, mode
);
1331 if (fid
->mode
!= -1)
1334 req
= p9_client_rpc(clnt
, P9_TCREATE
, "dsdb?s", fid
->fid
, name
, perm
,
1335 mode
& P9L_MODE_MASK
, extension
);
1341 err
= p9pdu_readf(&req
->rc
, clnt
->proto_version
, "Qd", &qid
, &iounit
);
1343 trace_9p_protocol_dump(clnt
, &req
->rc
);
1344 goto free_and_error
;
1347 p9_debug(P9_DEBUG_9P
, "<<< RCREATE qid %x.%llx.%x iounit %x\n",
1348 qid
.type
, qid
.path
, qid
.version
, iounit
);
1350 memmove(&fid
->qid
, &qid
, sizeof(struct p9_qid
));
1352 fid
->iounit
= iounit
;
1355 p9_req_put(clnt
, req
);
1359 EXPORT_SYMBOL(p9_client_fcreate
);
1361 int p9_client_symlink(struct p9_fid
*dfid
, const char *name
,
1362 const char *symtgt
, kgid_t gid
, struct p9_qid
*qid
)
1365 struct p9_client
*clnt
;
1366 struct p9_req_t
*req
;
1368 p9_debug(P9_DEBUG_9P
, ">>> TSYMLINK dfid %d name %s symtgt %s\n",
1369 dfid
->fid
, name
, symtgt
);
1372 req
= p9_client_rpc(clnt
, P9_TSYMLINK
, "dssg", dfid
->fid
, name
, symtgt
,
1379 err
= p9pdu_readf(&req
->rc
, clnt
->proto_version
, "Q", qid
);
1381 trace_9p_protocol_dump(clnt
, &req
->rc
);
1382 goto free_and_error
;
1385 p9_debug(P9_DEBUG_9P
, "<<< RSYMLINK qid %x.%llx.%x\n",
1386 qid
->type
, qid
->path
, qid
->version
);
1389 p9_req_put(clnt
, req
);
1393 EXPORT_SYMBOL(p9_client_symlink
);
1395 int p9_client_link(struct p9_fid
*dfid
, struct p9_fid
*oldfid
, const char *newname
)
1397 struct p9_client
*clnt
;
1398 struct p9_req_t
*req
;
1400 p9_debug(P9_DEBUG_9P
, ">>> TLINK dfid %d oldfid %d newname %s\n",
1401 dfid
->fid
, oldfid
->fid
, newname
);
1403 req
= p9_client_rpc(clnt
, P9_TLINK
, "dds", dfid
->fid
, oldfid
->fid
,
1406 return PTR_ERR(req
);
1408 p9_debug(P9_DEBUG_9P
, "<<< RLINK\n");
1409 p9_req_put(clnt
, req
);
1412 EXPORT_SYMBOL(p9_client_link
);
1414 int p9_client_fsync(struct p9_fid
*fid
, int datasync
)
1417 struct p9_client
*clnt
;
1418 struct p9_req_t
*req
;
1420 p9_debug(P9_DEBUG_9P
, ">>> TFSYNC fid %d datasync:%d\n",
1421 fid
->fid
, datasync
);
1424 req
= p9_client_rpc(clnt
, P9_TFSYNC
, "dd", fid
->fid
, datasync
);
1430 p9_debug(P9_DEBUG_9P
, "<<< RFSYNC fid %d\n", fid
->fid
);
1432 p9_req_put(clnt
, req
);
1437 EXPORT_SYMBOL(p9_client_fsync
);
1439 int p9_client_clunk(struct p9_fid
*fid
)
1442 struct p9_client
*clnt
;
1443 struct p9_req_t
*req
;
1447 p9_debug(P9_DEBUG_9P
, ">>> TCLUNK fid %d (try %d)\n",
1451 req
= p9_client_rpc(clnt
, P9_TCLUNK
, "d", fid
->fid
);
1457 p9_debug(P9_DEBUG_9P
, "<<< RCLUNK fid %d\n", fid
->fid
);
1459 p9_req_put(clnt
, req
);
1461 /* Fid is not valid even after a failed clunk
1462 * If interrupted, retry once then give up and
1463 * leak fid until umount.
1465 if (err
== -ERESTARTSYS
) {
1469 p9_fid_destroy(fid
);
1473 EXPORT_SYMBOL(p9_client_clunk
);
1475 int p9_client_remove(struct p9_fid
*fid
)
1478 struct p9_client
*clnt
;
1479 struct p9_req_t
*req
;
1481 p9_debug(P9_DEBUG_9P
, ">>> TREMOVE fid %d\n", fid
->fid
);
1484 req
= p9_client_rpc(clnt
, P9_TREMOVE
, "d", fid
->fid
);
1490 p9_debug(P9_DEBUG_9P
, "<<< RREMOVE fid %d\n", fid
->fid
);
1492 p9_req_put(clnt
, req
);
1494 if (err
== -ERESTARTSYS
)
1497 p9_fid_destroy(fid
);
1500 EXPORT_SYMBOL(p9_client_remove
);
1502 int p9_client_unlinkat(struct p9_fid
*dfid
, const char *name
, int flags
)
1505 struct p9_req_t
*req
;
1506 struct p9_client
*clnt
;
1508 p9_debug(P9_DEBUG_9P
, ">>> TUNLINKAT fid %d %s %d\n",
1509 dfid
->fid
, name
, flags
);
1512 req
= p9_client_rpc(clnt
, P9_TUNLINKAT
, "dsd", dfid
->fid
, name
, flags
);
1517 p9_debug(P9_DEBUG_9P
, "<<< RUNLINKAT fid %d %s\n", dfid
->fid
, name
);
1519 p9_req_put(clnt
, req
);
1523 EXPORT_SYMBOL(p9_client_unlinkat
);
1526 p9_client_read(struct p9_fid
*fid
, u64 offset
, struct iov_iter
*to
, int *err
)
1531 while (iov_iter_count(to
)) {
1534 count
= p9_client_read_once(fid
, offset
, to
, err
);
1542 EXPORT_SYMBOL(p9_client_read
);
1545 p9_client_read_once(struct p9_fid
*fid
, u64 offset
, struct iov_iter
*to
,
1548 struct p9_client
*clnt
= fid
->clnt
;
1549 struct p9_req_t
*req
;
1550 int count
= iov_iter_count(to
);
1551 int rsize
, received
, non_zc
= 0;
1555 p9_debug(P9_DEBUG_9P
, ">>> TREAD fid %d offset %llu %zu\n",
1556 fid
->fid
, offset
, iov_iter_count(to
));
1558 rsize
= fid
->iounit
;
1559 if (!rsize
|| rsize
> clnt
->msize
- P9_IOHDRSZ
)
1560 rsize
= clnt
->msize
- P9_IOHDRSZ
;
1565 /* Don't bother zerocopy for small IO (< 1024) */
1566 if (clnt
->trans_mod
->zc_request
&& rsize
> 1024) {
1567 /* response header len is 11
1568 * PDU Header(7) + IO Size (4)
1570 req
= p9_client_zc_rpc(clnt
, P9_TREAD
, to
, NULL
, rsize
,
1571 0, 11, "dqd", fid
->fid
,
1575 req
= p9_client_rpc(clnt
, P9_TREAD
, "dqd", fid
->fid
, offset
,
1579 *err
= PTR_ERR(req
);
1581 iov_iter_revert(to
, count
- iov_iter_count(to
));
1585 *err
= p9pdu_readf(&req
->rc
, clnt
->proto_version
,
1586 "D", &received
, &dataptr
);
1589 iov_iter_revert(to
, count
- iov_iter_count(to
));
1590 trace_9p_protocol_dump(clnt
, &req
->rc
);
1591 p9_req_put(clnt
, req
);
1594 if (rsize
< received
) {
1595 pr_err("bogus RREAD count (%d > %d)\n", received
, rsize
);
1599 p9_debug(P9_DEBUG_9P
, "<<< RREAD count %d\n", received
);
1602 int n
= copy_to_iter(dataptr
, received
, to
);
1604 if (n
!= received
) {
1606 p9_req_put(clnt
, req
);
1610 iov_iter_revert(to
, count
- received
- iov_iter_count(to
));
1612 p9_req_put(clnt
, req
);
1615 EXPORT_SYMBOL(p9_client_read_once
);
1618 p9_client_write(struct p9_fid
*fid
, u64 offset
, struct iov_iter
*from
, int *err
)
1620 struct p9_client
*clnt
= fid
->clnt
;
1621 struct p9_req_t
*req
;
1625 while (iov_iter_count(from
)) {
1626 int count
= iov_iter_count(from
);
1627 int rsize
= fid
->iounit
;
1630 if (!rsize
|| rsize
> clnt
->msize
- P9_IOHDRSZ
)
1631 rsize
= clnt
->msize
- P9_IOHDRSZ
;
1636 p9_debug(P9_DEBUG_9P
, ">>> TWRITE fid %d offset %llu count %d (/%d)\n",
1637 fid
->fid
, offset
, rsize
, count
);
1639 /* Don't bother zerocopy for small IO (< 1024) */
1640 if (clnt
->trans_mod
->zc_request
&& rsize
> 1024) {
1641 req
= p9_client_zc_rpc(clnt
, P9_TWRITE
, NULL
, from
, 0,
1642 rsize
, P9_ZC_HDR_SZ
, "dqd",
1643 fid
->fid
, offset
, rsize
);
1645 req
= p9_client_rpc(clnt
, P9_TWRITE
, "dqV", fid
->fid
,
1646 offset
, rsize
, from
);
1649 iov_iter_revert(from
, count
- iov_iter_count(from
));
1650 *err
= PTR_ERR(req
);
1654 *err
= p9pdu_readf(&req
->rc
, clnt
->proto_version
, "d", &written
);
1656 iov_iter_revert(from
, count
- iov_iter_count(from
));
1657 trace_9p_protocol_dump(clnt
, &req
->rc
);
1658 p9_req_put(clnt
, req
);
1661 if (rsize
< written
) {
1662 pr_err("bogus RWRITE count (%d > %d)\n", written
, rsize
);
1666 p9_debug(P9_DEBUG_9P
, "<<< RWRITE count %d\n", written
);
1668 p9_req_put(clnt
, req
);
1669 iov_iter_revert(from
, count
- written
- iov_iter_count(from
));
1675 EXPORT_SYMBOL(p9_client_write
);
1678 p9_client_write_subreq(struct netfs_io_subrequest
*subreq
)
1680 struct netfs_io_request
*wreq
= subreq
->rreq
;
1681 struct p9_fid
*fid
= wreq
->netfs_priv
;
1682 struct p9_client
*clnt
= fid
->clnt
;
1683 struct p9_req_t
*req
;
1684 unsigned long long start
= subreq
->start
+ subreq
->transferred
;
1685 int written
, len
= subreq
->len
- subreq
->transferred
;
1688 p9_debug(P9_DEBUG_9P
, ">>> TWRITE fid %d offset %llu len %d\n",
1689 fid
->fid
, start
, len
);
1691 /* Don't bother zerocopy for small IO (< 1024) */
1692 if (clnt
->trans_mod
->zc_request
&& len
> 1024) {
1693 req
= p9_client_zc_rpc(clnt
, P9_TWRITE
, NULL
, &subreq
->io_iter
,
1694 0, wreq
->len
, P9_ZC_HDR_SZ
, "dqd",
1695 fid
->fid
, start
, len
);
1697 req
= p9_client_rpc(clnt
, P9_TWRITE
, "dqV", fid
->fid
,
1698 start
, len
, &subreq
->io_iter
);
1701 netfs_write_subrequest_terminated(subreq
, PTR_ERR(req
), false);
1705 err
= p9pdu_readf(&req
->rc
, clnt
->proto_version
, "d", &written
);
1707 trace_9p_protocol_dump(clnt
, &req
->rc
);
1708 p9_req_put(clnt
, req
);
1709 netfs_write_subrequest_terminated(subreq
, err
, false);
1713 if (written
> len
) {
1714 pr_err("bogus RWRITE count (%d > %u)\n", written
, len
);
1718 p9_debug(P9_DEBUG_9P
, "<<< RWRITE count %d\n", len
);
1720 p9_req_put(clnt
, req
);
1721 netfs_write_subrequest_terminated(subreq
, written
, false);
1723 EXPORT_SYMBOL(p9_client_write_subreq
);
1725 struct p9_wstat
*p9_client_stat(struct p9_fid
*fid
)
1728 struct p9_client
*clnt
;
1729 struct p9_wstat
*ret
;
1730 struct p9_req_t
*req
;
1733 p9_debug(P9_DEBUG_9P
, ">>> TSTAT fid %d\n", fid
->fid
);
1735 ret
= kmalloc(sizeof(*ret
), GFP_KERNEL
);
1737 return ERR_PTR(-ENOMEM
);
1741 req
= p9_client_rpc(clnt
, P9_TSTAT
, "d", fid
->fid
);
1747 err
= p9pdu_readf(&req
->rc
, clnt
->proto_version
, "wS", &ignored
, ret
);
1749 trace_9p_protocol_dump(clnt
, &req
->rc
);
1750 p9_req_put(clnt
, req
);
1754 p9_debug(P9_DEBUG_9P
,
1755 "<<< RSTAT sz=%x type=%x dev=%x qid=%x.%llx.%x\n"
1756 "<<< mode=%8.8x atime=%8.8x mtime=%8.8x length=%llx\n"
1757 "<<< name=%s uid=%s gid=%s muid=%s extension=(%s)\n"
1758 "<<< uid=%d gid=%d n_muid=%d\n",
1759 ret
->size
, ret
->type
, ret
->dev
, ret
->qid
.type
, ret
->qid
.path
,
1760 ret
->qid
.version
, ret
->mode
,
1761 ret
->atime
, ret
->mtime
, ret
->length
,
1762 ret
->name
, ret
->uid
, ret
->gid
, ret
->muid
, ret
->extension
,
1763 from_kuid(&init_user_ns
, ret
->n_uid
),
1764 from_kgid(&init_user_ns
, ret
->n_gid
),
1765 from_kuid(&init_user_ns
, ret
->n_muid
));
1767 p9_req_put(clnt
, req
);
1772 return ERR_PTR(err
);
1774 EXPORT_SYMBOL(p9_client_stat
);
1776 struct p9_stat_dotl
*p9_client_getattr_dotl(struct p9_fid
*fid
,
1780 struct p9_client
*clnt
;
1781 struct p9_stat_dotl
*ret
;
1782 struct p9_req_t
*req
;
1784 p9_debug(P9_DEBUG_9P
, ">>> TGETATTR fid %d, request_mask %lld\n",
1785 fid
->fid
, request_mask
);
1787 ret
= kmalloc(sizeof(*ret
), GFP_KERNEL
);
1789 return ERR_PTR(-ENOMEM
);
1793 req
= p9_client_rpc(clnt
, P9_TGETATTR
, "dq", fid
->fid
, request_mask
);
1799 err
= p9pdu_readf(&req
->rc
, clnt
->proto_version
, "A", ret
);
1801 trace_9p_protocol_dump(clnt
, &req
->rc
);
1802 p9_req_put(clnt
, req
);
1806 p9_debug(P9_DEBUG_9P
, "<<< RGETATTR st_result_mask=%lld\n"
1807 "<<< qid=%x.%llx.%x\n"
1808 "<<< st_mode=%8.8x st_nlink=%llu\n"
1809 "<<< st_uid=%d st_gid=%d\n"
1810 "<<< st_rdev=%llx st_size=%llx st_blksize=%llu st_blocks=%llu\n"
1811 "<<< st_atime_sec=%lld st_atime_nsec=%lld\n"
1812 "<<< st_mtime_sec=%lld st_mtime_nsec=%lld\n"
1813 "<<< st_ctime_sec=%lld st_ctime_nsec=%lld\n"
1814 "<<< st_btime_sec=%lld st_btime_nsec=%lld\n"
1815 "<<< st_gen=%lld st_data_version=%lld\n",
1816 ret
->st_result_mask
,
1817 ret
->qid
.type
, ret
->qid
.path
, ret
->qid
.version
,
1818 ret
->st_mode
, ret
->st_nlink
,
1819 from_kuid(&init_user_ns
, ret
->st_uid
),
1820 from_kgid(&init_user_ns
, ret
->st_gid
),
1821 ret
->st_rdev
, ret
->st_size
, ret
->st_blksize
, ret
->st_blocks
,
1822 ret
->st_atime_sec
, ret
->st_atime_nsec
,
1823 ret
->st_mtime_sec
, ret
->st_mtime_nsec
,
1824 ret
->st_ctime_sec
, ret
->st_ctime_nsec
,
1825 ret
->st_btime_sec
, ret
->st_btime_nsec
,
1826 ret
->st_gen
, ret
->st_data_version
);
1828 p9_req_put(clnt
, req
);
1833 return ERR_PTR(err
);
1835 EXPORT_SYMBOL(p9_client_getattr_dotl
);
1837 static int p9_client_statsize(struct p9_wstat
*wst
, int proto_version
)
1841 /* NOTE: size shouldn't include its own length */
1842 /* size[2] type[2] dev[4] qid[13] */
1843 /* mode[4] atime[4] mtime[4] length[8]*/
1844 /* name[s] uid[s] gid[s] muid[s] */
1845 ret
= 2 + 4 + 13 + 4 + 4 + 4 + 8 + 2 + 2 + 2 + 2;
1848 ret
+= strlen(wst
->name
);
1850 ret
+= strlen(wst
->uid
);
1852 ret
+= strlen(wst
->gid
);
1854 ret
+= strlen(wst
->muid
);
1856 if (proto_version
== p9_proto_2000u
||
1857 proto_version
== p9_proto_2000L
) {
1858 /* extension[s] n_uid[4] n_gid[4] n_muid[4] */
1859 ret
+= 2 + 4 + 4 + 4;
1861 ret
+= strlen(wst
->extension
);
1867 int p9_client_wstat(struct p9_fid
*fid
, struct p9_wstat
*wst
)
1870 struct p9_req_t
*req
;
1871 struct p9_client
*clnt
;
1874 wst
->size
= p9_client_statsize(wst
, clnt
->proto_version
);
1875 p9_debug(P9_DEBUG_9P
, ">>> TWSTAT fid %d\n",
1877 p9_debug(P9_DEBUG_9P
,
1878 " sz=%x type=%x dev=%x qid=%x.%llx.%x\n"
1879 " mode=%8.8x atime=%8.8x mtime=%8.8x length=%llx\n"
1880 " name=%s uid=%s gid=%s muid=%s extension=(%s)\n"
1881 " uid=%d gid=%d n_muid=%d\n",
1882 wst
->size
, wst
->type
, wst
->dev
, wst
->qid
.type
,
1883 wst
->qid
.path
, wst
->qid
.version
,
1884 wst
->mode
, wst
->atime
, wst
->mtime
, wst
->length
,
1885 wst
->name
, wst
->uid
, wst
->gid
, wst
->muid
, wst
->extension
,
1886 from_kuid(&init_user_ns
, wst
->n_uid
),
1887 from_kgid(&init_user_ns
, wst
->n_gid
),
1888 from_kuid(&init_user_ns
, wst
->n_muid
));
1890 req
= p9_client_rpc(clnt
, P9_TWSTAT
, "dwS",
1891 fid
->fid
, wst
->size
+ 2, wst
);
1897 p9_debug(P9_DEBUG_9P
, "<<< RWSTAT fid %d\n", fid
->fid
);
1899 p9_req_put(clnt
, req
);
1903 EXPORT_SYMBOL(p9_client_wstat
);
1905 int p9_client_setattr(struct p9_fid
*fid
, struct p9_iattr_dotl
*p9attr
)
1908 struct p9_req_t
*req
;
1909 struct p9_client
*clnt
;
1912 p9_debug(P9_DEBUG_9P
, ">>> TSETATTR fid %d\n", fid
->fid
);
1913 p9_debug(P9_DEBUG_9P
, " valid=%x mode=%x uid=%d gid=%d size=%lld\n",
1914 p9attr
->valid
, p9attr
->mode
,
1915 from_kuid(&init_user_ns
, p9attr
->uid
),
1916 from_kgid(&init_user_ns
, p9attr
->gid
),
1918 p9_debug(P9_DEBUG_9P
, " atime_sec=%lld atime_nsec=%lld\n",
1919 p9attr
->atime_sec
, p9attr
->atime_nsec
);
1920 p9_debug(P9_DEBUG_9P
, " mtime_sec=%lld mtime_nsec=%lld\n",
1921 p9attr
->mtime_sec
, p9attr
->mtime_nsec
);
1923 req
= p9_client_rpc(clnt
, P9_TSETATTR
, "dI", fid
->fid
, p9attr
);
1929 p9_debug(P9_DEBUG_9P
, "<<< RSETATTR fid %d\n", fid
->fid
);
1930 p9_req_put(clnt
, req
);
1934 EXPORT_SYMBOL(p9_client_setattr
);
1936 int p9_client_statfs(struct p9_fid
*fid
, struct p9_rstatfs
*sb
)
1939 struct p9_req_t
*req
;
1940 struct p9_client
*clnt
;
1944 p9_debug(P9_DEBUG_9P
, ">>> TSTATFS fid %d\n", fid
->fid
);
1946 req
= p9_client_rpc(clnt
, P9_TSTATFS
, "d", fid
->fid
);
1952 err
= p9pdu_readf(&req
->rc
, clnt
->proto_version
, "ddqqqqqqd", &sb
->type
,
1953 &sb
->bsize
, &sb
->blocks
, &sb
->bfree
, &sb
->bavail
,
1954 &sb
->files
, &sb
->ffree
, &sb
->fsid
, &sb
->namelen
);
1956 trace_9p_protocol_dump(clnt
, &req
->rc
);
1957 p9_req_put(clnt
, req
);
1961 p9_debug(P9_DEBUG_9P
,
1962 "<<< RSTATFS fid %d type 0x%x bsize %u blocks %llu bfree %llu bavail %llu files %llu ffree %llu fsid %llu namelen %u\n",
1963 fid
->fid
, sb
->type
, sb
->bsize
, sb
->blocks
, sb
->bfree
,
1964 sb
->bavail
, sb
->files
, sb
->ffree
, sb
->fsid
, sb
->namelen
);
1966 p9_req_put(clnt
, req
);
1970 EXPORT_SYMBOL(p9_client_statfs
);
1972 int p9_client_rename(struct p9_fid
*fid
,
1973 struct p9_fid
*newdirfid
, const char *name
)
1976 struct p9_req_t
*req
;
1977 struct p9_client
*clnt
;
1981 p9_debug(P9_DEBUG_9P
, ">>> TRENAME fid %d newdirfid %d name %s\n",
1982 fid
->fid
, newdirfid
->fid
, name
);
1984 req
= p9_client_rpc(clnt
, P9_TRENAME
, "dds", fid
->fid
,
1985 newdirfid
->fid
, name
);
1991 p9_debug(P9_DEBUG_9P
, "<<< RRENAME fid %d\n", fid
->fid
);
1993 p9_req_put(clnt
, req
);
1997 EXPORT_SYMBOL(p9_client_rename
);
1999 int p9_client_renameat(struct p9_fid
*olddirfid
, const char *old_name
,
2000 struct p9_fid
*newdirfid
, const char *new_name
)
2003 struct p9_req_t
*req
;
2004 struct p9_client
*clnt
;
2006 clnt
= olddirfid
->clnt
;
2008 p9_debug(P9_DEBUG_9P
,
2009 ">>> TRENAMEAT olddirfid %d old name %s newdirfid %d new name %s\n",
2010 olddirfid
->fid
, old_name
, newdirfid
->fid
, new_name
);
2012 req
= p9_client_rpc(clnt
, P9_TRENAMEAT
, "dsds", olddirfid
->fid
,
2013 old_name
, newdirfid
->fid
, new_name
);
2019 p9_debug(P9_DEBUG_9P
, "<<< RRENAMEAT newdirfid %d new name %s\n",
2020 newdirfid
->fid
, new_name
);
2022 p9_req_put(clnt
, req
);
2026 EXPORT_SYMBOL(p9_client_renameat
);
2028 /* An xattrwalk without @attr_name gives the fid for the lisxattr namespace
2030 struct p9_fid
*p9_client_xattrwalk(struct p9_fid
*file_fid
,
2031 const char *attr_name
, u64
*attr_size
)
2034 struct p9_req_t
*req
;
2035 struct p9_client
*clnt
;
2036 struct p9_fid
*attr_fid
;
2038 clnt
= file_fid
->clnt
;
2039 attr_fid
= p9_fid_create(clnt
);
2044 p9_debug(P9_DEBUG_9P
,
2045 ">>> TXATTRWALK file_fid %d, attr_fid %d name '%s'\n",
2046 file_fid
->fid
, attr_fid
->fid
, attr_name
);
2048 req
= p9_client_rpc(clnt
, P9_TXATTRWALK
, "dds",
2049 file_fid
->fid
, attr_fid
->fid
, attr_name
);
2054 err
= p9pdu_readf(&req
->rc
, clnt
->proto_version
, "q", attr_size
);
2056 trace_9p_protocol_dump(clnt
, &req
->rc
);
2057 p9_req_put(clnt
, req
);
2060 p9_req_put(clnt
, req
);
2061 p9_debug(P9_DEBUG_9P
, "<<< RXATTRWALK fid %d size %llu\n",
2062 attr_fid
->fid
, *attr_size
);
2065 p9_fid_put(attr_fid
);
2068 if (attr_fid
&& attr_fid
!= file_fid
)
2069 p9_fid_destroy(attr_fid
);
2071 return ERR_PTR(err
);
2073 EXPORT_SYMBOL_GPL(p9_client_xattrwalk
);
2075 int p9_client_xattrcreate(struct p9_fid
*fid
, const char *name
,
2076 u64 attr_size
, int flags
)
2079 struct p9_req_t
*req
;
2080 struct p9_client
*clnt
;
2082 p9_debug(P9_DEBUG_9P
,
2083 ">>> TXATTRCREATE fid %d name %s size %llu flag %d\n",
2084 fid
->fid
, name
, attr_size
, flags
);
2086 req
= p9_client_rpc(clnt
, P9_TXATTRCREATE
, "dsqd",
2087 fid
->fid
, name
, attr_size
, flags
);
2092 p9_debug(P9_DEBUG_9P
, "<<< RXATTRCREATE fid %d\n", fid
->fid
);
2093 p9_req_put(clnt
, req
);
2097 EXPORT_SYMBOL_GPL(p9_client_xattrcreate
);
2099 int p9_client_readdir(struct p9_fid
*fid
, char *data
, u32 count
, u64 offset
)
2101 int err
, rsize
, non_zc
= 0;
2102 struct p9_client
*clnt
;
2103 struct p9_req_t
*req
;
2105 struct kvec kv
= {.iov_base
= data
, .iov_len
= count
};
2108 iov_iter_kvec(&to
, ITER_DEST
, &kv
, 1, count
);
2110 p9_debug(P9_DEBUG_9P
, ">>> TREADDIR fid %d offset %llu count %d\n",
2111 fid
->fid
, offset
, count
);
2115 rsize
= fid
->iounit
;
2116 if (!rsize
|| rsize
> clnt
->msize
- P9_READDIRHDRSZ
)
2117 rsize
= clnt
->msize
- P9_READDIRHDRSZ
;
2122 /* Don't bother zerocopy for small IO (< 1024) */
2123 if (clnt
->trans_mod
->zc_request
&& rsize
> 1024) {
2124 /* response header len is 11
2125 * PDU Header(7) + IO Size (4)
2127 req
= p9_client_zc_rpc(clnt
, P9_TREADDIR
, &to
, NULL
, rsize
, 0,
2128 11, "dqd", fid
->fid
, offset
, rsize
);
2131 req
= p9_client_rpc(clnt
, P9_TREADDIR
, "dqd", fid
->fid
,
2139 err
= p9pdu_readf(&req
->rc
, clnt
->proto_version
, "D", &count
, &dataptr
);
2141 trace_9p_protocol_dump(clnt
, &req
->rc
);
2142 goto free_and_error
;
2144 if (rsize
< count
) {
2145 pr_err("bogus RREADDIR count (%d > %d)\n", count
, rsize
);
2149 p9_debug(P9_DEBUG_9P
, "<<< RREADDIR count %d\n", count
);
2152 memmove(data
, dataptr
, count
);
2154 p9_req_put(clnt
, req
);
2158 p9_req_put(clnt
, req
);
2162 EXPORT_SYMBOL(p9_client_readdir
);
2164 int p9_client_mknod_dotl(struct p9_fid
*fid
, const char *name
, int mode
,
2165 dev_t rdev
, kgid_t gid
, struct p9_qid
*qid
)
2168 struct p9_client
*clnt
;
2169 struct p9_req_t
*req
;
2172 p9_debug(P9_DEBUG_9P
,
2173 ">>> TMKNOD fid %d name %s mode %d major %d minor %d\n",
2174 fid
->fid
, name
, mode
, MAJOR(rdev
), MINOR(rdev
));
2175 req
= p9_client_rpc(clnt
, P9_TMKNOD
, "dsdddg", fid
->fid
, name
, mode
,
2176 MAJOR(rdev
), MINOR(rdev
), gid
);
2178 return PTR_ERR(req
);
2180 err
= p9pdu_readf(&req
->rc
, clnt
->proto_version
, "Q", qid
);
2182 trace_9p_protocol_dump(clnt
, &req
->rc
);
2185 p9_debug(P9_DEBUG_9P
, "<<< RMKNOD qid %x.%llx.%x\n",
2186 qid
->type
, qid
->path
, qid
->version
);
2189 p9_req_put(clnt
, req
);
2192 EXPORT_SYMBOL(p9_client_mknod_dotl
);
2194 int p9_client_mkdir_dotl(struct p9_fid
*fid
, const char *name
, int mode
,
2195 kgid_t gid
, struct p9_qid
*qid
)
2198 struct p9_client
*clnt
;
2199 struct p9_req_t
*req
;
2202 p9_debug(P9_DEBUG_9P
, ">>> TMKDIR fid %d name %s mode %d gid %d\n",
2203 fid
->fid
, name
, mode
, from_kgid(&init_user_ns
, gid
));
2204 req
= p9_client_rpc(clnt
, P9_TMKDIR
, "dsdg",
2205 fid
->fid
, name
, mode
, gid
);
2207 return PTR_ERR(req
);
2209 err
= p9pdu_readf(&req
->rc
, clnt
->proto_version
, "Q", qid
);
2211 trace_9p_protocol_dump(clnt
, &req
->rc
);
2214 p9_debug(P9_DEBUG_9P
, "<<< RMKDIR qid %x.%llx.%x\n", qid
->type
,
2215 qid
->path
, qid
->version
);
2218 p9_req_put(clnt
, req
);
2221 EXPORT_SYMBOL(p9_client_mkdir_dotl
);
2223 int p9_client_lock_dotl(struct p9_fid
*fid
, struct p9_flock
*flock
, u8
*status
)
2226 struct p9_client
*clnt
;
2227 struct p9_req_t
*req
;
2230 p9_debug(P9_DEBUG_9P
,
2231 ">>> TLOCK fid %d type %i flags %d start %lld length %lld proc_id %d client_id %s\n",
2232 fid
->fid
, flock
->type
, flock
->flags
, flock
->start
,
2233 flock
->length
, flock
->proc_id
, flock
->client_id
);
2235 req
= p9_client_rpc(clnt
, P9_TLOCK
, "dbdqqds", fid
->fid
, flock
->type
,
2236 flock
->flags
, flock
->start
, flock
->length
,
2237 flock
->proc_id
, flock
->client_id
);
2240 return PTR_ERR(req
);
2242 err
= p9pdu_readf(&req
->rc
, clnt
->proto_version
, "b", status
);
2244 trace_9p_protocol_dump(clnt
, &req
->rc
);
2247 p9_debug(P9_DEBUG_9P
, "<<< RLOCK status %i\n", *status
);
2249 p9_req_put(clnt
, req
);
2252 EXPORT_SYMBOL(p9_client_lock_dotl
);
2254 int p9_client_getlock_dotl(struct p9_fid
*fid
, struct p9_getlock
*glock
)
2257 struct p9_client
*clnt
;
2258 struct p9_req_t
*req
;
2261 p9_debug(P9_DEBUG_9P
,
2262 ">>> TGETLOCK fid %d, type %i start %lld length %lld proc_id %d client_id %s\n",
2263 fid
->fid
, glock
->type
, glock
->start
, glock
->length
,
2264 glock
->proc_id
, glock
->client_id
);
2266 req
= p9_client_rpc(clnt
, P9_TGETLOCK
, "dbqqds", fid
->fid
,
2267 glock
->type
, glock
->start
, glock
->length
,
2268 glock
->proc_id
, glock
->client_id
);
2271 return PTR_ERR(req
);
2273 err
= p9pdu_readf(&req
->rc
, clnt
->proto_version
, "bqqds", &glock
->type
,
2274 &glock
->start
, &glock
->length
, &glock
->proc_id
,
2277 trace_9p_protocol_dump(clnt
, &req
->rc
);
2280 p9_debug(P9_DEBUG_9P
,
2281 "<<< RGETLOCK type %i start %lld length %lld proc_id %d client_id %s\n",
2282 glock
->type
, glock
->start
, glock
->length
,
2283 glock
->proc_id
, glock
->client_id
);
2285 p9_req_put(clnt
, req
);
2288 EXPORT_SYMBOL(p9_client_getlock_dotl
);
2290 int p9_client_readlink(struct p9_fid
*fid
, char **target
)
2293 struct p9_client
*clnt
;
2294 struct p9_req_t
*req
;
2297 p9_debug(P9_DEBUG_9P
, ">>> TREADLINK fid %d\n", fid
->fid
);
2299 req
= p9_client_rpc(clnt
, P9_TREADLINK
, "d", fid
->fid
);
2301 return PTR_ERR(req
);
2303 err
= p9pdu_readf(&req
->rc
, clnt
->proto_version
, "s", target
);
2305 trace_9p_protocol_dump(clnt
, &req
->rc
);
2308 p9_debug(P9_DEBUG_9P
, "<<< RREADLINK target %s\n", *target
);
2310 p9_req_put(clnt
, req
);
2313 EXPORT_SYMBOL(p9_client_readlink
);
2315 int __init
p9_client_init(void)
2317 p9_req_cache
= KMEM_CACHE(p9_req_t
, SLAB_TYPESAFE_BY_RCU
);
2318 return p9_req_cache
? 0 : -ENOMEM
;
2321 void __exit
p9_client_exit(void)
2323 kmem_cache_destroy(p9_req_cache
);