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 int get_protocol_version(const substring_t
*name
)
77 int version
= -EINVAL
;
79 if (!strncmp("9p2000", name
->from
, name
->to
-name
->from
)) {
80 version
= p9_proto_legacy
;
81 P9_DPRINTK(P9_DEBUG_9P
, "Protocol version: Legacy\n");
82 } else if (!strncmp("9p2000.u", name
->from
, name
->to
-name
->from
)) {
83 version
= p9_proto_2000u
;
84 P9_DPRINTK(P9_DEBUG_9P
, "Protocol version: 9P2000.u\n");
85 } else if (!strncmp("9p2000.L", name
->from
, name
->to
-name
->from
)) {
86 version
= p9_proto_2000L
;
87 P9_DPRINTK(P9_DEBUG_9P
, "Protocol version: 9P2000.L\n");
89 P9_DPRINTK(P9_DEBUG_ERROR
, "Unknown protocol version %s. ",
95 static struct p9_req_t
*
96 p9_client_rpc(struct p9_client
*c
, int8_t type
, const char *fmt
, ...);
99 * parse_options - parse mount options into client structure
100 * @opts: options string passed from mount
101 * @clnt: existing v9fs client information
103 * Return 0 upon success, -ERRNO upon failure
106 static int parse_opts(char *opts
, struct p9_client
*clnt
)
108 char *options
, *tmp_options
;
110 substring_t args
[MAX_OPT_ARGS
];
114 clnt
->proto_version
= p9_proto_2000u
;
120 tmp_options
= kstrdup(opts
, GFP_KERNEL
);
122 P9_DPRINTK(P9_DEBUG_ERROR
,
123 "failed to allocate copy of option string\n");
126 options
= tmp_options
;
128 while ((p
= strsep(&options
, ",")) != NULL
) {
132 token
= match_token(p
, tokens
, args
);
133 if (token
< Opt_trans
) {
134 int r
= match_int(&args
[0], &option
);
136 P9_DPRINTK(P9_DEBUG_ERROR
,
137 "integer field, but no integer?\n");
144 clnt
->msize
= option
;
147 clnt
->trans_mod
= v9fs_get_trans_by_name(&args
[0]);
148 if(clnt
->trans_mod
== NULL
) {
149 P9_DPRINTK(P9_DEBUG_ERROR
,
150 "Could not find request transport: %s\n",
153 goto free_and_return
;
157 clnt
->proto_version
= p9_proto_legacy
;
160 ret
= get_protocol_version(&args
[0]);
162 goto free_and_return
;
163 clnt
->proto_version
= ret
;
176 * p9_tag_alloc - lookup/allocate a request by tag
177 * @c: client session to lookup tag within
178 * @tag: numeric id for transaction
180 * this is a simple array lookup, but will grow the
181 * request_slots as necessary to accomodate transaction
182 * ids which did not previously have a slot.
184 * this code relies on the client spinlock to manage locks, its
185 * possible we should switch to something else, but I'd rather
186 * stick with something low-overhead for the common case.
190 static struct p9_req_t
*p9_tag_alloc(struct p9_client
*c
, u16 tag
)
194 struct p9_req_t
*req
;
196 /* This looks up the original request by tag so we know which
197 * buffer to read the data into */
200 if (tag
>= c
->max_tag
) {
201 spin_lock_irqsave(&c
->lock
, flags
);
202 /* check again since original check was outside of lock */
203 while (tag
>= c
->max_tag
) {
204 row
= (tag
/ P9_ROW_MAXTAG
);
205 c
->reqs
[row
] = kcalloc(P9_ROW_MAXTAG
,
206 sizeof(struct p9_req_t
), GFP_ATOMIC
);
209 printk(KERN_ERR
"Couldn't grow tag array\n");
210 spin_unlock_irqrestore(&c
->lock
, flags
);
211 return ERR_PTR(-ENOMEM
);
213 for (col
= 0; col
< P9_ROW_MAXTAG
; col
++) {
214 c
->reqs
[row
][col
].status
= REQ_STATUS_IDLE
;
215 c
->reqs
[row
][col
].tc
= NULL
;
217 c
->max_tag
+= P9_ROW_MAXTAG
;
219 spin_unlock_irqrestore(&c
->lock
, flags
);
221 row
= tag
/ P9_ROW_MAXTAG
;
222 col
= tag
% P9_ROW_MAXTAG
;
224 req
= &c
->reqs
[row
][col
];
226 req
->wq
= kmalloc(sizeof(wait_queue_head_t
), GFP_KERNEL
);
228 printk(KERN_ERR
"Couldn't grow tag array\n");
229 return ERR_PTR(-ENOMEM
);
231 init_waitqueue_head(req
->wq
);
232 req
->tc
= kmalloc(sizeof(struct p9_fcall
)+c
->msize
,
234 req
->rc
= kmalloc(sizeof(struct p9_fcall
)+c
->msize
,
236 if ((!req
->tc
) || (!req
->rc
)) {
237 printk(KERN_ERR
"Couldn't grow tag array\n");
241 req
->tc
= req
->rc
= NULL
;
243 return ERR_PTR(-ENOMEM
);
245 req
->tc
->sdata
= (char *) req
->tc
+ sizeof(struct p9_fcall
);
246 req
->tc
->capacity
= c
->msize
;
247 req
->rc
->sdata
= (char *) req
->rc
+ sizeof(struct p9_fcall
);
248 req
->rc
->capacity
= c
->msize
;
251 p9pdu_reset(req
->tc
);
252 p9pdu_reset(req
->rc
);
254 req
->tc
->tag
= tag
-1;
255 req
->status
= REQ_STATUS_ALLOC
;
257 return &c
->reqs
[row
][col
];
261 * p9_tag_lookup - lookup a request by tag
262 * @c: client session to lookup tag within
263 * @tag: numeric id for transaction
267 struct p9_req_t
*p9_tag_lookup(struct p9_client
*c
, u16 tag
)
271 /* This looks up the original request by tag so we know which
272 * buffer to read the data into */
275 BUG_ON(tag
>= c
->max_tag
);
277 row
= tag
/ P9_ROW_MAXTAG
;
278 col
= tag
% P9_ROW_MAXTAG
;
280 return &c
->reqs
[row
][col
];
282 EXPORT_SYMBOL(p9_tag_lookup
);
285 * p9_tag_init - setup tags structure and contents
286 * @c: v9fs client struct
288 * This initializes the tags structure for each client instance.
292 static int p9_tag_init(struct p9_client
*c
)
296 c
->tagpool
= p9_idpool_create();
297 if (IS_ERR(c
->tagpool
)) {
298 err
= PTR_ERR(c
->tagpool
);
303 p9_idpool_get(c
->tagpool
); /* reserve tag 0 */
311 * p9_tag_cleanup - cleans up tags structure and reclaims resources
312 * @c: v9fs client struct
314 * This frees resources associated with the tags structure
317 static void p9_tag_cleanup(struct p9_client
*c
)
321 /* check to insure all requests are idle */
322 for (row
= 0; row
< (c
->max_tag
/P9_ROW_MAXTAG
); row
++) {
323 for (col
= 0; col
< P9_ROW_MAXTAG
; col
++) {
324 if (c
->reqs
[row
][col
].status
!= REQ_STATUS_IDLE
) {
325 P9_DPRINTK(P9_DEBUG_MUX
,
326 "Attempting to cleanup non-free tag %d,%d\n",
328 /* TODO: delay execution of cleanup */
335 p9_idpool_destroy(c
->tagpool
);
337 /* free requests associated with tags */
338 for (row
= 0; row
< (c
->max_tag
/P9_ROW_MAXTAG
); row
++) {
339 for (col
= 0; col
< P9_ROW_MAXTAG
; col
++) {
340 kfree(c
->reqs
[row
][col
].wq
);
341 kfree(c
->reqs
[row
][col
].tc
);
342 kfree(c
->reqs
[row
][col
].rc
);
350 * p9_free_req - free a request and clean-up as necessary
352 * r: request to release
356 static void p9_free_req(struct p9_client
*c
, struct p9_req_t
*r
)
358 int tag
= r
->tc
->tag
;
359 P9_DPRINTK(P9_DEBUG_MUX
, "clnt %p req %p tag: %d\n", c
, r
, tag
);
361 r
->status
= REQ_STATUS_IDLE
;
362 if (tag
!= P9_NOTAG
&& p9_idpool_check(tag
, c
->tagpool
))
363 p9_idpool_put(tag
, c
->tagpool
);
367 * p9_client_cb - call back from transport to client
369 * req: request received
372 void p9_client_cb(struct p9_client
*c
, struct p9_req_t
*req
)
374 P9_DPRINTK(P9_DEBUG_MUX
, " tag %d\n", req
->tc
->tag
);
376 P9_DPRINTK(P9_DEBUG_MUX
, "wakeup: %d\n", req
->tc
->tag
);
378 EXPORT_SYMBOL(p9_client_cb
);
381 * p9_parse_header - parse header arguments out of a packet
382 * @pdu: packet to parse
383 * @size: size of packet
384 * @type: type of request
385 * @tag: tag of packet
386 * @rewind: set if we need to rewind offset afterwards
390 p9_parse_header(struct p9_fcall
*pdu
, int32_t *size
, int8_t *type
, int16_t *tag
,
396 int offset
= pdu
->offset
;
403 err
= p9pdu_readf(pdu
, 0, "dbw", &r_size
, &r_type
, &r_tag
);
405 goto rewind_and_exit
;
411 P9_DPRINTK(P9_DEBUG_9P
, "<<< size=%d type: %d tag: %d\n", pdu
->size
,
424 pdu
->offset
= offset
;
427 EXPORT_SYMBOL(p9_parse_header
);
430 * p9_check_errors - check 9p packet for error return and process it
431 * @c: current client instance
432 * @req: request to parse and check for error conditions
434 * returns error code if one is discovered, otherwise returns 0
436 * this will have to be more complicated if we have multiple
440 static int p9_check_errors(struct p9_client
*c
, struct p9_req_t
*req
)
445 err
= p9_parse_header(req
->rc
, NULL
, &type
, NULL
, 0);
447 P9_DPRINTK(P9_DEBUG_ERROR
, "couldn't parse header %d\n", err
);
451 if (type
== P9_RERROR
) {
455 err
= p9pdu_readf(req
->rc
, c
->proto_version
, "s?d",
458 P9_DPRINTK(P9_DEBUG_ERROR
, "couldn't parse error%d\n",
463 if (p9_is_proto_dotu(c
))
466 if (!err
|| !IS_ERR_VALUE(err
))
467 err
= p9_errstr2errno(ename
, strlen(ename
));
469 P9_DPRINTK(P9_DEBUG_9P
, "<<< RERROR (%d) %s\n", -ecode
, ename
);
479 * p9_client_flush - flush (cancel) a request
481 * @oldreq: request to cancel
483 * This sents a flush for a particular requests and links
484 * the flush request to the original request. The current
485 * code only supports a single flush request although the protocol
486 * allows for multiple flush requests to be sent for a single request.
490 static int p9_client_flush(struct p9_client
*c
, struct p9_req_t
*oldreq
)
492 struct p9_req_t
*req
;
496 err
= p9_parse_header(oldreq
->tc
, NULL
, NULL
, &oldtag
, 1);
500 P9_DPRINTK(P9_DEBUG_9P
, ">>> TFLUSH tag %d\n", oldtag
);
502 req
= p9_client_rpc(c
, P9_TFLUSH
, "w", oldtag
);
507 /* if we haven't received a response for oldreq,
508 remove it from the list. */
510 if (oldreq
->status
== REQ_STATUS_FLSH
)
511 list_del(&oldreq
->req_list
);
512 spin_unlock(&c
->lock
);
519 * p9_client_rpc - issue a request and wait for a response
521 * @type: type of request
522 * @fmt: protocol format string (see protocol.c)
524 * Returns request structure (which client must free using p9_free_req)
527 static struct p9_req_t
*
528 p9_client_rpc(struct p9_client
*c
, int8_t type
, const char *fmt
, ...)
532 struct p9_req_t
*req
;
536 P9_DPRINTK(P9_DEBUG_MUX
, "client %p op %d\n", c
, type
);
538 /* we allow for any status other than disconnected */
539 if (c
->status
== Disconnected
)
540 return ERR_PTR(-EIO
);
542 /* if status is begin_disconnected we allow only clunk request */
543 if ((c
->status
== BeginDisconnect
) && (type
!= P9_TCLUNK
))
544 return ERR_PTR(-EIO
);
546 if (signal_pending(current
)) {
548 clear_thread_flag(TIF_SIGPENDING
);
553 if (type
!= P9_TVERSION
) {
554 tag
= p9_idpool_get(c
->tagpool
);
556 return ERR_PTR(-ENOMEM
);
559 req
= p9_tag_alloc(c
, tag
);
563 /* marshall the data */
564 p9pdu_prepare(req
->tc
, tag
, type
);
566 err
= p9pdu_vwritef(req
->tc
, c
->proto_version
, fmt
, ap
);
568 p9pdu_finalize(req
->tc
);
570 err
= c
->trans_mod
->request(c
, req
);
572 c
->status
= Disconnected
;
576 P9_DPRINTK(P9_DEBUG_MUX
, "wait %p tag: %d\n", req
->wq
, tag
);
577 err
= wait_event_interruptible(*req
->wq
,
578 req
->status
>= REQ_STATUS_RCVD
);
579 P9_DPRINTK(P9_DEBUG_MUX
, "wait %p tag: %d returned %d\n",
582 if (req
->status
== REQ_STATUS_ERROR
) {
583 P9_DPRINTK(P9_DEBUG_ERROR
, "req_status error %d\n", req
->t_err
);
587 if ((err
== -ERESTARTSYS
) && (c
->status
== Connected
)) {
588 P9_DPRINTK(P9_DEBUG_MUX
, "flushing\n");
590 clear_thread_flag(TIF_SIGPENDING
);
592 if (c
->trans_mod
->cancel(c
, req
))
593 p9_client_flush(c
, req
);
595 /* if we received the response anyway, don't signal error */
596 if (req
->status
== REQ_STATUS_RCVD
)
601 spin_lock_irqsave(¤t
->sighand
->siglock
, flags
);
603 spin_unlock_irqrestore(¤t
->sighand
->siglock
, flags
);
609 err
= p9_check_errors(c
, req
);
611 P9_DPRINTK(P9_DEBUG_MUX
, "exit: client %p op %d\n", c
, type
);
616 P9_DPRINTK(P9_DEBUG_MUX
, "exit: client %p op %d error: %d\n", c
, type
,
622 static struct p9_fid
*p9_fid_create(struct p9_client
*clnt
)
628 P9_DPRINTK(P9_DEBUG_FID
, "clnt %p\n", clnt
);
629 fid
= kmalloc(sizeof(struct p9_fid
), GFP_KERNEL
);
631 return ERR_PTR(-ENOMEM
);
633 ret
= p9_idpool_get(clnt
->fidpool
);
640 memset(&fid
->qid
, 0, sizeof(struct p9_qid
));
642 fid
->uid
= current_fsuid();
645 spin_lock_irqsave(&clnt
->lock
, flags
);
646 list_add(&fid
->flist
, &clnt
->fidlist
);
647 spin_unlock_irqrestore(&clnt
->lock
, flags
);
656 static void p9_fid_destroy(struct p9_fid
*fid
)
658 struct p9_client
*clnt
;
661 P9_DPRINTK(P9_DEBUG_FID
, "fid %d\n", fid
->fid
);
663 p9_idpool_put(fid
->fid
, clnt
->fidpool
);
664 spin_lock_irqsave(&clnt
->lock
, flags
);
665 list_del(&fid
->flist
);
666 spin_unlock_irqrestore(&clnt
->lock
, flags
);
671 int p9_client_version(struct p9_client
*c
)
674 struct p9_req_t
*req
;
678 P9_DPRINTK(P9_DEBUG_9P
, ">>> TVERSION msize %d protocol %d\n",
679 c
->msize
, c
->proto_version
);
681 switch (c
->proto_version
) {
683 req
= p9_client_rpc(c
, P9_TVERSION
, "ds",
684 c
->msize
, "9P2000.L");
687 req
= p9_client_rpc(c
, P9_TVERSION
, "ds",
688 c
->msize
, "9P2000.u");
690 case p9_proto_legacy
:
691 req
= p9_client_rpc(c
, P9_TVERSION
, "ds",
702 err
= p9pdu_readf(req
->rc
, c
->proto_version
, "ds", &msize
, &version
);
704 P9_DPRINTK(P9_DEBUG_9P
, "version error %d\n", err
);
705 p9pdu_dump(1, req
->rc
);
709 P9_DPRINTK(P9_DEBUG_9P
, "<<< RVERSION msize %d %s\n", msize
, version
);
710 if (!strncmp(version
, "9P2000.L", 8))
711 c
->proto_version
= p9_proto_2000L
;
712 else if (!strncmp(version
, "9P2000.u", 8))
713 c
->proto_version
= p9_proto_2000u
;
714 else if (!strncmp(version
, "9P2000", 6))
715 c
->proto_version
= p9_proto_legacy
;
721 if (msize
< c
->msize
)
730 EXPORT_SYMBOL(p9_client_version
);
732 struct p9_client
*p9_client_create(const char *dev_name
, char *options
)
735 struct p9_client
*clnt
;
738 clnt
= kmalloc(sizeof(struct p9_client
), GFP_KERNEL
);
740 return ERR_PTR(-ENOMEM
);
742 clnt
->trans_mod
= NULL
;
744 spin_lock_init(&clnt
->lock
);
745 INIT_LIST_HEAD(&clnt
->fidlist
);
749 err
= parse_opts(options
, clnt
);
753 if (!clnt
->trans_mod
)
754 clnt
->trans_mod
= v9fs_get_default_trans();
756 if (clnt
->trans_mod
== NULL
) {
757 err
= -EPROTONOSUPPORT
;
758 P9_DPRINTK(P9_DEBUG_ERROR
,
759 "No transport defined or default transport\n");
763 clnt
->fidpool
= p9_idpool_create();
764 if (IS_ERR(clnt
->fidpool
)) {
765 err
= PTR_ERR(clnt
->fidpool
);
766 clnt
->fidpool
= NULL
;
770 P9_DPRINTK(P9_DEBUG_MUX
, "clnt %p trans %p msize %d protocol %d\n",
771 clnt
, clnt
->trans_mod
, clnt
->msize
, clnt
->proto_version
);
773 err
= clnt
->trans_mod
->create(clnt
, dev_name
, options
);
775 goto destroy_fidpool
;
777 if ((clnt
->msize
+P9_IOHDRSZ
) > clnt
->trans_mod
->maxsize
)
778 clnt
->msize
= clnt
->trans_mod
->maxsize
-P9_IOHDRSZ
;
780 err
= p9_client_version(clnt
);
787 clnt
->trans_mod
->close(clnt
);
789 p9_idpool_destroy(clnt
->fidpool
);
791 v9fs_put_trans(clnt
->trans_mod
);
796 EXPORT_SYMBOL(p9_client_create
);
798 void p9_client_destroy(struct p9_client
*clnt
)
800 struct p9_fid
*fid
, *fidptr
;
802 P9_DPRINTK(P9_DEBUG_MUX
, "clnt %p\n", clnt
);
805 clnt
->trans_mod
->close(clnt
);
807 v9fs_put_trans(clnt
->trans_mod
);
809 list_for_each_entry_safe(fid
, fidptr
, &clnt
->fidlist
, flist
) {
810 printk(KERN_INFO
"Found fid %d not clunked\n", fid
->fid
);
815 p9_idpool_destroy(clnt
->fidpool
);
817 p9_tag_cleanup(clnt
);
821 EXPORT_SYMBOL(p9_client_destroy
);
823 void p9_client_disconnect(struct p9_client
*clnt
)
825 P9_DPRINTK(P9_DEBUG_9P
, "clnt %p\n", clnt
);
826 clnt
->status
= Disconnected
;
828 EXPORT_SYMBOL(p9_client_disconnect
);
830 void p9_client_begin_disconnect(struct p9_client
*clnt
)
832 P9_DPRINTK(P9_DEBUG_9P
, "clnt %p\n", clnt
);
833 clnt
->status
= BeginDisconnect
;
835 EXPORT_SYMBOL(p9_client_begin_disconnect
);
837 struct p9_fid
*p9_client_attach(struct p9_client
*clnt
, struct p9_fid
*afid
,
838 char *uname
, u32 n_uname
, char *aname
)
841 struct p9_req_t
*req
;
845 P9_DPRINTK(P9_DEBUG_9P
, ">>> TATTACH afid %d uname %s aname %s\n",
846 afid
? afid
->fid
: -1, uname
, aname
);
849 fid
= p9_fid_create(clnt
);
856 req
= p9_client_rpc(clnt
, P9_TATTACH
, "ddss?d", fid
->fid
,
857 afid
? afid
->fid
: P9_NOFID
, uname
, aname
, n_uname
);
863 err
= p9pdu_readf(req
->rc
, clnt
->proto_version
, "Q", &qid
);
865 p9pdu_dump(1, req
->rc
);
866 p9_free_req(clnt
, req
);
870 P9_DPRINTK(P9_DEBUG_9P
, "<<< RATTACH qid %x.%llx.%x\n",
872 (unsigned long long)qid
.path
,
875 memmove(&fid
->qid
, &qid
, sizeof(struct p9_qid
));
877 p9_free_req(clnt
, req
);
885 EXPORT_SYMBOL(p9_client_attach
);
888 p9_client_auth(struct p9_client
*clnt
, char *uname
, u32 n_uname
, char *aname
)
891 struct p9_req_t
*req
;
895 P9_DPRINTK(P9_DEBUG_9P
, ">>> TAUTH uname %s aname %s\n", uname
, aname
);
898 afid
= p9_fid_create(clnt
);
905 req
= p9_client_rpc(clnt
, P9_TAUTH
, "dss?d",
906 afid
? afid
->fid
: P9_NOFID
, uname
, aname
, n_uname
);
912 err
= p9pdu_readf(req
->rc
, clnt
->proto_version
, "Q", &qid
);
914 p9pdu_dump(1, req
->rc
);
915 p9_free_req(clnt
, req
);
919 P9_DPRINTK(P9_DEBUG_9P
, "<<< RAUTH qid %x.%llx.%x\n",
921 (unsigned long long)qid
.path
,
924 memmove(&afid
->qid
, &qid
, sizeof(struct p9_qid
));
925 p9_free_req(clnt
, req
);
930 p9_fid_destroy(afid
);
933 EXPORT_SYMBOL(p9_client_auth
);
935 struct p9_fid
*p9_client_walk(struct p9_fid
*oldfid
, int nwname
, char **wnames
,
939 struct p9_client
*clnt
;
941 struct p9_qid
*wqids
;
942 struct p9_req_t
*req
;
943 int16_t nwqids
, count
;
948 fid
= p9_fid_create(clnt
);
955 fid
->uid
= oldfid
->uid
;
960 P9_DPRINTK(P9_DEBUG_9P
, ">>> TWALK fids %d,%d nwname %d wname[0] %s\n",
961 oldfid
->fid
, fid
->fid
, nwname
, wnames
? wnames
[0] : NULL
);
963 req
= p9_client_rpc(clnt
, P9_TWALK
, "ddT", oldfid
->fid
, fid
->fid
,
970 err
= p9pdu_readf(req
->rc
, clnt
->proto_version
, "R", &nwqids
, &wqids
);
972 p9pdu_dump(1, req
->rc
);
973 p9_free_req(clnt
, req
);
976 p9_free_req(clnt
, req
);
978 P9_DPRINTK(P9_DEBUG_9P
, "<<< RWALK nwqid %d:\n", nwqids
);
980 if (nwqids
!= nwname
) {
985 for (count
= 0; count
< nwqids
; count
++)
986 P9_DPRINTK(P9_DEBUG_9P
, "<<< [%d] %x.%llx.%x\n",
987 count
, wqids
[count
].type
,
988 (unsigned long long)wqids
[count
].path
,
989 wqids
[count
].version
);
992 memmove(&fid
->qid
, &wqids
[nwqids
- 1], sizeof(struct p9_qid
));
994 fid
->qid
= oldfid
->qid
;
999 p9_client_clunk(fid
);
1003 if (fid
&& (fid
!= oldfid
))
1004 p9_fid_destroy(fid
);
1006 return ERR_PTR(err
);
1008 EXPORT_SYMBOL(p9_client_walk
);
1010 int p9_client_open(struct p9_fid
*fid
, int mode
)
1013 struct p9_client
*clnt
;
1014 struct p9_req_t
*req
;
1018 P9_DPRINTK(P9_DEBUG_9P
, ">>> TOPEN fid %d mode %d\n", fid
->fid
, mode
);
1022 if (fid
->mode
!= -1)
1025 req
= p9_client_rpc(clnt
, P9_TOPEN
, "db", fid
->fid
, mode
);
1031 err
= p9pdu_readf(req
->rc
, clnt
->proto_version
, "Qd", &qid
, &iounit
);
1033 p9pdu_dump(1, req
->rc
);
1034 goto free_and_error
;
1037 P9_DPRINTK(P9_DEBUG_9P
, "<<< ROPEN qid %x.%llx.%x iounit %x\n",
1039 (unsigned long long)qid
.path
,
1040 qid
.version
, iounit
);
1043 fid
->iounit
= iounit
;
1046 p9_free_req(clnt
, req
);
1050 EXPORT_SYMBOL(p9_client_open
);
1052 int p9_client_fcreate(struct p9_fid
*fid
, char *name
, u32 perm
, int mode
,
1056 struct p9_client
*clnt
;
1057 struct p9_req_t
*req
;
1061 P9_DPRINTK(P9_DEBUG_9P
, ">>> TCREATE fid %d name %s perm %d mode %d\n",
1062 fid
->fid
, name
, perm
, mode
);
1066 if (fid
->mode
!= -1)
1069 req
= p9_client_rpc(clnt
, P9_TCREATE
, "dsdb?s", fid
->fid
, name
, perm
,
1076 err
= p9pdu_readf(req
->rc
, clnt
->proto_version
, "Qd", &qid
, &iounit
);
1078 p9pdu_dump(1, req
->rc
);
1079 goto free_and_error
;
1082 P9_DPRINTK(P9_DEBUG_9P
, "<<< RCREATE qid %x.%llx.%x iounit %x\n",
1084 (unsigned long long)qid
.path
,
1085 qid
.version
, iounit
);
1088 fid
->iounit
= iounit
;
1091 p9_free_req(clnt
, req
);
1095 EXPORT_SYMBOL(p9_client_fcreate
);
1097 int p9_client_clunk(struct p9_fid
*fid
)
1100 struct p9_client
*clnt
;
1101 struct p9_req_t
*req
;
1103 P9_DPRINTK(P9_DEBUG_9P
, ">>> TCLUNK fid %d\n", fid
->fid
);
1107 req
= p9_client_rpc(clnt
, P9_TCLUNK
, "d", fid
->fid
);
1113 P9_DPRINTK(P9_DEBUG_9P
, "<<< RCLUNK fid %d\n", fid
->fid
);
1115 p9_free_req(clnt
, req
);
1116 p9_fid_destroy(fid
);
1121 EXPORT_SYMBOL(p9_client_clunk
);
1123 int p9_client_remove(struct p9_fid
*fid
)
1126 struct p9_client
*clnt
;
1127 struct p9_req_t
*req
;
1129 P9_DPRINTK(P9_DEBUG_9P
, ">>> TREMOVE fid %d\n", fid
->fid
);
1133 req
= p9_client_rpc(clnt
, P9_TREMOVE
, "d", fid
->fid
);
1139 P9_DPRINTK(P9_DEBUG_9P
, "<<< RREMOVE fid %d\n", fid
->fid
);
1141 p9_free_req(clnt
, req
);
1142 p9_fid_destroy(fid
);
1147 EXPORT_SYMBOL(p9_client_remove
);
1150 p9_client_read(struct p9_fid
*fid
, char *data
, char __user
*udata
, u64 offset
,
1153 int err
, rsize
, total
;
1154 struct p9_client
*clnt
;
1155 struct p9_req_t
*req
;
1158 P9_DPRINTK(P9_DEBUG_9P
, ">>> TREAD fid %d offset %llu %d\n", fid
->fid
,
1159 (long long unsigned) offset
, count
);
1164 rsize
= fid
->iounit
;
1165 if (!rsize
|| rsize
> clnt
->msize
-P9_IOHDRSZ
)
1166 rsize
= clnt
->msize
- P9_IOHDRSZ
;
1171 req
= p9_client_rpc(clnt
, P9_TREAD
, "dqd", fid
->fid
, offset
, rsize
);
1177 err
= p9pdu_readf(req
->rc
, clnt
->proto_version
, "D", &count
, &dataptr
);
1179 p9pdu_dump(1, req
->rc
);
1180 goto free_and_error
;
1183 P9_DPRINTK(P9_DEBUG_9P
, "<<< RREAD count %d\n", count
);
1186 memmove(data
, dataptr
, count
);
1190 err
= copy_to_user(udata
, dataptr
, count
);
1193 goto free_and_error
;
1197 p9_free_req(clnt
, req
);
1201 p9_free_req(clnt
, req
);
1205 EXPORT_SYMBOL(p9_client_read
);
1208 p9_client_write(struct p9_fid
*fid
, char *data
, const char __user
*udata
,
1209 u64 offset
, u32 count
)
1211 int err
, rsize
, total
;
1212 struct p9_client
*clnt
;
1213 struct p9_req_t
*req
;
1215 P9_DPRINTK(P9_DEBUG_9P
, ">>> TWRITE fid %d offset %llu count %d\n",
1216 fid
->fid
, (long long unsigned) offset
, count
);
1221 rsize
= fid
->iounit
;
1222 if (!rsize
|| rsize
> clnt
->msize
-P9_IOHDRSZ
)
1223 rsize
= clnt
->msize
- P9_IOHDRSZ
;
1228 req
= p9_client_rpc(clnt
, P9_TWRITE
, "dqD", fid
->fid
, offset
,
1231 req
= p9_client_rpc(clnt
, P9_TWRITE
, "dqU", fid
->fid
, offset
,
1238 err
= p9pdu_readf(req
->rc
, clnt
->proto_version
, "d", &count
);
1240 p9pdu_dump(1, req
->rc
);
1241 goto free_and_error
;
1244 P9_DPRINTK(P9_DEBUG_9P
, "<<< RWRITE count %d\n", count
);
1246 p9_free_req(clnt
, req
);
1250 p9_free_req(clnt
, req
);
1254 EXPORT_SYMBOL(p9_client_write
);
1256 struct p9_wstat
*p9_client_stat(struct p9_fid
*fid
)
1259 struct p9_client
*clnt
;
1260 struct p9_wstat
*ret
= kmalloc(sizeof(struct p9_wstat
), GFP_KERNEL
);
1261 struct p9_req_t
*req
;
1264 P9_DPRINTK(P9_DEBUG_9P
, ">>> TSTAT fid %d\n", fid
->fid
);
1267 return ERR_PTR(-ENOMEM
);
1272 req
= p9_client_rpc(clnt
, P9_TSTAT
, "d", fid
->fid
);
1278 err
= p9pdu_readf(req
->rc
, clnt
->proto_version
, "wS", &ignored
, ret
);
1280 p9pdu_dump(1, req
->rc
);
1281 p9_free_req(clnt
, req
);
1285 P9_DPRINTK(P9_DEBUG_9P
,
1286 "<<< RSTAT sz=%x type=%x dev=%x qid=%x.%llx.%x\n"
1287 "<<< mode=%8.8x atime=%8.8x mtime=%8.8x length=%llx\n"
1288 "<<< name=%s uid=%s gid=%s muid=%s extension=(%s)\n"
1289 "<<< uid=%d gid=%d n_muid=%d\n",
1290 ret
->size
, ret
->type
, ret
->dev
, ret
->qid
.type
,
1291 (unsigned long long)ret
->qid
.path
, ret
->qid
.version
, ret
->mode
,
1292 ret
->atime
, ret
->mtime
, (unsigned long long)ret
->length
,
1293 ret
->name
, ret
->uid
, ret
->gid
, ret
->muid
, ret
->extension
,
1294 ret
->n_uid
, ret
->n_gid
, ret
->n_muid
);
1296 p9_free_req(clnt
, req
);
1301 return ERR_PTR(err
);
1303 EXPORT_SYMBOL(p9_client_stat
);
1305 static int p9_client_statsize(struct p9_wstat
*wst
, int proto_version
)
1309 /* NOTE: size shouldn't include its own length */
1310 /* size[2] type[2] dev[4] qid[13] */
1311 /* mode[4] atime[4] mtime[4] length[8]*/
1312 /* name[s] uid[s] gid[s] muid[s] */
1313 ret
= 2+4+13+4+4+4+8+2+2+2+2;
1316 ret
+= strlen(wst
->name
);
1318 ret
+= strlen(wst
->uid
);
1320 ret
+= strlen(wst
->gid
);
1322 ret
+= strlen(wst
->muid
);
1324 if ((proto_version
== p9_proto_2000u
) ||
1325 (proto_version
== p9_proto_2000L
)) {
1326 ret
+= 2+4+4+4; /* extension[s] n_uid[4] n_gid[4] n_muid[4] */
1328 ret
+= strlen(wst
->extension
);
1334 int p9_client_wstat(struct p9_fid
*fid
, struct p9_wstat
*wst
)
1337 struct p9_req_t
*req
;
1338 struct p9_client
*clnt
;
1342 wst
->size
= p9_client_statsize(wst
, clnt
->proto_version
);
1343 P9_DPRINTK(P9_DEBUG_9P
, ">>> TWSTAT fid %d\n", fid
->fid
);
1344 P9_DPRINTK(P9_DEBUG_9P
,
1345 " sz=%x type=%x dev=%x qid=%x.%llx.%x\n"
1346 " mode=%8.8x atime=%8.8x mtime=%8.8x length=%llx\n"
1347 " name=%s uid=%s gid=%s muid=%s extension=(%s)\n"
1348 " uid=%d gid=%d n_muid=%d\n",
1349 wst
->size
, wst
->type
, wst
->dev
, wst
->qid
.type
,
1350 (unsigned long long)wst
->qid
.path
, wst
->qid
.version
, wst
->mode
,
1351 wst
->atime
, wst
->mtime
, (unsigned long long)wst
->length
,
1352 wst
->name
, wst
->uid
, wst
->gid
, wst
->muid
, wst
->extension
,
1353 wst
->n_uid
, wst
->n_gid
, wst
->n_muid
);
1355 req
= p9_client_rpc(clnt
, P9_TWSTAT
, "dwS", fid
->fid
, wst
->size
+2, wst
);
1361 P9_DPRINTK(P9_DEBUG_9P
, "<<< RWSTAT fid %d\n", fid
->fid
);
1363 p9_free_req(clnt
, req
);
1367 EXPORT_SYMBOL(p9_client_wstat
);
1369 int p9_client_statfs(struct p9_fid
*fid
, struct p9_rstatfs
*sb
)
1372 struct p9_req_t
*req
;
1373 struct p9_client
*clnt
;
1378 P9_DPRINTK(P9_DEBUG_9P
, ">>> TSTATFS fid %d\n", fid
->fid
);
1380 req
= p9_client_rpc(clnt
, P9_TSTATFS
, "d", fid
->fid
);
1386 err
= p9pdu_readf(req
->rc
, clnt
->proto_version
, "ddqqqqqqd", &sb
->type
,
1387 &sb
->bsize
, &sb
->blocks
, &sb
->bfree
, &sb
->bavail
,
1388 &sb
->files
, &sb
->ffree
, &sb
->fsid
, &sb
->namelen
);
1390 p9pdu_dump(1, req
->rc
);
1391 p9_free_req(clnt
, req
);
1395 P9_DPRINTK(P9_DEBUG_9P
, "<<< RSTATFS fid %d type 0x%lx bsize %ld "
1396 "blocks %llu bfree %llu bavail %llu files %llu ffree %llu "
1397 "fsid %llu namelen %ld\n",
1398 fid
->fid
, (long unsigned int)sb
->type
, (long int)sb
->bsize
,
1399 sb
->blocks
, sb
->bfree
, sb
->bavail
, sb
->files
, sb
->ffree
,
1400 sb
->fsid
, (long int)sb
->namelen
);
1402 p9_free_req(clnt
, req
);
1406 EXPORT_SYMBOL(p9_client_statfs
);
1408 int p9_client_rename(struct p9_fid
*fid
, struct p9_fid
*newdirfid
, char *name
)
1411 struct p9_req_t
*req
;
1412 struct p9_client
*clnt
;
1417 P9_DPRINTK(P9_DEBUG_9P
, ">>> TRENAME fid %d newdirfid %d name %s\n",
1418 fid
->fid
, newdirfid
->fid
, name
);
1420 req
= p9_client_rpc(clnt
, P9_TRENAME
, "dds", fid
->fid
,
1421 newdirfid
->fid
, name
);
1427 P9_DPRINTK(P9_DEBUG_9P
, "<<< RRENAME fid %d\n", fid
->fid
);
1429 p9_free_req(clnt
, req
);
1433 EXPORT_SYMBOL(p9_client_rename
);