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 * v9fs_parse_options - parse mount options into session structure
64 * @options: options string passed from mount
65 * @v9ses: existing v9fs session 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]);
120 if (!clnt->trans_mod)
121 clnt->trans_mod = v9fs_get_default_trans();
128 * p9_tag_alloc - lookup/allocate a request by tag
129 * @c: client session to lookup tag within
130 * @tag: numeric id for transaction
132 * this is a simple array lookup, but will grow the
133 * request_slots as necessary to accomodate transaction
134 * ids which did not previously have a slot.
136 * this code relies on the client spinlock to manage locks, its
137 * possible we should switch to something else, but I'd rather
138 * stick with something low-overhead for the common case.
142 static struct p9_req_t *p9_tag_alloc(struct p9_client *c, u16 tag)
146 struct p9_req_t *req;
148 /* This looks up the original request by tag so we know which
149 * buffer to read the data into */
152 if (tag >= c->max_tag) {
153 spin_lock_irqsave(&c->lock, flags);
154 /* check again since original check was outside of lock */
155 while (tag >= c->max_tag) {
156 row = (tag / P9_ROW_MAXTAG);
157 c->reqs[row] = kcalloc(P9_ROW_MAXTAG,
158 sizeof(struct p9_req_t), GFP_ATOMIC);
161 printk(KERN_ERR "Couldn't grow tag array\n");
162 spin_unlock_irqrestore(&c->lock, flags);
163 return ERR_PTR(-ENOMEM);
165 for (col = 0; col < P9_ROW_MAXTAG; col++) {
166 c->reqs[row][col].status = REQ_STATUS_IDLE;
167 c->reqs[row][col].tc = NULL;
169 c->max_tag += P9_ROW_MAXTAG;
171 spin_unlock_irqrestore(&c->lock, flags);
173 row = tag / P9_ROW_MAXTAG;
174 col = tag % P9_ROW_MAXTAG;
176 req = &c->reqs[row][col];
178 req->wq = kmalloc(sizeof(wait_queue_head_t), GFP_KERNEL);
180 printk(KERN_ERR "Couldn't grow tag array\n");
181 return ERR_PTR(-ENOMEM);
183 init_waitqueue_head(req->wq);
184 req->tc = kmalloc(sizeof(struct p9_fcall)+c->msize,
186 req->rc = kmalloc(sizeof(struct p9_fcall)+c->msize,
188 if ((!req->tc) || (!req->rc)) {
189 printk(KERN_ERR "Couldn't grow tag array\n");
193 req->tc = req->rc = NULL;
195 return ERR_PTR(-ENOMEM);
197 req->tc->sdata = (char *) req->tc + sizeof(struct p9_fcall);
198 req->tc->capacity = c->msize;
199 req->rc->sdata = (char *) req->rc + sizeof(struct p9_fcall);
200 req->rc->capacity = c->msize;
203 p9pdu_reset(req->tc);
204 p9pdu_reset(req->rc);
206 req->tc->tag = tag-1;
207 req->status = REQ_STATUS_ALLOC;
209 return &c->reqs[row][col];
213 * p9_tag_lookup - lookup a request by tag
214 * @c: client session to lookup tag within
215 * @tag: numeric id for transaction
219 struct p9_req_t *p9_tag_lookup(struct p9_client *c, u16 tag)
223 /* This looks up the original request by tag so we know which
224 * buffer to read the data into */
227 BUG_ON(tag >= c->max_tag);
229 row = tag / P9_ROW_MAXTAG;
230 col = tag % P9_ROW_MAXTAG;
232 return &c->reqs[row][col];
234 EXPORT_SYMBOL(p9_tag_lookup);
237 * p9_tag_init - setup tags structure and contents
238 * @tags: tags structure from the client struct
240 * This initializes the tags structure for each client instance.
244 static int p9_tag_init(struct p9_client *c)
248 c->tagpool = p9_idpool_create();
249 if (IS_ERR(c->tagpool)) {
250 err = PTR_ERR(c->tagpool);
255 p9_idpool_get(c->tagpool); /* reserve tag 0 */
263 * p9_tag_cleanup - cleans up tags structure and reclaims resources
264 * @tags: tags structure from the client struct
266 * This frees resources associated with the tags structure
269 static void p9_tag_cleanup(struct p9_client *c)
273 /* check to insure all requests are idle */
274 for (row = 0; row < (c->max_tag/P9_ROW_MAXTAG); row++) {
275 for (col = 0; col < P9_ROW_MAXTAG; col++) {
276 if (c->reqs[row][col].status != REQ_STATUS_IDLE) {
277 P9_DPRINTK(P9_DEBUG_MUX,
278 "Attempting to cleanup non-free tag %d,%d\n",
280 /* TODO: delay execution of cleanup */
287 p9_idpool_destroy(c->tagpool);
289 /* free requests associated with tags */
290 for (row = 0; row < (c->max_tag/P9_ROW_MAXTAG); row++) {
291 for (col = 0; col < P9_ROW_MAXTAG; col++) {
292 kfree(c->reqs[row][col].wq);
293 kfree(c->reqs[row][col].tc);
294 kfree(c->reqs[row][col].rc);
302 * p9_free_req - free a request and clean-up as necessary
304 * r: request to release
308 static void p9_free_req(struct p9_client *c, struct p9_req_t *r)
310 int tag = r->tc->tag;
311 P9_DPRINTK(P9_DEBUG_MUX, "clnt %p req %p tag: %d\n", c, r, tag);
313 r->status = REQ_STATUS_IDLE;
314 if (tag != P9_NOTAG && p9_idpool_check(tag, c->tagpool))
315 p9_idpool_put(tag, c->tagpool);
319 * p9_client_cb - call back from transport to client
321 * req: request received
324 void p9_client_cb(struct p9_client *c, struct p9_req_t *req)
326 P9_DPRINTK(P9_DEBUG_MUX, " tag %d\n", req->tc->tag);
328 P9_DPRINTK(P9_DEBUG_MUX, "wakeup: %d\n", req->tc->tag);
330 EXPORT_SYMBOL(p9_client_cb);
333 * p9_parse_header - parse header arguments out of a packet
334 * @pdu: packet to parse
335 * @size: size of packet
336 * @type: type of request
337 * @tag: tag of packet
338 * @rewind: set if we need to rewind offset afterwards
342 p9_parse_header(struct p9_fcall *pdu, int32_t *size, int8_t *type, int16_t *tag,
348 int offset = pdu->offset;
355 err = p9pdu_readf(pdu, 0, "dbw", &r_size, &r_type, &r_tag);
357 goto rewind_and_exit;
363 P9_DPRINTK(P9_DEBUG_9P, "<<< size=%d type: %d tag: %d\n", pdu->size,
376 pdu->offset = offset;
379 EXPORT_SYMBOL(p9_parse_header);
382 * p9_check_errors - check 9p packet for error return and process it
383 * @c: current client instance
384 * @req: request to parse and check for error conditions
386 * returns error code if one is discovered, otherwise returns 0
388 * this will have to be more complicated if we have multiple
392 static int p9_check_errors(struct p9_client *c, struct p9_req_t *req)
397 err = p9_parse_header(req->rc, NULL, &type, NULL, 0);
399 P9_DPRINTK(P9_DEBUG_ERROR, "couldn't parse header %d\n", err);
403 if (type == P9_RERROR) {
407 err = p9pdu_readf(req->rc, c->dotu, "s?d", &ename, &ecode);
409 P9_DPRINTK(P9_DEBUG_ERROR, "couldn't parse error%d\n",
418 err = p9_errstr2errno(ename, strlen(ename));
420 /* string match failed */
425 P9_DPRINTK(P9_DEBUG_9P, "<<< RERROR (%d) %s\n", -ecode, ename);
435 * p9_client_flush - flush (cancel) a request
437 * req: request to cancel
439 * This sents a flush for a particular requests and links
440 * the flush request to the original request. The current
441 * code only supports a single flush request although the protocol
442 * allows for multiple flush requests to be sent for a single request.
446 static int p9_client_flush(struct p9_client *c, struct p9_req_t *oldreq)
448 struct p9_req_t *req;
452 err = p9_parse_header(oldreq->tc, NULL, NULL, &oldtag, 1);
456 P9_DPRINTK(P9_DEBUG_9P, ">>> TFLUSH tag %d\n", oldtag);
458 req = p9_client_rpc(c, P9_TFLUSH, "w", oldtag);
463 /* if we haven't received a response for oldreq,
464 remove it from the list. */
466 if (oldreq->status == REQ_STATUS_FLSH)
467 list_del(&oldreq->req_list);
468 spin_unlock(&c->lock);
475 * p9_client_rpc - issue a request and wait for a response
477 * @type: type of request
478 * @fmt: protocol format string (see protocol.c)
480 * Returns request structure (which client must free using p9_free_req)
483 static struct p9_req_t *
484 p9_client_rpc(struct p9_client *c, int8_t type, const char *fmt, ...)
488 struct p9_req_t *req;
492 P9_DPRINTK(P9_DEBUG_MUX, "client %p op %d\n", c, type);
494 if (c->status != Connected)
495 return ERR_PTR(-EIO);
497 if (signal_pending(current)) {
499 clear_thread_flag(TIF_SIGPENDING);
504 if (type != P9_TVERSION) {
505 tag = p9_idpool_get(c->tagpool);
507 return ERR_PTR(-ENOMEM);
510 req = p9_tag_alloc(c, tag);
514 /* marshall the data */
515 p9pdu_prepare(req->tc, tag, type);
517 err = p9pdu_vwritef(req->tc, c->dotu, fmt, ap);
519 p9pdu_finalize(req->tc);
521 err = c->trans_mod->request(c, req);
523 c->status = Disconnected;
527 P9_DPRINTK(P9_DEBUG_MUX, "wait %p tag: %d\n", req->wq, tag);
528 err = wait_event_interruptible(*req->wq,
529 req->status >= REQ_STATUS_RCVD);
530 P9_DPRINTK(P9_DEBUG_MUX, "wait %p tag: %d returned %d\n",
533 if (req->status == REQ_STATUS_ERROR) {
534 P9_DPRINTK(P9_DEBUG_ERROR, "req_status error %d\n", req->t_err);
538 if ((err == -ERESTARTSYS) && (c->status == Connected)) {
539 P9_DPRINTK(P9_DEBUG_MUX, "flushing\n");
541 clear_thread_flag(TIF_SIGPENDING);
543 if (c->trans_mod->cancel(c, req))
544 p9_client_flush(c, req);
546 /* if we received the response anyway, don't signal error */
547 if (req->status == REQ_STATUS_RCVD)
552 spin_lock_irqsave(¤t->sighand->siglock, flags);
554 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
560 err = p9_check_errors(c, req);
562 P9_DPRINTK(P9_DEBUG_MUX, "exit: client %p op %d\n", c, type);
567 P9_DPRINTK(P9_DEBUG_MUX, "exit: client %p op %d error: %d\n", c, type,
573 static struct p9_fid *p9_fid_create(struct p9_client *clnt)
579 P9_DPRINTK(P9_DEBUG_FID, "clnt %p\n", clnt);
580 fid = kmalloc(sizeof(struct p9_fid), GFP_KERNEL);
582 return ERR_PTR(-ENOMEM);
584 ret = p9_idpool_get(clnt->fidpool);
591 memset(&fid->qid, 0, sizeof(struct p9_qid));
594 fid->uid = current_fsuid();
598 spin_lock_irqsave(&clnt->lock, flags);
599 list_add(&fid->flist, &clnt->fidlist);
600 spin_unlock_irqrestore(&clnt->lock, flags);
609 static void p9_fid_destroy(struct p9_fid *fid)
611 struct p9_client *clnt;
614 P9_DPRINTK(P9_DEBUG_FID, "fid %d\n", fid->fid);
616 p9_idpool_put(fid->fid, clnt->fidpool);
617 spin_lock_irqsave(&clnt->lock, flags);
618 list_del(&fid->flist);
619 spin_unlock_irqrestore(&clnt->lock, flags);
623 int p9_client_version(struct p9_client *c)
626 struct p9_req_t *req;
630 P9_DPRINTK(P9_DEBUG_9P, ">>> TVERSION msize %d extended %d\n",
632 req = p9_client_rpc(c, P9_TVERSION, "ds", c->msize,
633 c->dotu ? "9P2000.u" : "9P2000");
637 err = p9pdu_readf(req->rc, c->dotu, "ds", &msize, &version);
639 P9_DPRINTK(P9_DEBUG_9P, "version error %d\n", err);
640 p9pdu_dump(1, req->rc);
644 P9_DPRINTK(P9_DEBUG_9P, "<<< RVERSION msize %d %s\n", msize, version);
645 if (!memcmp(version, "9P2000.u", 8))
647 else if (!memcmp(version, "9P2000", 6))
654 if (msize < c->msize)
663 EXPORT_SYMBOL(p9_client_version);
665 struct p9_client *p9_client_create(const char *dev_name, char *options)
668 struct p9_client *clnt;
671 clnt = kmalloc(sizeof(struct p9_client), GFP_KERNEL);
673 return ERR_PTR(-ENOMEM);
675 clnt->trans_mod = NULL;
677 spin_lock_init(&clnt->lock);
678 INIT_LIST_HEAD(&clnt->fidlist);
679 clnt->fidpool = p9_idpool_create();
680 if (IS_ERR(clnt->fidpool)) {
681 err = PTR_ERR(clnt->fidpool);
682 clnt->fidpool = NULL;
688 err = parse_opts(options, clnt);
692 if (clnt->trans_mod == NULL) {
693 err = -EPROTONOSUPPORT;
694 P9_DPRINTK(P9_DEBUG_ERROR,
695 "No transport defined or default transport\n");
699 P9_DPRINTK(P9_DEBUG_MUX, "clnt %p trans %p msize %d dotu %d\n",
700 clnt, clnt->trans_mod, clnt->msize, clnt->dotu);
702 err = clnt->trans_mod->create(clnt, dev_name, options);
706 if ((clnt->msize+P9_IOHDRSZ) > clnt->trans_mod->maxsize)
707 clnt->msize = clnt->trans_mod->maxsize-P9_IOHDRSZ;
709 err = p9_client_version(clnt);
716 p9_client_destroy(clnt);
719 EXPORT_SYMBOL(p9_client_create);
721 void p9_client_destroy(struct p9_client *clnt)
723 struct p9_fid *fid, *fidptr;
725 P9_DPRINTK(P9_DEBUG_MUX, "clnt %p\n", clnt);
728 clnt->trans_mod->close(clnt);
730 v9fs_put_trans(clnt->trans_mod);
732 list_for_each_entry_safe(fid, fidptr, &clnt->fidlist, flist)
736 p9_idpool_destroy(clnt->fidpool);
738 p9_tag_cleanup(clnt);
742 EXPORT_SYMBOL(p9_client_destroy);
744 void p9_client_disconnect(struct p9_client *clnt)
746 P9_DPRINTK(P9_DEBUG_9P, "clnt %p\n", clnt);
747 clnt->status = Disconnected;
749 EXPORT_SYMBOL(p9_client_disconnect);
751 struct p9_fid *p9_client_attach(struct p9_client *clnt, struct p9_fid *afid,
752 char *uname, u32 n_uname, char *aname)
755 struct p9_req_t *req;
759 P9_DPRINTK(P9_DEBUG_9P, ">>> TATTACH afid %d uname %s aname %s\n",
760 afid ? afid->fid : -1, uname, aname);
763 fid = p9_fid_create(clnt);
770 req = p9_client_rpc(clnt, P9_TATTACH, "ddss?d", fid->fid,
771 afid ? afid->fid : P9_NOFID, uname, aname, n_uname);
777 err = p9pdu_readf(req->rc, clnt->dotu, "Q", &qid);
779 p9pdu_dump(1, req->rc);
780 p9_free_req(clnt, req);
784 P9_DPRINTK(P9_DEBUG_9P, "<<< RATTACH qid %x.%llx.%x\n",
786 (unsigned long long)qid.path,
789 memmove(&fid->qid, &qid, sizeof(struct p9_qid));
791 p9_free_req(clnt, req);
799 EXPORT_SYMBOL(p9_client_attach);
802 p9_client_auth(struct p9_client *clnt, char *uname, u32 n_uname, char *aname)
805 struct p9_req_t *req;
809 P9_DPRINTK(P9_DEBUG_9P, ">>> TAUTH uname %s aname %s\n", uname, aname);
812 afid = p9_fid_create(clnt);
819 req = p9_client_rpc(clnt, P9_TAUTH, "dss?d",
820 afid ? afid->fid : P9_NOFID, uname, aname, n_uname);
826 err = p9pdu_readf(req->rc, clnt->dotu, "Q", &qid);
828 p9pdu_dump(1, req->rc);
829 p9_free_req(clnt, req);
833 P9_DPRINTK(P9_DEBUG_9P, "<<< RAUTH qid %x.%llx.%x\n",
835 (unsigned long long)qid.path,
838 memmove(&afid->qid, &qid, sizeof(struct p9_qid));
839 p9_free_req(clnt, req);
844 p9_fid_destroy(afid);
847 EXPORT_SYMBOL(p9_client_auth);
849 struct p9_fid *p9_client_walk(struct p9_fid *oldfid, int nwname, char **wnames,
853 struct p9_client *clnt;
855 struct p9_qid *wqids;
856 struct p9_req_t *req;
857 int16_t nwqids, count;
862 fid = p9_fid_create(clnt);
869 fid->uid = oldfid->uid;
874 P9_DPRINTK(P9_DEBUG_9P, ">>> TWALK fids %d,%d nwname %d wname[0] %s\n",
875 oldfid->fid, fid->fid, nwname, wnames ? wnames[0] : NULL);
877 req = p9_client_rpc(clnt, P9_TWALK, "ddT", oldfid->fid, fid->fid,
884 err = p9pdu_readf(req->rc, clnt->dotu, "R", &nwqids, &wqids);
886 p9pdu_dump(1, req->rc);
887 p9_free_req(clnt, req);
890 p9_free_req(clnt, req);
892 P9_DPRINTK(P9_DEBUG_9P, "<<< RWALK nwqid %d:\n", nwqids);
894 if (nwqids != nwname) {
899 for (count = 0; count < nwqids; count++)
900 P9_DPRINTK(P9_DEBUG_9P, "<<< [%d] %x.%llx.%x\n",
901 count, wqids[count].type,
902 (unsigned long long)wqids[count].path,
903 wqids[count].version);
906 memmove(&fid->qid, &wqids[nwqids - 1], sizeof(struct p9_qid));
908 fid->qid = oldfid->qid;
913 p9_client_clunk(fid);
917 if (fid && (fid != oldfid))
922 EXPORT_SYMBOL(p9_client_walk);
924 int p9_client_open(struct p9_fid *fid, int mode)
927 struct p9_client *clnt;
928 struct p9_req_t *req;
932 P9_DPRINTK(P9_DEBUG_9P, ">>> TOPEN fid %d mode %d\n", fid->fid, mode);
939 req = p9_client_rpc(clnt, P9_TOPEN, "db", fid->fid, mode);
945 err = p9pdu_readf(req->rc, clnt->dotu, "Qd", &qid, &iounit);
947 p9pdu_dump(1, req->rc);
951 P9_DPRINTK(P9_DEBUG_9P, "<<< ROPEN qid %x.%llx.%x iounit %x\n",
953 (unsigned long long)qid.path,
954 qid.version, iounit);
957 fid->iounit = iounit;
960 p9_free_req(clnt, req);
964 EXPORT_SYMBOL(p9_client_open);
966 int p9_client_fcreate(struct p9_fid *fid, char *name, u32 perm, int mode,
970 struct p9_client *clnt;
971 struct p9_req_t *req;
975 P9_DPRINTK(P9_DEBUG_9P, ">>> TCREATE fid %d name %s perm %d mode %d\n",
976 fid->fid, name, perm, mode);
983 req = p9_client_rpc(clnt, P9_TCREATE, "dsdb?s", fid->fid, name, perm,
990 err = p9pdu_readf(req->rc, clnt->dotu, "Qd", &qid, &iounit);
992 p9pdu_dump(1, req->rc);
996 P9_DPRINTK(P9_DEBUG_9P, "<<< RCREATE qid %x.%llx.%x iounit %x\n",
998 (unsigned long long)qid.path,
999 qid.version, iounit);
1002 fid->iounit = iounit;
1005 p9_free_req(clnt, req);
1009 EXPORT_SYMBOL(p9_client_fcreate);
1011 int p9_client_clunk(struct p9_fid *fid)
1014 struct p9_client *clnt;
1015 struct p9_req_t *req;
1017 P9_DPRINTK(P9_DEBUG_9P, ">>> TCLUNK fid %d\n", fid->fid);
1021 req = p9_client_rpc(clnt, P9_TCLUNK, "d", fid->fid);
1027 P9_DPRINTK(P9_DEBUG_9P, "<<< RCLUNK fid %d\n", fid->fid);
1029 p9_free_req(clnt, req);
1030 p9_fid_destroy(fid);
1035 EXPORT_SYMBOL(p9_client_clunk);
1037 int p9_client_remove(struct p9_fid *fid)
1040 struct p9_client *clnt;
1041 struct p9_req_t *req;
1043 P9_DPRINTK(P9_DEBUG_9P, ">>> TREMOVE fid %d\n", fid->fid);
1047 req = p9_client_rpc(clnt, P9_TREMOVE, "d", fid->fid);
1053 P9_DPRINTK(P9_DEBUG_9P, "<<< RREMOVE fid %d\n", fid->fid);
1055 p9_free_req(clnt, req);
1056 p9_fid_destroy(fid);
1061 EXPORT_SYMBOL(p9_client_remove);
1064 p9_client_read(struct p9_fid *fid, char *data, char __user *udata, u64 offset,
1067 int err, rsize, total;
1068 struct p9_client *clnt;
1069 struct p9_req_t *req;
1072 P9_DPRINTK(P9_DEBUG_9P, ">>> TREAD fid %d offset %llu %d\n", fid->fid,
1073 (long long unsigned) offset, count);
1078 rsize = fid->iounit;
1079 if (!rsize || rsize > clnt->msize-P9_IOHDRSZ)
1080 rsize = clnt->msize - P9_IOHDRSZ;
1085 req = p9_client_rpc(clnt, P9_TREAD, "dqd", fid->fid, offset, rsize);
1091 err = p9pdu_readf(req->rc, clnt->dotu, "D", &count, &dataptr);
1093 p9pdu_dump(1, req->rc);
1094 goto free_and_error;
1097 P9_DPRINTK(P9_DEBUG_9P, "<<< RREAD count %d\n", count);
1100 memmove(data, dataptr, count);
1105 err = copy_to_user(udata, dataptr, count);
1108 goto free_and_error;
1112 p9_free_req(clnt, req);
1116 p9_free_req(clnt, req);
1120 EXPORT_SYMBOL(p9_client_read);
1123 p9_client_write(struct p9_fid *fid, char *data, const char __user *udata,
1124 u64 offset, u32 count)
1126 int err, rsize, total;
1127 struct p9_client *clnt;
1128 struct p9_req_t *req;
1130 P9_DPRINTK(P9_DEBUG_9P, ">>> TWRITE fid %d offset %llu count %d\n",
1131 fid->fid, (long long unsigned) offset, count);
1136 rsize = fid->iounit;
1137 if (!rsize || rsize > clnt->msize-P9_IOHDRSZ)
1138 rsize = clnt->msize - P9_IOHDRSZ;
1143 req = p9_client_rpc(clnt, P9_TWRITE, "dqD", fid->fid, offset,
1146 req = p9_client_rpc(clnt, P9_TWRITE, "dqU", fid->fid, offset,
1153 err = p9pdu_readf(req->rc, clnt->dotu, "d", &count);
1155 p9pdu_dump(1, req->rc);
1156 goto free_and_error;
1159 P9_DPRINTK(P9_DEBUG_9P, "<<< RWRITE count %d\n", count);
1161 p9_free_req(clnt, req);
1165 p9_free_req(clnt, req);
1169 EXPORT_SYMBOL(p9_client_write);
1171 struct p9_wstat *p9_client_stat(struct p9_fid *fid)
1174 struct p9_client *clnt;
1175 struct p9_wstat *ret = kmalloc(sizeof(struct p9_wstat), GFP_KERNEL);
1176 struct p9_req_t *req;
1179 P9_DPRINTK(P9_DEBUG_9P, ">>> TSTAT fid %d\n", fid->fid);
1182 return ERR_PTR(-ENOMEM);
1187 req = p9_client_rpc(clnt, P9_TSTAT, "d", fid->fid);
1193 err = p9pdu_readf(req->rc, clnt->dotu, "wS", &ignored, ret);
1196 p9pdu_dump(1, req->rc);
1197 goto free_and_error;
1200 P9_DPRINTK(P9_DEBUG_9P,
1201 "<<< RSTAT sz=%x type=%x dev=%x qid=%x.%llx.%x\n"
1202 "<<< mode=%8.8x atime=%8.8x mtime=%8.8x length=%llx\n"
1203 "<<< name=%s uid=%s gid=%s muid=%s extension=(%s)\n"
1204 "<<< uid=%d gid=%d n_muid=%d\n",
1205 ret->size, ret->type, ret->dev, ret->qid.type,
1206 (unsigned long long)ret->qid.path, ret->qid.version, ret->mode,
1207 ret->atime, ret->mtime, (unsigned long long)ret->length,
1208 ret->name, ret->uid, ret->gid, ret->muid, ret->extension,
1209 ret->n_uid, ret->n_gid, ret->n_muid);
1211 p9_free_req(clnt, req);
1215 p9_free_req(clnt, req);
1218 return ERR_PTR(err);
1220 EXPORT_SYMBOL(p9_client_stat);
1222 static int p9_client_statsize(struct p9_wstat *wst, int optional)
1226 /* size[2] type[2] dev[4] qid[13] */
1227 /* mode[4] atime[4] mtime[4] length[8]*/
1228 /* name[s] uid[s] gid[s] muid[s] */
1229 ret = 2+2+4+13+4+4+4+8+2+2+2+2;
1232 ret += strlen(wst->name);
1234 ret += strlen(wst->uid);
1236 ret += strlen(wst->gid);
1238 ret += strlen(wst->muid);
1241 ret += 2+4+4+4; /* extension[s] n_uid[4] n_gid[4] n_muid[4] */
1243 ret += strlen(wst->extension);
1249 int p9_client_wstat(struct p9_fid *fid, struct p9_wstat *wst)
1252 struct p9_req_t *req;
1253 struct p9_client *clnt;
1257 wst->size = p9_client_statsize(wst, clnt->dotu);
1258 P9_DPRINTK(P9_DEBUG_9P, ">>> TWSTAT fid %d\n", fid->fid);
1259 P9_DPRINTK(P9_DEBUG_9P,
1260 " sz=%x type=%x dev=%x qid=%x.%llx.%x\n"
1261 " mode=%8.8x atime=%8.8x mtime=%8.8x length=%llx\n"
1262 " name=%s uid=%s gid=%s muid=%s extension=(%s)\n"
1263 " uid=%d gid=%d n_muid=%d\n",
1264 wst->size, wst->type, wst->dev, wst->qid.type,
1265 (unsigned long long)wst->qid.path, wst->qid.version, wst->mode,
1266 wst->atime, wst->mtime, (unsigned long long)wst->length,
1267 wst->name, wst->uid, wst->gid, wst->muid, wst->extension,
1268 wst->n_uid, wst->n_gid, wst->n_muid);
1270 req = p9_client_rpc(clnt, P9_TWSTAT, "dwS", fid->fid, wst->size, wst);
1276 P9_DPRINTK(P9_DEBUG_9P, "<<< RWSTAT fid %d\n", fid->fid);
1278 p9_free_req(clnt, req);
1282 EXPORT_SYMBOL(p9_client_wstat);