Skip to content

Commit fbd32d4

Browse files
authored
refactor: axio (#99)
* refactor: axio * update axio to 0.3.0-pre.1 * refactor: simplify FileLike with downcast-rs
1 parent 93df083 commit fbd32d4

File tree

19 files changed

+99
-283
lines changed

19 files changed

+99
-283
lines changed

Cargo.lock

Lines changed: 13 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ axtask = { path = "arceos/modules/axtask" }
5050
axbacktrace = "0.1"
5151
axerrno = "0.2"
5252
axfs-ng-vfs = "0.1"
53-
axio = { version = "0.2", git = "https://github.com/arceos-org/axio.git", tag = "dev-v02" }
53+
axio = "0.3.0-pre.1"
5454
axpoll = "0.1"
5555
bitflags = "2.10"
5656
bytemuck = { version = "1.23", features = ["unsound_ptr_pod_impl"] }

api/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ bitmaps = { version = "3.2.1", default-features = false }
3737
bytemuck.workspace = true
3838
cfg-if.workspace = true
3939
chrono = { version = "0.4.41", default-features = false }
40+
downcast-rs = { version = "2.0", default-features = false, features = ["sync"] }
4041
event-listener.workspace = true
4142
flatten_objects = "0.2.4"
4243
gimli = { version = "*", default-features = false, optional = true }

api/src/file/epoll.rs

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use alloc::{
1313
task::Wake,
1414
};
1515
use core::{
16-
any::Any,
1716
hash::{Hash, Hasher},
1817
sync::atomic::{AtomicBool, Ordering},
1918
task::{Context, Waker},
@@ -26,7 +25,7 @@ use hashbrown::HashMap;
2625
use kspin::SpinNoPreempt;
2726
use linux_raw_sys::general::{EPOLLET, EPOLLONESHOT, epoll_event};
2827

29-
use crate::file::{FileLike, Kstat, SealedBuf, SealedBufMut, get_file_like};
28+
use crate::file::{FileLike, get_file_like};
3029

3130
pub struct EpollEvent {
3231
pub events: IoEvents,
@@ -434,25 +433,9 @@ impl Epoll {
434433
}
435434

436435
impl FileLike for Epoll {
437-
fn read(&self, _dst: &mut SealedBufMut) -> AxResult<usize> {
438-
Err(AxError::InvalidInput)
439-
}
440-
441-
fn write(&self, _src: &mut SealedBuf) -> AxResult<usize> {
442-
Err(AxError::InvalidInput)
443-
}
444-
445-
fn stat(&self) -> AxResult<Kstat> {
446-
Ok(Kstat::default())
447-
}
448-
449436
fn path(&self) -> Cow<'_, str> {
450437
"anon_inode:[eventpoll]".into()
451438
}
452-
453-
fn into_any(self: Arc<Self>) -> Arc<dyn Any + Send + Sync> {
454-
self
455-
}
456439
}
457440

458441
impl Pollable for Epoll {

api/src/file/event.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
use alloc::{borrow::Cow, sync::Arc};
22
use core::{
3-
any::Any,
43
sync::atomic::{AtomicBool, AtomicU64, Ordering},
54
task::Context,
65
};
76

87
use axerrno::AxError;
9-
use axio::{Buf, BufMut, Read, Write};
108
use axpoll::{IoEvents, PollSet, Pollable};
119
use axtask::future::{block_on, poll_io};
1210

13-
use crate::file::{FileLike, Kstat, SealedBuf, SealedBufMut};
11+
use crate::file::{FileLike, IoDst, IoSrc};
1412

1513
pub struct EventFd {
1614
count: AtomicU64,
@@ -35,7 +33,7 @@ impl EventFd {
3533
}
3634

3735
impl FileLike for EventFd {
38-
fn read(&self, dst: &mut SealedBufMut) -> axio::Result<usize> {
36+
fn read(&self, dst: &mut IoDst) -> axio::Result<usize> {
3937
if dst.remaining_mut() < size_of::<u64>() {
4038
return Err(AxError::InvalidInput);
4139
}
@@ -62,7 +60,7 @@ impl FileLike for EventFd {
6260
}))
6361
}
6462

65-
fn write(&self, src: &mut SealedBuf) -> axio::Result<usize> {
63+
fn write(&self, src: &mut IoSrc) -> axio::Result<usize> {
6664
if src.remaining() < size_of::<u64>() {
6765
return Err(AxError::InvalidInput);
6866
}
@@ -94,10 +92,6 @@ impl FileLike for EventFd {
9492
}))
9593
}
9694

97-
fn stat(&self) -> axio::Result<Kstat> {
98-
Ok(Kstat::default())
99-
}
100-
10195
fn nonblocking(&self) -> bool {
10296
self.non_blocking.load(Ordering::Acquire)
10397
}
@@ -110,10 +104,6 @@ impl FileLike for EventFd {
110104
fn path(&self) -> Cow<'_, str> {
111105
"anon_inode:[eventfd]".into()
112106
}
113-
114-
fn into_any(self: Arc<Self>) -> Arc<dyn Any + Send + Sync> {
115-
self
116-
}
117107
}
118108

119109
impl Pollable for EventFd {

api/src/file/fs.rs

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use alloc::{borrow::Cow, string::ToString, sync::Arc};
22
use core::{
3-
any::Any,
43
ffi::c_int,
54
hint::likely,
65
sync::atomic::{AtomicBool, Ordering},
@@ -16,7 +15,7 @@ use axtask::future::{block_on, poll_io};
1615
use linux_raw_sys::general::{AT_EMPTY_PATH, AT_FDCWD, AT_SYMLINK_NOFOLLOW};
1716

1817
use super::{FileLike, Kstat, get_file_like};
19-
use crate::file::{SealedBuf, SealedBufMut};
18+
use crate::file::{IoDst, IoSrc};
2019

2120
pub fn with_fs<R>(dirfd: c_int, f: impl FnOnce(&mut FsContext) -> AxResult<R>) -> AxResult<R> {
2221
let mut fs = FS_CONTEXT.lock();
@@ -56,7 +55,7 @@ pub fn resolve_at(dirfd: c_int, path: Option<&str>, flags: u32) -> AxResult<Reso
5655
return Err(AxError::NotFound);
5756
}
5857
let file_like = get_file_like(dirfd)?;
59-
let f = file_like.clone().into_any();
58+
let f = file_like.clone();
6059
Ok(if let Some(file) = f.downcast_ref::<File>() {
6160
ResolveAtResult::File(file.inner().backend()?.location().clone())
6261
} else if let Some(dir) = f.downcast_ref::<Directory>() {
@@ -126,24 +125,24 @@ fn path_for(loc: &Location) -> Cow<'static, str> {
126125
}
127126

128127
impl FileLike for File {
129-
fn read(&self, dst: &mut SealedBufMut) -> AxResult<usize> {
128+
fn read(&self, dst: &mut IoDst) -> AxResult<usize> {
130129
let inner = self.inner();
131130
if likely(self.is_blocking()) {
132131
inner.read(dst)
133132
} else {
134133
block_on(poll_io(self, IoEvents::IN, self.nonblocking(), || {
135-
inner.read(dst)
134+
inner.read(&mut *dst)
136135
}))
137136
}
138137
}
139138

140-
fn write(&self, src: &mut SealedBuf) -> AxResult<usize> {
139+
fn write(&self, src: &mut IoSrc) -> AxResult<usize> {
141140
let inner = self.inner();
142141
if likely(self.is_blocking()) {
143142
inner.write(src)
144143
} else {
145144
block_on(poll_io(self, IoEvents::OUT, self.nonblocking(), || {
146-
inner.write(src)
145+
inner.write(&mut *src)
147146
}))
148147
}
149148
}
@@ -152,10 +151,6 @@ impl FileLike for File {
152151
Ok(metadata_to_kstat(&self.inner().location().metadata()?))
153152
}
154153

155-
fn into_any(self: Arc<Self>) -> Arc<dyn Any + Send + Sync> {
156-
self
157-
}
158-
159154
fn ioctl(&self, cmd: u32, arg: usize) -> AxResult<usize> {
160155
self.inner().backend()?.location().ioctl(cmd, arg)
161156
}
@@ -177,9 +172,7 @@ impl FileLike for File {
177172
where
178173
Self: Sized + 'static,
179174
{
180-
let any = get_file_like(fd)?.into_any();
181-
182-
any.downcast::<Self>().map_err(|any| {
175+
get_file_like(fd)?.downcast_arc().map_err(|any| {
183176
if any.is::<Directory>() {
184177
AxError::IsADirectory
185178
} else {
@@ -219,11 +212,11 @@ impl Directory {
219212
}
220213

221214
impl FileLike for Directory {
222-
fn read(&self, _dst: &mut SealedBufMut) -> AxResult<usize> {
215+
fn read(&self, _dst: &mut IoDst) -> AxResult<usize> {
223216
Err(AxError::BadFileDescriptor)
224217
}
225218

226-
fn write(&self, _src: &mut SealedBuf) -> AxResult<usize> {
219+
fn write(&self, _src: &mut IoSrc) -> AxResult<usize> {
227220
Err(AxError::BadFileDescriptor)
228221
}
229222

@@ -235,14 +228,9 @@ impl FileLike for Directory {
235228
path_for(&self.inner)
236229
}
237230

238-
fn into_any(self: Arc<Self>) -> Arc<dyn Any + Send + Sync> {
239-
self
240-
}
241-
242231
fn from_fd(fd: c_int) -> AxResult<Arc<Self>> {
243232
get_file_like(fd)?
244-
.into_any()
245-
.downcast::<Self>()
233+
.downcast_arc()
246234
.map_err(|_| AxError::NotADirectory)
247235
}
248236
}

0 commit comments

Comments
 (0)