]>
Commit | Line | Data |
---|---|---|
a4ffc152 MP |
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 | ||
ffa39380 | 17 | #include "dm-verity.h" |
a739ff3f | 18 | #include "dm-verity-fec.h" |
a4ffc152 MP |
19 | |
20 | #include <linux/module.h> | |
65ff5b7d | 21 | #include <linux/reboot.h> |
a4ffc152 MP |
22 | |
23 | #define DM_MSG_PREFIX "verity" | |
24 | ||
65ff5b7d ST |
25 | #define DM_VERITY_ENV_LENGTH 42 |
26 | #define DM_VERITY_ENV_VAR_NAME "DM_VERITY_ERR_BLOCK_NR" | |
27 | ||
a4ffc152 MP |
28 | #define DM_VERITY_DEFAULT_PREFETCH_SIZE 262144 |
29 | ||
65ff5b7d ST |
30 | #define DM_VERITY_MAX_CORRUPTED_ERRS 100 |
31 | ||
32 | #define DM_VERITY_OPT_LOGGING "ignore_corruption" | |
33 | #define DM_VERITY_OPT_RESTART "restart_on_corruption" | |
0cc37c2d | 34 | #define DM_VERITY_OPT_IGN_ZEROES "ignore_zero_blocks" |
a4ffc152 | 35 | |
0cc37c2d | 36 | #define DM_VERITY_OPTS_MAX (2 + DM_VERITY_OPTS_FEC) |
753c1fd0 | 37 | |
a4ffc152 MP |
38 | static unsigned dm_verity_prefetch_cluster = DM_VERITY_DEFAULT_PREFETCH_SIZE; |
39 | ||
40 | module_param_named(prefetch_cluster, dm_verity_prefetch_cluster, uint, S_IRUGO | S_IWUSR); | |
41 | ||
3b6b7813 MP |
42 | struct dm_verity_prefetch_work { |
43 | struct work_struct work; | |
44 | struct dm_verity *v; | |
45 | sector_t block; | |
46 | unsigned n_blocks; | |
47 | }; | |
48 | ||
a4ffc152 MP |
49 | /* |
50 | * Auxiliary structure appended to each dm-bufio buffer. If the value | |
51 | * hash_verified is nonzero, hash of the block has been verified. | |
52 | * | |
53 | * The variable hash_verified is set to 0 when allocating the buffer, then | |
54 | * it can be changed to 1 and it is never reset to 0 again. | |
55 | * | |
56 | * There is no lock around this value, a race condition can at worst cause | |
57 | * that multiple processes verify the hash of the same buffer simultaneously | |
58 | * and write 1 to hash_verified simultaneously. | |
59 | * This condition is harmless, so we don't need locking. | |
60 | */ | |
61 | struct buffer_aux { | |
62 | int hash_verified; | |
63 | }; | |
64 | ||
65 | /* | |
66 | * Initialize struct buffer_aux for a freshly created buffer. | |
67 | */ | |
68 | static void dm_bufio_alloc_callback(struct dm_buffer *buf) | |
69 | { | |
70 | struct buffer_aux *aux = dm_bufio_get_aux_data(buf); | |
71 | ||
72 | aux->hash_verified = 0; | |
73 | } | |
74 | ||
75 | /* | |
76 | * Translate input sector number to the sector number on the target device. | |
77 | */ | |
78 | static sector_t verity_map_sector(struct dm_verity *v, sector_t bi_sector) | |
79 | { | |
80 | return v->data_start + dm_target_offset(v->ti, bi_sector); | |
81 | } | |
82 | ||
83 | /* | |
84 | * Return hash position of a specified block at a specified tree level | |
85 | * (0 is the lowest level). | |
86 | * The lowest "hash_per_block_bits"-bits of the result denote hash position | |
87 | * inside a hash block. The remaining bits denote location of the hash block. | |
88 | */ | |
89 | static sector_t verity_position_at_level(struct dm_verity *v, sector_t block, | |
90 | int level) | |
91 | { | |
92 | return block >> (level * v->hash_per_block_bits); | |
93 | } | |
94 | ||
d1ac3ff0 GBY |
95 | static int verity_hash_update(struct dm_verity *v, struct ahash_request *req, |
96 | const u8 *data, size_t len, | |
12f1ffc4 | 97 | struct crypto_wait *wait) |
6dbeda34 | 98 | { |
d1ac3ff0 | 99 | struct scatterlist sg; |
6dbeda34 | 100 | |
92d36a96 MP |
101 | if (likely(!is_vmalloc_addr(data))) { |
102 | sg_init_one(&sg, data, len); | |
103 | ahash_request_set_crypt(req, &sg, NULL, len); | |
104 | return crypto_wait_req(crypto_ahash_update(req), wait); | |
105 | } else { | |
106 | do { | |
107 | int r; | |
108 | size_t this_step = min_t(size_t, len, PAGE_SIZE - offset_in_page(data)); | |
109 | flush_kernel_vmap_range((void *)data, this_step); | |
110 | sg_init_table(&sg, 1); | |
111 | sg_set_page(&sg, vmalloc_to_page(data), this_step, offset_in_page(data)); | |
112 | ahash_request_set_crypt(req, &sg, NULL, this_step); | |
113 | r = crypto_wait_req(crypto_ahash_update(req), wait); | |
114 | if (unlikely(r)) | |
115 | return r; | |
116 | data += this_step; | |
117 | len -= this_step; | |
118 | } while (len); | |
119 | return 0; | |
120 | } | |
d1ac3ff0 GBY |
121 | } |
122 | ||
123 | /* | |
124 | * Wrapper for crypto_ahash_init, which handles verity salting. | |
125 | */ | |
126 | static int verity_hash_init(struct dm_verity *v, struct ahash_request *req, | |
12f1ffc4 | 127 | struct crypto_wait *wait) |
d1ac3ff0 GBY |
128 | { |
129 | int r; | |
130 | ||
131 | ahash_request_set_tfm(req, v->tfm); | |
132 | ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP | | |
133 | CRYPTO_TFM_REQ_MAY_BACKLOG, | |
12f1ffc4 GBY |
134 | crypto_req_done, (void *)wait); |
135 | crypto_init_wait(wait); | |
d1ac3ff0 | 136 | |
12f1ffc4 | 137 | r = crypto_wait_req(crypto_ahash_init(req), wait); |
d1ac3ff0 GBY |
138 | |
139 | if (unlikely(r < 0)) { | |
140 | DMERR("crypto_ahash_init failed: %d", r); | |
141 | return r; | |
142 | } | |
143 | ||
f52236e0 | 144 | if (likely(v->salt_size && (v->version >= 1))) |
12f1ffc4 | 145 | r = verity_hash_update(v, req, v->salt, v->salt_size, wait); |
6dbeda34 ST |
146 | |
147 | return r; | |
148 | } | |
149 | ||
d1ac3ff0 | 150 | static int verity_hash_final(struct dm_verity *v, struct ahash_request *req, |
12f1ffc4 | 151 | u8 *digest, struct crypto_wait *wait) |
6dbeda34 ST |
152 | { |
153 | int r; | |
154 | ||
f52236e0 | 155 | if (unlikely(v->salt_size && (!v->version))) { |
12f1ffc4 | 156 | r = verity_hash_update(v, req, v->salt, v->salt_size, wait); |
6dbeda34 ST |
157 | |
158 | if (r < 0) { | |
d1ac3ff0 GBY |
159 | DMERR("verity_hash_final failed updating salt: %d", r); |
160 | goto out; | |
6dbeda34 ST |
161 | } |
162 | } | |
163 | ||
d1ac3ff0 | 164 | ahash_request_set_crypt(req, NULL, digest, 0); |
12f1ffc4 | 165 | r = crypto_wait_req(crypto_ahash_final(req), wait); |
d1ac3ff0 | 166 | out: |
6dbeda34 ST |
167 | return r; |
168 | } | |
169 | ||
d1ac3ff0 | 170 | int verity_hash(struct dm_verity *v, struct ahash_request *req, |
ffa39380 | 171 | const u8 *data, size_t len, u8 *digest) |
6dbeda34 ST |
172 | { |
173 | int r; | |
12f1ffc4 | 174 | struct crypto_wait wait; |
6dbeda34 | 175 | |
12f1ffc4 | 176 | r = verity_hash_init(v, req, &wait); |
6dbeda34 | 177 | if (unlikely(r < 0)) |
d1ac3ff0 | 178 | goto out; |
6dbeda34 | 179 | |
12f1ffc4 | 180 | r = verity_hash_update(v, req, data, len, &wait); |
6dbeda34 | 181 | if (unlikely(r < 0)) |
d1ac3ff0 GBY |
182 | goto out; |
183 | ||
12f1ffc4 | 184 | r = verity_hash_final(v, req, digest, &wait); |
6dbeda34 | 185 | |
d1ac3ff0 GBY |
186 | out: |
187 | return r; | |
6dbeda34 ST |
188 | } |
189 | ||
a4ffc152 MP |
190 | static void verity_hash_at_level(struct dm_verity *v, sector_t block, int level, |
191 | sector_t *hash_block, unsigned *offset) | |
192 | { | |
193 | sector_t position = verity_position_at_level(v, block, level); | |
194 | unsigned idx; | |
195 | ||
196 | *hash_block = v->hash_level_block[level] + (position >> v->hash_per_block_bits); | |
197 | ||
198 | if (!offset) | |
199 | return; | |
200 | ||
201 | idx = position & ((1 << v->hash_per_block_bits) - 1); | |
202 | if (!v->version) | |
203 | *offset = idx * v->digest_size; | |
204 | else | |
205 | *offset = idx << (v->hash_dev_block_bits - v->hash_per_block_bits); | |
206 | } | |
207 | ||
65ff5b7d ST |
208 | /* |
209 | * Handle verification errors. | |
210 | */ | |
211 | static int verity_handle_err(struct dm_verity *v, enum verity_block_type type, | |
212 | unsigned long long block) | |
213 | { | |
214 | char verity_env[DM_VERITY_ENV_LENGTH]; | |
215 | char *envp[] = { verity_env, NULL }; | |
216 | const char *type_str = ""; | |
217 | struct mapped_device *md = dm_table_get_md(v->ti->table); | |
218 | ||
219 | /* Corruption should be visible in device status in all modes */ | |
220 | v->hash_failed = 1; | |
221 | ||
222 | if (v->corrupted_errs >= DM_VERITY_MAX_CORRUPTED_ERRS) | |
223 | goto out; | |
224 | ||
225 | v->corrupted_errs++; | |
226 | ||
227 | switch (type) { | |
228 | case DM_VERITY_BLOCK_TYPE_DATA: | |
229 | type_str = "data"; | |
230 | break; | |
231 | case DM_VERITY_BLOCK_TYPE_METADATA: | |
232 | type_str = "metadata"; | |
233 | break; | |
234 | default: | |
235 | BUG(); | |
236 | } | |
237 | ||
175fc937 MB |
238 | DMERR_LIMIT("%s: %s block %llu is corrupted", v->data_dev->name, |
239 | type_str, block); | |
65ff5b7d ST |
240 | |
241 | if (v->corrupted_errs == DM_VERITY_MAX_CORRUPTED_ERRS) | |
242 | DMERR("%s: reached maximum errors", v->data_dev->name); | |
243 | ||
244 | snprintf(verity_env, DM_VERITY_ENV_LENGTH, "%s=%d,%llu", | |
245 | DM_VERITY_ENV_VAR_NAME, type, block); | |
246 | ||
247 | kobject_uevent_env(&disk_to_dev(dm_disk(md))->kobj, KOBJ_CHANGE, envp); | |
248 | ||
249 | out: | |
250 | if (v->mode == DM_VERITY_MODE_LOGGING) | |
251 | return 0; | |
252 | ||
253 | if (v->mode == DM_VERITY_MODE_RESTART) | |
254 | kernel_restart("dm-verity device corrupted"); | |
255 | ||
256 | return 1; | |
257 | } | |
258 | ||
a4ffc152 MP |
259 | /* |
260 | * Verify hash of a metadata block pertaining to the specified data block | |
261 | * ("block" argument) at a specified level ("level" argument). | |
262 | * | |
ffa39380 ST |
263 | * On successful return, verity_io_want_digest(v, io) contains the hash value |
264 | * for a lower tree level or for the data block (if we're at the lowest level). | |
a4ffc152 MP |
265 | * |
266 | * If "skip_unverified" is true, unverified buffer is skipped and 1 is returned. | |
267 | * If "skip_unverified" is false, unverified buffer is hashed and verified | |
ffa39380 | 268 | * against current value of verity_io_want_digest(v, io). |
a4ffc152 | 269 | */ |
6dbeda34 ST |
270 | static int verity_verify_level(struct dm_verity *v, struct dm_verity_io *io, |
271 | sector_t block, int level, bool skip_unverified, | |
272 | u8 *want_digest) | |
a4ffc152 | 273 | { |
a4ffc152 MP |
274 | struct dm_buffer *buf; |
275 | struct buffer_aux *aux; | |
276 | u8 *data; | |
277 | int r; | |
278 | sector_t hash_block; | |
279 | unsigned offset; | |
280 | ||
281 | verity_hash_at_level(v, block, level, &hash_block, &offset); | |
282 | ||
283 | data = dm_bufio_read(v->bufio, hash_block, &buf); | |
fc0a4461 | 284 | if (IS_ERR(data)) |
a4ffc152 MP |
285 | return PTR_ERR(data); |
286 | ||
287 | aux = dm_bufio_get_aux_data(buf); | |
288 | ||
289 | if (!aux->hash_verified) { | |
a4ffc152 MP |
290 | if (skip_unverified) { |
291 | r = 1; | |
292 | goto release_ret_r; | |
293 | } | |
294 | ||
d1ac3ff0 | 295 | r = verity_hash(v, verity_io_hash_req(v, io), |
6dbeda34 | 296 | data, 1 << v->hash_dev_block_bits, |
ffa39380 | 297 | verity_io_real_digest(v, io)); |
6dbeda34 | 298 | if (unlikely(r < 0)) |
a4ffc152 | 299 | goto release_ret_r; |
a4ffc152 | 300 | |
ffa39380 | 301 | if (likely(memcmp(verity_io_real_digest(v, io), want_digest, |
6dbeda34 ST |
302 | v->digest_size) == 0)) |
303 | aux->hash_verified = 1; | |
a739ff3f ST |
304 | else if (verity_fec_decode(v, io, |
305 | DM_VERITY_BLOCK_TYPE_METADATA, | |
306 | hash_block, data, NULL) == 0) | |
307 | aux->hash_verified = 1; | |
6dbeda34 ST |
308 | else if (verity_handle_err(v, |
309 | DM_VERITY_BLOCK_TYPE_METADATA, | |
310 | hash_block)) { | |
311 | r = -EIO; | |
a4ffc152 MP |
312 | goto release_ret_r; |
313 | } | |
a4ffc152 MP |
314 | } |
315 | ||
316 | data += offset; | |
6dbeda34 ST |
317 | memcpy(want_digest, data, v->digest_size); |
318 | r = 0; | |
a4ffc152 MP |
319 | |
320 | release_ret_r: | |
321 | dm_bufio_release(buf); | |
a4ffc152 MP |
322 | return r; |
323 | } | |
324 | ||
6dbeda34 ST |
325 | /* |
326 | * Find a hash for a given block, write it to digest and verify the integrity | |
327 | * of the hash tree if necessary. | |
328 | */ | |
ffa39380 | 329 | int verity_hash_for_block(struct dm_verity *v, struct dm_verity_io *io, |
0cc37c2d | 330 | sector_t block, u8 *digest, bool *is_zero) |
6dbeda34 | 331 | { |
0cc37c2d | 332 | int r = 0, i; |
6dbeda34 ST |
333 | |
334 | if (likely(v->levels)) { | |
335 | /* | |
336 | * First, we try to get the requested hash for | |
337 | * the current block. If the hash block itself is | |
338 | * verified, zero is returned. If it isn't, this | |
339 | * function returns 1 and we fall back to whole | |
340 | * chain verification. | |
341 | */ | |
342 | r = verity_verify_level(v, io, block, 0, true, digest); | |
343 | if (likely(r <= 0)) | |
0cc37c2d | 344 | goto out; |
6dbeda34 ST |
345 | } |
346 | ||
347 | memcpy(digest, v->root_digest, v->digest_size); | |
348 | ||
349 | for (i = v->levels - 1; i >= 0; i--) { | |
350 | r = verity_verify_level(v, io, block, i, false, digest); | |
351 | if (unlikely(r)) | |
0cc37c2d | 352 | goto out; |
6dbeda34 | 353 | } |
0cc37c2d ST |
354 | out: |
355 | if (!r && v->zero_digest) | |
356 | *is_zero = !memcmp(v->zero_digest, digest, v->digest_size); | |
357 | else | |
358 | *is_zero = false; | |
6dbeda34 | 359 | |
0cc37c2d | 360 | return r; |
6dbeda34 ST |
361 | } |
362 | ||
d1ac3ff0 GBY |
363 | /* |
364 | * Calculates the digest for the given bio | |
365 | */ | |
366 | int verity_for_io_block(struct dm_verity *v, struct dm_verity_io *io, | |
12f1ffc4 | 367 | struct bvec_iter *iter, struct crypto_wait *wait) |
d1ac3ff0 GBY |
368 | { |
369 | unsigned int todo = 1 << v->data_dev_block_bits; | |
370 | struct bio *bio = dm_bio_from_per_bio_data(io, v->ti->per_io_data_size); | |
371 | struct scatterlist sg; | |
372 | struct ahash_request *req = verity_io_hash_req(v, io); | |
373 | ||
374 | do { | |
375 | int r; | |
376 | unsigned int len; | |
377 | struct bio_vec bv = bio_iter_iovec(bio, *iter); | |
378 | ||
379 | sg_init_table(&sg, 1); | |
380 | ||
381 | len = bv.bv_len; | |
382 | ||
383 | if (likely(len >= todo)) | |
384 | len = todo; | |
385 | /* | |
386 | * Operating on a single page at a time looks suboptimal | |
387 | * until you consider the typical block size is 4,096B. | |
388 | * Going through this loops twice should be very rare. | |
389 | */ | |
390 | sg_set_page(&sg, bv.bv_page, len, bv.bv_offset); | |
391 | ahash_request_set_crypt(req, &sg, NULL, len); | |
12f1ffc4 | 392 | r = crypto_wait_req(crypto_ahash_update(req), wait); |
d1ac3ff0 GBY |
393 | |
394 | if (unlikely(r < 0)) { | |
395 | DMERR("verity_for_io_block crypto op failed: %d", r); | |
396 | return r; | |
397 | } | |
398 | ||
399 | bio_advance_iter(bio, iter, len); | |
400 | todo -= len; | |
401 | } while (todo); | |
402 | ||
403 | return 0; | |
404 | } | |
405 | ||
bb4d73ac ST |
406 | /* |
407 | * Calls function process for 1 << v->data_dev_block_bits bytes in the bio_vec | |
408 | * starting from iter. | |
409 | */ | |
410 | int verity_for_bv_block(struct dm_verity *v, struct dm_verity_io *io, | |
411 | struct bvec_iter *iter, | |
412 | int (*process)(struct dm_verity *v, | |
413 | struct dm_verity_io *io, u8 *data, | |
414 | size_t len)) | |
415 | { | |
416 | unsigned todo = 1 << v->data_dev_block_bits; | |
30187e1d | 417 | struct bio *bio = dm_bio_from_per_bio_data(io, v->ti->per_io_data_size); |
bb4d73ac ST |
418 | |
419 | do { | |
420 | int r; | |
421 | u8 *page; | |
422 | unsigned len; | |
423 | struct bio_vec bv = bio_iter_iovec(bio, *iter); | |
424 | ||
425 | page = kmap_atomic(bv.bv_page); | |
426 | len = bv.bv_len; | |
427 | ||
428 | if (likely(len >= todo)) | |
429 | len = todo; | |
430 | ||
431 | r = process(v, io, page + bv.bv_offset, len); | |
432 | kunmap_atomic(page); | |
433 | ||
434 | if (r < 0) | |
435 | return r; | |
436 | ||
437 | bio_advance_iter(bio, iter, len); | |
438 | todo -= len; | |
439 | } while (todo); | |
440 | ||
441 | return 0; | |
442 | } | |
443 | ||
0cc37c2d ST |
444 | static int verity_bv_zero(struct dm_verity *v, struct dm_verity_io *io, |
445 | u8 *data, size_t len) | |
446 | { | |
447 | memset(data, 0, len); | |
448 | return 0; | |
449 | } | |
450 | ||
a4ffc152 MP |
451 | /* |
452 | * Verify one "dm_verity_io" structure. | |
453 | */ | |
454 | static int verity_verify_io(struct dm_verity_io *io) | |
455 | { | |
0cc37c2d | 456 | bool is_zero; |
a4ffc152 | 457 | struct dm_verity *v = io->v; |
bb4d73ac | 458 | struct bvec_iter start; |
a4ffc152 | 459 | unsigned b; |
12f1ffc4 | 460 | struct crypto_wait wait; |
a4ffc152 MP |
461 | |
462 | for (b = 0; b < io->n_blocks; b++) { | |
a4ffc152 | 463 | int r; |
d1ac3ff0 | 464 | struct ahash_request *req = verity_io_hash_req(v, io); |
a4ffc152 | 465 | |
6dbeda34 | 466 | r = verity_hash_for_block(v, io, io->block + b, |
0cc37c2d ST |
467 | verity_io_want_digest(v, io), |
468 | &is_zero); | |
6dbeda34 ST |
469 | if (unlikely(r < 0)) |
470 | return r; | |
a4ffc152 | 471 | |
0cc37c2d ST |
472 | if (is_zero) { |
473 | /* | |
474 | * If we expect a zero block, don't validate, just | |
475 | * return zeros. | |
476 | */ | |
477 | r = verity_for_bv_block(v, io, &io->iter, | |
478 | verity_bv_zero); | |
479 | if (unlikely(r < 0)) | |
480 | return r; | |
481 | ||
482 | continue; | |
483 | } | |
484 | ||
12f1ffc4 | 485 | r = verity_hash_init(v, req, &wait); |
6dbeda34 | 486 | if (unlikely(r < 0)) |
a4ffc152 | 487 | return r; |
a4ffc152 | 488 | |
bb4d73ac | 489 | start = io->iter; |
12f1ffc4 | 490 | r = verity_for_io_block(v, io, &io->iter, &wait); |
bb4d73ac ST |
491 | if (unlikely(r < 0)) |
492 | return r; | |
a4ffc152 | 493 | |
d1ac3ff0 | 494 | r = verity_hash_final(v, req, verity_io_real_digest(v, io), |
12f1ffc4 | 495 | &wait); |
6dbeda34 | 496 | if (unlikely(r < 0)) |
a4ffc152 | 497 | return r; |
6dbeda34 | 498 | |
ffa39380 ST |
499 | if (likely(memcmp(verity_io_real_digest(v, io), |
500 | verity_io_want_digest(v, io), v->digest_size) == 0)) | |
6dbeda34 | 501 | continue; |
a739ff3f ST |
502 | else if (verity_fec_decode(v, io, DM_VERITY_BLOCK_TYPE_DATA, |
503 | io->block + b, NULL, &start) == 0) | |
504 | continue; | |
6dbeda34 | 505 | else if (verity_handle_err(v, DM_VERITY_BLOCK_TYPE_DATA, |
a739ff3f | 506 | io->block + b)) |
6dbeda34 | 507 | return -EIO; |
a4ffc152 | 508 | } |
a4ffc152 MP |
509 | |
510 | return 0; | |
511 | } | |
512 | ||
513 | /* | |
514 | * End one "io" structure with a given error. | |
515 | */ | |
4e4cbee9 | 516 | static void verity_finish_io(struct dm_verity_io *io, blk_status_t status) |
a4ffc152 | 517 | { |
a4ffc152 | 518 | struct dm_verity *v = io->v; |
30187e1d | 519 | struct bio *bio = dm_bio_from_per_bio_data(io, v->ti->per_io_data_size); |
a4ffc152 MP |
520 | |
521 | bio->bi_end_io = io->orig_bi_end_io; | |
4e4cbee9 | 522 | bio->bi_status = status; |
a4ffc152 | 523 | |
a739ff3f ST |
524 | verity_fec_finish_io(io); |
525 | ||
4246a0b6 | 526 | bio_endio(bio); |
a4ffc152 MP |
527 | } |
528 | ||
529 | static void verity_work(struct work_struct *w) | |
530 | { | |
531 | struct dm_verity_io *io = container_of(w, struct dm_verity_io, work); | |
532 | ||
4e4cbee9 | 533 | verity_finish_io(io, errno_to_blk_status(verity_verify_io(io))); |
a4ffc152 MP |
534 | } |
535 | ||
4246a0b6 | 536 | static void verity_end_io(struct bio *bio) |
a4ffc152 MP |
537 | { |
538 | struct dm_verity_io *io = bio->bi_private; | |
539 | ||
4e4cbee9 CH |
540 | if (bio->bi_status && !verity_fec_is_enabled(io->v)) { |
541 | verity_finish_io(io, bio->bi_status); | |
a4ffc152 MP |
542 | return; |
543 | } | |
544 | ||
545 | INIT_WORK(&io->work, verity_work); | |
546 | queue_work(io->v->verify_wq, &io->work); | |
547 | } | |
548 | ||
549 | /* | |
550 | * Prefetch buffers for the specified io. | |
551 | * The root buffer is not prefetched, it is assumed that it will be cached | |
552 | * all the time. | |
553 | */ | |
3b6b7813 | 554 | static void verity_prefetch_io(struct work_struct *work) |
a4ffc152 | 555 | { |
3b6b7813 MP |
556 | struct dm_verity_prefetch_work *pw = |
557 | container_of(work, struct dm_verity_prefetch_work, work); | |
558 | struct dm_verity *v = pw->v; | |
a4ffc152 MP |
559 | int i; |
560 | ||
561 | for (i = v->levels - 2; i >= 0; i--) { | |
562 | sector_t hash_block_start; | |
563 | sector_t hash_block_end; | |
3b6b7813 MP |
564 | verity_hash_at_level(v, pw->block, i, &hash_block_start, NULL); |
565 | verity_hash_at_level(v, pw->block + pw->n_blocks - 1, i, &hash_block_end, NULL); | |
a4ffc152 | 566 | if (!i) { |
6aa7de05 | 567 | unsigned cluster = READ_ONCE(dm_verity_prefetch_cluster); |
a4ffc152 MP |
568 | |
569 | cluster >>= v->data_dev_block_bits; | |
570 | if (unlikely(!cluster)) | |
571 | goto no_prefetch_cluster; | |
572 | ||
573 | if (unlikely(cluster & (cluster - 1))) | |
553d8fe0 | 574 | cluster = 1 << __fls(cluster); |
a4ffc152 MP |
575 | |
576 | hash_block_start &= ~(sector_t)(cluster - 1); | |
577 | hash_block_end |= cluster - 1; | |
578 | if (unlikely(hash_block_end >= v->hash_blocks)) | |
579 | hash_block_end = v->hash_blocks - 1; | |
580 | } | |
581 | no_prefetch_cluster: | |
582 | dm_bufio_prefetch(v->bufio, hash_block_start, | |
583 | hash_block_end - hash_block_start + 1); | |
584 | } | |
3b6b7813 MP |
585 | |
586 | kfree(pw); | |
587 | } | |
588 | ||
589 | static void verity_submit_prefetch(struct dm_verity *v, struct dm_verity_io *io) | |
590 | { | |
591 | struct dm_verity_prefetch_work *pw; | |
592 | ||
593 | pw = kmalloc(sizeof(struct dm_verity_prefetch_work), | |
594 | GFP_NOIO | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN); | |
595 | ||
596 | if (!pw) | |
597 | return; | |
598 | ||
599 | INIT_WORK(&pw->work, verity_prefetch_io); | |
600 | pw->v = v; | |
601 | pw->block = io->block; | |
602 | pw->n_blocks = io->n_blocks; | |
603 | queue_work(v->verify_wq, &pw->work); | |
a4ffc152 MP |
604 | } |
605 | ||
606 | /* | |
607 | * Bio map function. It allocates dm_verity_io structure and bio vector and | |
608 | * fills them. Then it issues prefetches and the I/O. | |
609 | */ | |
7de3ee57 | 610 | static int verity_map(struct dm_target *ti, struct bio *bio) |
a4ffc152 MP |
611 | { |
612 | struct dm_verity *v = ti->private; | |
613 | struct dm_verity_io *io; | |
614 | ||
74d46992 | 615 | bio_set_dev(bio, v->data_dev->bdev); |
4f024f37 | 616 | bio->bi_iter.bi_sector = verity_map_sector(v, bio->bi_iter.bi_sector); |
a4ffc152 | 617 | |
4f024f37 | 618 | if (((unsigned)bio->bi_iter.bi_sector | bio_sectors(bio)) & |
a4ffc152 MP |
619 | ((1 << (v->data_dev_block_bits - SECTOR_SHIFT)) - 1)) { |
620 | DMERR_LIMIT("unaligned io"); | |
846785e6 | 621 | return DM_MAPIO_KILL; |
a4ffc152 MP |
622 | } |
623 | ||
f73a1c7d | 624 | if (bio_end_sector(bio) >> |
a4ffc152 MP |
625 | (v->data_dev_block_bits - SECTOR_SHIFT) > v->data_blocks) { |
626 | DMERR_LIMIT("io out of range"); | |
846785e6 | 627 | return DM_MAPIO_KILL; |
a4ffc152 MP |
628 | } |
629 | ||
630 | if (bio_data_dir(bio) == WRITE) | |
846785e6 | 631 | return DM_MAPIO_KILL; |
a4ffc152 | 632 | |
30187e1d | 633 | io = dm_per_bio_data(bio, ti->per_io_data_size); |
a4ffc152 | 634 | io->v = v; |
a4ffc152 | 635 | io->orig_bi_end_io = bio->bi_end_io; |
4f024f37 KO |
636 | io->block = bio->bi_iter.bi_sector >> (v->data_dev_block_bits - SECTOR_SHIFT); |
637 | io->n_blocks = bio->bi_iter.bi_size >> v->data_dev_block_bits; | |
a4ffc152 MP |
638 | |
639 | bio->bi_end_io = verity_end_io; | |
640 | bio->bi_private = io; | |
003b5c57 | 641 | io->iter = bio->bi_iter; |
a4ffc152 | 642 | |
a739ff3f ST |
643 | verity_fec_init_io(io); |
644 | ||
3b6b7813 | 645 | verity_submit_prefetch(v, io); |
a4ffc152 MP |
646 | |
647 | generic_make_request(bio); | |
648 | ||
649 | return DM_MAPIO_SUBMITTED; | |
650 | } | |
651 | ||
652 | /* | |
653 | * Status: V (valid) or C (corruption found) | |
654 | */ | |
fd7c092e MP |
655 | static void verity_status(struct dm_target *ti, status_type_t type, |
656 | unsigned status_flags, char *result, unsigned maxlen) | |
a4ffc152 MP |
657 | { |
658 | struct dm_verity *v = ti->private; | |
a739ff3f | 659 | unsigned args = 0; |
a4ffc152 MP |
660 | unsigned sz = 0; |
661 | unsigned x; | |
662 | ||
663 | switch (type) { | |
664 | case STATUSTYPE_INFO: | |
665 | DMEMIT("%c", v->hash_failed ? 'C' : 'V'); | |
666 | break; | |
667 | case STATUSTYPE_TABLE: | |
668 | DMEMIT("%u %s %s %u %u %llu %llu %s ", | |
669 | v->version, | |
670 | v->data_dev->name, | |
671 | v->hash_dev->name, | |
672 | 1 << v->data_dev_block_bits, | |
673 | 1 << v->hash_dev_block_bits, | |
674 | (unsigned long long)v->data_blocks, | |
675 | (unsigned long long)v->hash_start, | |
676 | v->alg_name | |
677 | ); | |
678 | for (x = 0; x < v->digest_size; x++) | |
679 | DMEMIT("%02x", v->root_digest[x]); | |
680 | DMEMIT(" "); | |
681 | if (!v->salt_size) | |
682 | DMEMIT("-"); | |
683 | else | |
684 | for (x = 0; x < v->salt_size; x++) | |
685 | DMEMIT("%02x", v->salt[x]); | |
a739ff3f ST |
686 | if (v->mode != DM_VERITY_MODE_EIO) |
687 | args++; | |
688 | if (verity_fec_is_enabled(v)) | |
689 | args += DM_VERITY_OPTS_FEC; | |
0cc37c2d ST |
690 | if (v->zero_digest) |
691 | args++; | |
a739ff3f ST |
692 | if (!args) |
693 | return; | |
694 | DMEMIT(" %u", args); | |
65ff5b7d | 695 | if (v->mode != DM_VERITY_MODE_EIO) { |
a739ff3f | 696 | DMEMIT(" "); |
65ff5b7d ST |
697 | switch (v->mode) { |
698 | case DM_VERITY_MODE_LOGGING: | |
699 | DMEMIT(DM_VERITY_OPT_LOGGING); | |
700 | break; | |
701 | case DM_VERITY_MODE_RESTART: | |
702 | DMEMIT(DM_VERITY_OPT_RESTART); | |
703 | break; | |
704 | default: | |
705 | BUG(); | |
706 | } | |
707 | } | |
0cc37c2d ST |
708 | if (v->zero_digest) |
709 | DMEMIT(" " DM_VERITY_OPT_IGN_ZEROES); | |
a739ff3f | 710 | sz = verity_fec_status_table(v, sz, result, maxlen); |
a4ffc152 MP |
711 | break; |
712 | } | |
a4ffc152 MP |
713 | } |
714 | ||
e56f81e0 CH |
715 | static int verity_prepare_ioctl(struct dm_target *ti, |
716 | struct block_device **bdev, fmode_t *mode) | |
a4ffc152 MP |
717 | { |
718 | struct dm_verity *v = ti->private; | |
e56f81e0 CH |
719 | |
720 | *bdev = v->data_dev->bdev; | |
a4ffc152 MP |
721 | |
722 | if (v->data_start || | |
723 | ti->len != i_size_read(v->data_dev->bdev->bd_inode) >> SECTOR_SHIFT) | |
e56f81e0 CH |
724 | return 1; |
725 | return 0; | |
a4ffc152 MP |
726 | } |
727 | ||
a4ffc152 MP |
728 | static int verity_iterate_devices(struct dm_target *ti, |
729 | iterate_devices_callout_fn fn, void *data) | |
730 | { | |
731 | struct dm_verity *v = ti->private; | |
732 | ||
733 | return fn(ti, v->data_dev, v->data_start, ti->len, data); | |
734 | } | |
735 | ||
736 | static void verity_io_hints(struct dm_target *ti, struct queue_limits *limits) | |
737 | { | |
738 | struct dm_verity *v = ti->private; | |
739 | ||
740 | if (limits->logical_block_size < 1 << v->data_dev_block_bits) | |
741 | limits->logical_block_size = 1 << v->data_dev_block_bits; | |
742 | ||
743 | if (limits->physical_block_size < 1 << v->data_dev_block_bits) | |
744 | limits->physical_block_size = 1 << v->data_dev_block_bits; | |
745 | ||
746 | blk_limits_io_min(limits, limits->logical_block_size); | |
747 | } | |
748 | ||
749 | static void verity_dtr(struct dm_target *ti) | |
750 | { | |
751 | struct dm_verity *v = ti->private; | |
752 | ||
753 | if (v->verify_wq) | |
754 | destroy_workqueue(v->verify_wq); | |
755 | ||
a4ffc152 MP |
756 | if (v->bufio) |
757 | dm_bufio_client_destroy(v->bufio); | |
758 | ||
759 | kfree(v->salt); | |
760 | kfree(v->root_digest); | |
0cc37c2d | 761 | kfree(v->zero_digest); |
a4ffc152 MP |
762 | |
763 | if (v->tfm) | |
d1ac3ff0 | 764 | crypto_free_ahash(v->tfm); |
a4ffc152 MP |
765 | |
766 | kfree(v->alg_name); | |
767 | ||
768 | if (v->hash_dev) | |
769 | dm_put_device(ti, v->hash_dev); | |
770 | ||
771 | if (v->data_dev) | |
772 | dm_put_device(ti, v->data_dev); | |
773 | ||
a739ff3f ST |
774 | verity_fec_dtr(v); |
775 | ||
a4ffc152 MP |
776 | kfree(v); |
777 | } | |
778 | ||
0cc37c2d ST |
779 | static int verity_alloc_zero_digest(struct dm_verity *v) |
780 | { | |
781 | int r = -ENOMEM; | |
d1ac3ff0 | 782 | struct ahash_request *req; |
0cc37c2d ST |
783 | u8 *zero_data; |
784 | ||
785 | v->zero_digest = kmalloc(v->digest_size, GFP_KERNEL); | |
786 | ||
787 | if (!v->zero_digest) | |
788 | return r; | |
789 | ||
d1ac3ff0 | 790 | req = kmalloc(v->ahash_reqsize, GFP_KERNEL); |
0cc37c2d | 791 | |
d1ac3ff0 | 792 | if (!req) |
0cc37c2d ST |
793 | return r; /* verity_dtr will free zero_digest */ |
794 | ||
795 | zero_data = kzalloc(1 << v->data_dev_block_bits, GFP_KERNEL); | |
796 | ||
797 | if (!zero_data) | |
798 | goto out; | |
799 | ||
d1ac3ff0 | 800 | r = verity_hash(v, req, zero_data, 1 << v->data_dev_block_bits, |
0cc37c2d ST |
801 | v->zero_digest); |
802 | ||
803 | out: | |
d1ac3ff0 | 804 | kfree(req); |
0cc37c2d ST |
805 | kfree(zero_data); |
806 | ||
807 | return r; | |
808 | } | |
809 | ||
753c1fd0 ST |
810 | static int verity_parse_opt_args(struct dm_arg_set *as, struct dm_verity *v) |
811 | { | |
812 | int r; | |
813 | unsigned argc; | |
814 | struct dm_target *ti = v->ti; | |
815 | const char *arg_name; | |
816 | ||
5916a22b | 817 | static const struct dm_arg _args[] = { |
753c1fd0 ST |
818 | {0, DM_VERITY_OPTS_MAX, "Invalid number of feature args"}, |
819 | }; | |
820 | ||
821 | r = dm_read_arg_group(_args, as, &argc, &ti->error); | |
822 | if (r) | |
823 | return -EINVAL; | |
824 | ||
825 | if (!argc) | |
826 | return 0; | |
827 | ||
828 | do { | |
829 | arg_name = dm_shift_arg(as); | |
830 | argc--; | |
831 | ||
832 | if (!strcasecmp(arg_name, DM_VERITY_OPT_LOGGING)) { | |
833 | v->mode = DM_VERITY_MODE_LOGGING; | |
834 | continue; | |
835 | ||
836 | } else if (!strcasecmp(arg_name, DM_VERITY_OPT_RESTART)) { | |
837 | v->mode = DM_VERITY_MODE_RESTART; | |
838 | continue; | |
a739ff3f | 839 | |
0cc37c2d ST |
840 | } else if (!strcasecmp(arg_name, DM_VERITY_OPT_IGN_ZEROES)) { |
841 | r = verity_alloc_zero_digest(v); | |
842 | if (r) { | |
843 | ti->error = "Cannot allocate zero digest"; | |
844 | return r; | |
845 | } | |
846 | continue; | |
847 | ||
a739ff3f ST |
848 | } else if (verity_is_fec_opt_arg(arg_name)) { |
849 | r = verity_fec_parse_opt_args(as, v, &argc, arg_name); | |
850 | if (r) | |
851 | return r; | |
852 | continue; | |
753c1fd0 ST |
853 | } |
854 | ||
855 | ti->error = "Unrecognized verity feature request"; | |
856 | return -EINVAL; | |
857 | } while (argc && !r); | |
858 | ||
859 | return r; | |
860 | } | |
861 | ||
a4ffc152 MP |
862 | /* |
863 | * Target parameters: | |
864 | * <version> The current format is version 1. | |
865 | * Vsn 0 is compatible with original Chromium OS releases. | |
866 | * <data device> | |
867 | * <hash device> | |
868 | * <data block size> | |
869 | * <hash block size> | |
870 | * <the number of data blocks> | |
871 | * <hash start block> | |
872 | * <algorithm> | |
873 | * <digest> | |
874 | * <salt> Hex string or "-" if no salt. | |
875 | */ | |
876 | static int verity_ctr(struct dm_target *ti, unsigned argc, char **argv) | |
877 | { | |
878 | struct dm_verity *v; | |
65ff5b7d | 879 | struct dm_arg_set as; |
753c1fd0 | 880 | unsigned int num; |
a4ffc152 MP |
881 | unsigned long long num_ll; |
882 | int r; | |
883 | int i; | |
884 | sector_t hash_position; | |
885 | char dummy; | |
886 | ||
887 | v = kzalloc(sizeof(struct dm_verity), GFP_KERNEL); | |
888 | if (!v) { | |
889 | ti->error = "Cannot allocate verity structure"; | |
890 | return -ENOMEM; | |
891 | } | |
892 | ti->private = v; | |
893 | v->ti = ti; | |
894 | ||
a739ff3f ST |
895 | r = verity_fec_ctr_alloc(v); |
896 | if (r) | |
897 | goto bad; | |
898 | ||
a4ffc152 MP |
899 | if ((dm_table_get_mode(ti->table) & ~FMODE_READ)) { |
900 | ti->error = "Device must be readonly"; | |
901 | r = -EINVAL; | |
902 | goto bad; | |
903 | } | |
904 | ||
65ff5b7d ST |
905 | if (argc < 10) { |
906 | ti->error = "Not enough arguments"; | |
a4ffc152 MP |
907 | r = -EINVAL; |
908 | goto bad; | |
909 | } | |
910 | ||
5d8be843 MP |
911 | if (sscanf(argv[0], "%u%c", &num, &dummy) != 1 || |
912 | num > 1) { | |
a4ffc152 MP |
913 | ti->error = "Invalid version"; |
914 | r = -EINVAL; | |
915 | goto bad; | |
916 | } | |
917 | v->version = num; | |
918 | ||
919 | r = dm_get_device(ti, argv[1], FMODE_READ, &v->data_dev); | |
920 | if (r) { | |
921 | ti->error = "Data device lookup failed"; | |
922 | goto bad; | |
923 | } | |
924 | ||
925 | r = dm_get_device(ti, argv[2], FMODE_READ, &v->hash_dev); | |
926 | if (r) { | |
21ffe552 | 927 | ti->error = "Hash device lookup failed"; |
a4ffc152 MP |
928 | goto bad; |
929 | } | |
930 | ||
931 | if (sscanf(argv[3], "%u%c", &num, &dummy) != 1 || | |
932 | !num || (num & (num - 1)) || | |
933 | num < bdev_logical_block_size(v->data_dev->bdev) || | |
934 | num > PAGE_SIZE) { | |
935 | ti->error = "Invalid data device block size"; | |
936 | r = -EINVAL; | |
937 | goto bad; | |
938 | } | |
553d8fe0 | 939 | v->data_dev_block_bits = __ffs(num); |
a4ffc152 MP |
940 | |
941 | if (sscanf(argv[4], "%u%c", &num, &dummy) != 1 || | |
942 | !num || (num & (num - 1)) || | |
943 | num < bdev_logical_block_size(v->hash_dev->bdev) || | |
944 | num > INT_MAX) { | |
945 | ti->error = "Invalid hash device block size"; | |
946 | r = -EINVAL; | |
947 | goto bad; | |
948 | } | |
553d8fe0 | 949 | v->hash_dev_block_bits = __ffs(num); |
a4ffc152 MP |
950 | |
951 | if (sscanf(argv[5], "%llu%c", &num_ll, &dummy) != 1 || | |
1d55f6bc MP |
952 | (sector_t)(num_ll << (v->data_dev_block_bits - SECTOR_SHIFT)) |
953 | >> (v->data_dev_block_bits - SECTOR_SHIFT) != num_ll) { | |
a4ffc152 MP |
954 | ti->error = "Invalid data blocks"; |
955 | r = -EINVAL; | |
956 | goto bad; | |
957 | } | |
958 | v->data_blocks = num_ll; | |
959 | ||
960 | if (ti->len > (v->data_blocks << (v->data_dev_block_bits - SECTOR_SHIFT))) { | |
961 | ti->error = "Data device is too small"; | |
962 | r = -EINVAL; | |
963 | goto bad; | |
964 | } | |
965 | ||
966 | if (sscanf(argv[6], "%llu%c", &num_ll, &dummy) != 1 || | |
1d55f6bc MP |
967 | (sector_t)(num_ll << (v->hash_dev_block_bits - SECTOR_SHIFT)) |
968 | >> (v->hash_dev_block_bits - SECTOR_SHIFT) != num_ll) { | |
a4ffc152 MP |
969 | ti->error = "Invalid hash start"; |
970 | r = -EINVAL; | |
971 | goto bad; | |
972 | } | |
973 | v->hash_start = num_ll; | |
974 | ||
975 | v->alg_name = kstrdup(argv[7], GFP_KERNEL); | |
976 | if (!v->alg_name) { | |
977 | ti->error = "Cannot allocate algorithm name"; | |
978 | r = -ENOMEM; | |
979 | goto bad; | |
980 | } | |
981 | ||
d1ac3ff0 | 982 | v->tfm = crypto_alloc_ahash(v->alg_name, 0, 0); |
a4ffc152 MP |
983 | if (IS_ERR(v->tfm)) { |
984 | ti->error = "Cannot initialize hash function"; | |
985 | r = PTR_ERR(v->tfm); | |
986 | v->tfm = NULL; | |
987 | goto bad; | |
988 | } | |
d1ac3ff0 | 989 | v->digest_size = crypto_ahash_digestsize(v->tfm); |
a4ffc152 MP |
990 | if ((1 << v->hash_dev_block_bits) < v->digest_size * 2) { |
991 | ti->error = "Digest size too big"; | |
992 | r = -EINVAL; | |
993 | goto bad; | |
994 | } | |
d1ac3ff0 GBY |
995 | v->ahash_reqsize = sizeof(struct ahash_request) + |
996 | crypto_ahash_reqsize(v->tfm); | |
a4ffc152 MP |
997 | |
998 | v->root_digest = kmalloc(v->digest_size, GFP_KERNEL); | |
999 | if (!v->root_digest) { | |
1000 | ti->error = "Cannot allocate root digest"; | |
1001 | r = -ENOMEM; | |
1002 | goto bad; | |
1003 | } | |
1004 | if (strlen(argv[8]) != v->digest_size * 2 || | |
1005 | hex2bin(v->root_digest, argv[8], v->digest_size)) { | |
1006 | ti->error = "Invalid root digest"; | |
1007 | r = -EINVAL; | |
1008 | goto bad; | |
1009 | } | |
1010 | ||
1011 | if (strcmp(argv[9], "-")) { | |
1012 | v->salt_size = strlen(argv[9]) / 2; | |
1013 | v->salt = kmalloc(v->salt_size, GFP_KERNEL); | |
1014 | if (!v->salt) { | |
1015 | ti->error = "Cannot allocate salt"; | |
1016 | r = -ENOMEM; | |
1017 | goto bad; | |
1018 | } | |
1019 | if (strlen(argv[9]) != v->salt_size * 2 || | |
1020 | hex2bin(v->salt, argv[9], v->salt_size)) { | |
1021 | ti->error = "Invalid salt"; | |
1022 | r = -EINVAL; | |
1023 | goto bad; | |
1024 | } | |
1025 | } | |
1026 | ||
65ff5b7d ST |
1027 | argv += 10; |
1028 | argc -= 10; | |
1029 | ||
1030 | /* Optional parameters */ | |
1031 | if (argc) { | |
1032 | as.argc = argc; | |
1033 | as.argv = argv; | |
1034 | ||
753c1fd0 ST |
1035 | r = verity_parse_opt_args(&as, v); |
1036 | if (r < 0) | |
65ff5b7d | 1037 | goto bad; |
65ff5b7d ST |
1038 | } |
1039 | ||
a4ffc152 | 1040 | v->hash_per_block_bits = |
553d8fe0 | 1041 | __fls((1 << v->hash_dev_block_bits) / v->digest_size); |
a4ffc152 MP |
1042 | |
1043 | v->levels = 0; | |
1044 | if (v->data_blocks) | |
1045 | while (v->hash_per_block_bits * v->levels < 64 && | |
1046 | (unsigned long long)(v->data_blocks - 1) >> | |
1047 | (v->hash_per_block_bits * v->levels)) | |
1048 | v->levels++; | |
1049 | ||
1050 | if (v->levels > DM_VERITY_MAX_LEVELS) { | |
1051 | ti->error = "Too many tree levels"; | |
1052 | r = -E2BIG; | |
1053 | goto bad; | |
1054 | } | |
1055 | ||
1056 | hash_position = v->hash_start; | |
1057 | for (i = v->levels - 1; i >= 0; i--) { | |
1058 | sector_t s; | |
1059 | v->hash_level_block[i] = hash_position; | |
b1bf2de0 MP |
1060 | s = (v->data_blocks + ((sector_t)1 << ((i + 1) * v->hash_per_block_bits)) - 1) |
1061 | >> ((i + 1) * v->hash_per_block_bits); | |
a4ffc152 MP |
1062 | if (hash_position + s < hash_position) { |
1063 | ti->error = "Hash device offset overflow"; | |
1064 | r = -E2BIG; | |
1065 | goto bad; | |
1066 | } | |
1067 | hash_position += s; | |
1068 | } | |
1069 | v->hash_blocks = hash_position; | |
1070 | ||
1071 | v->bufio = dm_bufio_client_create(v->hash_dev->bdev, | |
1072 | 1 << v->hash_dev_block_bits, 1, sizeof(struct buffer_aux), | |
1073 | dm_bufio_alloc_callback, NULL); | |
1074 | if (IS_ERR(v->bufio)) { | |
1075 | ti->error = "Cannot initialize dm-bufio"; | |
1076 | r = PTR_ERR(v->bufio); | |
1077 | v->bufio = NULL; | |
1078 | goto bad; | |
1079 | } | |
1080 | ||
1081 | if (dm_bufio_get_device_size(v->bufio) < v->hash_blocks) { | |
1082 | ti->error = "Hash device is too small"; | |
1083 | r = -E2BIG; | |
1084 | goto bad; | |
1085 | } | |
1086 | ||
a4ffc152 MP |
1087 | /* WQ_UNBOUND greatly improves performance when running on ramdisk */ |
1088 | v->verify_wq = alloc_workqueue("kverityd", WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM | WQ_UNBOUND, num_online_cpus()); | |
1089 | if (!v->verify_wq) { | |
1090 | ti->error = "Cannot allocate workqueue"; | |
1091 | r = -ENOMEM; | |
1092 | goto bad; | |
1093 | } | |
1094 | ||
30187e1d | 1095 | ti->per_io_data_size = sizeof(struct dm_verity_io) + |
d1ac3ff0 | 1096 | v->ahash_reqsize + v->digest_size * 2; |
a739ff3f ST |
1097 | |
1098 | r = verity_fec_ctr(v); | |
1099 | if (r) | |
1100 | goto bad; | |
1101 | ||
30187e1d MS |
1102 | ti->per_io_data_size = roundup(ti->per_io_data_size, |
1103 | __alignof__(struct dm_verity_io)); | |
a739ff3f | 1104 | |
a4ffc152 MP |
1105 | return 0; |
1106 | ||
1107 | bad: | |
1108 | verity_dtr(ti); | |
1109 | ||
1110 | return r; | |
1111 | } | |
1112 | ||
1113 | static struct target_type verity_target = { | |
1114 | .name = "verity", | |
a739ff3f | 1115 | .version = {1, 3, 0}, |
a4ffc152 MP |
1116 | .module = THIS_MODULE, |
1117 | .ctr = verity_ctr, | |
1118 | .dtr = verity_dtr, | |
1119 | .map = verity_map, | |
1120 | .status = verity_status, | |
e56f81e0 | 1121 | .prepare_ioctl = verity_prepare_ioctl, |
a4ffc152 MP |
1122 | .iterate_devices = verity_iterate_devices, |
1123 | .io_hints = verity_io_hints, | |
1124 | }; | |
1125 | ||
1126 | static int __init dm_verity_init(void) | |
1127 | { | |
1128 | int r; | |
1129 | ||
1130 | r = dm_register_target(&verity_target); | |
1131 | if (r < 0) | |
1132 | DMERR("register failed %d", r); | |
1133 | ||
1134 | return r; | |
1135 | } | |
1136 | ||
1137 | static void __exit dm_verity_exit(void) | |
1138 | { | |
1139 | dm_unregister_target(&verity_target); | |
1140 | } | |
1141 | ||
1142 | module_init(dm_verity_init); | |
1143 | module_exit(dm_verity_exit); | |
1144 | ||
1145 | MODULE_AUTHOR("Mikulas Patocka <mpatocka@redhat.com>"); | |
1146 | MODULE_AUTHOR("Mandeep Baines <msb@chromium.org>"); | |
1147 | MODULE_AUTHOR("Will Drewry <wad@chromium.org>"); | |
1148 | MODULE_DESCRIPTION(DM_NAME " target for transparent disk integrity checking"); | |
1149 | MODULE_LICENSE("GPL"); |