diff options
-rw-r--r-- | Cargo.lock | 16 | ||||
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | ayafs-core/Cargo.toml (renamed from ayafs/Cargo.toml) | 11 | ||||
-rw-r--r-- | ayafs-core/src/bin/main.rs | 44 | ||||
-rw-r--r-- | ayafs-core/src/block_device/disk.rs (renamed from ayafs/src/block_device/disk.rs) | 5 | ||||
-rw-r--r-- | ayafs-core/src/block_device/memory_disk.rs (renamed from ayafs/src/block_device/memory_disk.rs) | 0 | ||||
-rw-r--r-- | ayafs-core/src/block_device/mod.rs (renamed from ayafs/src/block_device/mod.rs) | 2 | ||||
-rw-r--r-- | ayafs-core/src/disk/allocation.rs (renamed from ayafs/src/disk/allocation.rs) | 0 | ||||
-rw-r--r-- | ayafs-core/src/disk/bitmap.rs (renamed from ayafs/src/disk/bitmap.rs) | 0 | ||||
-rw-r--r-- | ayafs-core/src/disk/block.rs (renamed from ayafs/src/disk/block.rs) | 26 | ||||
-rw-r--r-- | ayafs-core/src/disk/inode.rs (renamed from ayafs/src/disk/inode.rs) | 4 | ||||
-rw-r--r-- | ayafs-core/src/disk/mod.rs (renamed from ayafs/src/disk/mod.rs) | 0 | ||||
-rw-r--r-- | ayafs-core/src/filesystem/mod.rs (renamed from ayafs/src/filesystem/mod.rs) | 0 | ||||
-rw-r--r-- | ayafs-core/src/filesystem/trait_impl.rs (renamed from ayafs/src/filesystem/trait_impl.rs) | 97 | ||||
-rw-r--r-- | ayafs-core/src/lib.rs (renamed from ayafs/src/main.rs) | 79 | ||||
-rw-r--r-- | ayafs-core/src/memory/cached_block.rs (renamed from ayafs/src/memory/cached_block.rs) | 0 | ||||
-rw-r--r-- | ayafs-core/src/memory/cached_inode.rs (renamed from ayafs/src/memory/cached_inode.rs) | 0 | ||||
-rw-r--r-- | ayafs-core/src/memory/dir_entry.rs (renamed from ayafs/src/memory/dir_entry.rs) | 16 | ||||
-rw-r--r-- | ayafs-core/src/memory/file_handle.rs (renamed from ayafs/src/memory/file_handle.rs) | 0 | ||||
-rw-r--r-- | ayafs-core/src/memory/mod.rs (renamed from ayafs/src/memory/mod.rs) | 0 | ||||
-rw-r--r-- | ayafs-core/src/tests/bitmap.rs (renamed from ayafs/src/tests/bitmap.rs) | 0 | ||||
-rw-r--r-- | ayafs-core/src/tests/block_cache.rs (renamed from ayafs/src/tests/block_cache.rs) | 0 | ||||
-rw-r--r-- | ayafs-core/src/tests/common/mod.rs (renamed from ayafs/src/tests/common/mod.rs) | 0 | ||||
-rw-r--r-- | ayafs-core/src/tests/mod.rs (renamed from ayafs/src/tests/mod.rs) | 0 | ||||
-rw-r--r-- | ayafs-core/src/utils/constants.rs (renamed from ayafs/src/utils/constants.rs) | 0 | ||||
-rw-r--r-- | ayafs-core/src/utils/mod.rs (renamed from ayafs/src/utils/mod.rs) | 0 | ||||
-rw-r--r-- | ayafs-core/src/utils/permissions.rs (renamed from ayafs/src/utils/permissions.rs) | 0 | ||||
-rw-r--r-- | mkfs.aya/Cargo.toml | 5 | ||||
-rw-r--r-- | mkfs.aya/src/block_device.rs | 5 | ||||
-rw-r--r-- | mkfs.aya/src/ioctl.rs | 6 | ||||
-rw-r--r-- | mkfs.aya/src/main.rs | 43 |
31 files changed, 257 insertions, 104 deletions
@@ -84,7 +84,7 @@ dependencies = [ ] [[package]] -name = "ayafs" +name = "ayafs-core" version = "0.1.0" dependencies = [ "and_then_some", @@ -293,11 +293,25 @@ checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167" name = "mkayafs" version = "0.1.0" dependencies = [ + "ayafs-core", "clap", + "libc", + "nix", "users", ] [[package]] +name = "nix" +version = "0.27.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2eb04e9c688eff1c89d72b407f168cf79bb9e867a9d3323ed6c01519eb9cc053" +dependencies = [ + "bitflags", + "cfg-if", + "libc", +] + +[[package]] name = "once_cell" version = "1.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1,6 +1,6 @@ [workspace] members = [ - "ayafs", + "ayafs-core", "mkfs.aya", ] diff --git a/ayafs/Cargo.toml b/ayafs-core/Cargo.toml index 43585e0..8e12bf5 100644 --- a/ayafs/Cargo.toml +++ b/ayafs-core/Cargo.toml @@ -1,10 +1,17 @@ [package] -name = "ayafs" +name = "ayafs-core" version = "0.1.0" edition = "2021" - # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[lib] +name = "aya" +path = "src/lib.rs" + +[[bin]] +name = "ayafs_cli" +path = "src/bin/main.rs" + [dependencies] and_then_some = "1.0.0" bitflags = "2.4.1" diff --git a/ayafs-core/src/bin/main.rs b/ayafs-core/src/bin/main.rs new file mode 100644 index 0000000..25504b9 --- /dev/null +++ b/ayafs-core/src/bin/main.rs @@ -0,0 +1,44 @@ +use std::path::PathBuf; +use std::sync::Arc; +use clap::Parser; +use fuser::MountOption; +use log::LevelFilter; +use aya::AyaFS; +use aya::block_device::disk::Disk; + +#[derive(Parser, Debug)] +#[command(author, version, about)] +struct Args { + mount_point: Option<String>, + #[arg(short, action = clap::ArgAction::Count)] + verbosity: u8, + #[arg(long)] + auto_unmount: bool, + #[arg(long)] + allow_root: bool, +} + +fn main() { + let args = Args::parse(); + let mount_point = args.mount_point.unwrap(); + let verbosity = args.verbosity; + let log_level = match verbosity { + 0 => LevelFilter::Error, + 1 => LevelFilter::Warn, + 2 => LevelFilter::Info, + 3 => LevelFilter::Debug, + _ => LevelFilter::Trace, + }; + env_logger::builder().filter_level(log_level).init(); + let options = vec![ + // MountOption::RO, + MountOption::FSName("hello".to_string()), + MountOption::AutoUnmount, + MountOption::AllowRoot, + ]; + let disk = Arc::new(Disk::new(PathBuf::from("/dev/nvme0n1p4"))); + // let disk = Arc::new(MemoryDisk::new(16384)); + let filesystem = AyaFS::new(disk, 7864320); + + fuser::mount2(filesystem, mount_point, &options).unwrap(); +}
\ No newline at end of file diff --git a/ayafs/src/block_device/disk.rs b/ayafs-core/src/block_device/disk.rs index 3f0b018..d2beee9 100644 --- a/ayafs/src/block_device/disk.rs +++ b/ayafs-core/src/block_device/disk.rs @@ -1,8 +1,8 @@ +use crate::block_device::{BlockDevice, BLOCK_SIZE}; use std::cell::RefCell; use std::fs::File; use std::io::{Read, Seek, SeekFrom, Write}; use std::path::{Path, PathBuf}; -use crate::block_device::{BLOCK_SIZE, BlockDevice}; pub struct Disk { disk_path: PathBuf, @@ -11,6 +11,7 @@ pub struct Disk { impl Disk { pub fn new(disk_path: PathBuf) -> Self { + let device = File::options() .read(true) .write(true) @@ -44,4 +45,4 @@ impl BlockDevice for Disk { .write_all(buffer) .expect("Unable to write 4096 bytes!"); } -}
\ No newline at end of file +} diff --git a/ayafs/src/block_device/memory_disk.rs b/ayafs-core/src/block_device/memory_disk.rs index 0639d3e..0639d3e 100644 --- a/ayafs/src/block_device/memory_disk.rs +++ b/ayafs-core/src/block_device/memory_disk.rs diff --git a/ayafs/src/block_device/mod.rs b/ayafs-core/src/block_device/mod.rs index a1e6544..53394a9 100644 --- a/ayafs/src/block_device/mod.rs +++ b/ayafs-core/src/block_device/mod.rs @@ -1,7 +1,7 @@ +pub mod disk; /// Abstracts for block devices. /// Currently only a mock memory disk. pub mod memory_disk; -pub mod disk; pub const BLOCK_SIZE: usize = 4096; pub trait BlockDevice { diff --git a/ayafs/src/disk/allocation.rs b/ayafs-core/src/disk/allocation.rs index a187fad..a187fad 100644 --- a/ayafs/src/disk/allocation.rs +++ b/ayafs-core/src/disk/allocation.rs diff --git a/ayafs/src/disk/bitmap.rs b/ayafs-core/src/disk/bitmap.rs index b68c341..b68c341 100644 --- a/ayafs/src/disk/bitmap.rs +++ b/ayafs-core/src/disk/bitmap.rs diff --git a/ayafs/src/disk/block.rs b/ayafs-core/src/disk/block.rs index 73819e2..76769b9 100644 --- a/ayafs/src/disk/block.rs +++ b/ayafs-core/src/disk/block.rs @@ -5,6 +5,32 @@ use std::os::unix::ffi::OsStrExt; pub trait Block: Default + Clone {} #[repr(C)] +pub struct SuperBlock { + pub(crate) data_bitmap_block_number: u64, + pub(crate) inode_bitmap_block_number: u64, + pub(crate) inode_block_number: u64, + pub(crate) data_block_number: u64, + padding: [u8; 4064], +} + +impl SuperBlock { + pub(crate) fn new( + data_bitmap_block_number: usize, + inode_bitmap_block_number: usize, + inode_block_number: usize, + data_block_number: usize, + ) -> Self { + Self { + data_bitmap_block_number: data_bitmap_block_number as u64, + inode_bitmap_block_number: inode_bitmap_block_number as u64, + inode_block_number: inode_block_number as u64, + data_block_number: data_block_number as u64, + padding: [0; 4064], + } + } +} + +#[repr(C)] #[derive(Clone)] pub struct DataBlock(pub(crate) [u8; 4096]); diff --git a/ayafs/src/disk/inode.rs b/ayafs-core/src/disk/inode.rs index 0c801ad..d94b795 100644 --- a/ayafs/src/disk/inode.rs +++ b/ayafs-core/src/disk/inode.rs @@ -1,7 +1,7 @@ +use crate::block_device::BLOCK_SIZE; use crate::utils; use bitflags::bitflags; use fuser::FileType; -use crate::block_device::BLOCK_SIZE; pub const DIRECT_NUMBER: usize = 15; @@ -309,4 +309,4 @@ impl Inode { } } pub const INODE_SIZE: usize = std::mem::size_of::<Inode>(); -pub const ENTRY_PER_BLOCK: usize = BLOCK_SIZE / 4;
\ No newline at end of file +pub const ENTRY_PER_BLOCK: usize = BLOCK_SIZE / 4; diff --git a/ayafs/src/disk/mod.rs b/ayafs-core/src/disk/mod.rs index 878e832..878e832 100644 --- a/ayafs/src/disk/mod.rs +++ b/ayafs-core/src/disk/mod.rs diff --git a/ayafs/src/filesystem/mod.rs b/ayafs-core/src/filesystem/mod.rs index 1eaa8e8..1eaa8e8 100644 --- a/ayafs/src/filesystem/mod.rs +++ b/ayafs-core/src/filesystem/mod.rs diff --git a/ayafs/src/filesystem/trait_impl.rs b/ayafs-core/src/filesystem/trait_impl.rs index 1c40b92..b551cf7 100644 --- a/ayafs/src/filesystem/trait_impl.rs +++ b/ayafs-core/src/filesystem/trait_impl.rs @@ -7,9 +7,13 @@ use crate::{AyaFS, TTL}; use fuser::TimeOrNow::{Now, SpecificTime}; use fuser::{ FileType, Filesystem, KernelConfig, ReplyAttr, ReplyData, ReplyDirectory, ReplyEmpty, - ReplyEntry, ReplyOpen, ReplyWrite, Request, TimeOrNow, + ReplyEntry, ReplyOpen, ReplyStatfs, ReplyWrite, ReplyXattr, Request, TimeOrNow, +}; +use libc::{ + c_int, EACCES, EBADF, EEXIST, EINVAL, EIO, EISDIR, ENAMETOOLONG, ENOENT, ENOSPC, ENOTDIR, + ENOTEMPTY, EPERM, IPOPT_OFFSET, O_ACCMODE, O_RDONLY, O_RDWR, O_WRONLY, RENAME_EXCHANGE, + RENAME_NOREPLACE, R_OK, S_ISGID, S_ISUID, S_IXGRP, S_IXOTH, S_IXUSR, W_OK, }; -use libc::{c_int, EACCES, EBADF, EEXIST, EINVAL, EIO, EISDIR, ENAMETOOLONG, ENOENT, ENOSPC, ENOTDIR, ENOTEMPTY, EPERM, IPOPT_OFFSET, O_ACCMODE, O_RDONLY, O_RDWR, O_WRONLY, R_OK, S_ISGID, S_ISUID, S_IXGRP, S_IXOTH, S_IXUSR, W_OK, RENAME_NOREPLACE, RENAME_EXCHANGE}; use log::debug; use std::ffi::OsStr; use std::os::unix::ffi::OsStrExt; @@ -823,53 +827,84 @@ impl Filesystem for AyaFS { match self.lookup_name(new_parent, &new_parent_inode, new_name) { Ok((new_inode_index, new_entry_index, new_inode)) => { // 新文件存在 - if flags & RENAME_NOREPLACE != 0 { // 指定 noreplace 之后不允许覆盖文件 + if flags & RENAME_NOREPLACE != 0 { + // 指定 noreplace 之后不允许覆盖文件 reply.error(EEXIST); return; } - if flags & RENAME_EXCHANGE != 0 { // 交换两个文件 + if flags & RENAME_EXCHANGE != 0 { + // 交换两个文件 if let Err(err_code) = self.exchange_direntry( parent, &mut parent_inode, name, new_parent, &mut new_parent_inode, - new_name + new_name, ) { reply.error(err_code); return; } - } else { // 用新文件替换旧文件 - let dir_entry = self.get_direntry_by_name(parent, &parent_inode, name) + } else { + // 用新文件替换旧文件 + let dir_entry = self + .get_direntry_by_name(parent, &parent_inode, name) .unwrap(); - if let Err(err_code) = self.remove_direntry(parent, &mut parent_inode, name, entry_index) { + if let Err(err_code) = self.remove_direntry( + parent, + &mut parent_inode, + name, + entry_index, + ) { reply.error(err_code); return; } - if let Err(err_code) = self.remove_direntry(new_parent, &mut new_parent_inode, new_name, new_entry_index) { + if let Err(err_code) = self.remove_direntry( + new_parent, + &mut new_parent_inode, + new_name, + new_entry_index, + ) { reply.error(err_code); return; } - if let Err(err_code) = self.add_direntry_2(new_parent, &mut new_parent_inode, new_name, dir_entry) { + if let Err(err_code) = self.add_direntry_2( + new_parent, + &mut new_parent_inode, + new_name, + dir_entry, + ) { reply.error(err_code); return; } } reply.ok(); - }, - Err(ENOENT) => { // 新文件不存在, 删除旧的创建新的 - let dir_entry = self.get_direntry_by_name(parent, &parent_inode, name) + } + Err(ENOENT) => { + // 新文件不存在, 删除旧的创建新的 + let dir_entry = self + .get_direntry_by_name(parent, &parent_inode, name) .unwrap(); - if let Err(err_code) = self.remove_direntry(parent, &mut parent_inode, name, entry_index) { + if let Err(err_code) = self.remove_direntry( + parent, + &mut parent_inode, + name, + entry_index, + ) { reply.error(err_code); return; } - if let Err(err_code) = self.add_direntry_2(new_parent, &mut new_parent_inode, new_name, dir_entry) { + if let Err(err_code) = self.add_direntry_2( + new_parent, + &mut new_parent_inode, + new_name, + dir_entry, + ) { reply.error(err_code); return; } reply.ok(); - }, + } Err(err_code) => { // 其他 Err code reply.error(err_code); @@ -879,7 +914,7 @@ impl Filesystem for AyaFS { } else { reply.error(ENOENT); } - }, + } Err(err_code) => { reply.error(err_code); return; @@ -1092,6 +1127,30 @@ impl Filesystem for AyaFS { } } + // fn getxattr( + // &mut self, + // _req: &Request<'_>, + // ino: u64, + // name: &OsStr, + // size: u32, + // reply: ReplyXattr, + // ) { + // todo!() + // } + // + // fn setxattr( + // &mut self, + // _req: &Request<'_>, + // ino: u64, + // name: &OsStr, + // _value: &[u8], + // flags: i32, + // position: u32, + // reply: ReplyEmpty, + // ) { + // todo!() + // } + fn release( &mut self, _req: &Request<'_>, @@ -1225,6 +1284,10 @@ impl Filesystem for AyaFS { } } + // fn statfs(&mut self, _req: &Request<'_>, _ino: u64, reply: ReplyStatfs) { + // todo!() + // } + fn access(&mut self, req: &Request<'_>, ino: u64, mask: i32, reply: ReplyEmpty) { // mask: // - 要么是 libc::F_OK (aka 0), 检查文件是否存在 diff --git a/ayafs/src/main.rs b/ayafs-core/src/lib.rs index db4a017..5ec118a 100644 --- a/ayafs/src/main.rs +++ b/ayafs-core/src/lib.rs @@ -1,43 +1,28 @@ -mod block_device; -mod disk; -mod filesystem; -mod memory; -mod tests; -mod utils; - -use clap::Parser; -use fuser::MountOption; +pub mod block_device; +pub mod disk; +pub mod filesystem; +pub mod memory; +pub mod tests; +pub mod utils; + + use indexmap::IndexMap; -use log::{debug, LevelFilter}; +use log::debug; use lru::LruCache; use std::collections::HashMap; use std::ffi::OsString; use std::num::NonZeroUsize; -use std::path::PathBuf; use std::sync::atomic::AtomicU64; use std::sync::Arc; use std::time::Duration; -use crate::disk::block::{DirectoryEntry, InodeBlock}; +use crate::disk::block::{DirectoryEntry, InodeBlock, SuperBlock}; use crate::memory::cached_block::BlockCache; -use block_device::{memory_disk::MemoryDisk, BlockDevice, BLOCK_SIZE}; +use block_device::{BlockDevice, BLOCK_SIZE}; use disk::bitmap::Bitmap; use disk::block::DataBlock; use disk::inode::INODE_SIZE; use users::{get_current_gid, get_current_uid}; -use crate::block_device::disk::Disk; - -#[derive(Parser, Debug)] -#[command(author, version, about)] -struct Args { - mount_point: Option<String>, - #[arg(short, action = clap::ArgAction::Count)] - verbosity: u8, - #[arg(long)] - auto_unmount: bool, - #[arg(long)] - allow_root: bool, -} const TTL: Duration = Duration::new(0, 0); const INODE_PER_BLOCK: usize = BLOCK_SIZE / INODE_SIZE; @@ -63,7 +48,7 @@ const INODE_PER_BLOCK: usize = BLOCK_SIZE / INODE_SIZE; /// With each block 4KiB, each Inode entry 128B #[repr(C)] -struct AyaFS { +pub struct AyaFS { device: Arc<dyn BlockDevice>, data_bitmap: Bitmap, inode_bitmap: Bitmap, @@ -79,10 +64,12 @@ struct AyaFS { cached_inodes: BlockCache<InodeBlock>, cached_blocks: BlockCache<DataBlock>, + + super_block: SuperBlock, } impl AyaFS { - fn new(device: Arc<dyn BlockDevice>, total_block_number: usize) -> Self { + pub fn new(device: Arc<dyn BlockDevice>, total_block_number: usize) -> Self { let max_inode_number: usize = 16384; // TODO: remove hard-coded magic number let inode_block_number = max_inode_number / INODE_PER_BLOCK; // == 128 let inode_bitmap_block_number = (inode_block_number + BLOCK_SIZE - 1) / BLOCK_SIZE; @@ -109,6 +96,13 @@ impl AyaFS { + data_block_number ); + let super_block = SuperBlock::new( + data_bitmap_block_number, + inode_bitmap_block_number, + inode_block_number, + data_block_number, + ); + let mut data_bitmap = Bitmap::new(1, data_bitmap_block_number, device.clone()); let _ = data_bitmap.allocate().unwrap(); // data block 0 is not usable let mut inode_bitmap = Bitmap::new( @@ -135,6 +129,8 @@ impl AyaFS { cached_inodes: BlockCache::new(device.clone(), 1024), cached_blocks: BlockCache::new(device.clone(), 8192), + + super_block, }; fs.create_directory(0o755, get_current_uid(), get_current_gid(), 0, None); @@ -142,30 +138,3 @@ impl AyaFS { fs } } - -fn main() { - let args = Args::parse(); - let mount_point = args.mount_point.unwrap(); - let verbosity = args.verbosity; - let log_level = match verbosity { - 0 => LevelFilter::Error, - 1 => LevelFilter::Warn, - 2 => LevelFilter::Info, - 3 => LevelFilter::Debug, - _ => LevelFilter::Trace, - }; - env_logger::builder() - .filter_level(log_level) - .init(); - let options = vec![ - // MountOption::RO, - MountOption::FSName("hello".to_string()), - MountOption::AutoUnmount, - MountOption::AllowRoot, - ]; - let disk = Arc::new(Disk::new(PathBuf::from("/dev/nvme0n1p4"))); - // let disk = Arc::new(MemoryDisk::new(16384)); - let filesystem = AyaFS::new(disk, 7864320); - - fuser::mount2(filesystem, mount_point, &options).unwrap(); -} diff --git a/ayafs/src/memory/cached_block.rs b/ayafs-core/src/memory/cached_block.rs index c3d0338..c3d0338 100644 --- a/ayafs/src/memory/cached_block.rs +++ b/ayafs-core/src/memory/cached_block.rs diff --git a/ayafs/src/memory/cached_inode.rs b/ayafs-core/src/memory/cached_inode.rs index 2f26dde..2f26dde 100644 --- a/ayafs/src/memory/cached_inode.rs +++ b/ayafs-core/src/memory/cached_inode.rs diff --git a/ayafs/src/memory/dir_entry.rs b/ayafs-core/src/memory/dir_entry.rs index c013e1e..55c67bd 100644 --- a/ayafs/src/memory/dir_entry.rs +++ b/ayafs-core/src/memory/dir_entry.rs @@ -1,12 +1,12 @@ use crate::disk::block::{DirectoryBlock, DirectoryEntry}; use crate::disk::inode::Inode; use crate::AyaFS; +use indexmap::map::Entry::Occupied; use indexmap::IndexMap; use libc::{c_int, ENOENT, ENOSPC}; use log::debug; use std::ffi::{OsStr, OsString}; use std::os::unix::ffi::OsStrExt; -use indexmap::map::Entry::Occupied; impl AyaFS { pub(crate) fn init_direntry_map(&mut self, index: usize) { @@ -112,13 +112,15 @@ impl AyaFS { self.load_direntry_map(parent_index, parent_inode)?; self.load_direntry_map(new_parent_index, new_parent_inode)?; - let dir_entry = self.dir_entry_map + let dir_entry = self + .dir_entry_map .get(&parent_index) .ok_or(ENOENT)? .get(name.as_ref()) .cloned() .ok_or(ENOENT)?; - let new_dir_entry = self.dir_entry_map + let new_dir_entry = self + .dir_entry_map .get(&new_parent_index) .ok_or(ENOENT)? .get(new_name.as_ref()) @@ -238,12 +240,10 @@ impl AyaFS { if let Some(dir_entry_map) = self.dir_entry_map.get(&parent_index) { debug!( " get_direntry(ino: {}, name: {:?}) using hashmap", - parent_index, name.as_ref() + parent_index, + name.as_ref() ); - dir_entry_map - .get(name.as_ref()) - .cloned() - .ok_or(ENOENT) + dir_entry_map.get(name.as_ref()).cloned().ok_or(ENOENT) } else { Err(ENOENT) } diff --git a/ayafs/src/memory/file_handle.rs b/ayafs-core/src/memory/file_handle.rs index c821619..c821619 100644 --- a/ayafs/src/memory/file_handle.rs +++ b/ayafs-core/src/memory/file_handle.rs diff --git a/ayafs/src/memory/mod.rs b/ayafs-core/src/memory/mod.rs index d1f1ab8..d1f1ab8 100644 --- a/ayafs/src/memory/mod.rs +++ b/ayafs-core/src/memory/mod.rs diff --git a/ayafs/src/tests/bitmap.rs b/ayafs-core/src/tests/bitmap.rs index 1a43f09..1a43f09 100644 --- a/ayafs/src/tests/bitmap.rs +++ b/ayafs-core/src/tests/bitmap.rs diff --git a/ayafs/src/tests/block_cache.rs b/ayafs-core/src/tests/block_cache.rs index 52117b9..52117b9 100644 --- a/ayafs/src/tests/block_cache.rs +++ b/ayafs-core/src/tests/block_cache.rs diff --git a/ayafs/src/tests/common/mod.rs b/ayafs-core/src/tests/common/mod.rs index 3abfcb4..3abfcb4 100644 --- a/ayafs/src/tests/common/mod.rs +++ b/ayafs-core/src/tests/common/mod.rs diff --git a/ayafs/src/tests/mod.rs b/ayafs-core/src/tests/mod.rs index df442c1..df442c1 100644 --- a/ayafs/src/tests/mod.rs +++ b/ayafs-core/src/tests/mod.rs diff --git a/ayafs/src/utils/constants.rs b/ayafs-core/src/utils/constants.rs index 8b13789..8b13789 100644 --- a/ayafs/src/utils/constants.rs +++ b/ayafs-core/src/utils/constants.rs diff --git a/ayafs/src/utils/mod.rs b/ayafs-core/src/utils/mod.rs index 468ebdb..468ebdb 100644 --- a/ayafs/src/utils/mod.rs +++ b/ayafs-core/src/utils/mod.rs diff --git a/ayafs/src/utils/permissions.rs b/ayafs-core/src/utils/permissions.rs index 6773511..6773511 100644 --- a/ayafs/src/utils/permissions.rs +++ b/ayafs-core/src/utils/permissions.rs diff --git a/mkfs.aya/Cargo.toml b/mkfs.aya/Cargo.toml index c75e3cb..930feb7 100644 --- a/mkfs.aya/Cargo.toml +++ b/mkfs.aya/Cargo.toml @@ -4,5 +4,8 @@ version = "0.1.0" edition = "2021" [dependencies] +ayafs-core = { path = "../ayafs-core" } users = "0.11.0" -clap = { version = "4.4.10", features = ["derive"] }
\ No newline at end of file +clap = { version = "4.4.10", features = ["derive"] } +nix = { version = "0.27.1", features = ["ioctl"] } +libc = "0.2.150"
\ No newline at end of file diff --git a/mkfs.aya/src/block_device.rs b/mkfs.aya/src/block_device.rs deleted file mode 100644 index 295b357..0000000 --- a/mkfs.aya/src/block_device.rs +++ /dev/null @@ -1,5 +0,0 @@ -pub(crate) const BLOCK_SIZE: usize = 4096; -pub(crate) trait BlockDevice { - fn read(&self, block_id: usize, buffer: &mut [u8]); - fn write(&self, block_id: usize, buffer: &[u8]); -}
\ No newline at end of file diff --git a/mkfs.aya/src/ioctl.rs b/mkfs.aya/src/ioctl.rs new file mode 100644 index 0000000..7327f4b --- /dev/null +++ b/mkfs.aya/src/ioctl.rs @@ -0,0 +1,6 @@ +use nix::ioctl_read; + +const BLKGETSIZE64_CODE: u8 = 0x12; +const BLKGETSIZE64_SEQ: u8 = 114; + +ioctl_read!(ioctl_blkgetsize64, BLKGETSIZE64_CODE, BLKGETSIZE64_SEQ, u64);
\ No newline at end of file diff --git a/mkfs.aya/src/main.rs b/mkfs.aya/src/main.rs index 57ca3fc..3a96d0c 100644 --- a/mkfs.aya/src/main.rs +++ b/mkfs.aya/src/main.rs @@ -1,21 +1,46 @@ -mod block_device; +mod ioctl; -use std::fs::File; -use std::path::PathBuf; use clap::Parser; -use users::{get_current_gid, get_current_uid}; +use std::fs::File; +use std::os::fd::AsRawFd; +use std::path::{Path, PathBuf}; +use std::sync::Arc; +use aya::AyaFS; +use aya::block_device::{BLOCK_SIZE, BlockDevice}; +use aya::block_device::disk::Disk; +use crate::ioctl::ioctl_blkgetsize64; #[derive(Parser, Debug)] #[command(author, version, about)] struct Args { block_device: Option<PathBuf>, - start_point: Option<usize>, - length: Option<usize>, } +fn get_device_size<T: AsRef<Path>>(path: T) -> u64 { + let device = File::options() + .write(true) + .open(path.as_ref()) + .unwrap(); + let device_raw_fd = device.as_raw_fd(); + let mut device_size = 0u64; + let device_size_ptr = &mut device_size as *mut u64; + unsafe { + ioctl_blkgetsize64(device_raw_fd, device_size_ptr).unwrap(); + } + device_size +} + + + fn main() { let args = Args::parse(); let device_path = args.block_device.unwrap(); - println!("{:?}", device_path.as_path()); - let mut device = File::open(device_path.as_path()).unwrap(); -}
\ No newline at end of file + let device_size = get_device_size(device_path.as_path()); + let device = File::options() + .read(true) + .write(true) + .open(device_path.as_path()) + .unwrap(); + let disk =Arc::new( Disk::new(device_path)); + let fs = AyaFS::new(disk, device_size as usize / BLOCK_SIZE); +} |