9p: add 9P2000.L statfs operation
[linux-2.6.git] / net / 9p / client.c
index b56d808..430a1c4 100644 (file)
@@ -29,6 +29,7 @@
 #include <linux/poll.h>
 #include <linux/idr.h>
 #include <linux/mutex.h>
+#include <linux/slab.h>
 #include <linux/sched.h>
 #include <linux/uaccess.h>
 #include <net/9p/9p.h>
@@ -46,6 +47,7 @@ enum {
        Opt_msize,
        Opt_trans,
        Opt_legacy,
+       Opt_version,
        Opt_err,
 };
 
@@ -53,40 +55,75 @@ static const match_table_t tokens = {
        {Opt_msize, "msize=%u"},
        {Opt_legacy, "noextend"},
        {Opt_trans, "trans=%s"},
+       {Opt_version, "version=%s"},
        {Opt_err, NULL},
 };
 
+inline int p9_is_proto_dotl(struct p9_client *clnt)
+{
+       return (clnt->proto_version == p9_proto_2000L);
+}
+EXPORT_SYMBOL(p9_is_proto_dotl);
+
+inline int p9_is_proto_dotu(struct p9_client *clnt)
+{
+       return (clnt->proto_version == p9_proto_2000u);
+}
+EXPORT_SYMBOL(p9_is_proto_dotu);
+
+/* Interpret mount option for protocol version */
+static int get_protocol_version(const substring_t *name)
+{
+       int version = -EINVAL;
+
+       if (!strncmp("9p2000", name->from, name->to-name->from)) {
+               version = p9_proto_legacy;
+               P9_DPRINTK(P9_DEBUG_9P, "Protocol version: Legacy\n");
+       } else if (!strncmp("9p2000.u", name->from, name->to-name->from)) {
+               version = p9_proto_2000u;
+               P9_DPRINTK(P9_DEBUG_9P, "Protocol version: 9P2000.u\n");
+       } else if (!strncmp("9p2000.L", name->from, name->to-name->from)) {
+               version = p9_proto_2000L;
+               P9_DPRINTK(P9_DEBUG_9P, "Protocol version: 9P2000.L\n");
+       } else {
+               P9_DPRINTK(P9_DEBUG_ERROR, "Unknown protocol version %s. ",
+                                                       name->from);
+       }
+       return version;
+}
+
 static struct p9_req_t *
 p9_client_rpc(struct p9_client *c, int8_t type, const char *fmt, ...);
 
 /**
- * v9fs_parse_options - parse mount options into session structure
- * @options: options string passed from mount
- * @v9ses: existing v9fs session information
+ * parse_options - parse mount options into client structure
+ * @opts: options string passed from mount
+ * @clnt: existing v9fs client information
  *
  * Return 0 upon success, -ERRNO upon failure
  */
 
 static int parse_opts(char *opts, struct p9_client *clnt)
 {
-       char *options;
+       char *options, *tmp_options;
        char *p;
        substring_t args[MAX_OPT_ARGS];
        int option;
        int ret = 0;
 
-       clnt->dotu = 1;
+       clnt->proto_version = p9_proto_2000u;
        clnt->msize = 8192;
 
        if (!opts)
                return 0;
 
-       options = kstrdup(opts, GFP_KERNEL);
-       if (!options) {
+       tmp_options = kstrdup(opts, GFP_KERNEL);
+       if (!tmp_options) {
                P9_DPRINTK(P9_DEBUG_ERROR,
                                "failed to allocate copy of option string\n");
                return -ENOMEM;
        }
+       options = tmp_options;
 
        while ((p = strsep(&options, ",")) != NULL) {
                int token;
@@ -108,19 +145,30 @@ static int parse_opts(char *opts, struct p9_client *clnt)
                        break;
                case Opt_trans:
                        clnt->trans_mod = v9fs_get_trans_by_name(&args[0]);
+                       if(clnt->trans_mod == NULL) {
+                               P9_DPRINTK(P9_DEBUG_ERROR,
+                                  "Could not find request transport: %s\n",
+                                  (char *) &args[0]);
+                               ret = -EINVAL;
+                               goto free_and_return;
+                       }
                        break;
                case Opt_legacy:
-                       clnt->dotu = 0;
+                       clnt->proto_version = p9_proto_legacy;
+                       break;
+               case Opt_version:
+                       ret = get_protocol_version(&args[0]);
+                       if (ret == -EINVAL)
+                               goto free_and_return;
+                       clnt->proto_version = ret;
                        break;
                default:
                        continue;
                }
        }
 
-       if (!clnt->trans_mod)
-               clnt->trans_mod = v9fs_get_default_trans();
-
-       kfree(options);
+free_and_return:
+       kfree(tmp_options);
        return ret;
 }
 
