Skip to content

Commit 7984272

Browse files
Merge pull request #271 from arihant2math/ext4plus-bump
Use as_any to downcast inodes for updating in ext4
2 parents d0e77a8 + 7edfe62 commit 7984272

17 files changed

Lines changed: 162 additions & 16 deletions

File tree

Cargo.lock

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

libkernel/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ edition = "2024"
66
[dependencies]
77
async-trait = { workspace = true }
88
bitflags = { workspace = true }
9-
ext4plus = "0.1.0-alpha.5"
9+
ext4plus = "0.1.0-alpha.7"
1010
intrusive-collections = { version = "0.10.0", default-features = false }
1111
log = { workspace = true }
1212
object = { version = "0.38.0", default-features = false, features = ["core", "elf", "read_core"] }

libkernel/src/fs/filesystems/ext4/mod.rs

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use alloc::{
2626
sync::{Arc, Weak},
2727
};
2828
use async_trait::async_trait;
29+
use core::any::Any;
2930
use core::error::Error;
3031
use core::marker::PhantomData;
3132
use core::num::NonZeroU32;
@@ -157,7 +158,7 @@ impl InodeInner {
157158
InodeInner::Regular(File::open_inode(fs, inode).unwrap())
158159
}
159160
ext4plus::FileType::Directory => {
160-
InodeInner::Directory(Dir::open_inode(fs, inode).await.unwrap())
161+
InodeInner::Directory(Dir::open_inode(fs, inode).unwrap())
161162
}
162163
_ => InodeInner::Other(inode),
163164
}
@@ -378,11 +379,13 @@ where
378379
if inode.id().fs_id() != fs.id() {
379380
return Err(KernelError::Fs(FsError::CrossDevice));
380381
}
381-
let mut other_inode = ExtInode::read(
382-
&fs.inner,
383-
(inode.id().inode_id() as u32).try_into().unwrap(),
384-
)
385-
.await?;
382+
let mut other_inode = inode
383+
.as_any()
384+
.downcast_ref::<Ext4Inode<CPU>>()
385+
.ok_or(FsError::CrossDevice)?
386+
.inner
387+
.lock()
388+
.await;
386389
let file_type = other_inode.file_type();
387390
inner_dir
388391
.link(
@@ -443,15 +446,20 @@ where
443446
return Ok(());
444447
}
445448

446-
let old_parent_id = old_parent.id();
449+
if old_parent.id().fs_id() != self.id().fs_id() {
450+
return Err(KernelError::Fs(FsError::CrossDevice));
451+
}
447452
let fs = self.fs_ref.upgrade().unwrap();
448453

449-
let old_parent_inode = ExtInode::read(
450-
&fs.inner,
451-
(old_parent_id.inode_id() as u32).try_into().unwrap(),
452-
)
453-
.await?;
454-
let old_parent_dir = Dir::open_inode(&fs.inner, old_parent_inode).await?;
454+
let old_parent_inode = old_parent
455+
.as_any()
456+
.downcast_ref::<Ext4Inode<CPU>>()
457+
.ok_or(FsError::CrossDevice)?
458+
.inner
459+
.lock()
460+
.await
461+
.clone();
462+
let old_parent_dir = Dir::open_inode(&fs.inner, old_parent_inode)?;
455463

456464
let inner = self.inner.lock().await;
457465
let inner_dir = match &*inner {
@@ -532,6 +540,13 @@ where
532540
child_inode,
533541
)
534542
.await?;
543+
*old_parent
544+
.as_any()
545+
.downcast_ref::<Ext4Inode<CPU>>()
546+
.ok_or(FsError::CrossDevice)?
547+
.inner
548+
.lock()
549+
.await = InodeInner::Directory(old_parent_dir);
535550
Ok(())
536551
}
537552

@@ -564,6 +579,10 @@ where
564579
inner.write(&fs.inner).await?;
565580
Ok(())
566581
}
582+
583+
fn as_any(&self) -> &dyn Any {
584+
self
585+
}
567586
}
568587

569588
/// An EXT4 filesystem instance.

libkernel/src/fs/filesystems/fat32/dir.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use crate::{
1111
};
1212
use alloc::{boxed::Box, string::String, sync::Arc, vec::Vec};
1313
use async_trait::async_trait;
14+
use core::any::Any;
1415
use log::warn;
1516

