CernVM-FS  2.12.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
file_chunk.h
Go to the documentation of this file.
1 
9 #ifndef CVMFS_FILE_CHUNK_H_
10 #define CVMFS_FILE_CHUNK_H_
11 
12 #include <pthread.h>
13 #include <stdint.h>
14 #include <sys/types.h>
15 
16 #include <string>
17 #include <vector>
18 
19 #include "bigvector.h"
21 #include "crypto/hash.h"
22 #include "shortstring.h"
23 #include "smallhash.h"
24 #include "util/atomic.h"
25 #include "util/single_copy.h"
26 
31 class FileChunk {
32  public:
33  FileChunk() : content_hash_(shash::Any(shash::kAny)), offset_(0), size_(0) { }
34  FileChunk(const shash::Any &hash,
35  const off_t offset,
36  const size_t size) :
37  content_hash_(hash),
38  offset_(offset),
39  size_(size) { }
40 
41  inline const shash::Any& content_hash() const { return content_hash_; }
42  inline off_t offset() const { return offset_; }
43  inline size_t size() const { return size_; }
44 
45  protected:
47  off_t offset_;
48  size_t size_;
49 };
50 
52 
56  , external_data(false) { }
58  const PathString &p,
59  zlib::Algorithms alg,
60  bool external)
61  : list(l)
62  , path(p)
63  , compression_alg(alg)
64  , external_data(external) { }
65 
66  unsigned FindChunkIdx(const uint64_t offset);
68 
73 };
74 
75 
80 struct ChunkFd {
81  ChunkFd() : fd(-1), chunk_idx(0) { }
82  int fd; // -1 or pointing to chunk_idx
83  unsigned chunk_idx;
84 };
85 
86 
90 struct ChunkTables {
91  ChunkTables();
92  ~ChunkTables();
93  ChunkTables(const ChunkTables &other);
94  ChunkTables &operator= (const ChunkTables &other);
95  void CopyFrom(const ChunkTables &other);
96  void InitLocks();
97  void InitHashmaps();
98 
99  pthread_mutex_t *Handle2Lock(const uint64_t handle) const;
100 
101  inline void Lock() {
102  int retval = pthread_mutex_lock(lock);
103  assert(retval == 0);
104  }
105 
106  inline void Unlock() {
107  int retval = pthread_mutex_unlock(lock);
108  assert(retval == 0);
109  }
110 
111  // Version 2 --> 4: add handle2uniqino
112  static const unsigned kVersion = 4;
113 
114  int version;
115  static const unsigned kNumHandleLocks = 128;
116  // Versions < 4 of ChunkTables didn't have this map. Therefore, after a
117  // hot patch a handle can be missing from this map. In this case, the fuse
118  // module falls back to the inode passed by the kernel.
121  // The file descriptors attached to handles need to be locked.
122  // Using a hash map to survive with a small, fixed number of locks
126  uint64_t next_handle;
127  pthread_mutex_t *lock;
128 };
129 
130 
138  public:
143  struct OpenChunks {
144  OpenChunks() : chunk_fd(NULL) { }
147  };
148 
151  int Add(FileChunkReflist chunks);
152  OpenChunks Get(int fd);
153  void Release(int fd);
154 
155  private:
156  inline void Lock() {
157  int retval = pthread_mutex_lock(lock_);
158  assert(retval == 0);
159  }
160 
161  inline void Unlock() {
162  int retval = pthread_mutex_unlock(lock_);
163  assert(retval == 0);
164  }
165 
166  std::vector<OpenChunks> fd_table_;
167  pthread_mutex_t *lock_;
168 };
169 
170 #endif // CVMFS_FILE_CHUNK_H_
size_t size_
uncompressed size of the data chunk
Definition: file_chunk.h:48
static const unsigned kVersion
Definition: file_chunk.h:112
FileChunkReflist chunk_reflist
Definition: file_chunk.h:146
std::vector< OpenChunks > fd_table_
Definition: file_chunk.h:166
SmallHashDynamic< uint64_t, uint64_t > handle2uniqino
Definition: file_chunk.h:119
zlib::Algorithms compression_alg
Definition: file_chunk.h:71
OpenChunks Get(int fd)
Definition: file_chunk.cc:190
pthread_mutex_t * lock_
Definition: file_chunk.h:167
ChunkFd()
Definition: file_chunk.h:81
const shash::Any & content_hash() const
Definition: file_chunk.h:41
assert((mem||(size==0))&&"Out Of Memory")
int fd
Definition: file_chunk.h:82
SmallHashDynamic< uint64_t, ChunkFd > handle2fd
Definition: file_chunk.h:120
pthread_mutex_t * lock
Definition: file_chunk.h:127
void Unlock()
Definition: file_chunk.h:106
PathString path
Definition: file_chunk.h:70
Algorithms
Definition: compression.h:44
BigVector< FileChunk > FileChunkList
Definition: file_chunk.h:51
shash::Any content_hash_
content hash of the compressed file chunk
Definition: file_chunk.h:46
static const unsigned kNumHandleLocks
Definition: file_chunk.h:115
pthread_mutex_t * Handle2Lock(const uint64_t handle) const
Definition: file_chunk.cc:141
void Lock()
Definition: file_chunk.h:101
off_t offset() const
Definition: file_chunk.h:42
unsigned chunk_idx
Definition: file_chunk.h:83
unsigned FindChunkIdx(const uint64_t offset)
Definition: file_chunk.cc:23
SmallHashDynamic< uint64_t, uint32_t > inode2references
Definition: file_chunk.h:125
ChunkTables & operator=(const ChunkTables &other)
Definition: file_chunk.cc:118
off_t offset_
byte offset in the uncompressed input file
Definition: file_chunk.h:47
FileChunkReflist(FileChunkList *l, const PathString &p, zlib::Algorithms alg, bool external)
Definition: file_chunk.h:57
int Add(FileChunkReflist chunks)
Definition: file_chunk.cc:170
void InitLocks()
Definition: file_chunk.cc:68
BigVector< pthread_mutex_t * > handle_locks
Definition: file_chunk.h:123
uint64_t next_handle
Definition: file_chunk.h:126
shash::Any HashChunkList()
Definition: file_chunk.cc:49
size_t size() const
Definition: file_chunk.h:43
void Release(int fd)
Definition: file_chunk.cc:204
void InitHashmaps()
Definition: file_chunk.cc:84
void CopyFrom(const ChunkTables &other)
Definition: file_chunk.cc:131
FileChunk(const shash::Any &hash, const off_t offset, const size_t size)
Definition: file_chunk.h:34
SmallHashDynamic< uint64_t, FileChunkReflist > inode2chunks
Definition: file_chunk.h:124
FileChunkList * list
Definition: file_chunk.h:69