x86, mce: implement panic synchronization
[safe/jmp/linux-2.6] / drivers / md / dm-crypt.c
index 8939cba..53394e8 100644 (file)
@@ -56,9 +56,11 @@ struct dm_crypt_io {
        atomic_t pending;
        int error;
        sector_t sector;
+       struct dm_crypt_io *base_io;
 };
 
 struct dm_crypt_request {
+       struct convert_context *ctx;
        struct scatterlist sg_in;
        struct scatterlist sg_out;
 };
@@ -93,7 +95,6 @@ struct crypt_config {
 
        struct workqueue_struct *io_queue;
        struct workqueue_struct *crypt_queue;
-       wait_queue_head_t writeq;
 
        /*
         * crypto related data
@@ -335,6 +336,18 @@ static void crypt_convert_init(struct crypt_config *cc,
        init_completion(&ctx->restart);
 }
 
+static struct dm_crypt_request *dmreq_of_req(struct crypt_config *cc,
+                                            struct ablkcipher_request *req)
+{
+       return (struct dm_crypt_request *)((char *)req + cc->dmreq_start);
+}
+
+static struct ablkcipher_request *req_of_dmreq(struct crypt_config *cc,
+                                              struct dm_crypt_request *dmreq)
+{
+       return (struct ablkcipher_request *)((char *)dmreq - cc->dmreq_start);
+}
+
 static int crypt_convert_block(struct crypt_config *cc,
                               struct convert_context *ctx,
                               struct ablkcipher_request *req)
@@ -345,10 +358,11 @@ static int crypt_convert_block(struct crypt_config *cc,
        u8 *iv;
        int r = 0;
 
-       dmreq = (struct dm_crypt_request *)((char *)req + cc->dmreq_start);
+       dmreq = dmreq_of_req(cc, req);
        iv = (u8 *)ALIGN((unsigned long)(dmreq + 1),
                         crypto_ablkcipher_alignmask(cc->tfm) + 1);
 
+       dmreq->ctx = ctx;
        sg_init_table(&dmreq->sg_in, 1);
        sg_set_page(&dmreq->sg_in, bv_in->bv_page, 1 << SECTOR_SHIFT,
                    bv_in->bv_offset + ctx->offset_in);
@@ -395,8 +409,9 @@ static void crypt_alloc_req(struct crypt_config *cc,
                cc->req = mempool_alloc(cc->req_pool, GFP_NOIO);
        ablkcipher_request_set_tfm(cc->req, cc->tfm);
        ablkcipher_request_set_callback(cc->req, CRYPTO_TFM_REQ_MAY_BACKLOG |
-                                            CRYPTO_TFM_REQ_MAY_SLEEP,
-                                            kcryptd_async_done, ctx);
+                                       CRYPTO_TFM_REQ_MAY_SLEEP,
+                                       kcryptd_async_done,
+                                       dmreq_of_req(cc, cc->req));
 }
 
 /*
@@ -534,6 +549,7 @@ static struct dm_crypt_io *crypt_io_alloc(struct dm_target *ti,
        io->base_bio = bio;
        io->sector = sector;
        io->error = 0;
+       io->base_io = NULL;
        atomic_set(&io->pending, 0);
 
        return io;
@@ -547,16 +563,27 @@ static void crypt_inc_pending(struct dm_crypt_io *io)
 /*
  * One of the bios was finished. Check for completion of
  * the whole request and correctly clean up the buffer.
+ * If base_io is set, wait for the last fragment to complete.
  */
 static void crypt_dec_pending(struct dm_crypt_io *io)
 {
        struct crypt_config *cc = io->target->private;
+       struct bio *base_bio = io->base_bio;
+       struct dm_crypt_io *base_io = io->base_io;
+       int error = io->error;
 
        if (!atomic_dec_and_test(&io->pending))
                return;
 
-       bio_endio(io->base_bio, io->error);
        mempool_free(io, cc->io_pool);
+
+       if (likely(!base_io))
+               bio_endio(base_bio, error);
+       else {
+               if (error && !base_io->error)
+                       base_io->error = error;
+               crypt_dec_pending(base_io);
+       }
 }
 
 /*
@@ -646,10 +673,7 @@ static void kcryptd_io_read(struct dm_crypt_io *io)
 static void kcryptd_io_write(struct dm_crypt_io *io)
 {
        struct bio *clone = io->ctx.bio_out;
-       struct crypt_config *cc = io->target->private;
-
        generic_make_request(clone);
-       wake_up(&cc->writeq);
 }
 
 static void kcryptd_io(struct work_struct *work)
@@ -688,7 +712,6 @@ static void kcryptd_crypt_write_io_submit(struct dm_crypt_io *io,
        BUG_ON(io->ctx.idx_out < clone->bi_vcnt);
 
        clone->bi_sector = cc->start + io->sector;
-       io->sector += bio_sectors(clone);
 
        if (async)
                kcryptd_queue_io(io);
@@ -700,16 +723,18 @@ static void kcryptd_crypt_write_convert(struct dm_crypt_io *io)
 {
        struct crypt_config *cc = io->target->private;
        struct bio *clone;
+       struct dm_crypt_io *new_io;
        int crypt_finished;
        unsigned out_of_pages = 0;
        unsigned remaining = io->base_bio->bi_size;
+       sector_t sector = io->sector;
        int r;
 
        /*
         * Prevent io from disappearing until this function completes.
         */
        crypt_inc_pending(io);
-       crypt_convert_init(cc, &io->ctx, NULL, io->base_bio, io->sector);
+       crypt_convert_init(cc, &io->ctx, NULL, io->base_bio, sector);
 
        /*
         * The allocated buffers can be smaller than the whole bio,
@@ -726,6 +751,7 @@ static void kcryptd_crypt_write_convert(struct dm_crypt_io *io)
                io->ctx.idx_out = 0;
 
                remaining -= clone->bi_size;
+               sector += bio_sectors(clone);
 
                crypt_inc_pending(io);
                r = crypt_convert(cc, &io->ctx);
@@ -741,6 +767,8 @@ static void kcryptd_crypt_write_convert(struct dm_crypt_io *io)
                         */
                        if (unlikely(r < 0))
                                break;
+
+                       io->sector = sector;
                }
 
                /*
@@ -750,8 +778,33 @@ static void kcryptd_crypt_write_convert(struct dm_crypt_io *io)
                if (unlikely(out_of_pages))
                        congestion_wait(WRITE, HZ/100);
 
-               if (unlikely(remaining))
-                       wait_event(cc->writeq, !atomic_read(&io->ctx.pending));
+               /*
+                * With async crypto it is unsafe to share the crypto context
+                * between fragments, so switch to a new dm_crypt_io structure.
+                */
+               if (unlikely(!crypt_finished && remaining)) {
+                       new_io = crypt_io_alloc(io->target, io->base_bio,
+                                               sector);
+                       crypt_inc_pending(new_io);
+                       crypt_convert_init(cc, &new_io->ctx, NULL,
+                                          io->base_bio, sector);
+                       new_io->ctx.idx_in = io->ctx.idx_in;
+                       new_io->ctx.offset_in = io->ctx.offset_in;
+
+                       /*
+                        * Fragments after the first use the base_io
+                        * pending count.
+                        */
+                       if (!io->base_io)
+                               new_io->base_io = io;
+                       else {
+                               new_io->base_io = io->base_io;
+                               crypt_inc_pending(io->base_io);
+                               crypt_dec_pending(io);
+                       }
+
+                       io = new_io;
+               }
        }
 
        crypt_dec_pending(io);
