nfsd4: reorganize write decoding
authorJ. Bruce Fields <bfields@redhat.com>
Fri, 16 Nov 2012 15:01:30 +0000 (10:01 -0500)
committerJ. Bruce Fields <bfields@redhat.com>
Mon, 26 Nov 2012 14:08:14 +0000 (09:08 -0500)
In preparation for moving some of it elsewhere.

Signed-off-by: J. Bruce Fields <bfields@redhat.com>
fs/nfsd/nfs4xdr.c

index cfebc9c4f4c99f8be093755b87cc1e142f2400d5..579dc707bad9ef924ea52a709d782812c3761904 100644 (file)
@@ -1139,12 +1139,30 @@ nfsd4_decode_verify(struct nfsd4_compoundargs *argp, struct nfsd4_verify *verify
        DECODE_TAIL;
 }
 
+static int fill_in_write_vector(struct kvec *vec, struct kvec *head, struct page **pagelist, int buflen)
+{
+       int i = 1;
+
+       vec[0].iov_base = head->iov_base;
+       vec[0].iov_len = min_t(int, buflen, head->iov_len);
+       buflen -= vec[0].iov_len;
+
+       while (buflen) {
+               vec[i].iov_base = page_address(pagelist[i - 1]);
+               vec[i].iov_len = min_t(int, PAGE_SIZE, buflen);
+               buflen -= vec[i].iov_len;
+               i++;
+       }
+       return i;
+}
+
 static __be32
 nfsd4_decode_write(struct nfsd4_compoundargs *argp, struct nfsd4_write *write)
 {
        int avail;
-       int v;
        int len;
+       struct page **pagelist;
+       struct kvec head;
        DECODE_HEAD;
 
        status = nfsd4_decode_stateid(argp, &write->wr_stateid);
@@ -1167,27 +1185,29 @@ nfsd4_decode_write(struct nfsd4_compoundargs *argp, struct nfsd4_write *write)
                                __FILE__, __LINE__);
                goto xdr_error;
        }
-       argp->rqstp->rq_vec[0].iov_base = p;
-       argp->rqstp->rq_vec[0].iov_len = avail;
-       v = 0;
-       len = write->wr_buflen;
-       while (len > argp->rqstp->rq_vec[v].iov_len) {
-               len -= argp->rqstp->rq_vec[v].iov_len;
-               v++;
-               argp->rqstp->rq_vec[v].iov_base = page_address(argp->pagelist[0]);
-               argp->pagelist++;
-               if (argp->pagelen >= PAGE_SIZE) {
-                       argp->rqstp->rq_vec[v].iov_len = PAGE_SIZE;
-                       argp->pagelen -= PAGE_SIZE;
-               } else {
-                       argp->rqstp->rq_vec[v].iov_len = argp->pagelen;
-                       argp->pagelen -= len;
-               }
+       head.iov_base = p;
+       head.iov_len = avail;
+       WARN_ON(avail != (XDR_QUADLEN(avail) << 2));
+       pagelist = argp->pagelist;
+
+       len = XDR_QUADLEN(write->wr_buflen) << 2;
+       if (len >= avail) {
+               int pages;
+
+               len -= avail;
+
+               pages = len >> PAGE_SHIFT;
+               argp->pagelist += pages;
+               argp->pagelen -= pages * PAGE_SIZE;
+               len -= pages * PAGE_SIZE;
+
+               argp->p = (__be32 *)page_address(argp->pagelist[0]);
+               argp->end = argp->p + XDR_QUADLEN(PAGE_SIZE);
        }
-       argp->end = (__be32*) (argp->rqstp->rq_vec[v].iov_base + argp->rqstp->rq_vec[v].iov_len);
-       argp->p = (__be32*)  (argp->rqstp->rq_vec[v].iov_base + (XDR_QUADLEN(len) << 2));
-       argp->rqstp->rq_vec[v].iov_len = len;
-       write->wr_vlen = v+1;
+       argp->p += XDR_QUADLEN(len);
+       write->wr_vlen = fill_in_write_vector(argp->rqstp->rq_vec,
+               &head, pagelist, write->wr_buflen);
+       WARN_ON_ONCE(write->wr_vlen > ARRAY_SIZE(argp->rqstp->rq_vec));
 
        DECODE_TAIL;
 }