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