Skip to content

Commit e362c64

Browse files
committed
fs: implicit brelse
1 parent c93ab7f commit e362c64

File tree

7 files changed

+31
-54
lines changed

7 files changed

+31
-54
lines changed

usr/xv6/kernel/fs/src/bcache.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ impl DerefMut for BufferBlockWrapper {
4545
pub struct BufferGuard {
4646
dev: u32,
4747
block_number: u32,
48-
index: i32,
48+
index: usize,
4949
buffer: Arc<Mutex<BufferBlockWrapper>>,
5050
bcache: &'static BufferCache,
5151
}
@@ -60,19 +60,19 @@ impl BufferGuard {
6060
}
6161

6262
pub fn pin(&self) {
63-
self.bcache.pin(self.index as usize)
63+
self.bcache.pin(self.index)
6464
}
6565

6666
pub fn unpin(&self) {
67-
self.bcache.unpin(self.index as usize)
67+
self.bcache.unpin(self.index)
6868
}
6969
}
7070

7171
// I could've get a reference to the bcache and do a brelse explicitly when the guard is dropped.
7272
// But I don't want to deal with the lifetime for now. Might do it later
7373
impl Drop for BufferGuard {
7474
fn drop(&mut self) {
75-
assert!(self.index < 0, "You forgot to release the buffer back to the bcache");
75+
self.bcache.release(self.index)
7676
}
7777
}
7878

@@ -147,15 +147,15 @@ impl BufferCacheInternal {
147147
// look through buffer cache, return the buffer
148148
// If the block does not exist, we preempt a not-in-use one
149149
// We let the caller to lock the buffer when they need to use it
150-
fn get(&mut self, dev: u32, block_number: u32) -> (bool, i32, Arc<Mutex<BufferBlockWrapper>>) {
150+
fn get(&mut self, dev: u32, block_number: u32) -> (bool, usize, Arc<Mutex<BufferBlockWrapper>>) {
151151
// println!("{:?} {:?}", &(dev, block_number), self.map.get(&(dev, block_number)));
152152
match self.map.get(&(dev, block_number)) {
153153
Some(index) => {
154154
let buffer = &mut self.buffers[*index];
155155
assert!(buffer.dev == dev);
156156
assert!(buffer.block_number == block_number);
157157
buffer.reference_count += 1;
158-
(true, *index as i32, buffer.data.clone())
158+
(true, *index, buffer.data.clone())
159159
},
160160
None => {
161161
// Not cached; recycle an unused buffer.
@@ -173,7 +173,7 @@ impl BufferCacheInternal {
173173
buffer.block_number = block_number;
174174
buffer.reference_count = 1;
175175
assert!(self.map.insert((dev, block_number), curr as usize).is_none());
176-
return (false, curr, buffer.data.clone())
176+
return (false, curr as usize, buffer.data.clone())
177177
}
178178
curr = buffer.prev;
179179
}
@@ -254,9 +254,8 @@ impl BufferCache {
254254
// This is confusing since it doesn't match xv6's brelse exactly so there could be a bug.
255255
// Check xv6 for details
256256
// TODO(tianjiao): fix this
257-
pub fn release(&self, guard: &mut BufferGuard) {
258-
self.internal.lock().release(guard.index as usize);
259-
guard.index = -1;
257+
fn release(&self, index: usize) {
258+
self.internal.lock().release(index);
260259
}
261260

262261
fn pin(&self, index: usize) {

usr/xv6/kernel/fs/src/block.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ pub fn free(trans: &mut Transaction, device: u32, block: u32) {
1818
buffer[bi / 8] &= !m;
1919
trans.write(&bguard);
2020
drop(buffer);
21-
BCACHE.force_get().release(&mut bguard);
2221
}
2322

2423
// Allocate a zeroed disk block.
@@ -39,17 +38,15 @@ pub fn alloc(trans: &mut Transaction, device: u32) -> Option<u32> {
3938
trans.write(&bguard);
4039

4140
drop(buffer);
42-
BCACHE.force_get().release(&mut bguard);
43-
41+
4442
zero(trans, device, b + bi as u32);
4543
return Some(b + bi as u32);
4644
}
4745
bi += 1;
4846
}
4947

5048
drop(buffer);
51-
BCACHE.force_get().release(&mut bguard);
52-
}
49+
}
5350

5451
// out of blocks
5552
None
@@ -67,8 +64,7 @@ fn zero(trans: &mut Transaction, device: u32, block_number: u32) {
6764

6865
trans.write(&bguard);
6966
drop(buffer);
70-
BCACHE.force_get().release(&mut bguard);
71-
}
67+
}
7268

7369
// Block of free map containing bit for block b
7470
// xv6 equivalent: BBLOCK

usr/xv6/kernel/fs/src/fs.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ impl SuperBlock {
4646
fn read_superblock(dev: u32) -> SuperBlock {
4747
let mut buffer = BCACHE.force_get().read(dev, 1);
4848
let superblock = SuperBlock::from_bytes(&***buffer.lock());
49-
BCACHE.force_get().release(&mut buffer);
50-
console::println!("Superblock read from disk: {:?}", superblock);
49+
console::println!("Superblock read from disk: {:?}", superblock);
5150
superblock
5251
}
5352

usr/xv6/kernel/fs/src/icache/icache.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,10 @@ impl ICache {
4949
trans.write(&bguard);
5050

5151
drop(buffer);
52-
BCACHE.force_get().release(&mut bguard);
53-
return self.get(device, inum);
52+
return self.get(device, inum);
5453
}
5554
drop(buffer);
56-
BCACHE.force_get().release(&mut bguard);
57-
}
55+
}
5856
None
5957
}
6058

usr/xv6/kernel/fs/src/icache/inode.rs

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,7 @@ impl INodeDataGuard<'_> {
146146
trans.write(&bguard);
147147

148148
drop(buffer);
149-
BCACHE.force_get().release(&mut bguard);
150-
}
149+
}
151150

