]> err.no Git - linux-2.6/blobdiff - drivers/md/dm-crypt.c
Merge branches 'release' and 'ppc-workaround' into release
[linux-2.6] / drivers / md / dm-crypt.c
index 00725d1863d54a513d70ed9b3cc6091ec6d1ae30..6b66ee46b87d5b44e0023ef3248656b3a9e7ed36 100644 (file)
@@ -113,7 +113,7 @@ static void clone_init(struct dm_crypt_io *, struct bio *);
  * Different IV generation algorithms:
  *
  * plain: the initial vector is the 32-bit little-endian version of the sector
- *        number, padded with zeros if neccessary.
+ *        number, padded with zeros if necessary.
  *
  * essiv: "encrypted sector|salt initial vector", the sector number is
  *        encrypted with the bulk cipher using a salt as key. The salt
@@ -168,7 +168,7 @@ static int crypt_iv_essiv_ctr(struct crypt_config *cc, struct dm_target *ti,
                return -ENOMEM;
        }
 
-       sg_set_buf(&sg, cc->key, cc->key_size);
+       sg_init_one(&sg, cc->key, cc->key_size);
        desc.tfm = hash_tfm;
        desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
        err = crypto_hash_digest(&desc, &sg, cc->key_size, salt);
@@ -348,16 +348,13 @@ static int crypt_convert(struct crypt_config *cc,
              ctx->idx_out < ctx->bio_out->bi_vcnt) {
                struct bio_vec *bv_in = bio_iovec_idx(ctx->bio_in, ctx->idx_in);
                struct bio_vec *bv_out = bio_iovec_idx(ctx->bio_out, ctx->idx_out);
-               struct scatterlist sg_in = {
-                       .page = bv_in->bv_page,
-                       .offset = bv_in->bv_offset + ctx->offset_in,
-                       .length = 1 << SECTOR_SHIFT
-               };
-               struct scatterlist sg_out = {
-                       .page = bv_out->bv_page,
-                       .offset = bv_out->bv_offset + ctx->offset_out,
-                       .length = 1 << SECTOR_SHIFT
-               };
+               struct scatterlist sg_in, sg_out;
+
+               sg_init_table(&sg_in, 1);
+               sg_set_page(&sg_in, bv_in->bv_page, 1 << SECTOR_SHIFT, bv_in->bv_offset + ctx->offset_in);
+
+               sg_init_table(&sg_out, 1);
+               sg_set_page(&sg_out, bv_out->bv_page, 1 << SECTOR_SHIFT, bv_out->bv_offset + ctx->offset_out);
 
                ctx->offset_in += sg_in.length;
                if (ctx->offset_in >= bv_in->bv_len) {
@@ -401,7 +398,8 @@ static struct bio *crypt_alloc_buffer(struct dm_crypt_io *io, unsigned size)
        struct bio *clone;
        unsigned int nr_iovecs = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
        gfp_t gfp_mask = GFP_NOIO | __GFP_HIGHMEM;
-       unsigned int i;
+       unsigned i, len;
+       struct page *page;
 
        clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, cc->bs);
        if (!clone)
@@ -410,10 +408,8 @@ static struct bio *crypt_alloc_buffer(struct dm_crypt_io *io, unsigned size)
        clone_init(io, clone);
 
        for (i = 0; i < nr_iovecs; i++) {
-               struct bio_vec *bv = bio_iovec_idx(clone, i);
-
-               bv->bv_page = mempool_alloc(cc->page_pool, gfp_mask);
-               if (!bv->bv_page)
+               page = mempool_alloc(cc->page_pool, gfp_mask);
+               if (!page)
                        break;
 
                /*
@@ -424,15 +420,14 @@ static struct bio *crypt_alloc_buffer(struct dm_crypt_io *io, unsigned size)
                if (i == (MIN_BIO_PAGES - 1))
                        gfp_mask = (gfp_mask | __GFP_NOWARN) & ~__GFP_WAIT;
 
-               bv->bv_offset = 0;
-               if (size > PAGE_SIZE)
-                       bv->bv_len = PAGE_SIZE;
-               else
-                       bv->bv_len = size;
+               len = (size > PAGE_SIZE) ? PAGE_SIZE : size;
+
+               if (!bio_add_page(clone, page, len, 0)) {
+                       mempool_free(page, cc->page_pool);
+                       break;
+               }
 
-               clone->bi_size += bv->bv_len;
-               clone->bi_vcnt++;
-               size -= bv->bv_len;
+               size -= len;
        }
 
        if (!clone->bi_size) {
@@ -460,7 +455,7 @@ 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 dec_pending(struct dm_crypt_io *io, int error)
+static void crypt_dec_pending(struct dm_crypt_io *io, int error)
 {
        struct crypt_config *cc = (struct crypt_config *) io->target->private;
 
@@ -514,6 +509,9 @@ static void crypt_endio(struct bio *clone, int error)
        struct crypt_config *cc = io->target->private;
        unsigned read_io = bio_data_dir(clone) == READ;
 
+       if (unlikely(!bio_flagged(clone, BIO_UPTODATE) && !error))
+               error = -EIO;
+
        /*
         * free the processed pages
         */
@@ -522,10 +520,8 @@ static void crypt_endio(struct bio *clone, int error)
                goto out;
        }
 
-       if (unlikely(!bio_flagged(clone, BIO_UPTODATE))) {
-               error = -EIO;
+       if (unlikely(error))
                goto out;
-       }
 
        bio_put(clone);
        kcryptd_queue_crypt(io);
@@ -533,7 +529,7 @@ static void crypt_endio(struct bio *clone, int error)
 
 out:
        bio_put(clone);
-       dec_pending(io, error);
+       crypt_dec_pending(io, error);
 }
 
 static void clone_init(struct dm_crypt_io *io, struct bio *clone)
