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
19 #include <linux/module.h>
20 #include <linux/device-mapper.h>
21 #include <linux/reboot.h>
22 #include <crypto/hash.h>
24 #define DM_MSG_PREFIX "verity"
26 #define DM_VERITY_ENV_LENGTH 42
27 #define DM_VERITY_ENV_VAR_NAME "DM_VERITY_ERR_BLOCK_NR"
29 #define DM_VERITY_IO_VEC_INLINE 16
30 #define DM_VERITY_MEMPOOL_SIZE 4
31 #define DM_VERITY_DEFAULT_PREFETCH_SIZE 262144
33 #define DM_VERITY_MAX_LEVELS 63
34 #define DM_VERITY_MAX_CORRUPTED_ERRS 100
36 #define DM_VERITY_OPT_LOGGING "ignore_corruption"
37 #define DM_VERITY_OPT_RESTART "restart_on_corruption"
39 static unsigned dm_verity_prefetch_cluster
= DM_VERITY_DEFAULT_PREFETCH_SIZE
;
41 module_param_named(prefetch_cluster
, dm_verity_prefetch_cluster
, uint
, S_IRUGO
| S_IWUSR
);
45 DM_VERITY_MODE_LOGGING
,
46 DM_VERITY_MODE_RESTART
49 enum verity_block_type
{
50 DM_VERITY_BLOCK_TYPE_DATA
,
51 DM_VERITY_BLOCK_TYPE_METADATA
55 struct dm_dev
*data_dev
;
56 struct dm_dev
*hash_dev
;
58 struct dm_bufio_client
*bufio
;
60 struct crypto_shash
*tfm
;
61 u8
*root_digest
; /* digest of the root block */
62 u8
*salt
; /* salt: its size is salt_size */
64 sector_t data_start
; /* data offset in 512-byte sectors */
65 sector_t hash_start
; /* hash start in blocks */
66 sector_t data_blocks
; /* the number of data blocks */
67 sector_t hash_blocks
; /* the number of hash blocks */
68 unsigned char data_dev_block_bits
; /* log2(data blocksize) */
69 unsigned char hash_dev_block_bits
; /* log2(hash blocksize) */
70 unsigned char hash_per_block_bits
; /* log2(hashes in hash block) */
71 unsigned char levels
; /* the number of tree levels */
72 unsigned char version
;
73 unsigned digest_size
; /* digest size for the current hash algorithm */
74 unsigned shash_descsize
;/* the size of temporary space for crypto */
75 int hash_failed
; /* set to 1 if hash of any block failed */
76 enum verity_mode mode
; /* mode for handling verification errors */
77 unsigned corrupted_errs
;/* Number of errors for corrupted blocks */
79 mempool_t
*vec_mempool
; /* mempool of bio vector */
81 struct workqueue_struct
*verify_wq
;
83 /* starting blocks for each tree level. 0 is the lowest level. */
84 sector_t hash_level_block
[DM_VERITY_MAX_LEVELS
];
90 /* original values of bio->bi_end_io and bio->bi_private */
91 bio_end_io_t
*orig_bi_end_io
;
92 void *orig_bi_private
;
97 struct bvec_iter iter
;
99 struct work_struct work
;
102 * Three variably-size fields follow this struct:
104 * u8 hash_desc[v->shash_descsize];
105 * u8 real_digest[v->digest_size];
106 * u8 want_digest[v->digest_size];
108 * To access them use: io_hash_desc(), io_real_digest() and io_want_digest().
112 struct dm_verity_prefetch_work
{
113 struct work_struct work
;
119 static struct shash_desc
*io_hash_desc(struct dm_verity
*v
, struct dm_verity_io
*io
)
121 return (struct shash_desc
*)(io
+ 1);
124 static u8
*io_real_digest(struct dm_verity
*v
, struct dm_verity_io
*io
)
126 return (u8
*)(io
+ 1) + v
->shash_descsize
;
129 static u8
*io_want_digest(struct dm_verity
*v
, struct dm_verity_io
*io
)
131 return (u8
*)(io
+ 1) + v
->shash_descsize
+ v
->digest_size
;
135 * Auxiliary structure appended to each dm-bufio buffer. If the value
136 * hash_verified is nonzero, hash of the block has been verified.
138 * The variable hash_verified is set to 0 when allocating the buffer, then
139 * it can be changed to 1 and it is never reset to 0 again.
141 * There is no lock around this value, a race condition can at worst cause
142 * that multiple processes verify the hash of the same buffer simultaneously
143 * and write 1 to hash_verified simultaneously.
144 * This condition is harmless, so we don't need locking.
151 * Initialize struct buffer_aux for a freshly created buffer.
153 static void dm_bufio_alloc_callback(struct dm_buffer
*buf
)
155 struct buffer_aux
*aux
= dm_bufio_get_aux_data(buf
);
157 aux
->hash_verified
= 0;
161 * Translate input sector number to the sector number on the target device.
163 static sector_t
verity_map_sector(struct dm_verity
*v
, sector_t bi_sector
)
165 return v
->data_start
+ dm_target_offset(v
->ti
, bi_sector
);
169 * Return hash position of a specified block at a specified tree level
170 * (0 is the lowest level).
171 * The lowest "hash_per_block_bits"-bits of the result denote hash position
172 * inside a hash block. The remaining bits denote location of the hash block.
174 static sector_t
verity_position_at_level(struct dm_verity
*v
, sector_t block
,
177 return block
>> (level
* v
->hash_per_block_bits
);
180 static void verity_hash_at_level(struct dm_verity
*v
, sector_t block
, int level
,
181 sector_t
*hash_block
, unsigned *offset
)
183 sector_t position
= verity_position_at_level(v
, block
, level
);
186 *hash_block
= v
->hash_level_block
[level
] + (position
>> v
->hash_per_block_bits
);
191 idx
= position
& ((1 << v
->hash_per_block_bits
) - 1);
193 *offset
= idx
* v
->digest_size
;
195 *offset
= idx
<< (v
->hash_dev_block_bits
- v
->hash_per_block_bits
);
199 * Handle verification errors.
201 static int verity_handle_err(struct dm_verity
*v
, enum verity_block_type type
,
202 unsigned long long block
)
204 char verity_env
[DM_VERITY_ENV_LENGTH
];
205 char *envp
[] = { verity_env
, NULL
};
206 const char *type_str
= "";
207 struct mapped_device
*md
= dm_table_get_md(v
->ti
->table
);
209 /* Corruption should be visible in device status in all modes */
212 if (v
->corrupted_errs
>= DM_VERITY_MAX_CORRUPTED_ERRS
)
218 case DM_VERITY_BLOCK_TYPE_DATA
:
221 case DM_VERITY_BLOCK_TYPE_METADATA
:
222 type_str
= "metadata";
228 DMERR("%s: %s block %llu is corrupted", v
->data_dev
->name
, type_str
,
231 if (v
->corrupted_errs
== DM_VERITY_MAX_CORRUPTED_ERRS
)
232 DMERR("%s: reached maximum errors", v
->data_dev
->name
);
234 snprintf(verity_env
, DM_VERITY_ENV_LENGTH
, "%s=%d,%llu",
235 DM_VERITY_ENV_VAR_NAME
, type
, block
);
237 kobject_uevent_env(&disk_to_dev(dm_disk(md
))->kobj
, KOBJ_CHANGE
, envp
);
240 if (v
->mode
== DM_VERITY_MODE_LOGGING
)
243 if (v
->mode
== DM_VERITY_MODE_RESTART
)
244 kernel_restart("dm-verity device corrupted");
250 * Verify hash of a metadata block pertaining to the specified data block
251 * ("block" argument) at a specified level ("level" argument).
253 * On successful return, io_want_digest(v, io) contains the hash value for
254 * a lower tree level or for the data block (if we're at the lowest leve).
256 * If "skip_unverified" is true, unverified buffer is skipped and 1 is returned.
257 * If "skip_unverified" is false, unverified buffer is hashed and verified
258 * against current value of io_want_digest(v, io).
260 static int verity_verify_level(struct dm_verity_io
*io
, sector_t block
,
261 int level
, bool skip_unverified
)
263 struct dm_verity
*v
= io
->v
;
264 struct dm_buffer
*buf
;
265 struct buffer_aux
*aux
;
271 verity_hash_at_level(v
, block
, level
, &hash_block
, &offset
);
273 data
= dm_bufio_read(v
->bufio
, hash_block
, &buf
);
274 if (unlikely(IS_ERR(data
)))
275 return PTR_ERR(data
);
277 aux
= dm_bufio_get_aux_data(buf
);
279 if (!aux
->hash_verified
) {
280 struct shash_desc
*desc
;
283 if (skip_unverified
) {
288 desc
= io_hash_desc(v
, io
);
290 desc
->flags
= CRYPTO_TFM_REQ_MAY_SLEEP
;
291 r
= crypto_shash_init(desc
);
293 DMERR("crypto_shash_init failed: %d", r
);
297 if (likely(v
->version
>= 1)) {
298 r
= crypto_shash_update(desc
, v
->salt
, v
->salt_size
);
300 DMERR("crypto_shash_update failed: %d", r
);
305 r
= crypto_shash_update(desc
, data
, 1 << v
->hash_dev_block_bits
);
307 DMERR("crypto_shash_update failed: %d", r
);
312 r
= crypto_shash_update(desc
, v
->salt
, v
->salt_size
);
314 DMERR("crypto_shash_update failed: %d", r
);
319 result
= io_real_digest(v
, io
);
320 r
= crypto_shash_final(desc
, result
);
322 DMERR("crypto_shash_final failed: %d", r
);
325 if (unlikely(memcmp(result
, io_want_digest(v
, io
), v
->digest_size
))) {
326 if (verity_handle_err(v
, DM_VERITY_BLOCK_TYPE_METADATA
,
332 aux
->hash_verified
= 1;
337 memcpy(io_want_digest(v
, io
), data
, v
->digest_size
);
339 dm_bufio_release(buf
);
343 dm_bufio_release(buf
);
349 * Verify one "dm_verity_io" structure.
351 static int verity_verify_io(struct dm_verity_io
*io
)
353 struct dm_verity
*v
= io
->v
;
354 struct bio
*bio
= dm_bio_from_per_bio_data(io
,
355 v
->ti
->per_bio_data_size
);
359 for (b
= 0; b
< io
->n_blocks
; b
++) {
360 struct shash_desc
*desc
;
365 if (likely(v
->levels
)) {
367 * First, we try to get the requested hash for
368 * the current block. If the hash block itself is
369 * verified, zero is returned. If it isn't, this
370 * function returns 0 and we fall back to whole
371 * chain verification.
373 int r
= verity_verify_level(io
, io
->block
+ b
, 0, true);
375 goto test_block_hash
;
380 memcpy(io_want_digest(v
, io
), v
->root_digest
, v
->digest_size
);
382 for (i
= v
->levels
- 1; i
>= 0; i
--) {
383 int r
= verity_verify_level(io
, io
->block
+ b
, i
, false);
389 desc
= io_hash_desc(v
, io
);
391 desc
->flags
= CRYPTO_TFM_REQ_MAY_SLEEP
;
392 r
= crypto_shash_init(desc
);
394 DMERR("crypto_shash_init failed: %d", r
);
398 if (likely(v
->version
>= 1)) {
399 r
= crypto_shash_update(desc
, v
->salt
, v
->salt_size
);
401 DMERR("crypto_shash_update failed: %d", r
);
405 todo
= 1 << v
->data_dev_block_bits
;
409 struct bio_vec bv
= bio_iter_iovec(bio
, io
->iter
);
411 page
= kmap_atomic(bv
.bv_page
);
413 if (likely(len
>= todo
))
415 r
= crypto_shash_update(desc
, page
+ bv
.bv_offset
, len
);
419 DMERR("crypto_shash_update failed: %d", r
);
423 bio_advance_iter(bio
, &io
->iter
, len
);
428 r
= crypto_shash_update(desc
, v
->salt
, v
->salt_size
);
430 DMERR("crypto_shash_update failed: %d", r
);
435 result
= io_real_digest(v
, io
);
436 r
= crypto_shash_final(desc
, result
);
438 DMERR("crypto_shash_final failed: %d", r
);
441 if (unlikely(memcmp(result
, io_want_digest(v
, io
), v
->digest_size
))) {
442 if (verity_handle_err(v
, DM_VERITY_BLOCK_TYPE_DATA
,
452 * End one "io" structure with a given error.
454 static void verity_finish_io(struct dm_verity_io
*io
, int error
)
456 struct dm_verity
*v
= io
->v
;
457 struct bio
*bio
= dm_bio_from_per_bio_data(io
, v
->ti
->per_bio_data_size
);
459 bio
->bi_end_io
= io
->orig_bi_end_io
;
460 bio
->bi_private
= io
->orig_bi_private
;
462 bio_endio(bio
, error
);
465 static void verity_work(struct work_struct
*w
)
467 struct dm_verity_io
*io
= container_of(w
, struct dm_verity_io
, work
);
469 verity_finish_io(io
, verity_verify_io(io
));
472 static void verity_end_io(struct bio
*bio
, int error
)
474 struct dm_verity_io
*io
= bio
->bi_private
;
477 verity_finish_io(io
, error
);
481 INIT_WORK(&io
->work
, verity_work
);
482 queue_work(io
->v
->verify_wq
, &io
->work
);
486 * Prefetch buffers for the specified io.
487 * The root buffer is not prefetched, it is assumed that it will be cached
490 static void verity_prefetch_io(struct work_struct
*work
)
492 struct dm_verity_prefetch_work
*pw
=
493 container_of(work
, struct dm_verity_prefetch_work
, work
);
494 struct dm_verity
*v
= pw
->v
;
497 for (i
= v
->levels
- 2; i
>= 0; i
--) {
498 sector_t hash_block_start
;
499 sector_t hash_block_end
;
500 verity_hash_at_level(v
, pw
->block
, i
, &hash_block_start
, NULL
);
501 verity_hash_at_level(v
, pw
->block
+ pw
->n_blocks
- 1, i
, &hash_block_end
, NULL
);
503 unsigned cluster
= ACCESS_ONCE(dm_verity_prefetch_cluster
);
505 cluster
>>= v
->data_dev_block_bits
;
506 if (unlikely(!cluster
))
507 goto no_prefetch_cluster
;
509 if (unlikely(cluster
& (cluster
- 1)))
510 cluster
= 1 << __fls(cluster
);
512 hash_block_start
&= ~(sector_t
)(cluster
- 1);
513 hash_block_end
|= cluster
- 1;
514 if (unlikely(hash_block_end
>= v
->hash_blocks
))
515 hash_block_end
= v
->hash_blocks
- 1;
518 dm_bufio_prefetch(v
->bufio
, hash_block_start
,
519 hash_block_end
- hash_block_start
+ 1);
525 static void verity_submit_prefetch(struct dm_verity
*v
, struct dm_verity_io
*io
)
527 struct dm_verity_prefetch_work
*pw
;
529 pw
= kmalloc(sizeof(struct dm_verity_prefetch_work
),
530 GFP_NOIO
| __GFP_NORETRY
| __GFP_NOMEMALLOC
| __GFP_NOWARN
);
535 INIT_WORK(&pw
->work
, verity_prefetch_io
);
537 pw
->block
= io
->block
;
538 pw
->n_blocks
= io
->n_blocks
;
539 queue_work(v
->verify_wq
, &pw
->work
);
543 * Bio map function. It allocates dm_verity_io structure and bio vector and
544 * fills them. Then it issues prefetches and the I/O.
546 static int verity_map(struct dm_target
*ti
, struct bio
*bio
)
548 struct dm_verity
*v
= ti
->private;
549 struct dm_verity_io
*io
;
551 bio
->bi_bdev
= v
->data_dev
->bdev
;
552 bio
->bi_iter
.bi_sector
= verity_map_sector(v
, bio
->bi_iter
.bi_sector
);
554 if (((unsigned)bio
->bi_iter
.bi_sector
| bio_sectors(bio
)) &
555 ((1 << (v
->data_dev_block_bits
- SECTOR_SHIFT
)) - 1)) {
556 DMERR_LIMIT("unaligned io");
560 if (bio_end_sector(bio
) >>
561 (v
->data_dev_block_bits
- SECTOR_SHIFT
) > v
->data_blocks
) {
562 DMERR_LIMIT("io out of range");
566 if (bio_data_dir(bio
) == WRITE
)
569 io
= dm_per_bio_data(bio
, ti
->per_bio_data_size
);
571 io
->orig_bi_end_io
= bio
->bi_end_io
;
572 io
->orig_bi_private
= bio
->bi_private
;
573 io
->block
= bio
->bi_iter
.bi_sector
>> (v
->data_dev_block_bits
- SECTOR_SHIFT
);
574 io
->n_blocks
= bio
->bi_iter
.bi_size
>> v
->data_dev_block_bits
;
576 bio
->bi_end_io
= verity_end_io
;
577 bio
->bi_private
= io
;
578 io
->iter
= bio
->bi_iter
;
580 verity_submit_prefetch(v
, io
);
582 generic_make_request(bio
);
584 return DM_MAPIO_SUBMITTED
;
588 * Status: V (valid) or C (corruption found)
590 static void verity_status(struct dm_target
*ti
, status_type_t type
,
591 unsigned status_flags
, char *result
, unsigned maxlen
)
593 struct dm_verity
*v
= ti
->private;
598 case STATUSTYPE_INFO
:
599 DMEMIT("%c", v
->hash_failed
? 'C' : 'V');
601 case STATUSTYPE_TABLE
:
602 DMEMIT("%u %s %s %u %u %llu %llu %s ",
606 1 << v
->data_dev_block_bits
,
607 1 << v
->hash_dev_block_bits
,
608 (unsigned long long)v
->data_blocks
,
609 (unsigned long long)v
->hash_start
,
612 for (x
= 0; x
< v
->digest_size
; x
++)
613 DMEMIT("%02x", v
->root_digest
[x
]);
618 for (x
= 0; x
< v
->salt_size
; x
++)
619 DMEMIT("%02x", v
->salt
[x
]);
620 if (v
->mode
!= DM_VERITY_MODE_EIO
) {
623 case DM_VERITY_MODE_LOGGING
:
624 DMEMIT(DM_VERITY_OPT_LOGGING
);
626 case DM_VERITY_MODE_RESTART
:
627 DMEMIT(DM_VERITY_OPT_RESTART
);
637 static int verity_ioctl(struct dm_target
*ti
, unsigned cmd
,
640 struct dm_verity
*v
= ti
->private;
644 ti
->len
!= i_size_read(v
->data_dev
->bdev
->bd_inode
) >> SECTOR_SHIFT
)
645 r
= scsi_verify_blk_ioctl(NULL
, cmd
);
647 return r
? : __blkdev_driver_ioctl(v
->data_dev
->bdev
, v
->data_dev
->mode
,
651 static int verity_merge(struct dm_target
*ti
, struct bvec_merge_data
*bvm
,
652 struct bio_vec
*biovec
, int max_size
)
654 struct dm_verity
*v
= ti
->private;
655 struct request_queue
*q
= bdev_get_queue(v
->data_dev
->bdev
);
657 if (!q
->merge_bvec_fn
)
660 bvm
->bi_bdev
= v
->data_dev
->bdev
;
661 bvm
->bi_sector
= verity_map_sector(v
, bvm
->bi_sector
);
663 return min(max_size
, q
->merge_bvec_fn(q
, bvm
, biovec
));
666 static int verity_iterate_devices(struct dm_target
*ti
,
667 iterate_devices_callout_fn fn
, void *data
)
669 struct dm_verity
*v
= ti
->private;
671 return fn(ti
, v
->data_dev
, v
->data_start
, ti
->len
, data
);
674 static void verity_io_hints(struct dm_target
*ti
, struct queue_limits
*limits
)
676 struct dm_verity
*v
= ti
->private;
678 if (limits
->logical_block_size
< 1 << v
->data_dev_block_bits
)
679 limits
->logical_block_size
= 1 << v
->data_dev_block_bits
;
681 if (limits
->physical_block_size
< 1 << v
->data_dev_block_bits
)
682 limits
->physical_block_size
= 1 << v
->data_dev_block_bits
;
684 blk_limits_io_min(limits
, limits
->logical_block_size
);
687 static void verity_dtr(struct dm_target
*ti
)
689 struct dm_verity
*v
= ti
->private;
692 destroy_workqueue(v
->verify_wq
);
695 mempool_destroy(v
->vec_mempool
);
698 dm_bufio_client_destroy(v
->bufio
);
701 kfree(v
->root_digest
);
704 crypto_free_shash(v
->tfm
);
709 dm_put_device(ti
, v
->hash_dev
);
712 dm_put_device(ti
, v
->data_dev
);
719 * <version> The current format is version 1.
720 * Vsn 0 is compatible with original Chromium OS releases.
725 * <the number of data blocks>
729 * <salt> Hex string or "-" if no salt.
731 static int verity_ctr(struct dm_target
*ti
, unsigned argc
, char **argv
)
734 struct dm_arg_set as
;
735 const char *opt_string
;
736 unsigned int num
, opt_params
;
737 unsigned long long num_ll
;
740 sector_t hash_position
;
743 static struct dm_arg _args
[] = {
744 {0, 1, "Invalid number of feature args"},
747 v
= kzalloc(sizeof(struct dm_verity
), GFP_KERNEL
);
749 ti
->error
= "Cannot allocate verity structure";
755 if ((dm_table_get_mode(ti
->table
) & ~FMODE_READ
)) {
756 ti
->error
= "Device must be readonly";
762 ti
->error
= "Not enough arguments";
767 if (sscanf(argv
[0], "%u%c", &num
, &dummy
) != 1 ||
769 ti
->error
= "Invalid version";
775 r
= dm_get_device(ti
, argv
[1], FMODE_READ
, &v
->data_dev
);
777 ti
->error
= "Data device lookup failed";
781 r
= dm_get_device(ti
, argv
[2], FMODE_READ
, &v
->hash_dev
);
783 ti
->error
= "Data device lookup failed";
787 if (sscanf(argv
[3], "%u%c", &num
, &dummy
) != 1 ||
788 !num
|| (num
& (num
- 1)) ||
789 num
< bdev_logical_block_size(v
->data_dev
->bdev
) ||
791 ti
->error
= "Invalid data device block size";
795 v
->data_dev_block_bits
= __ffs(num
);
797 if (sscanf(argv
[4], "%u%c", &num
, &dummy
) != 1 ||
798 !num
|| (num
& (num
- 1)) ||
799 num
< bdev_logical_block_size(v
->hash_dev
->bdev
) ||
801 ti
->error
= "Invalid hash device block size";
805 v
->hash_dev_block_bits
= __ffs(num
);
807 if (sscanf(argv
[5], "%llu%c", &num_ll
, &dummy
) != 1 ||
808 (sector_t
)(num_ll
<< (v
->data_dev_block_bits
- SECTOR_SHIFT
))
809 >> (v
->data_dev_block_bits
- SECTOR_SHIFT
) != num_ll
) {
810 ti
->error
= "Invalid data blocks";
814 v
->data_blocks
= num_ll
;
816 if (ti
->len
> (v
->data_blocks
<< (v
->data_dev_block_bits
- SECTOR_SHIFT
))) {
817 ti
->error
= "Data device is too small";
822 if (sscanf(argv
[6], "%llu%c", &num_ll
, &dummy
) != 1 ||
823 (sector_t
)(num_ll
<< (v
->hash_dev_block_bits
- SECTOR_SHIFT
))
824 >> (v
->hash_dev_block_bits
- SECTOR_SHIFT
) != num_ll
) {
825 ti
->error
= "Invalid hash start";
829 v
->hash_start
= num_ll
;
831 v
->alg_name
= kstrdup(argv
[7], GFP_KERNEL
);
833 ti
->error
= "Cannot allocate algorithm name";
838 v
->tfm
= crypto_alloc_shash(v
->alg_name
, 0, 0);
839 if (IS_ERR(v
->tfm
)) {
840 ti
->error
= "Cannot initialize hash function";
845 v
->digest_size
= crypto_shash_digestsize(v
->tfm
);
846 if ((1 << v
->hash_dev_block_bits
) < v
->digest_size
* 2) {
847 ti
->error
= "Digest size too big";
852 sizeof(struct shash_desc
) + crypto_shash_descsize(v
->tfm
);
854 v
->root_digest
= kmalloc(v
->digest_size
, GFP_KERNEL
);
855 if (!v
->root_digest
) {
856 ti
->error
= "Cannot allocate root digest";
860 if (strlen(argv
[8]) != v
->digest_size
* 2 ||
861 hex2bin(v
->root_digest
, argv
[8], v
->digest_size
)) {
862 ti
->error
= "Invalid root digest";
867 if (strcmp(argv
[9], "-")) {
868 v
->salt_size
= strlen(argv
[9]) / 2;
869 v
->salt
= kmalloc(v
->salt_size
, GFP_KERNEL
);
871 ti
->error
= "Cannot allocate salt";
875 if (strlen(argv
[9]) != v
->salt_size
* 2 ||
876 hex2bin(v
->salt
, argv
[9], v
->salt_size
)) {
877 ti
->error
= "Invalid salt";
886 /* Optional parameters */
891 r
= dm_read_arg_group(_args
, &as
, &opt_params
, &ti
->error
);
897 opt_string
= dm_shift_arg(&as
);
899 ti
->error
= "Not enough feature arguments";
904 if (!strcasecmp(opt_string
, DM_VERITY_OPT_LOGGING
))
905 v
->mode
= DM_VERITY_MODE_LOGGING
;
906 else if (!strcasecmp(opt_string
, DM_VERITY_OPT_RESTART
))
907 v
->mode
= DM_VERITY_MODE_RESTART
;
909 ti
->error
= "Invalid feature arguments";
916 v
->hash_per_block_bits
=
917 __fls((1 << v
->hash_dev_block_bits
) / v
->digest_size
);
921 while (v
->hash_per_block_bits
* v
->levels
< 64 &&
922 (unsigned long long)(v
->data_blocks
- 1) >>
923 (v
->hash_per_block_bits
* v
->levels
))
926 if (v
->levels
> DM_VERITY_MAX_LEVELS
) {
927 ti
->error
= "Too many tree levels";
932 hash_position
= v
->hash_start
;
933 for (i
= v
->levels
- 1; i
>= 0; i
--) {
935 v
->hash_level_block
[i
] = hash_position
;
936 s
= (v
->data_blocks
+ ((sector_t
)1 << ((i
+ 1) * v
->hash_per_block_bits
)) - 1)
937 >> ((i
+ 1) * v
->hash_per_block_bits
);
938 if (hash_position
+ s
< hash_position
) {
939 ti
->error
= "Hash device offset overflow";
945 v
->hash_blocks
= hash_position
;
947 v
->bufio
= dm_bufio_client_create(v
->hash_dev
->bdev
,
948 1 << v
->hash_dev_block_bits
, 1, sizeof(struct buffer_aux
),
949 dm_bufio_alloc_callback
, NULL
);
950 if (IS_ERR(v
->bufio
)) {
951 ti
->error
= "Cannot initialize dm-bufio";
952 r
= PTR_ERR(v
->bufio
);
957 if (dm_bufio_get_device_size(v
->bufio
) < v
->hash_blocks
) {
958 ti
->error
= "Hash device is too small";
963 ti
->per_bio_data_size
= roundup(sizeof(struct dm_verity_io
) + v
->shash_descsize
+ v
->digest_size
* 2, __alignof__(struct dm_verity_io
));
965 v
->vec_mempool
= mempool_create_kmalloc_pool(DM_VERITY_MEMPOOL_SIZE
,
966 BIO_MAX_PAGES
* sizeof(struct bio_vec
));
967 if (!v
->vec_mempool
) {
968 ti
->error
= "Cannot allocate vector mempool";
973 /* WQ_UNBOUND greatly improves performance when running on ramdisk */
974 v
->verify_wq
= alloc_workqueue("kverityd", WQ_CPU_INTENSIVE
| WQ_MEM_RECLAIM
| WQ_UNBOUND
, num_online_cpus());
976 ti
->error
= "Cannot allocate workqueue";
989 static struct target_type verity_target
= {
991 .version
= {1, 2, 0},
992 .module
= THIS_MODULE
,
996 .status
= verity_status
,
997 .ioctl
= verity_ioctl
,
998 .merge
= verity_merge
,
999 .iterate_devices
= verity_iterate_devices
,
1000 .io_hints
= verity_io_hints
,
1003 static int __init
dm_verity_init(void)
1007 r
= dm_register_target(&verity_target
);
1009 DMERR("register failed %d", r
);
1014 static void __exit
dm_verity_exit(void)
1016 dm_unregister_target(&verity_target
);
1019 module_init(dm_verity_init
);
1020 module_exit(dm_verity_exit
);
1022 MODULE_AUTHOR("Mikulas Patocka <mpatocka@redhat.com>");
1023 MODULE_AUTHOR("Mandeep Baines <msb@chromium.org>");
1024 MODULE_AUTHOR("Will Drewry <wad@chromium.org>");
1025 MODULE_DESCRIPTION(DM_NAME
" target for transparent disk integrity checking");
1026 MODULE_LICENSE("GPL");