]> Pileus Git - ~andy/linux/blob - drivers/md/dm-verity.c
random32: assign to network folks in MAINTAINERS
[~andy/linux] / drivers / md / dm-verity.c
1 /*
2  * Copyright (C) 2012 Red Hat, Inc.
3  *
4  * Author: Mikulas Patocka <mpatocka@redhat.com>
5  *
6  * Based on Chromium dm-verity driver (C) 2011 The Chromium OS Authors
7  *
8  * This file is released under the GPLv2.
9  *
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.
15  */
16
17 #include "dm-bufio.h"
18
19 #include <linux/module.h>
20 #include <linux/device-mapper.h>
21 #include <crypto/hash.h>
22
23 #define DM_MSG_PREFIX                   "verity"
24
25 #define DM_VERITY_IO_VEC_INLINE         16
26 #define DM_VERITY_MEMPOOL_SIZE          4
27 #define DM_VERITY_DEFAULT_PREFETCH_SIZE 262144
28
29 #define DM_VERITY_MAX_LEVELS            63
30
31 static unsigned dm_verity_prefetch_cluster = DM_VERITY_DEFAULT_PREFETCH_SIZE;
32
33 module_param_named(prefetch_cluster, dm_verity_prefetch_cluster, uint, S_IRUGO | S_IWUSR);
34
35 struct dm_verity {
36         struct dm_dev *data_dev;
37         struct dm_dev *hash_dev;
38         struct dm_target *ti;
39         struct dm_bufio_client *bufio;
40         char *alg_name;
41         struct crypto_shash *tfm;
42         u8 *root_digest;        /* digest of the root block */
43         u8 *salt;               /* salt: its size is salt_size */
44         unsigned salt_size;
45         sector_t data_start;    /* data offset in 512-byte sectors */
46         sector_t hash_start;    /* hash start in blocks */
47         sector_t data_blocks;   /* the number of data blocks */
48         sector_t hash_blocks;   /* the number of hash blocks */
49         unsigned char data_dev_block_bits;      /* log2(data blocksize) */
50         unsigned char hash_dev_block_bits;      /* log2(hash blocksize) */
51         unsigned char hash_per_block_bits;      /* log2(hashes in hash block) */
52         unsigned char levels;   /* the number of tree levels */
53         unsigned char version;
54         unsigned digest_size;   /* digest size for the current hash algorithm */
55         unsigned shash_descsize;/* the size of temporary space for crypto */
56         int hash_failed;        /* set to 1 if hash of any block failed */
57
58         mempool_t *vec_mempool; /* mempool of bio vector */
59
60         struct workqueue_struct *verify_wq;
61
62         /* starting blocks for each tree level. 0 is the lowest level. */
63         sector_t hash_level_block[DM_VERITY_MAX_LEVELS];
64 };
65
66 struct dm_verity_io {
67         struct dm_verity *v;
68
69         /* original values of bio->bi_end_io and bio->bi_private */
70         bio_end_io_t *orig_bi_end_io;
71         void *orig_bi_private;
72
73         sector_t block;
74         unsigned n_blocks;
75
76         struct bvec_iter iter;
77
78         struct work_struct work;
79
80         /*
81          * Three variably-size fields follow this struct:
82          *
83          * u8 hash_desc[v->shash_descsize];
84          * u8 real_digest[v->digest_size];
85          * u8 want_digest[v->digest_size];
86          *
87          * To access them use: io_hash_desc(), io_real_digest() and io_want_digest().
88          */
89 };
90
91 struct dm_verity_prefetch_work {
92         struct work_struct work;
93         struct dm_verity *v;
94         sector_t block;
95         unsigned n_blocks;
96 };
97
98 static struct shash_desc *io_hash_desc(struct dm_verity *v, struct dm_verity_io *io)
99 {
100         return (struct shash_desc *)(io + 1);
101 }
102
103 static u8 *io_real_digest(struct dm_verity *v, struct dm_verity_io *io)
104 {
105         return (u8 *)(io + 1) + v->shash_descsize;
106 }
107
108 static u8 *io_want_digest(struct dm_verity *v, struct dm_verity_io *io)
109 {
110         return (u8 *)(io + 1) + v->shash_descsize + v->digest_size;
111 }
112
113 /*
114  * Auxiliary structure appended to each dm-bufio buffer. If the value
115  * hash_verified is nonzero, hash of the block has been verified.
116  *
117  * The variable hash_verified is set to 0 when allocating the buffer, then
118  * it can be changed to 1 and it is never reset to 0 again.
119  *
120  * There is no lock around this value, a race condition can at worst cause
121  * that multiple processes verify the hash of the same buffer simultaneously
122  * and write 1 to hash_verified simultaneously.
123  * This condition is harmless, so we don't need locking.
124  */
125 struct buffer_aux {
126         int hash_verified;
127 };
128
129 /*
130  * Initialize struct buffer_aux for a freshly created buffer.
131  */
132 static void dm_bufio_alloc_callback(struct dm_buffer *buf)
133 {
134         struct buffer_aux *aux = dm_bufio_get_aux_data(buf);
135
136         aux->hash_verified = 0;
137 }
138
139 /*
140  * Translate input sector number to the sector number on the target device.
141  */
142 static sector_t verity_map_sector(struct dm_verity *v, sector_t bi_sector)
143 {
144         return v->data_start + dm_target_offset(v->ti, bi_sector);
145 }
146
147 /*
148  * Return hash position of a specified block at a specified tree level
149  * (0 is the lowest level).
150  * The lowest "hash_per_block_bits"-bits of the result denote hash position
151  * inside a hash block. The remaining bits denote location of the hash block.
152  */
153 static sector_t verity_position_at_level(struct dm_verity *v, sector_t block,
154                                          int level)
155 {
156         return block >> (level * v->hash_per_block_bits);
157 }
158
159 static void verity_hash_at_level(struct dm_verity *v, sector_t block, int level,
160                                  sector_t *hash_block, unsigned *offset)
161 {
162         sector_t position = verity_position_at_level(v, block, level);
163         unsigned idx;
164
165         *hash_block = v->hash_level_block[level] + (position >> v->hash_per_block_bits);
166
167         if (!offset)
168                 return;
169
170         idx = position & ((1 << v->hash_per_block_bits) - 1);
171         if (!v->version)
172                 *offset = idx * v->digest_size;
173         else
174                 *offset = idx << (v->hash_dev_block_bits - v->hash_per_block_bits);
175 }
176
177 /*
178  * Verify hash of a metadata block pertaining to the specified data block
179  * ("block" argument) at a specified level ("level" argument).
180  *
181  * On successful return, io_want_digest(v, io) contains the hash value for
182  * a lower tree level or for the data block (if we're at the lowest leve).
183  *
184  * If "skip_unverified" is true, unverified buffer is skipped and 1 is returned.
185  * If "skip_unverified" is false, unverified buffer is hashed and verified
186  * against current value of io_want_digest(v, io).
187  */
188 static int verity_verify_level(struct dm_verity_io *io, sector_t block,
189                                int level, bool skip_unverified)
190 {
191         struct dm_verity *v = io->v;
192         struct dm_buffer *buf;
193         struct buffer_aux *aux;
194         u8 *data;
195         int r;
196         sector_t hash_block;
197         unsigned offset;
198
199         verity_hash_at_level(v, block, level, &hash_block, &offset);
200
201         data = dm_bufio_read(v->bufio, hash_block, &buf);
202         if (unlikely(IS_ERR(data)))
203                 return PTR_ERR(data);
204
205         aux = dm_bufio_get_aux_data(buf);
206
207         if (!aux->hash_verified) {
208                 struct shash_desc *desc;
209                 u8 *result;
210
211                 if (skip_unverified) {
212                         r = 1;
213                         goto release_ret_r;
214                 }
215
216                 desc = io_hash_desc(v, io);
217                 desc->tfm = v->tfm;
218                 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
219                 r = crypto_shash_init(desc);
220                 if (r < 0) {
221                         DMERR("crypto_shash_init failed: %d", r);
222                         goto release_ret_r;
223                 }
224
225                 if (likely(v->version >= 1)) {
226                         r = crypto_shash_update(desc, v->salt, v->salt_size);
227                         if (r < 0) {
228                                 DMERR("crypto_shash_update failed: %d", r);
229                                 goto release_ret_r;
230                         }
231                 }
232
233                 r = crypto_shash_update(desc, data, 1 << v->hash_dev_block_bits);
234                 if (r < 0) {
235                         DMERR("crypto_shash_update failed: %d", r);
236                         goto release_ret_r;
237                 }
238
239                 if (!v->version) {
240                         r = crypto_shash_update(desc, v->salt, v->salt_size);
241                         if (r < 0) {
242                                 DMERR("crypto_shash_update failed: %d", r);
243                                 goto release_ret_r;
244                         }
245                 }
246
247                 result = io_real_digest(v, io);
248                 r = crypto_shash_final(desc, result);
249                 if (r < 0) {
250                         DMERR("crypto_shash_final failed: %d", r);
251                         goto release_ret_r;
252                 }
253                 if (unlikely(memcmp(result, io_want_digest(v, io), v->digest_size))) {
254                         DMERR_LIMIT("metadata block %llu is corrupted",
255                                 (unsigned long long)hash_block);
256                         v->hash_failed = 1;
257                         r = -EIO;
258                         goto release_ret_r;
259                 } else
260                         aux->hash_verified = 1;
261         }
262
263         data += offset;
264
265         memcpy(io_want_digest(v, io), data, v->digest_size);
266
267         dm_bufio_release(buf);
268         return 0;
269
270 release_ret_r:
271         dm_bufio_release(buf);
272
273         return r;
274 }
275
276 /*
277  * Verify one "dm_verity_io" structure.
278  */
279 static int verity_verify_io(struct dm_verity_io *io)
280 {
281         struct dm_verity *v = io->v;
282         struct bio *bio = dm_bio_from_per_bio_data(io,
283                                                    v->ti->per_bio_data_size);
284         unsigned b;
285         int i;
286
287         for (b = 0; b < io->n_blocks; b++) {
288                 struct shash_desc *desc;
289                 u8 *result;
290                 int r;
291                 unsigned todo;
292
293                 if (likely(v->levels)) {
294                         /*
295                          * First, we try to get the requested hash for
296                          * the current block. If the hash block itself is
297                          * verified, zero is returned. If it isn't, this
298                          * function returns 0 and we fall back to whole
299                          * chain verification.
300                          */
301                         int r = verity_verify_level(io, io->block + b, 0, true);
302                         if (likely(!r))
303                                 goto test_block_hash;
304                         if (r < 0)
305                                 return r;
306                 }
307
308                 memcpy(io_want_digest(v, io), v->root_digest, v->digest_size);
309
310                 for (i = v->levels - 1; i >= 0; i--) {
311                         int r = verity_verify_level(io, io->block + b, i, false);
312                         if (unlikely(r))
313                                 return r;
314                 }
315
316 test_block_hash:
317                 desc = io_hash_desc(v, io);
318                 desc->tfm = v->tfm;
319                 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
320                 r = crypto_shash_init(desc);
321                 if (r < 0) {
322                         DMERR("crypto_shash_init failed: %d", r);
323                         return r;
324                 }
325
326                 if (likely(v->version >= 1)) {
327                         r = crypto_shash_update(desc, v->salt, v->salt_size);
328                         if (r < 0) {
329                                 DMERR("crypto_shash_update failed: %d", r);
330                                 return r;
331                         }
332                 }
333
334                 todo = 1 << v->data_dev_block_bits;
335                 while (io->iter.bi_size) {
336                         u8 *page;
337                         struct bio_vec bv = bio_iter_iovec(bio, io->iter);
338
339                         page = kmap_atomic(bv.bv_page);
340                         r = crypto_shash_update(desc, page + bv.bv_offset,
341                                                 bv.bv_len);
342                         kunmap_atomic(page);
343
344                         if (r < 0) {
345                                 DMERR("crypto_shash_update failed: %d", r);
346                                 return r;
347                         }
348
349                         bio_advance_iter(bio, &io->iter, bv.bv_len);
350                 }
351
352                 if (!v->version) {
353                         r = crypto_shash_update(desc, v->salt, v->salt_size);
354                         if (r < 0) {
355                                 DMERR("crypto_shash_update failed: %d", r);
356                                 return r;
357                         }
358                 }
359
360                 result = io_real_digest(v, io);
361                 r = crypto_shash_final(desc, result);
362                 if (r < 0) {
363                         DMERR("crypto_shash_final failed: %d", r);
364                         return r;
365                 }
366                 if (unlikely(memcmp(result, io_want_digest(v, io), v->digest_size))) {
367                         DMERR_LIMIT("data block %llu is corrupted",
368                                 (unsigned long long)(io->block + b));
369                         v->hash_failed = 1;
370                         return -EIO;
371                 }
372         }
373
374         return 0;
375 }
376
377 /*
378  * End one "io" structure with a given error.
379  */
380 static void verity_finish_io(struct dm_verity_io *io, int error)
381 {
382         struct dm_verity *v = io->v;
383         struct bio *bio = dm_bio_from_per_bio_data(io, v->ti->per_bio_data_size);
384
385         bio->bi_end_io = io->orig_bi_end_io;
386         bio->bi_private = io->orig_bi_private;
387
388         bio_endio_nodec(bio, error);
389 }
390
391 static void verity_work(struct work_struct *w)
392 {
393         struct dm_verity_io *io = container_of(w, struct dm_verity_io, work);
394
395         verity_finish_io(io, verity_verify_io(io));
396 }
397
398 static void verity_end_io(struct bio *bio, int error)
399 {
400         struct dm_verity_io *io = bio->bi_private;
401
402         if (error) {
403                 verity_finish_io(io, error);
404                 return;
405         }
406
407         INIT_WORK(&io->work, verity_work);
408         queue_work(io->v->verify_wq, &io->work);
409 }
410
411 /*
412  * Prefetch buffers for the specified io.
413  * The root buffer is not prefetched, it is assumed that it will be cached
414  * all the time.
415  */
416 static void verity_prefetch_io(struct work_struct *work)
417 {
418         struct dm_verity_prefetch_work *pw =
419                 container_of(work, struct dm_verity_prefetch_work, work);
420         struct dm_verity *v = pw->v;
421         int i;
422
423         for (i = v->levels - 2; i >= 0; i--) {
424                 sector_t hash_block_start;
425                 sector_t hash_block_end;
426                 verity_hash_at_level(v, pw->block, i, &hash_block_start, NULL);
427                 verity_hash_at_level(v, pw->block + pw->n_blocks - 1, i, &hash_block_end, NULL);
428                 if (!i) {
429                         unsigned cluster = ACCESS_ONCE(dm_verity_prefetch_cluster);
430
431                         cluster >>= v->data_dev_block_bits;
432                         if (unlikely(!cluster))
433                                 goto no_prefetch_cluster;
434
435                         if (unlikely(cluster & (cluster - 1)))
436                                 cluster = 1 << __fls(cluster);
437
438                         hash_block_start &= ~(sector_t)(cluster - 1);
439                         hash_block_end |= cluster - 1;
440                         if (unlikely(hash_block_end >= v->hash_blocks))
441                                 hash_block_end = v->hash_blocks - 1;
442                 }
443 no_prefetch_cluster:
444                 dm_bufio_prefetch(v->bufio, hash_block_start,
445                                   hash_block_end - hash_block_start + 1);
446         }
447
448         kfree(pw);
449 }
450
451 static void verity_submit_prefetch(struct dm_verity *v, struct dm_verity_io *io)
452 {
453         struct dm_verity_prefetch_work *pw;
454
455         pw = kmalloc(sizeof(struct dm_verity_prefetch_work),
456                 GFP_NOIO | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN);
457
458         if (!pw)
459                 return;
460
461         INIT_WORK(&pw->work, verity_prefetch_io);
462         pw->v = v;
463         pw->block = io->block;
464         pw->n_blocks = io->n_blocks;
465         queue_work(v->verify_wq, &pw->work);
466 }
467
468 /*
469  * Bio map function. It allocates dm_verity_io structure and bio vector and
470  * fills them. Then it issues prefetches and the I/O.
471  */
472 static int verity_map(struct dm_target *ti, struct bio *bio)
473 {
474         struct dm_verity *v = ti->private;
475         struct dm_verity_io *io;
476
477         bio->bi_bdev = v->data_dev->bdev;
478         bio->bi_iter.bi_sector = verity_map_sector(v, bio->bi_iter.bi_sector);
479
480         if (((unsigned)bio->bi_iter.bi_sector | bio_sectors(bio)) &
481             ((1 << (v->data_dev_block_bits - SECTOR_SHIFT)) - 1)) {
482                 DMERR_LIMIT("unaligned io");
483                 return -EIO;
484         }
485
486         if (bio_end_sector(bio) >>
487             (v->data_dev_block_bits - SECTOR_SHIFT) > v->data_blocks) {
488                 DMERR_LIMIT("io out of range");
489                 return -EIO;
490         }
491
492         if (bio_data_dir(bio) == WRITE)
493                 return -EIO;
494
495         io = dm_per_bio_data(bio, ti->per_bio_data_size);
496         io->v = v;
497         io->orig_bi_end_io = bio->bi_end_io;
498         io->orig_bi_private = bio->bi_private;
499         io->block = bio->bi_iter.bi_sector >> (v->data_dev_block_bits - SECTOR_SHIFT);
500         io->n_blocks = bio->bi_iter.bi_size >> v->data_dev_block_bits;
501
502         bio->bi_end_io = verity_end_io;
503         bio->bi_private = io;
504         io->iter = bio->bi_iter;
505
506         verity_submit_prefetch(v, io);
507
508         generic_make_request(bio);
509
510         return DM_MAPIO_SUBMITTED;
511 }
512
513 /*
514  * Status: V (valid) or C (corruption found)
515  */
516 static void verity_status(struct dm_target *ti, status_type_t type,
517                           unsigned status_flags, char *result, unsigned maxlen)
518 {
519         struct dm_verity *v = ti->private;
520         unsigned sz = 0;
521         unsigned x;
522
523         switch (type) {
524         case STATUSTYPE_INFO:
525                 DMEMIT("%c", v->hash_failed ? 'C' : 'V');
526                 break;
527         case STATUSTYPE_TABLE:
528                 DMEMIT("%u %s %s %u %u %llu %llu %s ",
529                         v->version,
530                         v->data_dev->name,
531                         v->hash_dev->name,
532                         1 << v->data_dev_block_bits,
533                         1 << v->hash_dev_block_bits,
534                         (unsigned long long)v->data_blocks,
535                         (unsigned long long)v->hash_start,
536                         v->alg_name
537                         );
538                 for (x = 0; x < v->digest_size; x++)
539                         DMEMIT("%02x", v->root_digest[x]);
540                 DMEMIT(" ");
541                 if (!v->salt_size)
542                         DMEMIT("-");
543                 else
544                         for (x = 0; x < v->salt_size; x++)
545                                 DMEMIT("%02x", v->salt[x]);
546                 break;
547         }
548 }
549
550 static int verity_ioctl(struct dm_target *ti, unsigned cmd,
551                         unsigned long arg)
552 {
553         struct dm_verity *v = ti->private;
554         int r = 0;
555
556         if (v->data_start ||
557             ti->len != i_size_read(v->data_dev->bdev->bd_inode) >> SECTOR_SHIFT)
558                 r = scsi_verify_blk_ioctl(NULL, cmd);
559
560         return r ? : __blkdev_driver_ioctl(v->data_dev->bdev, v->data_dev->mode,
561                                      cmd, arg);
562 }
563
564 static int verity_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
565                         struct bio_vec *biovec, int max_size)
566 {
567         struct dm_verity *v = ti->private;
568         struct request_queue *q = bdev_get_queue(v->data_dev->bdev);
569
570         if (!q->merge_bvec_fn)
571                 return max_size;
572
573         bvm->bi_bdev = v->data_dev->bdev;
574         bvm->bi_sector = verity_map_sector(v, bvm->bi_sector);
575
576         return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
577 }
578
579 static int verity_iterate_devices(struct dm_target *ti,
580                                   iterate_devices_callout_fn fn, void *data)
581 {
582         struct dm_verity *v = ti->private;
583
584         return fn(ti, v->data_dev, v->data_start, ti->len, data);
585 }
586
587 static void verity_io_hints(struct dm_target *ti, struct queue_limits *limits)
588 {
589         struct dm_verity *v = ti->private;
590
591         if (limits->logical_block_size < 1 << v->data_dev_block_bits)
592                 limits->logical_block_size = 1 << v->data_dev_block_bits;
593
594         if (limits->physical_block_size < 1 << v->data_dev_block_bits)
595                 limits->physical_block_size = 1 << v->data_dev_block_bits;
596
597         blk_limits_io_min(limits, limits->logical_block_size);
598 }
599
600 static void verity_dtr(struct dm_target *ti)
601 {
602         struct dm_verity *v = ti->private;
603
604         if (v->verify_wq)
605                 destroy_workqueue(v->verify_wq);
606
607         if (v->vec_mempool)
608                 mempool_destroy(v->vec_mempool);
609
610         if (v->bufio)
611                 dm_bufio_client_destroy(v->bufio);
612
613         kfree(v->salt);
614         kfree(v->root_digest);
615
616         if (v->tfm)
617                 crypto_free_shash(v->tfm);
618
619         kfree(v->alg_name);
620
621         if (v->hash_dev)
622                 dm_put_device(ti, v->hash_dev);
623
624         if (v->data_dev)
625                 dm_put_device(ti, v->data_dev);
626
627         kfree(v);
628 }
629
630 /*
631  * Target parameters:
632  *      <version>       The current format is version 1.
633  *                      Vsn 0 is compatible with original Chromium OS releases.
634  *      <data device>
635  *      <hash device>
636  *      <data block size>
637  *      <hash block size>
638  *      <the number of data blocks>
639  *      <hash start block>
640  *      <algorithm>
641  *      <digest>
642  *      <salt>          Hex string or "-" if no salt.
643  */
644 static int verity_ctr(struct dm_target *ti, unsigned argc, char **argv)
645 {
646         struct dm_verity *v;
647         unsigned num;
648         unsigned long long num_ll;
649         int r;
650         int i;
651         sector_t hash_position;
652         char dummy;
653
654         v = kzalloc(sizeof(struct dm_verity), GFP_KERNEL);
655         if (!v) {
656                 ti->error = "Cannot allocate verity structure";
657                 return -ENOMEM;
658         }
659         ti->private = v;
660         v->ti = ti;
661
662         if ((dm_table_get_mode(ti->table) & ~FMODE_READ)) {
663                 ti->error = "Device must be readonly";
664                 r = -EINVAL;
665                 goto bad;
666         }
667
668         if (argc != 10) {
669                 ti->error = "Invalid argument count: exactly 10 arguments required";
670                 r = -EINVAL;
671                 goto bad;
672         }
673
674         if (sscanf(argv[0], "%u%c", &num, &dummy) != 1 ||
675             num > 1) {
676                 ti->error = "Invalid version";
677                 r = -EINVAL;
678                 goto bad;
679         }
680         v->version = num;
681
682         r = dm_get_device(ti, argv[1], FMODE_READ, &v->data_dev);
683         if (r) {
684                 ti->error = "Data device lookup failed";
685                 goto bad;
686         }
687
688         r = dm_get_device(ti, argv[2], FMODE_READ, &v->hash_dev);
689         if (r) {
690                 ti->error = "Data device lookup failed";
691                 goto bad;
692         }
693
694         if (sscanf(argv[3], "%u%c", &num, &dummy) != 1 ||
695             !num || (num & (num - 1)) ||
696             num < bdev_logical_block_size(v->data_dev->bdev) ||
697             num > PAGE_SIZE) {
698                 ti->error = "Invalid data device block size";
699                 r = -EINVAL;
700                 goto bad;
701         }
702         v->data_dev_block_bits = __ffs(num);
703
704         if (sscanf(argv[4], "%u%c", &num, &dummy) != 1 ||
705             !num || (num & (num - 1)) ||
706             num < bdev_logical_block_size(v->hash_dev->bdev) ||
707             num > INT_MAX) {
708                 ti->error = "Invalid hash device block size";
709                 r = -EINVAL;
710                 goto bad;
711         }
712         v->hash_dev_block_bits = __ffs(num);
713
714         if (sscanf(argv[5], "%llu%c", &num_ll, &dummy) != 1 ||
715             (sector_t)(num_ll << (v->data_dev_block_bits - SECTOR_SHIFT))
716             >> (v->data_dev_block_bits - SECTOR_SHIFT) != num_ll) {
717                 ti->error = "Invalid data blocks";
718                 r = -EINVAL;
719                 goto bad;
720         }
721         v->data_blocks = num_ll;
722
723         if (ti->len > (v->data_blocks << (v->data_dev_block_bits - SECTOR_SHIFT))) {
724                 ti->error = "Data device is too small";
725                 r = -EINVAL;
726                 goto bad;
727         }
728
729         if (sscanf(argv[6], "%llu%c", &num_ll, &dummy) != 1 ||
730             (sector_t)(num_ll << (v->hash_dev_block_bits - SECTOR_SHIFT))
731             >> (v->hash_dev_block_bits - SECTOR_SHIFT) != num_ll) {
732                 ti->error = "Invalid hash start";
733                 r = -EINVAL;
734                 goto bad;
735         }
736         v->hash_start = num_ll;
737
738         v->alg_name = kstrdup(argv[7], GFP_KERNEL);
739         if (!v->alg_name) {
740                 ti->error = "Cannot allocate algorithm name";
741                 r = -ENOMEM;
742                 goto bad;
743         }
744
745         v->tfm = crypto_alloc_shash(v->alg_name, 0, 0);
746         if (IS_ERR(v->tfm)) {
747                 ti->error = "Cannot initialize hash function";
748                 r = PTR_ERR(v->tfm);
749                 v->tfm = NULL;
750                 goto bad;
751         }
752         v->digest_size = crypto_shash_digestsize(v->tfm);
753         if ((1 << v->hash_dev_block_bits) < v->digest_size * 2) {
754                 ti->error = "Digest size too big";
755                 r = -EINVAL;
756                 goto bad;
757         }
758         v->shash_descsize =
759                 sizeof(struct shash_desc) + crypto_shash_descsize(v->tfm);
760
761         v->root_digest = kmalloc(v->digest_size, GFP_KERNEL);
762         if (!v->root_digest) {
763                 ti->error = "Cannot allocate root digest";
764                 r = -ENOMEM;
765                 goto bad;
766         }
767         if (strlen(argv[8]) != v->digest_size * 2 ||
768             hex2bin(v->root_digest, argv[8], v->digest_size)) {
769                 ti->error = "Invalid root digest";
770                 r = -EINVAL;
771                 goto bad;
772         }
773
774         if (strcmp(argv[9], "-")) {
775                 v->salt_size = strlen(argv[9]) / 2;
776                 v->salt = kmalloc(v->salt_size, GFP_KERNEL);
777                 if (!v->salt) {
778                         ti->error = "Cannot allocate salt";
779                         r = -ENOMEM;
780                         goto bad;
781                 }
782                 if (strlen(argv[9]) != v->salt_size * 2 ||
783                     hex2bin(v->salt, argv[9], v->salt_size)) {
784                         ti->error = "Invalid salt";
785                         r = -EINVAL;
786                         goto bad;
787                 }
788         }
789
790         v->hash_per_block_bits =
791                 __fls((1 << v->hash_dev_block_bits) / v->digest_size);
792
793         v->levels = 0;
794         if (v->data_blocks)
795                 while (v->hash_per_block_bits * v->levels < 64 &&
796                        (unsigned long long)(v->data_blocks - 1) >>
797                        (v->hash_per_block_bits * v->levels))
798                         v->levels++;
799
800         if (v->levels > DM_VERITY_MAX_LEVELS) {
801                 ti->error = "Too many tree levels";
802                 r = -E2BIG;
803                 goto bad;
804         }
805
806         hash_position = v->hash_start;
807         for (i = v->levels - 1; i >= 0; i--) {
808                 sector_t s;
809                 v->hash_level_block[i] = hash_position;
810                 s = (v->data_blocks + ((sector_t)1 << ((i + 1) * v->hash_per_block_bits)) - 1)
811                                         >> ((i + 1) * v->hash_per_block_bits);
812                 if (hash_position + s < hash_position) {
813                         ti->error = "Hash device offset overflow";
814                         r = -E2BIG;
815                         goto bad;
816                 }
817                 hash_position += s;
818         }
819         v->hash_blocks = hash_position;
820
821         v->bufio = dm_bufio_client_create(v->hash_dev->bdev,
822                 1 << v->hash_dev_block_bits, 1, sizeof(struct buffer_aux),
823                 dm_bufio_alloc_callback, NULL);
824         if (IS_ERR(v->bufio)) {
825                 ti->error = "Cannot initialize dm-bufio";
826                 r = PTR_ERR(v->bufio);
827                 v->bufio = NULL;
828                 goto bad;
829         }
830
831         if (dm_bufio_get_device_size(v->bufio) < v->hash_blocks) {
832                 ti->error = "Hash device is too small";
833                 r = -E2BIG;
834                 goto bad;
835         }
836
837         ti->per_bio_data_size = roundup(sizeof(struct dm_verity_io) + v->shash_descsize + v->digest_size * 2, __alignof__(struct dm_verity_io));
838
839         v->vec_mempool = mempool_create_kmalloc_pool(DM_VERITY_MEMPOOL_SIZE,
840                                         BIO_MAX_PAGES * sizeof(struct bio_vec));
841         if (!v->vec_mempool) {
842                 ti->error = "Cannot allocate vector mempool";
843                 r = -ENOMEM;
844                 goto bad;
845         }
846
847         /* WQ_UNBOUND greatly improves performance when running on ramdisk */
848         v->verify_wq = alloc_workqueue("kverityd", WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM | WQ_UNBOUND, num_online_cpus());
849         if (!v->verify_wq) {
850                 ti->error = "Cannot allocate workqueue";
851                 r = -ENOMEM;
852                 goto bad;
853         }
854
855         return 0;
856
857 bad:
858         verity_dtr(ti);
859
860         return r;
861 }
862
863 static struct target_type verity_target = {
864         .name           = "verity",
865         .version        = {1, 2, 0},
866         .module         = THIS_MODULE,
867         .ctr            = verity_ctr,
868         .dtr            = verity_dtr,
869         .map            = verity_map,
870         .status         = verity_status,
871         .ioctl          = verity_ioctl,
872         .merge          = verity_merge,
873         .iterate_devices = verity_iterate_devices,
874         .io_hints       = verity_io_hints,
875 };
876
877 static int __init dm_verity_init(void)
878 {
879         int r;
880
881         r = dm_register_target(&verity_target);
882         if (r < 0)
883                 DMERR("register failed %d", r);
884
885         return r;
886 }
887
888 static void __exit dm_verity_exit(void)
889 {
890         dm_unregister_target(&verity_target);
891 }
892
893 module_init(dm_verity_init);
894 module_exit(dm_verity_exit);
895
896 MODULE_AUTHOR("Mikulas Patocka <mpatocka@redhat.com>");
897 MODULE_AUTHOR("Mandeep Baines <msb@chromium.org>");
898 MODULE_AUTHOR("Will Drewry <wad@chromium.org>");
899 MODULE_DESCRIPTION(DM_NAME " target for transparent disk integrity checking");
900 MODULE_LICENSE("GPL");