@@ -203,7 +251,6 @@ static struct p9_req_t *p9_tag_alloc(struct p9_client *c, u16 tag)
        p9pdu_reset(req->tc);
        p9pdu_reset(req->rc);
 
-       req->flush_tag = 0;
        req->tc->tag = tag-1;
        req->status = REQ_STATUS_ALLOC;
 
@@ -236,7 +283,7 @@ EXPORT_SYMBOL(p9_tag_lookup);
 
 /**
  * p9_tag_init - setup tags structure and contents
- * @tags: tags structure from the client struct
+ * @c:  v9fs client struct
  *
  * This initializes the tags structure for each client instance.
  *
@@ -262,7 +309,7 @@ error:
 
 /**
  * p9_tag_cleanup - cleans up tags structure and reclaims resources
- * @tags: tags structure from the client struct
+ * @c:  v9fs client struct
  *
  * This frees resources associated with the tags structure
  *
@@ -324,35 +371,9 @@ static void p9_free_req(struct p9_client *c, struct p9_req_t *r)
  */
 void p9_client_cb(struct p9_client *c, struct p9_req_t *req)
 {
-       struct p9_req_t *other_req;
-       unsigned long flags;
-
        P9_DPRINTK(P9_DEBUG_MUX, " tag %d\n", req->tc->tag);
-
-       if (req->status == REQ_STATUS_ERROR)
-               wake_up(req->wq);
-
-       if (req->flush_tag) {                   /* flush receive path */
-               P9_DPRINTK(P9_DEBUG_9P, "<<< RFLUSH %d\n", req->tc->tag);
-               spin_lock_irqsave(&c->lock, flags);
-               other_req = p9_tag_lookup(c, req->flush_tag);
-               if (other_req->status != REQ_STATUS_FLSH) /* stale flush */
-                       spin_unlock_irqrestore(&c->lock, flags);
-               else {
-                       other_req->status = REQ_STATUS_FLSHD;
-                       spin_unlock_irqrestore(&c->lock, flags);
-                       wake_up(other_req->wq);
-               }
-               p9_free_req(c, req);
-       } else {                                /* normal receive path */
-               P9_DPRINTK(P9_DEBUG_MUX, "normal: tag %d\n", req->tc->tag);
-               spin_lock_irqsave(&c->lock, flags);
-               if (req->status != REQ_STATUS_FLSHD)
-                       req->status = REQ_STATUS_RCVD;
-               spin_unlock_irqrestore(&c->lock, flags);
-               wake_up(req->wq);
-               P9_DPRINTK(P9_DEBUG_MUX, "wakeup: %d\n", req->tc->tag);
-       }
+       wake_up(req->wq);
+       P9_DPRINTK(P9_DEBUG_MUX, "wakeup: %d\n", req->tc->tag);
 }
 EXPORT_SYMBOL(p9_client_cb);
 
@@ -431,24 +452,20 @@ static int p9_check_errors(struct p9_client *c, struct p9_req_t *req)
                int ecode;
                char *ename;
 
-               err = p9pdu_readf(req->rc, c->dotu, "s?d", &ename, &ecode);
+               err = p9pdu_readf(req->rc, c->proto_version, "s?d",
+                                                       &ename, &ecode);
                if (err) {
                        P9_DPRINTK(P9_DEBUG_ERROR, "couldn't parse error%d\n",
                                                                        err);
                        return err;
                }
 
-               if (c->dotu)
+               if (p9_is_proto_dotu(c))
                        err = -ecode;
 
