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
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 /* Interpret mount option for protocol version */
62 static unsigned char get_protocol_version(const substring_t *name)
64 unsigned char version = -EINVAL;
65 if (!strncmp("9p2000", name->from, name->to-name->from)) {
66 version = p9_proto_legacy;
67 P9_DPRINTK(P9_DEBUG_9P, "Protocol version: Legacy\n");
68 } else if (!strncmp("9p2000.u", name->from, name->to-name->from)) {
69 version = p9_proto_2000u;
70 P9_DPRINTK(P9_DEBUG_9P, "Protocol version: 9P2000.u\n");
71 } else if (!strncmp("9p2010.L", name->from, name->to-name->from)) {
72 version = p9_proto_2010L;
73 P9_DPRINTK(P9_DEBUG_9P, "Protocol version: 9P2010.L\n");
75 P9_DPRINTK(P9_DEBUG_ERROR, "Unknown protocol version %s. ",
81 static struct p9_req_t *
82 p9_client_rpc(struct p9_client *c, int8_t type, const char *fmt, ...);
85 * parse_options - parse mount options into client structure
86 * @opts: options string passed from mount
87 * @clnt: existing v9fs client information
89 * Return 0 upon success, -ERRNO upon failure
92 static int parse_opts(char *opts, struct p9_client *clnt)
94 char *options, *tmp_options;
96 substring_t args[MAX_OPT_ARGS];
106 tmp_options = kstrdup(opts, GFP_KERNEL);
108 P9_DPRINTK(P9_DEBUG_ERROR,
109 "failed to allocate copy of option string\n");
112 options = tmp_options;
114 while ((p = strsep(&options, ",")) != NULL) {
118 token = match_token(p, tokens, args);
119 if (token < Opt_trans) {
120 int r = match_int(&args[0], &option);
122 P9_DPRINTK(P9_DEBUG_ERROR,
123 "integer field, but no integer?\n");
130 clnt->msize = option;
133 clnt->trans_mod = v9fs_get_trans_by_name(&args[0]);
134 if(clnt->trans_mod == NULL) {
135 P9_DPRINTK(P9_DEBUG_ERROR,
136 "Could not find request transport: %s\n",
139 goto free_and_return;
146 ret = get_protocol_version(&args[0]);
148 goto free_and_return;
149 clnt->proto_version = ret;
162 * p9_tag_alloc - lookup/allocate a request by tag
163 * @c: client session to lookup tag within
164 * @tag: numeric id for transaction
166 * this is a simple array lookup, but will grow the
167 * request_slots as necessary to accomodate transaction
168 * ids which did not previously have a slot.
170 * this code relies on the client spinlock to manage locks, its
171 * possible we should switch to something else, but I'd rather
172 * stick with something low-overhead for the common case.
176 static struct p9_req_t *p9_tag_alloc(struct p9_client *c, u16 tag)
180 struct p9_req_t *req;
182 /* This looks up the original request by tag so we know which
183 * buffer to read the data into */
186 if (tag >= c->max_tag) {
187 spin_lock_irqsave(&c->lock, flags);
188 /* check again since original check was outside of lock */
189 while (tag >= c->max_tag) {
190 row = (tag / P9_ROW_MAXTAG);
191 c->reqs[row] = kcalloc(P9_ROW_MAXTAG,
192 sizeof(struct p9_req_t), GFP_ATOMIC);
195 printk(KERN_ERR "Couldn't grow tag array\n");
196 spin_unlock_irqrestore(&c->lock, flags);
197 return ERR_PTR(-ENOMEM);
199 for (col = 0; col < P9_ROW_MAXTAG; col++) {
200 c->reqs[row][col].status = REQ_STATUS_IDLE;
201 c->reqs[row][col].tc = NULL;
203 c->max_tag += P9_ROW_MAXTAG;
205 spin_unlock_irqrestore(&c->lock, flags);
207 row = tag / P9_ROW_MAXTAG;
208 col = tag % P9_ROW_MAXTAG;
210 req = &c->reqs[row][col];
212 req->wq = kmalloc(sizeof(wait_queue_head_t), GFP_KERNEL);
214 printk(KERN_ERR "Couldn't grow tag array\n");
215 return ERR_PTR(-ENOMEM);
217 init_waitqueue_head(req->wq);
218 req->tc = kmalloc(sizeof(struct p9_fcall)+c->msize,
220 req->rc = kmalloc(sizeof(struct p9_fcall)+c->msize,
222 if ((!req->tc) || (!req->rc)) {
223 printk(KERN_ERR "Couldn't grow tag array\n");
227 req->tc = req->rc = NULL;
229 return ERR_PTR(-ENOMEM);
231 req->tc->sdata = (char *) req->tc + sizeof(struct p9_fcall);
232 req->tc->capacity = c->msize;
233 req->rc->sdata = (char *) req->rc + sizeof(struct p9_fcall);
234 req->rc->capacity = c->msize;
237 p9pdu_reset(req->tc);
238 p9pdu_reset(req->rc);
240 req->tc->tag = tag-1;
241 req->status = REQ_STATUS_ALLOC;
243 return &c->reqs[row][col];
247 * p9_tag_lookup - lookup a request by tag
248 * @c: client session to lookup tag within
249 * @tag: numeric id for transaction
253 struct p9_req_t *p9_tag_lookup(struct p9_client *c, u16 tag)
257 /* This looks up the original request by tag so we know which
258 * buffer to read the data into */
261 BUG_ON(tag >= c->max_tag);
263 row = tag / P9_ROW_MAXTAG;
264 col = tag % P9_ROW_MAXTAG;
266 return &c->reqs[row][col];
268 EXPORT_SYMBOL(p9_tag_lookup);
271 * p9_tag_init - setup tags structure and contents
272 * @c: v9fs client struct
274 * This initializes the tags structure for each client instance.
278 static int p9_tag_init(struct p9_client *c)
282 c->tagpool = p9_idpool_create();
283 if (IS_ERR(c->tagpool)) {
284 err = PTR_ERR(c->tagpool);
289 p9_idpool_get(c->tagpool); /* reserve tag 0 */
297 * p9_tag_cleanup - cleans up tags structure and reclaims resources
298 * @c: v9fs client struct
300 * This frees resources associated with the tags structure
303 static void p9_tag_cleanup(struct p9_client *c)
307 /* check to insure all requests are idle */
308 for (row = 0; row < (c->max_tag/P9_ROW_MAXTAG); row++) {
309 for (col = 0; col < P9_ROW_MAXTAG; col++) {
310 if (c->reqs[row][col].status != REQ_STATUS_IDLE) {
311 P9_DPRINTK(P9_DEBUG_MUX,
312 "Attempting to cleanup non-free tag %d,%d\n",
314 /* TODO: delay execution of cleanup */
321 p9_idpool_destroy(c->tagpool);
323 /* free requests associated with tags */
324 for (row = 0; row < (c->max_tag/P9_ROW_MAXTAG); row++) {
325 for (col = 0; col < P9_ROW_MAXTAG; col++) {
326 kfree(c->reqs[row][col].wq);
327 kfree(c->reqs[row][col].tc);
328 kfree(c->reqs[row][col].rc);
336 * p9_free_req - free a request and clean-up as necessary
338 * r: request to release
342 static void p9_free_req(struct p9_client *c, struct p9_req_t *r)
344 int tag = r->tc->tag;
345 P9_DPRINTK(P9_DEBUG_MUX, "clnt %p req %p tag: %d\n", c, r, tag);
347 r->status = REQ_STATUS_IDLE;
348 if (tag != P9_NOTAG && p9_idpool_check(tag, c->tagpool))
349 p9_idpool_put(tag, c->tagpool);
353 * p9_client_cb - call back from transport to client
355 * req: request received
358 void p9_client_cb(struct p9_client *c, struct p9_req_t *req)
360 P9_DPRINTK(P9_DEBUG_MUX, " tag %d\n", req->tc->tag);
362 P9_DPRINTK(P9_DEBUG_MUX, "wakeup: %d\n", req->tc->tag);
364 EXPORT_SYMBOL(p9_client_cb);
367 * p9_parse_header - parse header arguments out of a packet
368 * @pdu: packet to parse
369 * @size: size of packet
370 * @type: type of request
371 * @tag: tag of packet
372 * @rewind: set if we need to rewind offset afterwards
376 p9_parse_header(struct p9_fcall *pdu, int32_t *size, int8_t *type, int16_t *tag,
382 int offset = pdu->offset;
389 err = p9pdu_readf(pdu, 0, "dbw", &r_size, &r_type, &r_tag);
391 goto rewind_and_exit;
397 P9_DPRINTK(P9_DEBUG_9P, "<<< size=%d type: %d tag: %d\n", pdu->size,
410 pdu->offset = offset;
413 EXPORT_SYMBOL(p9_parse_header);
416 * p9_check_errors - check 9p packet for error return and process it
417 * @c: current client instance
418 * @req: request to parse and check for error conditions
420 * returns error code if one is discovered, otherwise returns 0
422 * this will have to be more complicated if we have multiple
426 static int p9_check_errors(struct p9_client *c, struct p9_req_t *req)
431 err = p9_parse_header(req->rc, NULL, &type, NULL, 0);
433 P9_DPRINTK(P9_DEBUG_ERROR, "couldn't parse header %d\n", err);
437 if (type == P9_RERROR) {
441 err = p9pdu_readf(req->rc, c->dotu, "s?d", &ename, &ecode);
443 P9_DPRINTK(P9_DEBUG_ERROR, "couldn't parse error%d\n",
451 if (!err || !IS_ERR_VALUE(err))
452 err = p9_errstr2errno(ename, strlen(ename));
454 P9_DPRINTK(P9_DEBUG_9P, "<<< RERROR (%d) %s\n", -ecode, ename);
464 * p9_client_flush - flush (cancel) a request
466 * @oldreq: request to cancel
468 * This sents a flush for a particular requests and links
469 * the flush request to the original request. The current
470 * code only supports a single flush request although the protocol
471 * allows for multiple flush requests to be sent for a single request.
475 static int p9_client_flush(struct p9_client *c, struct p9_req_t *oldreq)
477 struct p9_req_t *req;
481 err = p9_parse_header(oldreq->tc, NULL, NULL, &oldtag, 1);
485 P9_DPRINTK(P9_DEBUG_9P, ">>> TFLUSH tag %d\n", oldtag);
487 req = p9_client_rpc(c, P9_TFLUSH, "w", oldtag);
492 /* if we haven't received a response for oldreq,
493 remove it from the list. */
495 if (oldreq->status == REQ_STATUS_FLSH)
496 list_del(&oldreq->req_list);
497 spin_unlock(&c->lock);
504 * p9_client_rpc - issue a request and wait for a response
506 * @type: type of request
507 * @fmt: protocol format string (see protocol.c)
509 * Returns request structure (which client must free using p9_free_req)
512 static struct p9_req_t *
513 p9_client_rpc(struct p9_client *c, int8_t type, const char *fmt, ...)
517 struct p9_req_t *req;
521 P9_DPRINTK(P9_DEBUG_MUX, "client %p op %d\n", c, type);
523 if (c->status != Connected)
524 return ERR_PTR(-EIO);
526 if (signal_pending(current)) {
528 clear_thread_flag(TIF_SIGPENDING);
533 if (type != P9_TVERSION) {
534 tag = p9_idpool_get(c->tagpool);
536 return ERR_PTR(-ENOMEM);
539 req = p9_tag_alloc(c, tag);
543 /* marshall the data */
544 p9pdu_prepare(req->tc, tag, type);
546 err = p9pdu_vwritef(req->tc, c->dotu, fmt, ap);
548 p9pdu_finalize(req->tc);
550 err = c->trans_mod->request(c, req);
552 c->status = Disconnected;
556 P9_DPRINTK(P9_DEBUG_MUX, "wait %p tag: %d\n", req->wq, tag);
557 err = wait_event_interruptible(*req->wq,
558 req->status >= REQ_STATUS_RCVD);
559 P9_DPRINTK(P9_DEBUG_MUX, "wait %p tag: %d returned %d\n",
562 if (req->status == REQ_STATUS_ERROR) {
563 P9_DPRINTK(P9_DEBUG_ERROR, "req_status error %d\n", req->t_err);
567 if ((err == -ERESTARTSYS) && (c->status == Connected)) {
568 P9_DPRINTK(P9_DEBUG_MUX, "flushing\n");
570 clear_thread_flag(TIF_SIGPENDING);
572 if (c->trans_mod->cancel(c, req))
573 p9_client_flush(c, req);
575 /* if we received the response anyway, don't signal error */
576 if (req->status == REQ_STATUS_RCVD)
581 spin_lock_irqsave(¤t->sighand->siglock, flags);
583 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
589 err = p9_check_errors(c, req);
591 P9_DPRINTK(P9_DEBUG_MUX, "exit: client %p op %d\n", c, type);
596 P9_DPRINTK(P9_DEBUG_MUX, "exit: client %p op %d error: %d\n", c, type,
602 static struct p9_fid *p9_fid_create(struct p9_client *clnt)
608 P9_DPRINTK(P9_DEBUG_FID, "clnt %p\n", clnt);
609 fid = kmalloc(sizeof(struct p9_fid), GFP_KERNEL);
611 return ERR_PTR(-ENOMEM);
613 ret = p9_idpool_get(clnt->fidpool);
620 memset(&fid->qid, 0, sizeof(struct p9_qid));
622 fid->uid = current_fsuid();
625 spin_lock_irqsave(&clnt->lock, flags);
626 list_add(&fid->flist, &clnt->fidlist);
627 spin_unlock_irqrestore(&clnt->lock, flags);
636 static void p9_fid_destroy(struct p9_fid *fid)
638 struct p9_client *clnt;
641 P9_DPRINTK(P9_DEBUG_FID, "fid %d\n", fid->fid);
643 p9_idpool_put(fid->fid, clnt->fidpool);
644 spin_lock_irqsave(&clnt->lock, flags);
645 list_del(&fid->flist);
646 spin_unlock_irqrestore(&clnt->lock, flags);
651 int p9_client_version(struct p9_client *c)
654 struct p9_req_t *req;
658 P9_DPRINTK(P9_DEBUG_9P, ">>> TVERSION msize %d extended %d\n",
660 req = p9_client_rpc(c, P9_TVERSION, "ds", c->msize,
661 c->dotu ? "9P2000.u" : "9P2000");
665 err = p9pdu_readf(req->rc, c->dotu, "ds", &msize, &version);
667 P9_DPRINTK(P9_DEBUG_9P, "version error %d\n", err);
668 p9pdu_dump(1, req->rc);
672 P9_DPRINTK(P9_DEBUG_9P, "<<< RVERSION msize %d %s\n", msize, version);
673 if (!memcmp(version, "9P2000.u", 8))
675 else if (!memcmp(version, "9P2000", 6))
682 if (msize < c->msize)
691 EXPORT_SYMBOL(p9_client_version);
693 struct p9_client *p9_client_create(const char *dev_name, char *options)
696 struct p9_client *clnt;
699 clnt = kmalloc(sizeof(struct p9_client), GFP_KERNEL);
701 return ERR_PTR(-ENOMEM);
703 clnt->trans_mod = NULL;
705 spin_lock_init(&clnt->lock);
706 INIT_LIST_HEAD(&clnt->fidlist);
710 err = parse_opts(options, clnt);
714 if (!clnt->trans_mod)
715 clnt->trans_mod = v9fs_get_default_trans();
717 if (clnt->trans_mod == NULL) {
718 err = -EPROTONOSUPPORT;
719 P9_DPRINTK(P9_DEBUG_ERROR,
720 "No transport defined or default transport\n");
724 clnt->fidpool = p9_idpool_create();
725 if (IS_ERR(clnt->fidpool)) {
726 err = PTR_ERR(clnt->fidpool);
727 clnt->fidpool = NULL;
731 P9_DPRINTK(P9_DEBUG_MUX, "clnt %p trans %p msize %d dotu %d\n",
732 clnt, clnt->trans_mod, clnt->msize, clnt->dotu);
734 err = clnt->trans_mod->create(clnt, dev_name, options);
736 goto destroy_fidpool;
738 if ((clnt->msize+P9_IOHDRSZ) > clnt->trans_mod->maxsize)
739 clnt->msize = clnt->trans_mod->maxsize-P9_IOHDRSZ;
741 err = p9_client_version(clnt);
748 clnt->trans_mod->close(clnt);
750 p9_idpool_destroy(clnt->fidpool);
752 v9fs_put_trans(clnt->trans_mod);
757 EXPORT_SYMBOL(p9_client_create);
759 void p9_client_destroy(struct p9_client *clnt)
761 struct p9_fid *fid, *fidptr;
763 P9_DPRINTK(P9_DEBUG_MUX, "clnt %p\n", clnt);
766 clnt->trans_mod->close(clnt);
768 v9fs_put_trans(clnt->trans_mod);
770 list_for_each_entry_safe(fid, fidptr, &clnt->fidlist, flist)
774 p9_idpool_destroy(clnt->fidpool);
776 p9_tag_cleanup(clnt);
780 EXPORT_SYMBOL(p9_client_destroy);
782 void p9_client_disconnect(struct p9_client *clnt)
784 P9_DPRINTK(P9_DEBUG_9P, "clnt %p\n", clnt);
785 clnt->status = Disconnected;
787 EXPORT_SYMBOL(p9_client_disconnect);
789 struct p9_fid *p9_client_attach(struct p9_client *clnt, struct p9_fid *afid,
790 char *uname, u32 n_uname, char *aname)
793 struct p9_req_t *req;
797 P9_DPRINTK(P9_DEBUG_9P, ">>> TATTACH afid %d uname %s aname %s\n",
798 afid ? afid->fid : -1, uname, aname);
801 fid = p9_fid_create(clnt);
808 req = p9_client_rpc(clnt, P9_TATTACH, "ddss?d", fid->fid,
809 afid ? afid->fid : P9_NOFID, uname, aname, n_uname);
815 err = p9pdu_readf(req->rc, clnt->dotu, "Q", &qid);
817 p9pdu_dump(1, req->rc);
818 p9_free_req(clnt, req);
822 P9_DPRINTK(P9_DEBUG_9P, "<<< RATTACH qid %x.%llx.%x\n",
824 (unsigned long long)qid.path,
827 memmove(&fid->qid, &qid, sizeof(struct p9_qid));
829 p9_free_req(clnt, req);
837 EXPORT_SYMBOL(p9_client_attach);
840 p9_client_auth(struct p9_client *clnt, char *uname, u32 n_uname, char *aname)
843 struct p9_req_t *req;
847 P9_DPRINTK(P9_DEBUG_9P, ">>> TAUTH uname %s aname %s\n", uname, aname);
850 afid = p9_fid_create(clnt);
857 req = p9_client_rpc(clnt, P9_TAUTH, "dss?d",
858 afid ? afid->fid : P9_NOFID, uname, aname, n_uname);
864 err = p9pdu_readf(req->rc, clnt->dotu, "Q", &qid);
866 p9pdu_dump(1, req->rc);
867 p9_free_req(clnt, req);
871 P9_DPRINTK(P9_DEBUG_9P, "<<< RAUTH qid %x.%llx.%x\n",
873 (unsigned long long)qid.path,
876 memmove(&afid->qid, &qid, sizeof(struct p9_qid));
877 p9_free_req(clnt, req);
882 p9_fid_destroy(afid);
885 EXPORT_SYMBOL(p9_client_auth);
887 struct p9_fid *p9_client_walk(struct p9_fid *oldfid, int nwname, char **wnames,
891 struct p9_client *clnt;
893 struct p9_qid *wqids;
894 struct p9_req_t *req;
895 int16_t nwqids, count;
900 fid = p9_fid_create(clnt);
907 fid->uid = oldfid->uid;
912 P9_DPRINTK(P9_DEBUG_9P, ">>> TWALK fids %d,%d nwname %d wname[0] %s\n",
913 oldfid->fid, fid->fid, nwname, wnames ? wnames[0] : NULL);
915 req = p9_client_rpc(clnt, P9_TWALK, "ddT", oldfid->fid, fid->fid,
922 err = p9pdu_readf(req->rc, clnt->dotu, "R", &nwqids, &wqids);
924 p9pdu_dump(1, req->rc);
925 p9_free_req(clnt, req);
928 p9_free_req(clnt, req);
930 P9_DPRINTK(P9_DEBUG_9P, "<<< RWALK nwqid %d:\n", nwqids);
932 if (nwqids != nwname) {
937 for (count = 0; count < nwqids; count++)
938 P9_DPRINTK(P9_DEBUG_9P, "<<< [%d] %x.%llx.%x\n",
939 count, wqids[count].type,
940 (unsigned long long)wqids[count].path,
941 wqids[count].version);
944 memmove(&fid->qid, &wqids[nwqids - 1], sizeof(struct p9_qid));
946 fid->qid = oldfid->qid;
951 p9_client_clunk(fid);
955 if (fid && (fid != oldfid))
960 EXPORT_SYMBOL(p9_client_walk);
962 int p9_client_open(struct p9_fid *fid, int mode)
965 struct p9_client *clnt;
966 struct p9_req_t *req;
970 P9_DPRINTK(P9_DEBUG_9P, ">>> TOPEN fid %d mode %d\n", fid->fid, mode);
977 req = p9_client_rpc(clnt, P9_TOPEN, "db", fid->fid, mode);
983 err = p9pdu_readf(req->rc, clnt->dotu, "Qd", &qid, &iounit);
985 p9pdu_dump(1, req->rc);
989 P9_DPRINTK(P9_DEBUG_9P, "<<< ROPEN qid %x.%llx.%x iounit %x\n",
991 (unsigned long long)qid.path,
992 qid.version, iounit);
995 fid->iounit = iounit;
998 p9_free_req(clnt, req);
1002 EXPORT_SYMBOL(p9_client_open);
1004 int p9_client_fcreate(struct p9_fid *fid, char *name, u32 perm, int mode,
1008 struct p9_client *clnt;
1009 struct p9_req_t *req;
1013 P9_DPRINTK(P9_DEBUG_9P, ">>> TCREATE fid %d name %s perm %d mode %d\n",
1014 fid->fid, name, perm, mode);
1018 if (fid->mode != -1)
1021 req = p9_client_rpc(clnt, P9_TCREATE, "dsdb?s", fid->fid, name, perm,
1028 err = p9pdu_readf(req->rc, clnt->dotu, "Qd", &qid, &iounit);
1030 p9pdu_dump(1, req->rc);
1031 goto free_and_error;
1034 P9_DPRINTK(P9_DEBUG_9P, "<<< RCREATE qid %x.%llx.%x iounit %x\n",
1036 (unsigned long long)qid.path,
1037 qid.version, iounit);
1040 fid->iounit = iounit;
1043 p9_free_req(clnt, req);
1047 EXPORT_SYMBOL(p9_client_fcreate);
1049 int p9_client_clunk(struct p9_fid *fid)
1052 struct p9_client *clnt;
1053 struct p9_req_t *req;
1055 P9_DPRINTK(P9_DEBUG_9P, ">>> TCLUNK fid %d\n", fid->fid);
1059 req = p9_client_rpc(clnt, P9_TCLUNK, "d", fid->fid);
1065 P9_DPRINTK(P9_DEBUG_9P, "<<< RCLUNK fid %d\n", fid->fid);
1067 p9_free_req(clnt, req);
1068 p9_fid_destroy(fid);
1073 EXPORT_SYMBOL(p9_client_clunk);
1075 int p9_client_remove(struct p9_fid *fid)
1078 struct p9_client *clnt;
1079 struct p9_req_t *req;
1081 P9_DPRINTK(P9_DEBUG_9P, ">>> TREMOVE fid %d\n", fid->fid);
1085 req = p9_client_rpc(clnt, P9_TREMOVE, "d", fid->fid);
1091 P9_DPRINTK(P9_DEBUG_9P, "<<< RREMOVE fid %d\n", fid->fid);
1093 p9_free_req(clnt, req);
1094 p9_fid_destroy(fid);
1099 EXPORT_SYMBOL(p9_client_remove);
1102 p9_client_read(struct p9_fid *fid, char *data, char __user *udata, u64 offset,
1105 int err, rsize, total;
1106 struct p9_client *clnt;
1107 struct p9_req_t *req;
1110 P9_DPRINTK(P9_DEBUG_9P, ">>> TREAD fid %d offset %llu %d\n", fid->fid,
1111 (long long unsigned) offset, count);
1116 rsize = fid->iounit;
1117 if (!rsize || rsize > clnt->msize-P9_IOHDRSZ)
1118 rsize = clnt->msize - P9_IOHDRSZ;
1123 req = p9_client_rpc(clnt, P9_TREAD, "dqd", fid->fid, offset, rsize);
1129 err = p9pdu_readf(req->rc, clnt->dotu, "D", &count, &dataptr);
1131 p9pdu_dump(1, req->rc);
1132 goto free_and_error;
1135 P9_DPRINTK(P9_DEBUG_9P, "<<< RREAD count %d\n", count);
1138 memmove(data, dataptr, count);
1142 err = copy_to_user(udata, dataptr, count);
1145 goto free_and_error;
1149 p9_free_req(clnt, req);
1153 p9_free_req(clnt, req);
1157 EXPORT_SYMBOL(p9_client_read);
1160 p9_client_write(struct p9_fid *fid, char *data, const char __user *udata,
1161 u64 offset, u32 count)
1163 int err, rsize, total;
1164 struct p9_client *clnt;
1165 struct p9_req_t *req;
1167 P9_DPRINTK(P9_DEBUG_9P, ">>> TWRITE fid %d offset %llu count %d\n",
1168 fid->fid, (long long unsigned) offset, count);
1173 rsize = fid->iounit;
1174 if (!rsize || rsize > clnt->msize-P9_IOHDRSZ)
1175 rsize = clnt->msize - P9_IOHDRSZ;
1180 req = p9_client_rpc(clnt, P9_TWRITE, "dqD", fid->fid, offset,
1183 req = p9_client_rpc(clnt, P9_TWRITE, "dqU", fid->fid, offset,
1190 err = p9pdu_readf(req->rc, clnt->dotu, "d", &count);
1192 p9pdu_dump(1, req->rc);
1193 goto free_and_error;
1196 P9_DPRINTK(P9_DEBUG_9P, "<<< RWRITE count %d\n", count);
1198 p9_free_req(clnt, req);
1202 p9_free_req(clnt, req);
1206 EXPORT_SYMBOL(p9_client_write);
1208 struct p9_wstat *p9_client_stat(struct p9_fid *fid)
1211 struct p9_client *clnt;
1212 struct p9_wstat *ret = kmalloc(sizeof(struct p9_wstat), GFP_KERNEL);
1213 struct p9_req_t *req;
1216 P9_DPRINTK(P9_DEBUG_9P, ">>> TSTAT fid %d\n", fid->fid);
1219 return ERR_PTR(-ENOMEM);
1224 req = p9_client_rpc(clnt, P9_TSTAT, "d", fid->fid);
1230 err = p9pdu_readf(req->rc, clnt->dotu, "wS", &ignored, ret);
1232 p9pdu_dump(1, req->rc);
1233 p9_free_req(clnt, req);
1237 P9_DPRINTK(P9_DEBUG_9P,
1238 "<<< RSTAT sz=%x type=%x dev=%x qid=%x.%llx.%x\n"
1239 "<<< mode=%8.8x atime=%8.8x mtime=%8.8x length=%llx\n"
1240 "<<< name=%s uid=%s gid=%s muid=%s extension=(%s)\n"
1241 "<<< uid=%d gid=%d n_muid=%d\n",
1242 ret->size, ret->type, ret->dev, ret->qid.type,
1243 (unsigned long long)ret->qid.path, ret->qid.version, ret->mode,
1244 ret->atime, ret->mtime, (unsigned long long)ret->length,
1245 ret->name, ret->uid, ret->gid, ret->muid, ret->extension,
1246 ret->n_uid, ret->n_gid, ret->n_muid);
1248 p9_free_req(clnt, req);
1253 return ERR_PTR(err);
1255 EXPORT_SYMBOL(p9_client_stat);
1257 static int p9_client_statsize(struct p9_wstat *wst, int optional)
1261 /* NOTE: size shouldn't include its own length */
1262 /* size[2] type[2] dev[4] qid[13] */
1263 /* mode[4] atime[4] mtime[4] length[8]*/
1264 /* name[s] uid[s] gid[s] muid[s] */
1265 ret = 2+4+13+4+4+4+8+2+2+2+2;
1268 ret += strlen(wst->name);
1270 ret += strlen(wst->uid);
1272 ret += strlen(wst->gid);
1274 ret += strlen(wst->muid);
1277 ret += 2+4+4+4; /* extension[s] n_uid[4] n_gid[4] n_muid[4] */
1279 ret += strlen(wst->extension);
1285 int p9_client_wstat(struct p9_fid *fid, struct p9_wstat *wst)
1288 struct p9_req_t *req;
1289 struct p9_client *clnt;
1293 wst->size = p9_client_statsize(wst, clnt->dotu);
1294 P9_DPRINTK(P9_DEBUG_9P, ">>> TWSTAT fid %d\n", fid->fid);
1295 P9_DPRINTK(P9_DEBUG_9P,
1296 " sz=%x type=%x dev=%x qid=%x.%llx.%x\n"
1297 " mode=%8.8x atime=%8.8x mtime=%8.8x length=%llx\n"
1298 " name=%s uid=%s gid=%s muid=%s extension=(%s)\n"
1299 " uid=%d gid=%d n_muid=%d\n",
1300 wst->size, wst->type, wst->dev, wst->qid.type,
1301 (unsigned long long)wst->qid.path, wst->qid.version, wst->mode,
1302 wst->atime, wst->mtime, (unsigned long long)wst->length,
1303 wst->name, wst->uid, wst->gid, wst->muid, wst->extension,
1304 wst->n_uid, wst->n_gid, wst->n_muid);
1306 req = p9_client_rpc(clnt, P9_TWSTAT, "dwS", fid->fid, wst->size+2, wst);
1312 P9_DPRINTK(P9_DEBUG_9P, "<<< RWSTAT fid %d\n", fid->fid);
1314 p9_free_req(clnt, req);
1318 EXPORT_SYMBOL(p9_client_wstat);