UBUNTU: Ubuntu-2.6.38-12.51
[linux-flexiantxendom0-natty.git] / crypto / algif_skcipher.c
index 211c956..6a6dfc0 100644 (file)
@@ -52,12 +52,18 @@ struct skcipher_ctx {
 #define MAX_SGL_ENTS ((PAGE_SIZE - sizeof(struct skcipher_sg_list)) / \
                      sizeof(struct scatterlist) - 1)
 
-static inline bool skcipher_writable(struct sock *sk)
+static inline int skcipher_sndbuf(struct sock *sk)
 {
        struct alg_sock *ask = alg_sk(sk);
        struct skcipher_ctx *ctx = ask->private;
 
-       return ctx->used + PAGE_SIZE <= max_t(int, sk->sk_sndbuf, PAGE_SIZE);
+       return max_t(int, max_t(int, sk->sk_sndbuf & PAGE_MASK, PAGE_SIZE) -
+                         ctx->used, 0);
+}
+
+static inline bool skcipher_writable(struct sock *sk)
+{
+       return PAGE_SIZE <= skcipher_sndbuf(sk);
 }
 
 static int skcipher_alloc_sgl(struct sock *sk)
@@ -245,7 +251,6 @@ static int skcipher_sendmsg(struct kiocb *unused, struct socket *sock,
        struct af_alg_control con = {};
        long copied = 0;
        bool enc = 0;
-       int limit;
        int err;
        int i;
 
@@ -281,9 +286,6 @@ static int skcipher_sendmsg(struct kiocb *unused, struct socket *sock,
                        memcpy(ctx->iv, con.iv->iv, ivsize);
        }
 
-       limit = max_t(int, sk->sk_sndbuf, PAGE_SIZE);
-       limit -= ctx->used;
-
        while (size) {
                struct scatterlist *sg;
                unsigned long len = size;
@@ -309,20 +311,16 @@ static int skcipher_sendmsg(struct kiocb *unused, struct socket *sock,
                        ctx->used += len;
                        copied += len;
                        size -= len;
-                       limit -= len;
                        continue;
                }
 
-               if (limit < PAGE_SIZE) {
+               if (!skcipher_writable(sk)) {
                        err = skcipher_wait_for_wmem(sk, msg->msg_flags);
                        if (err)
                                goto unlock;
-
-                       limit = max_t(int, sk->sk_sndbuf, PAGE_SIZE);
-                       limit -= ctx->used;
                }
 
-               len = min_t(unsigned long, len, limit);
+               len = min_t(unsigned long, len, skcipher_sndbuf(sk));
 
                err = skcipher_alloc_sgl(sk);
                if (err)
@@ -352,7 +350,6 @@ static int skcipher_sendmsg(struct kiocb *unused, struct socket *sock,
                        ctx->used += plen;
                        copied += plen;
                        size -= plen;
-                       limit -= plen;
                        sgl->cur++;
                } while (len && sgl->cur < MAX_SGL_ENTS);
 
@@ -380,7 +377,6 @@ static ssize_t skcipher_sendpage(struct socket *sock, struct page *page,
        struct skcipher_ctx *ctx = ask->private;
        struct skcipher_sg_list *sgl;
        int err = -EINVAL;
-       int limit;
 
        lock_sock(sk);
        if (!ctx->more && ctx->used)
@@ -389,16 +385,10 @@ static ssize_t skcipher_sendpage(struct socket *sock, struct page *page,
        if (!size)
                goto done;
 
-       limit = max_t(int, sk->sk_sndbuf, PAGE_SIZE);
-       limit -= ctx->used;
-
-       if (limit < PAGE_SIZE) {
+       if (!skcipher_writable(sk)) {
                err = skcipher_wait_for_wmem(sk, flags);
                if (err)
                        goto unlock;
-
-               limit = max_t(int, sk->sk_sndbuf, PAGE_SIZE);
-               limit -= ctx->used;
        }
 
        err = skcipher_alloc_sgl(sk);
@@ -464,16 +454,17 @@ static int skcipher_recvmsg(struct kiocb *unused, struct socket *sock,
 
                        used = min_t(unsigned long, used, seglen);
 
+                       used = af_alg_make_sg(&ctx->rsgl, from, used, 1);
+                       err = used;
+                       if (err < 0)
+                               goto unlock;
+
                        if (ctx->more || used < ctx->used)
                                used -= used % bs;
 
                        err = -EINVAL;
                        if (!used)
-                               goto unlock;
-
-                       used = af_alg_make_sg(&ctx->rsgl, from, used, 1);
-                       if (used < 0)
-                               goto unlock;
+                               goto free;
 
                        ablkcipher_request_set_crypt(&ctx->req, sg,
                                                     ctx->rsgl.sg, used,
@@ -485,6 +476,7 @@ static int skcipher_recvmsg(struct kiocb *unused, struct socket *sock,
                                        crypto_ablkcipher_decrypt(&ctx->req),
                                &ctx->completion);
 
+free:
                        af_alg_free_sg(&ctx->rsgl);
 
                        if (err)