xref: /DragonOS/kernel/src/driver/base/kset.rs (revision 08a2ee408498b0db4c76c57b149f1cf047758f3c)
1 use alloc::{
2     string::String,
3     sync::{Arc, Weak},
4     vec::Vec,
5 };
6 
7 use core::hash::Hash;
8 
9 use crate::{
10     filesystem::kernfs::KernFSInode,
11     libs::rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard},
12     syscall::SystemError,
13 };
14 
15 use super::kobject::{
16     DynamicKObjKType, KObjType, KObject, KObjectManager, KObjectState, LockedKObjectState,
17 };
18 
19 #[derive(Debug)]
20 pub struct KSet {
21     /// 属于当前kset的kobject
22     kobjects: RwLock<Vec<Weak<dyn KObject>>>,
23     /// 节点的一些信息
24     inner: RwLock<InnerKSet>,
25     /// kobject的状态
26     kobj_state: LockedKObjectState,
27     /// 与父节点有关的一些信息
28     parent_data: RwLock<KSetParentData>,
29     self_ref: Weak<KSet>,
30 }
31 
32 impl Hash for KSet {
33     fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
34         self.self_ref.as_ptr().hash(state);
35         self.inner.read().name.hash(state);
36     }
37 }
38 
39 impl core::cmp::Eq for KSet {}
40 
41 impl core::cmp::PartialEq for KSet {
42     fn eq(&self, other: &Self) -> bool {
43         self.self_ref.as_ptr() == other.self_ref.as_ptr()
44     }
45 }
46 
47 impl KSet {
48     pub fn new(name: String) -> Arc<Self> {
49         let r = Self {
50             kobjects: RwLock::new(Vec::new()),
51             inner: RwLock::new(InnerKSet::new(name)),
52             kobj_state: LockedKObjectState::new(None),
53             parent_data: RwLock::new(KSetParentData::new(None, None)),
54             self_ref: Weak::default(),
55         };
56 
57         let r = Arc::new(r);
58 
59         unsafe {
60             let p = r.as_ref() as *const Self as *mut Self;
61             (*p).self_ref = Arc::downgrade(&r);
62         }
63 
64         return r;
65     }
66 
67     /// 创建一个kset,并且设置它的父亲为parent_kobj。然后把这个kset注册到sysfs
68     ///
69     /// ## 参数
70     ///
71     /// - name: kset的名字
72     /// - parent_kobj: 父亲kobject
73     /// - join_kset: 如果不为None,那么这个kset会加入到join_kset中
74     pub fn new_and_add(
75         name: String,
76         parent_kobj: Option<Arc<dyn KObject>>,
77         join_kset: Option<Arc<KSet>>,
78     ) -> Result<Arc<Self>, SystemError> {
79         let kset = KSet::new(name);
80         if let Some(parent_kobj) = parent_kobj {
81             kset.set_parent(Some(Arc::downgrade(&parent_kobj)));
82         }
83         kset.register(join_kset)?;
84         return Ok(kset);
85     }
86 
87     /// 注册一个kset
88     ///
89     /// ## 参数
90     ///
91     /// - join_kset: 如果不为None,那么这个kset会加入到join_kset中
92     pub fn register(&self, join_kset: Option<Arc<KSet>>) -> Result<(), SystemError> {
93         return KObjectManager::add_kobj(self.self_ref.upgrade().unwrap(), join_kset);
94         // todo: 引入uevent之后,发送uevent
95     }
96 
97     /// 注销一个kset
98     #[allow(dead_code)]
99     pub fn unregister(&self) {
100         KObjectManager::remove_kobj(self.self_ref.upgrade().unwrap());
101     }
102 
103     /// 把一个kobject加入到当前kset中。
104     ///
105     /// 该函数不会修改kobj的parent,需要调用者自己视情况修改。
106     ///
107     /// ## Panic
108     ///
109     /// 这个kobject的kset必须是None,否则会panic
110     pub fn join(&self, kobj: &Arc<dyn KObject>) {
111         assert!(kobj.kset().is_none());
112         kobj.set_kset(self.self_ref.upgrade());
113         self.kobjects.write().push(Arc::downgrade(&kobj));
114     }
115 
116     /// 把一个kobject从当前kset中移除。
117     pub fn leave(&self, kobj: &Arc<dyn KObject>) {
118         let mut kobjects = self.kobjects.write();
119         kobjects.retain(|x| x.upgrade().is_some());
120         let index = kobjects.iter().position(|x| {
121             if let Some(x) = x.upgrade() {
122                 return Arc::ptr_eq(&x, kobj);
123             }
124             return false;
125         });
126         if let Some(index) = index {
127             let x = kobjects.remove(index);
128             let x = x.upgrade().unwrap();
129             drop(kobjects);
130             x.set_kset(None);
131         }
132     }
133 
134     /// 清除所有已经被释放的kobject
135     #[allow(dead_code)]
136     pub fn cleanup_weak(&self) {
137         let mut kobjects = self.kobjects.write();
138         kobjects.retain(|x| x.upgrade().is_some());
139     }
140 
141     pub fn as_kobject(&self) -> Arc<dyn KObject> {
142         return self.self_ref.upgrade().unwrap();
143     }
144 
145     pub fn kobjects(&self) -> RwLockReadGuard<Vec<Weak<dyn KObject>>> {
146         return self.kobjects.read();
147     }
148 }
149 
150 impl KObject for KSet {
151     fn as_any_ref(&self) -> &dyn core::any::Any {
152         self
153     }
154 
155     fn inode(&self) -> Option<Arc<KernFSInode>> {
156         self.inner.read().kern_inode.clone()
157     }
158 
159     fn set_inode(&self, inode: Option<Arc<KernFSInode>>) {
160         self.inner.write().kern_inode = inode;
161     }
162 
163     fn parent(&self) -> Option<Weak<dyn KObject>> {
164         self.parent_data.read().parent.clone()
165     }
166 
167     fn set_parent(&self, parent: Option<Weak<dyn KObject>>) {
168         self.parent_data.write().parent = parent;
169     }
170 
171     fn kobj_state(&self) -> RwLockReadGuard<KObjectState> {
172         self.kobj_state.read()
173     }
174 
175     fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> {
176         self.kobj_state.write()
177     }
178 
179     fn kobj_type(&self) -> Option<&'static dyn KObjType> {
180         self.inner.read().ktype
181     }
182 
183     fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>) {
184         self.inner.write().ktype = ktype;
185     }
186 
187     fn kset(&self) -> Option<Arc<KSet>> {
188         self.parent_data.read().kset.clone()
189     }
190 
191     fn set_kset(&self, kset: Option<Arc<KSet>>) {
192         self.parent_data.write().kset = kset;
193     }
194 
195     fn name(&self) -> String {
196         return self.inner.read().name.clone();
197     }
198 
199     fn set_name(&self, name: String) {
200         self.inner.write().name = name;
201     }
202 
203     fn set_kobj_state(&self, state: KObjectState) {
204         *self.kobj_state.write() = state;
205     }
206 }
207 
208 #[derive(Debug)]
209 struct KSetParentData {
210     parent: Option<Weak<dyn KObject>>,
211     kset: Option<Arc<KSet>>,
212 }
213 
214 impl KSetParentData {
215     fn new(parent: Option<Weak<dyn KObject>>, kset: Option<Arc<KSet>>) -> Self {
216         Self { parent, kset }
217     }
218 }
219 
220 #[derive(Debug)]
221 struct InnerKSet {
222     kern_inode: Option<Arc<KernFSInode>>,
223     name: String,
224     ktype: Option<&'static dyn KObjType>,
225 }
226 
227 impl InnerKSet {
228     fn new(name: String) -> Self {
229         Self {
230             kern_inode: None,
231             name,
232             ktype: Some(&DynamicKObjKType),
233         }
234     }
235 }
236