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
|
use io;
// BFS state
export type bfs = struct {
device: io::handle,
bootsector: *bootsector,
};
// Open a BFS from a device
export fn open(device: io::handle) (*bfs | error | io::error) = {
// Allocate bootsector object
const bootsector = alloc(bootsector {
...
});
// Allocate BFS object
const bfs = alloc(bfs {
device = device,
bootsector = bootsector,
});
// Pull the data
match (pull(bfs)) {
case let e: io::error =>
free(bootsector);
free(bfs);
return e;
case => void;
};
return bfs;
};
// Pull the bootsector from the device
export fn pull(filesystem: *bfs) (void | io::error) = {
pread(filesystem.device, filesystem.bootsector: *[512]u8, 0)?;
};
// Commit the bootsector to the device
export fn commit(filesystem: *bfs) (void | io::error) = {
pwrite(filesystem.device, filesystem.bootsector: *[512]u8, 0)?;
};
// Close the BFS
export fn close(filesystem: *bfs) (void | io::error) = {
// Commit before closing
commit(filesystem)?;
// Freeing the underlying objects
free(filesystem.bootsector);
free(filesystem);
};
// Allocate a block
export fn allocblock(self: *bfs) (u64 | io::error) = {
if (self.bootsector.lastblock == 0) {
const blockid = self.bootsector.freeblock;
self.bootsector.freeblock += 1;
commit(self)?;
return blockid;
};
const blockid = self.bootsector.lastblock;
let freeblock = free_block { ... };
getfreeblock(self, blockid, &freeblock)?;
if (freeblock.prevblock == 0)
self.bootsector.nextblock = 0;
self.bootsector.lastblock = freeblock.prevblock;
commit(self)?;
return blockid;
};
// Free a block given a block ID
export fn freeblock(self: *bfs, blockid: u64) (void | io::error) = {
if (self.bootsector.nextblock != 0) {
const freeblock = free_block {
prevblock = blockid,
};
setfreeblock(self, self.bootsector.nextblock, &freeblock)?;
} else
self.bootsector.lastblock = blockid;
const freeblock = free_block { ... };
setfreeblock(self, blockid, &freeblock)?;
self.bootsector.nextblock = blockid;
commit(self)?;
};
// Clears an inode
fn zeroinode(self: *bfs, inid: u64) (void | io::error) = {
const in = inode { ... };
setinode(self, inid, &in)?;
};
// Allocate a new inode
export fn allocinode(self: *bfs) (u64 | io::error) = {
// If no currently free inode block,
if (self.bootsector.nextinblk == 0) {
// Allocate new inode block
const inblockid = allocblock(self)?;
// Initialize it
const inid = (inblockid << self.bootsector.blocksize) / size(inode): u64 + 1;
const inblock = inode_block {
freeinode = inid + 1,
freecount = (1: u64 << self.bootsector.blocksize) / size(inode): u64 - 2,
...
};
// Insert it
insert_inodeblock(self, inblockid, &inblock)?;
// Write to it
setinblock(self, inblockid, &inblock)?;
// Zero the inode
zeroinode(self, inid)?;
return inid;
};
const inblockid = self.bootsector.nextinblk;
// Get inode block
let inblock = inode_block { ... };
getinblock(self, inblockid, &inblock)?;
// Check if inode in inode freelist
let inid = inblock.nextinode;
if (inblock.nextinode == 0) {
// No inode in freelist, bump freeinode
inid = inblock.freeinode;
inblock.freeinode += 1;
} else {
// Get the free inode's next inode (stored in block field)
let in = inode { ... };
getinode(self, inblock.nextinode, &in)?;
// Update the inode block first inode to it
inblock.nextinode = in.block;
};
// Decrement the inode block's freecount
inblock.freecount -= 1;
// If no free inode in the inode block, remove the inode block
// from the freelist
if (inblock.freecount == 0)
remove_inodeblock(self, inblockid, &inblock)?;
// Finally, set the block and zero the inode
setinblock(self, inblockid, &inblock)?;
zeroinode(self, inid)?;
return inid;
};
// Free an inode given its inode ID
export fn freeinode(self: *bfs, inid: u64) (void | io::error) = {
const inblockid = inid >> (self.bootsector.blocksize - 6);
let inblock = inode_block { ... };
getinblock(self, inblockid, &inblock)?;
let in = inode { ... };
getinode(self, inid, &in)?;
in.block = inblock.nextinode;
inblock.nextinode = inid;
inblock.freecount += 1;
if (inblock.freecount == ((1: u64 << self.bootsector.blocksize) / size(inode): u64 - 1)) {
remove_inodeblock(self, inblockid, &inblock)?;
freeblock(self, inblockid)?;
return;
};
if (inblock.freecount == 1)
insert_inodeblock(self, inblockid, &inblock)?;
setinode(self, inid, &in)?;
setinblock(self, inblockid, &inblock)?;
};
fn insert_inodeblock(self: *bfs, inblockid: u64, inblock: *inode_block) (void | io::error) = {
inblock.prevblock = 0;
if (self.bootsector.nextinblk != 0) {
let nextinblock = inode_block { ... };
getinblock(self, self.bootsector.nextinblk, &nextinblock)?;
nextinblock.prevblock = inblockid;
setinblock(self, self.bootsector.nextinblk, &nextinblock)?;
};
inblock.nextblock = self.bootsector.nextinblk;
self.bootsector.nextinblk = inblockid;
commit(self)?;
};
fn remove_inodeblock(self: *bfs, inblockid: u64, inblock: *inode_block) (void | io::error) = {
if (inblock.prevblock != 0) {
let previnblock = inode_block { ... };
getinblock(self, inblock.prevblock, &previnblock)?;
previnblock.nextblock = inblock.nextblock;
setinblock(self, inblock.prevblock, &previnblock)?;
} else {
self.bootsector.nextinblk = inblock.nextblock;
commit(self)?;
};
if (inblock.nextblock != 0) {
let nextinblock = inode_block { ... };
getinblock(self, inblock.nextblock, &nextinblock)?;
nextinblock.prevblock = inblock.prevblock;
setinblock(self, inblock.nextblock, &nextinblock)?;
};
inblock.prevblock = 0;
inblock.nextblock = 0;
};
export fn open_file(self: *bfs, inid: u64) (*file | io::error) = {
let in = alloc(inode { ... });
match (getinode(self, inid, in)) {
case let e: io::error =>
free(in);
return e;
case => void;
};
return alloc(file {
stream_trait = &file_impl,
fs = self,
inid = inid,
in = in,
...
});
};
export fn create_file(self: *bfs) (*file | io::error) = {
const inid = allocinode(self)?;
let file = match (open_file(self, inid)) {
case let e: io::error =>
freeinode(self, inid)!;
return e;
case let f: *file =>
yield f;
};
file.in.block = match (allocblock(self)) {
case let e: io::error =>
io::close(file)!;
freeinode(self, inid)!;
return e;
case let b: u64 =>
yield b;
};
return file;
};
export fn root(self: *bfs) (*file | io::error) = {
if (self.bootsector.rootinode == 0) {
const file = create_file(self)?;
self.bootsector.rootinode == file.inid;
commit(self)!;
return file;
};
return open_file(self, self.bootsector.rootinode)?;
};
|