forked from dayne/zunkfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chunk-db.c
165 lines (131 loc) · 3.29 KB
/
chunk-db.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
#define _GNU_SOURCE
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
#include <signal.h>
#include <openssl/sha.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "zunkfs.h"
#include "chunk-db.h"
#include "utils.h"
static chunkdb_ctor *ctor_list = NULL;
static int ctor_count = 0;
static struct chunk_db **chunkdb_list = NULL;
static int chunkdb_count = 0;
static inline int cmp_digest(const unsigned char *a, const unsigned char *b)
{
return memcmp(a, b, CHUNK_DIGEST_LEN);
}
static inline unsigned char *digest_chunk(const unsigned char *chunk,
unsigned char *digest)
{
SHA1(chunk, CHUNK_SIZE, digest);
return digest;
}
int verify_chunk(const unsigned char *chunk, const unsigned char *digest)
{
unsigned char tmp_digest[CHUNK_DIGEST_LEN];
if (cmp_digest(digest, digest_chunk(chunk, tmp_digest))) {
TRACE("chunk failed verification: %s vs %s\n",
digest_string(digest),
digest_string(tmp_digest));
return 0;
}
return 1;
}
const char *__digest_string(const unsigned char *digest, char *strbuf)
{
static const char digit[] = "0123456789abcdef";
char *ptr;
int i;
for (i = 0, ptr = strbuf; i < CHUNK_DIGEST_LEN; i ++) {
*ptr++ = digit[digest[i] & 0xf];
*ptr++ = digit[(digest[i] >> 4) & 0xf];
}
*ptr = 0;
return strbuf;
}
#define INT_CHUNK_SIZE ((CHUNK_SIZE + sizeof(int) - 1) / sizeof(int))
int random_chunk_digest(unsigned char *digest)
{
int i, chunk_data[INT_CHUNK_SIZE] = {0};
for (i = 0; i < INT_CHUNK_SIZE; i ++)
chunk_data[i] = rand();
return write_chunk((void *)chunk_data, digest);
}
void register_chunkdb(chunkdb_ctor ctor)
{
int n = ctor_count ++;
ctor_list = realloc(ctor_list, ctor_count * sizeof(chunkdb_ctor));
if (!ctor_list)
panic("Failed to resize list of chunk database types.\n");
ctor_list[n] = ctor;
}
int add_chunkdb(int mode, const char *spec)
{
struct chunk_db *cdb;
int i, n;
for (i = 0; i < ctor_count; i ++) {
cdb = ctor_list[i](mode, spec);
if (cdb)
goto found;
}
return -ENOENT;
found:
if (IS_ERR(cdb))
return -PTR_ERR(cdb);
n = chunkdb_count ++;
chunkdb_list = realloc(chunkdb_list,
chunkdb_count * sizeof(struct chunk_db *));
if (!chunkdb_list)
panic("Failed to resize list of chunk databases.\n");
chunkdb_list[n] = cdb;
return 0;
}
int read_chunk(unsigned char *chunk, const unsigned char *digest)
{
struct chunk_db *cdb;
int i, len;
for (i = 0; i < chunkdb_count; i ++) {
cdb = chunkdb_list[i];
if (cdb->read_chunk) {
len = cdb->read_chunk(chunk, digest, cdb->db_info);
if (len > 0 && verify_chunk(chunk, digest))
goto cache_chunk;
}
}
TRACE("chunk not found: %s\n", digest_string(digest));
return -EIO;
cache_chunk:
while (i--) {
cdb = chunkdb_list[i];
if (cdb->write_chunk)
cdb->write_chunk(chunk, digest, cdb->db_info);
}
return len;
}
int write_chunk(const unsigned char *chunk, unsigned char *digest)
{
struct chunk_db *cdb;
int i, err;
digest_chunk(chunk, digest);
TRACE("digest=%s\n", digest_string(digest));
for (i = 0; i < chunkdb_count; i ++) {
cdb = chunkdb_list[i];
if (cdb->write_chunk) {
err = cdb->write_chunk(chunk, digest, cdb->db_info);
if (err > 0)
return err;
}
}
return -EIO;
}