Linux 3.4.102
[linux/fpc-iii.git] / drivers / md / dm-verity.c
blobac099847a4b32dcff3990d1ddcc39cd84839e507
1 /*
2 * Copyright (C) 2012 Red Hat, Inc.
4 * Author: Mikulas Patocka <mpatocka@redhat.com>
6 * Based on Chromium dm-verity driver (C) 2011 The Chromium OS Authors
8 * This file is released under the GPLv2.
10 * In the file "/sys/module/dm_verity/parameters/prefetch_cluster" you can set
11 * default prefetch value. Data are read in "prefetch_cluster" chunks from the
12 * hash device. Setting this greatly improves performance when data and hash
13 * are on the same disk on different partitions on devices with poor random
14 * access behavior.
17 #include "dm-bufio.h"
19 #include <linux/module.h>
20 #include <linux/device-mapper.h>
21 #include <crypto/hash.h>
23 #define DM_MSG_PREFIX "verity"
25 #define DM_VERITY_IO_VEC_INLINE 16
26 #define DM_VERITY_MEMPOOL_SIZE 4
27 #define DM_VERITY_DEFAULT_PREFETCH_SIZE 262144
29 #define DM_VERITY_MAX_LEVELS 63
31 static unsigned dm_verity_prefetch_cluster = DM_VERITY_DEFAULT_PREFETCH_SIZE;
33 module_param_named(prefetch_cluster, dm_verity_prefetch_cluster, uint, S_IRUGO | S_IWUSR);
35 struct dm_verity {
36 struct dm_dev *data_dev;
37 struct dm_dev *hash_dev;
38 struct dm_target *ti;
39 struct dm_bufio_client *bufio;
40 char *alg_name;
41 struct crypto_shash *tfm;
42 u8 *root_digest; /* digest of the root block */
43 u8 *salt; /* salt: its size is salt_size */
44 unsigned salt_size;
45 sector_t data_start; /* data offset in 512-byte sectors */
46 sector_t hash_start; /* hash start in blocks */
47 sector_t data_blocks; /* the number of data blocks */
48 sector_t hash_blocks; /* the number of hash blocks */
49 unsigned char data_dev_block_bits; /* log2(data blocksize) */
50 unsigned char hash_dev_block_bits; /* log2(hash blocksize) */
51 unsigned char hash_per_block_bits; /* log2(hashes in hash block) */
52 unsigned char levels; /* the number of tree levels */
53 unsigned char version;
54 unsigned digest_size; /* digest size for the current hash algorithm */
55 unsigned shash_descsize;/* the size of temporary space for crypto */
56 int hash_failed; /* set to 1 if hash of any block failed */
58 mempool_t *io_mempool; /* mempool of struct dm_verity_io */
59 mempool_t *vec_mempool; /* mempool of bio vector */
61 struct workqueue_struct *verify_wq;
63 /* starting blocks for each tree level. 0 is the lowest level. */
64 sector_t hash_level_block[DM_VERITY_MAX_LEVELS];
67 struct dm_verity_io {
68 struct dm_verity *v;
69 struct bio *bio;
71 /* original values of bio->bi_end_io and bio->bi_private */
72 bio_end_io_t *orig_bi_end_io;
73 void *orig_bi_private;
75 sector_t block;
76 unsigned n_blocks;
78 /* saved bio vector */
79 struct bio_vec *io_vec;
80 unsigned io_vec_size;
82 struct work_struct work;
84 /* A space for short vectors; longer vectors are allocated separately. */
85 struct bio_vec io_vec_inline[DM_VERITY_IO_VEC_INLINE];
88 * Three variably-size fields follow this struct:
90 * u8 hash_desc[v->shash_descsize];
91 * u8 real_digest[v->digest_size];
92 * u8 want_digest[v->digest_size];
94 * To access them use: io_hash_desc(), io_real_digest() and io_want_digest().
98 static struct shash_desc *io_hash_desc(struct dm_verity *v, struct dm_verity_io *io)
100 return (struct shash_desc *)(io + 1);
103 static u8 *io_real_digest(struct dm_verity *v, struct dm_verity_io *io)
105 return (u8 *)(io + 1) + v->shash_descsize;
108 static u8 *io_want_digest(struct dm_verity *v, struct dm_verity_io *io)
110 return (u8 *)(io + 1) + v->shash_descsize + v->digest_size;
114 * Auxiliary structure appended to each dm-bufio buffer. If the value
115 * hash_verified is nonzero, hash of the block has been verified.
117 * The variable hash_verified is set to 0 when allocating the buffer, then
118 * it can be changed to 1 and it is never reset to 0 again.
120 * There is no lock around this value, a race condition can at worst cause
121 * that multiple processes verify the hash of the same buffer simultaneously
122 * and write 1 to hash_verified simultaneously.
123 * This condition is harmless, so we don't need locking.
125 struct buffer_aux {
126 int hash_verified;
130 * Initialize struct buffer_aux for a freshly created buffer.
132 static void dm_bufio_alloc_callback(struct dm_buffer *buf)
134 struct buffer_aux *aux = dm_bufio_get_aux_data(buf);
136 aux->hash_verified = 0;
140 * Translate input sector number to the sector number on the target device.
142 static sector_t verity_map_sector(struct dm_verity *v, sector_t bi_sector)
144 return v->data_start + dm_target_offset(v->ti, bi_sector);
148 * Return hash position of a specified block at a specified tree level
149 * (0 is the lowest level).
150 * The lowest "hash_per_block_bits"-bits of the result denote hash position
151 * inside a hash block. The remaining bits denote location of the hash block.
153 static sector_t verity_position_at_level(struct dm_verity *v, sector_t block,
154 int level)
156 return block >> (level * v->hash_per_block_bits);
159 static void verity_hash_at_level(struct dm_verity *v, sector_t block, int level,
160 sector_t *hash_block, unsigned *offset)
162 sector_t position = verity_position_at_level(v, block, level);
163 unsigned idx;
165 *hash_block = v->hash_level_block[level] + (position >> v->hash_per_block_bits);
167 if (!offset)
168 return;
170 idx = position & ((1 << v->hash_per_block_bits) - 1);
171 if (!v->version)
172 *offset = idx * v->digest_size;
173 else
174 *offset = idx << (v->hash_dev_block_bits - v->hash_per_block_bits);
178 * Verify hash of a metadata block pertaining to the specified data block
179 * ("block" argument) at a specified level ("level" argument).
181 * On successful return, io_want_digest(v, io) contains the hash value for
182 * a lower tree level or for the data block (if we're at the lowest leve).
184 * If "skip_unverified" is true, unverified buffer is skipped and 1 is returned.
185 * If "skip_unverified" is false, unverified buffer is hashed and verified
186 * against current value of io_want_digest(v, io).
188 static int verity_verify_level(struct dm_verity_io *io, sector_t block,
189 int level, bool skip_unverified)
191 struct dm_verity *v = io->v;
192 struct dm_buffer *buf;
193 struct buffer_aux *aux;
194 u8 *data;
195 int r;
196 sector_t hash_block;
197 unsigned offset;
199 verity_hash_at_level(v, block, level, &hash_block, &offset);
201 data = dm_bufio_read(v->bufio, hash_block, &buf);
202 if (unlikely(IS_ERR(data)))
203 return PTR_ERR(data);
205 aux = dm_bufio_get_aux_data(buf);
207 if (!aux->hash_verified) {
208 struct shash_desc *desc;
209 u8 *result;
211 if (skip_unverified) {
212 r = 1;
213 goto release_ret_r;
216 desc = io_hash_desc(v, io);
217 desc->tfm = v->tfm;
218 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
219 r = crypto_shash_init(desc);
220 if (r < 0) {
221 DMERR("crypto_shash_init failed: %d", r);
222 goto release_ret_r;
225 if (likely(v->version >= 1)) {
226 r = crypto_shash_update(desc, v->salt, v->salt_size);
227 if (r < 0) {
228 DMERR("crypto_shash_update failed: %d", r);
229 goto release_ret_r;
233 r = crypto_shash_update(desc, data, 1 << v->hash_dev_block_bits);
234 if (r < 0) {
235 DMERR("crypto_shash_update failed: %d", r);
236 goto release_ret_r;
239 if (!v->version) {
240 r = crypto_shash_update(desc, v->salt, v->salt_size);
241 if (r < 0) {
242 DMERR("crypto_shash_update failed: %d", r);
243 goto release_ret_r;
247 result = io_real_digest(v, io);
248 r = crypto_shash_final(desc, result);
249 if (r < 0) {
250 DMERR("crypto_shash_final failed: %d", r);
251 goto release_ret_r;
253 if (unlikely(memcmp(result, io_want_digest(v, io), v->digest_size))) {
254 DMERR_LIMIT("metadata block %llu is corrupted",
255 (unsigned long long)hash_block);
256 v->hash_failed = 1;
257 r = -EIO;
258 goto release_ret_r;
259 } else
260 aux->hash_verified = 1;
263 data += offset;
265 memcpy(io_want_digest(v, io), data, v->digest_size);
267 dm_bufio_release(buf);
268 return 0;
270 release_ret_r:
271 dm_bufio_release(buf);
273 return r;
277 * Verify one "dm_verity_io" structure.
279 static int verity_verify_io(struct dm_verity_io *io)
281 struct dm_verity *v = io->v;
282 unsigned b;
283 int i;
284 unsigned vector = 0, offset = 0;
286 for (b = 0; b < io->n_blocks; b++) {
287 struct shash_desc *desc;
288 u8 *result;
289 int r;
290 unsigned todo;
292 if (likely(v->levels)) {
294 * First, we try to get the requested hash for
295 * the current block. If the hash block itself is
296 * verified, zero is returned. If it isn't, this
297 * function returns 0 and we fall back to whole
298 * chain verification.
300 int r = verity_verify_level(io, io->block + b, 0, true);
301 if (likely(!r))
302 goto test_block_hash;
303 if (r < 0)
304 return r;
307 memcpy(io_want_digest(v, io), v->root_digest, v->digest_size);
309 for (i = v->levels - 1; i >= 0; i--) {
310 int r = verity_verify_level(io, io->block + b, i, false);
311 if (unlikely(r))
312 return r;
315 test_block_hash:
316 desc = io_hash_desc(v, io);
317 desc->tfm = v->tfm;
318 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
319 r = crypto_shash_init(desc);
320 if (r < 0) {
321 DMERR("crypto_shash_init failed: %d", r);
322 return r;
325 if (likely(v->version >= 1)) {
326 r = crypto_shash_update(desc, v->salt, v->salt_size);
327 if (r < 0) {
328 DMERR("crypto_shash_update failed: %d", r);
329 return r;
333 todo = 1 << v->data_dev_block_bits;
334 do {
335 struct bio_vec *bv;
336 u8 *page;
337 unsigned len;
339 BUG_ON(vector >= io->io_vec_size);
340 bv = &io->io_vec[vector];
341 page = kmap_atomic(bv->bv_page);
342 len = bv->bv_len - offset;
343 if (likely(len >= todo))
344 len = todo;
345 r = crypto_shash_update(desc,
346 page + bv->bv_offset + offset, len);
347 kunmap_atomic(page);
348 if (r < 0) {
349 DMERR("crypto_shash_update failed: %d", r);
350 return r;
352 offset += len;
353 if (likely(offset == bv->bv_len)) {
354 offset = 0;
355 vector++;
357 todo -= len;
358 } while (todo);
360 if (!v->version) {
361 r = crypto_shash_update(desc, v->salt, v->salt_size);
362 if (r < 0) {
363 DMERR("crypto_shash_update failed: %d", r);
364 return r;
368 result = io_real_digest(v, io);
369 r = crypto_shash_final(desc, result);
370 if (r < 0) {
371 DMERR("crypto_shash_final failed: %d", r);
372 return r;
374 if (unlikely(memcmp(result, io_want_digest(v, io), v->digest_size))) {
375 DMERR_LIMIT("data block %llu is corrupted",
376 (unsigned long long)(io->block + b));
377 v->hash_failed = 1;
378 return -EIO;
381 BUG_ON(vector != io->io_vec_size);
382 BUG_ON(offset);
384 return 0;
388 * End one "io" structure with a given error.
390 static void verity_finish_io(struct dm_verity_io *io, int error)
392 struct bio *bio = io->bio;
393 struct dm_verity *v = io->v;
395 bio->bi_end_io = io->orig_bi_end_io;
396 bio->bi_private = io->orig_bi_private;
398 if (io->io_vec != io->io_vec_inline)
399 mempool_free(io->io_vec, v->vec_mempool);
401 mempool_free(io, v->io_mempool);
403 bio_endio(bio, error);
406 static void verity_work(struct work_struct *w)
408 struct dm_verity_io *io = container_of(w, struct dm_verity_io, work);
410 verity_finish_io(io, verity_verify_io(io));
413 static void verity_end_io(struct bio *bio, int error)
415 struct dm_verity_io *io = bio->bi_private;
417 if (error) {
418 verity_finish_io(io, error);
419 return;
422 INIT_WORK(&io->work, verity_work);
423 queue_work(io->v->verify_wq, &io->work);
427 * Prefetch buffers for the specified io.
428 * The root buffer is not prefetched, it is assumed that it will be cached
429 * all the time.
431 static void verity_prefetch_io(struct dm_verity *v, struct dm_verity_io *io)
433 int i;
435 for (i = v->levels - 2; i >= 0; i--) {
436 sector_t hash_block_start;
437 sector_t hash_block_end;
438 verity_hash_at_level(v, io->block, i, &hash_block_start, NULL);
439 verity_hash_at_level(v, io->block + io->n_blocks - 1, i, &hash_block_end, NULL);
440 if (!i) {
441 unsigned cluster = *(volatile unsigned *)&dm_verity_prefetch_cluster;
443 cluster >>= v->data_dev_block_bits;
444 if (unlikely(!cluster))
445 goto no_prefetch_cluster;
447 if (unlikely(cluster & (cluster - 1)))
448 cluster = 1 << (fls(cluster) - 1);
450 hash_block_start &= ~(sector_t)(cluster - 1);
451 hash_block_end |= cluster - 1;
452 if (unlikely(hash_block_end >= v->hash_blocks))
453 hash_block_end = v->hash_blocks - 1;
455 no_prefetch_cluster:
456 dm_bufio_prefetch(v->bufio, hash_block_start,
457 hash_block_end - hash_block_start + 1);
462 * Bio map function. It allocates dm_verity_io structure and bio vector and
463 * fills them. Then it issues prefetches and the I/O.
465 static int verity_map(struct dm_target *ti, struct bio *bio,
466 union map_info *map_context)
468 struct dm_verity *v = ti->private;
469 struct dm_verity_io *io;
471 bio->bi_bdev = v->data_dev->bdev;
472 bio->bi_sector = verity_map_sector(v, bio->bi_sector);
474 if (((unsigned)bio->bi_sector | bio_sectors(bio)) &
475 ((1 << (v->data_dev_block_bits - SECTOR_SHIFT)) - 1)) {
476 DMERR_LIMIT("unaligned io");
477 return -EIO;
480 if ((bio->bi_sector + bio_sectors(bio)) >>
481 (v->data_dev_block_bits - SECTOR_SHIFT) > v->data_blocks) {
482 DMERR_LIMIT("io out of range");
483 return -EIO;
486 if (bio_data_dir(bio) == WRITE)
487 return -EIO;
489 io = mempool_alloc(v->io_mempool, GFP_NOIO);
490 io->v = v;
491 io->bio = bio;
492 io->orig_bi_end_io = bio->bi_end_io;
493 io->orig_bi_private = bio->bi_private;
494 io->block = bio->bi_sector >> (v->data_dev_block_bits - SECTOR_SHIFT);
495 io->n_blocks = bio->bi_size >> v->data_dev_block_bits;
497 bio->bi_end_io = verity_end_io;
498 bio->bi_private = io;
499 io->io_vec_size = bio->bi_vcnt - bio->bi_idx;
500 if (io->io_vec_size < DM_VERITY_IO_VEC_INLINE)
501 io->io_vec = io->io_vec_inline;
502 else
503 io->io_vec = mempool_alloc(v->vec_mempool, GFP_NOIO);
504 memcpy(io->io_vec, bio_iovec(bio),
505 io->io_vec_size * sizeof(struct bio_vec));
507 verity_prefetch_io(v, io);
509 generic_make_request(bio);
511 return DM_MAPIO_SUBMITTED;
515 * Status: V (valid) or C (corruption found)
517 static void verity_status(struct dm_target *ti, status_type_t type,
518 char *result, unsigned maxlen)
520 struct dm_verity *v = ti->private;
521 unsigned sz = 0;
522 unsigned x;
524 switch (type) {
525 case STATUSTYPE_INFO:
526 DMEMIT("%c", v->hash_failed ? 'C' : 'V');
527 break;
528 case STATUSTYPE_TABLE:
529 DMEMIT("%u %s %s %u %u %llu %llu %s ",
530 v->version,
531 v->data_dev->name,
532 v->hash_dev->name,
533 1 << v->data_dev_block_bits,
534 1 << v->hash_dev_block_bits,
535 (unsigned long long)v->data_blocks,
536 (unsigned long long)v->hash_start,
537 v->alg_name
539 for (x = 0; x < v->digest_size; x++)
540 DMEMIT("%02x", v->root_digest[x]);
541 DMEMIT(" ");
542 if (!v->salt_size)
543 DMEMIT("-");
544 else
545 for (x = 0; x < v->salt_size; x++)
546 DMEMIT("%02x", v->salt[x]);
547 break;
551 static int verity_ioctl(struct dm_target *ti, unsigned cmd,
552 unsigned long arg)
554 struct dm_verity *v = ti->private;
555 int r = 0;
557 if (v->data_start ||
558 ti->len != i_size_read(v->data_dev->bdev->bd_inode) >> SECTOR_SHIFT)
559 r = scsi_verify_blk_ioctl(NULL, cmd);
561 return r ? : __blkdev_driver_ioctl(v->data_dev->bdev, v->data_dev->mode,
562 cmd, arg);
565 static int verity_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
566 struct bio_vec *biovec, int max_size)
568 struct dm_verity *v = ti->private;
569 struct request_queue *q = bdev_get_queue(v->data_dev->bdev);
571 if (!q->merge_bvec_fn)
572 return max_size;
574 bvm->bi_bdev = v->data_dev->bdev;
575 bvm->bi_sector = verity_map_sector(v, bvm->bi_sector);
577 return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
580 static int verity_iterate_devices(struct dm_target *ti,
581 iterate_devices_callout_fn fn, void *data)
583 struct dm_verity *v = ti->private;
585 return fn(ti, v->data_dev, v->data_start, ti->len, data);
588 static void verity_io_hints(struct dm_target *ti, struct queue_limits *limits)
590 struct dm_verity *v = ti->private;
592 if (limits->logical_block_size < 1 << v->data_dev_block_bits)
593 limits->logical_block_size = 1 << v->data_dev_block_bits;
595 if (limits->physical_block_size < 1 << v->data_dev_block_bits)
596 limits->physical_block_size = 1 << v->data_dev_block_bits;
598 blk_limits_io_min(limits, limits->logical_block_size);
601 static void verity_dtr(struct dm_target *ti)
603 struct dm_verity *v = ti->private;
605 if (v->verify_wq)
606 destroy_workqueue(v->verify_wq);
608 if (v->vec_mempool)
609 mempool_destroy(v->vec_mempool);
611 if (v->io_mempool)
612 mempool_destroy(v->io_mempool);
614 if (v->bufio)
615 dm_bufio_client_destroy(v->bufio);
617 kfree(v->salt);
618 kfree(v->root_digest);
620 if (v->tfm)
621 crypto_free_shash(v->tfm);
623 kfree(v->alg_name);
625 if (v->hash_dev)
626 dm_put_device(ti, v->hash_dev);
628 if (v->data_dev)
629 dm_put_device(ti, v->data_dev);
631 kfree(v);
635 * Target parameters:
636 * <version> The current format is version 1.
637 * Vsn 0 is compatible with original Chromium OS releases.
638 * <data device>
639 * <hash device>
640 * <data block size>
641 * <hash block size>
642 * <the number of data blocks>
643 * <hash start block>
644 * <algorithm>
645 * <digest>
646 * <salt> Hex string or "-" if no salt.
648 static int verity_ctr(struct dm_target *ti, unsigned argc, char **argv)
650 struct dm_verity *v;
651 unsigned num;
652 unsigned long long num_ll;
653 int r;
654 int i;
655 sector_t hash_position;
656 char dummy;
658 v = kzalloc(sizeof(struct dm_verity), GFP_KERNEL);
659 if (!v) {
660 ti->error = "Cannot allocate verity structure";
661 return -ENOMEM;
663 ti->private = v;
664 v->ti = ti;
666 if ((dm_table_get_mode(ti->table) & ~FMODE_READ)) {
667 ti->error = "Device must be readonly";
668 r = -EINVAL;
669 goto bad;
672 if (argc != 10) {
673 ti->error = "Invalid argument count: exactly 10 arguments required";
674 r = -EINVAL;
675 goto bad;
678 if (sscanf(argv[0], "%d%c", &num, &dummy) != 1 ||
679 num < 0 || num > 1) {
680 ti->error = "Invalid version";
681 r = -EINVAL;
682 goto bad;
684 v->version = num;
686 r = dm_get_device(ti, argv[1], FMODE_READ, &v->data_dev);
687 if (r) {
688 ti->error = "Data device lookup failed";
689 goto bad;
692 r = dm_get_device(ti, argv[2], FMODE_READ, &v->hash_dev);
693 if (r) {
694 ti->error = "Data device lookup failed";
695 goto bad;
698 if (sscanf(argv[3], "%u%c", &num, &dummy) != 1 ||
699 !num || (num & (num - 1)) ||
700 num < bdev_logical_block_size(v->data_dev->bdev) ||
701 num > PAGE_SIZE) {
702 ti->error = "Invalid data device block size";
703 r = -EINVAL;
704 goto bad;
706 v->data_dev_block_bits = ffs(num) - 1;
708 if (sscanf(argv[4], "%u%c", &num, &dummy) != 1 ||
709 !num || (num & (num - 1)) ||
710 num < bdev_logical_block_size(v->hash_dev->bdev) ||
711 num > INT_MAX) {
712 ti->error = "Invalid hash device block size";
713 r = -EINVAL;
714 goto bad;
716 v->hash_dev_block_bits = ffs(num) - 1;
718 if (sscanf(argv[5], "%llu%c", &num_ll, &dummy) != 1 ||
719 (sector_t)(num_ll << (v->data_dev_block_bits - SECTOR_SHIFT))
720 >> (v->data_dev_block_bits - SECTOR_SHIFT) != num_ll) {
721 ti->error = "Invalid data blocks";
722 r = -EINVAL;
723 goto bad;
725 v->data_blocks = num_ll;
727 if (ti->len > (v->data_blocks << (v->data_dev_block_bits - SECTOR_SHIFT))) {
728 ti->error = "Data device is too small";
729 r = -EINVAL;
730 goto bad;
733 if (sscanf(argv[6], "%llu%c", &num_ll, &dummy) != 1 ||
734 (sector_t)(num_ll << (v->hash_dev_block_bits - SECTOR_SHIFT))
735 >> (v->hash_dev_block_bits - SECTOR_SHIFT) != num_ll) {
736 ti->error = "Invalid hash start";
737 r = -EINVAL;
738 goto bad;
740 v->hash_start = num_ll;
742 v->alg_name = kstrdup(argv[7], GFP_KERNEL);
743 if (!v->alg_name) {
744 ti->error = "Cannot allocate algorithm name";
745 r = -ENOMEM;
746 goto bad;
749 v->tfm = crypto_alloc_shash(v->alg_name, 0, 0);
750 if (IS_ERR(v->tfm)) {
751 ti->error = "Cannot initialize hash function";
752 r = PTR_ERR(v->tfm);
753 v->tfm = NULL;
754 goto bad;
756 v->digest_size = crypto_shash_digestsize(v->tfm);
757 if ((1 << v->hash_dev_block_bits) < v->digest_size * 2) {
758 ti->error = "Digest size too big";
759 r = -EINVAL;
760 goto bad;
762 v->shash_descsize =
763 sizeof(struct shash_desc) + crypto_shash_descsize(v->tfm);
765 v->root_digest = kmalloc(v->digest_size, GFP_KERNEL);
766 if (!v->root_digest) {
767 ti->error = "Cannot allocate root digest";
768 r = -ENOMEM;
769 goto bad;
771 if (strlen(argv[8]) != v->digest_size * 2 ||
772 hex2bin(v->root_digest, argv[8], v->digest_size)) {
773 ti->error = "Invalid root digest";
774 r = -EINVAL;
775 goto bad;
778 if (strcmp(argv[9], "-")) {
779 v->salt_size = strlen(argv[9]) / 2;
780 v->salt = kmalloc(v->salt_size, GFP_KERNEL);
781 if (!v->salt) {
782 ti->error = "Cannot allocate salt";
783 r = -ENOMEM;
784 goto bad;
786 if (strlen(argv[9]) != v->salt_size * 2 ||
787 hex2bin(v->salt, argv[9], v->salt_size)) {
788 ti->error = "Invalid salt";
789 r = -EINVAL;
790 goto bad;
794 v->hash_per_block_bits =
795 fls((1 << v->hash_dev_block_bits) / v->digest_size) - 1;
797 v->levels = 0;
798 if (v->data_blocks)
799 while (v->hash_per_block_bits * v->levels < 64 &&
800 (unsigned long long)(v->data_blocks - 1) >>
801 (v->hash_per_block_bits * v->levels))
802 v->levels++;
804 if (v->levels > DM_VERITY_MAX_LEVELS) {
805 ti->error = "Too many tree levels";
806 r = -E2BIG;
807 goto bad;
810 hash_position = v->hash_start;
811 for (i = v->levels - 1; i >= 0; i--) {
812 sector_t s;
813 v->hash_level_block[i] = hash_position;
814 s = (v->data_blocks + ((sector_t)1 << ((i + 1) * v->hash_per_block_bits)) - 1)
815 >> ((i + 1) * v->hash_per_block_bits);
816 if (hash_position + s < hash_position) {
817 ti->error = "Hash device offset overflow";
818 r = -E2BIG;
819 goto bad;
821 hash_position += s;
823 v->hash_blocks = hash_position;
825 v->bufio = dm_bufio_client_create(v->hash_dev->bdev,
826 1 << v->hash_dev_block_bits, 1, sizeof(struct buffer_aux),
827 dm_bufio_alloc_callback, NULL);
828 if (IS_ERR(v->bufio)) {
829 ti->error = "Cannot initialize dm-bufio";
830 r = PTR_ERR(v->bufio);
831 v->bufio = NULL;
832 goto bad;
835 if (dm_bufio_get_device_size(v->bufio) < v->hash_blocks) {
836 ti->error = "Hash device is too small";
837 r = -E2BIG;
838 goto bad;
841 v->io_mempool = mempool_create_kmalloc_pool(DM_VERITY_MEMPOOL_SIZE,
842 sizeof(struct dm_verity_io) + v->shash_descsize + v->digest_size * 2);
843 if (!v->io_mempool) {
844 ti->error = "Cannot allocate io mempool";
845 r = -ENOMEM;
846 goto bad;
849 v->vec_mempool = mempool_create_kmalloc_pool(DM_VERITY_MEMPOOL_SIZE,
850 BIO_MAX_PAGES * sizeof(struct bio_vec));
851 if (!v->vec_mempool) {
852 ti->error = "Cannot allocate vector mempool";
853 r = -ENOMEM;
854 goto bad;
857 /* WQ_UNBOUND greatly improves performance when running on ramdisk */
858 v->verify_wq = alloc_workqueue("kverityd", WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM | WQ_UNBOUND, num_online_cpus());
859 if (!v->verify_wq) {
860 ti->error = "Cannot allocate workqueue";
861 r = -ENOMEM;
862 goto bad;
865 return 0;
867 bad:
868 verity_dtr(ti);
870 return r;
873 static struct target_type verity_target = {
874 .name = "verity",
875 .version = {1, 0, 0},
876 .module = THIS_MODULE,
877 .ctr = verity_ctr,
878 .dtr = verity_dtr,
879 .map = verity_map,
880 .status = verity_status,
881 .ioctl = verity_ioctl,
882 .merge = verity_merge,
883 .iterate_devices = verity_iterate_devices,
884 .io_hints = verity_io_hints,
887 static int __init dm_verity_init(void)
889 int r;
891 r = dm_register_target(&verity_target);
892 if (r < 0)
893 DMERR("register failed %d", r);
895 return r;
898 static void __exit dm_verity_exit(void)
900 dm_unregister_target(&verity_target);
903 module_init(dm_verity_init);
904 module_exit(dm_verity_exit);
906 MODULE_AUTHOR("Mikulas Patocka <mpatocka@redhat.com>");
907 MODULE_AUTHOR("Mandeep Baines <msb@chromium.org>");
908 MODULE_AUTHOR("Will Drewry <wad@chromium.org>");
909 MODULE_DESCRIPTION(DM_NAME " target for transparent disk integrity checking");
910 MODULE_LICENSE("GPL");