@@ -563,7 +559,7 @@ static void process_read(struct dm_crypt_io *io)
         */
        clone = bio_alloc_bioset(GFP_NOIO, bio_segments(base_bio), cc->bs);
        if (unlikely(!clone)) {
-               dec_pending(io, -ENOMEM);
+               crypt_dec_pending(io, -ENOMEM);
                return;
        }
 
@@ -598,7 +594,7 @@ static void process_write(struct dm_crypt_io *io)
        while (remaining) {
                clone = crypt_alloc_buffer(io, remaining);
                if (unlikely(!clone)) {
-                       dec_pending(io, -ENOMEM);
+                       crypt_dec_pending(io, -ENOMEM);
                        return;
                }
 
@@ -608,7 +604,7 @@ static void process_write(struct dm_crypt_io *io)
                if (unlikely(crypt_convert(cc, &ctx) < 0)) {
                        crypt_free_buffer_pages(cc, clone);
                        bio_put(clone);
-                       dec_pending(io, -EIO);
+                       crypt_dec_pending(io, -EIO);
                        return;
                }
 
@@ -643,7 +639,7 @@ static void process_read_endio(struct dm_crypt_io *io)
        crypt_convert_init(cc, &ctx, io->base_bio, io->base_bio,
                           io->base_bio->bi_sector - io->target->begin, 0);
 
-       dec_pending(io, crypt_convert(cc, &ctx));
+       crypt_dec_pending(io, crypt_convert(cc, &ctx));
 }
 
 static void kcryptd_do_work(struct work_struct *work)
@@ -771,7 +767,7 @@ static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
 
        if (crypt_set_key(cc, argv[1])) {
                ti->error = "Error decoding key";
-               goto bad1;
+               goto bad_cipher;
        }
 
        /* Compatiblity mode for old dm-crypt cipher strings */