1617
bitflags::bitflags! {
@@ -399,6 +400,10 @@ impl<T: Fat32Operations> Inode for Fat32DirNode<T> {
399400
async fn getattr(&self) -> Result<FileAttr> {
400401
Ok(self.attr.clone())
401402
}
403+
404+
fn as_any(&self) -> &dyn Any {
405+
self
406+
}
402407
}
403408

404409
#[cfg(test)]

libkernel/src/fs/filesystems/fat32/file.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::{
55
use alloc::boxed::Box;
66
use alloc::sync::Arc;
77
use async_trait::async_trait;
8+
use core::any::Any;
89

910
use super::{Cluster, Fat32Operations, reader::Fat32Reader};
1011

@@ -39,6 +40,10 @@ impl<T: Fat32Operations> Inode for Fat32FileNode<T> {
3940
async fn getattr(&self) -> Result<FileAttr> {
4041
Ok(self.attr.clone())
4142
}
43+
44+
fn as_any(&self) -> &dyn Any {
45+
self
46+
}
4247
}
4348

4449
#[cfg(test)]

libkernel/src/fs/filesystems/tmpfs.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use alloc::{
2323
vec::Vec,
2424
};
2525
use async_trait::async_trait;
26+
use core::any::Any;
2627
use core::time::Duration;
2728
use core::{
2829
cmp::min,
@@ -325,6 +326,10 @@ where
325326
*self.attr.lock_save_irq() = attr;
326327
Ok(())
327328
}
329+
330+
fn as_any(&self) -> &dyn Any {
331+
self
332+
}
328333
}
329334

330335
struct TmpFsDirEnt {
@@ -660,6 +665,10 @@ where
660665
fn dir_is_empty(&self) -> Result<bool> {
661666
Ok(self.entries.lock_save_irq().is_empty())
662667
}
668+
669+
fn as_any(&self) -> &dyn Any {
670+
self
671+
}
663672
}
664673

665674
impl<C, G, T> TmpFsDirInode<C, G, T>
@@ -752,6 +761,10 @@ impl<C: CpuOps> Inode for TmpFsSymlinkInode<C> {
752761
Ok(())
753762
}
754763
}
764+
765+
fn as_any(&self) -> &dyn Any {
766+
self
767+
}
755768
}
756769

757770
impl<C: CpuOps> TmpFsSymlinkInode<C> {

libkernel/src/fs/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,8 @@ pub trait Inode: Send + Sync + Any {
328328
async fn datasync(&self) -> Result<()> {
329329
Ok(())
330330
}
331+
332+
fn as_any(&self) -> &dyn Any;
331333
}
332334

333335
/// A simplified trait for read-only files in procfs/sysfs that provides default implementations
@@ -377,6 +379,10 @@ where
377379
async fn readlink(&self) -> Result<PathBuf> {
378380
self.readlink().await
379381
}
382+
383+
fn as_any(&self) -> &dyn Any {
384+
self
385+
}
380386
}
381387

382388
pub struct SimpleDirStream {

libkernel/src/memory/proc_vm/vmarea.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,7 @@ impl VMArea {
486486
#[cfg(test)]
487487
pub mod tests {
488488
use crate::fs::InodeId;
489+
use core::any::Any;
489490

490491
use super::*;
491492
use async_trait::async_trait;
@@ -498,6 +499,10 @@ pub mod tests {
498499
fn id(&self) -> InodeId {
499500
unreachable!("Not called")
500501
}
502+
503+
fn as_any(&self) -> &dyn Any {
504+
self
505+
}
501506
}
502507

503508
pub fn create_test_vma(vaddr: usize, memsz: usize, file_offset: u64, filesz: u64) -> VMArea {

src/drivers/fs/cgroup/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ macro_rules! static_dir {
9494
)*
9595
Ok(Box::new(SimpleDirStream::new(entries, start_offset)))
9696
}
97+
98+
fn as_any(&self) -> &dyn core::any::Any {
99+
self
100+
}
97101
}
98102
};
99103
}

src/drivers/fs/dev.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use alloc::{
77
sync::Arc,
88
};
99
use async_trait::async_trait;
10+
use core::any::Any;
1011
use core::sync::atomic::{AtomicU64, Ordering};
1112
use libkernel::fs::attr::{FileAttr, FilePermissions};
1213
use libkernel::fs::{BlockDevice, DirStream, Dirent, Filesystem};
@@ -171,6 +172,10 @@ impl Inode for DevFsINode {
171172
InodeKind::CharDevice { .. } => Err(FsError::NotADirectory.into()),
172173
}
173174
}
175+
176+
fn as_any(&self) -> &dyn Any {
177+
self
178+
}
174179
}
175180

176181
/// The driver for the `devfs` filesystem itself.

0 commit comments

Comments
 (0)