-               if (!err) {
+               if (!err || !IS_ERR_VALUE(err))
                        err = p9_errstr2errno(ename, strlen(ename));
 
-                       /* string match failed */
-                       if (!err)
-                               err = -ESERVERFAULT;
-               }
-
                P9_DPRINTK(P9_DEBUG_9P, "<<< RERROR (%d) %s\n", -ecode, ename);
 
                kfree(ename);
@@ -460,8 +477,8 @@ static int p9_check_errors(struct p9_client *c, struct p9_req_t *req)
 
 /**
  * p9_client_flush - flush (cancel) a request
- * c: client state
- * req: request to cancel
+ * @c: client state
+ * @oldreq: request to cancel
  *
  * This sents a flush for a particular requests and links
  * the flush request to the original request.  The current
@@ -486,9 +503,15 @@ static int p9_client_flush(struct p9_client *c, struct p9_req_t *oldreq)
        if (IS_ERR(req))
                return PTR_ERR(req);
 
-       req->flush_tag = oldtag;
 
-       /* we don't free anything here because RPC isn't complete */
+       /* if we haven't received a response for oldreq,
+          remove it from the list. */
+       spin_lock(&c->lock);
+       if (oldreq->status == REQ_STATUS_FLSH)
+               list_del(&oldreq->req_list);
+       spin_unlock(&c->lock);
+
+       p9_free_req(c, req);
        return 0;
 }
 
@@ -509,11 +532,15 @@ p9_client_rpc(struct p9_client *c, int8_t type, const char *fmt, ...)
        struct p9_req_t *req;
        unsigned long flags;
        int sigpending;
-       int flushed = 0;
 
        P9_DPRINTK(P9_DEBUG_MUX, "client %p op %d\n", c, type);
 
-       if (c->status != Connected)
+       /* we allow for any status other than disconnected */
+       if (c->status == Disconnected)
+               return ERR_PTR(-EIO);
+
+       /* if status is begin_disconnected we allow only clunk request */
+       if ((c->status == BeginDisconnect) && (type != P9_TCLUNK))
                return ERR_PTR(-EIO);
 
        if (signal_pending(current)) {
@@ -536,7 +563,7 @@ p9_client_rpc(struct p9_client *c, int8_t type, const char *fmt, ...)
        /* marshall the data */
        p9pdu_prepare(req->tc, tag, type);
        va_start(ap, fmt);
-       err = p9pdu_vwritef(req->tc, c->dotu, fmt, ap);
+       err = p9pdu_vwritef(req->tc, c->proto_version, fmt, ap);
        va_end(ap);
        p9pdu_finalize(req->tc);
 
@@ -546,42 +573,28 @@ p9_client_rpc(struct p9_client *c, int8_t type, const char *fmt, ...)
                goto reterr;
        }
 
-       /* if it was a flush we just transmitted, return our tag */
-       if (type == P9_TFLUSH)
-               return req;
-again:
        P9_DPRINTK(P9_DEBUG_MUX, "wait %p tag: %d\n", req->wq, tag);
        err = wait_event_interruptible(*req->wq,
                                                req->status >= REQ_STATUS_RCVD);
-       P9_DPRINTK(P9_DEBUG_MUX, "wait %p tag: %d returned %d (flushed=%d)\n",
-                                               req->wq, tag, err, flushed);
+       P9_DPRINTK(P9_DEBUG_MUX, "wait %p tag: %d returned %d\n",
+                                               req->wq, tag, err);
 
        if (req->status == REQ_STATUS_ERROR) {
                P9_DPRINTK(P9_DEBUG_ERROR, "req_status error %d\n", req->t_err);
                err = req->t_err;
-       } else if (err == -ERESTARTSYS && flushed) {
-               P9_DPRINTK(P9_DEBUG_MUX, "flushed - going again\n");
-               goto again;
-       } else if (req->status == REQ_STATUS_FLSHD) {
-               P9_DPRINTK(P9_DEBUG_MUX, "flushed - erestartsys\n");
-               err = -ERESTARTSYS;
        }
 
