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/sched.h>
33 #include <linux/uaccess.h>
34 #include <net/9p/9p.h>
35 #include <linux/parser.h>
36 #include <net/9p/client.h>
37 #include <net/9p/transport.h>
41 * Client Option Parsing (code inspired by NFS code)
42 * - a little lazy - parse all client options
52 static const match_table_t tokens
= {
53 {Opt_msize
, "msize=%u"},
54 {Opt_legacy
, "noextend"},
55 {Opt_trans
, "trans=%s"},
59 static struct p9_req_t
*
60 p9_client_rpc(struct p9_client
*c
, int8_t type
, const char *fmt
, ...);
63 * parse_options - parse mount options into client structure
64 * @opts: options string passed from mount
65 * @clnt: existing v9fs client information
67 * Return 0 upon success, -ERRNO upon failure
70 static int parse_opts(char *opts
, struct p9_client
*clnt
)
74 substring_t args
[MAX_OPT_ARGS
];
84 options
= kstrdup(opts
, GFP_KERNEL
);
86 P9_DPRINTK(P9_DEBUG_ERROR
,
87 "failed to allocate copy of option string\n");
91 while ((p
= strsep(&options
, ",")) != NULL
) {
95 token
= match_token(p
, tokens
, args
);
96 if (token
< Opt_trans
) {
97 int r
= match_int(&args
[0], &option
);
99 P9_DPRINTK(P9_DEBUG_ERROR
,
100 "integer field, but no integer?\n");
107 clnt
->msize
= option
;
110 clnt
->trans_mod
= v9fs_get_trans_by_name(&args
[0]);
125 * p9_tag_alloc - lookup/allocate a request by tag
126 * @c: client session to lookup tag within
127 * @tag: numeric id for transaction
129 * this is a simple array lookup, but will grow the
130 * request_slots as necessary to accomodate transaction
131 * ids which did not previously have a slot.
133 * this code relies on the client spinlock to manage locks, its
134 * possible we should switch to something else, but I'd rather
135 * stick with something low-overhead for the common case.
139 static struct p9_req_t
*p9_tag_alloc(struct p9_client
*c
, u16 tag
)
143 struct p9_req_t
*req
;
145 /* This looks up the original request by tag so we know which
146 * buffer to read the data into */
149 if (tag
>= c
->max_tag
) {
150 spin_lock_irqsave(&c
->lock
, flags
);
151 /* check again since original check was outside of lock */
152 while (tag
>= c
->max_tag
) {
153 row
= (tag
/ P9_ROW_MAXTAG
);
154 c
->reqs
[row
] = kcalloc(P9_ROW_MAXTAG
,
155 sizeof(struct p9_req_t
), GFP_ATOMIC
);
158 printk(KERN_ERR
"Couldn't grow tag array\n");
159 spin_unlock_irqrestore(&c
->lock
, flags
);
160 return ERR_PTR(-ENOMEM
);
162 for (col
= 0; col
< P9_ROW_MAXTAG
; col
++) {
163 c
->reqs
[row
][col
].status
= REQ_STATUS_IDLE
;
164 c
->reqs
[row
][col
].tc
= NULL
;
166 c
->max_tag
+= P9_ROW_MAXTAG
;
168 spin_unlock_irqrestore(&c
->lock
, flags
);
170 row
= tag
/ P9_ROW_MAXTAG
;
171 col
= tag
% P9_ROW_MAXTAG
;
173 req
= &c
->reqs
[row
][col
];
175 req
->wq
= kmalloc(sizeof(wait_queue_head_t
), GFP_KERNEL
);
177 printk(KERN_ERR
"Couldn't grow tag array\n");
178 return ERR_PTR(-ENOMEM
);
180 init_waitqueue_head(req
->wq
);
181 req
->tc
= kmalloc(sizeof(struct p9_fcall
)+c
->msize
,
183 req
->rc
= kmalloc(sizeof(struct p9_fcall
)+c
->msize
,
185 if ((!req
->tc
) || (!req
->rc
)) {
186 printk(KERN_ERR
"Couldn't grow tag array\n");
190 req
->tc
= req
->rc
= NULL
;
192 return ERR_PTR(-ENOMEM
);
194 req
->tc
->sdata
= (char *) req
->tc
+ sizeof(struct p9_fcall
);
195 req
->tc
->capacity
= c
->msize
;
196 req
->rc
->sdata
= (char *) req
->rc
+ sizeof(struct p9_fcall
);
197 req
->rc
->capacity
= c
->msize
;
200 p9pdu_reset(req
->tc
);
201 p9pdu_reset(req
->rc
);
203 req
->tc
->tag
= tag
-1;
204 req
->status
= REQ_STATUS_ALLOC
;
206 return &c
->reqs
[row
][col
];
210 * p9_tag_lookup - lookup a request by tag
211 * @c: client session to lookup tag within
212 * @tag: numeric id for transaction
216 struct p9_req_t
*p9_tag_lookup(struct p9_client
*c
, u16 tag
)
220 /* This looks up the original request by tag so we know which
221 * buffer to read the data into */
224 if(tag
>= c
->max_tag
)
227 row
= tag
/ P9_ROW_MAXTAG
;
228 col
= tag
% P9_ROW_MAXTAG
;
230 return &c
->reqs
[row
][col
];
232 EXPORT_SYMBOL(p9_tag_lookup
);
235 * p9_tag_init - setup tags structure and contents
236 * @c: v9fs client struct
238 * This initializes the tags structure for each client instance.
242 static int p9_tag_init(struct p9_client
*c
)
246 c
->tagpool
= p9_idpool_create();
247 if (IS_ERR(c
->tagpool
)) {
248 err
= PTR_ERR(c
->tagpool
);
253 p9_idpool_get(c
->tagpool
); /* reserve tag 0 */
261 * p9_tag_cleanup - cleans up tags structure and reclaims resources
262 * @c: v9fs client struct
264 * This frees resources associated with the tags structure
267 static void p9_tag_cleanup(struct p9_client
*c
)
271 /* check to insure all requests are idle */
272 for (row
= 0; row
< (c
->max_tag
/P9_ROW_MAXTAG
); row
++) {
273 for (col
= 0; col
< P9_ROW_MAXTAG
; col
++) {
274 if (c
->reqs
[row
][col
].status
!= REQ_STATUS_IDLE
) {
275 P9_DPRINTK(P9_DEBUG_MUX
,
276 "Attempting to cleanup non-free tag %d,%d\n",
278 /* TODO: delay execution of cleanup */
285 p9_idpool_destroy(c
->tagpool
);
287 /* free requests associated with tags */
288 for (row
= 0; row
< (c
->max_tag
/P9_ROW_MAXTAG
); row
++) {
289 for (col
= 0; col
< P9_ROW_MAXTAG
; col
++) {
290 kfree(c
->reqs
[row
][col
].wq
);
291 kfree(c
->reqs
[row
][col
].tc
);
292 kfree(c
->reqs
[row
][col
].rc
);
300 * p9_free_req - free a request and clean-up as necessary
302 * r: request to release
306 static void p9_free_req(struct p9_client
*c
, struct p9_req_t
*r
)
308 int tag
= r
->tc
->tag
;
309 P9_DPRINTK(P9_DEBUG_MUX
, "clnt %p req %p tag: %d\n", c
, r
, tag
);
311 r
->status
= REQ_STATUS_IDLE
;
312 if (tag
!= P9_NOTAG
&& p9_idpool_check(tag
, c
->tagpool
))
313 p9_idpool_put(tag
, c
->tagpool
);
317 * p9_client_cb - call back from transport to client
319 * req: request received
322 void p9_client_cb(struct p9_client
*c
, struct p9_req_t
*req
)
324 P9_DPRINTK(P9_DEBUG_MUX
, " tag %d\n", req
->tc
->tag
);
326 P9_DPRINTK(P9_DEBUG_MUX
, "wakeup: %d\n", req
->tc
->tag
);
328 EXPORT_SYMBOL(p9_client_cb
);
331 * p9_parse_header - parse header arguments out of a packet
332 * @pdu: packet to parse
333 * @size: size of packet
334 * @type: type of request
335 * @tag: tag of packet
336 * @rewind: set if we need to rewind offset afterwards
340 p9_parse_header(struct p9_fcall
*pdu
, int32_t *size
, int8_t *type
, int16_t *tag
,
346 int offset
= pdu
->offset
;
353 err
= p9pdu_readf(pdu
, 0, "dbw", &r_size
, &r_type
, &r_tag
);
355 goto rewind_and_exit
;
361 P9_DPRINTK(P9_DEBUG_9P
, "<<< size=%d type: %d tag: %d\n", pdu
->size
,
374 pdu
->offset
= offset
;
377 EXPORT_SYMBOL(p9_parse_header
);
380 * p9_check_errors - check 9p packet for error return and process it
381 * @c: current client instance
382 * @req: request to parse and check for error conditions
384 * returns error code if one is discovered, otherwise returns 0
386 * this will have to be more complicated if we have multiple
390 static int p9_check_errors(struct p9_client
*c
, struct p9_req_t
*req
)
395 err
= p9_parse_header(req
->rc
, NULL
, &type
, NULL
, 0);
397 P9_DPRINTK(P9_DEBUG_ERROR
, "couldn't parse header %d\n", err
);
401 if (type
== P9_RERROR
) {
405 err
= p9pdu_readf(req
->rc
, c
->dotu
, "s?d", &ename
, &ecode
);
407 P9_DPRINTK(P9_DEBUG_ERROR
, "couldn't parse error%d\n",
415 if (!err
|| !IS_ERR_VALUE(err
))
416 err
= p9_errstr2errno(ename
, strlen(ename
));
418 P9_DPRINTK(P9_DEBUG_9P
, "<<< RERROR (%d) %s\n", -ecode
, ename
);
428 * p9_client_flush - flush (cancel) a request
430 * @oldreq: request to cancel
432 * This sents a flush for a particular requests and links
433 * the flush request to the original request. The current
434 * code only supports a single flush request although the protocol
435 * allows for multiple flush requests to be sent for a single request.
439 static int p9_client_flush(struct p9_client
*c
, struct p9_req_t
*oldreq
)
441 struct p9_req_t
*req
;
445 err
= p9_parse_header(oldreq
->tc
, NULL
, NULL
, &oldtag
, 1);
449 P9_DPRINTK(P9_DEBUG_9P
, ">>> TFLUSH tag %d\n", oldtag
);
451 req
= p9_client_rpc(c
, P9_TFLUSH
, "w", oldtag
);
456 /* if we haven't received a response for oldreq,
457 remove it from the list. */
459 if (oldreq
->status
== REQ_STATUS_FLSH
)
460 list_del(&oldreq
->req_list
);
461 spin_unlock(&c
->lock
);
468 * p9_client_rpc - issue a request and wait for a response
470 * @type: type of request
471 * @fmt: protocol format string (see protocol.c)
473 * Returns request structure (which client must free using p9_free_req)
476 static struct p9_req_t
*
477 p9_client_rpc(struct p9_client
*c
, int8_t type
, const char *fmt
, ...)
481 struct p9_req_t
*req
;
485 P9_DPRINTK(P9_DEBUG_MUX
, "client %p op %d\n", c
, type
);
487 if (c
->status
!= Connected
)
488 return ERR_PTR(-EIO
);
490 if (signal_pending(current
)) {
492 clear_thread_flag(TIF_SIGPENDING
);
497 if (type
!= P9_TVERSION
) {
498 tag
= p9_idpool_get(c
->tagpool
);
500 return ERR_PTR(-ENOMEM
);
503 req
= p9_tag_alloc(c
, tag
);
507 /* marshall the data */
508 p9pdu_prepare(req
->tc
, tag
, type
);
510 err
= p9pdu_vwritef(req
->tc
, c
->dotu
, fmt
, ap
);
512 p9pdu_finalize(req
->tc
);
514 err
= c
->trans_mod
->request(c
, req
);
516 c
->status
= Disconnected
;
520 P9_DPRINTK(P9_DEBUG_MUX
, "wait %p tag: %d\n", req
->wq
, tag
);
521 err
= wait_event_interruptible(*req
->wq
,
522 req
->status
>= REQ_STATUS_RCVD
);
523 P9_DPRINTK(P9_DEBUG_MUX
, "wait %p tag: %d returned %d\n",
526 if (req
->status
== REQ_STATUS_ERROR
) {
527 P9_DPRINTK(P9_DEBUG_ERROR
, "req_status error %d\n", req
->t_err
);
531 if ((err
== -ERESTARTSYS
) && (c
->status
== Connected
)) {
532 P9_DPRINTK(P9_DEBUG_MUX
, "flushing\n");
534 clear_thread_flag(TIF_SIGPENDING
);
536 if (c
->trans_mod
->cancel(c
, req
))
537 p9_client_flush(c
, req
);
539 /* if we received the response anyway, don't signal error */
540 if (req
->status
== REQ_STATUS_RCVD
)
545 spin_lock_irqsave(¤t
->sighand
->siglock
, flags
);
547 spin_unlock_irqrestore(¤t
->sighand
->siglock
, flags
);
553 err
= p9_check_errors(c
, req
);
555 P9_DPRINTK(P9_DEBUG_MUX
, "exit: client %p op %d\n", c
, type
);
560 P9_DPRINTK(P9_DEBUG_MUX
, "exit: client %p op %d error: %d\n", c
, type
,
566 static struct p9_fid
*p9_fid_create(struct p9_client
*clnt
)
572 P9_DPRINTK(P9_DEBUG_FID
, "clnt %p\n", clnt
);
573 fid
= kmalloc(sizeof(struct p9_fid
), GFP_KERNEL
);
575 return ERR_PTR(-ENOMEM
);
577 ret
= p9_idpool_get(clnt
->fidpool
);
584 memset(&fid
->qid
, 0, sizeof(struct p9_qid
));
586 fid
->uid
= current_fsuid();
589 spin_lock_irqsave(&clnt
->lock
, flags
);
590 list_add(&fid
->flist
, &clnt
->fidlist
);
591 spin_unlock_irqrestore(&clnt
->lock
, flags
);
600 static void p9_fid_destroy(struct p9_fid
*fid
)
602 struct p9_client
*clnt
;
605 P9_DPRINTK(P9_DEBUG_FID
, "fid %d\n", fid
->fid
);
607 p9_idpool_put(fid
->fid
, clnt
->fidpool
);
608 spin_lock_irqsave(&clnt
->lock
, flags
);
609 list_del(&fid
->flist
);
610 spin_unlock_irqrestore(&clnt
->lock
, flags
);
615 int p9_client_version(struct p9_client
*c
)
618 struct p9_req_t
*req
;
622 P9_DPRINTK(P9_DEBUG_9P
, ">>> TVERSION msize %d extended %d\n",
624 req
= p9_client_rpc(c
, P9_TVERSION
, "ds", c
->msize
,
625 c
->dotu
? "9P2000.u" : "9P2000");
629 err
= p9pdu_readf(req
->rc
, c
->dotu
, "ds", &msize
, &version
);
631 P9_DPRINTK(P9_DEBUG_9P
, "version error %d\n", err
);
632 p9pdu_dump(1, req
->rc
);
636 P9_DPRINTK(P9_DEBUG_9P
, "<<< RVERSION msize %d %s\n", msize
, version
);
637 if (!memcmp(version
, "9P2000.u", 8))
639 else if (!memcmp(version
, "9P2000", 6))
646 if (msize
< c
->msize
)
655 EXPORT_SYMBOL(p9_client_version
);
657 struct p9_client
*p9_client_create(const char *dev_name
, char *options
)
660 struct p9_client
*clnt
;
663 clnt
= kmalloc(sizeof(struct p9_client
), GFP_KERNEL
);
665 return ERR_PTR(-ENOMEM
);
667 clnt
->trans_mod
= NULL
;
669 spin_lock_init(&clnt
->lock
);
670 INIT_LIST_HEAD(&clnt
->fidlist
);
671 clnt
->fidpool
= p9_idpool_create();
672 if (IS_ERR(clnt
->fidpool
)) {
673 err
= PTR_ERR(clnt
->fidpool
);
674 clnt
->fidpool
= NULL
;
680 err
= parse_opts(options
, clnt
);
684 if (!clnt
->trans_mod
)
685 clnt
->trans_mod
= v9fs_get_default_trans();
687 if (clnt
->trans_mod
== NULL
) {
688 err
= -EPROTONOSUPPORT
;
689 P9_DPRINTK(P9_DEBUG_ERROR
,
690 "No transport defined or default transport\n");
694 P9_DPRINTK(P9_DEBUG_MUX
, "clnt %p trans %p msize %d dotu %d\n",
695 clnt
, clnt
->trans_mod
, clnt
->msize
, clnt
->dotu
);
697 err
= clnt
->trans_mod
->create(clnt
, dev_name
, options
);
701 if (clnt
->msize
> clnt
->trans_mod
->maxsize
)
702 clnt
->msize
= clnt
->trans_mod
->maxsize
;
704 err
= p9_client_version(clnt
);
711 p9_client_destroy(clnt
);
714 EXPORT_SYMBOL(p9_client_create
);
716 void p9_client_destroy(struct p9_client
*clnt
)
718 struct p9_fid
*fid
, *fidptr
;
720 P9_DPRINTK(P9_DEBUG_MUX
, "clnt %p\n", clnt
);
723 clnt
->trans_mod
->close(clnt
);
725 v9fs_put_trans(clnt
->trans_mod
);
727 list_for_each_entry_safe(fid
, fidptr
, &clnt
->fidlist
, flist
)
731 p9_idpool_destroy(clnt
->fidpool
);
733 p9_tag_cleanup(clnt
);
737 EXPORT_SYMBOL(p9_client_destroy
);
739 void p9_client_disconnect(struct p9_client
*clnt
)
741 P9_DPRINTK(P9_DEBUG_9P
, "clnt %p\n", clnt
);
742 clnt
->status
= Disconnected
;
744 EXPORT_SYMBOL(p9_client_disconnect
);
746 struct p9_fid
*p9_client_attach(struct p9_client
*clnt
, struct p9_fid
*afid
,
747 char *uname
, u32 n_uname
, char *aname
)
750 struct p9_req_t
*req
;
754 P9_DPRINTK(P9_DEBUG_9P
, ">>> TATTACH afid %d uname %s aname %s\n",
755 afid
? afid
->fid
: -1, uname
, aname
);
758 fid
= p9_fid_create(clnt
);
765 req
= p9_client_rpc(clnt
, P9_TATTACH
, "ddss?d", fid
->fid
,
766 afid
? afid
->fid
: P9_NOFID
, uname
, aname
, n_uname
);
772 err
= p9pdu_readf(req
->rc
, clnt
->dotu
, "Q", &qid
);
774 p9pdu_dump(1, req
->rc
);
775 p9_free_req(clnt
, req
);
779 P9_DPRINTK(P9_DEBUG_9P
, "<<< RATTACH qid %x.%llx.%x\n",
781 (unsigned long long)qid
.path
,
784 memmove(&fid
->qid
, &qid
, sizeof(struct p9_qid
));
786 p9_free_req(clnt
, req
);
794 EXPORT_SYMBOL(p9_client_attach
);
797 p9_client_auth(struct p9_client
*clnt
, char *uname
, u32 n_uname
, char *aname
)
800 struct p9_req_t
*req
;
804 P9_DPRINTK(P9_DEBUG_9P
, ">>> TAUTH uname %s aname %s\n", uname
, aname
);
807 afid
= p9_fid_create(clnt
);
814 req
= p9_client_rpc(clnt
, P9_TAUTH
, "dss?d",
815 afid
? afid
->fid
: P9_NOFID
, uname
, aname
, n_uname
);
821 err
= p9pdu_readf(req
->rc
, clnt
->dotu
, "Q", &qid
);
823 p9pdu_dump(1, req
->rc
);
824 p9_free_req(clnt
, req
);
828 P9_DPRINTK(P9_DEBUG_9P
, "<<< RAUTH qid %x.%llx.%x\n",
830 (unsigned long long)qid
.path
,
833 memmove(&afid
->qid
, &qid
, sizeof(struct p9_qid
));
834 p9_free_req(clnt
, req
);
839 p9_fid_destroy(afid
);
842 EXPORT_SYMBOL(p9_client_auth
);
844 struct p9_fid
*p9_client_walk(struct p9_fid
*oldfid
, int nwname
, char **wnames
,
848 struct p9_client
*clnt
;
850 struct p9_qid
*wqids
;
851 struct p9_req_t
*req
;
852 int16_t nwqids
, count
;
857 fid
= p9_fid_create(clnt
);
864 fid
->uid
= oldfid
->uid
;
869 P9_DPRINTK(P9_DEBUG_9P
, ">>> TWALK fids %d,%d nwname %d wname[0] %s\n",
870 oldfid
->fid
, fid
->fid
, nwname
, wnames
? wnames
[0] : NULL
);
872 req
= p9_client_rpc(clnt
, P9_TWALK
, "ddT", oldfid
->fid
, fid
->fid
,
879 err
= p9pdu_readf(req
->rc
, clnt
->dotu
, "R", &nwqids
, &wqids
);
881 p9pdu_dump(1, req
->rc
);
882 p9_free_req(clnt
, req
);
885 p9_free_req(clnt
, req
);
887 P9_DPRINTK(P9_DEBUG_9P
, "<<< RWALK nwqid %d:\n", nwqids
);
889 if (nwqids
!= nwname
) {
894 for (count
= 0; count
< nwqids
; count
++)
895 P9_DPRINTK(P9_DEBUG_9P
, "<<< [%d] %x.%llx.%x\n",
896 count
, wqids
[count
].type
,
897 (unsigned long long)wqids
[count
].path
,
898 wqids
[count
].version
);
901 memmove(&fid
->qid
, &wqids
[nwqids
- 1], sizeof(struct p9_qid
));
903 fid
->qid
= oldfid
->qid
;
908 p9_client_clunk(fid
);
912 if (fid
&& (fid
!= oldfid
))
917 EXPORT_SYMBOL(p9_client_walk
);
919 int p9_client_open(struct p9_fid
*fid
, int mode
)
922 struct p9_client
*clnt
;
923 struct p9_req_t
*req
;
927 P9_DPRINTK(P9_DEBUG_9P
, ">>> TOPEN fid %d mode %d\n", fid
->fid
, mode
);
934 req
= p9_client_rpc(clnt
, P9_TOPEN
, "db", fid
->fid
, mode
);
940 err
= p9pdu_readf(req
->rc
, clnt
->dotu
, "Qd", &qid
, &iounit
);
942 p9pdu_dump(1, req
->rc
);
946 P9_DPRINTK(P9_DEBUG_9P
, "<<< ROPEN qid %x.%llx.%x iounit %x\n",
948 (unsigned long long)qid
.path
,
949 qid
.version
, iounit
);
952 fid
->iounit
= iounit
;
955 p9_free_req(clnt
, req
);
959 EXPORT_SYMBOL(p9_client_open
);
961 int p9_client_fcreate(struct p9_fid
*fid
, char *name
, u32 perm
, int mode
,
965 struct p9_client
*clnt
;
966 struct p9_req_t
*req
;
970 P9_DPRINTK(P9_DEBUG_9P
, ">>> TCREATE fid %d name %s perm %d mode %d\n",
971 fid
->fid
, name
, perm
, mode
);
978 req
= p9_client_rpc(clnt
, P9_TCREATE
, "dsdb?s", fid
->fid
, name
, perm
,
985 err
= p9pdu_readf(req
->rc
, clnt
->dotu
, "Qd", &qid
, &iounit
);
987 p9pdu_dump(1, req
->rc
);
991 P9_DPRINTK(P9_DEBUG_9P
, "<<< RCREATE qid %x.%llx.%x iounit %x\n",
993 (unsigned long long)qid
.path
,
994 qid
.version
, iounit
);
997 fid
->iounit
= iounit
;
1000 p9_free_req(clnt
, req
);
1004 EXPORT_SYMBOL(p9_client_fcreate
);
1006 int p9_client_clunk(struct p9_fid
*fid
)
1009 struct p9_client
*clnt
;
1010 struct p9_req_t
*req
;
1012 P9_DPRINTK(P9_DEBUG_9P
, ">>> TCLUNK fid %d\n", fid
->fid
);
1016 req
= p9_client_rpc(clnt
, P9_TCLUNK
, "d", fid
->fid
);
1022 P9_DPRINTK(P9_DEBUG_9P
, "<<< RCLUNK fid %d\n", fid
->fid
);
1024 p9_free_req(clnt
, req
);
1027 * Fid is not valid even after a failed clunk
1029 p9_fid_destroy(fid
);
1032 EXPORT_SYMBOL(p9_client_clunk
);
1034 int p9_client_remove(struct p9_fid
*fid
)
1037 struct p9_client
*clnt
;
1038 struct p9_req_t
*req
;
1040 P9_DPRINTK(P9_DEBUG_9P
, ">>> TREMOVE fid %d\n", fid
->fid
);
1044 req
= p9_client_rpc(clnt
, P9_TREMOVE
, "d", fid
->fid
);
1050 P9_DPRINTK(P9_DEBUG_9P
, "<<< RREMOVE fid %d\n", fid
->fid
);
1052 p9_free_req(clnt
, req
);
1053 p9_fid_destroy(fid
);
1058 EXPORT_SYMBOL(p9_client_remove
);
1061 p9_client_read(struct p9_fid
*fid
, char *data
, char __user
*udata
, u64 offset
,
1064 int err
, rsize
, total
;
1065 struct p9_client
*clnt
;
1066 struct p9_req_t
*req
;
1069 P9_DPRINTK(P9_DEBUG_9P
, ">>> TREAD fid %d offset %llu %d\n", fid
->fid
,
1070 (long long unsigned) offset
, count
);
1075 rsize
= fid
->iounit
;
1076 if (!rsize
|| rsize
> clnt
->msize
-P9_IOHDRSZ
)
1077 rsize
= clnt
->msize
- P9_IOHDRSZ
;
1082 req
= p9_client_rpc(clnt
, P9_TREAD
, "dqd", fid
->fid
, offset
, rsize
);
1088 err
= p9pdu_readf(req
->rc
, clnt
->dotu
, "D", &count
, &dataptr
);
1090 p9pdu_dump(1, req
->rc
);
1091 goto free_and_error
;
1094 P9_DPRINTK(P9_DEBUG_9P
, "<<< RREAD count %d\n", count
);
1097 memmove(data
, dataptr
, count
);
1101 err
= copy_to_user(udata
, dataptr
, count
);
1104 goto free_and_error
;
1108 p9_free_req(clnt
, req
);
1112 p9_free_req(clnt
, req
);
1116 EXPORT_SYMBOL(p9_client_read
);
1119 p9_client_write(struct p9_fid
*fid
, char *data
, const char __user
*udata
,
1120 u64 offset
, u32 count
)
1122 int err
, rsize
, total
;
1123 struct p9_client
*clnt
;
1124 struct p9_req_t
*req
;
1126 P9_DPRINTK(P9_DEBUG_9P
, ">>> TWRITE fid %d offset %llu count %d\n",
1127 fid
->fid
, (long long unsigned) offset
, count
);
1132 rsize
= fid
->iounit
;
1133 if (!rsize
|| rsize
> clnt
->msize
-P9_IOHDRSZ
)
1134 rsize
= clnt
->msize
- P9_IOHDRSZ
;
1139 req
= p9_client_rpc(clnt
, P9_TWRITE
, "dqD", fid
->fid
, offset
,
1142 req
= p9_client_rpc(clnt
, P9_TWRITE
, "dqU", fid
->fid
, offset
,
1149 err
= p9pdu_readf(req
->rc
, clnt
->dotu
, "d", &count
);
1151 p9pdu_dump(1, req
->rc
);
1152 goto free_and_error
;
1155 P9_DPRINTK(P9_DEBUG_9P
, "<<< RWRITE count %d\n", count
);
1157 p9_free_req(clnt
, req
);
1161 p9_free_req(clnt
, req
);
1165 EXPORT_SYMBOL(p9_client_write
);
1167 struct p9_wstat
*p9_client_stat(struct p9_fid
*fid
)
1170 struct p9_client
*clnt
;
1171 struct p9_wstat
*ret
= kmalloc(sizeof(struct p9_wstat
), GFP_KERNEL
);
1172 struct p9_req_t
*req
;
1175 P9_DPRINTK(P9_DEBUG_9P
, ">>> TSTAT fid %d\n", fid
->fid
);
1178 return ERR_PTR(-ENOMEM
);
1183 req
= p9_client_rpc(clnt
, P9_TSTAT
, "d", fid
->fid
);
1189 err
= p9pdu_readf(req
->rc
, clnt
->dotu
, "wS", &ignored
, ret
);
1191 p9pdu_dump(1, req
->rc
);
1192 p9_free_req(clnt
, req
);
1196 P9_DPRINTK(P9_DEBUG_9P
,
1197 "<<< RSTAT sz=%x type=%x dev=%x qid=%x.%llx.%x\n"
1198 "<<< mode=%8.8x atime=%8.8x mtime=%8.8x length=%llx\n"
1199 "<<< name=%s uid=%s gid=%s muid=%s extension=(%s)\n"
1200 "<<< uid=%d gid=%d n_muid=%d\n",
1201 ret
->size
, ret
->type
, ret
->dev
, ret
->qid
.type
,
1202 (unsigned long long)ret
->qid
.path
, ret
->qid
.version
, ret
->mode
,
1203 ret
->atime
, ret
->mtime
, (unsigned long long)ret
->length
,
1204 ret
->name
, ret
->uid
, ret
->gid
, ret
->muid
, ret
->extension
,
1205 ret
->n_uid
, ret
->n_gid
, ret
->n_muid
);
1207 p9_free_req(clnt
, req
);
1212 return ERR_PTR(err
);
1214 EXPORT_SYMBOL(p9_client_stat
);
1216 static int p9_client_statsize(struct p9_wstat
*wst
, int optional
)
1220 /* size[2] type[2] dev[4] qid[13] */
1221 /* mode[4] atime[4] mtime[4] length[8]*/
1222 /* name[s] uid[s] gid[s] muid[s] */
1223 ret
= 2+2+4+13+4+4+4+8+2+2+2+2;
1226 ret
+= strlen(wst
->name
);
1228 ret
+= strlen(wst
->uid
);
1230 ret
+= strlen(wst
->gid
);
1232 ret
+= strlen(wst
->muid
);
1235 ret
+= 2+4+4+4; /* extension[s] n_uid[4] n_gid[4] n_muid[4] */
1237 ret
+= strlen(wst
->extension
);
1243 int p9_client_wstat(struct p9_fid
*fid
, struct p9_wstat
*wst
)
1246 struct p9_req_t
*req
;
1247 struct p9_client
*clnt
;
1251 wst
->size
= p9_client_statsize(wst
, clnt
->dotu
);
1252 P9_DPRINTK(P9_DEBUG_9P
, ">>> TWSTAT fid %d\n", fid
->fid
);
1253 P9_DPRINTK(P9_DEBUG_9P
,
1254 " sz=%x type=%x dev=%x qid=%x.%llx.%x\n"
1255 " mode=%8.8x atime=%8.8x mtime=%8.8x length=%llx\n"
1256 " name=%s uid=%s gid=%s muid=%s extension=(%s)\n"
1257 " uid=%d gid=%d n_muid=%d\n",
1258 wst
->size
, wst
->type
, wst
->dev
, wst
->qid
.type
,
1259 (unsigned long long)wst
->qid
.path
, wst
->qid
.version
, wst
->mode
,
1260 wst
->atime
, wst
->mtime
, (unsigned long long)wst
->length
,
1261 wst
->name
, wst
->uid
, wst
->gid
, wst
->muid
, wst
->extension
,
1262 wst
->n_uid
, wst
->n_gid
, wst
->n_muid
);
1264 req
= p9_client_rpc(clnt
, P9_TWSTAT
, "dwS", fid
->fid
, wst
->size
, wst
);
1270 P9_DPRINTK(P9_DEBUG_9P
, "<<< RWSTAT fid %d\n", fid
->fid
);
1272 p9_free_req(clnt
, req
);
1276 EXPORT_SYMBOL(p9_client_wstat
);