irqchip/bcm7120-l2: Perform suspend/resume even without installed child IRQs
[linux-2.6/btrfs-unstable.git] / drivers / md / dm-verity.c
blobbb9c6a00e4b05e0330e3917d52226fb962e5c1bb
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 <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);
43 enum verity_mode {
44 DM_VERITY_MODE_EIO,
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
54 struct dm_verity {
55 struct dm_dev *data_dev;
56 struct dm_dev *hash_dev;
57 struct dm_target *ti;
58 struct dm_bufio_client *bufio;
59 char *alg_name;
60 struct crypto_shash *tfm;
61 u8 *root_digest; /* digest of the root block */
62 u8 *salt; /* salt: its size is salt_size */
63 unsigned 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];
87 struct dm_verity_io {
88 struct dm_verity *v;
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;
94 sector_t block;
95 unsigned n_blocks;
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;
114 struct dm_verity *v;
115 sector_t block;
116 unsigned n_blocks;
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.
146 struct buffer_aux {
147 int hash_verified;
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,
175 int level)
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);
184 unsigned idx;
186 *hash_block = v->hash_level_block[level] + (position >> v->hash_per_block_bits);
188 if (!offset)
189 return;
191 idx = position & ((1 << v->hash_per_block_bits) - 1);
192 if (!v->version)
193 *offset = idx * v->digest_size;
194 else
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 */
210 v->hash_failed = 1;
212 if (v->corrupted_errs >= DM_VERITY_MAX_CORRUPTED_ERRS)
213 goto out;
215 v->corrupted_errs++;
217 switch (type) {
218 case DM_VERITY_BLOCK_TYPE_DATA:
219 type_str = "data";
220 break;
221 case DM_VERITY_BLOCK_TYPE_METADATA:
222 type_str = "metadata";
223 break;
224 default:
225 BUG();
228 DMERR("%s: %s block %llu is corrupted", v->data_dev->name, type_str,
229 block);
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);
239 out:
240 if (v->mode == DM_VERITY_MODE_LOGGING)
241 return 0;
243 if (v->mode == DM_VERITY_MODE_RESTART)
244 kernel_restart("dm-verity device corrupted");
246 return 1;
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;
266 u8 *data;
267 int r;
268 sector_t hash_block;
269 unsigned offset;
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;
281 u8 *result;
283 if (skip_unverified) {
284 r = 1;
285 goto release_ret_r;
288 desc = io_hash_desc(v, io);
289 desc->tfm = v->tfm;
290 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
291 r = crypto_shash_init(desc);
292 if (r < 0) {
293 DMERR("crypto_shash_init failed: %d", r);
294 goto release_ret_r;
297 if (likely(v->version >= 1)) {
298 r = crypto_shash_update(desc, v->salt, v->salt_size);
299 if (r < 0) {
300 DMERR("crypto_shash_update failed: %d", r);
301 goto release_ret_r;
305 r = crypto_shash_update(desc, data, 1 << v->hash_dev_block_bits);
306 if (r < 0) {
307 DMERR("crypto_shash_update failed: %d", r);
308 goto release_ret_r;
311 if (!v->version) {
312 r = crypto_shash_update(desc, v->salt, v->salt_size);
313 if (r < 0) {
314 DMERR("crypto_shash_update failed: %d", r);
315 goto release_ret_r;
319 result = io_real_digest(v, io);
320 r = crypto_shash_final(desc, result);
321 if (r < 0) {
322 DMERR("crypto_shash_final failed: %d", r);
323 goto release_ret_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,
327 hash_block)) {
328 r = -EIO;
329 goto release_ret_r;
331 } else
332 aux->hash_verified = 1;
335 data += offset;
337 memcpy(io_want_digest(v, io), data, v->digest_size);
339 dm_bufio_release(buf);
340 return 0;
342 release_ret_r:
343 dm_bufio_release(buf);
345 return r;
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);
356 unsigned b;
357 int i;
359 for (b = 0; b < io->n_blocks; b++) {
360 struct shash_desc *desc;
361 u8 *result;
362 int r;
363 unsigned todo;
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);
374 if (likely(!r))
375 goto test_block_hash;
376 if (r < 0)
377 return r;
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);
384 if (unlikely(r))
385 return r;
388 test_block_hash:
389 desc = io_hash_desc(v, io);
390 desc->tfm = v->tfm;
391 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
392 r = crypto_shash_init(desc);
393 if (r < 0) {
394 DMERR("crypto_shash_init failed: %d", r);
395 return r;
398 if (likely(v->version >= 1)) {
399 r = crypto_shash_update(desc, v->salt, v->salt_size);
400 if (r < 0) {
401 DMERR("crypto_shash_update failed: %d", r);
402 return r;
405 todo = 1 << v->data_dev_block_bits;
406 do {
407 u8 *page;
408 unsigned len;
409 struct bio_vec bv = bio_iter_iovec(bio, io->iter);
411 page = kmap_atomic(bv.bv_page);
412 len = bv.bv_len;
413 if (likely(len >= todo))
414 len = todo;
415 r = crypto_shash_update(desc, page + bv.bv_offset, len);
416 kunmap_atomic(page);
418 if (r < 0) {
419 DMERR("crypto_shash_update failed: %d", r);
420 return r;
423 bio_advance_iter(bio, &io->iter, len);
424 todo -= len;
425 } while (todo);
427 if (!v->version) {
428 r = crypto_shash_update(desc, v->salt, v->salt_size);
429 if (r < 0) {
430 DMERR("crypto_shash_update failed: %d", r);
431 return r;
435 result = io_real_digest(v, io);
436 r = crypto_shash_final(desc, result);
437 if (r < 0) {
438 DMERR("crypto_shash_final failed: %d", r);
439 return 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,
443 io->block + b))
444 return -EIO;
448 return 0;
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;
476 if (error) {
477 verity_finish_io(io, error);
478 return;
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
488 * all the time.
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;
495 int i;
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);
502 if (!i) {
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;
517 no_prefetch_cluster:
518 dm_bufio_prefetch(v->bufio, hash_block_start,
519 hash_block_end - hash_block_start + 1);
522 kfree(pw);
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);
532 if (!pw)
533 return;
535 INIT_WORK(&pw->work, verity_prefetch_io);
536 pw->v = v;
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");
557 return -EIO;
560 if (bio_end_sector(bio) >>
561 (v->data_dev_block_bits - SECTOR_SHIFT) > v->data_blocks) {
562 DMERR_LIMIT("io out of range");
563 return -EIO;
566 if (bio_data_dir(bio) == WRITE)
567 return -EIO;
569 io = dm_per_bio_data(bio, ti->per_bio_data_size);
570 io->v = v;
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;
594 unsigned sz = 0;
595 unsigned x;
597 switch (type) {
598 case STATUSTYPE_INFO:
599 DMEMIT("%c", v->hash_failed ? 'C' : 'V');
600 break;
601 case STATUSTYPE_TABLE:
602 DMEMIT("%u %s %s %u %u %llu %llu %s ",
603 v->version,
604 v->data_dev->name,
605 v->hash_dev->name,
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,
610 v->alg_name
612 for (x = 0; x < v->digest_size; x++)
613 DMEMIT("%02x", v->root_digest[x]);
614 DMEMIT(" ");
615 if (!v->salt_size)
616 DMEMIT("-");
617 else
618 for (x = 0; x < v->salt_size; x++)
619 DMEMIT("%02x", v->salt[x]);
620 if (v->mode != DM_VERITY_MODE_EIO) {
621 DMEMIT(" 1 ");
622 switch (v->mode) {
623 case DM_VERITY_MODE_LOGGING:
624 DMEMIT(DM_VERITY_OPT_LOGGING);
625 break;
626 case DM_VERITY_MODE_RESTART:
627 DMEMIT(DM_VERITY_OPT_RESTART);
628 break;
629 default:
630 BUG();
633 break;
637 static int verity_ioctl(struct dm_target *ti, unsigned cmd,
638 unsigned long arg)
640 struct dm_verity *v = ti->private;
641 int r = 0;
643 if (v->data_start ||
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,
648 cmd, arg);
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)
658 return max_size;
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;
691 if (v->verify_wq)
692 destroy_workqueue(v->verify_wq);
694 if (v->vec_mempool)
695 mempool_destroy(v->vec_mempool);
697 if (v->bufio)
698 dm_bufio_client_destroy(v->bufio);
700 kfree(v->salt);
701 kfree(v->root_digest);
703 if (v->tfm)
704 crypto_free_shash(v->tfm);
706 kfree(v->alg_name);
708 if (v->hash_dev)
709 dm_put_device(ti, v->hash_dev);
711 if (v->data_dev)
712 dm_put_device(ti, v->data_dev);
714 kfree(v);
718 * Target parameters:
719 * <version> The current format is version 1.
720 * Vsn 0 is compatible with original Chromium OS releases.
721 * <data device>
722 * <hash device>
723 * <data block size>
724 * <hash block size>
725 * <the number of data blocks>
726 * <hash start block>
727 * <algorithm>
728 * <digest>
729 * <salt> Hex string or "-" if no salt.
731 static int verity_ctr(struct dm_target *ti, unsigned argc, char **argv)
733 struct dm_verity *v;
734 struct dm_arg_set as;
735 const char *opt_string;
736 unsigned int num, opt_params;
737 unsigned long long num_ll;
738 int r;
739 int i;
740 sector_t hash_position;
741 char dummy;
743 static struct dm_arg _args[] = {
744 {0, 1, "Invalid number of feature args"},
747 v = kzalloc(sizeof(struct dm_verity), GFP_KERNEL);
748 if (!v) {
749 ti->error = "Cannot allocate verity structure";
750 return -ENOMEM;
752 ti->private = v;
753 v->ti = ti;
755 if ((dm_table_get_mode(ti->table) & ~FMODE_READ)) {
756 ti->error = "Device must be readonly";
757 r = -EINVAL;
758 goto bad;
761 if (argc < 10) {
762 ti->error = "Not enough arguments";
763 r = -EINVAL;
764 goto bad;
767 if (sscanf(argv[0], "%u%c", &num, &dummy) != 1 ||
768 num > 1) {
769 ti->error = "Invalid version";
770 r = -EINVAL;
771 goto bad;
773 v->version = num;
775 r = dm_get_device(ti, argv[1], FMODE_READ, &v->data_dev);
776 if (r) {
777 ti->error = "Data device lookup failed";
778 goto bad;
781 r = dm_get_device(ti, argv[2], FMODE_READ, &v->hash_dev);
782 if (r) {
783 ti->error = "Data device lookup failed";
784 goto bad;
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) ||
790 num > PAGE_SIZE) {
791 ti->error = "Invalid data device block size";
792 r = -EINVAL;
793 goto bad;
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) ||
800 num > INT_MAX) {
801 ti->error = "Invalid hash device block size";
802 r = -EINVAL;
803 goto bad;
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";
811 r = -EINVAL;
812 goto bad;
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";
818 r = -EINVAL;
819 goto bad;
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";
826 r = -EINVAL;
827 goto bad;
829 v->hash_start = num_ll;
831 v->alg_name = kstrdup(argv[7], GFP_KERNEL);
832 if (!v->alg_name) {
833 ti->error = "Cannot allocate algorithm name";
834 r = -ENOMEM;
835 goto bad;
838 v->tfm = crypto_alloc_shash(v->alg_name, 0, 0);
839 if (IS_ERR(v->tfm)) {
840 ti->error = "Cannot initialize hash function";
841 r = PTR_ERR(v->tfm);
842 v->tfm = NULL;
843 goto bad;
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";
848 r = -EINVAL;
849 goto bad;
851 v->shash_descsize =
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";
857 r = -ENOMEM;
858 goto bad;
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";
863 r = -EINVAL;
864 goto bad;
867 if (strcmp(argv[9], "-")) {
868 v->salt_size = strlen(argv[9]) / 2;
869 v->salt = kmalloc(v->salt_size, GFP_KERNEL);
870 if (!v->salt) {
871 ti->error = "Cannot allocate salt";
872 r = -ENOMEM;
873 goto bad;
875 if (strlen(argv[9]) != v->salt_size * 2 ||
876 hex2bin(v->salt, argv[9], v->salt_size)) {
877 ti->error = "Invalid salt";
878 r = -EINVAL;
879 goto bad;
883 argv += 10;
884 argc -= 10;
886 /* Optional parameters */
887 if (argc) {
888 as.argc = argc;
889 as.argv = argv;
891 r = dm_read_arg_group(_args, &as, &opt_params, &ti->error);
892 if (r)
893 goto bad;
895 while (opt_params) {
896 opt_params--;
897 opt_string = dm_shift_arg(&as);
898 if (!opt_string) {
899 ti->error = "Not enough feature arguments";
900 r = -EINVAL;
901 goto bad;
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;
908 else {
909 ti->error = "Invalid feature arguments";
910 r = -EINVAL;
911 goto bad;
916 v->hash_per_block_bits =
917 __fls((1 << v->hash_dev_block_bits) / v->digest_size);
919 v->levels = 0;
920 if (v->data_blocks)
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))
924 v->levels++;
926 if (v->levels > DM_VERITY_MAX_LEVELS) {
927 ti->error = "Too many tree levels";
928 r = -E2BIG;
929 goto bad;
932 hash_position = v->hash_start;
933 for (i = v->levels - 1; i >= 0; i--) {
934 sector_t s;
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";
940 r = -E2BIG;
941 goto bad;
943 hash_position += s;
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);
953 v->bufio = NULL;
954 goto bad;
957 if (dm_bufio_get_device_size(v->bufio) < v->hash_blocks) {
958 ti->error = "Hash device is too small";
959 r = -E2BIG;
960 goto bad;
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";
969 r = -ENOMEM;
970 goto bad;
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());
975 if (!v->verify_wq) {
976 ti->error = "Cannot allocate workqueue";
977 r = -ENOMEM;
978 goto bad;
981 return 0;
983 bad:
984 verity_dtr(ti);
986 return r;
989 static struct target_type verity_target = {
990 .name = "verity",
991 .version = {1, 2, 0},
992 .module = THIS_MODULE,
993 .ctr = verity_ctr,
994 .dtr = verity_dtr,
995 .map = verity_map,
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)
1005 int r;
1007 r = dm_register_target(&verity_target);
1008 if (r < 0)
1009 DMERR("register failed %d", r);
1011 return 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");