1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
|
/*
* Copyright (C) 2012 Red Hat, Inc.
*
* Author: Mikulas Patocka <mpatocka@redhat.com>
*
* Based on Chromium dm-verity driver (C) 2011 The Chromium OS Authors
*
* This file is released under the GPLv2.
*/
/* Author: Jitesh Shah <j1.shah@sta.samsung.com>
* 27 March 2014.
*
* Updated dm-verity file to include write-able target. dm-dirty.
* Write-able target for dm-verity.
*/
#include "dm-bufio.h"
#include <linux/module.h>
#include <linux/device-mapper.h>
#include <crypto/hash.h>
#include <linux/vmalloc.h>
#include <linux/string.h>
#define REBUILD_TREE 1
/* Copied from dm-verity.c. Name also retained */
#define DM_VERITY_MEMPOOL_SIZE 64
#define DM_VERITY_MAX_LEVELS 63
struct dm_verity {
struct dm_dev *data_dev;
struct dm_dev *hash_dev;
struct dm_target *ti;
struct dm_bufio_client *bufio;
char *alg_name;
struct crypto_shash *tfm;
u8 *root_digest; /* digest of the root block */
u8 *salt; /* salt: its size is salt_size */
unsigned salt_size;
sector_t data_start; /* data offset in 512-byte sectors */
sector_t hash_start; /* hash start in blocks */
sector_t data_blocks; /* the number of data blocks */
sector_t hash_blocks; /* the number of hash blocks */
unsigned char data_dev_block_bits; /* log2(data blocksize) */
unsigned char hash_dev_block_bits; /* log2(hash blocksize) */
unsigned char hash_per_block_bits; /* log2(hashes in hash block) */
unsigned char levels; /* the number of tree levels */
unsigned char version;
unsigned digest_size; /* digest size for the current hash algorithm */
unsigned shash_descsize;/* the size of temporary space for crypto */
int hash_failed; /* set to 1 if hash of any block failed */
mempool_t *io_mempool; /* mempool of struct dm_verity_io */
mempool_t *vec_mempool; /* mempool of bio vector */
struct workqueue_struct *verify_wq;
/* starting blocks for each tree level. 0 is the lowest level. */
sector_t hash_level_block[DM_VERITY_MAX_LEVELS];
};
static sector_t verity_position_at_level(struct dm_verity *v, sector_t block,
int level)
{
return block >> (level * v->hash_per_block_bits);
}
static void verity_hash_at_level(struct dm_verity *v, sector_t block, int level,
sector_t *hash_block, unsigned *offset)
{
sector_t position = verity_position_at_level(v, block, level);
unsigned idx;
*hash_block = v->hash_level_block[level] + (position >> v->hash_per_block_bits);
if (!offset)
return;
idx = position & ((1 << v->hash_per_block_bits) - 1);
if (!v->version)
*offset = idx * v->digest_size;
else
*offset = idx << (v->hash_dev_block_bits - v->hash_per_block_bits);
}
/* End of copied stuff */
struct dm_dirty_io {
struct dm_verity *v;
sector_t block;
unsigned int size;
void *orig_bi_private;
struct bio *bio;
bio_end_io_t *orig_bi_end_io;
struct work_struct work;
unsigned short idx;
};
#define DM_MSG_PREFIX "dirty"
static void dirty_finish_io(struct dm_dirty_io *io, struct bio *bio, int error)
{
struct dm_verity *v = io->v;
bio->bi_end_io = io->orig_bi_end_io;
bio->bi_private = io->orig_bi_private;
mempool_free(io, v->io_mempool);
bio_endio(bio, error);
}
static void dirty_hash(struct bio *bio, int error)
{
struct bio_vec *iovec;
int i, ret;
u8* page;
u8* result;
struct shash_desc *desc;
struct dm_dirty_io *io = bio->bi_private;
struct dm_buffer *buf = NULL;
u8* hash_block_buf;
sector_t hash_block;
unsigned offset;
struct dm_verity *v = io->v;
unsigned short idx = io->idx;
/* At this point only WRITE bios and non-error bios are allowed.
* bio_data_dir() and error should have been checked before.
*/
/* TODO: Assumption here is that the FS sends block sized buffers. This seems to be
* true for ext4 but should not be assumed. Re-factor this code to remove that
* assumption.
*/
for(i = 0; i < (io->size >> v->data_dev_block_bits); i++) {
/* Loop over all blocks */
verity_hash_at_level(v, io->block + (sector_t)i, 0, &hash_block, &offset);
hash_block_buf = dm_bufio_read(v->bufio, hash_block, &buf);
if (unlikely(IS_ERR(hash_block_buf))) {
DMERR("Error getting hash block");
buf = NULL;
goto free_all_res;
}
desc = (struct shash_desc *)(io + 1);
desc->tfm = v->tfm;
desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
ret = crypto_shash_init(desc);
if (ret < 0) {
DMERR("Error initing hash");
goto free_all_res;
}
ret = crypto_shash_update(desc, v->salt, v->salt_size);
if (ret < 0) {
DMERR("Error updating salt hash");
goto free_all_res;
}
iovec = bio_iovec_idx(bio, idx + i);
if (idx + i >= bio->bi_vcnt) {
DMERR("Excedding vcnt: idx:%hu vcnt:%hu i:%d", idx, bio->bi_vcnt, i);
goto free_all_res;
}
page = kmap_atomic(iovec->bv_page);
if(page == NULL) {
DMERR("Failed to kmap dirty page");
goto free_bufio;
}
if(iovec->bv_len != (1 << v->data_dev_block_bits)) {
DMERR("Non-page data len: %u offset: %u", iovec->bv_len, iovec->bv_offset);
}
ret = crypto_shash_update(desc, page + iovec->bv_offset, iovec->bv_len);
if (ret < 0) {
DMERR("Error updating data hash");
}
kunmap_atomic(page);
result = hash_block_buf + offset;
ret = crypto_shash_final(desc, result);
if (ret < 0) {
DMERR("Error finalizing hash");
}
if (0 == io->block + (sector_t)i) {
// for data block 0, we use dummy hash
memset(result, 1, v->digest_size);
}
dm_bufio_mark_buffer_dirty(buf);
free_bufio:
dm_bufio_release(buf);
buf = NULL;
}
free_all_res:
if (buf)
dm_bufio_release(buf);
dirty_finish_io(io, bio, error);
}
static void dirty_work(struct work_struct *w)
{
struct dm_dirty_io *io = container_of(w, struct dm_dirty_io, work);
dirty_hash(io->bio, 0);
}
static void dirty_end_io(struct bio *bio, int error)
{
struct dm_dirty_io *io = bio->bi_private;
if (error) {
/* If there was an error writing the data, do not recalculate the hash.
* Just go ahead and finish the bio
*/
dirty_finish_io(io, bio, error);
return;
}
INIT_WORK(&io->work, dirty_work);
queue_work(io->v->verify_wq, &io->work);
}
static inline u64 sector_to_block(struct dm_verity *v, u64 sector)
{
return (sector >> (v->data_dev_block_bits - SECTOR_SHIFT));
}
static inline unsigned bytes_to_block(struct dm_verity *v, unsigned bytes)
{
return (bytes >> v->data_dev_block_bits);
}
static int dirty_map(struct dm_target *ti, struct bio *bio)
{
struct dm_dirty_io *io;
struct dm_verity *v = ti->private;
/* If a WRITE coems with FLUSH flag set, we don't need to do anything special.
* Its not the end of the world if the hash is not updated to the disk right
* away.
*/
if (bio_data_dir(bio) != WRITE || bio->bi_size == 0)
goto process_bio;
if (bio->bi_size & ((1 << v->data_dev_block_bits) - 1) ||
(bio->bi_sector & ((1 << (v->data_dev_block_bits - SECTOR_SHIFT)) - 1))) {
DMERR("Not page size IO: size:%u, sector:%llu", bio->bi_size, (long long unsigned int)bio->bi_sector);
return -EIO;
}
if(sector_to_block(v, bio->bi_sector) + bytes_to_block(v, bio->bi_size) > v->data_blocks) {
DMERR("Out of range. sector:%llu, size:%u", (long long unsigned int)bio->bi_sector, bio->bi_size);
return -EIO;
}
/* Allocate an io structure and save required info */
io = mempool_alloc(v->io_mempool, GFP_NOIO);
io->v = v;
io->bio = bio;
io->orig_bi_end_io = bio->bi_end_io;
io->orig_bi_private = bio->bi_private;
/* When bio is done. bi_sector points to offset from start of device, NOT the partition
* Size is changed to 0 (the residual size). so save these values beforehand.
*/
io->block = bio->bi_sector >> (v->data_dev_block_bits - SECTOR_SHIFT);
io->size = bio->bi_size;
io->idx = bio->bi_idx;
/* Link io to bio structure now and assign an end function */
bio->bi_private = io;
bio->bi_end_io = dirty_end_io;
process_bio:
/* Submit bio now */
bio->bi_bdev = v->data_dev->bdev;
submit_bio(bio->bi_rw, bio);
return DM_MAPIO_SUBMITTED;
}
static sector_t dirty_nr_blocks_at_level(struct dm_verity *v, int level)
{
if (level == 0) {
return v->hash_blocks - v->hash_level_block[0];
} else
return v->hash_level_block[level - 1] - v->hash_level_block[level];
}
static void dirty_one_level_up(struct dm_verity *v, sector_t block, int source_level, sector_t *hash_block, unsigned *offset)
{
sector_t block_offset;
block_offset = block - v->hash_level_block[source_level];
*hash_block = v->hash_level_block[source_level + 1] + (block_offset >> v->hash_per_block_bits);
*offset = (block_offset & ((1 << v->hash_per_block_bits)-1)) << (v->hash_dev_block_bits - v->hash_per_block_bits);
}
static int dirty_update_hash(struct dm_verity *v, sector_t block, struct shash_desc *desc, u8 *result)
{
struct dm_buffer *buf = NULL;
u8* hash_block_buf;
int ret = -1;
hash_block_buf = dm_bufio_read(v->bufio, block, &buf);
if (unlikely(IS_ERR(hash_block_buf))) {
DMERR("Error getting hash block in dtr");
buf = NULL;
return ret;
}
desc->tfm = v->tfm;
desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
ret = crypto_shash_init(desc);
if (ret < 0) {
DMERR("Error initing hash");
goto free_all_res;
}
ret = crypto_shash_update(desc, v->salt, v->salt_size);
if (ret < 0) {
DMERR("Error updating salt hash");
goto free_all_res;
}
/* Sadly assumption is hash_block size = 4096. FIX THIS */
ret = crypto_shash_update(desc, hash_block_buf, 1 << v->hash_dev_block_bits);
if (ret < 0) {
DMERR("Error updating hash");
goto free_all_res;
}
ret = crypto_shash_final(desc, result);
if (ret < 0) {
DMERR("Error finalizing hash");
goto free_all_res;
}
ret = 0;
free_all_res:
dm_bufio_release(buf);
return ret;
}
static int finish_verity_tree(struct dm_verity *v)
{
sector_t hash_block, nr_blocks, block_num;
unsigned offset;
int i, ret = -1;
struct shash_desc *desc;
struct dm_buffer *buf = NULL;
u8* hash_block_buf;
u8 *result;
desc = kmalloc(sizeof(struct shash_desc) + crypto_shash_descsize(v->tfm) + v->digest_size, GFP_KERNEL);
if(NULL == desc) {
DMERR("Error allocating desc mem");
return ret;
}
result = (u8*)desc + sizeof(struct shash_desc) + crypto_shash_descsize(v->tfm);
for(i = 1; i < v->levels; i++) {
nr_blocks = dirty_nr_blocks_at_level(v, i - 1);
for(block_num = v->hash_level_block[i - 1]; nr_blocks > 0; nr_blocks--,block_num++) {
dirty_one_level_up(v, block_num, i - 1, &hash_block, &offset);
if (dirty_update_hash(v, block_num, desc, result)) {
DMERR("Error calculating hash for block %llu", (long long unsigned int)block_num);
goto free_desc;
}
hash_block_buf = dm_bufio_read(v->bufio, hash_block, &buf);
if (unlikely(IS_ERR(hash_block_buf))) {
DMERR("Error getting hash block %llu in dtr", (long long unsigned int)hash_block);
buf = NULL;
goto free_desc;
}
memcpy(hash_block_buf + offset, result, v->digest_size);
dm_bufio_mark_buffer_dirty(buf);
dm_bufio_release(buf);
}
}
/* Get last level node */
hash_block = v->hash_level_block[v->levels - 1];
/* Calculate root hash */
if(dirty_update_hash(v, hash_block, desc, result)) {
DMERR("Failed to calculate root hash. Block: %llu", (long long unsigned int)hash_block);
goto free_desc;
}
memcpy(v->root_digest, result, v->digest_size);
ret = 0;
free_desc:
kfree(desc);
return ret;
}
static void dirty_status(struct dm_target *ti, status_type_t type,
unsigned status_flags, char *result, unsigned maxlen)
{
return;
}
static int dirty_ioctl(struct dm_target *ti, unsigned cmd,
unsigned long arg)
{
struct dm_verity *v = ti->private;
struct {
unsigned int size;
int ret;
} output;
switch(cmd) {
case REBUILD_TREE:
if(__copy_from_user(&output.size, (void *)arg, sizeof(unsigned int))) {
DMINFO("Error in copy_from_user");
}
if (output.size < v->digest_size) {
DMERR("Allocated size (%u) less than root digest size (%u)", output.size,
v->digest_size);
output.size = 0;
output.ret = -1;
goto make_output;
}
/* All checks done. Lets finalize the hash */
if (finish_verity_tree(v)) {
output.ret = -1;
goto make_output;
}
output.size = v->digest_size;
output.ret = 0;
if(copy_to_user((void *)(arg + sizeof(output)), v->root_digest, v->digest_size)) {
DMERR("Failed to copy root digest");
output.ret = -1;
}
make_output:
if(copy_to_user((void *)arg, &output, sizeof(output)))
DMERR("Failed to copy to user");
break;
default:
DMERR("Unknown cmd: %u", cmd);
}
return 0;
}
static void dirty_dtr(struct dm_target *ti)
{
struct dm_verity *v = ti->private;
DMINFO("dirty destructor called");
if (v->verify_wq)
destroy_workqueue(v->verify_wq);
if (v->io_mempool)
mempool_destroy(v->io_mempool);
if (v->bufio)
dm_bufio_write_dirty_buffers(v->bufio);
if (v->bufio)
dm_bufio_client_destroy(v->bufio);
kfree(v->salt);
kfree(v->root_digest);
if (v->tfm)
crypto_free_shash(v->tfm);
kfree(v->alg_name);
if (v->hash_dev)
dm_put_device(ti, v->hash_dev);
if (v->data_dev)
dm_put_device(ti, v->data_dev);
kfree(v);
}
static int dirty_ctr(struct dm_target *ti, unsigned argc, char **argv)
{
struct dm_verity *v;
unsigned num;
unsigned long long num_ll;
int r;
int i;
sector_t hash_position;
u32 nr_hash_blocks;
char dummy;
v = kzalloc(sizeof(struct dm_verity), GFP_KERNEL);
if (!v) {
ti->error = "Cannot allocate verity structure";
return -ENOMEM;
}
ti->private = v;
v->ti = ti;
if (argc != 10) {
ti->error = "Invalid argument count: exactly 10 arguments required";
r = -EINVAL;
goto bad;
}
if (sscanf(argv[0], "%d%c", &num, &dummy) != 1 ||
num > 1) {
ti->error = "Invalid version";
r = -EINVAL;
goto bad;
}
v->version = num;
r = dm_get_device(ti, argv[1], FMODE_READ | FMODE_WRITE, &v->data_dev);
if (r) {
ti->error = "Data device lookup failed";
goto bad;
}
r = dm_get_device(ti, argv[2], FMODE_READ | FMODE_WRITE, &v->hash_dev);
if (r) {
ti->error = "Data device lookup failed";
goto bad;
}
if (sscanf(argv[3], "%u%c", &num, &dummy) != 1 ||
!num || (num & (num - 1)) ||
num < bdev_logical_block_size(v->data_dev->bdev) ||
num > PAGE_SIZE) {
ti->error = "Invalid data device block size";
r = -EINVAL;
goto bad;
}
v->data_dev_block_bits = ffs(num) - 1;
if (sscanf(argv[4], "%u%c", &num, &dummy) != 1 ||
!num || (num & (num - 1)) ||
num < bdev_logical_block_size(v->hash_dev->bdev) ||
num > INT_MAX) {
ti->error = "Invalid hash device block size";
r = -EINVAL;
goto bad;
}
v->hash_dev_block_bits = ffs(num) - 1;
if (sscanf(argv[5], "%llu%c", &num_ll, &dummy) != 1 ||
(sector_t)(num_ll << (v->data_dev_block_bits - SECTOR_SHIFT))
>> (v->data_dev_block_bits - SECTOR_SHIFT) != num_ll) {
ti->error = "Invalid data blocks";
r = -EINVAL;
goto bad;
}
v->data_blocks = num_ll;
if (ti->len > (v->data_blocks << (v->data_dev_block_bits - SECTOR_SHIFT))) {
ti->error = "Data device is too small";
r = -EINVAL;
goto bad;
}
if (sscanf(argv[6], "%llu%c", &num_ll, &dummy) != 1 ||
(sector_t)(num_ll << (v->hash_dev_block_bits - SECTOR_SHIFT))
>> (v->hash_dev_block_bits - SECTOR_SHIFT) != num_ll) {
ti->error = "Invalid hash start";
r = -EINVAL;
goto bad;
}
v->hash_start = num_ll;
v->alg_name = kstrdup(argv[7], GFP_KERNEL);
if (!v->alg_name) {
ti->error = "Cannot allocate algorithm name";
r = -ENOMEM;
goto bad;
}
v->tfm = crypto_alloc_shash(v->alg_name, 0, 0);
if (IS_ERR(v->tfm)) {
ti->error = "Cannot initialize hash function";
r = PTR_ERR(v->tfm);
v->tfm = NULL;
goto bad;
}
v->digest_size = crypto_shash_digestsize(v->tfm);
if ((1 << v->hash_dev_block_bits) < v->digest_size * 2) {
ti->error = "Digest size too big";
r = -EINVAL;
goto bad;
}
v->shash_descsize =
sizeof(struct shash_desc) + crypto_shash_descsize(v->tfm);
v->root_digest = kmalloc(v->digest_size, GFP_KERNEL);
if (!v->root_digest) {
ti->error = "Cannot allocate root digest";
r = -ENOMEM;
goto bad;
}
if (strlen(argv[8]) != v->digest_size * 2 ||
hex2bin(v->root_digest, argv[8], v->digest_size)) {
ti->error = "Invalid root digest";
r = -EINVAL;
goto bad;
}
if (strcmp(argv[9], "-")) {
v->salt_size = strlen(argv[9]) / 2;
v->salt = kmalloc(v->salt_size, GFP_KERNEL);
if (!v->salt) {
ti->error = "Cannot allocate salt";
r = -ENOMEM;
goto bad;
}
if (strlen(argv[9]) != v->salt_size * 2 ||
hex2bin(v->salt, argv[9], v->salt_size)) {
ti->error = "Invalid salt";
r = -EINVAL;
goto bad;
}
}
v->hash_per_block_bits =
fls((1 << v->hash_dev_block_bits) / v->digest_size) - 1;
v->levels = 0;
if (v->data_blocks)
while (v->hash_per_block_bits * v->levels < 64 &&
(unsigned long long)(v->data_blocks - 1) >>
(v->hash_per_block_bits * v->levels))
v->levels++;
if (v->levels > DM_VERITY_MAX_LEVELS) {
ti->error = "Too many tree levels";
r = -E2BIG;
goto bad;
}
hash_position = v->hash_start;
for (i = v->levels - 1; i >= 0; i--) {
sector_t s;
v->hash_level_block[i] = hash_position;
s = verity_position_at_level(v, v->data_blocks, i);
s = (s >> v->hash_per_block_bits) +
!!(s & ((1 << v->hash_per_block_bits) - 1));
if (hash_position + s < hash_position) {
ti->error = "Hash device offset overflow";
r = -E2BIG;
goto bad;
}
hash_position += s;
}
v->hash_blocks = hash_position;
/* Following typecast will only work for Small samsung disks */
nr_hash_blocks = (u32)(hash_position - v->hash_start);
v->bufio = dm_bufio_client_create(v->hash_dev->bdev,
1 << v->hash_dev_block_bits, nr_hash_blocks, 0,
NULL, NULL);
if (IS_ERR(v->bufio)) {
ti->error = "Cannot initialize dm-bufio";
r = PTR_ERR(v->bufio);
v->bufio = NULL;
goto bad;
}
if (dm_bufio_get_device_size(v->bufio) < v->hash_blocks) {
ti->error = "Hash device is too small";
r = -E2BIG;
goto bad;
}
/* We have reserved nr_hash_blocks. Prefetch all */
dm_bufio_prefetch(v->bufio, v->hash_start, nr_hash_blocks);
v->io_mempool = mempool_create_kmalloc_pool(DM_VERITY_MEMPOOL_SIZE,
sizeof(struct dm_dirty_io) + v->shash_descsize + v->digest_size);
if (!v->io_mempool) {
ti->error = "Cannot allocate io mempool";
r = -ENOMEM;
goto bad;
}
/* WQ_UNBOUND greatly improves performance when running on ramdisk */
v->verify_wq = alloc_workqueue("kdirtyd", WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM | WQ_UNBOUND, 1 /* num_online_cpus() */);
if (!v->verify_wq) {
ti->error = "Cannot allocate workqueue";
r = -ENOMEM;
goto bad;
}
DMINFO("dirty constructor succeeded\n");
return 0;
bad:
dirty_dtr(ti);
return r;
}
static int dirty_iterate_devices(struct dm_target *ti,
iterate_devices_callout_fn fn, void *data)
{
struct dm_verity *v = ti->private;
return fn(ti, v->data_dev, v->data_start, ti->len, data);
}
static void dirty_io_hints(struct dm_target *ti, struct queue_limits *limits)
{
struct dm_verity *v = ti->private;
if (limits->logical_block_size < 1 << v->data_dev_block_bits) {
limits->logical_block_size = 1 << v->data_dev_block_bits;
}
if (limits->physical_block_size < 1 << v->data_dev_block_bits) {
limits->physical_block_size = 1 << v->data_dev_block_bits;
}
blk_limits_io_min(limits, limits->logical_block_size);
}
static struct target_type dirty_target = {
.name = "dirty",
.version = {1, 0, 0},
.module = THIS_MODULE,
.ctr = dirty_ctr,
.dtr = dirty_dtr,
.map = dirty_map,
.status = dirty_status,
.ioctl = dirty_ioctl,
.iterate_devices = dirty_iterate_devices,
.io_hints = dirty_io_hints,
};
static int __init dm_dirty_init(void)
{
int ret;
ret = dm_register_target(&dirty_target);
if (ret < 0)
DMERR("dirty register failed %d", ret);
return ret;
}
static void __exit dm_dirty_exit(void)
{
dm_unregister_target(&dirty_target);
}
module_init(dm_dirty_init);
module_exit(dm_dirty_exit);
MODULE_AUTHOR("Jitesh Shah <j1.shah@sta.samsung.com>");
MODULE_DESCRIPTION(DM_NAME " update target for dm-verity");
MODULE_LICENSE("GPL");
|