forked from westerndigitalcorporation/zenfs
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfs_zenfs.h
356 lines (294 loc) · 13 KB
/
fs_zenfs.h
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
// Copyright (c) 2019-present, Western Digital Corporation
// This source code is licensed under both the GPLv2 (found in the
// COPYING file in the root directory) and Apache 2.0 License
// (found in the LICENSE.Apache file in the root directory).
#pragma once
#include "io_zenfs.h"
#include "rocksdb/env.h"
#include "rocksdb/file_system.h"
#include "rocksdb/plugin/zenfs/fs/zbd_stat.h"
#include "rocksdb/status.h"
#include "zbd_zenfs.h"
namespace ROCKSDB_NAMESPACE {
#if !defined(ROCKSDB_LITE) && defined(OS_LINUX)
class Superblock {
uint32_t magic_ = 0;
char uuid_[37] = {0};
uint32_t sequence_ = 0;
uint32_t version_ = 0;
uint32_t flags_ = 0;
uint32_t block_size_ = 0; /* in bytes */
uint32_t zone_size_ = 0; /* in blocks */
uint32_t nr_zones_ = 0;
char aux_fs_path_[256] = {0};
uint32_t finish_treshold_ = 0;
char reserved_[187] = {0};
public:
const uint32_t MAGIC = 0x5a454e46; /* ZENF */
const uint32_t ENCODED_SIZE = 512;
const uint32_t CURRENT_VERSION = 1;
const uint32_t DEFAULT_FLAGS = 0;
Superblock() {}
/* Create a superblock for a filesystem covering the entire zoned block device
*/
Superblock(ZonedBlockDevice* zbd, std::string aux_fs_path = "",
uint32_t finish_threshold = 0) {
std::string uuid = Env::Default()->GenerateUniqueId();
int uuid_len =
std::min(uuid.length(),
sizeof(uuid_) - 1); /* make sure uuid is nullterminated */
memcpy((void*)uuid_, uuid.c_str(), uuid_len);
magic_ = MAGIC;
version_ = CURRENT_VERSION;
flags_ = DEFAULT_FLAGS;
finish_treshold_ = finish_threshold;
block_size_ = zbd->GetBlockSize();
zone_size_ = zbd->GetZoneSize() / block_size_;
nr_zones_ = zbd->GetNrZones();
strncpy(aux_fs_path_, aux_fs_path.c_str(), sizeof(aux_fs_path_) - 1);
}
Status DecodeFrom(Slice* input);
void EncodeTo(std::string* output);
Status CompatibleWith(ZonedBlockDevice* zbd);
uint32_t GetSeq() { return sequence_; }
std::string GetAuxFsPath() { return std::string(aux_fs_path_); }
uint32_t GetFinishTreshold() { return finish_treshold_; }
std::string GetUUID() { return std::string(uuid_); }
};
class ZenMetaLog {
uint64_t read_pos_;
Zone* zone_;
ZonedBlockDevice* zbd_;
size_t bs_;
/* Every meta log record is prefixed with a CRC(32 bits) and record length (32
* bits) */
const size_t zMetaHeaderSize = sizeof(uint32_t) * 2;
public:
ZenMetaLog(ZonedBlockDevice* zbd, Zone* zone) {
zbd_ = zbd;
zone_ = zone;
zone_->open_for_write_ = true;
bs_ = zbd_->GetBlockSize();
read_pos_ = zone->start_;
}
virtual ~ZenMetaLog() { zone_->open_for_write_ = false; }
IOStatus AddRecord(const Slice& slice);
IOStatus ReadRecord(Slice* record, std::string* scratch);
Zone* GetZone() { return zone_; };
private:
IOStatus Read(Slice* slice);
};
class ZenFS : public FileSystemWrapper {
ZonedBlockDevice* zbd_;
std::map<std::string, ZoneFile*> files_;
std::mutex files_mtx_;
std::shared_ptr<Logger> logger_;
std::atomic<uint64_t> next_file_id_;
Zone* cur_meta_zone_ = nullptr;
std::unique_ptr<ZenMetaLog> meta_log_;
std::mutex metadata_sync_mtx_;
std::unique_ptr<Superblock> superblock_;
std::shared_ptr<Logger> GetLogger() { return logger_; }
struct MetadataWriter : public ZonedWritableFile::MetadataWriter {
ZenFS* zenFS;
IOStatus Persist(ZoneFile* zoneFile) {
Debug(zenFS->GetLogger(), "Syncing metadata for: %s",
zoneFile->GetFilename().c_str());
return zenFS->SyncFileMetadata(zoneFile);
}
};
MetadataWriter metadata_writer_;
enum ZenFSTag : uint32_t {
kCompleteFilesSnapshot = 1,
kFileUpdate = 2,
kFileDeletion = 3,
kEndRecord = 4,
};
void LogFiles();
void ClearFiles();
IOStatus WriteSnapshotLocked(ZenMetaLog* meta_log);
IOStatus WriteEndRecord(ZenMetaLog* meta_log);
IOStatus RollMetaZoneLocked();
IOStatus PersistSnapshot(ZenMetaLog* meta_writer);
IOStatus PersistRecord(std::string record);
IOStatus SyncFileMetadata(ZoneFile* zoneFile);
void EncodeSnapshotTo(std::string* output);
void EncodeFileDeletionTo(ZoneFile* zoneFile, std::string* output);
Status DecodeSnapshotFrom(Slice* input);
Status DecodeFileUpdateFrom(Slice* slice);
Status DecodeFileDeletionFrom(Slice* slice);
Status RecoverFrom(ZenMetaLog* log);
std::string ToAuxPath(std::string path) {
return superblock_->GetAuxFsPath() + path;
}
std::string ToZenFSPath(std::string aux_path) {
std::string path = aux_path;
path.erase(0, superblock_->GetAuxFsPath().length());
return path;
}
ZoneFile* GetFileInternal(std::string fname);
ZoneFile* GetFile(std::string fname);
IOStatus DeleteFile(std::string fname);
public:
explicit ZenFS(ZonedBlockDevice* zbd, std::shared_ptr<FileSystem> aux_fs,
std::shared_ptr<Logger> logger);
virtual ~ZenFS();
Status Mount(bool readonly);
Status MkFS(std::string aux_fs_path, uint32_t finish_threshold);
std::map<std::string, Env::WriteLifeTimeHint> GetWriteLifeTimeHints();
const char* Name() const override {
return "ZenFS - The Zoned-enabled File System";
}
void EncodeJson(std::stringstream& json_stream);
virtual IOStatus NewSequentialFile(const std::string& fname,
const FileOptions& file_opts,
std::unique_ptr<FSSequentialFile>* result,
IODebugContext* dbg) override;
virtual IOStatus NewRandomAccessFile(
const std::string& fname, const FileOptions& file_opts,
std::unique_ptr<FSRandomAccessFile>* result,
IODebugContext* dbg) override;
virtual IOStatus NewWritableFile(const std::string& fname,
const FileOptions& file_opts,
std::unique_ptr<FSWritableFile>* result,
IODebugContext* dbg) override;
virtual IOStatus ReuseWritableFile(const std::string& fname,
const std::string& old_fname,
const FileOptions& file_opts,
std::unique_ptr<FSWritableFile>* result,
IODebugContext* dbg) override;
virtual IOStatus ReopenWritableFile(const std::string& fname,
const FileOptions& options,
std::unique_ptr<FSWritableFile>* result,
IODebugContext* dbg) override;
virtual IOStatus FileExists(const std::string& fname,
const IOOptions& options,
IODebugContext* dbg) override;
virtual IOStatus GetChildren(const std::string& dir, const IOOptions& options,
std::vector<std::string>* result,
IODebugContext* dbg) override;
virtual IOStatus DeleteFile(const std::string& fname,
const IOOptions& options,
IODebugContext* dbg) override;
IOStatus GetFileSize(const std::string& f, const IOOptions& options,
uint64_t* size, IODebugContext* dbg) override;
IOStatus RenameFile(const std::string& f, const std::string& t,
const IOOptions& options, IODebugContext* dbg) override;
IOStatus GetFreeSpace(const std::string& /*path*/,
const IOOptions& /*options*/, uint64_t* diskfree,
IODebugContext* /*dbg*/) override {
*diskfree = zbd_->GetFreeSpace();
return IOStatus::OK();
}
IOStatus GetFileModificationTime(const std::string& fname,
const IOOptions& options, uint64_t* mtime,
IODebugContext* dbg) override;
// The directory structure is stored in the aux file system
IOStatus IsDirectory(const std::string& path, const IOOptions& options,
bool* is_dir, IODebugContext* dbg) override {
if (GetFile(path) != nullptr) {
*is_dir = false;
return IOStatus::OK();
}
return target()->IsDirectory(ToAuxPath(path), options, is_dir, dbg);
}
IOStatus NewDirectory(const std::string& name, const IOOptions& io_opts,
std::unique_ptr<FSDirectory>* result,
IODebugContext* dbg) override {
Debug(logger_, "NewDirectory: %s to aux: %s\n", name.c_str(),
ToAuxPath(name).c_str());
return target()->NewDirectory(ToAuxPath(name), io_opts, result, dbg);
}
IOStatus CreateDir(const std::string& d, const IOOptions& options,
IODebugContext* dbg) override {
Debug(logger_, "CreatDir: %s to aux: %s\n", d.c_str(),
ToAuxPath(d).c_str());
return target()->CreateDir(ToAuxPath(d), options, dbg);
}
IOStatus CreateDirIfMissing(const std::string& d, const IOOptions& options,
IODebugContext* dbg) override {
Debug(logger_, "CreatDirIfMissing: %s to aux: %s\n", d.c_str(),
ToAuxPath(d).c_str());
return target()->CreateDirIfMissing(ToAuxPath(d), options, dbg);
}
IOStatus DeleteDir(const std::string& d, const IOOptions& options,
IODebugContext* dbg) override {
std::vector<std::string> children;
IOStatus s;
Debug(logger_, "DeleteDir: %s aux: %s\n", d.c_str(), ToAuxPath(d).c_str());
s = GetChildren(d, options, &children, dbg);
if (children.size() != 0)
return IOStatus::IOError("Directory has children");
return target()->DeleteDir(ToAuxPath(d), options, dbg);
}
// We might want to override these in the future
IOStatus GetAbsolutePath(const std::string& db_path, const IOOptions& options,
std::string* output_path,
IODebugContext* dbg) override {
return target()->GetAbsolutePath(ToAuxPath(db_path), options, output_path,
dbg);
}
IOStatus LockFile(const std::string& f, const IOOptions& options,
FileLock** l, IODebugContext* dbg) override {
return target()->LockFile(ToAuxPath(f), options, l, dbg);
}
IOStatus UnlockFile(FileLock* l, const IOOptions& options,
IODebugContext* dbg) override {
return target()->UnlockFile(l, options, dbg);
}
IOStatus GetTestDirectory(const IOOptions& options, std::string* path,
IODebugContext* dbg) override {
*path = "rocksdbtest";
Debug(logger_, "GetTestDirectory: %s aux: %s\n", path->c_str(),
ToAuxPath(*path).c_str());
return target()->CreateDirIfMissing(ToAuxPath(*path), options, dbg);
}
IOStatus NewLogger(const std::string& fname, const IOOptions& options,
std::shared_ptr<Logger>* result,
IODebugContext* dbg) override {
return target()->NewLogger(ToAuxPath(fname), options, result, dbg);
}
// Not supported (at least not yet)
IOStatus Truncate(const std::string& /*fname*/, size_t /*size*/,
const IOOptions& /*options*/,
IODebugContext* /*dbg*/) override {
return IOStatus::NotSupported("Truncate is not implemented in ZenFS");
}
virtual IOStatus NewRandomRWFile(const std::string& /*fname*/,
const FileOptions& /*options*/,
std::unique_ptr<FSRandomRWFile>* /*result*/,
IODebugContext* /*dbg*/) override {
return IOStatus::NotSupported("RandomRWFile is not implemented in ZenFS");
}
virtual IOStatus NewMemoryMappedFileBuffer(
const std::string& /*fname*/,
std::unique_ptr<MemoryMappedFileBuffer>* /*result*/) override {
return IOStatus::NotSupported(
"MemoryMappedFileBuffer is not implemented in ZenFS");
}
virtual IOStatus LinkFile(const std::string& /*src*/,
const std::string& /*target*/,
const IOOptions& /*options*/,
IODebugContext* /*dbg*/) override {
return IOStatus::NotSupported("LinkFile is not supported in ZenFS");
}
virtual IOStatus NumFileLinks(const std::string& /*fname*/,
const IOOptions& /*options*/,
uint64_t* /*count*/,
IODebugContext* /*dbg*/) override {
return IOStatus::NotSupported(
"Getting number of file links is not supported in ZenFS");
}
virtual IOStatus AreFilesSame(const std::string& /*first*/,
const std::string& /*second*/,
const IOOptions& /*options*/, bool* /*res*/,
IODebugContext* /*dbg*/) override {
return IOStatus::NotSupported("AreFilesSame is not supported in ZenFS");
}
std::vector<ZoneStat> GetStat();
};
#endif // !defined(ROCKSDB_LITE) && defined(OS_LINUX)
Status NewZenFS(FileSystem** fs, const std::string& bdevname);
std::map<std::string, std::string> ListZenFileSystems();
} // namespace ROCKSDB_NAMESPACE