xref: /DragonOS/kernel/src/filesystem/kernfs/callback.rs (revision 34e6d6c80f36494088db3284f85d1a2c63aa18a8)
1 use crate::{
2     filesystem::{sysfs::SysFSKernPrivateData, vfs::PollStatus},
3     libs::spinlock::SpinLockGuard,
4     syscall::SystemError,
5 };
6 use alloc::sync::Arc;
7 use core::fmt::Debug;
8 
9 use super::KernFSInode;
10 
11 /// KernFS文件的回调接口
12 ///
13 /// 当用户态程序打开、读取、写入、关闭文件时,kernfs会调用相应的回调函数。
14 pub trait KernFSCallback: Send + Sync + Debug {
15     fn open(&self, data: KernCallbackData) -> Result<(), SystemError>;
16 
17     fn read(
18         &self,
19         data: KernCallbackData,
20         buf: &mut [u8],
21         offset: usize,
22     ) -> Result<usize, SystemError>;
23 
24     fn write(
25         &self,
26         data: KernCallbackData,
27         buf: &[u8],
28         offset: usize,
29     ) -> Result<usize, SystemError>;
30 
31     fn poll(&self, data: KernCallbackData) -> Result<PollStatus, SystemError>;
32 }
33 
34 /// KernFS文件的回调数据
35 #[derive(Debug)]
36 pub struct KernCallbackData<'a> {
37     kern_inode: Arc<KernFSInode>,
38     private_data: SpinLockGuard<'a, Option<KernInodePrivateData>>,
39 }
40 
41 #[allow(dead_code)]
42 impl<'a> KernCallbackData<'a> {
43     pub fn new(
44         kern_inode: Arc<KernFSInode>,
45         private_data: SpinLockGuard<'a, Option<KernInodePrivateData>>,
46     ) -> Self {
47         Self {
48             kern_inode,
49             private_data,
50         }
51     }
52 
53     #[inline(always)]
54     pub fn kern_inode(&self) -> &Arc<KernFSInode> {
55         return &self.kern_inode;
56     }
57 
58     #[inline(always)]
59     pub fn private_data(&self) -> &Option<KernInodePrivateData> {
60         return &self.private_data;
61     }
62 
63     #[inline(always)]
64     pub fn private_data_mut(&mut self) -> &mut Option<KernInodePrivateData> {
65         return &mut self.private_data;
66     }
67 }
68 
69 #[allow(dead_code)]
70 #[derive(Debug)]
71 pub enum KernInodePrivateData {
72     SysFS(SysFSKernPrivateData),
73 }
74