@@ -786,7 +839,8 @@ static void kcryptd_crypt_read_convert(struct dm_crypt_io *io)
 static void kcryptd_async_done(struct crypto_async_request *async_req,
                               int error)
 {
-       struct convert_context *ctx = async_req->data;
+       struct dm_crypt_request *dmreq = async_req->data;
+       struct convert_context *ctx = dmreq->ctx;
        struct dm_crypt_io *io = container_of(ctx, struct dm_crypt_io, ctx);
        struct crypt_config *cc = io->target->private;
 
@@ -795,7 +849,7 @@ static void kcryptd_async_done(struct crypto_async_request *async_req,
                return;
        }
 
-       mempool_free(ablkcipher_request_cast(async_req), cc->req_pool);
+       mempool_free(req_of_dmreq(cc, dmreq), cc->req_pool);
 
        if (!atomic_dec_and_test(&ctx->pending))
                return;
@@ -1025,7 +1079,7 @@ static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
                goto bad_page_pool;
        }
 
-       cc->bs = bioset_create(MIN_IOS, MIN_IOS);
+       cc->bs = bioset_create(MIN_IOS, 0);
        if (!cc->bs) {
                ti->error = "Cannot allocate crypt bioset";
                goto bad_bs;
@@ -1078,7 +1132,6 @@ static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
                goto bad_crypt_queue;
        }
 
-       init_waitqueue_head(&cc->writeq);
        ti->private = cc;
        return 0;
 
@@ -1103,8 +1156,7 @@ bad_ivmode:
        crypto_free_ablkcipher(tfm);
 bad_cipher:
        /* Must zero key material before freeing */
-       memset(cc, 0, sizeof(*cc) + cc->key_size * sizeof(u8));
-       kfree(cc);
+       kzfree(cc);
        return -EINVAL;
 }
 
@@ -1130,8 +1182,7 @@ static void crypt_dtr(struct dm_target *ti)
        dm_put_device(ti, cc->dev);
 
        /* Must zero key material before freeing */
-       memset(cc, 0, sizeof(*cc) + cc->key_size * sizeof(u8));
-       kfree(cc);
+       kzfree(cc);
 }
 
 static int crypt_map(struct dm_target *ti, struct bio *bio,
@@ -1288,11 +1339,7 @@ static int __init dm_crypt_init(void)
 
 static void __exit dm_crypt_exit(void)
 {
-       int r = dm_unregister_target(&crypt_target);
-
-       if (r < 0)
-               DMERR("unregister failed %d", r);
-
+       dm_unregister_target(&crypt_target);
        kmem_cache_destroy(_crypt_io_pool);
 }