6 * Copyright (C) 2008 by Eric Van Hensbergen <ericvh@gmail.com>
7 * Copyright (C) 2007 by Latchesar Ionkov <lucho@ionkov.net>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2
11 * as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to:
20 * Free Software Foundation
21 * 51 Franklin Street, Fifth Floor
22 * Boston, MA 02111-1301 USA
26 #include <linux/module.h>
27 #include <linux/errno.h>
29 #include <linux/poll.h>
30 #include <linux/idr.h>
31 #include <linux/mutex.h>
32 #include <linux/slab.h>
33 #include <linux/sched.h>
34 #include <linux/uaccess.h>
35 #include <net/9p/9p.h>
36 #include <linux/parser.h>
37 #include <net/9p/client.h>
38 #include <net/9p/transport.h>
42 * Client Option Parsing (code inspired by NFS code)
43 * - a little lazy - parse all client options
54 static const match_table_t tokens
= {
55 {Opt_msize
, "msize=%u"},
56 {Opt_legacy
, "noextend"},
57 {Opt_trans
, "trans=%s"},
58 {Opt_version
, "version=%s"},
62 inline int p9_is_proto_dotl(struct p9_client
*clnt
)
64 return (clnt
->proto_version
== p9_proto_2000L
);
66 EXPORT_SYMBOL(p9_is_proto_dotl
);
68 inline int p9_is_proto_dotu(struct p9_client
*clnt
)
70 return (clnt
->proto_version
== p9_proto_2000u
);
72 EXPORT_SYMBOL(p9_is_proto_dotu
);
74 /* Interpret mount option for protocol version */
75 static unsigned char get_protocol_version(const substring_t
*name
)
77 unsigned char version
= -EINVAL
;
78 if (!strncmp("9p2000", name
->from
, name
->to
-name
->from
)) {
79 version
= p9_proto_legacy
;
80 P9_DPRINTK(P9_DEBUG_9P
, "Protocol version: Legacy\n");
81 } else if (!strncmp("9p2000.u", name
->from
, name
->to
-name
->from
)) {
82 version
= p9_proto_2000u
;
83 P9_DPRINTK(P9_DEBUG_9P
, "Protocol version: 9P2000.u\n");
84 } else if (!strncmp("9p2000.L", name
->from
, name
->to
-name
->from
)) {
85 version
= p9_proto_2000L
;
86 P9_DPRINTK(P9_DEBUG_9P
, "Protocol version: 9P2000.L\n");
88 P9_DPRINTK(P9_DEBUG_ERROR
, "Unknown protocol version %s. ",
94 static struct p9_req_t
*
95 p9_client_rpc(struct p9_client
*c
, int8_t type
, const char *fmt
, ...);
98 * parse_options - parse mount options into client structure
99 * @opts: options string passed from mount
100 * @clnt: existing v9fs client information
102 * Return 0 upon success, -ERRNO upon failure
105 static int parse_opts(char *opts
, struct p9_client
*clnt
)
107 char *options
, *tmp_options
;
109 substring_t args
[MAX_OPT_ARGS
];
113 clnt
->proto_version
= p9_proto_2000u
;
119 tmp_options
= kstrdup(opts
, GFP_KERNEL
);
121 P9_DPRINTK(P9_DEBUG_ERROR
,
122 "failed to allocate copy of option string\n");
125 options
= tmp_options
;
127 while ((p
= strsep(&options
, ",")) != NULL
) {
131 token
= match_token(p
, tokens
, args
);
132 if (token
< Opt_trans
) {
133 int r
= match_int(&args
[0], &option
);
135 P9_DPRINTK(P9_DEBUG_ERROR
,
136 "integer field, but no integer?\n");
143 clnt
->msize
= option
;
146 clnt
->trans_mod
= v9fs_get_trans_by_name(&args
[0]);
147 if(clnt
->trans_mod
== NULL
) {
148 P9_DPRINTK(P9_DEBUG_ERROR
,
149 "Could not find request transport: %s\n",
152 goto free_and_return
;
156 clnt
->proto_version
= p9_proto_legacy
;
159 ret
= get_protocol_version(&args
[0]);
161 goto free_and_return
;
162 clnt
->proto_version
= ret
;
175 * p9_tag_alloc - lookup/allocate a request by tag
176 * @c: client session to lookup tag within
177 * @tag: numeric id for transaction
179 * this is a simple array lookup, but will grow the
180 * request_slots as necessary to accomodate transaction
181 * ids which did not previously have a slot.
183 * this code relies on the client spinlock to manage locks, its
184 * possible we should switch to something else, but I'd rather
185 * stick with something low-overhead for the common case.
189 static struct p9_req_t
*p9_tag_alloc(struct p9_client
*c
, u16 tag
)
193 struct p9_req_t
*req
;
195 /* This looks up the original request by tag so we know which
196 * buffer to read the data into */
199 if (tag
>= c
->max_tag
) {
200 spin_lock_irqsave(&c
->lock
, flags
);
201 /* check again since original check was outside of lock */
202 while (tag
>= c
->max_tag
) {
203 row
= (tag
/ P9_ROW_MAXTAG
);
204 c
->reqs
[row
] = kcalloc(P9_ROW_MAXTAG
,
205 sizeof(struct p9_req_t
), GFP_ATOMIC
);
208 printk(KERN_ERR
"Couldn't grow tag array\n");
209 spin_unlock_irqrestore(&c
->lock
, flags
);
210 return ERR_PTR(-ENOMEM
);
212 for (col
= 0; col
< P9_ROW_MAXTAG
; col
++) {
213 c
->reqs
[row
][col
].status
= REQ_STATUS_IDLE
;
214 c
->reqs
[row
][col
].tc
= NULL
;
216 c
->max_tag
+= P9_ROW_MAXTAG
;
218 spin_unlock_irqrestore(&c
->lock
, flags
);
220 row
= tag
/ P9_ROW_MAXTAG
;
221 col
= tag
% P9_ROW_MAXTAG
;
223 req
= &c
->reqs
[row
][col
];
225 req
->wq
= kmalloc(sizeof(wait_queue_head_t
), GFP_KERNEL
);
227 printk(KERN_ERR
"Couldn't grow tag array\n");
228 return ERR_PTR(-ENOMEM
);
230 init_waitqueue_head(req
->wq
);
231 req
->tc
= kmalloc(sizeof(struct p9_fcall
)+c
->msize
,
233 req
->rc
= kmalloc(sizeof(struct p9_fcall
)+c
->msize
,
235 if ((!req
->tc
) || (!req
->rc
)) {
236 printk(KERN_ERR
"Couldn't grow tag array\n");
240 req
->tc
= req
->rc
= NULL
;
242 return ERR_PTR(-ENOMEM
);
244 req
->tc
->sdata
= (char *) req
->tc
+ sizeof(struct p9_fcall
);
245 req
->tc
->capacity
= c
->msize
;
246 req
->rc
->sdata
= (char *) req
->rc
+ sizeof(struct p9_fcall
);
247 req
->rc
->capacity
= c
->msize
;
250 p9pdu_reset(req
->tc
);
251 p9pdu_reset(req
->rc
);
253 req
->tc
->tag
= tag
-1;
254 req
->status
= REQ_STATUS_ALLOC
;
256 return &c
->reqs
[row
][col
];
260 * p9_tag_lookup - lookup a request by tag
261 * @c: client session to lookup tag within
262 * @tag: numeric id for transaction
266 struct p9_req_t
*p9_tag_lookup(struct p9_client
*c
, u16 tag
)
270 /* This looks up the original request by tag so we know which
271 * buffer to read the data into */
274 BUG_ON(tag
>= c
->max_tag
);
276 row
= tag
/ P9_ROW_MAXTAG
;
277 col
= tag
% P9_ROW_MAXTAG
;
279 return &c
->reqs
[row
][col
];
281 EXPORT_SYMBOL(p9_tag_lookup
);
284 * p9_tag_init - setup tags structure and contents
285 * @c: v9fs client struct
287 * This initializes the tags structure for each client instance.
291 static int p9_tag_init(struct p9_client
*c
)
295 c
->tagpool
= p9_idpool_create();
296 if (IS_ERR(c
->tagpool
)) {
297 err
= PTR_ERR(c
->tagpool
);
302 p9_idpool_get(c
->tagpool
); /* reserve tag 0 */
310 * p9_tag_cleanup - cleans up tags structure and reclaims resources
311 * @c: v9fs client struct
313 * This frees resources associated with the tags structure
316 static void p9_tag_cleanup(struct p9_client
*c
)
320 /* check to insure all requests are idle */
321 for (row
= 0; row
< (c
->max_tag
/P9_ROW_MAXTAG
); row
++) {
322 for (col
= 0; col
< P9_ROW_MAXTAG
; col
++) {
323 if (c
->reqs
[row
][col
].status
!= REQ_STATUS_IDLE
) {
324 P9_DPRINTK(P9_DEBUG_MUX
,
325 "Attempting to cleanup non-free tag %d,%d\n",
327 /* TODO: delay execution of cleanup */
334 p9_idpool_destroy(c
->tagpool
);
336 /* free requests associated with tags */
337 for (row
= 0; row
< (c
->max_tag
/P9_ROW_MAXTAG
); row
++) {
338 for (col
= 0; col
< P9_ROW_MAXTAG
; col
++) {
339 kfree(c
->reqs
[row
][col
].wq
);
340 kfree(c
->reqs
[row
][col
].tc
);
341 kfree(c
->reqs
[row
][col
].rc
);
349 * p9_free_req - free a request and clean-up as necessary
351 * r: request to release
355 static void p9_free_req(struct p9_client
*c
, struct p9_req_t
*r
)
357 int tag
= r
->tc
->tag
;
358 P9_DPRINTK(P9_DEBUG_MUX
, "clnt %p req %p tag: %d\n", c
, r
, tag
);
360 r
->status
= REQ_STATUS_IDLE
;
361 if (tag
!= P9_NOTAG
&& p9_idpool_check(tag
, c
->tagpool
))
362 p9_idpool_put(tag
, c
->tagpool
);
366 * p9_client_cb - call back from transport to client
368 * req: request received
371 void p9_client_cb(struct p9_client
*c
, struct p9_req_t
*req
)
373 P9_DPRINTK(P9_DEBUG_MUX
, " tag %d\n", req
->tc
->tag
);
375 P9_DPRINTK(P9_DEBUG_MUX
, "wakeup: %d\n", req
->tc
->tag
);
377 EXPORT_SYMBOL(p9_client_cb
);
380 * p9_parse_header - parse header arguments out of a packet
381 * @pdu: packet to parse
382 * @size: size of packet
383 * @type: type of request
384 * @tag: tag of packet
385 * @rewind: set if we need to rewind offset afterwards
389 p9_parse_header(struct p9_fcall
*pdu
, int32_t *size
, int8_t *type
, int16_t *tag
,
395 int offset
= pdu
->offset
;
402 err
= p9pdu_readf(pdu
, 0, "dbw", &r_size
, &r_type
, &r_tag
);
404 goto rewind_and_exit
;
410 P9_DPRINTK(P9_DEBUG_9P
, "<<< size=%d type: %d tag: %d\n", pdu
->size
,
423 pdu
->offset
= offset
;
426 EXPORT_SYMBOL(p9_parse_header
);
429 * p9_check_errors - check 9p packet for error return and process it
430 * @c: current client instance
431 * @req: request to parse and check for error conditions
433 * returns error code if one is discovered, otherwise returns 0
435 * this will have to be more complicated if we have multiple
439 static int p9_check_errors(struct p9_client
*c
, struct p9_req_t
*req
)
444 err
= p9_parse_header(req
->rc
, NULL
, &type
, NULL
, 0);
446 P9_DPRINTK(P9_DEBUG_ERROR
, "couldn't parse header %d\n", err
);
450 if (type
== P9_RERROR
) {
454 err
= p9pdu_readf(req
->rc
, c
->proto_version
, "s?d",
457 P9_DPRINTK(P9_DEBUG_ERROR
, "couldn't parse error%d\n",
462 if (p9_is_proto_dotu(c
))
465 if (!err
|| !IS_ERR_VALUE(err
))
466 err
= p9_errstr2errno(ename
, strlen(ename
));
468 P9_DPRINTK(P9_DEBUG_9P
, "<<< RERROR (%d) %s\n", -ecode
, ename
);
478 * p9_client_flush - flush (cancel) a request
480 * @oldreq: request to cancel
482 * This sents a flush for a particular requests and links
483 * the flush request to the original request. The current
484 * code only supports a single flush request although the protocol
485 * allows for multiple flush requests to be sent for a single request.
489 static int p9_client_flush(struct p9_client
*c
, struct p9_req_t
*oldreq
)
491 struct p9_req_t
*req
;
495 err
= p9_parse_header(oldreq
->tc
, NULL
, NULL
, &oldtag
, 1);
499 P9_DPRINTK(P9_DEBUG_9P
, ">>> TFLUSH tag %d\n", oldtag
);
501 req
= p9_client_rpc(c
, P9_TFLUSH
, "w", oldtag
);
506 /* if we haven't received a response for oldreq,
507 remove it from the list. */
509 if (oldreq
->status
== REQ_STATUS_FLSH
)
510 list_del(&oldreq
->req_list
);
511 spin_unlock(&c
->lock
);
518 * p9_client_rpc - issue a request and wait for a response
520 * @type: type of request
521 * @fmt: protocol format string (see protocol.c)
523 * Returns request structure (which client must free using p9_free_req)
526 static struct p9_req_t
*
527 p9_client_rpc(struct p9_client
*c
, int8_t type
, const char *fmt
, ...)
531 struct p9_req_t
*req
;
535 P9_DPRINTK(P9_DEBUG_MUX
, "client %p op %d\n", c
, type
);
537 if (c
->status
!= Connected
)
538 return ERR_PTR(-EIO
);
540 if (signal_pending(current
)) {
542 clear_thread_flag(TIF_SIGPENDING
);
547 if (type
!= P9_TVERSION
) {
548 tag
= p9_idpool_get(c
->tagpool
);
550 return ERR_PTR(-ENOMEM
);
553 req
= p9_tag_alloc(c
, tag
);
557 /* marshall the data */
558 p9pdu_prepare(req
->tc
, tag
, type
);
560 err
= p9pdu_vwritef(req
->tc
, c
->proto_version
, fmt
, ap
);
562 p9pdu_finalize(req
->tc
);
564 err
= c
->trans_mod
->request(c
, req
);
566 c
->status
= Disconnected
;
570 P9_DPRINTK(P9_DEBUG_MUX
, "wait %p tag: %d\n", req
->wq
, tag
);
571 err
= wait_event_interruptible(*req
->wq
,
572 req
->status
>= REQ_STATUS_RCVD
);
573 P9_DPRINTK(P9_DEBUG_MUX
, "wait %p tag: %d returned %d\n",
576 if (req
->status
== REQ_STATUS_ERROR
) {
577 P9_DPRINTK(P9_DEBUG_ERROR
, "req_status error %d\n", req
->t_err
);
581 if ((err
== -ERESTARTSYS
) && (c
->status
== Connected
)) {
582 P9_DPRINTK(P9_DEBUG_MUX
, "flushing\n");
584 clear_thread_flag(TIF_SIGPENDING
);
586 if (c
->trans_mod
->cancel(c
, req
))
587 p9_client_flush(c
, req
);
589 /* if we received the response anyway, don't signal error */
590 if (req
->status
== REQ_STATUS_RCVD
)
595 spin_lock_irqsave(¤t
->sighand
->siglock
, flags
);
597 spin_unlock_irqrestore(¤t
->sighand
->siglock
, flags
);
603 err
= p9_check_errors(c
, req
);
605 P9_DPRINTK(P9_DEBUG_MUX
, "exit: client %p op %d\n", c
, type
);
610 P9_DPRINTK(P9_DEBUG_MUX
, "exit: client %p op %d error: %d\n", c
, type
,
616 static struct p9_fid
*p9_fid_create(struct p9_client
*clnt
)
622 P9_DPRINTK(P9_DEBUG_FID
, "clnt %p\n", clnt
);
623 fid
= kmalloc(sizeof(struct p9_fid
), GFP_KERNEL
);
625 return ERR_PTR(-ENOMEM
);
627 ret
= p9_idpool_get(clnt
->fidpool
);
634 memset(&fid
->qid
, 0, sizeof(struct p9_qid
));
636 fid
->uid
= current_fsuid();
639 spin_lock_irqsave(&clnt
->lock
, flags
);
640 list_add(&fid
->flist
, &clnt
->fidlist
);
641 spin_unlock_irqrestore(&clnt
->lock
, flags
);
650 static void p9_fid_destroy(struct p9_fid
*fid
)
652 struct p9_client
*clnt
;
655 P9_DPRINTK(P9_DEBUG_FID
, "fid %d\n", fid
->fid
);
657 p9_idpool_put(fid
->fid
, clnt
->fidpool
);
658 spin_lock_irqsave(&clnt
->lock
, flags
);
659 list_del(&fid
->flist
);
660 spin_unlock_irqrestore(&clnt
->lock
, flags
);
665 int p9_client_version(struct p9_client
*c
)
668 struct p9_req_t
*req
;
672 P9_DPRINTK(P9_DEBUG_9P
, ">>> TVERSION msize %d protocol %d\n",
673 c
->msize
, c
->proto_version
);
675 switch (c
->proto_version
) {
677 req
= p9_client_rpc(c
, P9_TVERSION
, "ds",
678 c
->msize
, "9P2000.L");
681 req
= p9_client_rpc(c
, P9_TVERSION
, "ds",
682 c
->msize
, "9P2000.u");
684 case p9_proto_legacy
:
685 req
= p9_client_rpc(c
, P9_TVERSION
, "ds",
696 err
= p9pdu_readf(req
->rc
, c
->proto_version
, "ds", &msize
, &version
);
698 P9_DPRINTK(P9_DEBUG_9P
, "version error %d\n", err
);
699 p9pdu_dump(1, req
->rc
);
703 P9_DPRINTK(P9_DEBUG_9P
, "<<< RVERSION msize %d %s\n", msize
, version
);
704 if (!strncmp(version
, "9P2000.L", 8))
705 c
->proto_version
= p9_proto_2000L
;
706 else if (!strncmp(version
, "9P2000.u", 8))
707 c
->proto_version
= p9_proto_2000u
;
708 else if (!strncmp(version
, "9P2000", 6))
709 c
->proto_version
= p9_proto_legacy
;
715 if (msize
< c
->msize
)
724 EXPORT_SYMBOL(p9_client_version
);
726 struct p9_client
*p9_client_create(const char *dev_name
, char *options
)
729 struct p9_client
*clnt
;
732 clnt
= kmalloc(sizeof(struct p9_client
), GFP_KERNEL
);
734 return ERR_PTR(-ENOMEM
);
736 clnt
->trans_mod
= NULL
;
738 spin_lock_init(&clnt
->lock
);
739 INIT_LIST_HEAD(&clnt
->fidlist
);
743 err
= parse_opts(options
, clnt
);
747 if (!clnt
->trans_mod
)
748 clnt
->trans_mod
= v9fs_get_default_trans();
750 if (clnt
->trans_mod
== NULL
) {
751 err
= -EPROTONOSUPPORT
;
752 P9_DPRINTK(P9_DEBUG_ERROR
,
753 "No transport defined or default transport\n");
757 clnt
->fidpool
= p9_idpool_create();
758 if (IS_ERR(clnt
->fidpool
)) {
759 err
= PTR_ERR(clnt
->fidpool
);
760 clnt
->fidpool
= NULL
;
764 P9_DPRINTK(P9_DEBUG_MUX
, "clnt %p trans %p msize %d protocol %d\n",
765 clnt
, clnt
->trans_mod
, clnt
->msize
, clnt
->proto_version
);
767 err
= clnt
->trans_mod
->create(clnt
, dev_name
, options
);
769 goto destroy_fidpool
;
771 if ((clnt
->msize
+P9_IOHDRSZ
) > clnt
->trans_mod
->maxsize
)
772 clnt
->msize
= clnt
->trans_mod
->maxsize
-P9_IOHDRSZ
;
774 err
= p9_client_version(clnt
);
781 clnt
->trans_mod
->close(clnt
);
783 p9_idpool_destroy(clnt
->fidpool
);
785 v9fs_put_trans(clnt
->trans_mod
);
790 EXPORT_SYMBOL(p9_client_create
);
792 void p9_client_destroy(struct p9_client
*clnt
)
794 struct p9_fid
*fid
, *fidptr
;
796 P9_DPRINTK(P9_DEBUG_MUX
, "clnt %p\n", clnt
);
799 clnt
->trans_mod
->close(clnt
);
801 v9fs_put_trans(clnt
->trans_mod
);
803 list_for_each_entry_safe(fid
, fidptr
, &clnt
->fidlist
, flist
)
807 p9_idpool_destroy(clnt
->fidpool
);
809 p9_tag_cleanup(clnt
);
813 EXPORT_SYMBOL(p9_client_destroy
);
815 void p9_client_disconnect(struct p9_client
*clnt
)
817 P9_DPRINTK(P9_DEBUG_9P
, "clnt %p\n", clnt
);
818 clnt
->status
= Disconnected
;
820 EXPORT_SYMBOL(p9_client_disconnect
);
822 struct p9_fid
*p9_client_attach(struct p9_client
*clnt
, struct p9_fid
*afid
,
823 char *uname
, u32 n_uname
, char *aname
)
826 struct p9_req_t
*req
;
830 P9_DPRINTK(P9_DEBUG_9P
, ">>> TATTACH afid %d uname %s aname %s\n",
831 afid
? afid
->fid
: -1, uname
, aname
);
834 fid
= p9_fid_create(clnt
);
841 req
= p9_client_rpc(clnt
, P9_TATTACH
, "ddss?d", fid
->fid
,
842 afid
? afid
->fid
: P9_NOFID
, uname
, aname
, n_uname
);
848 err
= p9pdu_readf(req
->rc
, clnt
->proto_version
, "Q", &qid
);
850 p9pdu_dump(1, req
->rc
);
851 p9_free_req(clnt
, req
);
855 P9_DPRINTK(P9_DEBUG_9P
, "<<< RATTACH qid %x.%llx.%x\n",
857 (unsigned long long)qid
.path
,
860 memmove(&fid
->qid
, &qid
, sizeof(struct p9_qid
));
862 p9_free_req(clnt
, req
);
870 EXPORT_SYMBOL(p9_client_attach
);
873 p9_client_auth(struct p9_client
*clnt
, char *uname
, u32 n_uname
, char *aname
)
876 struct p9_req_t
*req
;
880 P9_DPRINTK(P9_DEBUG_9P
, ">>> TAUTH uname %s aname %s\n", uname
, aname
);
883 afid
= p9_fid_create(clnt
);
890 req
= p9_client_rpc(clnt
, P9_TAUTH
, "dss?d",
891 afid
? afid
->fid
: P9_NOFID
, uname
, aname
, n_uname
);
897 err
= p9pdu_readf(req
->rc
, clnt
->proto_version
, "Q", &qid
);
899 p9pdu_dump(1, req
->rc
);
900 p9_free_req(clnt
, req
);
904 P9_DPRINTK(P9_DEBUG_9P
, "<<< RAUTH qid %x.%llx.%x\n",
906 (unsigned long long)qid
.path
,
909 memmove(&afid
->qid
, &qid
, sizeof(struct p9_qid
));
910 p9_free_req(clnt
, req
);
915 p9_fid_destroy(afid
);
918 EXPORT_SYMBOL(p9_client_auth
);
920 struct p9_fid
*p9_client_walk(struct p9_fid
*oldfid
, int nwname
, char **wnames
,
924 struct p9_client
*clnt
;
926 struct p9_qid
*wqids
;
927 struct p9_req_t
*req
;
928 int16_t nwqids
, count
;
933 fid
= p9_fid_create(clnt
);
940 fid
->uid
= oldfid
->uid
;
945 P9_DPRINTK(P9_DEBUG_9P
, ">>> TWALK fids %d,%d nwname %d wname[0] %s\n",
946 oldfid
->fid
, fid
->fid
, nwname
, wnames
? wnames
[0] : NULL
);
948 req
= p9_client_rpc(clnt
, P9_TWALK
, "ddT", oldfid
->fid
, fid
->fid
,
955 err
= p9pdu_readf(req
->rc
, clnt
->proto_version
, "R", &nwqids
, &wqids
);
957 p9pdu_dump(1, req
->rc
);
958 p9_free_req(clnt
, req
);
961 p9_free_req(clnt
, req
);
963 P9_DPRINTK(P9_DEBUG_9P
, "<<< RWALK nwqid %d:\n", nwqids
);
965 if (nwqids
!= nwname
) {
970 for (count
= 0; count
< nwqids
; count
++)
971 P9_DPRINTK(P9_DEBUG_9P
, "<<< [%d] %x.%llx.%x\n",
972 count
, wqids
[count
].type
,
973 (unsigned long long)wqids
[count
].path
,
974 wqids
[count
].version
);
977 memmove(&fid
->qid
, &wqids
[nwqids
- 1], sizeof(struct p9_qid
));
979 fid
->qid
= oldfid
->qid
;
984 p9_client_clunk(fid
);
988 if (fid
&& (fid
!= oldfid
))
993 EXPORT_SYMBOL(p9_client_walk
);
995 int p9_client_open(struct p9_fid
*fid
, int mode
)
998 struct p9_client
*clnt
;
999 struct p9_req_t
*req
;
1003 P9_DPRINTK(P9_DEBUG_9P
, ">>> TOPEN fid %d mode %d\n", fid
->fid
, mode
);
1007 if (fid
->mode
!= -1)
1010 req
= p9_client_rpc(clnt
, P9_TOPEN
, "db", fid
->fid
, mode
);
1016 err
= p9pdu_readf(req
->rc
, clnt
->proto_version
, "Qd", &qid
, &iounit
);
1018 p9pdu_dump(1, req
->rc
);
1019 goto free_and_error
;
1022 P9_DPRINTK(P9_DEBUG_9P
, "<<< ROPEN qid %x.%llx.%x iounit %x\n",
1024 (unsigned long long)qid
.path
,
1025 qid
.version
, iounit
);
1028 fid
->iounit
= iounit
;
1031 p9_free_req(clnt
, req
);
1035 EXPORT_SYMBOL(p9_client_open
);
1037 int p9_client_fcreate(struct p9_fid
*fid
, char *name
, u32 perm
, int mode
,
1041 struct p9_client
*clnt
;
1042 struct p9_req_t
*req
;
1046 P9_DPRINTK(P9_DEBUG_9P
, ">>> TCREATE fid %d name %s perm %d mode %d\n",
1047 fid
->fid
, name
, perm
, mode
);
1051 if (fid
->mode
!= -1)
1054 req
= p9_client_rpc(clnt
, P9_TCREATE
, "dsdb?s", fid
->fid
, name
, perm
,
1061 err
= p9pdu_readf(req
->rc
, clnt
->proto_version
, "Qd", &qid
, &iounit
);
1063 p9pdu_dump(1, req
->rc
);
1064 goto free_and_error
;
1067 P9_DPRINTK(P9_DEBUG_9P
, "<<< RCREATE qid %x.%llx.%x iounit %x\n",
1069 (unsigned long long)qid
.path
,
1070 qid
.version
, iounit
);
1073 fid
->iounit
= iounit
;
1076 p9_free_req(clnt
, req
);
1080 EXPORT_SYMBOL(p9_client_fcreate
);
1082 int p9_client_clunk(struct p9_fid
*fid
)
1085 struct p9_client
*clnt
;
1086 struct p9_req_t
*req
;
1088 P9_DPRINTK(P9_DEBUG_9P
, ">>> TCLUNK fid %d\n", fid
->fid
);
1092 req
= p9_client_rpc(clnt
, P9_TCLUNK
, "d", fid
->fid
);
1098 P9_DPRINTK(P9_DEBUG_9P
, "<<< RCLUNK fid %d\n", fid
->fid
);
1100 p9_free_req(clnt
, req
);
1101 p9_fid_destroy(fid
);
1106 EXPORT_SYMBOL(p9_client_clunk
);
1108 int p9_client_remove(struct p9_fid
*fid
)
1111 struct p9_client
*clnt
;
1112 struct p9_req_t
*req
;
1114 P9_DPRINTK(P9_DEBUG_9P
, ">>> TREMOVE fid %d\n", fid
->fid
);
1118 req
= p9_client_rpc(clnt
, P9_TREMOVE
, "d", fid
->fid
);
1124 P9_DPRINTK(P9_DEBUG_9P
, "<<< RREMOVE fid %d\n", fid
->fid
);
1126 p9_free_req(clnt
, req
);
1127 p9_fid_destroy(fid
);
1132 EXPORT_SYMBOL(p9_client_remove
);
1135 p9_client_read(struct p9_fid
*fid
, char *data
, char __user
*udata
, u64 offset
,
1138 int err
, rsize
, total
;
1139 struct p9_client
*clnt
;
1140 struct p9_req_t
*req
;
1143 P9_DPRINTK(P9_DEBUG_9P
, ">>> TREAD fid %d offset %llu %d\n", fid
->fid
,
1144 (long long unsigned) offset
, count
);
1149 rsize
= fid
->iounit
;
1150 if (!rsize
|| rsize
> clnt
->msize
-P9_IOHDRSZ
)
1151 rsize
= clnt
->msize
- P9_IOHDRSZ
;
1156 req
= p9_client_rpc(clnt
, P9_TREAD
, "dqd", fid
->fid
, offset
, rsize
);
1162 err
= p9pdu_readf(req
->rc
, clnt
->proto_version
, "D", &count
, &dataptr
);
1164 p9pdu_dump(1, req
->rc
);
1165 goto free_and_error
;
1168 P9_DPRINTK(P9_DEBUG_9P
, "<<< RREAD count %d\n", count
);
1171 memmove(data
, dataptr
, count
);
1175 err
= copy_to_user(udata
, dataptr
, count
);
1178 goto free_and_error
;
1182 p9_free_req(clnt
, req
);
1186 p9_free_req(clnt
, req
);
1190 EXPORT_SYMBOL(p9_client_read
);
1193 p9_client_write(struct p9_fid
*fid
, char *data
, const char __user
*udata
,
1194 u64 offset
, u32 count
)
1196 int err
, rsize
, total
;
1197 struct p9_client
*clnt
;
1198 struct p9_req_t
*req
;
1200 P9_DPRINTK(P9_DEBUG_9P
, ">>> TWRITE fid %d offset %llu count %d\n",
1201 fid
->fid
, (long long unsigned) offset
, count
);
1206 rsize
= fid
->iounit
;
1207 if (!rsize
|| rsize
> clnt
->msize
-P9_IOHDRSZ
)
1208 rsize
= clnt
->msize
- P9_IOHDRSZ
;
1213 req
= p9_client_rpc(clnt
, P9_TWRITE
, "dqD", fid
->fid
, offset
,
1216 req
= p9_client_rpc(clnt
, P9_TWRITE
, "dqU", fid
->fid
, offset
,
1223 err
= p9pdu_readf(req
->rc
, clnt
->proto_version
, "d", &count
);
1225 p9pdu_dump(1, req
->rc
);
1226 goto free_and_error
;
1229 P9_DPRINTK(P9_DEBUG_9P
, "<<< RWRITE count %d\n", count
);
1231 p9_free_req(clnt
, req
);
1235 p9_free_req(clnt
, req
);
1239 EXPORT_SYMBOL(p9_client_write
);
1241 struct p9_wstat
*p9_client_stat(struct p9_fid
*fid
)
1244 struct p9_client
*clnt
;
1245 struct p9_wstat
*ret
= kmalloc(sizeof(struct p9_wstat
), GFP_KERNEL
);
1246 struct p9_req_t
*req
;
1249 P9_DPRINTK(P9_DEBUG_9P
, ">>> TSTAT fid %d\n", fid
->fid
);
1252 return ERR_PTR(-ENOMEM
);
1257 req
= p9_client_rpc(clnt
, P9_TSTAT
, "d", fid
->fid
);
1263 err
= p9pdu_readf(req
->rc
, clnt
->proto_version
, "wS", &ignored
, ret
);
1265 p9pdu_dump(1, req
->rc
);
1266 p9_free_req(clnt
, req
);
1270 P9_DPRINTK(P9_DEBUG_9P
,
1271 "<<< RSTAT sz=%x type=%x dev=%x qid=%x.%llx.%x\n"
1272 "<<< mode=%8.8x atime=%8.8x mtime=%8.8x length=%llx\n"
1273 "<<< name=%s uid=%s gid=%s muid=%s extension=(%s)\n"
1274 "<<< uid=%d gid=%d n_muid=%d\n",
1275 ret
->size
, ret
->type
, ret
->dev
, ret
->qid
.type
,
1276 (unsigned long long)ret
->qid
.path
, ret
->qid
.version
, ret
->mode
,
1277 ret
->atime
, ret
->mtime
, (unsigned long long)ret
->length
,
1278 ret
->name
, ret
->uid
, ret
->gid
, ret
->muid
, ret
->extension
,
1279 ret
->n_uid
, ret
->n_gid
, ret
->n_muid
);
1281 p9_free_req(clnt
, req
);
1286 return ERR_PTR(err
);
1288 EXPORT_SYMBOL(p9_client_stat
);
1290 static int p9_client_statsize(struct p9_wstat
*wst
, int proto_version
)
1294 /* NOTE: size shouldn't include its own length */
1295 /* size[2] type[2] dev[4] qid[13] */
1296 /* mode[4] atime[4] mtime[4] length[8]*/
1297 /* name[s] uid[s] gid[s] muid[s] */
1298 ret
= 2+4+13+4+4+4+8+2+2+2+2;
1301 ret
+= strlen(wst
->name
);
1303 ret
+= strlen(wst
->uid
);
1305 ret
+= strlen(wst
->gid
);
1307 ret
+= strlen(wst
->muid
);
1309 if (proto_version
== p9_proto_2000u
) {
1310 ret
+= 2+4+4+4; /* extension[s] n_uid[4] n_gid[4] n_muid[4] */
1312 ret
+= strlen(wst
->extension
);
1318 int p9_client_wstat(struct p9_fid
*fid
, struct p9_wstat
*wst
)
1321 struct p9_req_t
*req
;
1322 struct p9_client
*clnt
;
1326 wst
->size
= p9_client_statsize(wst
, clnt
->proto_version
);
1327 P9_DPRINTK(P9_DEBUG_9P
, ">>> TWSTAT fid %d\n", fid
->fid
);
1328 P9_DPRINTK(P9_DEBUG_9P
,
1329 " sz=%x type=%x dev=%x qid=%x.%llx.%x\n"
1330 " mode=%8.8x atime=%8.8x mtime=%8.8x length=%llx\n"
1331 " name=%s uid=%s gid=%s muid=%s extension=(%s)\n"
1332 " uid=%d gid=%d n_muid=%d\n",
1333 wst
->size
, wst
->type
, wst
->dev
, wst
->qid
.type
,
1334 (unsigned long long)wst
->qid
.path
, wst
->qid
.version
, wst
->mode
,
1335 wst
->atime
, wst
->mtime
, (unsigned long long)wst
->length
,
1336 wst
->name
, wst
->uid
, wst
->gid
, wst
->muid
, wst
->extension
,
1337 wst
->n_uid
, wst
->n_gid
, wst
->n_muid
);
1339 req
= p9_client_rpc(clnt
, P9_TWSTAT
, "dwS", fid
->fid
, wst
->size
+2, wst
);
1345 P9_DPRINTK(P9_DEBUG_9P
, "<<< RWSTAT fid %d\n", fid
->fid
);
1347 p9_free_req(clnt
, req
);
1351 EXPORT_SYMBOL(p9_client_wstat
);