CernVM-FS  2.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
compat.cc
Go to the documentation of this file.
1 
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 
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 
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 
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 
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 
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 
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 
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
SmallHashDynamic< shash_v1::Md5, PathInfo > map_
Definition: compat.h:495
SmallHashDynamic< uint64_t, shash_v1::Md5 > map_
Definition: compat.h:745
SmallHashDynamic< uint64_t, FileChunkReflist > inode2chunks
Definition: compat.h:976
static uint32_t hasher_md5(const shash_v1::Md5 &key)
Definition: compat.cc:104
SmallHashDynamic< uint64_t, FileChunkReflist > inode2chunks
Definition: compat.h:883
SmallHashDynamic< uint64_t,::ChunkFd > handle2fd
Definition: compat.h:972
SmallHashDynamic< shash_v1::Md5, PathInfo > map_
Definition: compat.h:701
BigVector< FileChunk > * list
Definition: compat.h:861
uint32_t(* hasher_)(const Key &key)
Definition: compat.h:408
assert((mem||(size==0))&&"Out Of Memory")
Algorithms algorithm
Definition: compat.h:174
Algorithms algorithm
Definition: hash.h:123
const char * kSuffixes[]
Definition: compat.cc:19
SmallHashDynamic< uint64_t,::ChunkFd > handle2fd
Definition: compat.h:879
unsigned char digest[digest_size_]
Definition: compat.h:173
bool ConstructPath(const uint64_t inode, PathString *path)
Definition: compat.cc:61
unsigned char digest[digest_size_]
Definition: hash.h:122
SmallHashDynamic< shash_v1::Md5, uint64_t > map_
Definition: compat.h:724
char algorithm
SmallHashDynamic< uint64_t, uint32_t > map_
Definition: compat.h:532
BigVector< FileChunk > * list
Definition: compat.h:920
Algorithms
Definition: hash.h:39
BigVector< FileChunk > FileChunkList
Definition: file_chunk.h:51
const unsigned kDigestSizes[]
Definition: compat.h:37
bool FindPath(const uint64_t inode, PathString *path)
Definition: compat.h:796
Algorithms algorithm
Definition: compat.h:46
uint32_t capacity() const
Definition: smallhash.h:277
void VfsGetBy(const uint64_t inode, const uint32_t by, const PathString &path)
Definition: glue_buffer.h:524
shash_v2::Any content_hash_
content hash of the compressed file chunk
Definition: compat.h:910
SmallHashDynamic< uint64_t, uint32_t > inode2references
Definition: compat.h:977
SmallHashDynamic< uint64_t, shash_v1::Md5 > map_
Definition: compat.h:515
SmallHashDynamic< uint64_t, uint32_t > inode2references
Definition: compat.h:943
void Insert(const Key &key, const Value &value)
Definition: smallhash.h:88
Key * keys() const
Definition: smallhash.h:132
unsigned char digest[20]
void Append(const char *chars, const unsigned length)
Definition: shortstring.h:70
SmallHashDynamic< uint64_t, uint32_t > map_
Definition: compat.h:762
shash_v1::Any content_hash_
content hash of the compressed file chunk
Definition: compat.h:851
void PushBack(const Item &item)
Definition: bigvector.h:60
SmallHashDynamic< uint64_t, uint32_t > inode2references
Definition: compat.h:884
void Migrate(InodeTracker *old_tracker, glue::InodeTracker *new_tracker)
Definition: compat.cc:83
void SetHasher(uint32_t(*hasher)(const Key &key))
Definition: smallhash.h:136
bool FindPath(const uint64_t inode, PathString *path)
Definition: compat.h:566
SmallHashDynamic< uint64_t,::ChunkFd > handle2fd
Definition: compat.h:938
void MigrateAny(const Any *old_hash, shash::Any *new_hash)
Definition: compat.cc:31
Value * values() const
Definition: smallhash.h:133
Suffix suffix
Definition: hash.h:124
static void size_t size
Definition: smalloc.h:47
SmallHashDynamic< uint64_t, FileChunkReflist > inode2chunks
Definition: compat.h:942
const char kSuffixNone
Definition: hash.h:51
const Item * AtPtr(const size_t index) const
Definition: bigvector.h:55
unsigned char digest[digest_size_]
Definition: compat.h:45
uint32_t MurmurHash2(const void *key, int len, uint32_t seed)
Definition: murmur.hxx:23
size_t size() const
Definition: bigvector.h:100
static uint32_t hasher_inode(const uint64_t &inode)
Definition: compat.cc:108