CernVM-FS  2.13.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, const off_t offset, const size_t size)
35  : content_hash_(hash), offset_(offset), size_(size) { }
36 
37  inline const shash::Any &content_hash() const { return content_hash_; }
38  inline off_t offset() const { return offset_; }
39  inline size_t size() const { return size_; }
40 
41  protected:
43  off_t offset_;
44  size_t size_;
45 };
46 
48 
51  : list(NULL)
53  , external_data(false) { }
55  const PathString &p,
56  zlib::Algorithms alg,
57  bool external)
58  : list(l), path(p), compression_alg(alg), external_data(external) { }
59 
60  unsigned FindChunkIdx(const uint64_t offset);
62 
67 };
68 
69 
74 struct ChunkFd {
75  ChunkFd() : fd(-1), chunk_idx(0) { }
76  int fd; // -1 or pointing to chunk_idx
77  unsigned chunk_idx;
78 };
79 
80 
84 struct ChunkTables {
85  ChunkTables();
86  ~ChunkTables();
87  ChunkTables(const ChunkTables &other);
88  ChunkTables &operator=(const ChunkTables &other);
89  void CopyFrom(const ChunkTables &other);
90  void InitLocks();
91  void InitHashmaps();
92 
93  pthread_mutex_t *Handle2Lock(const uint64_t handle) const;
94 
95  inline void Lock() {
96  int retval = pthread_mutex_lock(lock);
97  assert(retval == 0);
98  }
99 
100  inline void Unlock() {
101  int retval = pthread_mutex_unlock(lock);
102  assert(retval == 0);
103  }
104 
105  // Version 2 --> 4: add handle2uniqino
106  static const unsigned kVersion = 4;
107 
108  int version;
109  static const unsigned kNumHandleLocks = 128;
110  // Versions < 4 of ChunkTables didn't have this map. Therefore, after a
111  // hot patch a handle can be missing from this map. In this case, the fuse
112  // module falls back to the inode passed by the kernel.
115  // The file descriptors attached to handles need to be locked.
116  // Using a hash map to survive with a small, fixed number of locks
120  uint64_t next_handle;
121  pthread_mutex_t *lock;
122 };
123 
124 
132  public:
137  struct OpenChunks {
138  OpenChunks() : chunk_fd(NULL) { }
141  };
142 
145  int Add(FileChunkReflist chunks);
146  OpenChunks Get(int fd);
147  void Release(int fd);
148 
149  private:
150  inline void Lock() {
151  int retval = pthread_mutex_lock(lock_);
152  assert(retval == 0);
153  }
154 
155  inline void Unlock() {
156  int retval = pthread_mutex_unlock(lock_);
157  assert(retval == 0);
158  }
159 
160  std::vector<OpenChunks> fd_table_;
161  pthread_mutex_t *lock_;
162 };
163 
164 #endif // CVMFS_FILE_CHUNK_H_
size_t size_
uncompressed size of the data chunk
Definition: file_chunk.h:44
static const unsigned kVersion
Definition: file_chunk.h:106
FileChunkReflist chunk_reflist
Definition: file_chunk.h:140
std::vector< OpenChunks > fd_table_
Definition: file_chunk.h:160
SmallHashDynamic< uint64_t, uint64_t > handle2uniqino
Definition: file_chunk.h:113
zlib::Algorithms compression_alg
Definition: file_chunk.h:65
OpenChunks Get(int fd)
Definition: file_chunk.cc:187
pthread_mutex_t * lock_
Definition: file_chunk.h:161
ChunkFd()
Definition: file_chunk.h:75
const shash::Any & content_hash() const
Definition: file_chunk.h:37
assert((mem||(size==0))&&"Out Of Memory")
int fd
Definition: file_chunk.h:76
SmallHashDynamic< uint64_t, ChunkFd > handle2fd
Definition: file_chunk.h:114
pthread_mutex_t * lock
Definition: file_chunk.h:121
void Unlock()
Definition: file_chunk.h:100
PathString path
Definition: file_chunk.h:64
Algorithms
Definition: compression.h:44
BigVector< FileChunk > FileChunkList
Definition: file_chunk.h:47
shash::Any content_hash_
content hash of the compressed file chunk
Definition: file_chunk.h:42
static const unsigned kNumHandleLocks
Definition: file_chunk.h:109
pthread_mutex_t * Handle2Lock(const uint64_t handle) const
Definition: file_chunk.cc:139
void Lock()
Definition: file_chunk.h:95
off_t offset() const
Definition: file_chunk.h:38
unsigned chunk_idx
Definition: file_chunk.h:77
unsigned FindChunkIdx(const uint64_t offset)
Definition: file_chunk.cc:23
SmallHashDynamic< uint64_t, uint32_t > inode2references
Definition: file_chunk.h:119
ChunkTables & operator=(const ChunkTables &other)
Definition: file_chunk.cc:116
off_t offset_
byte offset in the uncompressed input file
Definition: file_chunk.h:43
FileChunkReflist(FileChunkList *l, const PathString &p, zlib::Algorithms alg, bool external)
Definition: file_chunk.h:54
int Add(FileChunkReflist chunks)
Definition: file_chunk.cc:167
void InitLocks()
Definition: file_chunk.cc:67
BigVector< pthread_mutex_t * > handle_locks
Definition: file_chunk.h:117
uint64_t next_handle
Definition: file_chunk.h:120
shash::Any HashChunkList()
Definition: file_chunk.cc:49
size_t size() const
Definition: file_chunk.h:39
void Release(int fd)
Definition: file_chunk.cc:201
void InitHashmaps()
Definition: file_chunk.cc:82
void CopyFrom(const ChunkTables &other)
Definition: file_chunk.cc:129
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:118
FileChunkList * list
Definition: file_chunk.h:63