-       if ((err == -ERESTARTSYS) && (c->status == Connected) && (!flushed)) {
+       if ((err == -ERESTARTSYS) && (c->status == Connected)) {
                P9_DPRINTK(P9_DEBUG_MUX, "flushing\n");
-               spin_lock_irqsave(&c->lock, flags);
-               if (req->status == REQ_STATUS_SENT)
-                       req->status = REQ_STATUS_FLSH;
-               spin_unlock_irqrestore(&c->lock, flags);
                sigpending = 1;
-               flushed = 1;
                clear_thread_flag(TIF_SIGPENDING);
 
-               if (c->trans_mod->cancel(c, req)) {
-                       err = p9_client_flush(c, req);
-                       if (err == 0)
-                               goto again;
-               }
+               if (c->trans_mod->cancel(c, req))
+                       p9_client_flush(c, req);
+
+               /* if we received the response anyway, don't signal error */
+               if (req->status == REQ_STATUS_RCVD)
+                       err = 0;
        }
 
        if (sigpending) {
@@ -608,7 +621,7 @@ reterr:
 
 static struct p9_fid *p9_fid_create(struct p9_client *clnt)
 {
-       int err;
+       int ret;
        struct p9_fid *fid;
        unsigned long flags;
 
@@ -617,19 +630,18 @@ static struct p9_fid *p9_fid_create(struct p9_client *clnt)
        if (!fid)
                return ERR_PTR(-ENOMEM);
 
-       fid->fid = p9_idpool_get(clnt->fidpool);
-       if (fid->fid < 0) {
-               err = -ENOSPC;
+       ret = p9_idpool_get(clnt->fidpool);
+       if (ret < 0) {
+               ret = -ENOSPC;
                goto error;
        }
+       fid->fid = ret;
 
        memset(&fid->qid, 0, sizeof(struct p9_qid));
        fid->mode = -1;
-       fid->rdir_fpos = 0;
-       fid->uid = current->fsuid;
+       fid->uid = current_fsuid();
        fid->clnt = clnt;
-       fid->aux = NULL;
-
+       fid->rdir = NULL;
        spin_lock_irqsave(&clnt->lock, flags);
        list_add(&fid->flist, &clnt->fidlist);
        spin_unlock_irqrestore(&clnt->lock, flags);
@@ -638,7 +650,7 @@ static struct p9_fid *p9_fid_create(struct p9_client *clnt)
 
 error:
        kfree(fid);
-       return ERR_PTR(err);
+       return ERR_PTR(ret);
 }
 
 static void p9_fid_destroy(struct p9_fid *fid)
@@ -652,6 +664,7 @@ static void p9_fid_destroy(struct p9_fid *fid)
        spin_lock_irqsave(&clnt->lock, flags);
        list_del(&fid->flist);
        spin_unlock_irqrestore(&clnt->lock, flags);
+       kfree(fid->rdir);
        kfree(fid);
 }
 
@@ -662,14 +675,31 @@ int p9_client_version(struct p9_client *c)
        char *version;
        int msize;
 
-       P9_DPRINTK(P9_DEBUG_9P, ">>> TVERSION msize %d extended %d\n",
-                                                       c->msize, c->dotu);
-       req = p9_client_rpc(c, P9_TVERSION, "ds", c->msize,
-                               c->dotu ? "9P2000.u" : "9P2000");
+       P9_DPRINTK(P9_DEBUG_9P, ">>> TVERSION msize %d protocol %d\n",
+                                               c->msize, c->proto_version);
+
+       switch (c->proto_version) {
+       case p9_proto_2000L:
+               req = p9_client_rpc(c, P9_TVERSION, "ds",
+                                       c->msize, "9P2000.L");
+               break;
+       case p9_proto_2000u:
+               req = p9_client_rpc(c, P9_TVERSION, "ds",
+                                       c->msize, "9P2000.u");
+               break;
+       case p9_proto_legacy:
+               req = p9_client_rpc(c, P9_TVERSION, "ds",
+                                       c->msize, "9P2000");
+               break;
+       default:
+               return -EINVAL;
+               break;
+       }
+
        if (IS_ERR(req))
                return PTR_ERR(req);
 
-       err = p9pdu_readf(req->rc, c->dotu, "ds", &msize, &version);
+       err = p9pdu_readf(req->rc, c->proto_version, "ds", &msize, &version);
        if (err) {
                P9_DPRINTK(P9_DEBUG_9P, "version error %d\n", err);
                p9pdu_dump(1, req->rc);
@@ -677,10 +707,12 @@ int p9_client_version(struct p9_client *c)
        }
 
        P9_DPRINTK(P9_DEBUG_9P, "<<< RVERSION msize %d %s\n", msize, version);
-       if (!memcmp(version, "9P2000.u", 8))
-               c->dotu = 1;
-       else if (!memcmp(version, "9P2000", 6))
-               c->dotu = 0;
+       if (!strncmp(version, "9P2000.L", 8))
+               c->proto_version = p9_proto_2000L;
+       else if (!strncmp(version, "9P2000.u", 8))
+               c->proto_version = p9_proto_2000u;
+       else if (!strncmp(version, "9P2000", 6))
+               c->proto_version = p9_proto_legacy;
        else {
                err = -EREMOTEIO;
                goto error;
@@ -711,44 +743,54 @@ struct p9_client *p9_client_create(const char *dev_name, char *options)
        clnt->trans = NULL;
        spin_lock_init(&clnt->lock);
        INIT_LIST_HEAD(&clnt->fidlist);
-       clnt->fidpool = p9_idpool_create();
-       if (IS_ERR(clnt->fidpool)) {
-               err = PTR_ERR(clnt->fidpool);
-               clnt->fidpool = NULL;
-               goto error;
-       }
 
        p9_tag_init(clnt);
 
        err = parse_opts(options, clnt);
        if (err < 0)
-               goto error;
+               goto free_client;
+
+       if (!clnt->trans_mod)
+               clnt->trans_mod = v9fs_get_default_trans();
 
        if (clnt->trans_mod == NULL) {
                err = -EPROTONOSUPPORT;
                P9_DPRINTK(P9_DEBUG_ERROR,
                                "No transport defined or default transport\n");
-               goto error;
+               goto free_client;
+       }
+
+       clnt->fidpool = p9_idpool_create();
+       if (IS_ERR(clnt->fidpool)) {
+               err = PTR_ERR(clnt->fidpool);
+               clnt->fidpool = NULL;
+               goto put_trans;
        }
 
-       P9_DPRINTK(P9_DEBUG_MUX, "clnt %p trans %p msize %d dotu %d\n",
-               clnt, clnt->trans_mod, clnt->msize, clnt->dotu);
+       P9_DPRINTK(P9_DEBUG_MUX, "clnt %p trans %p msize %d protocol %d\n",
+               clnt, clnt->trans_mod, clnt->msize, clnt->proto_version);
 
        err = clnt->trans_mod->create(clnt, dev_name, options);
        if (err)
-               goto error;
+               goto destroy_fidpool;
 
        if ((clnt->msize+P9_IOHDRSZ) > clnt->trans_mod->maxsize)
                clnt->msize = clnt->trans_mod->maxsize-P9_IOHDRSZ;
 
        err = p9_client_version(clnt);
        if (err)
-               goto error;
+               goto close_trans;
 
        return clnt;
 
-error:
-       p9_client_destroy(clnt);
+close_trans:
+       clnt->trans_mod->close(clnt);
+destroy_fidpool:
+       p9_idpool_destroy(clnt->fidpool);
+put_trans:
+       v9fs_put_trans(clnt->trans_mod);
+free_client:
+       kfree(clnt);
        return ERR_PTR(err);
 }
 EXPORT_SYMBOL(p9_client_create);
@@ -764,8 +806,10 @@ void p9_client_destroy(struct p9_client *clnt)
 
        v9fs_put_trans(clnt->trans_mod);
 
-       list_for_each_entry_safe(fid, fidptr, &clnt->fidlist, flist)
+       list_for_each_entry_safe(fid, fidptr, &clnt->fidlist, flist) {
+               printk(KERN_INFO "Found fid %d not clunked\n", fid->fid);
                p9_fid_destroy(fid);
+       }
 
        if (clnt->fidpool)
                p9_idpool_destroy(clnt->fidpool);
@@ -783,6 +827,13 @@ void p9_client_disconnect(struct p9_client *clnt)
 }
 EXPORT_SYMBOL(p9_client_disconnect);
 
+void p9_client_begin_disconnect(struct p9_client *clnt)
+{
+       P9_DPRINTK(P9_DEBUG_9P, "clnt %p\n", clnt);
+       clnt->status = BeginDisconnect;
+}
+EXPORT_SYMBOL(p9_client_begin_disconnect);
+
 struct p9_fid *p9_client_attach(struct p9_client *clnt, struct p9_fid *afid,
        char *uname, u32 n_uname, char *aname)
 {
@@ -809,7 +860,7 @@ struct p9_fid *p9_client_attach(struct p9_client *clnt, struct p9_fid *afid,
                goto error;
        }
 
-       err = p9pdu_readf(req->rc, clnt->dotu, "Q", &qid);
+       err = p9pdu_readf(req->rc, clnt->proto_version, "Q", &qid);
        if (err) {
                p9pdu_dump(1, req->rc);
                p9_free_req(clnt, req);
@@ -817,7 +868,9 @@ struct p9_fid *p9_client_attach(struct p9_client *clnt, struct p9_fid *afid,
        }
 
        P9_DPRINTK(P9_DEBUG_9P, "<<< RATTACH qid %x.%llx.%x\n",
-                                       qid.type, qid.path, qid.version);
+                                       qid.type,
+                                       (unsigned long long)qid.path,
+                                       qid.version);
 
        memmove(&fid->qid, &qid, sizeof(struct p9_qid));
 
@@ -856,7 +909,7 @@ p9_client_auth(struct p9_client *clnt, char *uname, u32 n_uname, char *aname)
                goto error;
        }
 
-       err = p9pdu_readf(req->rc, clnt->dotu, "Q", &qid);
+       err = p9pdu_readf(req->rc, clnt->proto_version, "Q", &qid);
        if (err) {
                p9pdu_dump(1, req->rc);
                p9_free_req(clnt, req);
@@ -864,7 +917,9 @@ p9_client_auth(struct p9_client *clnt, char *uname, u32 n_uname, char *aname)
        }
 
        P9_DPRINTK(P9_DEBUG_9P, "<<< RAUTH qid %x.%llx.%x\n",
-                                       qid.type, qid.path, qid.version);
+                                       qid.type,
+                                       (unsigned long long)qid.path,
+                                       qid.version);
 
        memmove(&afid->qid, &qid, sizeof(struct p9_qid));
        p9_free_req(clnt, req);
