# efs.rs - source
Source of the Rust file `/home/runner/work/rCore-Tutorial-v3/rCore-Tutorial-v3/easy-fs/src/efs.rs`.
Keywords: rust, rustlang, rust-lang

    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

    use super::{
        block_cache_sync_all, get_block_cache, Bitmap, BlockDevice, DiskInode, DiskInodeType, Inode,
        SuperBlock,
    };
    use crate::BLOCK_SZ;
    use alloc::sync::Arc;
    use spin::Mutex;
    ///An easy file system on block
    pub struct EasyFileSystem {
        ///Real device
        pub block_device: Arc<dyn BlockDevice>,
        ///Inode bitmap
        pub inode_bitmap: Bitmap,
        ///Data bitmap
        pub data_bitmap: Bitmap,
        inode_area_start_block: u32,
        data_area_start_block: u32,
    }

    type DataBlock = [u8; BLOCK_SZ];
    /// An easy fs over a block device
    impl EasyFileSystem {
        /// A data block of block size
        pub fn create(
            block_device: Arc<dyn BlockDevice>,
            total_blocks: u32,
            inode_bitmap_blocks: u32,
        ) -> Arc<Mutex<Self>> {
            // calculate block size of areas & create bitmaps
            let inode_bitmap = Bitmap::new(1, inode_bitmap_blocks as usize);
            let inode_num = inode_bitmap.maximum();
            let inode_area_blocks =
                ((inode_num * core::mem::size_of::<DiskInode>() + BLOCK_SZ - 1) / BLOCK_SZ) as u32;
            let inode_total_blocks = inode_bitmap_blocks + inode_area_blocks;
            let data_total_blocks = total_blocks - 1 - inode_total_blocks;
            let data_bitmap_blocks = (data_total_blocks + 4096) / 4097;
            let data_area_blocks = data_total_blocks - data_bitmap_blocks;
            let data_bitmap = Bitmap::new(
                (1 + inode_bitmap_blocks + inode_area_blocks) as usize,
                data_bitmap_blocks as usize,
            );
            let mut efs = Self {
                block_device: Arc::clone(&block_device),
                inode_bitmap,
                data_bitmap,
                inode_area_start_block: 1 + inode_bitmap_blocks,
                data_area_start_block: 1 + inode_total_blocks + data_bitmap_blocks,
            };
            // clear all blocks
            for i in 0..total_blocks {
                get_block_cache(i as usize, Arc::clone(&block_device))
                    .lock()
                    .modify(0, |data_block: &mut DataBlock| {
                        for byte in data_block.iter_mut() {
                            *byte = 0;
                        }
                    });
            }
            // initialize SuperBlock
            get_block_cache(0, Arc::clone(&block_device)).lock().modify(
                0,
                |super_block: &mut SuperBlock| {
                    super_block.initialize(
                        total_blocks,
                        inode_bitmap_blocks,
                        inode_area_blocks,
                        data_bitmap_blocks,
                        data_area_blocks,
                    );
                },
            );
            // write back immediately
            // create a inode for root node "/"
            assert_eq!(efs.alloc_inode(), 0);
            let (root_inode_block_id, root_inode_offset) = efs.get_disk_inode_pos(0);
            get_block_cache(root_inode_block_id as usize, Arc::clone(&block_device))
                .lock()
                .modify(root_inode_offset, |disk_inode: &mut DiskInode| {
                    disk_inode.initialize(DiskInodeType::Directory);
                });
            block_cache_sync_all();
            Arc::new(Mutex::new(efs))
        }
        /// Open a block device as a filesystem
        pub fn open(block_device: Arc<dyn BlockDevice>) -> Arc<Mutex<Self>> {
            // read SuperBlock
            get_block_cache(0, Arc::clone(&block_device))
                .lock()
                .read(0, |super_block: &SuperBlock| {
                    assert!(super_block.is_valid(), "Error loading EFS!");
                    let inode_total_blocks =
                        super_block.inode_bitmap_blocks + super_block.inode_area_blocks;
                    let efs = Self {
                        block_device,
                        inode_bitmap: Bitmap::new(1, super_block.inode_bitmap_blocks as usize),
                        data_bitmap: Bitmap::new(
                            (1 + inode_total_blocks) as usize,
                            super_block.data_bitmap_blocks as usize,
                        ),
                        inode_area_start_block: 1 + super_block.inode_bitmap_blocks,
                        data_area_start_block: 1 + inode_total_blocks + super_block.data_bitmap_blocks,
                    };
                    Arc::new(Mutex::new(efs))
                })
        }
        /// Get the root inode of the filesystem
        pub fn root_inode(efs: &Arc<Mutex<Self>>) -> Inode {
            let block_device = Arc::clone(&efs.lock().block_device);
            // acquire efs lock temporarily
            let (block_id, block_offset) = efs.lock().get_disk_inode_pos(0);
            // release efs lock
            Inode::new(block_id, block_offset, Arc::clone(efs), block_device)
        }
        /// Get inode by id
        pub fn get_disk_inode_pos(&self, inode_id: u32) -> (u32, usize) {
            let inode_size = core::mem::size_of::<DiskInode>();
            let inodes_per_block = (BLOCK_SZ / inode_size) as u32;
            let block_id = self.inode_area_start_block + inode_id / inodes_per_block;
            (
                block_id,
                (inode_id % inodes_per_block) as usize * inode_size,
            )
        }
        /// Get data block by id
        pub fn get_data_block_id(&self, data_block_id: u32) -> u32 {
            self.data_area_start_block + data_block_id
        }
        /// Allocate a new inode
        pub fn alloc_inode(&mut self) -> u32 {
            self.inode_bitmap.alloc(&self.block_device).unwrap() as u32
        }

        /// Allocate a data block
        pub fn alloc_data(&mut self) -> u32 {
            self.data_bitmap.alloc(&self.block_device).unwrap() as u32 + self.data_area_start_block
        }
        /// Deallocate a data block
        pub fn dealloc_data(&mut self, block_id: u32) {
            get_block_cache(block_id as usize, Arc::clone(&self.block_device))
                .lock()
                .modify(0, |data_block: &mut DataBlock| {
                    data_block.iter_mut().for_each(|p| {
                        *p = 0;
                    })
                });
            self.data_bitmap.dealloc(
                &self.block_device,
                (block_id - self.data_area_start_block) as usize,
            )
        }
    }