Skip to content

Commit

Permalink
fix: unmap
Browse files Browse the repository at this point in the history
  • Loading branch information
akiradeveloper committed May 18, 2024
1 parent 531578f commit 06f998e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
36 changes: 19 additions & 17 deletions azbuse/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,30 +78,30 @@ nix::ioctl_write_ptr_bad!(azbuse_put_req, AZBUSE_PUT_REQ, AzbuseCompletion);
nix::ioctl_write_int_bad!(azbuse_connect, AZBUSE_CONNECT);

pub struct IOVec {
page_address: usize,
page_offset: usize,
vm_addr: usize,
vm_len: usize,
io_offset: usize,
io_len: usize,
}
impl IOVec {
pub fn start(&self) -> *mut c_void {
unsafe { std::mem::transmute::<usize, &mut c_void>(self.page_address + self.page_offset) }
unsafe { std::mem::transmute::<usize, &mut c_void>(self.vm_addr + self.io_offset) }
}
pub fn len(&self) -> usize {
self.io_len
}
}
impl Drop for IOVec {
fn drop(&mut self) {
let p = unsafe { std::mem::transmute::<usize, *mut c_void>(self.page_address) };
let map_len = self.page_offset + self.io_len;
unsafe { munmap(p, map_len) }.expect("failed to munmap");
let p = unsafe { std::mem::transmute::<usize, *mut c_void>(self.vm_addr) };
unsafe { munmap(p, self.vm_len) }.expect("failed to munmap");
}
}

pub struct Request {
pub cmd_flags: CmdFlags,
pub start: u64,
pub len: u64,
pub io_start: u64,
pub io_len: u64,
pub io_vecs: Vec<IOVec>,
pub request_id: u64,
fd: i32,
Expand Down Expand Up @@ -136,8 +136,8 @@ impl <Engine: StorageEngine> RequestHandler<Engine> {
const BIO_MAX_VECS: usize = 256;

pub struct Config {
pub dev_number: u16,
pub dev_size: u64,
pub minor: u16,
pub device_size: u64,
}

pub async fn run_on(config: Config, engine: impl StorageEngine) {
Expand All @@ -146,7 +146,7 @@ pub async fn run_on(config: Config, engine: impl StorageEngine) {

let fd = open("/dev/azbusectl", OFlag::O_RDWR, Mode::empty()).expect("couldn't open /dev/azbusectl");
let devfd = {
let devpath = format!("/dev/azbuse{}", config.dev_number);
let devpath = format!("/dev/azbuse{}", config.minor);
open(devpath.as_str(), OFlag::empty(), Mode::empty()).expect("couldn't open device")
};

Expand All @@ -157,7 +157,7 @@ pub async fn run_on(config: Config, engine: impl StorageEngine) {
dbg!(&info);
unsafe { azbuse_reset(fd) }.expect("couldn't reset device");
// size must be some multiple of blocksize
info.size = config.dev_size;
info.size = config.device_size;
info.blocksize = 4096;
unsafe { azbuse_set_status(fd, &info) }.expect("couldn't set info");

Expand Down Expand Up @@ -229,20 +229,22 @@ pub async fn run_on(config: Config, engine: impl StorageEngine) {
let mut io_vecs = vec![];
for i in 0..n {
let io_vec = &xfr_io_vec[i];
let map_len = (io_vec.n_pages << PAGE_SHIFT) as usize;
io_vecs.push(IOVec {
page_address: cur,
page_offset: io_vec.offset as usize,
vm_addr: cur,
vm_len: map_len,
io_offset: io_vec.offset as usize,
io_len: io_vec.len as usize,
});
cur += (io_vec.n_pages << PAGE_SHIFT) as usize;
cur += map_len;
}

let cmd_flags = CmdFlags::from_bits(xfr.cmd_flags).unwrap();
let req = Request {
cmd_flags,
io_vecs,
start: xfr.offset,
len: xfr.len,
io_start: xfr.offset,
io_len: xfr.len,
request_id: xfr.id,
fd,
};
Expand Down
8 changes: 4 additions & 4 deletions tests/ramdisk/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ async fn main() {
let dev_number = opts.dev_number;
let sz = 1500 << 20; // 1500MB
let config = azbuse::Config {
dev_number,
dev_size: sz as u64,
minor,
device_size: sz as u64,
};
let engine = Engine {
mem: MemBuffer::new(sz),
Expand All @@ -33,12 +33,12 @@ impl StorageEngine for Engine {
match req_op {
CmdFlags::OP_WRITE => {
let m = &mut self.mem;
m.write(req.start as usize, &req.io_vecs);
m.write(req.io_start as usize, &req.io_vecs);
req.endio(0);
}
CmdFlags::OP_READ => {
let m = &self.mem;
m.read(req.start as usize, &req.io_vecs);
m.read(req.io_start as usize, &req.io_vecs);
req.endio(0);
}
_ => {
Expand Down

0 comments on commit 06f998e

Please sign in to comment.