OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2010 The Chromium OS Authors <chromium-os-dev@chromium.org> | 2 * Copyright (C) 2010 The Chromium OS Authors <chromium-os-dev@chromium.org> |
3 * | 3 * |
4 * Device-Mapper block hash tree interface. | 4 * Device-Mapper block hash tree interface. |
5 * See Documentation/device-mapper/dm-bht.txt for details. | 5 * See Documentation/device-mapper/dm-bht.txt for details. |
6 * | 6 * |
7 * This file is released under the GPL. | 7 * This file is released under the GPL. |
8 */ | 8 */ |
9 | 9 |
10 #include <asm/atomic.h> | 10 #include <asm/atomic.h> |
(...skipping 664 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
675 return 0; | 675 return 0; |
676 } | 676 } |
677 | 677 |
678 /* dm_bht_verify_path | 678 /* dm_bht_verify_path |
679 * Verifies the path from block_index to depth=0. Returns 0 on ok. | 679 * Verifies the path from block_index to depth=0. Returns 0 on ok. |
680 */ | 680 */ |
681 static int dm_bht_verify_path(struct dm_bht *bht, unsigned int block_index) | 681 static int dm_bht_verify_path(struct dm_bht *bht, unsigned int block_index) |
682 { | 682 { |
683 unsigned int depth = bht->depth - 1; | 683 unsigned int depth = bht->depth - 1; |
684 struct dm_bht_entry *entry = dm_bht_get_entry(bht, depth, block_index); | 684 struct dm_bht_entry *entry = dm_bht_get_entry(bht, depth, block_index); |
| 685 int state = atomic_read(&entry->state); |
685 | 686 |
686 » while (depth > 0) { | 687 » while (depth > 0 && state != DM_BHT_ENTRY_VERIFIED) { |
687 u8 digest[DM_BHT_MAX_DIGEST_SIZE]; | 688 u8 digest[DM_BHT_MAX_DIGEST_SIZE]; |
688 struct dm_bht_entry *parent; | 689 struct dm_bht_entry *parent; |
689 struct page *page; | 690 struct page *page; |
690 u8 *node; | 691 u8 *node; |
691 int state; | |
692 | |
693 DMDEBUG("verify_path for b=%u on d=%d", block_index, depth); | |
694 /* TODO(msb,wad): would be nice to avoid two atomic reads */ | |
695 state = atomic_read(&entry->state); | |
696 if (state == DM_BHT_ENTRY_VERIFIED) { | |
697 DMDEBUG("verify_path node %u is verified to root", | |
698 block_index); | |
699 depth++; /* avoid an extra cmpxchg */ | |
700 break; | |
701 } else if (state <= DM_BHT_ENTRY_ERROR) { | |
702 DMCRIT("entry(d=%u,b=%u) is in an error state: %d", | |
703 depth, block_index, state); | |
704 DMCRIT("verification is not possible"); | |
705 goto mismatch; | |
706 } else if (state <= DM_BHT_ENTRY_PENDING) { | |
707 DMERR("entry not ready for verify: d=%u,b=%u", | |
708 depth, block_index); | |
709 goto mismatch; | |
710 } | |
711 | 692 |
712 /* We need to check that this entry matches the expected | 693 /* We need to check that this entry matches the expected |
713 * hash in the parent->nodes. | 694 * hash in the parent->nodes. |
714 */ | 695 */ |
715 parent = dm_bht_get_entry(bht, depth - 1, block_index); | 696 parent = dm_bht_get_entry(bht, depth - 1, block_index); |
| 697 state = atomic_read(&parent->state); |
716 /* This call is only safe if all nodes along the path | 698 /* This call is only safe if all nodes along the path |
717 * are already populated (i.e. READY) via dm_bht_populate. | 699 * are already populated (i.e. READY) via dm_bht_populate. |
718 */ | 700 */ |
719 » » BUG_ON(atomic_read(&parent->state) < DM_BHT_ENTRY_READY); | 701 » » BUG_ON(state < DM_BHT_ENTRY_READY); |
720 node = dm_bht_get_node(bht, parent, depth, block_index); | 702 node = dm_bht_get_node(bht, parent, depth, block_index); |
721 page = virt_to_page(entry->nodes); | 703 page = virt_to_page(entry->nodes); |
722 | 704 |
723 if (dm_bht_compute_hash(bht, page, digest) || | 705 if (dm_bht_compute_hash(bht, page, digest) || |
724 » » dm_bht_compare_hash(bht, digest, node)) { | 706 » » dm_bht_compare_hash(bht, digest, node)) |
725 » » » DMERR("failed to verify entry's hash against parent " | |
726 » » » "(d=%u,bi=%u)", depth, block_index); | |
727 goto mismatch; | 707 goto mismatch; |
728 } | |
729 | 708 |
730 entry = parent; | 709 entry = parent; |
731 depth--; | 710 depth--; |
732 } | 711 } |
| 712 |
733 /* Mark path to leaf as verified. */ | 713 /* Mark path to leaf as verified. */ |
734 » for (; depth < bht->depth; depth++) { | 714 » for (depth++; depth < bht->depth; depth++) { |
735 entry = dm_bht_get_entry(bht, depth, block_index); | 715 entry = dm_bht_get_entry(bht, depth, block_index); |
736 atomic_cmpxchg(&entry->state, | 716 atomic_cmpxchg(&entry->state, |
737 DM_BHT_ENTRY_READY, | 717 DM_BHT_ENTRY_READY, |
738 DM_BHT_ENTRY_VERIFIED); | 718 DM_BHT_ENTRY_VERIFIED); |
739 } | 719 } |
740 | 720 |
| 721 DMDEBUG("verify_path: node %u is verified to root", block_index); |
741 return 0; | 722 return 0; |
742 | 723 |
743 mismatch: | 724 mismatch: |
| 725 DMERR("verify_path: failed to verify hash against parent (d=%u,bi=%u)", |
| 726 depth, block_index); |
744 return DM_BHT_ENTRY_ERROR_MISMATCH; | 727 return DM_BHT_ENTRY_ERROR_MISMATCH; |
745 } | 728 } |
746 | 729 |
747 /** | 730 /** |
748 * dm_bht_store_block - sets a given block's hash in the tree | 731 * dm_bht_store_block - sets a given block's hash in the tree |
749 * @bht: pointer to a dm_bht_create()d bht | 732 * @bht: pointer to a dm_bht_create()d bht |
750 * @block_index:numeric index of the block in the tree | 733 * @block_index:numeric index of the block in the tree |
751 * @digest: array of u8s containing the digest of length @bht->digest_size | 734 * @digest: array of u8s containing the digest of length @bht->digest_size |
752 * | 735 * |
753 * Returns 0 on success, >0 when data is pending, and <0 when a IO or other | 736 * Returns 0 on success, >0 when data is pending, and <0 when a IO or other |
(...skipping 476 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1230 DMERR("no root digest exists to export"); | 1213 DMERR("no root digest exists to export"); |
1231 if (available > 0) | 1214 if (available > 0) |
1232 *hexdigest = 0; | 1215 *hexdigest = 0; |
1233 return -1; | 1216 return -1; |
1234 } | 1217 } |
1235 dm_bht_bin_to_hex(bht->root_digest, hexdigest, bht->digest_size); | 1218 dm_bht_bin_to_hex(bht->root_digest, hexdigest, bht->digest_size); |
1236 return 0; | 1219 return 0; |
1237 } | 1220 } |
1238 EXPORT_SYMBOL(dm_bht_root_hexdigest); | 1221 EXPORT_SYMBOL(dm_bht_root_hexdigest); |
1239 | 1222 |
OLD | NEW |