@@ -912,7 +967,7 @@ struct p9_fid *p9_client_walk(struct p9_fid *oldfid, int nwname, char **wnames,
                goto error;
        }
 
-       err = p9pdu_readf(req->rc, clnt->dotu, "R", &nwqids, &wqids);
+       err = p9pdu_readf(req->rc, clnt->proto_version, "R", &nwqids, &wqids);
        if (err) {
                p9pdu_dump(1, req->rc);
                p9_free_req(clnt, req);
@@ -929,7 +984,8 @@ struct p9_fid *p9_client_walk(struct p9_fid *oldfid, int nwname, char **wnames,
 
        for (count = 0; count < nwqids; count++)
                P9_DPRINTK(P9_DEBUG_9P, "<<<     [%d] %x.%llx.%x\n",
-                       count, wqids[count].type, wqids[count].path,
+                       count, wqids[count].type,
+                       (unsigned long long)wqids[count].path,
                        wqids[count].version);
 
        if (nwname)
@@ -972,14 +1028,16 @@ int p9_client_open(struct p9_fid *fid, int mode)
                goto error;
        }
 
-       err = p9pdu_readf(req->rc, clnt->dotu, "Qd", &qid, &iounit);
+       err = p9pdu_readf(req->rc, clnt->proto_version, "Qd", &qid, &iounit);
        if (err) {
                p9pdu_dump(1, req->rc);
                goto free_and_error;
        }
 
        P9_DPRINTK(P9_DEBUG_9P, "<<< ROPEN qid %x.%llx.%x iounit %x\n",
-                               qid.type, qid.path, qid.version, iounit);
+                               qid.type,
+                               (unsigned long long)qid.path,
+                               qid.version, iounit);
 
        fid->mode = mode;
        fid->iounit = iounit;
