/
storage_delete.c
336 lines (274 loc) · 6.88 KB
/
storage_delete.c
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
#include "bsdtar_platform.h"
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "crypto.h"
#include "netpacket.h"
#include "netproto.h"
#include "storage_internal.h"
#include "warnp.h"
#include "storage.h"
/*
* Maximum number of delete operations which are allowed to be pending
* before storage_delete_file will block.
*/
#define MAXPENDING_DELETE 1024
struct storage_delete_internal {
/* Transaction parameters. */
NETPACKET_CONNECTION * NPC;
uint64_t machinenum;
uint8_t nonce[32];
/* Number of pending deletes. */
size_t npending;
};
struct delete_file_internal {
/* Pointer to transaction to which this belongs. */
struct storage_delete_internal * S;
/* General state information. */
uint64_t machinenum;
int done;
/* Parameters used in delete_file. */
uint8_t class;
uint8_t name[32];
uint8_t nonce[32];
uint8_t status;
};
static sendpacket_callback callback_delete_file_send;
static handlepacket_callback callback_delete_file_response;
/**
* storage_delete_start(machinenum, lastseq, seqnum):
* Start a delete transaction, presuming that ${lastseq} is the the sequence
* number of the last committed transaction, or zeroes if there is no
* previous transaction; and store the sequence number of the new transaction
* into ${seqnum}.
*/
STORAGE_D *
storage_delete_start(uint64_t machinenum, const uint8_t lastseq[32],
uint8_t seqnum[32])
{
struct storage_delete_internal * S;
/* Allocate memory. */
if ((S = malloc(sizeof(struct storage_delete_internal))) == NULL)
goto err0;
/* Store machine number. */
S->machinenum = machinenum;
/* No pending deletes so far. */
S->npending = 0;
/* Open netpacket connection. */
if ((S->NPC = netpacket_open()) == NULL)
goto err1;
/* Start a delete transaction. */
if (storage_transaction_start_delete(S->NPC, machinenum, lastseq,
S->nonce))
goto err2;
/* Copy the transaction nonce out. */
memcpy(seqnum, S->nonce, 32);
/* Success! */
return (S);
err2:
netpacket_close(S->NPC);
err1:
free(S);
err0:
/* Failure! */
return (NULL);
}
/**
* storage_fsck_start(machinenum, seqnum):
* Start a fsck transaction, and store the sequence number of said
* transaction into ${seqnum}.
*/
STORAGE_D *
storage_fsck_start(uint64_t machinenum, uint8_t seqnum[32])
{
struct storage_delete_internal * S;
/* Allocate memory. */
if ((S = malloc(sizeof(struct storage_delete_internal))) == NULL)
goto err0;
/* Store machine number. */
S->machinenum = machinenum;
/* No pending deletes so far. */
S->npending = 0;
/* Open netpacket connection. */
if ((S->NPC = netpacket_open()) == NULL)
goto err1;
/* Start a delete transaction. */
if (storage_transaction_start_fsck(S->NPC, machinenum, S->nonce))
goto err2;
/* Copy the transaction nonce out. */
memcpy(seqnum, S->nonce, 32);
/* Success! */
return (S);
err2:
netpacket_close(S->NPC);
err1:
free(S);
err0:
/* Failure! */
return (NULL);
}
/**
* storage_delete_file(S, class, name):
* Delete the file ${name} from class ${class} as part of the delete
* transaction associated with the cookie ${S}.
*/
int
storage_delete_file(STORAGE_D * S, char class, const uint8_t name[32])
{
struct delete_file_internal * C;
/* Create delete cookie. */
if ((C = malloc(sizeof(struct delete_file_internal))) == NULL)
goto err0;
C->S = S;
C->machinenum = S->machinenum;
C->class = class;
memcpy(C->name, name, 32);
memcpy(C->nonce, S->nonce, 32);
C->done = 0;
/* We're issuing a delete operation. */
S->npending += 1;
/*
* Make sure the pending operation queue isn't too large before we
* add yet another operation to it.
*/
if (S->npending > MAXPENDING_DELETE) {
/* Avoid silly window syndrome. */
while (S->npending > MAXPENDING_DELETE / 2 + 1) {
if (network_select(1))
goto err1;
}
}
/* Ask the netpacket layer to send a request and get a response. */
if (netpacket_op(S->NPC, callback_delete_file_send, C))
goto err0;
/* Success! */
return (0);
err1:
free(C);
err0:
/* Failure! */
return (-1);
}
static int
callback_delete_file_send(void * cookie, NETPACKET_CONNECTION * NPC)
{
struct delete_file_internal * C = cookie;
/* Ask the server to delete the file in question. */
return (netpacket_delete_file(NPC, C->machinenum, C->class,
C->name, C->nonce, callback_delete_file_response));
}
static int
callback_delete_file_response(void * cookie,
NETPACKET_CONNECTION * NPC, int status, uint8_t packettype,
const uint8_t * packetbuf, size_t packetlen)
{
struct delete_file_internal * C = cookie;
(void)packetlen; /* UNUSED */
(void)NPC; /* UNUSED */
/* Handle errors. */
if (status != NETWORK_STATUS_OK) {
netproto_printerr(status);
goto err1;
}
/* Make sure we received the right type of packet. */
if (packettype != NETPACKET_DELETE_FILE_RESPONSE)
goto err2;
/* Verify packet hmac. */
switch (netpacket_hmac_verify(packettype, C->nonce,
packetbuf, 34, CRYPTO_KEY_AUTH_DELETE)) {
case 1:
goto err2;
case -1:
goto err1;
}
/* Make sure that the packet corresponds to the right file. */
if ((packetbuf[1] != C->class) ||
(memcmp(&packetbuf[2], C->name, 32)))
goto err2;
/* Parse status returned by server. */
switch (packetbuf[0]) {
case 0:
/* This delete operation is no longer pending. */
C->S->npending -= 1;
break;
case 1:
warn0("Cannot delete file: File does not exist");
goto err1;
case 2:
/* Bad nonce. */
warn0("Delete transaction interrupted");
goto err1;
default:
goto err2;
}
/* Free delete cookie. */
free(C);
/* Success! */
return (0);
err2:
netproto_printerr(NETPROTO_STATUS_PROTERR);
err1:
free(C);
/* Failure! */
return (-1);
}
/**
* storage_delete_flush(S):
* Make sure all operations performed as part of the transaction associated
* with the cookie ${S} have been safely stored in preparation for being
* committed.
*/
int
storage_delete_flush(STORAGE_D * S)
{
/* Wait until all pending deletes have been completed. */
while (S->npending > 0) {
if (network_select(1))
goto err0;
}
/* Success! */
return (0);
err0:
/* Failure! */
return (-1);
}
/**
* storage_delete_end(S):
* Make sure that all operations performed as part of the transaction
* associated with the cookie ${S} have been safely stored in
* preparation for being committed; and close the transaction and free
* associated memory.
*/
int
storage_delete_end(STORAGE_D * S)
{
/* Flush any pending deletes. */
if (storage_delete_flush(S))
goto err2;
/* Close netpacket connection. */
if (netpacket_close(S->NPC))
goto err1;
/* Free structure. */
free(S);
/* Success! */
return (0);
err2:
netpacket_close(S->NPC);
err1:
free(S);
/* Failure! */
return (-1);
}
/**
* storage_delete_free(S):
* Free any memory allocated as part of the delete transcation associated
* with the cookie ${S}; the transaction will not be committed.
*/
void
storage_delete_free(STORAGE_D * S)
{
/* Close netpacket connection. */
netpacket_close(S->NPC);
/* Free structure. */
free(S);
}