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 * Callback function for asynchrnous crypto API completion notification
98 static void verity_op_done(struct crypto_async_request *base, int err)
100 struct verity_result *res = (struct verity_result *)base->data;
102 if (err == -EINPROGRESS)
106 complete(&res->completion);
110 * Wait for async crypto API callback
112 static inline int verity_complete_op(struct verity_result *res, int ret)
120 ret = wait_for_completion_interruptible(&res->completion);
123 reinit_completion(&res->completion);
127 DMERR("verity_wait_hash: crypto op submission failed: %d", ret);
130 if (unlikely(ret < 0))
131 DMERR("verity_wait_hash: crypto op failed: %d", ret);
136 static int verity_hash_update(struct dm_verity *v, struct ahash_request *req,
137 const u8 *data, size_t len,
138 struct verity_result *res)
140 struct scatterlist sg;
142 if (likely(!is_vmalloc_addr(data))) {
143 sg_init_one(&sg, data, len);
144 ahash_request_set_crypt(req, &sg, NULL, len);
145 return verity_complete_op(res, crypto_ahash_update(req));
149 size_t this_step = min_t(size_t, len, PAGE_SIZE - offset_in_page(data));
150 flush_kernel_vmap_range((void *)data, this_step);
151 sg_init_table(&sg, 1);
152 sg_set_page(&sg, vmalloc_to_page(data), this_step, offset_in_page(data));
153 ahash_request_set_crypt(req, &sg, NULL, this_step);
154 r = verity_complete_op(res, crypto_ahash_update(req));
165 * Wrapper for crypto_ahash_init, which handles verity salting.
167 static int verity_hash_init(struct dm_verity *v, struct ahash_request *req,
168 struct verity_result *res)
172 ahash_request_set_tfm(req, v->tfm);
173 ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP |
174 CRYPTO_TFM_REQ_MAY_BACKLOG,
175 verity_op_done, (void *)res);
176 init_completion(&res->completion);
178 r = verity_complete_op(res, crypto_ahash_init(req));
180 if (unlikely(r < 0)) {
181 DMERR("crypto_ahash_init failed: %d", r);
185 if (likely(v->salt_size && (v->version >= 1)))
186 r = verity_hash_update(v, req, v->salt, v->salt_size, res);
191 static int verity_hash_final(struct dm_verity *v, struct ahash_request *req,
192 u8 *digest, struct verity_result *res)
196 if (unlikely(v->salt_size && (!v->version))) {
197 r = verity_hash_update(v, req, v->salt, v->salt_size, res);
200 DMERR("verity_hash_final failed updating salt: %d", r);
205 ahash_request_set_crypt(req, NULL, digest, 0);
206 r = verity_complete_op(res, crypto_ahash_final(req));
211 int verity_hash(struct dm_verity *v, struct ahash_request *req,
212 const u8 *data, size_t len, u8 *digest)
215 struct verity_result res;
217 r = verity_hash_init(v, req, &res);
221 r = verity_hash_update(v, req, data, len, &res);
225 r = verity_hash_final(v, req, digest, &res);
231 static void verity_hash_at_level(struct dm_verity *v, sector_t block, int level,
232 sector_t *hash_block, unsigned *offset)
234 sector_t position = verity_position_at_level(v, block, level);
237 *hash_block = v->hash_level_block[level] + (position >> v->hash_per_block_bits);
242 idx = position & ((1 << v->hash_per_block_bits) - 1);
244 *offset = idx * v->digest_size;
246 *offset = idx << (v->hash_dev_block_bits - v->hash_per_block_bits);
250 * Handle verification errors.
252 static int verity_handle_err(struct dm_verity *v, enum verity_block_type type,
253 unsigned long long block)
255 char verity_env[DM_VERITY_ENV_LENGTH];
256 char *envp[] = { verity_env, NULL };
257 const char *type_str = "";
258 struct mapped_device *md = dm_table_get_md(v->ti->table);
260 /* Corruption should be visible in device status in all modes */
263 if (v->corrupted_errs >= DM_VERITY_MAX_CORRUPTED_ERRS)
269 case DM_VERITY_BLOCK_TYPE_DATA:
272 case DM_VERITY_BLOCK_TYPE_METADATA:
273 type_str = "metadata";
279 DMERR_LIMIT("%s: %s block %llu is corrupted", v->data_dev->name,
282 if (v->corrupted_errs == DM_VERITY_MAX_CORRUPTED_ERRS)
283 DMERR("%s: reached maximum errors", v->data_dev->name);
285 snprintf(verity_env, DM_VERITY_ENV_LENGTH, "%s=%d,%llu",
286 DM_VERITY_ENV_VAR_NAME, type, block);
288 kobject_uevent_env(&disk_to_dev(dm_disk(md))->kobj, KOBJ_CHANGE, envp);
291 if (v->mode == DM_VERITY_MODE_LOGGING)
294 if (v->mode == DM_VERITY_MODE_RESTART)
295 kernel_restart("dm-verity device corrupted");
301 * Verify hash of a metadata block pertaining to the specified data block
302 * ("block" argument) at a specified level ("level" argument).
304 * On successful return, verity_io_want_digest(v, io) contains the hash value
305 * for a lower tree level or for the data block (if we're at the lowest level).
307 * If "skip_unverified" is true, unverified buffer is skipped and 1 is returned.
308 * If "skip_unverified" is false, unverified buffer is hashed and verified
309 * against current value of verity_io_want_digest(v, io).
311 static int verity_verify_level(struct dm_verity *v, struct dm_verity_io *io,
312 sector_t block, int level, bool skip_unverified,
315 struct dm_buffer *buf;
316 struct buffer_aux *aux;
322 verity_hash_at_level(v, block, level, &hash_block, &offset);
324 data = dm_bufio_read(v->bufio, hash_block, &buf);
326 return PTR_ERR(data);
328 aux = dm_bufio_get_aux_data(buf);
330 if (!aux->hash_verified) {
331 if (skip_unverified) {
336 r = verity_hash(v, verity_io_hash_req(v, io),
337 data, 1 << v->hash_dev_block_bits,
338 verity_io_real_digest(v, io));
342 if (likely(memcmp(verity_io_real_digest(v, io), want_digest,
343 v->digest_size) == 0))
344 aux->hash_verified = 1;
345 else if (verity_fec_decode(v, io,
346 DM_VERITY_BLOCK_TYPE_METADATA,
347 hash_block, data, NULL) == 0)
348 aux->hash_verified = 1;
349 else if (verity_handle_err(v,
350 DM_VERITY_BLOCK_TYPE_METADATA,
358 memcpy(want_digest, data, v->digest_size);
362 dm_bufio_release(buf);
367 * Find a hash for a given block, write it to digest and verify the integrity
368 * of the hash tree if necessary.
370 int verity_hash_for_block(struct dm_verity *v, struct dm_verity_io *io,
371 sector_t block, u8 *digest, bool *is_zero)
375 if (likely(v->levels)) {
377 * First, we try to get the requested hash for
378 * the current block. If the hash block itself is
379 * verified, zero is returned. If it isn't, this
380 * function returns 1 and we fall back to whole
381 * chain verification.
383 r = verity_verify_level(v, io, block, 0, true, digest);
388 memcpy(digest, v->root_digest, v->digest_size);
390 for (i = v->levels - 1; i >= 0; i--) {
391 r = verity_verify_level(v, io, block, i, false, digest);
396 if (!r && v->zero_digest)
397 *is_zero = !memcmp(v->zero_digest, digest, v->digest_size);
405 * Calculates the digest for the given bio
407 int verity_for_io_block(struct dm_verity *v, struct dm_verity_io *io,
408 struct bvec_iter *iter, struct verity_result *res)
410 unsigned int todo = 1 << v->data_dev_block_bits;
411 struct bio *bio = dm_bio_from_per_bio_data(io, v->ti->per_io_data_size);
412 struct scatterlist sg;
413 struct ahash_request *req = verity_io_hash_req(v, io);
418 struct bio_vec bv = bio_iter_iovec(bio, *iter);
420 sg_init_table(&sg, 1);
424 if (likely(len >= todo))
427 * Operating on a single page at a time looks suboptimal
428 * until you consider the typical block size is 4,096B.
429 * Going through this loops twice should be very rare.
431 sg_set_page(&sg, bv.bv_page, len, bv.bv_offset);
432 ahash_request_set_crypt(req, &sg, NULL, len);
433 r = verity_complete_op(res, crypto_ahash_update(req));
435 if (unlikely(r < 0)) {
436 DMERR("verity_for_io_block crypto op failed: %d", r);
440 bio_advance_iter(bio, iter, len);
448 * Calls function process for 1 << v->data_dev_block_bits bytes in the bio_vec
449 * starting from iter.
451 int verity_for_bv_block(struct dm_verity *v, struct dm_verity_io *io,
452 struct bvec_iter *iter,
453 int (*process)(struct dm_verity *v,
454 struct dm_verity_io *io, u8 *data,
457 unsigned todo = 1 << v->data_dev_block_bits;
458 struct bio *bio = dm_bio_from_per_bio_data(io, v->ti->per_io_data_size);
464 struct bio_vec bv = bio_iter_iovec(bio, *iter);
466 page = kmap_atomic(bv.bv_page);
469 if (likely(len >= todo))
472 r = process(v, io, page + bv.bv_offset, len);
478 bio_advance_iter(bio, iter, len);
485 static int verity_bv_zero(struct dm_verity *v, struct dm_verity_io *io,
486 u8 *data, size_t len)
488 memset(data, 0, len);
493 * Verify one "dm_verity_io" structure.
495 static int verity_verify_io(struct dm_verity_io *io)
498 struct dm_verity *v = io->v;
499 struct bvec_iter start;
501 struct verity_result res;
503 for (b = 0; b < io->n_blocks; b++) {
505 struct ahash_request *req = verity_io_hash_req(v, io);
507 r = verity_hash_for_block(v, io, io->block + b,
508 verity_io_want_digest(v, io),
515 * If we expect a zero block, don't validate, just
518 r = verity_for_bv_block(v, io, &io->iter,
526 r = verity_hash_init(v, req, &res);
531 r = verity_for_io_block(v, io, &io->iter, &res);
535 r = verity_hash_final(v, req, verity_io_real_digest(v, io),
540 if (likely(memcmp(verity_io_real_digest(v, io),
541 verity_io_want_digest(v, io), v->digest_size) == 0))
543 else if (verity_fec_decode(v, io, DM_VERITY_BLOCK_TYPE_DATA,
544 io->block + b, NULL, &start) == 0)
546 else if (verity_handle_err(v, DM_VERITY_BLOCK_TYPE_DATA,
555 * Skip verity work in response to I/O error when system is shutting down.
557 static inline bool verity_is_system_shutting_down(void)
559 return system_state == SYSTEM_HALT || system_state == SYSTEM_POWER_OFF
560 || system_state == SYSTEM_RESTART;
564 * End one "io" structure with a given error.
566 static void verity_finish_io(struct dm_verity_io *io, blk_status_t status)
568 struct dm_verity *v = io->v;
569 struct bio *bio = dm_bio_from_per_bio_data(io, v->ti->per_io_data_size);
571 bio->bi_end_io = io->orig_bi_end_io;
572 bio->bi_status = status;
574 verity_fec_finish_io(io);
579 static void verity_work(struct work_struct *w)
581 struct dm_verity_io *io = container_of(w, struct dm_verity_io, work);
583 verity_finish_io(io, errno_to_blk_status(verity_verify_io(io)));
586 static void verity_end_io(struct bio *bio)
588 struct dm_verity_io *io = bio->bi_private;
590 if (bio->bi_status &&
591 (!verity_fec_is_enabled(io->v) || verity_is_system_shutting_down())) {
592 verity_finish_io(io, bio->bi_status);
596 INIT_WORK(&io->work, verity_work);
597 queue_work(io->v->verify_wq, &io->work);
601 * Prefetch buffers for the specified io.
602 * The root buffer is not prefetched, it is assumed that it will be cached
605 static void verity_prefetch_io(struct work_struct *work)
607 struct dm_verity_prefetch_work *pw =
608 container_of(work, struct dm_verity_prefetch_work, work);
609 struct dm_verity *v = pw->v;
612 for (i = v->levels - 2; i >= 0; i--) {
613 sector_t hash_block_start;
614 sector_t hash_block_end;
615 verity_hash_at_level(v, pw->block, i, &hash_block_start, NULL);
616 verity_hash_at_level(v, pw->block + pw->n_blocks - 1, i, &hash_block_end, NULL);
618 unsigned cluster = ACCESS_ONCE(dm_verity_prefetch_cluster);
620 cluster >>= v->data_dev_block_bits;
621 if (unlikely(!cluster))
622 goto no_prefetch_cluster;
624 if (unlikely(cluster & (cluster - 1)))
625 cluster = 1 << __fls(cluster);
627 hash_block_start &= ~(sector_t)(cluster - 1);
628 hash_block_end |= cluster - 1;
629 if (unlikely(hash_block_end >= v->hash_blocks))
630 hash_block_end = v->hash_blocks - 1;
633 dm_bufio_prefetch(v->bufio, hash_block_start,
634 hash_block_end - hash_block_start + 1);
640 static void verity_submit_prefetch(struct dm_verity *v, struct dm_verity_io *io)
642 struct dm_verity_prefetch_work *pw;
644 pw = kmalloc(sizeof(struct dm_verity_prefetch_work),
645 GFP_NOIO | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN);
650 INIT_WORK(&pw->work, verity_prefetch_io);
652 pw->block = io->block;
653 pw->n_blocks = io->n_blocks;
654 queue_work(v->verify_wq, &pw->work);
658 * Bio map function. It allocates dm_verity_io structure and bio vector and
659 * fills them. Then it issues prefetches and the I/O.
661 static int verity_map(struct dm_target *ti, struct bio *bio)
663 struct dm_verity *v = ti->private;
664 struct dm_verity_io *io;
666 bio_set_dev(bio, v->data_dev->bdev);
667 bio->bi_iter.bi_sector = verity_map_sector(v, bio->bi_iter.bi_sector);
669 if (((unsigned)bio->bi_iter.bi_sector | bio_sectors(bio)) &
670 ((1 << (v->data_dev_block_bits - SECTOR_SHIFT)) - 1)) {
671 DMERR_LIMIT("unaligned io");
672 return DM_MAPIO_KILL;
675 if (bio_end_sector(bio) >>
676 (v->data_dev_block_bits - SECTOR_SHIFT) > v->data_blocks) {
677 DMERR_LIMIT("io out of range");
678 return DM_MAPIO_KILL;
681 if (bio_data_dir(bio) == WRITE)
682 return DM_MAPIO_KILL;
684 io = dm_per_bio_data(bio, ti->per_io_data_size);
686 io->orig_bi_end_io = bio->bi_end_io;
687 io->block = bio->bi_iter.bi_sector >> (v->data_dev_block_bits - SECTOR_SHIFT);
688 io->n_blocks = bio->bi_iter.bi_size >> v->data_dev_block_bits;
690 bio->bi_end_io = verity_end_io;
691 bio->bi_private = io;
692 io->iter = bio->bi_iter;
694 verity_fec_init_io(io);
696 verity_submit_prefetch(v, io);
698 generic_make_request(bio);
700 return DM_MAPIO_SUBMITTED;
704 * Status: V (valid) or C (corruption found)
706 static void verity_status(struct dm_target *ti, status_type_t type,
707 unsigned status_flags, char *result, unsigned maxlen)
709 struct dm_verity *v = ti->private;
715 case STATUSTYPE_INFO:
716 DMEMIT("%c", v->hash_failed ? 'C' : 'V');
718 case STATUSTYPE_TABLE:
719 DMEMIT("%u %s %s %u %u %llu %llu %s ",
723 1 << v->data_dev_block_bits,
724 1 << v->hash_dev_block_bits,
725 (unsigned long long)v->data_blocks,
726 (unsigned long long)v->hash_start,
729 for (x = 0; x < v->digest_size; x++)
730 DMEMIT("%02x", v->root_digest[x]);
735 for (x = 0; x < v->salt_size; x++)
736 DMEMIT("%02x", v->salt[x]);
737 if (v->mode != DM_VERITY_MODE_EIO)
739 if (verity_fec_is_enabled(v))
740 args += DM_VERITY_OPTS_FEC;
746 if (v->mode != DM_VERITY_MODE_EIO) {
749 case DM_VERITY_MODE_LOGGING:
750 DMEMIT(DM_VERITY_OPT_LOGGING);
752 case DM_VERITY_MODE_RESTART:
753 DMEMIT(DM_VERITY_OPT_RESTART);
760 DMEMIT(" " DM_VERITY_OPT_IGN_ZEROES);
761 sz = verity_fec_status_table(v, sz, result, maxlen);
766 static int verity_prepare_ioctl(struct dm_target *ti,
767 struct block_device **bdev, fmode_t *mode)
769 struct dm_verity *v = ti->private;
771 *bdev = v->data_dev->bdev;
774 ti->len != i_size_read(v->data_dev->bdev->bd_inode) >> SECTOR_SHIFT)
779 static int verity_iterate_devices(struct dm_target *ti,
780 iterate_devices_callout_fn fn, void *data)
782 struct dm_verity *v = ti->private;
784 return fn(ti, v->data_dev, v->data_start, ti->len, data);
787 static void verity_io_hints(struct dm_target *ti, struct queue_limits *limits)
789 struct dm_verity *v = ti->private;
791 if (limits->logical_block_size < 1 << v->data_dev_block_bits)
792 limits->logical_block_size = 1 << v->data_dev_block_bits;
794 if (limits->physical_block_size < 1 << v->data_dev_block_bits)
795 limits->physical_block_size = 1 << v->data_dev_block_bits;
797 blk_limits_io_min(limits, limits->logical_block_size);
800 static void verity_dtr(struct dm_target *ti)
802 struct dm_verity *v = ti->private;
805 destroy_workqueue(v->verify_wq);
808 dm_bufio_client_destroy(v->bufio);
811 kfree(v->root_digest);
812 kfree(v->zero_digest);
815 crypto_free_ahash(v->tfm);
820 dm_put_device(ti, v->hash_dev);
823 dm_put_device(ti, v->data_dev);
830 static int verity_alloc_zero_digest(struct dm_verity *v)
833 struct ahash_request *req;
836 v->zero_digest = kmalloc(v->digest_size, GFP_KERNEL);
841 req = kmalloc(v->ahash_reqsize, GFP_KERNEL);
844 return r; /* verity_dtr will free zero_digest */
846 zero_data = kzalloc(1 << v->data_dev_block_bits, GFP_KERNEL);
851 r = verity_hash(v, req, zero_data, 1 << v->data_dev_block_bits,
861 static int verity_parse_opt_args(struct dm_arg_set *as, struct dm_verity *v)
865 struct dm_target *ti = v->ti;
866 const char *arg_name;
868 static const struct dm_arg _args[] = {
869 {0, DM_VERITY_OPTS_MAX, "Invalid number of feature args"},
872 r = dm_read_arg_group(_args, as, &argc, &ti->error);
880 arg_name = dm_shift_arg(as);
883 if (!strcasecmp(arg_name, DM_VERITY_OPT_LOGGING)) {
884 v->mode = DM_VERITY_MODE_LOGGING;
887 } else if (!strcasecmp(arg_name, DM_VERITY_OPT_RESTART)) {
888 v->mode = DM_VERITY_MODE_RESTART;
891 } else if (!strcasecmp(arg_name, DM_VERITY_OPT_IGN_ZEROES)) {
892 r = verity_alloc_zero_digest(v);
894 ti->error = "Cannot allocate zero digest";
899 } else if (verity_is_fec_opt_arg(arg_name)) {
900 r = verity_fec_parse_opt_args(as, v, &argc, arg_name);
906 ti->error = "Unrecognized verity feature request";
908 } while (argc && !r);
915 * <version> The current format is version 1.
916 * Vsn 0 is compatible with original Chromium OS releases.
921 * <the number of data blocks>
925 * <salt> Hex string or "-" if no salt.
927 static int verity_ctr(struct dm_target *ti, unsigned argc, char **argv)
930 struct dm_arg_set as;
932 unsigned long long num_ll;
935 sector_t hash_position;
938 v = kzalloc(sizeof(struct dm_verity), GFP_KERNEL);
940 ti->error = "Cannot allocate verity structure";
946 r = verity_fec_ctr_alloc(v);
950 if ((dm_table_get_mode(ti->table) & ~FMODE_READ)) {
951 ti->error = "Device must be readonly";
957 ti->error = "Not enough arguments";
962 if (sscanf(argv[0], "%u%c", &num, &dummy) != 1 ||
964 ti->error = "Invalid version";
970 r = dm_get_device(ti, argv[1], FMODE_READ, &v->data_dev);
972 ti->error = "Data device lookup failed";
976 r = dm_get_device(ti, argv[2], FMODE_READ, &v->hash_dev);
978 ti->error = "Hash device lookup failed";
982 if (sscanf(argv[3], "%u%c", &num, &dummy) != 1 ||
983 !num || (num & (num - 1)) ||
984 num < bdev_logical_block_size(v->data_dev->bdev) ||
986 ti->error = "Invalid data device block size";
990 v->data_dev_block_bits = __ffs(num);
992 if (sscanf(argv[4], "%u%c", &num, &dummy) != 1 ||
993 !num || (num & (num - 1)) ||
994 num < bdev_logical_block_size(v->hash_dev->bdev) ||
996 ti->error = "Invalid hash device block size";
1000 v->hash_dev_block_bits = __ffs(num);
1002 if (sscanf(argv[5], "%llu%c", &num_ll, &dummy) != 1 ||
1003 (sector_t)(num_ll << (v->data_dev_block_bits - SECTOR_SHIFT))
1004 >> (v->data_dev_block_bits - SECTOR_SHIFT) != num_ll) {
1005 ti->error = "Invalid data blocks";
1009 v->data_blocks = num_ll;
1011 if (ti->len > (v->data_blocks << (v->data_dev_block_bits - SECTOR_SHIFT))) {
1012 ti->error = "Data device is too small";
1017 if (sscanf(argv[6], "%llu%c", &num_ll, &dummy) != 1 ||
1018 (sector_t)(num_ll << (v->hash_dev_block_bits - SECTOR_SHIFT))
1019 >> (v->hash_dev_block_bits - SECTOR_SHIFT) != num_ll) {
1020 ti->error = "Invalid hash start";
1024 v->hash_start = num_ll;
1026 v->alg_name = kstrdup(argv[7], GFP_KERNEL);
1028 ti->error = "Cannot allocate algorithm name";
1033 v->tfm = crypto_alloc_ahash(v->alg_name, 0, 0);
1034 if (IS_ERR(v->tfm)) {
1035 ti->error = "Cannot initialize hash function";
1036 r = PTR_ERR(v->tfm);
1040 v->digest_size = crypto_ahash_digestsize(v->tfm);
1041 if ((1 << v->hash_dev_block_bits) < v->digest_size * 2) {
1042 ti->error = "Digest size too big";
1046 v->ahash_reqsize = sizeof(struct ahash_request) +
1047 crypto_ahash_reqsize(v->tfm);
1049 v->root_digest = kmalloc(v->digest_size, GFP_KERNEL);
1050 if (!v->root_digest) {
1051 ti->error = "Cannot allocate root digest";
1055 if (strlen(argv[8]) != v->digest_size * 2 ||
1056 hex2bin(v->root_digest, argv[8], v->digest_size)) {
1057 ti->error = "Invalid root digest";
1062 if (strcmp(argv[9], "-")) {
1063 v->salt_size = strlen(argv[9]) / 2;
1064 v->salt = kmalloc(v->salt_size, GFP_KERNEL);
1066 ti->error = "Cannot allocate salt";
1070 if (strlen(argv[9]) != v->salt_size * 2 ||
1071 hex2bin(v->salt, argv[9], v->salt_size)) {
1072 ti->error = "Invalid salt";
1081 /* Optional parameters */
1086 r = verity_parse_opt_args(&as, v);
1091 v->hash_per_block_bits =
1092 __fls((1 << v->hash_dev_block_bits) / v->digest_size);
1096 while (v->hash_per_block_bits * v->levels < 64 &&
1097 (unsigned long long)(v->data_blocks - 1) >>
1098 (v->hash_per_block_bits * v->levels))
1101 if (v->levels > DM_VERITY_MAX_LEVELS) {
1102 ti->error = "Too many tree levels";
1107 hash_position = v->hash_start;
1108 for (i = v->levels - 1; i >= 0; i--) {
1110 v->hash_level_block[i] = hash_position;
1111 s = (v->data_blocks + ((sector_t)1 << ((i + 1) * v->hash_per_block_bits)) - 1)
1112 >> ((i + 1) * v->hash_per_block_bits);
1113 if (hash_position + s < hash_position) {
1114 ti->error = "Hash device offset overflow";
1120 v->hash_blocks = hash_position;
1122 v->bufio = dm_bufio_client_create(v->hash_dev->bdev,
1123 1 << v->hash_dev_block_bits, 1, sizeof(struct buffer_aux),
1124 dm_bufio_alloc_callback, NULL);
1125 if (IS_ERR(v->bufio)) {
1126 ti->error = "Cannot initialize dm-bufio";
1127 r = PTR_ERR(v->bufio);
1132 if (dm_bufio_get_device_size(v->bufio) < v->hash_blocks) {
1133 ti->error = "Hash device is too small";
1138 /* WQ_UNBOUND greatly improves performance when running on ramdisk */
1139 v->verify_wq = alloc_workqueue("kverityd", WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM | WQ_UNBOUND, num_online_cpus());
1140 if (!v->verify_wq) {
1141 ti->error = "Cannot allocate workqueue";
1146 ti->per_io_data_size = sizeof(struct dm_verity_io) +
1147 v->ahash_reqsize + v->digest_size * 2;
1149 r = verity_fec_ctr(v);
1153 ti->per_io_data_size = roundup(ti->per_io_data_size,
1154 __alignof__(struct dm_verity_io));
1164 static struct target_type verity_target = {
1166 .features = DM_TARGET_IMMUTABLE,
1167 .version = {1, 3, 0},
1168 .module = THIS_MODULE,
1172 .status = verity_status,
1173 .prepare_ioctl = verity_prepare_ioctl,
1174 .iterate_devices = verity_iterate_devices,
1175 .io_hints = verity_io_hints,
1178 static int __init dm_verity_init(void)
1182 r = dm_register_target(&verity_target);
1184 DMERR("register failed %d", r);
1189 static void __exit dm_verity_exit(void)
1191 dm_unregister_target(&verity_target);
1194 module_init(dm_verity_init);
1195 module_exit(dm_verity_exit);
1197 MODULE_AUTHOR("Mikulas Patocka <mpatocka@redhat.com>");
1198 MODULE_AUTHOR("Mandeep Baines <msb@chromium.org>");
1199 MODULE_AUTHOR("Will Drewry <wad@chromium.org>");
1200 MODULE_DESCRIPTION(DM_NAME " target for transparent disk integrity checking");
1201 MODULE_LICENSE("GPL");