@@ -1015,14 +1073,16 @@ int p9_client_fcreate(struct p9_fid *fid, char *name, u32 perm, int mode,
                goto error;
        }
 
-       err = p9pdu_readf(req->rc, clnt->dotu, "Qd", &qid, &iounit);
+       err = p9pdu_readf(req->rc, clnt->proto_version, "Qd", &qid, &iounit);
        if (err) {
                p9pdu_dump(1, req->rc);
                goto free_and_error;
        }
 
        P9_DPRINTK(P9_DEBUG_9P, "<<< RCREATE qid %x.%llx.%x iounit %x\n",
-                               qid.type, qid.path, qid.version, iounit);
+                               qid.type,
+                               (unsigned long long)qid.path,
+                               qid.version, iounit);
 
        fid->mode = mode;
        fid->iounit = iounit;
@@ -1114,7 +1174,7 @@ p9_client_read(struct p9_fid *fid, char *data, char __user *udata, u64 offset,
                goto error;
        }
 
-       err = p9pdu_readf(req->rc, clnt->dotu, "D", &count, &dataptr);
+       err = p9pdu_readf(req->rc, clnt->proto_version, "D", &count, &dataptr);
        if (err) {
                p9pdu_dump(1, req->rc);
                goto free_and_error;
@@ -1124,7 +1184,6 @@ p9_client_read(struct p9_fid *fid, char *data, char __user *udata, u64 offset,
 
        if (data) {
                memmove(data, dataptr, count);
-               data += count;
        }
 
        if (udata) {
@@ -1176,7 +1235,7 @@ p9_client_write(struct p9_fid *fid, char *data, const char __user *udata,
                goto error;
        }
 
-       err = p9pdu_readf(req->rc, clnt->dotu, "d", &count);
+       err = p9pdu_readf(req->rc, clnt->proto_version, "d", &count);
        if (err) {
                p9pdu_dump(1, req->rc);
                goto free_and_error;
@@ -1216,11 +1275,11 @@ struct p9_wstat *p9_client_stat(struct p9_fid *fid)
                goto error;
        }
 
-       err = p9pdu_readf(req->rc, clnt->dotu, "wS", &ignored, ret);
+       err = p9pdu_readf(req->rc, clnt->proto_version, "wS", &ignored, ret);
        if (err) {
-               ret = ERR_PTR(err);
                p9pdu_dump(1, req->rc);
-               goto free_and_error;
+               p9_free_req(clnt, req);
+               goto error;
        }
 
        P9_DPRINTK(P9_DEBUG_9P,
@@ -1229,24 +1288,58 @@ struct p9_wstat *p9_client_stat(struct p9_fid *fid)
                "<<<    name=%s uid=%s gid=%s muid=%s extension=(%s)\n"
                "<<<    uid=%d gid=%d n_muid=%d\n",
                ret->size, ret->type, ret->dev, ret->qid.type,
-               ret->qid.path, ret->qid.version, ret->mode,
-               ret->atime, ret->mtime, ret->length, ret->name,
-               ret->uid, ret->gid, ret->muid, ret->extension,
+               (unsigned long long)ret->qid.path, ret->qid.version, ret->mode,
+               ret->atime, ret->mtime, (unsigned long long)ret->length,
+               ret->name, ret->uid, ret->gid, ret->muid, ret->extension,
                ret->n_uid, ret->n_gid, ret->n_muid);
 
-free_and_error:
        p9_free_req(clnt, req);
-error:
        return ret;
+
+error:
+       kfree(ret);
+       return ERR_PTR(err);
 }
 EXPORT_SYMBOL(p9_client_stat);
 
+static int p9_client_statsize(struct p9_wstat *wst, int proto_version)
+{
+       int ret;
+
+       /* NOTE: size shouldn't include its own length */
+       /* size[2] type[2] dev[4] qid[13] */
+       /* mode[4] atime[4] mtime[4] length[8]*/
+       /* name[s] uid[s] gid[s] muid[s] */
+       ret = 2+4+13+4+4+4+8+2+2+2+2;
+
+       if (wst->name)
+               ret += strlen(wst->name);
+       if (wst->uid)
+               ret += strlen(wst->uid);
+       if (wst->gid)
+               ret += strlen(wst->gid);
+       if (wst->muid)
+               ret += strlen(wst->muid);
+
+       if ((proto_version == p9_proto_2000u) ||
+               (proto_version == p9_proto_2000L)) {
+               ret += 2+4+4+4; /* extension[s] n_uid[4] n_gid[4] n_muid[4] */
+               if (wst->extension)
+                       ret += strlen(wst->extension);
+       }
+
+       return ret;
+}
+
 int p9_client_wstat(struct p9_fid *fid, struct p9_wstat *wst)
 {
        int err;
        struct p9_req_t *req;
        struct p9_client *clnt;
 
+       err = 0;
+       clnt = fid->clnt;
+       wst->size = p9_client_statsize(wst, clnt->proto_version);
        P9_DPRINTK(P9_DEBUG_9P, ">>> TWSTAT fid %d\n", fid->fid);
        P9_DPRINTK(P9_DEBUG_9P,
                "     sz=%x type=%x dev=%x qid=%x.%llx.%x\n"
@@ -1254,14 +1347,12 @@ int p9_client_wstat(struct p9_fid *fid, struct p9_wstat *wst)
                "     name=%s uid=%s gid=%s muid=%s extension=(%s)\n"
                "     uid=%d gid=%d n_muid=%d\n",
                wst->size, wst->type, wst->dev, wst->qid.type,
-               wst->qid.path, wst->qid.version, wst->mode,
-               wst->atime, wst->mtime, wst->length, wst->name,
-               wst->uid, wst->gid, wst->muid, wst->extension,
+               (unsigned long long)wst->qid.path, wst->qid.version, wst->mode,
+               wst->atime, wst->mtime, (unsigned long long)wst->length,
+               wst->name, wst->uid, wst->gid, wst->muid, wst->extension,
                wst->n_uid, wst->n_gid, wst->n_muid);
-       err = 0;
-       clnt = fid->clnt;
 
-       req = p9_client_rpc(clnt, P9_TWSTAT, "dwS", fid->fid, 0, wst);
+       req = p9_client_rpc(clnt, P9_TWSTAT, "dwS", fid->fid, wst->size+2, wst);
        if (IS_ERR(req)) {
                err = PTR_ERR(req);
                goto error;
@@ -1274,3 +1365,42 @@ error:
        return err;
 }
 EXPORT_SYMBOL(p9_client_wstat);
+
+int p9_client_statfs(struct p9_fid *fid, struct p9_rstatfs *sb)
+{
+       int err;
+       struct p9_req_t *req;
+       struct p9_client *clnt;
+
+       err = 0;
+       clnt = fid->clnt;
+
+       P9_DPRINTK(P9_DEBUG_9P, ">>> TSTATFS fid %d\n", fid->fid);
+
+       req = p9_client_rpc(clnt, P9_TSTATFS, "d", fid->fid);
+       if (IS_ERR(req)) {
+               err = PTR_ERR(req);
+               goto error;
+       }
+
+       err = p9pdu_readf(req->rc, clnt->proto_version, "ddqqqqqqd", &sb->type,
+               &sb->bsize, &sb->blocks, &sb->bfree, &sb->bavail,
+               &sb->files, &sb->ffree, &sb->fsid, &sb->namelen);
+       if (err) {
+               p9pdu_dump(1, req->rc);
+               p9_free_req(clnt, req);
+               goto error;
+       }
+
+       P9_DPRINTK(P9_DEBUG_9P, "<<< RSTATFS fid %d type 0x%lx bsize %ld "
+               "blocks %llu bfree %llu bavail %llu files %llu ffree %llu "
+               "fsid %llu namelen %ld\n",
+               fid->fid, (long unsigned int)sb->type, (long int)sb->bsize,
+               sb->blocks, sb->bfree, sb->bavail, sb->files,  sb->ffree,
+               sb->fsid, (long int)sb->namelen);
+
+       p9_free_req(clnt, req);
+error:
+       return err;
+}
+EXPORT_SYMBOL(p9_client_statfs);