Skip to content

Commit 120cf2f

Browse files
committed
Init the ch7
1 parent d069510 commit 120cf2f

File tree

29 files changed

+824
-218
lines changed

29 files changed

+824
-218
lines changed

.github/workflows/build.yml

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,6 @@ env:
77
rust_toolchain: nightly
88

99
jobs:
10-
build-doc:
11-
if: github.repository == 'LearningOS/rCore-Tutorial-Code'
12-
runs-on: ubuntu-latest
13-
steps:
14-
- uses: actions/checkout@v4
15-
- name: Build doc
16-
run: |
17-
git clone https://github.com/LearningOS/rCore-Tutorial-Test.git user
18-
cd os
19-
make
20-
cargo doc --no-deps --verbose
21-
- name: Push to gh-pages
22-
uses: peaceiris/actions-gh-pages@v3
23-
with:
24-
github_token: ${{ secrets.GITHUB_TOKEN }}
25-
publish_dir: ./os/target/riscv64gc-unknown-none-elf/doc
26-
destination_dir: ${{ github.ref_name }}
2710
basic-test:
2811
runs-on: ubuntu-latest
2912
outputs:
@@ -40,13 +23,13 @@ jobs:
4023
git clone https://github.com/LearningOS/rCore-Tutorial-Checker.git ci-user
4124
git clone https://github.com/LearningOS/rCore-Tutorial-Test.git ci-user/user
4225
ID=`git rev-parse --abbrev-ref HEAD | grep -oP 'ch\K[0-9]'`
43-
cd ci-user && make test CHAPTER=$ID passwd=${{ secrets.BASE_TEST_TOKEN }} > ../output.txt
26+
cd ci-user && make test CHAPTER=$ID passwd=${{ secrets.BASE_TEST_TOKEN }} OFFLINE=1 > ../output.txt
4427
cat ../output.txt
4528
- name: end
4629
id: end
4730
run: |
48-
cat output.txt | grep -a "Test passed" | grep -oP "\d{1,}/\d{1,}" | xargs -i echo "points={}"
49-
cat output.txt | grep -a "Test passed" | grep -oP "\d{1,}/\d{1,}" | xargs -i echo "points={}" >> $GITHUB_OUTPUT
31+
cat output.txt | grep "Test passed" | grep -oP "\d{1,}/\d{1,}" | xargs -i echo "points={}"
32+
cat output.txt | grep "Test passed" | grep -oP "\d{1,}/\d{1,}" | xargs -i echo "points={}" >> $GITHUB_OUTPUT
5033
deploy:
5134
if: github.repository != 'LearningOS/rCore-Tutorial-Code'
5235
name: Deploy to pages

easy-fs/src/bitmap.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,30 @@
11
use super::{get_block_cache, BlockDevice, BLOCK_SZ};
22
use alloc::sync::Arc;
3-
/// A bitmap block
3+
44
type BitmapBlock = [u64; 64];
5-
/// Number of bits in a block
5+
66
const BLOCK_BITS: usize = BLOCK_SZ * 8;
7-
/// A bitmap
7+
88
pub struct Bitmap {
99
start_block_id: usize,
1010
blocks: usize,
1111
}
1212

13-
/// Decompose bits into (block_pos, bits64_pos, inner_pos)
13+
/// Return (block_pos, bits64_pos, inner_pos)
1414
fn decomposition(mut bit: usize) -> (usize, usize, usize) {
1515
let block_pos = bit / BLOCK_BITS;
1616
bit %= BLOCK_BITS;
1717
(block_pos, bit / 64, bit % 64)
1818
}
1919