152151
// Discard contents of node
153152
// Only called when node has no links and no other in-memory references to it
@@ -175,8 +174,7 @@ impl INodeDataGuard<'_> {
175174
}
176175
}
177176
drop(buffer);
178-
BCACHE.force_get().release(&mut bguard);
179-
177+
180178
self.data.addresses[params::NDIRECT] = 0;
181179
}
182180

@@ -244,8 +242,7 @@ impl INodeDataGuard<'_> {
244242
}
245243

246244
drop(buffer);
247-
BCACHE.force_get().release(&mut bguard);
248-
245+
249246
// Load level 2 indirect block, allocating if necessary.
250247
let mut bguard = BCACHE.force_get().read(self.node.meta.device, address);
251248
let buffer = bguard.lock();
@@ -266,8 +263,7 @@ impl INodeDataGuard<'_> {
266263
}
267264

268265
drop(buffer);
269-
BCACHE.force_get().release(&mut bguard);
270-
266+
271267
address
272268
}
273269

@@ -368,8 +364,7 @@ impl INodeDataGuard<'_> {
368364
user_buffer[user_offset..(user_offset + bytes_read)].copy_from_slice(&buffer[start..(start + bytes_read)]);
369365

370366
drop(buffer);
371-
BCACHE.force_get().release(&mut bguard);
372-
367+
373368
total += bytes_read;
374369
offset += bytes_read;
375370
user_offset += bytes_read;
@@ -410,8 +405,7 @@ impl INodeDataGuard<'_> {
410405

