]> err.no Git - linux-2.6/blobdiff - drivers/md/dm-crypt.c
dm crypt: tidy crypt_endio
[linux-2.6] / drivers / md / dm-crypt.c
index 6b66ee46b87d5b44e0023ef3248656b3a9e7ed36..278659975d729a95d0ccc529eb6da02d61236f79 100644 (file)
 #define DM_MSG_PREFIX "crypt"
 #define MESG_STR(x) x, sizeof(x)
 
-/*
- * per bio private data
- */
-struct dm_crypt_io {
-       struct dm_target *target;
-       struct bio *base_bio;
-       struct work_struct work;
-       atomic_t pending;
-       int error;
-};
-
 /*
  * context holding the current state of a multi-part conversion
  */
@@ -49,7 +38,20 @@ struct convert_context {
        unsigned int idx_in;
        unsigned int idx_out;
        sector_t sector;
-       int write;
+};
+
+/*
+ * per bio private data
+ */
+struct dm_crypt_io {
+       struct dm_target *target;
+       struct bio *base_bio;
+       struct work_struct work;
+
+       struct convert_context ctx;
+
+       atomic_t pending;
+       int error;
 };
 
 struct crypt_config;
@@ -324,7 +326,7 @@ crypt_convert_scatterlist(struct crypt_config *cc, struct scatterlist *out,
 static void crypt_convert_init(struct crypt_config *cc,
                               struct convert_context *ctx,
                               struct bio *bio_out, struct bio *bio_in,
-                              sector_t sector, int write)
+                              sector_t sector)
 {
        ctx->bio_in = bio_in;
        ctx->bio_out = bio_out;
@@ -333,7 +335,6 @@ static void crypt_convert_init(struct crypt_config *cc,
        ctx->idx_in = bio_in ? bio_in->bi_idx : 0;
        ctx->idx_out = bio_out ? bio_out->bi_idx : 0;
        ctx->sector = sector + cc->iv_offset;
-       ctx->write = write;
 }
 
 /*
@@ -369,7 +370,7 @@ static int crypt_convert(struct crypt_config *cc,
                }
 
                r = crypt_convert_scatterlist(cc, &sg_out, &sg_in, sg_in.length,
-                                             ctx->write, ctx->sector);
+                       bio_data_dir(ctx->bio_in) == WRITE, ctx->sector);
                if (r < 0)
                        break;
 
@@ -455,18 +456,14 @@ static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone)
  * One of the bios was finished. Check for completion of
  * the whole request and correctly clean up the buffer.
  */
-static void crypt_dec_pending(struct dm_crypt_io *io, int error)
+static void crypt_dec_pending(struct dm_crypt_io *io)
 {
-       struct crypt_config *cc = (struct crypt_config *) io->target->private;
-
-       if (error < 0)
-               io->error = error;
+       struct crypt_config *cc = io->target->private;
 
        if (!atomic_dec_and_test(&io->pending))
                return;
 
        bio_endio(io->base_bio, io->error);
-
        mempool_free(io, cc->io_pool);
 }
 
@@ -507,7 +504,7 @@ static void crypt_endio(struct bio *clone, int error)
 {
        struct dm_crypt_io *io = clone->bi_private;
        struct crypt_config *cc = io->target->private;
-       unsigned read_io = bio_data_dir(clone) == READ;
+       unsigned rw = bio_data_dir(clone);
 
        if (unlikely(!bio_flagged(clone, BIO_UPTODATE) && !error))
                error = -EIO;
@@ -515,21 +512,20 @@ static void crypt_endio(struct bio *clone, int error)
        /*
         * free the processed pages
         */
-       if (!read_io) {
+       if (rw == WRITE)
                crypt_free_buffer_pages(cc, clone);
-               goto out;
+
+       bio_put(clone);
+
+       if (rw == READ && !error) {
+               kcryptd_queue_crypt(io);
+               return;
        }
 
        if (unlikely(error))
-               goto out;
-
-       bio_put(clone);
-       kcryptd_queue_crypt(io);
-       return;
+               io->error = error;
 
-out:
-       bio_put(clone);
-       crypt_dec_pending(io, error);
+       crypt_dec_pending(io);
 }
 
 static void clone_init(struct dm_crypt_io *io, struct bio *clone)
@@ -559,7 +555,8 @@ static void process_read(struct dm_crypt_io *io)
         */
        clone = bio_alloc_bioset(GFP_NOIO, bio_segments(base_bio), cc->bs);
        if (unlikely(!clone)) {
-               crypt_dec_pending(io, -ENOMEM);
+               io->error = -ENOMEM;
+               crypt_dec_pending(io);
                return;
        }
 
@@ -579,13 +576,12 @@ static void process_write(struct dm_crypt_io *io)
        struct crypt_config *cc = io->target->private;
        struct bio *base_bio = io->base_bio;
        struct bio *clone;
-       struct convert_context ctx;
        unsigned remaining = base_bio->bi_size;
        sector_t sector = base_bio->bi_sector - io->target->begin;
 
        atomic_inc(&io->pending);
 
-       crypt_convert_init(cc, &ctx, NULL, base_bio, sector, 1);
+       crypt_convert_init(cc, &io->ctx, NULL, base_bio, sector);
 
        /*
         * The allocated buffers can be smaller than the whole bio,
@@ -594,22 +590,24 @@ static void process_write(struct dm_crypt_io *io)
        while (remaining) {
                clone = crypt_alloc_buffer(io, remaining);
                if (unlikely(!clone)) {
-                       crypt_dec_pending(io, -ENOMEM);
+                       io->error = -ENOMEM;
+                       crypt_dec_pending(io);
                        return;
                }
 
-               ctx.bio_out = clone;
-               ctx.idx_out = 0;
+               io->ctx.bio_out = clone;
+               io->ctx.idx_out = 0;
 
-               if (unlikely(crypt_convert(cc, &ctx) < 0)) {
+               if (unlikely(crypt_convert(cc, &io->ctx) < 0)) {
                        crypt_free_buffer_pages(cc, clone);
                        bio_put(clone);
-                       crypt_dec_pending(io, -EIO);
+                       io->error = -EIO;
+                       crypt_dec_pending(io);
                        return;
                }
 
                /* crypt_convert should have filled the clone bio */
-               BUG_ON(ctx.idx_out < clone->bi_vcnt);
+               BUG_ON(io->ctx.idx_out < clone->bi_vcnt);
 
                clone->bi_sector = cc->start + sector;
                remaining -= clone->bi_size;
@@ -631,15 +629,25 @@ static void process_write(struct dm_crypt_io *io)
        }
 }
 
+static void crypt_read_done(struct dm_crypt_io *io, int error)
+{
+       if (unlikely(error < 0))
+               io->error = -EIO;
+
+       crypt_dec_pending(io);
+}
+
 static void process_read_endio(struct dm_crypt_io *io)
 {
        struct crypt_config *cc = io->target->private;
-       struct convert_context ctx;
+       int r = 0;
+
+       crypt_convert_init(cc, &io->ctx, io->base_bio, io->base_bio,
+                          io->base_bio->bi_sector - io->target->begin);
 
-       crypt_convert_init(cc, &ctx, io->base_bio, io->base_bio,
-                          io->base_bio->bi_sector - io->target->begin, 0);
+       r = crypt_convert(cc, &io->ctx);
 
-       crypt_dec_pending(io, crypt_convert(cc, &ctx));
+       crypt_read_done(io, r);
 }
 
 static void kcryptd_do_work(struct work_struct *work)