2020
impl Bitmap {
21-
/// A new bitmap from start block id and number of blocks
2221
pub fn new(start_block_id: usize, blocks: usize) -> Self {
2322
Self {
2423
start_block_id,
2524
blocks,
2625
}
2726
}
28-
/// Allocate a new block from a block device
27+
2928
pub fn alloc(&self, block_device: &Arc<dyn BlockDevice>) -> Option<usize> {
3029
for block_id in 0..self.blocks {
3130
let pos = get_block_cache(
@@ -53,7 +52,7 @@ impl Bitmap {
5352
}
5453
None
5554
}
56-
/// Deallocate a block
55+
5756
pub fn dealloc(&self, block_device: &Arc<dyn BlockDevice>, bit: usize) {
5857
let (block_pos, bits64_pos, inner_pos) = decomposition(bit);
5958
get_block_cache(block_pos + self.start_block_id, Arc::clone(block_device))
@@ -63,7 +62,7 @@ impl Bitmap {
6362
bitmap_block[bits64_pos] -= 1u64 << inner_pos;
6463
});
6564
}
66-
/// Get the max number of allocatable blocks
65+
6766
pub fn maximum(&self) -> usize {
6867
self.blocks * BLOCK_BITS
6968
}

easy-fs/src/block_cache.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
use super::{BlockDevice, BLOCK_SZ};
22
use alloc::collections::VecDeque;
33
use alloc::sync::Arc;
4+
use alloc::vec;
5+
use alloc::vec::Vec;
46
use lazy_static::*;
57
use spin::Mutex;
6-
/// Cached block inside memory
8+
79
pub struct BlockCache {
8-
/// cached block data
9-
cache: [u8; BLOCK_SZ],
10-
/// underlying block id
10+
cache: Vec<u8>,
1111
block_id: usize,
12-
/// underlying block device
1312
block_device: Arc<dyn BlockDevice>,
14-
/// whether the block is dirty
1513
modified: bool,
1614
}
1715

1816
impl BlockCache {
1917
/// Load a new BlockCache from disk.
2018
pub fn new(block_id: usize, block_device: Arc<dyn BlockDevice>) -> Self {
21-
let mut cache = [0u8; BLOCK_SZ];
19+
// for alignment and move effciency
20+
let mut cache = vec![0u8; BLOCK_SZ];
2221
block_device.read_block(block_id, &mut cache);
2322
Self {
2423
cache,
@@ -27,7 +26,7 @@ impl BlockCache {
2726
modified: false,
2827
}
2928
}
30-
/// Get the address of an offset inside the cached block data
29+
3130
fn addr_of_offset(&self, offset: usize) -> usize {
3231
&self.cache[offset] as *const _ as usize
3332
}
@@ -74,7 +73,7 @@ impl Drop for BlockCache {
7473
self.sync()
7574
}
7675
}
77-
/// Use a block cache of 16 blocks
76+
7877
const BLOCK_CACHE_SIZE: usize = 16;
7978

8079
pub struct BlockCacheManager {
@@ -122,11 +121,10 @@ impl BlockCacheManager {
122121
}
123122

124123
lazy_static! {
125-
/// The global block cache manager
126124
pub static ref BLOCK_CACHE_MANAGER: Mutex<BlockCacheManager> =
127125
Mutex::new(BlockCacheManager::new());
128126
}
129-
/// Get the block cache corresponding to the given block id and block device
127+
130128
pub fn get_block_cache(
131129
block_id: usize,
132130
block_device: Arc<dyn BlockDevice>,
@@ -135,7 +133,7 @@ pub fn get_block_cache(
135133
.lock()
136134
.get_block_cache(block_id, block_device)
137135
}
138-
/// Sync all block cache to block device
136+
139137
pub fn block_cache_sync_all() {
140138
let manager = BLOCK_CACHE_MANAGER.lock();
141139
for (_, cache) in manager.queue.iter() {

easy-fs/src/block_dev.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
use core::any::Any;
2-
/// Trait for block devices
3-
/// which reads and writes data in the unit of blocks
2+
43
pub trait BlockDevice: Send + Sync + Any {
5-
///Read data form block to buffer
64
fn read_block(&self, block_id: usize, buf: &mut [u8]);
7-
///Write data from buffer to block
85
fn write_block(&self, block_id: usize, buf: &[u8]);
96
}

easy-fs/src/efs.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,18 @@ use super::{
55
use crate::BLOCK_SZ;
66
use alloc::sync::Arc;
77
use spin::Mutex;
8-
///An easy file system on block
8+
99
pub struct EasyFileSystem {
10-
///Real device
1110
pub block_device: Arc<dyn BlockDevice>,
12-
///Inode bitmap
1311
pub inode_bitmap: Bitmap,
14-
///Data bitmap
1512
pub data_bitmap: Bitmap,
1613
inode_area_start_block: u32,
1714
data_area_start_block: u32,
1815
}
1916

2017
type DataBlock = [u8; BLOCK_SZ];
21-
/// An easy fs over a block device
18+
2219
impl EasyFileSystem {
23-
/// A data block of block size
2420
pub fn create(
2521
block_device: Arc<dyn BlockDevice>,
2622
total_blocks: u32,
@@ -81,7 +77,7 @@ impl EasyFileSystem {
8177
block_cache_sync_all();
8278
Arc::new(Mutex::new(efs))
8379
}
84-
/// Open a block device as a filesystem
80+
8581
pub fn open(block_device: Arc<dyn BlockDevice>) -> Arc<Mutex<Self>> {
8682
// read SuperBlock
8783
get_block_cache(0, Arc::clone(&block_device))
@@ -103,15 +99,15 @@ impl EasyFileSystem {
10399
Arc::new(Mutex::new(efs))
104100
})
105101
}
106-
/// Get the root inode of the filesystem
102+
107103
pub fn root_inode(efs: &Arc<Mutex<Self>>) -> Inode {
108104
let block_device = Arc::clone(&efs.lock().block_device);
109105
// acquire efs lock temporarily
110106
let (block_id, block_offset) = efs.lock().get_disk_inode_pos(0);
111107
// release efs lock
112108
Inode::new(block_id, block_offset, Arc::clone(efs), block_device)
113109
}
114-
/// Get inode by id
110+
115111
pub fn get_disk_inode_pos(&self, inode_id: u32) -> (u32, usize) {
116112
let inode_size = core::mem::size_of::<DiskInode>();
117113
let inodes_per_block = (BLOCK_SZ / inode_size) as u32;
@@ -121,20 +117,20 @@ impl EasyFileSystem {
121117
(inode_id % inodes_per_block) as usize * inode_size,
122118
)
123119
}
124-
/// Get data block by id
120+
125121
pub fn get_data_block_id(&self, data_block_id: u32) -> u32 {
126122
self.data_area_start_block + data_block_id
127123
}
128-
/// Allocate a new inode
124+
129125
pub fn alloc_inode(&mut self) -> u32 {
130126
self.inode_bitmap.alloc(&self.block_device).unwrap() as u32
131127
}
132128

133-
/// Allocate a data block
129+
/// Return a block ID not ID in the data area.
134130
pub fn alloc_data(&mut self) -> u32 {
135131
self.data_bitmap.alloc(&self.block_device).unwrap() as u32 + self.data_area_start_block
136132
}
137-
/// Deallocate a data block
133+
138134
pub fn dealloc_data(&mut self, block_id: u32) {
139135
get_block_cache(block_id as usize, Arc::clone(&self.block_device))
140136
.lock()

0 commit comments

Comments
 (0)