411406
trans.write(&bguard);
412407
drop(buffer);
413-
BCACHE.force_get().release(&mut bguard);
414-
408+
415409
total += bytes_written;
416410
offset += bytes_written;
417411
user_offset += bytes_written;
@@ -457,11 +451,9 @@ impl INodeDataGuard<'_> {
457451
}
458452
}
459453
drop(buffer);
460-
BCACHE.force_get().release(&mut bguard);
461-
}
454+
}
462455
drop(buffer);
463-
BCACHE.force_get().release(&mut bguard);
464-
456+
465457
if self.data.file_type != INodeFileType::Directory {
466458
return;
467459
}
@@ -545,8 +537,7 @@ impl INode {
545537
data.copy_from_bytes(&buffer[dinode_offset..dinode_offset + DINODE_SIZE]);
546538

547539
drop(buffer);
548-
BCACHE.force_get().release(&mut bguard);
549-
540+
550541
self.meta.valid.store(true, Ordering::Relaxed);
551542

552543
if data.file_type == INodeFileType::Unitialized {

usr/xv6/kernel/fs/src/log/log.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,14 @@ impl LogInternal {
7474
BCACHE.force_get().write(dbuf.block_number(), &mut locked_dbuf); // write dst to disk
7575
}
7676
dbuf.unpin();
77-
BCACHE.force_get().release(&mut lbuf);
78-
BCACHE.force_get().release(&mut dbuf);
79-
}
77+
}
8078
}
8179

8280
// Read the log header from disk into the in-memory log header
8381
fn read_head(&mut self) {
8482
let mut buf = BCACHE.force_get().read(self.dev, self.start);
8583
self.logheader.from_buffer_block(&buf.lock());
86-
BCACHE.force_get().release(&mut buf);
87-
console::println!("Log::read_head: {:?}", self);
84+
console::println!("Log::read_head: {:?}", self);
8885
}
8986

9087
// Write in-memory log header to disk.
@@ -97,8 +94,7 @@ impl LogInternal {
9794
self.logheader.to_buffer_block(&mut locked_buf);
9895
BCACHE.force_get().write(buf.block_number(), &mut locked_buf);
9996
}
100-
BCACHE.force_get().release(&mut buf);
101-
}
97+
}
10298

10399
fn recover_from_log(&mut self) {
104100
self.read_head();
@@ -114,7 +110,7 @@ impl LogInternal {
114110
// And end_op will be called when the guard is dropped.
115111
// TODO(tianjiao): fix this
116112
pub fn try_begin_op(&mut self) -> bool {
117-
console::println!("try_begin_op; {:?}", self);
113+
// console::println!("try_begin_op; {:?}", self);
118114
if self.committing {
119115
return false;
120116
}
@@ -157,9 +153,7 @@ impl LogInternal {
157153
locked_to = from.lock();
158154
BCACHE.force_get().write(to.block_number(), &mut locked_to); // write the log
159155
}
160-
BCACHE.force_get().release(&mut from);
161-
BCACHE.force_get().release(&mut to);
162-
}
156+
}
163157
}
164158

165159
fn commit(&mut self) {
@@ -187,7 +181,7 @@ impl LogInternal {
187181
"too big a transaction");
188182
assert!(self.outstanding >= 1, "log_write outside of trans");
189183

190-
console::println!("writing {} to transaction", buffer.block_number());
184+
// console::println!("writing {} to transaction", buffer.block_number());
191185
// Find the index that the block should belong to.
192186
// Log absorbtion: if the block is already in the log, don't need to do anything.
193187
// Else, add the new block to the log

usr/xv6/usr/sh/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub fn init(s: Box<dyn Syscall + Send + Sync>, heap: Box<dyn Heap + Send + Sync>
3737
rref::init(heap);
3838
println!("Starting rv6 shell with args: {}", args);
3939

40-
// sys_load_domain("benchfs", "benchfs", &[Some(0), Some(1), Some(2)]).unwrap();
40+
sys_load_domain("benchfs", "benchfs", &[Some(0), Some(1), Some(2)]).unwrap();
4141

4242
const prompt: &'static str = "rv6> ";
4343
loop {

0 commit comments

Comments
 (0)