@@ -782,19 +778,19 @@ static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
 
        if (strcmp(chainmode, "ecb") && !ivmode) {
                ti->error = "This chaining mode requires an IV mechanism";
-               goto bad1;
+               goto bad_cipher;
        }
 
        if (snprintf(cc->cipher, CRYPTO_MAX_ALG_NAME, "%s(%s)",
                     chainmode, cipher) >= CRYPTO_MAX_ALG_NAME) {
                ti->error = "Chain mode + cipher name is too long";
-               goto bad1;
+               goto bad_cipher;
        }
 
        tfm = crypto_alloc_blkcipher(cc->cipher, 0, CRYPTO_ALG_ASYNC);
        if (IS_ERR(tfm)) {
                ti->error = "Error allocating crypto tfm";
-               goto bad1;
+               goto bad_cipher;
        }
 
        strcpy(cc->cipher, cipher);
@@ -818,12 +814,12 @@ static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
                cc->iv_gen_ops = &crypt_iv_null_ops;
        else {
                ti->error = "Invalid IV mode";
-               goto bad2;
+               goto bad_ivmode;
        }
 
        if (cc->iv_gen_ops && cc->iv_gen_ops->ctr &&
            cc->iv_gen_ops->ctr(cc, ti, ivopts) < 0)
-               goto bad2;
+               goto bad_ivmode;
 
        cc->iv_size = crypto_blkcipher_ivsize(tfm);
        if (cc->iv_size)
@@ -842,13 +838,13 @@ static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
        cc->io_pool = mempool_create_slab_pool(MIN_IOS, _crypt_io_pool);
        if (!cc->io_pool) {
                ti->error = "Cannot allocate crypt io mempool";
-               goto bad3;
+               goto bad_slab_pool;
        }
 
        cc->page_pool = mempool_create_page_pool(MIN_POOL_PAGES, 0);
        if (!cc->page_pool) {
                ti->error = "Cannot allocate page mempool";
-               goto bad4;
+               goto bad_page_pool;
        }
 
        cc->bs = bioset_create(MIN_IOS, MIN_IOS);
@@ -859,25 +855,25 @@ static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
 
        if (crypto_blkcipher_setkey(tfm, cc->key, key_size) < 0) {
                ti->error = "Error setting key";
-               goto bad5;
+               goto bad_device;
        }
 
        if (sscanf(argv[2], "%llu", &tmpll) != 1) {
                ti->error = "Invalid iv_offset sector";
-               goto bad5;
+               goto bad_device;
        }
        cc->iv_offset = tmpll;
 
        if (sscanf(argv[4], "%llu", &tmpll) != 1) {
                ti->error = "Invalid device sector";
-               goto bad5;
+               goto bad_device;
        }
        cc->start = tmpll;
 
        if (dm_get_device(ti, argv[3], cc->start, ti->len,
                          dm_table_get_mode(ti->table), &cc->dev)) {
                ti->error = "Device lookup failed";
-               goto bad5;
+               goto bad_device;
        }
 
        if (ivmode && cc->iv_gen_ops) {
@@ -886,7 +882,7 @@ static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
                cc->iv_mode = kmalloc(strlen(ivmode) + 1, GFP_KERNEL);
                if (!cc->iv_mode) {
                        ti->error = "Error kmallocing iv_mode string";
-                       goto bad_iv_mode;
+                       goto bad_ivmode_string;
                }
                strcpy(cc->iv_mode, ivmode);
        } else
@@ -911,20 +907,20 @@ bad_crypt_queue:
        destroy_workqueue(cc->io_queue);
 bad_io_queue:
        kfree(cc->iv_mode);
-bad_iv_mode:
+bad_ivmode_string:
        dm_put_device(ti, cc->dev);
-bad5:
+bad_device:
        bioset_free(cc->bs);
 bad_bs:
        mempool_destroy(cc->page_pool);
-bad4:
+bad_page_pool:
        mempool_destroy(cc->io_pool);
-bad3:
+bad_slab_pool:
        if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
                cc->iv_gen_ops->dtr(cc);
-bad2:
+bad_ivmode:
        crypto_free_blkcipher(tfm);
-bad1:
+bad_cipher:
        /* Must zero key material before freeing */
        memset(cc, 0, sizeof(*cc) + cc->key_size * sizeof(u8));
        kfree(cc);