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
53 static const match_table_t tokens = {
54 {Opt_msize, "msize=%u"},
55 {Opt_legacy, "noextend"},
56 {Opt_trans, "trans=%s"},
57 {Opt_version, "version=%s"},
61 inline int p9_is_proto_dotl(struct p9_client *clnt)
63 return (clnt->proto_version == p9_proto_2010L);
65 EXPORT_SYMBOL(p9_is_proto_dotl);
67 inline int p9_is_proto_dotu(struct p9_client *clnt)
69 return (clnt->proto_version == p9_proto_2000u);
71 EXPORT_SYMBOL(p9_is_proto_dotu);
73 /* Interpret mount option for protocol version */
74 static unsigned char get_protocol_version(const substring_t *name)
76 unsigned char version = -EINVAL;
77 if (!strncmp("9p2000", name->from, name->to-name->from)) {
78 version = p9_proto_legacy;
79 P9_DPRINTK(P9_DEBUG_9P, "Protocol version: Legacy\n");
80 } else if (!strncmp("9p2000.u", name->from, name->to-name->from)) {
81 version = p9_proto_2000u;
82 P9_DPRINTK(P9_DEBUG_9P, "Protocol version: 9P2000.u\n");
83 } else if (!strncmp("9p2010.L", name->from, name->to-name->from)) {
84 version = p9_proto_2010L;
85 P9_DPRINTK(P9_DEBUG_9P, "Protocol version: 9P2010.L\n");
87 P9_DPRINTK(P9_DEBUG_ERROR, "Unknown protocol version %s. ",
93 static struct p9_req_t *
94 p9_client_rpc(struct p9_client *c, int8_t type, const char *fmt, ...);
97 * parse_options - parse mount options into client structure
98 * @opts: options string passed from mount
99 * @clnt: existing v9fs client information
101 * Return 0 upon success, -ERRNO upon failure
104 static int parse_opts(char *opts, struct p9_client *clnt)
106 char *options, *tmp_options;
108 substring_t args[MAX_OPT_ARGS];
112 clnt->proto_version = p9_proto_2000u;
118 tmp_options = kstrdup(opts, GFP_KERNEL);
120 P9_DPRINTK(P9_DEBUG_ERROR,
121 "failed to allocate copy of option string\n");
124 options = tmp_options;
126 while ((p = strsep(&options, ",")) != NULL) {
130 token = match_token(p, tokens, args);
131 if (token < Opt_trans) {
132 int r = match_int(&args[0], &option);
134 P9_DPRINTK(P9_DEBUG_ERROR,
135 "integer field, but no integer?\n");
142 clnt->msize = option;
145 clnt->trans_mod = v9fs_get_trans_by_name(&args[0]);
146 if(clnt->trans_mod == NULL) {
147 P9_DPRINTK(P9_DEBUG_ERROR,
148 "Could not find request transport: %s\n",
151 goto free_and_return;
155 clnt->proto_version = p9_proto_legacy;
158 ret = get_protocol_version(&args[0]);
160 goto free_and_return;
161 clnt->proto_version = ret;
174 * p9_tag_alloc - lookup/allocate a request by tag
175 * @c: client session to lookup tag within
176 * @tag: numeric id for transaction
178 * this is a simple array lookup, but will grow the
179 * request_slots as necessary to accomodate transaction
180 * ids which did not previously have a slot.
182 * this code relies on the client spinlock to manage locks, its
183 * possible we should switch to something else, but I'd rather
184 * stick with something low-overhead for the common case.
188 static struct p9_req_t *p9_tag_alloc(struct p9_client *c, u16 tag)
192 struct p9_req_t *req;
194 /* This looks up the original request by tag so we know which
195 * buffer to read the data into */
198 if (tag >= c->max_tag) {
199 spin_lock_irqsave(&c->lock, flags);
200 /* check again since original check was outside of lock */
201 while (tag >= c->max_tag) {
202 row = (tag / P9_ROW_MAXTAG);
203 c->reqs[row] = kcalloc(P9_ROW_MAXTAG,
204 sizeof(struct p9_req_t), GFP_ATOMIC);
207 printk(KERN_ERR "Couldn't grow tag array\n");
208 spin_unlock_irqrestore(&c->lock, flags);
209 return ERR_PTR(-ENOMEM);
211 for (col = 0; col < P9_ROW_MAXTAG; col++) {
212 c->reqs[row][col].status = REQ_STATUS_IDLE;
213 c->reqs[row][col].tc = NULL;
215 c->max_tag += P9_ROW_MAXTAG;
217 spin_unlock_irqrestore(&c->lock, flags);
219 row = tag / P9_ROW_MAXTAG;
220 col = tag % P9_ROW_MAXTAG;
222 req = &c->reqs[row][col];
224 req->wq = kmalloc(sizeof(wait_queue_head_t), GFP_KERNEL);
226 printk(KERN_ERR "Couldn't grow tag array\n");
227 return ERR_PTR(-ENOMEM);
229 init_waitqueue_head(req->wq);
230 req->tc = kmalloc(sizeof(struct p9_fcall)+c->msize,
232 req->rc = kmalloc(sizeof(struct p9_fcall)+c->msize,
234 if ((!req->tc) || (!req->rc)) {
235 printk(KERN_ERR "Couldn't grow tag array\n");
239 req->tc = req->rc = NULL;
241 return ERR_PTR(-ENOMEM);
243 req->tc->sdata = (char *) req->tc + sizeof(struct p9_fcall);
244 req->tc->capacity = c->msize;
245 req->rc->sdata = (char *) req->rc + sizeof(struct p9_fcall);
246 req->rc->capacity = c->msize;
249 p9pdu_reset(req->tc);
250 p9pdu_reset(req->rc);
252 req->tc->tag = tag-1;
253 req->status = REQ_STATUS_ALLOC;
255 return &c->reqs[row][col];
259 * p9_tag_lookup - lookup a request by tag
260 * @c: client session to lookup tag within
261 * @tag: numeric id for transaction
265 struct p9_req_t *p9_tag_lookup(struct p9_client *c, u16 tag)
269 /* This looks up the original request by tag so we know which
270 * buffer to read the data into */
273 BUG_ON(tag >= c->max_tag);
275 row = tag / P9_ROW_MAXTAG;
276 col = tag % P9_ROW_MAXTAG;
278 return &c->reqs[row][col];
280 EXPORT_SYMBOL(p9_tag_lookup);
283 * p9_tag_init - setup tags structure and contents
284 * @c: v9fs client struct
286 * This initializes the tags structure for each client instance.
290 static int p9_tag_init(struct p9_client *c)
294 c->tagpool = p9_idpool_create();
295 if (IS_ERR(c->tagpool)) {
296 err = PTR_ERR(c->tagpool);
301 p9_idpool_get(c->tagpool); /* reserve tag 0 */
309 * p9_tag_cleanup - cleans up tags structure and reclaims resources
310 * @c: v9fs client struct
312 * This frees resources associated with the tags structure
315 static void p9_tag_cleanup(struct p9_client *c)
319 /* check to insure all requests are idle */
320 for (row = 0; row < (c->max_tag/P9_ROW_MAXTAG); row++) {
321 for (col = 0; col < P9_ROW_MAXTAG; col++) {
322 if (c->reqs[row][col].status != REQ_STATUS_IDLE) {
323 P9_DPRINTK(P9_DEBUG_MUX,
324 "Attempting to cleanup non-free tag %d,%d\n",
326 /* TODO: delay execution of cleanup */
333 p9_idpool_destroy(c->tagpool);
335 /* free requests associated with tags */
336 for (row = 0; row < (c->max_tag/P9_ROW_MAXTAG); row++) {
337 for (col = 0; col < P9_ROW_MAXTAG; col++) {
338 kfree(c->reqs[row][col].wq);
339 kfree(c->reqs[row][col].tc);
340 kfree(c->reqs[row][col].rc);
348 * p9_free_req - free a request and clean-up as necessary
350 * r: request to release
354 static void p9_free_req(struct p9_client *c, struct p9_req_t *r)
356 int tag = r->tc->tag;
357 P9_DPRINTK(P9_DEBUG_MUX, "clnt %p req %p tag: %d\n", c, r, tag);
359 r->status = REQ_STATUS_IDLE;
360 if (tag != P9_NOTAG && p9_idpool_check(tag, c->tagpool))
361 p9_idpool_put(tag, c->tagpool);
365 * p9_client_cb - call back from transport to client
367 * req: request received
370 void p9_client_cb(struct p9_client *c, struct p9_req_t *req)
372 P9_DPRINTK(P9_DEBUG_MUX, " tag %d\n", req->tc->tag);
374 P9_DPRINTK(P9_DEBUG_MUX, "wakeup: %d\n", req->tc->tag);
376 EXPORT_SYMBOL(p9_client_cb);
379 * p9_parse_header - parse header arguments out of a packet
380 * @pdu: packet to parse
381 * @size: size of packet
382 * @type: type of request
383 * @tag: tag of packet
384 * @rewind: set if we need to rewind offset afterwards
388 p9_parse_header(struct p9_fcall *pdu, int32_t *size, int8_t *type, int16_t *tag,
394 int offset = pdu->offset;
401 err = p9pdu_readf(pdu, 0, "dbw", &r_size, &r_type, &r_tag);
403 goto rewind_and_exit;
409 P9_DPRINTK(P9_DEBUG_9P, "<<< size=%d type: %d tag: %d\n", pdu->size,
422 pdu->offset = offset;
425 EXPORT_SYMBOL(p9_parse_header);
428 * p9_check_errors - check 9p packet for error return and process it
429 * @c: current client instance
430 * @req: request to parse and check for error conditions
432 * returns error code if one is discovered, otherwise returns 0
434 * this will have to be more complicated if we have multiple
438 static int p9_check_errors(struct p9_client *c, struct p9_req_t *req)
443 err = p9_parse_header(req->rc, NULL, &type, NULL, 0);
445 P9_DPRINTK(P9_DEBUG_ERROR, "couldn't parse header %d\n", err);
449 if (type == P9_RERROR) {
453 err = p9pdu_readf(req->rc, c->proto_version, "s?d",
456 P9_DPRINTK(P9_DEBUG_ERROR, "couldn't parse error%d\n",
461 if (p9_is_proto_dotu(c))
464 if (!err || !IS_ERR_VALUE(err))
465 err = p9_errstr2errno(ename, strlen(ename));
467 P9_DPRINTK(P9_DEBUG_9P, "<<< RERROR (%d) %s\n", -ecode, ename);
477 * p9_client_flush - flush (cancel) a request
479 * @oldreq: request to cancel
481 * This sents a flush for a particular requests and links
482 * the flush request to the original request. The current
483 * code only supports a single flush request although the protocol
484 * allows for multiple flush requests to be sent for a single request.
488 static int p9_client_flush(struct p9_client *c, struct p9_req_t *oldreq)
490 struct p9_req_t *req;
494 err = p9_parse_header(oldreq->tc, NULL, NULL, &oldtag, 1);
498 P9_DPRINTK(P9_DEBUG_9P, ">>> TFLUSH tag %d\n", oldtag);
500 req = p9_client_rpc(c, P9_TFLUSH, "w", oldtag);
505 /* if we haven't received a response for oldreq,
506 remove it from the list. */
508 if (oldreq->status == REQ_STATUS_FLSH)
509 list_del(&oldreq->req_list);
510 spin_unlock(&c->lock);
517 * p9_client_rpc - issue a request and wait for a response
519 * @type: type of request
520 * @fmt: protocol format string (see protocol.c)
522 * Returns request structure (which client must free using p9_free_req)
525 static struct p9_req_t *
526 p9_client_rpc(struct p9_client *c, int8_t type, const char *fmt, ...)
530 struct p9_req_t *req;
534 P9_DPRINTK(P9_DEBUG_MUX, "client %p op %d\n", c, type);
536 if (c->status != Connected)
537 return ERR_PTR(-EIO);
539 if (signal_pending(current)) {
541 clear_thread_flag(TIF_SIGPENDING);
546 if (type != P9_TVERSION) {
547 tag = p9_idpool_get(c->tagpool);
549 return ERR_PTR(-ENOMEM);
552 req = p9_tag_alloc(c, tag);
556 /* marshall the data */
557 p9pdu_prepare(req->tc, tag, type);
559 err = p9pdu_vwritef(req->tc, c->proto_version, fmt, ap);
561 p9pdu_finalize(req->tc);
563 err = c->trans_mod->request(c, req);
565 c->status = Disconnected;
569 P9_DPRINTK(P9_DEBUG_MUX, "wait %p tag: %d\n", req->wq, tag);
570 err = wait_event_interruptible(*req->wq,
571 req->status >= REQ_STATUS_RCVD);
572 P9_DPRINTK(P9_DEBUG_MUX, "wait %p tag: %d returned %d\n",
575 if (req->status == REQ_STATUS_ERROR) {
576 P9_DPRINTK(P9_DEBUG_ERROR, "req_status error %d\n", req->t_err);
580 if ((err == -ERESTARTSYS) && (c->status == Connected)) {
581 P9_DPRINTK(P9_DEBUG_MUX, "flushing\n");
583 clear_thread_flag(TIF_SIGPENDING);
585 if (c->trans_mod->cancel(c, req))
586 p9_client_flush(c, req);
588 /* if we received the response anyway, don't signal error */
589 if (req->status == REQ_STATUS_RCVD)
594 spin_lock_irqsave(¤t->sighand->siglock, flags);
596 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
602 err = p9_check_errors(c, req);
604 P9_DPRINTK(P9_DEBUG_MUX, "exit: client %p op %d\n", c, type);
609 P9_DPRINTK(P9_DEBUG_MUX, "exit: client %p op %d error: %d\n", c, type,
615 static struct p9_fid *p9_fid_create(struct p9_client *clnt)
621 P9_DPRINTK(P9_DEBUG_FID, "clnt %p\n", clnt);
622 fid = kmalloc(sizeof(struct p9_fid), GFP_KERNEL);
624 return ERR_PTR(-ENOMEM);
626 ret = p9_idpool_get(clnt->fidpool);
633 memset(&fid->qid, 0, sizeof(struct p9_qid));
635 fid->uid = current_fsuid();
638 spin_lock_irqsave(&clnt->lock, flags);
639 list_add(&fid->flist, &clnt->fidlist);
640 spin_unlock_irqrestore(&clnt->lock, flags);
649 static void p9_fid_destroy(struct p9_fid *fid)
651 struct p9_client *clnt;
654 P9_DPRINTK(P9_DEBUG_FID, "fid %d\n", fid->fid);
656 p9_idpool_put(fid->fid, clnt->fidpool);
657 spin_lock_irqsave(&clnt->lock, flags);
658 list_del(&fid->flist);
659 spin_unlock_irqrestore(&clnt->lock, flags);
664 int p9_client_version(struct p9_client *c)
667 struct p9_req_t *req;
671 P9_DPRINTK(P9_DEBUG_9P, ">>> TVERSION msize %d protocol %d\n",
672 c->msize, c->proto_version);
674 switch (c->proto_version) {
676 req = p9_client_rpc(c, P9_TVERSION, "ds",
677 c->msize, "9P2010.L");
680 req = p9_client_rpc(c, P9_TVERSION, "ds",
681 c->msize, "9P2000.u");
683 case p9_proto_legacy:
684 req = p9_client_rpc(c, P9_TVERSION, "ds",
695 err = p9pdu_readf(req->rc, c->proto_version, "ds", &msize, &version);
697 P9_DPRINTK(P9_DEBUG_9P, "version error %d\n", err);
698 p9pdu_dump(1, req->rc);
702 P9_DPRINTK(P9_DEBUG_9P, "<<< RVERSION msize %d %s\n", msize, version);
703 if (!strncmp(version, "9P2010.L", 8))
704 c->proto_version = p9_proto_2010L;
705 else if (!strncmp(version, "9P2000.u", 8))
706 c->proto_version = p9_proto_2000u;
707 else if (!strncmp(version, "9P2000", 6))
708 c->proto_version = p9_proto_legacy;
714 if (msize < c->msize)
723 EXPORT_SYMBOL(p9_client_version);
725 struct p9_client *p9_client_create(const char *dev_name, char *options)
728 struct p9_client *clnt;
731 clnt = kmalloc(sizeof(struct p9_client), GFP_KERNEL);
733 return ERR_PTR(-ENOMEM);
735 clnt->trans_mod = NULL;
737 spin_lock_init(&clnt->lock);
738 INIT_LIST_HEAD(&clnt->fidlist);
742 err = parse_opts(options, clnt);
746 if (!clnt->trans_mod)
747 clnt->trans_mod = v9fs_get_default_trans();
749 if (clnt->trans_mod == NULL) {
750 err = -EPROTONOSUPPORT;
751 P9_DPRINTK(P9_DEBUG_ERROR,
752 "No transport defined or default transport\n");
756 clnt->fidpool = p9_idpool_create();
757 if (IS_ERR(clnt->fidpool)) {
758 err = PTR_ERR(clnt->fidpool);
759 clnt->fidpool = NULL;
763 P9_DPRINTK(P9_DEBUG_MUX, "clnt %p trans %p msize %d protocol %d\n",
764 clnt, clnt->trans_mod, clnt->msize, clnt->proto_version);
766 err = clnt->trans_mod->create(clnt, dev_name, options);
768 goto destroy_fidpool;
770 if ((clnt->msize+P9_IOHDRSZ) > clnt->trans_mod->maxsize)
771 clnt->msize = clnt->trans_mod->maxsize-P9_IOHDRSZ;
773 err = p9_client_version(clnt);
780 clnt->trans_mod->close(clnt);
782 p9_idpool_destroy(clnt->fidpool);
784 v9fs_put_trans(clnt->trans_mod);
789 EXPORT_SYMBOL(p9_client_create);
791 void p9_client_destroy(struct p9_client *clnt)
793 struct p9_fid *fid, *fidptr;
795 P9_DPRINTK(P9_DEBUG_MUX, "clnt %p\n", clnt);
798 clnt->trans_mod->close(clnt);
800 v9fs_put_trans(clnt->trans_mod);
802 list_for_each_entry_safe(fid, fidptr, &clnt->fidlist, flist)
806 p9_idpool_destroy(clnt->fidpool);
808 p9_tag_cleanup(clnt);
812 EXPORT_SYMBOL(p9_client_destroy);
814 void p9_client_disconnect(struct p9_client *clnt)
816 P9_DPRINTK(P9_DEBUG_9P, "clnt %p\n", clnt);
817 clnt->status = Disconnected;
819 EXPORT_SYMBOL(p9_client_disconnect);
821 struct p9_fid *p9_client_attach(struct p9_client *clnt, struct p9_fid *afid,
822 char *uname, u32 n_uname, char *aname)
825 struct p9_req_t *req;
829 P9_DPRINTK(P9_DEBUG_9P, ">>> TATTACH afid %d uname %s aname %s\n",
830 afid ? afid->fid : -1, uname, aname);
833 fid = p9_fid_create(clnt);
840 req = p9_client_rpc(clnt, P9_TATTACH, "ddss?d", fid->fid,
841 afid ? afid->fid : P9_NOFID, uname, aname, n_uname);
847 err = p9pdu_readf(req->rc, clnt->proto_version, "Q", &qid);
849 p9pdu_dump(1, req->rc);
850 p9_free_req(clnt, req);
854 P9_DPRINTK(P9_DEBUG_9P, "<<< RATTACH qid %x.%llx.%x\n",
856 (unsigned long long)qid.path,
859 memmove(&fid->qid, &qid, sizeof(struct p9_qid));
861 p9_free_req(clnt, req);
869 EXPORT_SYMBOL(p9_client_attach);
872 p9_client_auth(struct p9_client *clnt, char *uname, u32 n_uname, char *aname)
875 struct p9_req_t *req;
879 P9_DPRINTK(P9_DEBUG_9P, ">>> TAUTH uname %s aname %s\n", uname, aname);
882 afid = p9_fid_create(clnt);
889 req = p9_client_rpc(clnt, P9_TAUTH, "dss?d",
890 afid ? afid->fid : P9_NOFID, uname, aname, n_uname);
896 err = p9pdu_readf(req->rc, clnt->proto_version, "Q", &qid);
898 p9pdu_dump(1, req->rc);
899 p9_free_req(clnt, req);
903 P9_DPRINTK(P9_DEBUG_9P, "<<< RAUTH qid %x.%llx.%x\n",
905 (unsigned long long)qid.path,
908 memmove(&afid->qid, &qid, sizeof(struct p9_qid));
909 p9_free_req(clnt, req);
914 p9_fid_destroy(afid);
917 EXPORT_SYMBOL(p9_client_auth);
919 struct p9_fid *p9_client_walk(struct p9_fid *oldfid, int nwname, char **wnames,
923 struct p9_client *clnt;
925 struct p9_qid *wqids;
926 struct p9_req_t *req;
927 int16_t nwqids, count;
932 fid = p9_fid_create(clnt);
939 fid->uid = oldfid->uid;
944 P9_DPRINTK(P9_DEBUG_9P, ">>> TWALK fids %d,%d nwname %d wname[0] %s\n",
945 oldfid->fid, fid->fid, nwname, wnames ? wnames[0] : NULL);
947 req = p9_client_rpc(clnt, P9_TWALK, "ddT", oldfid->fid, fid->fid,
954 err = p9pdu_readf(req->rc, clnt->proto_version, "R", &nwqids, &wqids);
956 p9pdu_dump(1, req->rc);
957 p9_free_req(clnt, req);
960 p9_free_req(clnt, req);
962 P9_DPRINTK(P9_DEBUG_9P, "<<< RWALK nwqid %d:\n", nwqids);
964 if (nwqids != nwname) {
969 for (count = 0; count < nwqids; count++)
970 P9_DPRINTK(P9_DEBUG_9P, "<<< [%d] %x.%llx.%x\n",
971 count, wqids[count].type,
972 (unsigned long long)wqids[count].path,
973 wqids[count].version);
976 memmove(&fid->qid, &wqids[nwqids - 1], sizeof(struct p9_qid));
978 fid->qid = oldfid->qid;
983 p9_client_clunk(fid);
987 if (fid && (fid != oldfid))
992 EXPORT_SYMBOL(p9_client_walk);
994 int p9_client_open(struct p9_fid *fid, int mode)
997 struct p9_client *clnt;
998 struct p9_req_t *req;
1002 P9_DPRINTK(P9_DEBUG_9P, ">>> TOPEN fid %d mode %d\n", fid->fid, mode);
1006 if (fid->mode != -1)
1009 req = p9_client_rpc(clnt, P9_TOPEN, "db", fid->fid, mode);
1015 err = p9pdu_readf(req->rc, clnt->proto_version, "Qd", &qid, &iounit);
1017 p9pdu_dump(1, req->rc);
1018 goto free_and_error;
1021 P9_DPRINTK(P9_DEBUG_9P, "<<< ROPEN qid %x.%llx.%x iounit %x\n",
1023 (unsigned long long)qid.path,
1024 qid.version, iounit);
1027 fid->iounit = iounit;
1030 p9_free_req(clnt, req);
1034 EXPORT_SYMBOL(p9_client_open);
1036 int p9_client_fcreate(struct p9_fid *fid, char *name, u32 perm, int mode,
1040 struct p9_client *clnt;
1041 struct p9_req_t *req;
1045 P9_DPRINTK(P9_DEBUG_9P, ">>> TCREATE fid %d name %s perm %d mode %d\n",
1046 fid->fid, name, perm, mode);
1050 if (fid->mode != -1)
1053 req = p9_client_rpc(clnt, P9_TCREATE, "dsdb?s", fid->fid, name, perm,
1060 err = p9pdu_readf(req->rc, clnt->proto_version, "Qd", &qid, &iounit);
1062 p9pdu_dump(1, req->rc);
1063 goto free_and_error;
1066 P9_DPRINTK(P9_DEBUG_9P, "<<< RCREATE qid %x.%llx.%x iounit %x\n",
1068 (unsigned long long)qid.path,
1069 qid.version, iounit);
1072 fid->iounit = iounit;
1075 p9_free_req(clnt, req);
1079 EXPORT_SYMBOL(p9_client_fcreate);
1081 int p9_client_clunk(struct p9_fid *fid)
1084 struct p9_client *clnt;
1085 struct p9_req_t *req;
1087 P9_DPRINTK(P9_DEBUG_9P, ">>> TCLUNK fid %d\n", fid->fid);
1091 req = p9_client_rpc(clnt, P9_TCLUNK, "d", fid->fid);
1097 P9_DPRINTK(P9_DEBUG_9P, "<<< RCLUNK fid %d\n", fid->fid);
1099 p9_free_req(clnt, req);
1100 p9_fid_destroy(fid);
1105 EXPORT_SYMBOL(p9_client_clunk);
1107 int p9_client_remove(struct p9_fid *fid)
1110 struct p9_client *clnt;
1111 struct p9_req_t *req;
1113 P9_DPRINTK(P9_DEBUG_9P, ">>> TREMOVE fid %d\n", fid->fid);
1117 req = p9_client_rpc(clnt, P9_TREMOVE, "d", fid->fid);
1123 P9_DPRINTK(P9_DEBUG_9P, "<<< RREMOVE fid %d\n", fid->fid);
1125 p9_free_req(clnt, req);
1126 p9_fid_destroy(fid);
1131 EXPORT_SYMBOL(p9_client_remove);
1134 p9_client_read(struct p9_fid *fid, char *data, char __user *udata, u64 offset,
1137 int err, rsize, total;
1138 struct p9_client *clnt;
1139 struct p9_req_t *req;
1142 P9_DPRINTK(P9_DEBUG_9P, ">>> TREAD fid %d offset %llu %d\n", fid->fid,
1143 (long long unsigned) offset, count);
1148 rsize = fid->iounit;
1149 if (!rsize || rsize > clnt->msize-P9_IOHDRSZ)
1150 rsize = clnt->msize - P9_IOHDRSZ;
1155 req = p9_client_rpc(clnt, P9_TREAD, "dqd", fid->fid, offset, rsize);
1161 err = p9pdu_readf(req->rc, clnt->proto_version, "D", &count, &dataptr);
1163 p9pdu_dump(1, req->rc);
1164 goto free_and_error;
1167 P9_DPRINTK(P9_DEBUG_9P, "<<< RREAD count %d\n", count);
1170 memmove(data, dataptr, count);
1174 err = copy_to_user(udata, dataptr, count);
1177 goto free_and_error;
1181 p9_free_req(clnt, req);
1185 p9_free_req(clnt, req);
1189 EXPORT_SYMBOL(p9_client_read);
1192 p9_client_write(struct p9_fid *fid, char *data, const char __user *udata,
1193 u64 offset, u32 count)
1195 int err, rsize, total;
1196 struct p9_client *clnt;
1197 struct p9_req_t *req;
1199 P9_DPRINTK(P9_DEBUG_9P, ">>> TWRITE fid %d offset %llu count %d\n",
1200 fid->fid, (long long unsigned) offset, count);
1205 rsize = fid->iounit;
1206 if (!rsize || rsize > clnt->msize-P9_IOHDRSZ)
1207 rsize = clnt->msize - P9_IOHDRSZ;
1212 req = p9_client_rpc(clnt, P9_TWRITE, "dqD", fid->fid, offset,
1215 req = p9_client_rpc(clnt, P9_TWRITE, "dqU", fid->fid, offset,
1222 err = p9pdu_readf(req->rc, clnt->proto_version, "d", &count);
1224 p9pdu_dump(1, req->rc);
1225 goto free_and_error;
1228 P9_DPRINTK(P9_DEBUG_9P, "<<< RWRITE count %d\n", count);
1230 p9_free_req(clnt, req);
1234 p9_free_req(clnt, req);
1238 EXPORT_SYMBOL(p9_client_write);
1240 struct p9_wstat *p9_client_stat(struct p9_fid *fid)
1243 struct p9_client *clnt;
1244 struct p9_wstat *ret = kmalloc(sizeof(struct p9_wstat), GFP_KERNEL);
1245 struct p9_req_t *req;
1248 P9_DPRINTK(P9_DEBUG_9P, ">>> TSTAT fid %d\n", fid->fid);
1251 return ERR_PTR(-ENOMEM);
1256 req = p9_client_rpc(clnt, P9_TSTAT, "d", fid->fid);
1262 err = p9pdu_readf(req->rc, clnt->proto_version, "wS", &ignored, ret);
1264 p9pdu_dump(1, req->rc);
1265 p9_free_req(clnt, req);
1269 P9_DPRINTK(P9_DEBUG_9P,
1270 "<<< RSTAT sz=%x type=%x dev=%x qid=%x.%llx.%x\n"
1271 "<<< mode=%8.8x atime=%8.8x mtime=%8.8x length=%llx\n"
1272 "<<< name=%s uid=%s gid=%s muid=%s extension=(%s)\n"
1273 "<<< uid=%d gid=%d n_muid=%d\n",
1274 ret->size, ret->type, ret->dev, ret->qid.type,
1275 (unsigned long long)ret->qid.path, ret->qid.version, ret->mode,
1276 ret->atime, ret->mtime, (unsigned long long)ret->length,
1277 ret->name, ret->uid, ret->gid, ret->muid, ret->extension,
1278 ret->n_uid, ret->n_gid, ret->n_muid);
1280 p9_free_req(clnt, req);
1285 return ERR_PTR(err);
1287 EXPORT_SYMBOL(p9_client_stat);
1289 static int p9_client_statsize(struct p9_wstat *wst, int proto_version)
1293 /* NOTE: size shouldn't include its own length */
1294 /* size[2] type[2] dev[4] qid[13] */
1295 /* mode[4] atime[4] mtime[4] length[8]*/
1296 /* name[s] uid[s] gid[s] muid[s] */
1297 ret = 2+4+13+4+4+4+8+2+2+2+2;
1300 ret += strlen(wst->name);
1302 ret += strlen(wst->uid);
1304 ret += strlen(wst->gid);
1306 ret += strlen(wst->muid);
1308 if (proto_version == p9_proto_2000u) {
1309 ret += 2+4+4+4; /* extension[s] n_uid[4] n_gid[4] n_muid[4] */
1311 ret += strlen(wst->extension);
1317 int p9_client_wstat(struct p9_fid *fid, struct p9_wstat *wst)
1320 struct p9_req_t *req;
1321 struct p9_client *clnt;
1325 wst->size = p9_client_statsize(wst, clnt->proto_version);
1326 P9_DPRINTK(P9_DEBUG_9P, ">>> TWSTAT fid %d\n", fid->fid);
1327 P9_DPRINTK(P9_DEBUG_9P,
1328 " sz=%x type=%x dev=%x qid=%x.%llx.%x\n"
1329 " mode=%8.8x atime=%8.8x mtime=%8.8x length=%llx\n"
1330 " name=%s uid=%s gid=%s muid=%s extension=(%s)\n"
1331 " uid=%d gid=%d n_muid=%d\n",
1332 wst->size, wst->type, wst->dev, wst->qid.type,
1333 (unsigned long long)wst->qid.path, wst->qid.version, wst->mode,
1334 wst->atime, wst->mtime, (unsigned long long)wst->length,
1335 wst->name, wst->uid, wst->gid, wst->muid, wst->extension,
1336 wst->n_uid, wst->n_gid, wst->n_muid);
1338 req = p9_client_rpc(clnt, P9_TWSTAT, "dwS", fid->fid, wst->size+2, wst);
1344 P9_DPRINTK(P9_DEBUG_9P, "<<< RWSTAT fid %d\n", fid->fid);
1346 p9_free_req(clnt, req);
1350 EXPORT_SYMBOL(p9_client_wstat);