-
Notifications
You must be signed in to change notification settings - Fork 77
/
erase.c
267 lines (236 loc) · 8.45 KB
/
erase.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
/*
* s3backer - FUSE-based single file backing store via Amazon S3
*
* Copyright 2008-2023 Archie L. Cobbs <archie.cobbs@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations including
* the two.
*
* You must obey the GNU General Public License in all respects for all
* of the code used other than OpenSSL. If you modify file(s) with this
* exception, you may extend this exception to your version of the
* file(s), but you are not obligated to do so. If you do not wish to do
* so, delete this exception statement from your version. If you delete
* this exception statement from all source files in the program, then
* also delete it here.
*/
#include "s3backer.h"
#include "block_cache.h"
#include "ec_protect.h"
#include "zero_cache.h"
#include "fuse_ops.h"
#include "http_io.h"
#include "test_io.h"
#include "s3b_config.h"
#include "erase.h"
#include "util.h"
#define BLOCKS_PER_DOT 0x100
#define MAX_QUEUE_LENGTH 100000
#define NUM_ERASURE_THREADS 25
// Erasure state
struct erase_state {
struct s3backer_store *s3b;
s3b_block_t queue[MAX_QUEUE_LENGTH];
u_int qlen;
pthread_t threads[NUM_ERASURE_THREADS];
int quiet;
int stopping;
uintmax_t count;
bitmap_t *seen;
pthread_mutex_t mutex;
pthread_cond_t thread_wakeup;
pthread_cond_t queue_not_full;
pthread_cond_t queue_empty;
};
// Internal functions
static block_list_func_t erase_list_callback;
static void *erase_thread_main(void *arg);
int
s3backer_erase(struct s3b_config *config)
{
struct erase_state state;
struct erase_state *const priv = &state;
char response[10];
int num_threads;
int ok = 0;
int r;
// Double check with user
if (!config->force) {
warnx("`--erase' flag given: erasing all blocks in %s", config->description);
fprintf(stderr, "s3backer: is this correct? [y/N] ");
*response = '\0';
if (fgets(response, sizeof(response), stdin) != NULL) {
while (*response && isspace(response[strlen(response) - 1]))
response[strlen(response) - 1] = '\0';
}
if (strcasecmp(response, "y") != 0 && strcasecmp(response, "yes") != 0) {
warnx("not confirmed");
goto fail0;
}
}
// Initialize state
memset(priv, 0, sizeof(*priv));
priv->quiet = config->quiet;
if ((r = pthread_mutex_init(&priv->mutex, NULL)) != 0) {
warnx("pthread_mutex_init: %s", strerror(r));
goto fail0;
}
if ((r = pthread_cond_init(&priv->thread_wakeup, NULL)) != 0) {
warnx("pthread_cond_init: %s", strerror(r));
goto fail1;
}
if ((r = pthread_cond_init(&priv->queue_not_full, NULL)) != 0) {
warnx("pthread_cond_init: %s", strerror(r));
goto fail2;
}
if ((r = pthread_cond_init(&priv->queue_empty, NULL)) != 0) {
warnx("pthread_cond_init: %s", strerror(r));
goto fail3;
}
if ((priv->seen = bitmap_init(config->num_blocks, 0)) == NULL) {
r = errno;
warnx("calloc: %s", strerror(r));
goto fail4;
}
for (num_threads = 0; num_threads < NUM_ERASURE_THREADS; num_threads++) {
if ((r = pthread_create(&priv->threads[num_threads], NULL, erase_thread_main, priv)) != 0)
goto fail5;
}
// Logging
if (!config->quiet) {
fprintf(stderr, "s3backer: erasing non-zero blocks...");
fflush(stderr);
}
// Create temporary lower layer
if ((priv->s3b = config->test ? test_io_create(&config->test_io) : http_io_create(&config->http_io)) == NULL) {
warnx(config->test ? "test_io_create" : "http_io_create");
goto fail5;
}
// Iterate over non-zero blocks
if ((r = (*priv->s3b->survey_non_zero)(priv->s3b, erase_list_callback, priv)) != 0) {
warnx("can't list blocks: %s", strerror(r));
goto fail5;
}
// Wait for queue to drain
pthread_mutex_lock(&priv->mutex);
while (priv->qlen > 0)
pthread_cond_wait(&priv->queue_empty, &priv->mutex);
CHECK_RETURN(pthread_mutex_unlock(&priv->mutex));
// Clear mount token
if ((r = (*priv->s3b->set_mount_token)(priv->s3b, NULL, 0)) != 0) {
warnx("can't clear mount token: %s", strerror(r));
goto fail5;
}
// Success
ok = 1;
// Clean up
fail5:
pthread_mutex_lock(&priv->mutex);
priv->stopping = 1;
pthread_cond_broadcast(&priv->thread_wakeup);
CHECK_RETURN(pthread_mutex_unlock(&priv->mutex));
while (num_threads > 0) {
if ((r = pthread_join(priv->threads[--num_threads], NULL)) != 0)
warnx("pthread_join: %s", strerror(r));
}
if (priv->s3b != NULL) {
if (ok && !config->quiet) {
fprintf(stderr, "done\n");
warnx("erased %ju non-zero blocks", priv->count);
}
(*priv->s3b->shutdown)(priv->s3b);
(*priv->s3b->destroy)(priv->s3b);
}
bitmap_free(&priv->seen);
fail4:
pthread_cond_destroy(&priv->queue_empty);
fail3:
pthread_cond_destroy(&priv->queue_not_full);
fail2:
pthread_cond_destroy(&priv->thread_wakeup);
fail1:
pthread_mutex_destroy(&priv->mutex);
fail0:
return ok ? 0 : -1;
}
static int
erase_list_callback(void *arg, const s3b_block_t *block_nums, u_int num_blocks)
{
struct erase_state *const priv = arg;
pthread_mutex_lock(&priv->mutex);
while (num_blocks-- > 0) {
const s3b_block_t block_num = *block_nums++;
if (bitmap_test(priv->seen, block_num))
continue; // already reported to us
bitmap_set(priv->seen, block_num, 1);
while (priv->qlen == MAX_QUEUE_LENGTH)
pthread_cond_wait(&priv->queue_not_full, &priv->mutex);
priv->queue[priv->qlen++] = block_num;
}
pthread_cond_broadcast(&priv->thread_wakeup);
CHECK_RETURN(pthread_mutex_unlock(&priv->mutex));
return 0;
}
static void *
erase_thread_main(void *arg)
{
struct erase_state *const priv = arg;
s3b_block_t block_num;
int r;
// Acquire lock
pthread_mutex_lock(&priv->mutex);
// Erase blocks until there are no more
while (1) {
// Is there a block to erase?
if (priv->qlen > 0) {
// Grab next bock
if (priv->qlen == MAX_QUEUE_LENGTH)
pthread_cond_broadcast(&priv->queue_not_full);
block_num = priv->queue[--priv->qlen];
if (priv->qlen == 0)
pthread_cond_signal(&priv->queue_empty);
// Do block deletion
CHECK_RETURN(pthread_mutex_unlock(&priv->mutex));
r = (*priv->s3b->write_block)(priv->s3b, block_num, NULL, NULL, NULL, NULL);
pthread_mutex_lock(&priv->mutex);
// Check for error
if (r != 0) {
warnx("can't delete block %0*jx: %s", S3B_BLOCK_NUM_DIGITS, (uintmax_t)block_num, strerror(r));
continue;
}
// Update count and output a dot
if ((++priv->count % BLOCKS_PER_DOT) == 0 && !priv->quiet) {
fprintf(stderr, ".");
fflush(stderr);
}
// Spin again
continue;
}
// Are we done?
if (priv->stopping)
break;
// Wait for something to do
pthread_cond_wait(&priv->thread_wakeup, &priv->mutex);
}
// Done
CHECK_RETURN(pthread_mutex_unlock(&priv->mutex));
return NULL;
}