1 use core::fmt::Debug;
2
3 use self::{dir::SysKernDirPriv, file::SysKernFilePriv};
4
5 use super::{
6 kernfs::{KernFS, KernFSInode},
7 vfs::{syscall::ModeType, FileSystem},
8 };
9 use crate::{
10 driver::base::kobject::KObject,
11 filesystem::vfs::ROOT_INODE,
12 kinfo, kwarn,
13 libs::{casting::DowncastArc, once::Once},
14 };
15 use alloc::sync::Arc;
16 use system_error::SystemError;
17
18 pub mod dir;
19 pub mod file;
20 pub mod group;
21 pub mod symlink;
22
23 /// 全局的sysfs实例
24 pub(self) static mut SYSFS_INSTANCE: Option<SysFS> = None;
25
26 #[inline(always)]
sysfs_instance() -> &'static SysFS27 pub fn sysfs_instance() -> &'static SysFS {
28 unsafe {
29 return &SYSFS_INSTANCE.as_ref().unwrap();
30 }
31 }
32
sysfs_init() -> Result<(), SystemError>33 pub fn sysfs_init() -> Result<(), SystemError> {
34 static INIT: Once = Once::new();
35 let mut result = None;
36 INIT.call_once(|| {
37 kinfo!("Initializing SysFS...");
38
39 // 创建 sysfs 实例
40 // let sysfs: Arc<OldSysFS> = OldSysFS::new();
41 let sysfs = SysFS::new();
42 unsafe { SYSFS_INSTANCE = Some(sysfs) };
43
44 // sysfs 挂载
45 let _t = ROOT_INODE()
46 .find("sys")
47 .expect("Cannot find /sys")
48 .mount(sysfs_instance().fs().clone())
49 .expect("Failed to mount sysfs");
50 kinfo!("SysFS mounted.");
51
52 // kdebug!("sys_bus_init result: {:?}", SYS_BUS_INODE().list());
53 result = Some(Ok(()));
54 });
55
56 return result.unwrap();
57 }
58
59 /// SysFS在KernFS的inode中的私有信息
60 #[allow(dead_code)]
61 #[derive(Debug)]
62 pub enum SysFSKernPrivateData {
63 Dir(SysKernDirPriv),
64 File(SysKernFilePriv),
65 }
66
67 impl SysFSKernPrivateData {
68 #[inline(always)]
callback_read(&self, buf: &mut [u8], offset: usize) -> Result<usize, SystemError>69 pub fn callback_read(&self, buf: &mut [u8], offset: usize) -> Result<usize, SystemError> {
70 match self {
71 SysFSKernPrivateData::File(file) => {
72 let len = file.callback_read(buf, offset)?;
73
74 return Ok(len);
75 }
76 _ => {
77 return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
78 }
79 }
80 }
81
82 #[inline(always)]
callback_write(&self, buf: &[u8], offset: usize) -> Result<usize, SystemError>83 pub fn callback_write(&self, buf: &[u8], offset: usize) -> Result<usize, SystemError> {
84 match self {
85 SysFSKernPrivateData::File(file) => {
86 return file.callback_write(buf, offset);
87 }
88 _ => {
89 return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
90 }
91 }
92 }
93 }
94
95 /// sysfs文件目录的属性组
96 pub trait AttributeGroup: Debug + Send + Sync {
97 /// 属性组的名称
98 ///
99 /// 如果属性组的名称为None,则所有的属性都会被添加到父目录下,而不是创建一个新的目录
name(&self) -> Option<&str>100 fn name(&self) -> Option<&str>;
101 /// 属性组的属性列表
attrs(&self) -> &[&'static dyn Attribute]102 fn attrs(&self) -> &[&'static dyn Attribute];
103
104 /// 属性在当前属性组内的权限(该方法可选)
105 ///
106 /// 如果返回None,则使用Attribute的mode()方法返回的权限
107 ///
108 /// 如果返回Some,则使用返回的权限。
109 /// 如果要标识属性不可见,则返回Some(ModeType::empty())
is_visible(&self, kobj: Arc<dyn KObject>, attr: &'static dyn Attribute) -> Option<ModeType>110 fn is_visible(&self, kobj: Arc<dyn KObject>, attr: &'static dyn Attribute) -> Option<ModeType>;
111 }
112
113 /// sysfs只读属性文件的权限
114 pub const SYSFS_ATTR_MODE_RO: ModeType = ModeType::from_bits_truncate(0o444);
115 /// sysfs只写属性文件的权限
116 pub const SYSFS_ATTR_MODE_WO: ModeType = ModeType::from_bits_truncate(0o200);
117 /// sysfs读写属性文件的权限
118 pub const SYSFS_ATTR_MODE_RW: ModeType = ModeType::from_bits_truncate(0o644);
119
120 /// sysfs文件的属性
121 pub trait Attribute: Debug + Send + Sync {
name(&self) -> &str122 fn name(&self) -> &str;
mode(&self) -> ModeType123 fn mode(&self) -> ModeType;
124
support(&self) -> SysFSOpsSupport125 fn support(&self) -> SysFSOpsSupport;
126
show(&self, _kobj: Arc<dyn KObject>, _buf: &mut [u8]) -> Result<usize, SystemError>127 fn show(&self, _kobj: Arc<dyn KObject>, _buf: &mut [u8]) -> Result<usize, SystemError> {
128 return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
129 }
130
store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError>131 fn store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError> {
132 return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
133 }
134 }
135
136 pub trait BinAttribute: Attribute {
support_battr(&self) -> SysFSOpsSupport137 fn support_battr(&self) -> SysFSOpsSupport;
138
write( &self, _kobj: Arc<dyn KObject>, _buf: &[u8], _offset: usize, ) -> Result<usize, SystemError>139 fn write(
140 &self,
141 _kobj: Arc<dyn KObject>,
142 _buf: &[u8],
143 _offset: usize,
144 ) -> Result<usize, SystemError> {
145 return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
146 }
147
read( &self, _kobj: Arc<dyn KObject>, _buf: &mut [u8], _offset: usize, ) -> Result<usize, SystemError>148 fn read(
149 &self,
150 _kobj: Arc<dyn KObject>,
151 _buf: &mut [u8],
152 _offset: usize,
153 ) -> Result<usize, SystemError> {
154 return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
155 }
156
size(&self) -> usize157 fn size(&self) -> usize;
158 }
159
160 pub trait SysFSOps: Debug {
161 /// 获取当前文件的支持的操作
support(&self, attr: &dyn Attribute) -> SysFSOpsSupport162 fn support(&self, attr: &dyn Attribute) -> SysFSOpsSupport {
163 return attr.support();
164 }
165
support_battr(&self, attr: &Arc<dyn BinAttribute>) -> SysFSOpsSupport166 fn support_battr(&self, attr: &Arc<dyn BinAttribute>) -> SysFSOpsSupport {
167 return attr.support();
168 }
169
show( &self, kobj: Arc<dyn KObject>, attr: &dyn Attribute, buf: &mut [u8], ) -> Result<usize, SystemError>170 fn show(
171 &self,
172 kobj: Arc<dyn KObject>,
173 attr: &dyn Attribute,
174 buf: &mut [u8],
175 ) -> Result<usize, SystemError>;
176
store( &self, kobj: Arc<dyn KObject>, attr: &dyn Attribute, buf: &[u8], ) -> Result<usize, SystemError>177 fn store(
178 &self,
179 kobj: Arc<dyn KObject>,
180 attr: &dyn Attribute,
181 buf: &[u8],
182 ) -> Result<usize, SystemError>;
183 }
184
185 bitflags! {
186 pub struct SysFSOpsSupport: u8{
187 // === for attribute ===
188 const ATTR_SHOW = 1 << 0;
189 const ATTR_STORE = 1 << 1;
190 // === for bin attribute ===
191 const BATTR_READ = 1 << 2;
192 const BATTR_WRITE = 1 << 3;
193 }
194 }
195
196 #[derive(Debug)]
197 pub struct SysFS {
198 root_inode: Arc<KernFSInode>,
199 kernfs: Arc<KernFS>,
200 }
201
202 impl SysFS {
new() -> Self203 pub fn new() -> Self {
204 let kernfs: Arc<KernFS> = KernFS::new();
205
206 let root_inode: Arc<KernFSInode> = kernfs.root_inode().downcast_arc().unwrap();
207
208 let sysfs = SysFS { root_inode, kernfs };
209
210 return sysfs;
211 }
212
root_inode(&self) -> &Arc<KernFSInode>213 pub fn root_inode(&self) -> &Arc<KernFSInode> {
214 return &self.root_inode;
215 }
216
fs(&self) -> &Arc<KernFS>217 pub fn fs(&self) -> &Arc<KernFS> {
218 return &self.kernfs;
219 }
220
221 /// 警告:重复的sysfs entry
warn_duplicate(&self, parent: &Arc<KernFSInode>, name: &str)222 pub(self) fn warn_duplicate(&self, parent: &Arc<KernFSInode>, name: &str) {
223 let path = self.kernfs_path(parent);
224 kwarn!("duplicate sysfs entry: {path}/{name}");
225 }
226 }
227