@@ -27,13 +27,15 @@ pub struct SqliteInner(std::sync::Mutex<SqliteMoreInner>);
27
27
28
28
impl ExtentInner for SqliteInner {
29
29
fn gen_number ( & self ) -> Result < u64 , CrucibleError > {
30
- self . 0 . lock ( ) . unwrap ( ) . gen_number ( )
30
+ let v = self . 0 . lock ( ) . unwrap ( ) . gen_number ( ) ?;
31
+ Ok ( v)
31
32
}
32
33
fn flush_number ( & self ) -> Result < u64 , CrucibleError > {
33
- self . 0 . lock ( ) . unwrap ( ) . flush_number ( )
34
+ let v = self . 0 . lock ( ) . unwrap ( ) . flush_number ( ) ?;
35
+ Ok ( v)
34
36
}
35
37
fn dirty ( & self ) -> Result < bool , CrucibleError > {
36
- self . 0 . lock ( ) . unwrap ( ) . dirty ( )
38
+ Ok ( self . 0 . lock ( ) . unwrap ( ) . dirty ( ) )
37
39
}
38
40
39
41
fn pre_flush (
@@ -42,7 +44,8 @@ impl ExtentInner for SqliteInner {
42
44
new_gen : u64 ,
43
45
job_id : JobOrReconciliationId ,
44
46
) -> Result < ( ) , CrucibleError > {
45
- self . 0 . lock ( ) . unwrap ( ) . pre_flush ( new_flush, new_gen, job_id)
47
+ self . 0 . lock ( ) . unwrap ( ) . pre_flush ( new_flush, new_gen, job_id) ;
48
+ Ok ( ( ) )
46
49
}
47
50
48
51
fn flush_inner (
@@ -61,7 +64,8 @@ impl ExtentInner for SqliteInner {
61
64
self . 0
62
65
. lock ( )
63
66
. unwrap ( )
64
- . post_flush ( new_flush, new_gen, job_id)
67
+ . post_flush ( new_flush, new_gen, job_id) ?;
68
+ Ok ( ( ) )
65
69
}
66
70
67
71
fn read (
@@ -85,7 +89,8 @@ impl ExtentInner for SqliteInner {
85
89
write,
86
90
only_write_unwritten,
87
91
iov_max,
88
- )
92
+ ) ?;
93
+ Ok ( ( ) )
89
94
}
90
95
91
96
#[ cfg( test) ]
@@ -94,7 +99,8 @@ impl ExtentInner for SqliteInner {
94
99
block : u64 ,
95
100
count : u64 ,
96
101
) -> Result < Vec < Option < DownstairsBlockContext > > , CrucibleError > {
97
- self . 0 . lock ( ) . unwrap ( ) . get_block_contexts ( block, count)
102
+ let bc = self . 0 . lock ( ) . unwrap ( ) . get_block_contexts ( block, count) ?;
103
+ Ok ( bc)
98
104
}
99
105
100
106
#[ cfg( test) ]
@@ -158,7 +164,8 @@ impl SqliteInner {
158
164
. unwrap ( )
159
165
. truncate_encryption_contexts_and_hashes (
160
166
extent_block_indexes_and_hashes,
161
- )
167
+ ) ?;
168
+ Ok ( ( ) )
162
169
}
163
170
}
164
171
@@ -186,8 +193,42 @@ struct SqliteMoreInner {
186
193
dirty_blocks : BTreeMap < usize , Option < u64 > > ,
187
194
}
188
195
196
+ // To avoid CrucibleError having a Rusqlite variant, use an "inner" error
197
+ // variant and convert it to CrucibleError for the ExtentInner function
198
+ // implementations.
199
+ #[ derive( thiserror:: Error , Debug ) ]
200
+ enum SqliteMoreInnerError {
201
+ #[ error( "crucible error" ) ]
202
+ Crucible ( #[ from] CrucibleError ) ,
203
+
204
+ #[ error( "rusqlite error" ) ]
205
+ Rusqlite ( #[ from] rusqlite:: Error ) ,
206
+
207
+ #[ error( "io error" ) ]
208
+ Io ( #[ from] std:: io:: Error ) ,
209
+
210
+ #[ error( "anyhow error" ) ]
211
+ Anyhow ( #[ from] anyhow:: Error ) ,
212
+ }
213
+
214
+ impl From < SqliteMoreInnerError > for CrucibleError {
215
+ fn from ( e : SqliteMoreInnerError ) -> CrucibleError {
216
+ match e {
217
+ SqliteMoreInnerError :: Crucible ( e) => e,
218
+
219
+ SqliteMoreInnerError :: Rusqlite ( e) => {
220
+ CrucibleError :: GenericError ( format ! ( "{:?}" , e) )
221
+ }
222
+
223
+ SqliteMoreInnerError :: Io ( e) => e. into ( ) ,
224
+
225
+ SqliteMoreInnerError :: Anyhow ( e) => e. into ( ) ,
226
+ }
227
+ }
228
+ }
229
+
189
230
impl SqliteMoreInner {
190
- fn gen_number ( & self ) -> Result < u64 , CrucibleError > {
231
+ fn gen_number ( & self ) -> Result < u64 , SqliteMoreInnerError > {
191
232
let mut stmt = self . metadb . prepare_cached (
192
233
"SELECT value FROM metadata where name='gen_number'" ,
193
234
) ?;
@@ -198,7 +239,7 @@ impl SqliteMoreInner {
198
239
Ok ( gen_number)
199
240
}
200
241
201
- fn flush_number ( & self ) -> Result < u64 , CrucibleError > {
242
+ fn flush_number ( & self ) -> Result < u64 , SqliteMoreInnerError > {
202
243
let mut stmt = self . metadb . prepare_cached (
203
244
"SELECT value FROM metadata where name='flush_number'" ,
204
245
) ?;
@@ -209,24 +250,22 @@ impl SqliteMoreInner {
209
250
Ok ( flush_number)
210
251
}
211
252
212
- fn dirty ( & self ) -> Result < bool , CrucibleError > {
213
- Ok ( self . dirty . get ( ) )
253
+ fn dirty ( & self ) -> bool {
254
+ self . dirty . get ( )
214
255
}
215
256
216
257
fn pre_flush (
217
258
& mut self ,
218
259
_new_flush : u64 ,
219
260
_new_gen : u64 ,
220
261
job_id : JobOrReconciliationId ,
221
- ) -> Result < ( ) , CrucibleError > {
262
+ ) {
222
263
// Used for profiling
223
264
let n_dirty_blocks = self . dirty_blocks . len ( ) as u64 ;
224
265
225
266
cdt:: extent__flush__start!( || {
226
267
( job_id. get( ) , self . extent_number. 0 , n_dirty_blocks)
227
268
} ) ;
228
-
229
- Ok ( ( ) )
230
269
}
231
270
232
271
fn flush_inner (
@@ -262,7 +301,7 @@ impl SqliteMoreInner {
262
301
new_flush : u64 ,
263
302
new_gen : u64 ,
264
303
job_id : JobOrReconciliationId ,
265
- ) -> Result < ( ) , CrucibleError > {
304
+ ) -> Result < ( ) , SqliteMoreInnerError > {
266
305
// Clear old block contexts. In order to be crash consistent, only
267
306
// perform this after the extent fsync is done. For each block
268
307
// written since the last flush, remove all block context rows where
@@ -404,7 +443,7 @@ impl SqliteMoreInner {
404
443
write : & ExtentWrite ,
405
444
only_write_unwritten : bool ,
406
445
_iov_max : usize ,
407
- ) -> Result < ( ) , CrucibleError > {
446
+ ) -> Result < ( ) , SqliteMoreInnerError > {
408
447
check_input ( self . extent_size , write. offset , write. data . len ( ) ) ?;
409
448
410
449
/*
@@ -622,7 +661,7 @@ impl SqliteMoreInner {
622
661
& self ,
623
662
block : u64 ,
624
663
count : u64 ,
625
- ) -> Result < Vec < Option < DownstairsBlockContext > > , CrucibleError > {
664
+ ) -> Result < Vec < Option < DownstairsBlockContext > > , SqliteMoreInnerError > {
626
665
let stmt =
627
666
"SELECT block, hash, nonce, tag, on_disk_hash FROM block_context \
628
667
WHERE block BETWEEN ?1 AND ?2";
@@ -695,7 +734,8 @@ impl SqliteMoreInner {
695
734
"extent {}: incomplete read \
696
735
(expected {block_size}, got {num_bytes})",
697
736
self . extent_number
698
- ) ) ) ;
737
+ ) )
738
+ . into ( ) ) ;
699
739
}
700
740
let hash = integrity_hash ( & [ & buffer] ) ;
701
741
known_hashes[ i] = Some ( hash) ;
@@ -964,7 +1004,7 @@ impl SqliteMoreInner {
964
1004
Ok ( out)
965
1005
}
966
1006
967
- fn set_dirty ( & self ) -> Result < ( ) > {
1007
+ fn set_dirty ( & self ) -> Result < ( ) , SqliteMoreInnerError > {
968
1008
if !self . dirty . get ( ) {
969
1009
let _ = self
970
1010
. metadb
@@ -980,7 +1020,7 @@ impl SqliteMoreInner {
980
1020
fn set_block_context (
981
1021
& self ,
982
1022
block_context : & DownstairsBlockContext ,
983
- ) -> Result < ( ) , CrucibleError > {
1023
+ ) -> Result < ( ) , SqliteMoreInnerError > {
984
1024
let stmt =
985
1025
"INSERT OR IGNORE INTO block_context (block, hash, nonce, tag, on_disk_hash) \
986
1026
VALUES (?1, ?2, ?3, ?4, ?5)";
@@ -1016,7 +1056,7 @@ impl SqliteMoreInner {
1016
1056
fn truncate_encryption_contexts_and_hashes (
1017
1057
& self ,
1018
1058
extent_block_indexes_and_hashes : & [ ( usize , u64 ) ] ,
1019
- ) -> Result < ( ) > {
1059
+ ) -> Result < ( ) , SqliteMoreInnerError > {
1020
1060
let tx = self . metadb . unchecked_transaction ( ) ?;
1021
1061
1022
1062
self . truncate_encryption_contexts_and_hashes_with_tx (
@@ -1038,7 +1078,7 @@ impl SqliteMoreInner {
1038
1078
& self ,
1039
1079
extent_block_indexes_and_hashes : impl ExactSizeIterator < Item = ( usize , u64 ) > ,
1040
1080
tx : & Transaction ,
1041
- ) -> Result < ( ) > {
1081
+ ) -> Result < ( ) , SqliteMoreInnerError > {
1042
1082
let n_blocks = extent_block_indexes_and_hashes. len ( ) ;
1043
1083
cdt:: extent__context__truncate__start!( || n_blocks as u64 ) ;
1044
1084
@@ -1134,7 +1174,7 @@ impl SqliteMoreInner {
1134
1174
& mut self ,
1135
1175
force_override_dirty : bool ,
1136
1176
) -> Result < ( ) , CrucibleError > {
1137
- if !force_override_dirty && !self . dirty ( ) ? {
1177
+ if !force_override_dirty && !self . dirty ( ) {
1138
1178
return Ok ( ( ) ) ;
1139
1179
}
1140
1180
0 commit comments