1 |
|
|
/** |
2 |
|
|
* This file is part of the CernVM File System. |
3 |
|
|
*/ |
4 |
|
|
|
5 |
|
|
#include "cvmfs_config.h" |
6 |
|
|
#include "compat.h" |
7 |
|
|
|
8 |
|
|
#include <openssl/md5.h> |
9 |
|
|
|
10 |
|
|
#include <cstdlib> |
11 |
|
|
#include <cstring> |
12 |
|
|
|
13 |
|
|
using namespace std; // NOLINT |
14 |
|
|
|
15 |
|
|
namespace compat { |
16 |
|
|
|
17 |
|
|
namespace shash_v1 { |
18 |
|
|
|
19 |
|
|
const char *kSuffixes[] = {"", "", "-rmd160", ""}; |
20 |
|
|
|
21 |
|
|
Md5::Md5(const char *chars, const unsigned length) { |
22 |
|
|
algorithm = kMd5; |
23 |
|
|
|
24 |
|
|
MD5_CTX md5_state; |
25 |
|
|
MD5_Init(&md5_state); |
26 |
|
|
MD5_Update(&md5_state, reinterpret_cast<const unsigned char *>(chars), |
27 |
|
|
length); |
28 |
|
|
MD5_Final(digest, &md5_state); |
29 |
|
|
} |
30 |
|
|
|
31 |
|
|
void MigrateAny(const Any *old_hash, shash::Any *new_hash) { |
32 |
|
|
memcpy(new_hash->digest, old_hash->digest, kDigestSizes[kAny]); |
33 |
|
|
new_hash->algorithm = shash::Algorithms(old_hash->algorithm); |
34 |
|
|
new_hash->suffix = shash::kSuffixNone; |
35 |
|
|
} |
36 |
|
|
|
37 |
|
|
} // namespace shash_v1 |
38 |
|
|
|
39 |
|
|
|
40 |
|
|
//------------------------------------------------------------------------------ |
41 |
|
|
|
42 |
|
|
|
43 |
|
|
namespace shash_v2 { |
44 |
|
|
|
45 |
|
|
const char *kSuffixes[] = {"", "", "-rmd160", ""}; |
46 |
|
|
|
47 |
|
|
void MigrateAny(const Any *old_hash, shash::Any *new_hash) { |
48 |
|
|
memcpy(new_hash->digest, old_hash->digest, kDigestSizes[kAny]); |
49 |
|
|
new_hash->algorithm = shash::Algorithms(old_hash->algorithm); |
50 |
|
|
new_hash->suffix = old_hash->suffix; |
51 |
|
|
} |
52 |
|
|
|
53 |
|
|
} // namespace shash_v2 |
54 |
|
|
|
55 |
|
|
|
56 |
|
|
//------------------------------------------------------------------------------ |
57 |
|
|
|
58 |
|
|
|
59 |
|
|
namespace inode_tracker { |
60 |
|
|
|
61 |
|
|
bool InodeContainer::ConstructPath(const uint64_t inode, PathString *path) { |
62 |
|
|
InodeMap::const_iterator needle = map_.find(inode); |
63 |
|
|
if (needle == map_.end()) |
64 |
|
|
return false; |
65 |
|
|
|
66 |
|
|
if (needle->second.name.IsEmpty()) |
67 |
|
|
return true; |
68 |
|
|
|
69 |
|
|
bool retval = ConstructPath(needle->second.parent_inode, path); |
70 |
|
|
path->Append("/", 1); |
71 |
|
|
path->Append(needle->second.name.GetChars(), |
72 |
|
|
needle->second.name.GetLength()); |
73 |
|
|
assert(retval); |
74 |
|
|
return retval; |
75 |
|
|
} |
76 |
|
|
|
77 |
|
|
|
78 |
|
|
InodeTracker::~InodeTracker() { |
79 |
|
|
pthread_mutex_destroy(lock_); |
80 |
|
|
free(lock_); |
81 |
|
|
} |
82 |
|
|
|
83 |
|
|
void Migrate(InodeTracker *old_tracker, glue::InodeTracker *new_tracker) { |
84 |
|
|
InodeContainer::InodeMap::const_iterator i, iEnd; |
85 |
|
|
i = old_tracker->inode2path_.map_.begin(); |
86 |
|
|
iEnd = old_tracker->inode2path_.map_.end(); |
87 |
|
|
for (; i != iEnd; ++i) { |
88 |
|
|
uint64_t inode = i->first; |
89 |
|
|
uint32_t references = i->second.references; |
90 |
|
|
PathString path; |
91 |
|
|
old_tracker->inode2path_.ConstructPath(inode, &path); |
92 |
|
|
new_tracker->VfsGetBy(inode, references, path); |
93 |
|
|
} |
94 |
|
|
} |
95 |
|
|
|
96 |
|
|
} // namespace inode_tracker |
97 |
|
|
|
98 |
|
|
|
99 |
|
|
//------------------------------------------------------------------------------ |
100 |
|
|
|
101 |
|
|
|
102 |
|
|
namespace inode_tracker_v2 { |
103 |
|
|
|
104 |
|
|
static uint32_t hasher_md5(const shash_v1::Md5 &key) { |
105 |
|
|
return (uint32_t) *((uint32_t *)key.digest + 1); // NOLINT |
106 |
|
|
} |
107 |
|
|
|
108 |
|
|
static uint32_t hasher_inode(const uint64_t &inode) { |
109 |
|
|
return MurmurHash2(&inode, sizeof(inode), 0x07387a4f); |
110 |
|
|
} |
111 |
|
|
|
112 |
|
|
void Migrate(InodeTracker *old_tracker, glue::InodeTracker *new_tracker) { |
113 |
|
|
old_tracker->inode_map_.map_.hasher_ = hasher_inode; |
114 |
|
|
old_tracker->path_map_.map_.hasher_ = hasher_md5; |
115 |
|
|
|
116 |
|
|
SmallHashDynamic<uint64_t, uint32_t> *old_inodes = |
117 |
|
|
&old_tracker->inode_references_.map_; |
118 |
|
|
for (unsigned i = 0; i < old_inodes->capacity_; ++i) { |
119 |
|
|
const uint64_t inode = old_inodes->keys_[i]; |
120 |
|
|
if (inode == 0) continue; |
121 |
|
|
|
122 |
|
|
const uint32_t references = old_inodes->values_[i]; |
123 |
|
|
PathString path; |
124 |
|
|
bool retval = old_tracker->FindPath(inode, &path); |
125 |
|
|
assert(retval); |
126 |
|
|
new_tracker->VfsGetBy(inode, references, path); |
127 |
|
|
} |
128 |
|
|
} |
129 |
|
|
|
130 |
|
|
} // namespace inode_tracker_v2 |
131 |
|
|
|
132 |
|
|
|
133 |
|
|
//------------------------------------------------------------------------------ |
134 |
|
|
|
135 |
|
|
|
136 |
|
|
namespace inode_tracker_v3 { |
137 |
|
|
|
138 |
|
|
static uint32_t hasher_md5(const shash_v1::Md5 &key) { |
139 |
|
|
return (uint32_t) *((uint32_t *)key.digest + 1); // NOLINT |
140 |
|
|
} |
141 |
|
|
|
142 |
|
|
static uint32_t hasher_inode(const uint64_t &inode) { |
143 |
|
|
return MurmurHash2(&inode, sizeof(inode), 0x07387a4f); |
144 |
|
|
} |
145 |
|
|
|
146 |
|
|
void Migrate(InodeTracker *old_tracker, glue::InodeTracker *new_tracker) { |
147 |
|
|
old_tracker->inode_map_.map_.SetHasher(hasher_inode); |
148 |
|
|
old_tracker->path_map_.map_.SetHasher(hasher_md5); |
149 |
|
|
old_tracker->path_map_.path_store_.map_.SetHasher(hasher_md5); |
150 |
|
|
|
151 |
|
|
SmallHashDynamic<uint64_t, uint32_t> *old_inodes = |
152 |
|
|
&old_tracker->inode_references_.map_; |
153 |
|
|
for (unsigned i = 0; i < old_inodes->capacity(); ++i) { |
154 |
|
|
const uint64_t inode = old_inodes->keys()[i]; |
155 |
|
|
if (inode == 0) continue; |
156 |
|
|
|
157 |
|
|
const uint32_t references = old_inodes->values()[i]; |
158 |
|
|
PathString path; |
159 |
|
|
bool retval = old_tracker->FindPath(inode, &path); |
160 |
|
|
assert(retval); |
161 |
|
|
new_tracker->VfsGetBy(inode, references, path); |
162 |
|
|
} |
163 |
|
|
} |
164 |
|
|
|
165 |
|
|
} // namespace inode_tracker_v3 |
166 |
|
|
|
167 |
|
|
|
168 |
|
|
//------------------------------------------------------------------------------ |
169 |
|
|
|
170 |
|
|
|
171 |
|
|
namespace chunk_tables { |
172 |
|
|
|
173 |
|
|
ChunkTables::~ChunkTables() { |
174 |
|
|
pthread_mutex_destroy(lock); |
175 |
|
|
free(lock); |
176 |
|
|
for (unsigned i = 0; i < kNumHandleLocks; ++i) { |
177 |
|
|
pthread_mutex_destroy(handle_locks.At(i)); |
178 |
|
|
free(handle_locks.At(i)); |
179 |
|
|
} |
180 |
|
|
} |
181 |
|
|
|
182 |
|
|
void Migrate(ChunkTables *old_tables, ::ChunkTables *new_tables) { |
183 |
|
|
new_tables->next_handle = old_tables->next_handle; |
184 |
|
|
new_tables->handle2fd = old_tables->handle2fd; |
185 |
|
|
new_tables->inode2references = old_tables->inode2references; |
186 |
|
|
|
187 |
|
|
SmallHashDynamic<uint64_t, FileChunkReflist> *old_inode2chunks = |
188 |
|
|
&old_tables->inode2chunks; |
189 |
|
|
for (unsigned keyno = 0; keyno < old_inode2chunks->capacity(); ++keyno) { |
190 |
|
|
const uint64_t inode = old_inode2chunks->keys()[keyno]; |
191 |
|
|
if (inode == 0) continue; |
192 |
|
|
|
193 |
|
|
FileChunkReflist *old_reflist = &old_inode2chunks->values()[keyno]; |
194 |
|
|
BigVector<FileChunk> *old_list = old_reflist->list; |
195 |
|
|
FileChunkList *new_list = new FileChunkList(); |
196 |
|
|
for (unsigned i = 0; i < old_list->size(); ++i) { |
197 |
|
|
const FileChunk *old_chunk = old_list->AtPtr(i); |
198 |
|
|
off_t offset = old_chunk->offset(); |
199 |
|
|
size_t size = old_chunk->size(); |
200 |
|
|
shash::Any hash; |
201 |
|
|
shash_v1::MigrateAny(&old_chunk->content_hash_, &hash); |
202 |
|
|
new_list->PushBack(::FileChunk(hash, offset, size)); |
203 |
|
|
} |
204 |
|
|
delete old_list; |
205 |
|
|
::FileChunkReflist new_reflist(new_list, old_reflist->path, |
206 |
|
|
zlib::kZlibDefault, false); |
207 |
|
|
new_tables->inode2chunks.Insert(inode, new_reflist); |
208 |
|
|
} |
209 |
|
|
} |
210 |
|
|
|
211 |
|
|
} // namespace chunk_tables |
212 |
|
|
|
213 |
|
|
|
214 |
|
|
//------------------------------------------------------------------------------ |
215 |
|
|
|
216 |
|
|
|
217 |
|
|
namespace chunk_tables_v2 { |
218 |
|
|
|
219 |
|
|
ChunkTables::~ChunkTables() { |
220 |
|
|
pthread_mutex_destroy(lock); |
221 |
|
|
free(lock); |
222 |
|
|
for (unsigned i = 0; i < kNumHandleLocks; ++i) { |
223 |
|
|
pthread_mutex_destroy(handle_locks.At(i)); |
224 |
|
|
free(handle_locks.At(i)); |
225 |
|
|
} |
226 |
|
|
} |
227 |
|
|
|
228 |
|
|
void Migrate(ChunkTables *old_tables, ::ChunkTables *new_tables) { |
229 |
|
|
new_tables->next_handle = old_tables->next_handle; |
230 |
|
|
new_tables->handle2fd = old_tables->handle2fd; |
231 |
|
|
new_tables->inode2references = old_tables->inode2references; |
232 |
|
|
|
233 |
|
|
SmallHashDynamic<uint64_t, FileChunkReflist> *old_inode2chunks = |
234 |
|
|
&old_tables->inode2chunks; |
235 |
|
|
for (unsigned keyno = 0; keyno < old_inode2chunks->capacity(); ++keyno) { |
236 |
|
|
const uint64_t inode = old_inode2chunks->keys()[keyno]; |
237 |
|
|
if (inode == 0) continue; |
238 |
|
|
|
239 |
|
|
FileChunkReflist *old_reflist = &old_inode2chunks->values()[keyno]; |
240 |
|
|
BigVector<FileChunk> *old_list = old_reflist->list; |
241 |
|
|
FileChunkList *new_list = new FileChunkList(); |
242 |
|
|
for (unsigned i = 0; i < old_list->size(); ++i) { |
243 |
|
|
const FileChunk *old_chunk = old_list->AtPtr(i); |
244 |
|
|
off_t offset = old_chunk->offset(); |
245 |
|
|
size_t size = old_chunk->size(); |
246 |
|
|
shash::Any hash; |
247 |
|
|
shash_v2::MigrateAny(&old_chunk->content_hash_, &hash); |
248 |
|
|
new_list->PushBack(::FileChunk(hash, offset, size)); |
249 |
|
|
} |
250 |
|
|
delete old_list; |
251 |
|
|
::FileChunkReflist new_reflist(new_list, old_reflist->path, |
252 |
|
|
zlib::kZlibDefault, false); |
253 |
|
|
new_tables->inode2chunks.Insert(inode, new_reflist); |
254 |
|
|
} |
255 |
|
|
} |
256 |
|
|
|
257 |
|
|
} // namespace chunk_tables_v2 |
258 |
|
|
|
259 |
|
|
|
260 |
|
|
//------------------------------------------------------------------------------ |
261 |
|
|
|
262 |
|
|
|
263 |
|
|
namespace chunk_tables_v3 { |
264 |
|
|
|
265 |
|
|
ChunkTables::~ChunkTables() { |
266 |
|
|
pthread_mutex_destroy(lock); |
267 |
|
|
free(lock); |
268 |
|
|
for (unsigned i = 0; i < kNumHandleLocks; ++i) { |
269 |
|
|
pthread_mutex_destroy(handle_locks.At(i)); |
270 |
|
|
free(handle_locks.At(i)); |
271 |
|
|
} |
272 |
|
|
} |
273 |
|
|
|
274 |
|
|
void Migrate(ChunkTables *old_tables, ::ChunkTables *new_tables) { |
275 |
|
|
new_tables->next_handle = old_tables->next_handle; |
276 |
|
|
new_tables->handle2fd = old_tables->handle2fd; |
277 |
|
|
new_tables->inode2chunks = old_tables->inode2chunks; |
278 |
|
|
new_tables->inode2references = old_tables->inode2references; |
279 |
|
|
} |
280 |
|
|
|
281 |
|
|
} // namespace chunk_tables_v3 |
282 |
|
|
|
283 |
|
|
} // namespace compat |