forked from aerospike/act
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsalt.c
529 lines (397 loc) · 11.7 KB
/
salt.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
/*
* salt.c
*/
//==========================================================
// Includes
//
#include <dirent.h>
#include <execinfo.h> // for debugging
#include <fcntl.h>
#include <inttypes.h>
#include <pthread.h>
#include <signal.h> // for debugging
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <linux/fs.h>
#include <openssl/rand.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
//==========================================================
// Constants
//
const uint32_t NUM_SALT_BUFFERS = 256;
const uint32_t NUM_SALT_THREADS = 8;
const uint32_t NUM_ZERO_THREADS = 8;
const uint32_t LARGE_BLOCK_BYTES = 1024 * 128;
const uint32_t RAND_SEED_SIZE = 64;
// Linux has removed O_DIRECT, but not its functionality.
#ifndef O_DIRECT
#define O_DIRECT 00040000
#endif
//==========================================================
// Typedefs
//
typedef struct _salter {
uint8_t* p_buffer;
pthread_mutex_t lock;
uint32_t stamp;
} salter;
//==========================================================
// Globals
//
static char* g_device_name = NULL;
static uint64_t g_num_large_blocks = 0;
static salter* g_salters = NULL;
static uint8_t* g_p_zero_buffer = NULL;
static uint64_t g_blocks_per_salt_thread = 0;
static uint64_t g_blocks_per_zero_thread = 0;
static uint64_t g_extra_blocks_to_zero = 0;
static uint64_t g_extra_blocks_to_salt = 0;
static uint64_t g_extra_bytes_to_zero = 0;
//==========================================================
// Forward Declarations
//
static void* run_salt(void* pv_n);
static void* run_zero(void* pv_n);
static inline uint8_t* cf_valloc(size_t size);
static bool create_salters();
static bool create_zero_buffer();
static void destroy_salters();
static bool discover_num_blocks();
static inline int fd_get();
static bool rand_fill(uint8_t* p_buffer, uint32_t size);
static bool rand_seed();
static void set_scheduler();
static void as_sig_handle_segv(int sig_num);
static void as_sig_handle_term(int sig_num);
//==========================================================
// Main
//
int main(int argc, char* argv[]) {
signal(SIGSEGV, as_sig_handle_segv);
signal(SIGTERM , as_sig_handle_term);
if (argc != 2) {
fprintf(stdout, "usage: salt [device name]\n");
exit(0);
}
char device_name[strlen(argv[1]) + 1];
strcpy(device_name, argv[1]);
g_device_name = device_name;
set_scheduler();
if (! discover_num_blocks()) {
exit(-1);
}
//------------------------
// Begin zeroing.
fprintf(stdout, "cleaning device %s\n", g_device_name);
if (! create_zero_buffer()) {
exit(-1);
}
pthread_t zero_threads[NUM_ZERO_THREADS];
for (uint32_t n = 0; n < NUM_ZERO_THREADS; n++) {
if (pthread_create(&zero_threads[n], NULL, run_zero,
(void*)(uint64_t)n)) {
fprintf(stdout, "ERROR: creating zero thread %" PRIu32 "\n", n);
exit(-1);
}
}
for (uint32_t n = 0; n < NUM_ZERO_THREADS; n++) {
void* pv_value;
pthread_join(zero_threads[n], &pv_value);
}
free(g_p_zero_buffer);
//------------------------
// Begin salting.
fprintf(stdout, "salting device %s\n", g_device_name);
srand(time(NULL));
if (! rand_seed()) {
exit(-1);
}
salter salters[NUM_SALT_BUFFERS];
g_salters = salters;
if (! create_salters()) {
exit(-1);
}
pthread_t salt_threads[NUM_SALT_THREADS];
for (uint32_t n = 0; n < NUM_SALT_THREADS; n++) {
if (pthread_create(&salt_threads[n], NULL, run_salt,
(void*)(uint64_t)n)) {
fprintf(stdout, "ERROR: creating salt thread %" PRIu32 "\n", n);
exit(-1);
}
}
for (uint32_t n = 0; n < NUM_SALT_THREADS; n++) {
void* pv_value;
pthread_join(salt_threads[n], &pv_value);
}
destroy_salters();
return 0;
}
//==========================================================
// Thread "Run" Functions
//
//------------------------------------------------
// Runs in all (NUM_SALT_THREADS) salt_threads,
// salts a portion of the device.
//
static void* run_salt(void* pv_n) {
uint32_t n = (uint32_t)(uint64_t)pv_n;
uint64_t offset = n * g_blocks_per_salt_thread * LARGE_BLOCK_BYTES;
uint64_t blocks_to_salt = g_blocks_per_salt_thread;
uint64_t progress_blocks = 0;
bool last_thread = n + 1 == NUM_SALT_THREADS;
if (last_thread) {
blocks_to_salt += g_extra_blocks_to_salt;
progress_blocks = blocks_to_salt / 100;
if (! progress_blocks) {
progress_blocks = 1;
}
}
// fprintf(stdout, "thread %d: blks-to-salt = %" PRIu64 ", prg-blks = %"
// PRIu64 "\n", n, blocks_to_salt, progress_blocks);
int fd = fd_get();
if (fd == -1) {
fprintf(stdout, "ERROR: open in salt thread %" PRIu32 "\n", n);
// TODO - what?
return NULL;
}
if (lseek(fd, offset, SEEK_SET) != offset) {
close(fd);
fprintf(stdout, "ERROR: seek in salt thread %" PRIu32 "\n", n);
// TODO - what?
return NULL;
}
for (uint64_t b = 0; b < blocks_to_salt; b++) {
salter* p_salter = &g_salters[rand() % NUM_SALT_BUFFERS];
pthread_mutex_lock(&p_salter->lock);
*(uint32_t*)p_salter->p_buffer = p_salter->stamp++;
if (write(fd, p_salter->p_buffer, LARGE_BLOCK_BYTES) !=
(ssize_t)LARGE_BLOCK_BYTES) {
pthread_mutex_unlock(&p_salter->lock);
fprintf(stdout, "ERROR: write in salt thread %" PRIu32 "\n", n);
// TODO - what?
break;
}
pthread_mutex_unlock(&p_salter->lock);
if (progress_blocks && ! (b % progress_blocks)) {
fprintf(stdout, ".");
fflush(stdout);
}
}
if (progress_blocks) {
fprintf(stdout, "\n");
}
close(fd);
return NULL;
}
//------------------------------------------------
// Runs in all (NUM_ZERO_THREADS) zero_threads,
// zeros a portion of the device.
//
static void* run_zero(void* pv_n) {
uint32_t n = (uint32_t)(uint64_t)pv_n;
uint64_t offset = n * g_blocks_per_zero_thread * LARGE_BLOCK_BYTES;
uint64_t blocks_to_zero = g_blocks_per_zero_thread;
uint64_t progress_blocks = 0;
bool last_thread = n + 1 == NUM_ZERO_THREADS;
if (last_thread) {
blocks_to_zero += g_extra_blocks_to_zero;
progress_blocks = blocks_to_zero / 100;
if (! progress_blocks) {
progress_blocks = 1;
}
}
// fprintf(stdout, "thread %d: blks-to-zero = %" PRIu64 ", prg-blks = %"
// PRIu64 "\n", n, blocks_to_zero, progress_blocks);
int fd = fd_get();
if (fd == -1) {
fprintf(stdout, "ERROR: open in zero thread %" PRIu32 "\n", n);
// TODO - what?
return NULL;
}
if (lseek(fd, offset, SEEK_SET) != offset) {
close(fd);
fprintf(stdout, "ERROR: seek in zero thread %" PRIu32 "\n", n);
// TODO - what?
return NULL;
}
for (uint64_t b = 0; b < blocks_to_zero; b++) {
if (write(fd, g_p_zero_buffer, LARGE_BLOCK_BYTES) !=
(ssize_t)LARGE_BLOCK_BYTES) {
fprintf(stdout, "ERROR: write in zero thread %" PRIu32 "\n", n);
// TODO - what?
break;
}
if (progress_blocks && ! (b % progress_blocks)) {
fprintf(stdout, ".");
fflush(stdout);
}
}
if (progress_blocks) {
fprintf(stdout, "\n");
}
if (last_thread) {
if (write(fd, g_p_zero_buffer, g_extra_bytes_to_zero) !=
(ssize_t)g_extra_bytes_to_zero) {
fprintf(stdout, "ERROR: write in zero thread %" PRIu32 "\n", n);
}
}
close(fd);
return NULL;
}
//==========================================================
// Helpers
//
//------------------------------------------------
// Aligned memory allocation.
//
static inline uint8_t* cf_valloc(size_t size) {
void* pv;
return posix_memalign(&pv, 4096, size) == 0 ? (uint8_t*)pv : 0;
}
//------------------------------------------------
// Allocate large block sized salt buffers, etc.
//
static bool create_salters() {
for (uint32_t n = 0; n < NUM_SALT_BUFFERS; n++) {
if (! (g_salters[n].p_buffer = cf_valloc(LARGE_BLOCK_BYTES))) {
fprintf(stdout, "ERROR: salt buffer %" PRIu32 " cf_valloc()\n", n);
return false;
}
if (! rand_fill(g_salters[n].p_buffer, LARGE_BLOCK_BYTES)) {
return false;
}
pthread_mutex_init(&g_salters[n].lock, NULL);
}
return true;
}
//------------------------------------------------
// Allocate and zero one large block sized buffer.
//
static bool create_zero_buffer() {
g_p_zero_buffer = cf_valloc(LARGE_BLOCK_BYTES);
if (! g_p_zero_buffer) {
fprintf(stdout, "ERROR: zero buffer cf_valloc()\n");
return false;
}
memset(g_p_zero_buffer, 0, LARGE_BLOCK_BYTES);
return true;
}
//------------------------------------------------
// Destroy large block sized salt buffers.
//
static void destroy_salters() {
for (uint32_t n = 0; n < NUM_SALT_BUFFERS; n++) {
free(g_salters[n].p_buffer);
pthread_mutex_destroy(&g_salters[n].lock);
}
}
//------------------------------------------------
// Discover device storage capacity.
//
static bool discover_num_blocks() {
int fd = fd_get();
if (fd == -1) {
fprintf(stdout, "ERROR: opening device %s\n", g_device_name);
return false;
}
uint64_t device_bytes = 0;
ioctl(fd, BLKGETSIZE64, &device_bytes);
close(fd);
g_num_large_blocks = device_bytes / LARGE_BLOCK_BYTES;
g_extra_bytes_to_zero = device_bytes % LARGE_BLOCK_BYTES;
g_blocks_per_zero_thread = g_num_large_blocks / NUM_ZERO_THREADS;
g_blocks_per_salt_thread = g_num_large_blocks / NUM_SALT_THREADS;
g_extra_blocks_to_zero = g_num_large_blocks % NUM_ZERO_THREADS;
g_extra_blocks_to_salt = g_num_large_blocks % NUM_SALT_THREADS;
fprintf(stdout, "%s size = %" PRIu64 " bytes, %" PRIu64 " large blocks\n",
g_device_name, device_bytes, g_num_large_blocks);
return true;
}
//------------------------------------------------
// Get a file descriptor.
//
static inline int fd_get() {
return open(g_device_name, O_DIRECT | O_RDWR, S_IRUSR | S_IWUSR);
}
//------------------------------------------------
// Fill a buffer with random bits.
//
static bool rand_fill(uint8_t* p_buffer, uint32_t size) {
if (RAND_bytes(p_buffer, size) != 1) {
fprintf(stdout, "ERROR: RAND_bytes() failed\n");
return false;
}
return true;
}
//------------------------------------------------
// Seed for random fill.
//
static bool rand_seed() {
int fd = open("/dev/urandom", O_RDONLY);
if (fd == -1) {
fprintf(stdout, "ERROR: can't open /dev/urandom\n");
return false;
}
uint8_t seed_buffer[RAND_SEED_SIZE];
ssize_t read_result = read(fd, seed_buffer, RAND_SEED_SIZE);
if (read_result != (ssize_t)RAND_SEED_SIZE) {
close(fd);
fprintf(stdout, "ERROR: can't seed random number generator\n");
return false;
}
close(fd);
RAND_seed(seed_buffer, read_result);
return true;
}
//------------------------------------------------
// Set device's system block scheduler to noop.
//
static void set_scheduler() {
const char* p_slash = strrchr(g_device_name, '/');
const char* device_tag = p_slash ? p_slash + 1 : g_device_name;
char scheduler_file_name[128];
strcpy(scheduler_file_name, "/sys/block/");
strcat(scheduler_file_name, device_tag);
strcat(scheduler_file_name, "/queue/scheduler");
FILE* scheduler_file = fopen(scheduler_file_name, "w");
if (! scheduler_file) {
fprintf(stdout, "ERROR: couldn't open %s\n", scheduler_file_name);
return;
}
if (fwrite("noop", 4, 1, scheduler_file) != 1) {
fprintf(stdout, "ERROR: writing noop to %s\n", scheduler_file_name);
}
fclose(scheduler_file);
}
//==========================================================
// Debugging Helpers
//
static void as_sig_handle_segv(int sig_num) {
fprintf(stdout, "Signal SEGV received: stack trace\n");
void* bt[50];
uint sz = backtrace(bt, 50);
char** strings = backtrace_symbols(bt, sz);
for (int i = 0; i < sz; ++i) {
fprintf(stdout, "stacktrace: frame %d: %s\n", i, strings[i]);
}
free(strings);
fflush(stdout);
_exit(-1);
}
static void as_sig_handle_term(int sig_num) {
fprintf(stdout, "Signal TERM received, aborting\n");
void* bt[50];
uint sz = backtrace(bt, 50);
char** strings = backtrace_symbols(bt, sz);
for (int i = 0; i < sz; ++i) {
fprintf(stdout, "stacktrace: frame %d: %s\n", i, strings[i]);
}
free(strings);
fflush(stdout);
_exit(0);
}