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
17 #include "dm-verity.h"
18 #include "dm-verity-fec.h"
20 #include <linux/module.h>
21 #include <linux/reboot.h>
23 #define DM_MSG_PREFIX "verity"
25 #define DM_VERITY_ENV_LENGTH 42
26 #define DM_VERITY_ENV_VAR_NAME "DM_VERITY_ERR_BLOCK_NR"
28 #define DM_VERITY_DEFAULT_PREFETCH_SIZE 262144
30 #define DM_VERITY_MAX_CORRUPTED_ERRS 100
32 #define DM_VERITY_OPT_LOGGING "ignore_corruption"
33 #define DM_VERITY_OPT_RESTART "restart_on_corruption"
34 #define DM_VERITY_OPT_IGN_ZEROES "ignore_zero_blocks"
36 #define DM_VERITY_OPTS_MAX (2 + DM_VERITY_OPTS_FEC)
38 static unsigned dm_verity_prefetch_cluster = DM_VERITY_DEFAULT_PREFETCH_SIZE;
40 module_param_named(prefetch_cluster, dm_verity_prefetch_cluster, uint, S_IRUGO | S_IWUSR);
42 struct dm_verity_prefetch_work {
43 struct work_struct work;
50 * Auxiliary structure appended to each dm-bufio buffer. If the value
51 * hash_verified is nonzero, hash of the block has been verified.
53 * The variable hash_verified is set to 0 when allocating the buffer, then
54 * it can be changed to 1 and it is never reset to 0 again.
56 * There is no lock around this value, a race condition can at worst cause
57 * that multiple processes verify the hash of the same buffer simultaneously
58 * and write 1 to hash_verified simultaneously.
59 * This condition is harmless, so we don't need locking.
66 * Initialize struct buffer_aux for a freshly created buffer.
68 static void dm_bufio_alloc_callback(struct dm_buffer *buf)
70 struct buffer_aux *aux = dm_bufio_get_aux_data(buf);
72 aux->hash_verified = 0;
76 * Translate input sector number to the sector number on the target device.
78 static sector_t verity_map_sector(struct dm_verity *v, sector_t bi_sector)
80 return v->data_start + dm_target_offset(v->ti, bi_sector);
84 * Return hash position of a specified block at a specified tree level
85 * (0 is the lowest level).
86 * The lowest "hash_per_block_bits"-bits of the result denote hash position
87 * inside a hash block. The remaining bits denote location of the hash block.
89 static sector_t verity_position_at_level(struct dm_verity *v, sector_t block,
92 return block >> (level * v->hash_per_block_bits);
96 * Wrapper for crypto_shash_init, which handles verity salting.
98 static int verity_hash_init(struct dm_verity *v, struct shash_desc *desc)
103 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
105 r = crypto_shash_init(desc);
107 if (unlikely(r < 0)) {
108 DMERR("crypto_shash_init failed: %d", r);
112 if (likely(v->version >= 1)) {
113 r = crypto_shash_update(desc, v->salt, v->salt_size);
115 if (unlikely(r < 0)) {
116 DMERR("crypto_shash_update failed: %d", r);
124 static int verity_hash_update(struct dm_verity *v, struct shash_desc *desc,
125 const u8 *data, size_t len)
127 int r = crypto_shash_update(desc, data, len);
130 DMERR("crypto_shash_update failed: %d", r);
135 static int verity_hash_final(struct dm_verity *v, struct shash_desc *desc,
140 if (unlikely(!v->version)) {
141 r = crypto_shash_update(desc, v->salt, v->salt_size);
144 DMERR("crypto_shash_update failed: %d", r);
149 r = crypto_shash_final(desc, digest);
152 DMERR("crypto_shash_final failed: %d", r);
157 int verity_hash(struct dm_verity *v, struct shash_desc *desc,
158 const u8 *data, size_t len, u8 *digest)
162 r = verity_hash_init(v, desc);
166 r = verity_hash_update(v, desc, data, len);
170 return verity_hash_final(v, desc, digest);
173 static void verity_hash_at_level(struct dm_verity *v, sector_t block, int level,
174 sector_t *hash_block, unsigned *offset)
176 sector_t position = verity_position_at_level(v, block, level);
179 *hash_block = v->hash_level_block[level] + (position >> v->hash_per_block_bits);
184 idx = position & ((1 << v->hash_per_block_bits) - 1);
186 *offset = idx * v->digest_size;
188 *offset = idx << (v->hash_dev_block_bits - v->hash_per_block_bits);
192 * Handle verification errors.
194 static int verity_handle_err(struct dm_verity *v, enum verity_block_type type,
195 unsigned long long block)
197 char verity_env[DM_VERITY_ENV_LENGTH];
198 char *envp[] = { verity_env, NULL };
199 const char *type_str = "";
200 struct mapped_device *md = dm_table_get_md(v->ti->table);
202 /* Corruption should be visible in device status in all modes */
205 if (v->corrupted_errs >= DM_VERITY_MAX_CORRUPTED_ERRS)
211 case DM_VERITY_BLOCK_TYPE_DATA:
214 case DM_VERITY_BLOCK_TYPE_METADATA:
215 type_str = "metadata";
221 DMERR("%s: %s block %llu is corrupted", v->data_dev->name, type_str,
224 if (v->corrupted_errs == DM_VERITY_MAX_CORRUPTED_ERRS)
225 DMERR("%s: reached maximum errors", v->data_dev->name);
227 snprintf(verity_env, DM_VERITY_ENV_LENGTH, "%s=%d,%llu",
228 DM_VERITY_ENV_VAR_NAME, type, block);
230 kobject_uevent_env(&disk_to_dev(dm_disk(md))->kobj, KOBJ_CHANGE, envp);
233 if (v->mode == DM_VERITY_MODE_LOGGING)
236 if (v->mode == DM_VERITY_MODE_RESTART)
237 kernel_restart("dm-verity device corrupted");
243 * Verify hash of a metadata block pertaining to the specified data block
244 * ("block" argument) at a specified level ("level" argument).
246 * On successful return, verity_io_want_digest(v, io) contains the hash value
247 * for a lower tree level or for the data block (if we're at the lowest level).
249 * If "skip_unverified" is true, unverified buffer is skipped and 1 is returned.
250 * If "skip_unverified" is false, unverified buffer is hashed and verified
251 * against current value of verity_io_want_digest(v, io).
253 static int verity_verify_level(struct dm_verity *v, struct dm_verity_io *io,
254 sector_t block, int level, bool skip_unverified,
257 struct dm_buffer *buf;
258 struct buffer_aux *aux;
264 verity_hash_at_level(v, block, level, &hash_block, &offset);
266 data = dm_bufio_read(v->bufio, hash_block, &buf);
268 return PTR_ERR(data);
270 aux = dm_bufio_get_aux_data(buf);
272 if (!aux->hash_verified) {
273 if (skip_unverified) {
278 r = verity_hash(v, verity_io_hash_desc(v, io),
279 data, 1 << v->hash_dev_block_bits,
280 verity_io_real_digest(v, io));
284 if (likely(memcmp(verity_io_real_digest(v, io), want_digest,
285 v->digest_size) == 0))
286 aux->hash_verified = 1;
287 else if (verity_fec_decode(v, io,
288 DM_VERITY_BLOCK_TYPE_METADATA,
289 hash_block, data, NULL) == 0)
290 aux->hash_verified = 1;
291 else if (verity_handle_err(v,
292 DM_VERITY_BLOCK_TYPE_METADATA,
300 memcpy(want_digest, data, v->digest_size);
304 dm_bufio_release(buf);
309 * Find a hash for a given block, write it to digest and verify the integrity
310 * of the hash tree if necessary.
312 int verity_hash_for_block(struct dm_verity *v, struct dm_verity_io *io,
313 sector_t block, u8 *digest, bool *is_zero)
317 if (likely(v->levels)) {
319 * First, we try to get the requested hash for
320 * the current block. If the hash block itself is
321 * verified, zero is returned. If it isn't, this
322 * function returns 1 and we fall back to whole
323 * chain verification.
325 r = verity_verify_level(v, io, block, 0, true, digest);
330 memcpy(digest, v->root_digest, v->digest_size);
332 for (i = v->levels - 1; i >= 0; i--) {
333 r = verity_verify_level(v, io, block, i, false, digest);
338 if (!r && v->zero_digest)
339 *is_zero = !memcmp(v->zero_digest, digest, v->digest_size);
347 * Calls function process for 1 << v->data_dev_block_bits bytes in the bio_vec
348 * starting from iter.
350 int verity_for_bv_block(struct dm_verity *v, struct dm_verity_io *io,
351 struct bvec_iter *iter,
352 int (*process)(struct dm_verity *v,
353 struct dm_verity_io *io, u8 *data,
356 unsigned todo = 1 << v->data_dev_block_bits;
357 struct bio *bio = dm_bio_from_per_bio_data(io, v->ti->per_bio_data_size);
363 struct bio_vec bv = bio_iter_iovec(bio, *iter);
365 page = kmap_atomic(bv.bv_page);
368 if (likely(len >= todo))
371 r = process(v, io, page + bv.bv_offset, len);
377 bio_advance_iter(bio, iter, len);
384 static int verity_bv_hash_update(struct dm_verity *v, struct dm_verity_io *io,
385 u8 *data, size_t len)
387 return verity_hash_update(v, verity_io_hash_desc(v, io), data, len);
390 static int verity_bv_zero(struct dm_verity *v, struct dm_verity_io *io,
391 u8 *data, size_t len)
393 memset(data, 0, len);
398 * Verify one "dm_verity_io" structure.
400 static int verity_verify_io(struct dm_verity_io *io)
403 struct dm_verity *v = io->v;
404 struct bvec_iter start;
407 for (b = 0; b < io->n_blocks; b++) {
409 struct shash_desc *desc = verity_io_hash_desc(v, io);
411 r = verity_hash_for_block(v, io, io->block + b,
412 verity_io_want_digest(v, io),
419 * If we expect a zero block, don't validate, just
422 r = verity_for_bv_block(v, io, &io->iter,
430 r = verity_hash_init(v, desc);
435 r = verity_for_bv_block(v, io, &io->iter, verity_bv_hash_update);
439 r = verity_hash_final(v, desc, verity_io_real_digest(v, io));
443 if (likely(memcmp(verity_io_real_digest(v, io),
444 verity_io_want_digest(v, io), v->digest_size) == 0))
446 else if (verity_fec_decode(v, io, DM_VERITY_BLOCK_TYPE_DATA,
447 io->block + b, NULL, &start) == 0)
449 else if (verity_handle_err(v, DM_VERITY_BLOCK_TYPE_DATA,
458 * End one "io" structure with a given error.
460 static void verity_finish_io(struct dm_verity_io *io, int error)
462 struct dm_verity *v = io->v;
463 struct bio *bio = dm_bio_from_per_bio_data(io, v->ti->per_bio_data_size);
465 bio->bi_end_io = io->orig_bi_end_io;
466 bio->bi_error = error;
468 verity_fec_finish_io(io);
473 static void verity_work(struct work_struct *w)
475 struct dm_verity_io *io = container_of(w, struct dm_verity_io, work);
477 verity_finish_io(io, verity_verify_io(io));
480 static void verity_end_io(struct bio *bio)
482 struct dm_verity_io *io = bio->bi_private;
484 if (bio->bi_error && !verity_fec_is_enabled(io->v)) {
485 verity_finish_io(io, bio->bi_error);
489 INIT_WORK(&io->work, verity_work);
490 queue_work(io->v->verify_wq, &io->work);
494 * Prefetch buffers for the specified io.
495 * The root buffer is not prefetched, it is assumed that it will be cached
498 static void verity_prefetch_io(struct work_struct *work)
500 struct dm_verity_prefetch_work *pw =
501 container_of(work, struct dm_verity_prefetch_work, work);
502 struct dm_verity *v = pw->v;
504 sector_t prefetch_size;
506 for (i = v->levels - 2; i >= 0; i--) {
507 sector_t hash_block_start;
508 sector_t hash_block_end;
509 verity_hash_at_level(v, pw->block, i, &hash_block_start, NULL);
510 verity_hash_at_level(v, pw->block + pw->n_blocks - 1, i, &hash_block_end, NULL);
512 unsigned cluster = ACCESS_ONCE(dm_verity_prefetch_cluster);
514 cluster >>= v->data_dev_block_bits;
515 if (unlikely(!cluster))
516 goto no_prefetch_cluster;
518 if (unlikely(cluster & (cluster - 1)))
519 cluster = 1 << __fls(cluster);
521 hash_block_start &= ~(sector_t)(cluster - 1);
522 hash_block_end |= cluster - 1;
523 if (unlikely(hash_block_end >= v->hash_blocks))
524 hash_block_end = v->hash_blocks - 1;
527 // for emmc, it is more efficient to send bigger read
528 prefetch_size = max((sector_t)CONFIG_DM_VERITY_HASH_PREFETCH_MIN_SIZE,
529 hash_block_end - hash_block_start + 1);
530 if ((hash_block_start + prefetch_size) >= (v->hash_start + v->hash_blocks)) {
531 prefetch_size = hash_block_end - hash_block_start + 1;
533 dm_bufio_prefetch(v->bufio, hash_block_start,
540 static void verity_submit_prefetch(struct dm_verity *v, struct dm_verity_io *io)
542 struct dm_verity_prefetch_work *pw;
544 pw = kmalloc(sizeof(struct dm_verity_prefetch_work),
545 GFP_NOIO | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN);
550 INIT_WORK(&pw->work, verity_prefetch_io);
552 pw->block = io->block;
553 pw->n_blocks = io->n_blocks;
554 queue_work(v->verify_wq, &pw->work);
558 * Bio map function. It allocates dm_verity_io structure and bio vector and
559 * fills them. Then it issues prefetches and the I/O.
561 int verity_map(struct dm_target *ti, struct bio *bio)
563 struct dm_verity *v = ti->private;
564 struct dm_verity_io *io;
566 bio->bi_bdev = v->data_dev->bdev;
567 bio->bi_iter.bi_sector = verity_map_sector(v, bio->bi_iter.bi_sector);
569 if (((unsigned)bio->bi_iter.bi_sector | bio_sectors(bio)) &
570 ((1 << (v->data_dev_block_bits - SECTOR_SHIFT)) - 1)) {
571 DMERR_LIMIT("unaligned io");
575 if (bio_end_sector(bio) >>
576 (v->data_dev_block_bits - SECTOR_SHIFT) > v->data_blocks) {
577 DMERR_LIMIT("io out of range");
581 if (bio_data_dir(bio) == WRITE)
584 io = dm_per_bio_data(bio, ti->per_bio_data_size);
586 io->orig_bi_end_io = bio->bi_end_io;
587 io->block = bio->bi_iter.bi_sector >> (v->data_dev_block_bits - SECTOR_SHIFT);
588 io->n_blocks = bio->bi_iter.bi_size >> v->data_dev_block_bits;
590 bio->bi_end_io = verity_end_io;
591 bio->bi_private = io;
592 io->iter = bio->bi_iter;
594 verity_fec_init_io(io);
596 verity_submit_prefetch(v, io);
598 generic_make_request(bio);
600 return DM_MAPIO_SUBMITTED;
602 EXPORT_SYMBOL_GPL(verity_map);
605 * Status: V (valid) or C (corruption found)
607 void verity_status(struct dm_target *ti, status_type_t type,
608 unsigned status_flags, char *result, unsigned maxlen)
610 struct dm_verity *v = ti->private;
616 case STATUSTYPE_INFO:
617 DMEMIT("%c", v->hash_failed ? 'C' : 'V');
619 case STATUSTYPE_TABLE:
620 DMEMIT("%u %s %s %u %u %llu %llu %s ",
624 1 << v->data_dev_block_bits,
625 1 << v->hash_dev_block_bits,
626 (unsigned long long)v->data_blocks,
627 (unsigned long long)v->hash_start,
630 for (x = 0; x < v->digest_size; x++)
631 DMEMIT("%02x", v->root_digest[x]);
636 for (x = 0; x < v->salt_size; x++)
637 DMEMIT("%02x", v->salt[x]);
638 if (v->mode != DM_VERITY_MODE_EIO)
640 if (verity_fec_is_enabled(v))
641 args += DM_VERITY_OPTS_FEC;
647 if (v->mode != DM_VERITY_MODE_EIO) {
650 case DM_VERITY_MODE_LOGGING:
651 DMEMIT(DM_VERITY_OPT_LOGGING);
653 case DM_VERITY_MODE_RESTART:
654 DMEMIT(DM_VERITY_OPT_RESTART);
661 DMEMIT(" " DM_VERITY_OPT_IGN_ZEROES);
662 sz = verity_fec_status_table(v, sz, result, maxlen);
666 EXPORT_SYMBOL_GPL(verity_status);
668 int verity_prepare_ioctl(struct dm_target *ti,
669 struct block_device **bdev, fmode_t *mode)
671 struct dm_verity *v = ti->private;
673 *bdev = v->data_dev->bdev;
676 ti->len != i_size_read(v->data_dev->bdev->bd_inode) >> SECTOR_SHIFT)
680 EXPORT_SYMBOL_GPL(verity_prepare_ioctl);
682 int verity_iterate_devices(struct dm_target *ti,
683 iterate_devices_callout_fn fn, void *data)
685 struct dm_verity *v = ti->private;
687 return fn(ti, v->data_dev, v->data_start, ti->len, data);
689 EXPORT_SYMBOL_GPL(verity_iterate_devices);
691 void verity_io_hints(struct dm_target *ti, struct queue_limits *limits)
693 struct dm_verity *v = ti->private;
695 if (limits->logical_block_size < 1 << v->data_dev_block_bits)
696 limits->logical_block_size = 1 << v->data_dev_block_bits;
698 if (limits->physical_block_size < 1 << v->data_dev_block_bits)
699 limits->physical_block_size = 1 << v->data_dev_block_bits;
701 blk_limits_io_min(limits, limits->logical_block_size);
703 EXPORT_SYMBOL_GPL(verity_io_hints);
705 void verity_dtr(struct dm_target *ti)
707 struct dm_verity *v = ti->private;
710 destroy_workqueue(v->verify_wq);
713 dm_bufio_client_destroy(v->bufio);
716 kfree(v->root_digest);
717 kfree(v->zero_digest);
720 crypto_free_shash(v->tfm);
725 dm_put_device(ti, v->hash_dev);
728 dm_put_device(ti, v->data_dev);
734 EXPORT_SYMBOL_GPL(verity_dtr);
736 static int verity_alloc_zero_digest(struct dm_verity *v)
739 struct shash_desc *desc;
742 v->zero_digest = kmalloc(v->digest_size, GFP_KERNEL);
747 desc = kmalloc(v->shash_descsize, GFP_KERNEL);
750 return r; /* verity_dtr will free zero_digest */
752 zero_data = kzalloc(1 << v->data_dev_block_bits, GFP_KERNEL);
757 r = verity_hash(v, desc, zero_data, 1 << v->data_dev_block_bits,
767 static int verity_parse_opt_args(struct dm_arg_set *as, struct dm_verity *v)
771 struct dm_target *ti = v->ti;
772 const char *arg_name;
774 static struct dm_arg _args[] = {
775 {0, DM_VERITY_OPTS_MAX, "Invalid number of feature args"},
778 r = dm_read_arg_group(_args, as, &argc, &ti->error);
786 arg_name = dm_shift_arg(as);
789 if (!strcasecmp(arg_name, DM_VERITY_OPT_LOGGING)) {
790 v->mode = DM_VERITY_MODE_LOGGING;
793 } else if (!strcasecmp(arg_name, DM_VERITY_OPT_RESTART)) {
794 v->mode = DM_VERITY_MODE_RESTART;
797 } else if (!strcasecmp(arg_name, DM_VERITY_OPT_IGN_ZEROES)) {
798 r = verity_alloc_zero_digest(v);
800 ti->error = "Cannot allocate zero digest";
805 } else if (verity_is_fec_opt_arg(arg_name)) {
806 r = verity_fec_parse_opt_args(as, v, &argc, arg_name);
812 ti->error = "Unrecognized verity feature request";
814 } while (argc && !r);
821 * <version> The current format is version 1.
822 * Vsn 0 is compatible with original Chromium OS releases.
827 * <the number of data blocks>
831 * <salt> Hex string or "-" if no salt.
833 int verity_ctr(struct dm_target *ti, unsigned argc, char **argv)
836 struct dm_arg_set as;
838 unsigned long long num_ll;
841 sector_t hash_position;
844 v = kzalloc(sizeof(struct dm_verity), GFP_KERNEL);
846 ti->error = "Cannot allocate verity structure";
852 r = verity_fec_ctr_alloc(v);
856 if ((dm_table_get_mode(ti->table) & ~FMODE_READ)) {
857 ti->error = "Device must be readonly";
863 ti->error = "Not enough arguments";
868 if (sscanf(argv[0], "%u%c", &num, &dummy) != 1 ||
870 ti->error = "Invalid version";
876 r = dm_get_device(ti, argv[1], FMODE_READ, &v->data_dev);
878 ti->error = "Data device lookup failed";
882 r = dm_get_device(ti, argv[2], FMODE_READ, &v->hash_dev);
884 ti->error = "Data device lookup failed";
888 if (sscanf(argv[3], "%u%c", &num, &dummy) != 1 ||
889 !num || (num & (num - 1)) ||
890 num < bdev_logical_block_size(v->data_dev->bdev) ||
892 ti->error = "Invalid data device block size";
896 v->data_dev_block_bits = __ffs(num);
898 if (sscanf(argv[4], "%u%c", &num, &dummy) != 1 ||
899 !num || (num & (num - 1)) ||
900 num < bdev_logical_block_size(v->hash_dev->bdev) ||
902 ti->error = "Invalid hash device block size";
906 v->hash_dev_block_bits = __ffs(num);
908 if (sscanf(argv[5], "%llu%c", &num_ll, &dummy) != 1 ||
909 (sector_t)(num_ll << (v->data_dev_block_bits - SECTOR_SHIFT))
910 >> (v->data_dev_block_bits - SECTOR_SHIFT) != num_ll) {
911 ti->error = "Invalid data blocks";
915 v->data_blocks = num_ll;
917 if (ti->len > (v->data_blocks << (v->data_dev_block_bits - SECTOR_SHIFT))) {
918 ti->error = "Data device is too small";
923 if (sscanf(argv[6], "%llu%c", &num_ll, &dummy) != 1 ||
924 (sector_t)(num_ll << (v->hash_dev_block_bits - SECTOR_SHIFT))
925 >> (v->hash_dev_block_bits - SECTOR_SHIFT) != num_ll) {
926 ti->error = "Invalid hash start";
930 v->hash_start = num_ll;
932 v->alg_name = kstrdup(argv[7], GFP_KERNEL);
934 ti->error = "Cannot allocate algorithm name";
939 v->tfm = crypto_alloc_shash(v->alg_name, 0, 0);
940 if (IS_ERR(v->tfm)) {
941 ti->error = "Cannot initialize hash function";
946 v->digest_size = crypto_shash_digestsize(v->tfm);
947 if ((1 << v->hash_dev_block_bits) < v->digest_size * 2) {
948 ti->error = "Digest size too big";
953 sizeof(struct shash_desc) + crypto_shash_descsize(v->tfm);
955 v->root_digest = kmalloc(v->digest_size, GFP_KERNEL);
956 if (!v->root_digest) {
957 ti->error = "Cannot allocate root digest";
961 if (strlen(argv[8]) != v->digest_size * 2 ||
962 hex2bin(v->root_digest, argv[8], v->digest_size)) {
963 ti->error = "Invalid root digest";
968 if (strcmp(argv[9], "-")) {
969 v->salt_size = strlen(argv[9]) / 2;
970 v->salt = kmalloc(v->salt_size, GFP_KERNEL);
972 ti->error = "Cannot allocate salt";
976 if (strlen(argv[9]) != v->salt_size * 2 ||
977 hex2bin(v->salt, argv[9], v->salt_size)) {
978 ti->error = "Invalid salt";
987 /* Optional parameters */
992 r = verity_parse_opt_args(&as, v);
997 v->hash_per_block_bits =
998 __fls((1 << v->hash_dev_block_bits) / v->digest_size);
1002 while (v->hash_per_block_bits * v->levels < 64 &&
1003 (unsigned long long)(v->data_blocks - 1) >>
1004 (v->hash_per_block_bits * v->levels))
1007 if (v->levels > DM_VERITY_MAX_LEVELS) {
1008 ti->error = "Too many tree levels";
1013 hash_position = v->hash_start;
1014 for (i = v->levels - 1; i >= 0; i--) {
1016 v->hash_level_block[i] = hash_position;
1017 s = (v->data_blocks + ((sector_t)1 << ((i + 1) * v->hash_per_block_bits)) - 1)
1018 >> ((i + 1) * v->hash_per_block_bits);
1019 if (hash_position + s < hash_position) {
1020 ti->error = "Hash device offset overflow";
1026 v->hash_blocks = hash_position;
1028 v->bufio = dm_bufio_client_create(v->hash_dev->bdev,
1029 1 << v->hash_dev_block_bits, 1, sizeof(struct buffer_aux),
1030 dm_bufio_alloc_callback, NULL);
1031 if (IS_ERR(v->bufio)) {
1032 ti->error = "Cannot initialize dm-bufio";
1033 r = PTR_ERR(v->bufio);
1038 if (dm_bufio_get_device_size(v->bufio) < v->hash_blocks) {
1039 ti->error = "Hash device is too small";
1044 /* WQ_UNBOUND greatly improves performance when running on ramdisk */
1045 v->verify_wq = alloc_workqueue("kverityd", WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM | WQ_UNBOUND, num_online_cpus());
1046 if (!v->verify_wq) {
1047 ti->error = "Cannot allocate workqueue";
1052 ti->per_bio_data_size = sizeof(struct dm_verity_io) +
1053 v->shash_descsize + v->digest_size * 2;
1055 r = verity_fec_ctr(v);
1059 ti->per_bio_data_size = roundup(ti->per_bio_data_size,
1060 __alignof__(struct dm_verity_io));
1069 EXPORT_SYMBOL_GPL(verity_ctr);
1071 static struct target_type verity_target = {
1073 .version = {1, 3, 0},
1074 .module = THIS_MODULE,
1078 .status = verity_status,
1079 .prepare_ioctl = verity_prepare_ioctl,
1080 .iterate_devices = verity_iterate_devices,
1081 .io_hints = verity_io_hints,
1084 static int __init dm_verity_init(void)
1088 r = dm_register_target(&verity_target);
1090 DMERR("register failed %d", r);
1095 static void __exit dm_verity_exit(void)
1097 dm_unregister_target(&verity_target);
1100 module_init(dm_verity_init);
1101 module_exit(dm_verity_exit);
1103 MODULE_AUTHOR("Mikulas Patocka <mpatocka@redhat.com>");
1104 MODULE_AUTHOR("Mandeep Baines <msb@chromium.org>");
1105 MODULE_AUTHOR("Will Drewry <wad@chromium.org>");
1106 MODULE_DESCRIPTION(DM_NAME " target for transparent disk integrity checking");
1107 MODULE_LICENSE("GPL");