vserver 2.0 rc7
[linux-2.6.git] / crypto / scatterwalk.c
index f6a5c9e..50c9461 100644 (file)
@@ -17,6 +17,7 @@
 #include <linux/mm.h>
 #include <linux/pagemap.h>
 #include <linux/highmem.h>
+#include <asm/bug.h>
 #include <asm/scatterlist.h>
 #include "internal.h"
 #include "scatterwalk.h"
@@ -28,16 +29,6 @@ enum km_type crypto_km_types[] = {
        KM_SOFTIRQ1,
 };
 
-void *scatterwalk_whichbuf(struct scatter_walk *walk, unsigned int nbytes, void *scratch)
-{
-       if (nbytes <= walk->len_this_page &&
-           (((unsigned long)walk->data) & (PAGE_CACHE_SIZE - 1)) + nbytes <=
-           PAGE_CACHE_SIZE)
-               return walk->data;
-       else
-               return scratch;
-}
-
 static void memcpy_dir(void *buf, void *sgdata, size_t nbytes, int out)
 {
        if (out)
@@ -55,6 +46,8 @@ void scatterwalk_start(struct scatter_walk *walk, struct scatterlist *sg)
        walk->page = sg->page;
        walk->len_this_segment = sg->length;
 
+       BUG_ON(!sg->length);
+
        rest_of_page = PAGE_CACHE_SIZE - (sg->offset & (PAGE_CACHE_SIZE - 1));
        walk->len_this_page = min(sg->length, rest_of_page);
        walk->offset = sg->offset;
@@ -65,13 +58,17 @@ void scatterwalk_map(struct scatter_walk *walk, int out)
        walk->data = crypto_kmap(walk->page, out) + walk->offset;
 }
 
-static void scatterwalk_pagedone(struct scatter_walk *walk, int out,
-                                unsigned int more)
+static inline void scatterwalk_unmap(struct scatter_walk *walk, int out)
 {
        /* walk->data may be pointing the first byte of the next page;
           however, we know we transfered at least one byte.  So,
           walk->data - 1 will be a virtual address in the mapped page. */
+       crypto_kunmap(walk->data - 1, out);
+}
 
+static void scatterwalk_pagedone(struct scatter_walk *walk, int out,
+                                unsigned int more)
+{
        if (out)
                flush_dcache_page(walk->page);
 
@@ -91,7 +88,7 @@ static void scatterwalk_pagedone(struct scatter_walk *walk, int out,
 
 void scatterwalk_done(struct scatter_walk *walk, int out, int more)
 {
-       crypto_kunmap(walk->data, out);
+       scatterwalk_unmap(walk, out);
        if (walk->len_this_page == 0 || !more)
                scatterwalk_pagedone(walk, out, more);
 }
@@ -103,22 +100,16 @@ void scatterwalk_done(struct scatter_walk *walk, int out, int more)
 int scatterwalk_copychunks(void *buf, struct scatter_walk *walk,
                           size_t nbytes, int out)
 {
-       if (buf != walk->data) {
-               while (nbytes > walk->len_this_page) {
-                       memcpy_dir(buf, walk->data, walk->len_this_page, out);
-                       buf += walk->len_this_page;
-                       nbytes -= walk->len_this_page;
-
-                       crypto_kunmap(walk->data, out);
-                       scatterwalk_pagedone(walk, out, 1);
-                       scatterwalk_map(walk, out);
-               }
-
-               memcpy_dir(buf, walk->data, nbytes, out);
-       }
-
-       walk->offset += nbytes;
-       walk->len_this_page -= nbytes;
-       walk->len_this_segment -= nbytes;
-       return 0;
+       do {
+               memcpy_dir(buf, walk->data, walk->len_this_page, out);
+               buf += walk->len_this_page;
+               nbytes -= walk->len_this_page;
+
+               scatterwalk_unmap(walk, out);
+               scatterwalk_pagedone(walk, out, 1);
+               scatterwalk_map(walk, out);
+       } while (nbytes > walk->len_this_page);
+
+       memcpy_dir(buf, walk->data, nbytes, out);
+       return nbytes;
 }