RDFS
The Rice Comp413 2017 class' continuation on the work of the 2016 RDFS.
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
native_filesystem.h
1 // Copyright 2017 Rice University, COMP 413 2017
2 
3 #include <easylogging++.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 
7 #include <fstream>
8 #include <mutex>
9 #include <iostream>
10 #include <map>
11 #include <string>
12 #include <vector>
13 
14 #pragma once
15 
16 namespace nativefs {
17 
18 typedef struct {
19  uint64_t blockid;
20  uint64_t offset; // Address in the FS
21  uint32_t len; // length of the data
22  // in the block
23  uint32_t allocated_size; // total
24  // allocated size for this block
25  bool free;
26 } block_info;
27 
28 const size_t MIN_BLOCK_POWER = 13;
29 const size_t MAX_BLOCK_POWER = 27;
30 constexpr size_t MIN_BLOCK_SIZE = 1 << MIN_BLOCK_POWER;
31 constexpr size_t MAX_BLOCK_SIZE = 1 << MAX_BLOCK_POWER;
32 constexpr size_t FREE_LIST_SIZE = MAX_BLOCK_POWER - MIN_BLOCK_POWER + 1;
33 constexpr size_t DISK_SIZE = MAX_BLOCK_SIZE * 6;
34 constexpr size_t BLOCK_LIST_LEN = DISK_SIZE / MIN_BLOCK_SIZE;
35 constexpr size_t BLOCK_LIST_SIZE = BLOCK_LIST_LEN * sizeof(block_info);
36 // If MAGIC changes, make sure to change the 8 in RESERVED_SIZE too.
37 const char MAGIC[] = "LIVEBEEF";
38 // Reserved space for magic bytes + block_info array.
39 constexpr size_t RESERVED_SIZE = BLOCK_LIST_SIZE + 8;
40 
41 class NativeFS {
42  public:
46  explicit NativeFS(std::string);
50  ~NativeFS();
55  bool writeBlock(uint64_t, const std::string &);
61  bool getBlock(uint64_t, std::string &);
66  bool hasBlock(uint64_t);
71  bool rmBlock(uint64_t);
75  uint64_t getTotalSpace();
79  uint64_t getFreeSpace();
80 
84  std::vector<std::uint64_t> getKnownBlocks();
85 
90  bool fetchBlock(uint64_t, block_info &info);
91 
95  bool extendBlock(uint64_t block_id, std::string block_data);
96 
97  private:
102  int addBlock(const block_info &info);
106  void freeRange(uint64_t start, uint64_t end);
111  bool allocateBlock(size_t size, uint64_t &offset);
115  void constructFreeLists();
119  void flushAllBlocks();
123  void flushBlock(int block_index);
124 
125  size_t findBlock(uint64_t block_id);
126 
130  void printFreeBlocks();
131 
132  block_info *blocks;
133  mutable std::mutex listMtx;
134  std::vector<std::vector<uint64_t>> freeLists;
135  std::fstream disk;
136  static const std::string CLASS_NAME;
137 };
138 
139 } // namespace nativefs
bool getBlock(uint64_t, std::string &)
Definition: native_filesystem.cc:309
Definition: native_filesystem.h:41
Definition: native_filesystem.h:18
uint64_t getTotalSpace()
Definition: native_filesystem.cc:345
std::vector< std::uint64_t > getKnownBlocks()
Definition: native_filesystem.cc:155
bool writeBlock(uint64_t, const std::string &)
Definition: native_filesystem.cc:202
uint64_t getFreeSpace()
Definition: native_filesystem.cc:349
~NativeFS()
Definition: native_filesystem.cc:100
bool hasBlock(uint64_t)
Definition: native_filesystem.cc:300
bool fetchBlock(uint64_t, block_info &info)
Definition: native_filesystem.cc:287
bool rmBlock(uint64_t)
Definition: native_filesystem.cc:329
bool extendBlock(uint64_t block_id, std::string block_data)
Definition: native_filesystem.cc:357
NativeFS(std::string)
Definition: native_filesystem.cc:53