-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathk-chkfs.cc
371 lines (303 loc) · 10.4 KB
/
k-chkfs.cc
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#include "k-chkfs.hh"
#include "k-ahci.hh"
#include "k-chkfsiter.hh"
bufcache bufcache::bc;
bufcache::bufcache() {
}
// bufcache::get_disk_entry(bn, cleaner)
// Reads disk block `bn` into the buffer cache, obtains a reference to it,
// and returns a pointer to its bcentry. The returned bcentry has
// `buf_ != nullptr` and `estate_ >= es_clean`. The function may block.
//
// If this function reads the disk block from disk, and `cleaner != nullptr`,
// then `cleaner` is called on the entry to clean the block data.
//
// Returns `nullptr` if there's no room for the block.
bcentry* bufcache::get_disk_entry(chkfs::blocknum_t bn,
bcentry_clean_function cleaner) {
assert(chkfs::blocksize == PAGESIZE);
auto irqs = lock_.lock();
// look for slot containing `bn`
size_t i, empty_slot = -1;
for (i = 0; i != ne; ++i) {
if (e_[i].empty()) {
if (empty_slot == size_t(-1)) {
empty_slot = i;
}
} else if (e_[i].bn_ == bn) {
break;
}
}
// if not found, use free slot
if (i == ne) {
if (empty_slot == size_t(-1)) {
// cache full!
lock_.unlock(irqs);
log_printf("bufcache: no room for block %u\n", bn);
return nullptr;
}
i = empty_slot;
}
// obtain entry lock
e_[i].lock_.lock_noirq();
// mark allocated if empty
if (e_[i].empty()) {
e_[i].estate_ = bcentry::es_allocated;
e_[i].bn_ = bn;
}
// no longer need cache lock
lock_.unlock_noirq();
// mark reference
++e_[i].ref_;
// load block
bool ok = e_[i].load(irqs, cleaner);
// unlock and return entry
if (!ok) {
--e_[i].ref_;
}
e_[i].lock_.unlock(irqs);
return ok ? &e_[i] : nullptr;
}
// bcentry::load(irqs, cleaner)
// Completes the loading process for a block. Requires that `lock_` is
// locked, that `estate_ >= es_allocated`, and that `bn_` is set to the
// desired block number.
bool bcentry::load(irqstate& irqs, bcentry_clean_function cleaner) {
bufcache& bc = bufcache::get();
// load block, or wait for concurrent reader to load it
while (true) {
assert(estate_ != es_empty);
if (estate_ == es_allocated) {
if (!buf_) {
buf_ = reinterpret_cast<unsigned char*>
(kalloc(chkfs::blocksize));
if (!buf_) {
return false;
}
}
estate_ = es_loading;
lock_.unlock(irqs);
sata_disk->read(buf_, chkfs::blocksize,
bn_ * chkfs::blocksize);
irqs = lock_.lock();
estate_ = es_clean;
if (cleaner) {
cleaner(this);
}
bc.read_wq_.wake_all();
} else if (estate_ == es_loading) {
waiter().block_until(bc.read_wq_, [&] () {
return estate_ != es_loading;
}, lock_, irqs);
} else {
return true;
}
}
}
// bcentry::put()
// Releases a reference to this buffer cache entry. The caller must
// not use the entry after this call.
void bcentry::put() {
spinlock_guard guard(lock_);
assert(ref_ != 0);
if (--ref_ == 0) {
clear();
}
}
// bcentry::get_write()
// Obtains a write reference for this entry.
void bcentry::get_write() {
// Your code here
assert(false);
}
// bcentry::put_write()
// Releases a write reference for this entry.
void bcentry::put_write() {
// Your code here
assert(false);
}
// bufcache::sync(drop)
// Writes all dirty buffers to disk, blocking until complete.
// If `drop > 0`, then additionally free all buffer cache contents,
// except referenced blocks. If `drop > 1`, then assert that all inode
// and data blocks are unreferenced.
int bufcache::sync(int drop) {
// write dirty buffers to disk
// Your code here!
// drop clean buffers if requested
if (drop > 0) {
spinlock_guard guard(lock_);
for (size_t i = 0; i != ne; ++i) {
spinlock_guard eguard(e_[i].lock_);
// validity checks: referenced entries aren't empty; if drop > 1,
// no data blocks are referenced
assert(e_[i].ref_ == 0 || e_[i].estate_ != bcentry::es_empty);
if (e_[i].ref_ > 0 && drop > 1 && e_[i].bn_ >= 2) {
error_printf(CPOS(22, 0), COLOR_ERROR, "sync(2): block %u has nonzero reference count\n", e_[i].bn_);
assert_fail(__FILE__, __LINE__, "e_[i].bn_ < 2");
}
// actually drop buffer
if (e_[i].ref_ == 0) {
e_[i].clear();
}
}
}
return 0;
}
// inode lock functions
// The inode lock protects the inode's size and data references.
// It is a read/write lock; multiple readers can hold the lock
// simultaneously.
//
// IMPORTANT INVARIANT: If a kernel task has an inode lock, it
// must also hold a reference to the disk page containing that
// inode.
namespace chkfs {
void inode::lock_read() {
mlock_t v = mlock.load(std::memory_order_relaxed);
while (true) {
if (v >= mlock_t(-2)) {
current()->yield();
v = mlock.load(std::memory_order_relaxed);
} else if (mlock.compare_exchange_weak(v, v + 1,
std::memory_order_acquire)) {
return;
} else {
// `compare_exchange_weak` already reloaded `v`
pause();
}
}
}
void inode::unlock_read() {
mlock_t v = mlock.load(std::memory_order_relaxed);
assert(v != 0 && v != mlock_t(-1));
while (!mlock.compare_exchange_weak(v, v - 1,
std::memory_order_release)) {
pause();
}
}
void inode::lock_write() {
mlock_t v = 0;
while (!mlock.compare_exchange_weak(v, mlock_t(-1),
std::memory_order_acquire)) {
current()->yield();
v = 0;
}
}
void inode::unlock_write() {
assert(has_write_lock());
mlock.store(0, std::memory_order_release);
}
bool inode::has_write_lock() const {
return mlock.load(std::memory_order_relaxed) == mlock_t(-1);
}
}
// chickadeefs state
chkfsstate chkfsstate::fs;
chkfsstate::chkfsstate() {
}
// clean_inode_block(entry)
// Called when loading an inode block into the buffer cache. It clears
// values that are only used in memory.
static void clean_inode_block(bcentry* entry) {
uint32_t entry_index = entry->index();
auto is = reinterpret_cast<chkfs::inode*>(entry->buf_);
for (unsigned i = 0; i != chkfs::inodesperblock; ++i) {
// inode is initially unlocked
is[i].mlock = 0;
// containing entry's buffer cache position is `entry_index`
is[i].mbcindex = entry_index;
}
}
// chkfsstate::get_inode(inum)
// Returns inode number `inum`, or `nullptr` if there's no such inode.
// Obtains a reference on the buffer cache block containing the inode;
// you should eventually release this reference by calling `ino->put()`.
chkfs::inode* chkfsstate::get_inode(inum_t inum) {
auto& bc = bufcache::get();
auto superblock_entry = bc.get_disk_entry(0);
assert(superblock_entry);
auto& sb = *reinterpret_cast<chkfs::superblock*>
(&superblock_entry->buf_[chkfs::superblock_offset]);
superblock_entry->put();
chkfs::inode* ino = nullptr;
if (inum > 0 && inum < sb.ninodes) {
auto bn = sb.inode_bn + inum / chkfs::inodesperblock;
if (auto inode_entry = bc.get_disk_entry(bn, clean_inode_block)) {
ino = reinterpret_cast<inode*>(inode_entry->buf_);
}
}
if (ino != nullptr) {
ino += inum % chkfs::inodesperblock;
}
return ino;
}
namespace chkfs {
// chkfs::inode::entry()
// Returns a pointer to the buffer cache entry containing this inode.
// Requires that this inode is a pointer into buffer cache data.
bcentry* inode::entry() {
assert(mbcindex < bufcache::ne);
auto entry = &bufcache::get().e_[mbcindex];
assert(entry->contains(this));
return entry;
}
// chkfs::inode::put()
// Releases the caller’s reference to this inode, which must be located
// in the buffer cache.
void inode::put() {
entry()->put();
}
}
// chkfsstate::lookup_inode(dirino, filename)
// Looks up `filename` in the directory inode `dirino`, returning the
// corresponding inode (or nullptr if not found). The caller must have
// a read lock on `dirino`. The returned inode has a reference that
// the caller should eventually release with `ino->put()`.
chkfs::inode* chkfsstate::lookup_inode(inode* dirino,
const char* filename) {
chkfs_fileiter it(dirino);
// read directory to find file inode
chkfs::inum_t in = 0;
for (size_t diroff = 0; !in; diroff += blocksize) {
if (bcentry* e = it.find(diroff).get_disk_entry()) {
size_t bsz = min(dirino->size - diroff, blocksize);
auto dirent = reinterpret_cast<chkfs::dirent*>(e->buf_);
for (unsigned i = 0; i * sizeof(*dirent) < bsz; ++i, ++dirent) {
if (dirent->inum && strcmp(dirent->name, filename) == 0) {
in = dirent->inum;
break;
}
}
e->put();
} else {
return nullptr;
}
}
return get_inode(in);
}
// chkfsstate::lookup_inode(filename)
// Looks up `filename` in the root directory.
chkfs::inode* chkfsstate::lookup_inode(const char* filename) {
auto dirino = get_inode(1);
if (dirino) {
dirino->lock_read();
auto ino = fs.lookup_inode(dirino, filename);
dirino->unlock_read();
dirino->put();
return ino;
} else {
return nullptr;
}
}
// chkfsstate::allocate_extent(unsigned count)
// Allocates and returns the first block number of a fresh extent.
// The returned extent doesn't need to be initialized (but it should not be
// in flight to the disk or part of any incomplete journal transaction).
// Returns the block number of the first block in the extent, or an error
// code on failure. Errors can be distinguished by
// `blocknum >= blocknum_t(E_MINERROR)`.
auto chkfsstate::allocate_extent(unsigned count) -> blocknum_t {
// Your code here
return E_INVAL;
}