forked from CS161/chickadee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathk-chkfs.cc
454 lines (374 loc) · 13.1 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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
#include "k-chkfs.hh"
#include "k-devices.hh"
#include "k-chkfsiter.hh"
buffcache buffcache::bc;
buffcache::buffcache() {
}
// buffcache::get_disk_entry(bn, cleaner)
// Read disk block `bn` into the buffer cache, obtain a reference to it,
// and return a pointer to its buffentry. The function may block. If this
// function reads the disk block from disk, and `cleaner != nullptr`,
// then `cleaner` is called on the block data. Returns `nullptr` if
// there's no room for the block.
buffentry* buffcache::get_disk_entry(chickadeefs::blocknum_t bn,
clean_block_function cleaner) {
assert(chickadeefs::blocksize == PAGESIZE);
auto irqs = lock_.lock();
// look for slot containing `bn`
size_t i;
for (i = 0; i != ne; ++i) {
if (e_[i].bn_ == bn) {
break;
}
}
// if not found, look for free slot
if (i == ne) {
for (i = 0; i != ne && e_[i].bn_ != emptyblock; ++i) {
}
if (i == ne) {
// cache full!
lock_.unlock(irqs);
log_printf("buffcache: no room for block %u\n", bn);
return nullptr;
}
e_[i].bn_ = bn;
}
// mark reference
++e_[i].ref_;
// switch lock to entry lock
e_[i].lock_.lock_noirq();
lock_.unlock_noirq();
// load block, or wait for concurrent reader to load it
while (!(e_[i].flags_ & buffentry::f_loaded)) {
if (!(e_[i].flags_ & buffentry::f_loading)) {
if (!e_[i].buff_) {
e_[i].buff_ = kalloc(chickadeefs::blocksize);
if (!e_[i].buff_) {
--e_[i].ref_;
e_[i].lock_.unlock(irqs);
return nullptr;
}
}
e_[i].flags_ |= buffentry::f_loading;
e_[i].lock_.unlock(irqs);
sata_disk->read(e_[i].buff_, chickadeefs::blocksize,
bn * chickadeefs::blocksize);
irqs = e_[i].lock_.lock();
e_[i].flags_ = (e_[i].flags_ & ~buffentry::f_loading)
| buffentry::f_loaded;
if (cleaner) {
cleaner(e_[i].buff_);
}
read_wq_.wake_all();
} else {
waiter(current()).block_until(read_wq_, [&] () {
return (e_[i].flags_ & buffentry::f_loading) == 0;
}, e_[i].lock_, irqs);
}
}
// return entry
e_[i].lock_.unlock(irqs);
return &e_[i];
}
// buffcache::find_entry(buff)
// Return the `buffentry` containing pointer `buff`. This entry
// must have a nonzero `ref_`.
buffentry* buffcache::find_entry(void* buff) {
if (buff) {
buff = ROUNDDOWN(buff, chickadeefs::blocksize);
// Synchronization is not necessary!
// 1. The relevant entry has nonzero `ref_`, so its `buff_`
// will not change.
// 2. No other entry has the same `buff_` because nonempty
// entries have unique `buff_`s.
// (XXX Really, though, `buff_` should be std::atomic<void*>.)
for (size_t i = 0; i != ne; ++i) {
if (e_[i].buff_ == buff) {
return &e_[i];
}
}
assert(false);
}
return nullptr;
}
// buffcache::put_entry(e)
// Decrement the reference count for buffer cache entry `e`.
void buffcache::put_entry(buffentry* e) {
if (e) {
auto irqs = e->lock_.lock();
--e->ref_;
if (e->ref_ == 0) {
kfree(e->buff_);
e->clear();
}
e->lock_.unlock(irqs);
}
}
// buffcache::get_write(e)
// Obtain a write reference for `e`.
void buffcache::get_write(buffentry* e) {
// Your code here
assert(false);
}
// buffcache::put_write(e)
// Release a write reference for `e`.
void buffcache::put_write(buffentry* e) {
// Your code here
assert(false);
}
// buffcache::sync(drop)
// Write all dirty buffers to disk (blocking until complete).
// Additionally free all buffer cache contents, except referenced
// blocks, if `drop` is true.
int buffcache::sync(bool drop) {
// Write dirty buffers to disk: your code here!
if (drop) {
auto irqs = lock_.lock();
for (size_t i = 0; i != ne; ++i) {
if (e_[i].bn_ != emptyblock && !e_[i].ref_) {
kfree(e_[i].buff_);
e_[i].clear();
}
}
lock_.unlock(irqs);
}
return 0;
}
// clean_inode_block(buff)
// This function is called when loading an inode block into the
// buffer cache. It clears values that are only used in memory.
static void clean_inode_block(void* buff) {
auto is = reinterpret_cast<chickadeefs::inode*>(buff);
for (unsigned i = 0; i != chickadeefs::inodesperblock; ++i) {
is[i].mlock = is[i].mref = 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 chickadeefs {
void inode::lock_read() {
uint32_t v = mlock.load(std::memory_order_relaxed);
while (1) {
if (v == uint32_t(-1)) {
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() {
uint32_t v = mlock.load(std::memory_order_relaxed);
assert(v != 0 && v != uint32_t(-1));
while (!mlock.compare_exchange_weak(v, v - 1,
std::memory_order_release)) {
pause();
}
}
void inode::lock_write() {
uint32_t v = 0;
while (!mlock.compare_exchange_weak(v, uint32_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) == uint32_t(-1);
}
}
// chickadeefs state
chkfsstate chkfsstate::fs;
chkfsstate::chkfsstate() {
}
// chkfsstate::get_inode(inum)
// Return inode number `inum`, or `nullptr` if there's no such inode.
// The returned pointer should eventually be passed to `put_inode`.
chickadeefs::inode* chkfsstate::get_inode(inum_t inum) {
auto& bc = buffcache::get();
unsigned char* superblock_data = reinterpret_cast<unsigned char*>
(bc.get_disk_block(0));
assert(superblock_data);
auto sb = reinterpret_cast<chickadeefs::superblock*>
(&superblock_data[chickadeefs::superblock_offset]);
auto inode_bn = sb->inode_bn;
auto ninodes = sb->ninodes;
bc.put_block(superblock_data);
chickadeefs::inode* ino = nullptr;
if (inum > 0 && inum < ninodes) {
ino = reinterpret_cast<inode*>
(bc.get_disk_block(inode_bn + inum / chickadeefs::inodesperblock,
clean_inode_block));
}
if (ino != nullptr) {
ino += inum % chickadeefs::inodesperblock;
}
return ino;
}
// chkfsstate::put_inode(ino)
// Drop the reference to `ino`.
void chkfsstate::put_inode(inode* ino) {
if (ino) {
buffcache::get().put_block(ROUNDDOWN(ino, PAGESIZE));
}
}
// chkfsstate::get_data_block(ino, off)
// Return a pointer to the data page at offset `off` into inode `ino`.
// `off` must be a multiple of `blocksize`. May return `nullptr` if
// no block has been allocated there. If the file is being read,
// then at most `min(blocksize, ino->size - off)` bytes of data
// in the returned page are valid.
unsigned char* chkfsstate::get_data_block(inode* ino, size_t off) {
assert(off % blocksize == 0);
auto& bc = buffcache::get();
buffentry* i2e = nullptr; // buffentry for indirect2 block (if needed)
blocknum_t* iptr = nullptr; // pointer to indirect block # (if needed)
buffentry* ie = nullptr; // buffentry for indirect block (if needed)
blocknum_t* dptr = nullptr; // pointer to direct block #
buffentry* de = nullptr; // buffentry for direct block
unsigned bi = off / blocksize;
// Set `iptr` to point to the relevant indirect block number
// (if one is needed). This is either a pointer into the
// indirect2 block, or a pointer to `ino->indirect`, or null.
if (bi >= chickadeefs::ndirect + chickadeefs::nindirect) {
if (ino->indirect2 != 0) {
i2e = bc.get_disk_entry(ino->indirect2);
}
if (!i2e) {
goto done;
}
iptr = reinterpret_cast<blocknum_t*>(i2e->buff_)
+ chickadeefs::bi_indirect_index(bi);
} else if (bi >= chickadeefs::ndirect) {
iptr = &ino->indirect;
}
// Set `dptr` to point to the relevant data block number.
// This is either a pointer into an indirect block, or a
// pointer to one of the `ino->direct` entries.
if (iptr) {
if (*iptr != 0) {
ie = bc.get_disk_entry(*iptr);
}
if (!ie) {
goto done;
}
dptr = reinterpret_cast<blocknum_t*>(ie->buff_)
+ chickadeefs::bi_direct_index(bi);
} else {
dptr = &ino->direct[chickadeefs::bi_direct_index(bi)];
}
// Finally, load the data block.
if (*dptr != 0) {
de = bc.get_disk_entry(*dptr);
}
done:
// We don't need the indirect and doubly-indirect entries.
bc.put_entry(ie);
bc.put_entry(i2e);
return de ? reinterpret_cast<unsigned char*>(de->buff_) : nullptr;
}
// chkfsstate::lookup_inode(dirino, filename)
// Look up `filename` in the directory inode `dirino`, returning the
// corresponding inode (or nullptr if not found). The caller should
// eventually call `put_inode` on the returned inode pointer.
chickadeefs::inode* chkfsstate::lookup_inode(inode* dirino,
const char* filename) {
auto& bc = buffcache::get();
chkfs_fileiter it(dirino);
// read directory to find file inode
chickadeefs::inum_t in = 0;
for (size_t diroff = 0; !in; diroff += blocksize) {
buffentry* e;
if (!(it.find(diroff).present()
&& (e = bc.get_disk_entry(it.blocknum())))) {
break;
}
size_t bsz = min(dirino->size - diroff, blocksize);
auto dirent = reinterpret_cast<chickadeefs::dirent*>(e->buff_);
for (unsigned i = 0; i * sizeof(*dirent) < bsz; ++i, ++dirent) {
if (dirent->inum && strcmp(dirent->name, filename) == 0) {
in = dirent->inum;
break;
}
}
bc.put_entry(e);
}
return get_inode(in);
}
// chkfsstate::allocate_block()
// Allocate and return the number of a fresh block. The returned
// block need not be initialized (but it should not be in flight
// to the disk or part of any incomplete journal transaction).
// Returns the block number or an error code on failure. Errors
// can be distinguished by `blocknum >= blocknum_t(E_MINERROR)`.
auto chkfsstate::allocate_block() -> blocknum_t {
// Your code here
return E_INVAL;
}
// chickadeefs_read_file_data(filename, buff, sz, off)
// Read up to `sz` bytes, from the file named `filename` in the
// disk's root directory, into `buff`, starting at file offset `off`.
// Returns the number of bytes read.
size_t chickadeefs_read_file_data(const char* filename,
void* buff, size_t sz, size_t off) {
auto& bc = buffcache::get();
auto& fs = chkfsstate::get();
// read directory to find file inode number
auto dirino = fs.get_inode(1);
assert(dirino);
dirino->lock_read();
auto ino = fs.lookup_inode(dirino, filename);
dirino->unlock_read();
fs.put_inode(dirino);
if (!ino) {
return 0;
}
// read file inode
ino->lock_read();
chkfs_fileiter it(ino);
size_t nread = 0;
while (sz > 0) {
size_t ncopy = 0;
// read inode contents, copy data
buffentry* e;
if (it.find(off).present()
&& (e = bc.get_disk_entry(it.blocknum()))) {
size_t blockoff = ROUNDDOWN(off, fs.blocksize);
size_t bsz = min(ino->size - blockoff, fs.blocksize);
size_t boff = off - blockoff;
if (bsz > boff) {
ncopy = bsz - boff;
if (ncopy > sz) {
ncopy = sz;
}
memcpy(reinterpret_cast<unsigned char*>(buff) + nread,
reinterpret_cast<unsigned char*>(e->buff_) + boff,
ncopy);
}
bc.put_entry(e);
}
// account for copied data
if (ncopy == 0) {
break;
}
nread += ncopy;
off += ncopy;
sz -= ncopy;
}
ino->unlock_read();
fs.put_inode(ino);
return nread;
}