@@ -655,55 +655,56 @@ spa_keystore_dsl_key_hold_dd(spa_t *spa, dsl_dir_t *dd, void *tag,
655
655
{
656
656
int ret ;
657
657
avl_index_t where ;
658
- dsl_crypto_key_t * dck = NULL ;
658
+ dsl_crypto_key_t * dck_io = NULL , * dck_ks = NULL ;
659
659
dsl_wrapping_key_t * wkey = NULL ;
660
660
uint64_t dckobj = dd -> dd_crypto_obj ;
661
661
662
- rw_enter (& spa -> spa_keystore .sk_dk_lock , RW_WRITER );
663
-
664
- /* lookup the key in the tree of currently loaded keys */
665
- ret = spa_keystore_dsl_key_hold_impl (spa , dckobj , tag , & dck );
666
- if (!ret ) {
667
- rw_exit (& spa -> spa_keystore .sk_dk_lock );
668
- * dck_out = dck ;
662
+ /* Lookup the key in the tree of currently loaded keys */
663
+ rw_enter (& spa -> spa_keystore .sk_dk_lock , RW_READER );
664
+ ret = spa_keystore_dsl_key_hold_impl (spa , dckobj , tag , & dck_ks );
665
+ rw_exit (& spa -> spa_keystore .sk_dk_lock );
666
+ if (ret == 0 ) {
667
+ * dck_out = dck_ks ;
669
668
return (0 );
670
669
}
671
670
672
- /* lookup the wrapping key from the keystore */
671
+ /* Lookup the wrapping key from the keystore */
673
672
ret = spa_keystore_wkey_hold_dd (spa , dd , FTAG , & wkey );
674
673
if (ret != 0 ) {
675
- ret = SET_ERROR ( EACCES ) ;
676
- goto error_unlock ;
674
+ * dck_out = NULL ;
675
+ return ( SET_ERROR ( EACCES )) ;
677
676
}
678
677
679
- /* read the key from disk */
678
+ /* Read the key from disk */
680
679
ret = dsl_crypto_key_open (spa -> spa_meta_objset , wkey , dckobj ,
681
- tag , & dck );
682
- if (ret != 0 )
683
- goto error_unlock ;
680
+ tag , & dck_io );
681
+ if (ret != 0 ) {
682
+ dsl_wrapping_key_rele (wkey , FTAG );
683
+ * dck_out = NULL ;
684
+ return (ret );
685
+ }
684
686
685
687
/*
686
- * add the key to the keystore (this should always succeed
687
- * since we made sure it didn't exist before)
688
+ * Add the key to the keystore. It may already exist if it was
689
+ * added while performing the read from disk. In this case discard
690
+ * it and return the key from the keystore.
688
691
*/
689
- avl_find (& spa -> spa_keystore .sk_dsl_keys , dck , & where );
690
- avl_insert (& spa -> spa_keystore .sk_dsl_keys , dck , where );
692
+ rw_enter (& spa -> spa_keystore .sk_dk_lock , RW_WRITER );
693
+ ret = spa_keystore_dsl_key_hold_impl (spa , dckobj , tag , & dck_ks );
694
+ if (ret != 0 ) {
695
+ avl_find (& spa -> spa_keystore .sk_dsl_keys , dck_io , & where );
696
+ avl_insert (& spa -> spa_keystore .sk_dsl_keys , dck_io , where );
697
+ * dck_out = dck_io ;
698
+ } else {
699
+ dsl_crypto_key_free (dck_io );
700
+ * dck_out = dck_ks ;
701
+ }
691
702
692
- /* release the wrapping key (the dsl key now has a reference to it) */
703
+ /* Release the wrapping key (the dsl key now has a reference to it) */
693
704
dsl_wrapping_key_rele (wkey , FTAG );
694
-
695
705
rw_exit (& spa -> spa_keystore .sk_dk_lock );
696
706
697
- * dck_out = dck ;
698
707
return (0 );
699
-
700
- error_unlock :
701
- rw_exit (& spa -> spa_keystore .sk_dk_lock );
702
- if (wkey != NULL )
703
- dsl_wrapping_key_rele (wkey , FTAG );
704
-
705
- * dck_out = NULL ;
706
- return (ret );
707
708
}
708
709
709
710
void
@@ -934,20 +935,19 @@ spa_keystore_create_mapping_impl(spa_t *spa, uint64_t dsobj,
934
935
{
935
936
int ret ;
936
937
avl_index_t where ;
937
- dsl_key_mapping_t * km = NULL , * found_km ;
938
+ dsl_key_mapping_t * km , * found_km ;
938
939
boolean_t should_free = B_FALSE ;
939
940
940
- /* allocate the mapping */
941
- km = kmem_alloc (sizeof (dsl_key_mapping_t ), KM_SLEEP );
942
- if (!km )
943
- return (SET_ERROR (ENOMEM ));
944
-
945
- /* initialize the mapping */
941
+ /* Allocate and initialize the mapping */
942
+ km = kmem_zalloc (sizeof (dsl_key_mapping_t ), KM_SLEEP );
946
943
refcount_create (& km -> km_refcnt );
947
944
948
945
ret = spa_keystore_dsl_key_hold_dd (spa , dd , km , & km -> km_key );
949
- if (ret != 0 )
950
- goto error ;
946
+ if (ret != 0 ) {
947
+ refcount_destroy (& km -> km_refcnt );
948
+ kmem_free (km , sizeof (dsl_key_mapping_t ));
949
+ return (ret );
950
+ }
951
951
952
952
km -> km_dsobj = dsobj ;
953
953
@@ -979,15 +979,6 @@ spa_keystore_create_mapping_impl(spa_t *spa, uint64_t dsobj,
979
979
}
980
980
981
981
return (0 );
982
-
983
- error :
984
- if (km -> km_key )
985
- spa_keystore_dsl_key_rele (spa , km -> km_key , km );
986
-
987
- refcount_destroy (& km -> km_refcnt );
988
- kmem_free (km , sizeof (dsl_key_mapping_t ));
989
-
990
- return (ret );
991
982
}
992
983
993
984
int
0 commit comments