From 7658341eb936889420d68cc9a07e82e367d74b8e Mon Sep 17 00:00:00 2001 From: WangRunji Date: Sat, 28 Apr 2018 23:49:46 +0800 Subject: [PATCH] Init. Basic structures. --- .gitignore | 5 +++ Cargo.toml | 7 ++++ src/lib.rs | 10 +++++ src/structs.rs | 111 +++++++++++++++++++++++++++++++++++++++++++++++++ src/tests.rs | 4 ++ 5 files changed, 137 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 src/lib.rs create mode 100644 src/structs.rs create mode 100644 src/tests.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d3f1b89 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +/target +**/*.rs.bk +Cargo.lock +.idea +/sfs.img diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..acec5ca --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "simple-filesystem" +version = "0.1.0" +authors = ["WangRunji "] + +[dependencies] +spin = "0.4" \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..a2a1f1e --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,10 @@ +#![feature(alloc)] +#![no_std] + +extern crate spin; +extern crate alloc; + +mod vfs; +mod structs; +#[cfg(test)] +mod tests; \ No newline at end of file diff --git a/src/structs.rs b/src/structs.rs new file mode 100644 index 0000000..e23d2e3 --- /dev/null +++ b/src/structs.rs @@ -0,0 +1,111 @@ +use spin::Mutex; +use alloc::Vec; +//use core::fmt::{Debug, Formatter, Error}; + +/// On-disk superblock +#[repr(C, packed)] +struct SuperBlock { + /// magic number, should be SFS_MAGIC + magic: u32, + /// number of blocks in fs + blocks: u32, + /// number of unused blocks in fs + unused_blocks: u32, + /// infomation for sfs + info: [u8; MAX_INFO_LEN + 1], +} + +/// inode (on disk) +#[repr(C, packed)] +#[derive(Debug)] +struct DiskInode { + /// size of the file (in bytes) + size: u32, + /// one of SYS_TYPE_* above + type_: u16, + /// number of hard links to this file + nlinks: u16, + /// number of blocks + blocks: u32, + /// direct blocks + direct: [u32; NDIRECT], + /// indirect blocks + indirect: u32, + /// double indirect blocks + db_indirect: u32, +} + +/// file entry (on disk) +#[repr(C, packed)] +struct DiskEntry { + /// inode number + inode_number: u32, + /// file name + name: [u8; MAX_FNAME_LEN + 1], +} + +/// inode for sfs +struct Inode { + /// on-disk inode + disk_inode: *mut DiskInode, + /// inode number + id: u32, + /// true if inode modified + dirty: bool, + /// kill inode if it hits zero + reclaim_count: u32, + /// semaphore for din + mutex: Mutex<()>, +} + +/// filesystem for sfs +struct SimpleFileSystem { + /// on-disk superblock + super_block: SuperBlock, + /// blocks in use are mared 0 + free_map: [u8; 0], + /// true if super/freemap modified + super_dirty: bool, + /// buffer for non-block aligned io + buffer: [u8; 0], + /// semaphore for fs + fs_mutex: Mutex<()>, + /// semaphore for io + io_mutex: Mutex<()>, + /// semaphore for link/unlink and rename + link_mutex: Mutex<()>, + /// inode list + inodes: Vec +} + +/* + * Simple FS (SFS) definitions visible to ucore. This covers the on-disk format + * and is used by tools that work on SFS volumes, such as mksfs. + */ +/// magic number for sfs +const MAGIC: usize = 0x2f8dbe2a; +/// size of block +const BLKSIZE: usize = 4096; +/// number of direct blocks in inode +const NDIRECT: usize = 12; +/// max length of infomation +const MAX_INFO_LEN: usize = 31; +/// max length of filename +const MAX_FNAME_LEN: usize = 255; +/// max file size (128M) +const MAX_FILE_SIZE: usize = 1024 * 1024 * 128; +/// block the superblock lives in +const BLKN_SUPER: usize = 0; +/// location of the root dir inode +const BLKN_ROOT: usize = 1; +/// 1st block of the freemap +const BLKN_FREEMAP: usize = 2; +/// number of bits in a block +const BLKBITS: usize = BLKSIZE * 8; +/// number of entries in a block +const BLK_NENTRY: usize = BLKSIZE / 4; + +/// file types +enum FileType { + Invalid = 0, File = 1, Dir = 2, Link = 3, +} \ No newline at end of file diff --git a/src/tests.rs b/src/tests.rs new file mode 100644 index 0000000..43f948c --- /dev/null +++ b/src/tests.rs @@ -0,0 +1,4 @@ +#[test] +fn test() { + +} \ No newline at end of file