-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinodeTable.cpp
252 lines (207 loc) · 6.83 KB
/
inodeTable.cpp
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
#include <cstdint>
#include <ios>
#include <iostream>
#include <chrono>
#include <limits>
#include <cassert>
#include <fstream>
#include <functional>
#include <forward_list>
#include <string>
#include "inodeTable.h"
using namespace std::chrono;
using namespace FileSys;
using namespace std::placeholders;
using std::ios_base;
using std::cerr; using std::endl; using std::clog;
using std::numeric_limits;
using std::fstream;
using std::ref; using std::bind;
using std::forward_list;
using std::string;
//////////////////////////////////////
// PRIVATE MEMBER FUNCTIONS
//////////////////////////////////////
// Pre: inode table file node_file exists
// Called by: FileMan()
InodeTable::InodeTable(string nfn): ns(nfn, ios_base::in | ios_base::out
| ios_base::binary), fileName(nfn)
{
assert(ns);
loadTbl();
}
// Called by: ~FileMan()
InodeTable::~InodeTable()
{
storeTbl();
if (ns.is_open())
ns.close();
}
Inode &InodeTable::refTblNode(inNum_t iNum)
{
uint32_t blkNum = iNum / INODES_PER_BLOCK;
uint32_t blkIx = iNum % INODES_PER_BLOCK;
return tbl[blkNum][blkIx];
}
const Inode &InodeTable::refTblNode(inNum_t iNum) const
{
uint32_t blkNum = iNum / INODES_PER_BLOCK;
uint32_t blkIx = iNum % INODES_PER_BLOCK;
return tbl[blkNum][blkIx];
}
// Return the first available inode number, if any
// Else return SENTINEL_INUM
// Called by:FileMan::createFile()
inNum_t InodeTable::assignInN()
{
inNum_t ret = SENTINEL_INUM;
const inNum_t MAX_INUM = NUM_INODE_TBL_BLOCKS * INODES_PER_BLOCK;
for (inNum_t ix = 0U; ix != MAX_INUM; ++ix)
if (avail.test(ix)) {
ret = ix;
avail.reset(ix);
break;
}
if (ret != SENTINEL_INUM)
refTblNode(ret).crTime = getCurTime(true);
return ret;
}
// Called by: FileMan::deleteFile()
void InodeTable::releaseInN(inNum_t iNum)
{
if (iNum == SENTINEL_INUM)
return;
Inode &r_tblNode = refTblNode(iNum);
// clear the node
for (auto &bN : r_tblNode.bNums)
bN = SENTINEL_BNUM;
r_tblNode.crTime = 0UL;
for (auto &bN : r_tblNode.indirect)
bN = SENTINEL_BNUM;
avail.set(iNum); // mark the inode as available
}
// Called by: FileMan::fileExists(), remvBlock()
bool InodeTable::nodeInUse(inNum_t iNum) const
{
assert(iNum != SENTINEL_INUM);
return !avail.test(iNum);
}
// Called by: FileMan::addBlock(), FileMan::remvBlock()
bool InodeTable::nodeLocked(inNum_t iNum) const
{
assert(iNum != SENTINEL_INUM);
return refTblNode(iNum).lkd != SENTINEL_INUM;
}
// Assign block blk to inode iNum if there is room in the inode
// We have already checked that the inode has not been locked by another client
// Called by: FileMan::addBlock()
bool InodeTable::assignBlkN(inNum_t iNum, bNum_t blk)
{
assert(iNum != SENTINEL_INUM);
assert(!avail.test(iNum)); // check we're not assigning block to unused node
bool ret = false;
for (auto &item : refTblNode(iNum).bNums)
if (item == SENTINEL_BNUM) { // item represents an open slot in inode
item = blk;
ret = true;
break;
}
return ret;
}
// Called by: releaseAllBlkN(), fileMan::remvBlock()
bool InodeTable::releaseBlkN(inNum_t iNum, bNum_t tgt)
{
bool found = false;
if (iNum != SENTINEL_INUM)
for (auto &item: refTblNode(iNum).bNums) {
if (item == tgt) {
found = true;
item = SENTINEL_BNUM; // release the block
clog << tabs(2, true) << "Releasing block number " << tgt
<< " from inode " << iNum << '\n';
break;
}
}
return found;
}
void InodeTable::releaseAllBlkN(inNum_t iNum)
{
if (iNum == SENTINEL_INUM)
return;
for (auto &item : refTblNode(iNum).bNums) // item is a bNum_t
if (item != SENTINEL_BNUM)
releaseBlkN(iNum, item);
}
forward_list<bNum_t> InodeTable::listAllBlkN(inNum_t iNum)
{
forward_list<bNum_t> tempList;
if (iNum == SENTINEL_INUM)
cerr << "InodeTable::listAllBlkN() called with SENTINEL_INUM" << endl;
else {
for (auto item : refTblNode(iNum).bNums)
if (item != SENTINEL_BNUM)
tempList.push_front(item);
}
return tempList; // move, not copy
}
// Pre: node_file is open for update on fstream ns
// Called by: InodeTable()
void InodeTable::loadTbl()
{
ns.read(reinterpret_cast<char *>(&avail),
NUM_INODE_TBL_BLOCKS * INODES_PER_BLOCK >> 3);
for (uint32_t i = 0U; i != NUM_INODE_TBL_BLOCKS; ++i)
for (uint32_t j = 0U; j != INODES_PER_BLOCK; ++j) {
Inode tempNode;
ns.read(reinterpret_cast<char *>(&tempNode.bNums),
sizeof(tempNode.bNums));
ns.read(reinterpret_cast<char *>(&tempNode.lkd),
sizeof(tempNode.lkd));
ns.read(reinterpret_cast<char *>(&tempNode.crTime),
sizeof(tempNode.crTime));
ns.read(reinterpret_cast<char *>(&tempNode.indirect),
sizeof(tempNode.indirect));
ns.read(reinterpret_cast<char *>(&tempNode.iNum),
sizeof(tempNode.iNum));
tbl[i][j] = tempNode;
}
}
// Pre: file fileName (i.e., 'node_file') exists
//
// Stores inode table using FileShifter class as follows:
//
// Creates 'fileName.tmp' containing new file table
// Unlinks 'fileName'
// Links 'fileName' to 'fileName.tmp'
// Unlinks 'fileName.tmp'
//
// Called by: ~InodeTable()
void InodeTable::storeTbl()
{
auto bound_member_fn = bind(&InodeTable::doStoreTbl, ref(*this), _1);
int32_t errVal = shifter.shiftFiles(fileName, bound_member_fn);
assert(!errVal);
}
// pre: node_file is open for update on fstream ns
void InodeTable::doStoreTbl(fstream &ns)
{
assert(ns);
assert(ns.is_open());
ns.write(reinterpret_cast<char *>(&avail),
NUM_INODE_TBL_BLOCKS * INODES_PER_BLOCK >> 3);
for (uint32_t i = 0U; i != NUM_INODE_TBL_BLOCKS; ++i)
for (uint32_t j = 0U; j != INODES_PER_BLOCK; ++j) {
Inode tempNode = tbl[i][j];
ns.write(reinterpret_cast<char *>(&tempNode.bNums),
sizeof(tempNode.bNums));
ns.write(reinterpret_cast<char *>(&tempNode.lkd),
sizeof(tempNode.lkd));
ns.write(reinterpret_cast<char *>(&tempNode.crTime),
sizeof(tempNode.crTime));
ns.write(reinterpret_cast<char *>(&tempNode.indirect),
sizeof(tempNode.indirect));
ns.write(reinterpret_cast<char *>(&tempNode.iNum),
sizeof(tempNode.iNum));
}
clog << '\n' << tabs(1) << "Inode table stored.\n";
}