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