xref: /DragonOS/kernel/src/ipc/pipe.rs (revision 6fc066ac11d2f9a3ac629d57487a6144fda1ac63)
1f678331aShanjiezhou use crate::{
2f0c87a89SGnoCiYeH     arch::CurrentIrqArch,
3f678331aShanjiezhou     exception::InterruptArch,
4f678331aShanjiezhou     filesystem::vfs::{
56b4e7a29SLoGin         core::generate_inode_id, file::FileMode, syscall::ModeType, FilePrivateData, FileSystem,
640609970SGnoCiYeH         FileType, IndexNode, Metadata,
7f678331aShanjiezhou     },
8dfe53cf0SGnoCiYeH     libs::{
9dfe53cf0SGnoCiYeH         spinlock::{SpinLock, SpinLockGuard},
10dfe53cf0SGnoCiYeH         wait_queue::WaitQueue,
11dfe53cf0SGnoCiYeH     },
125e948c56SGnoCiYeH     net::event_poll::{EPollEventType, EPollItem, EventPoll},
131496ba7bSLoGin     process::ProcessState,
14f0c87a89SGnoCiYeH     sched::{schedule, SchedMode},
15*6fc066acSJomo     time::PosixTimeSpec,
16f678331aShanjiezhou };
17f678331aShanjiezhou 
185e948c56SGnoCiYeH use alloc::{
195e948c56SGnoCiYeH     collections::LinkedList,
205e948c56SGnoCiYeH     sync::{Arc, Weak},
215e948c56SGnoCiYeH };
2291e9d4abSLoGin use system_error::SystemError;
23f678331aShanjiezhou 
24f678331aShanjiezhou /// 我们设定pipe_buff的总大小为1024字节
25f678331aShanjiezhou const PIPE_BUFF_SIZE: usize = 1024;
26f678331aShanjiezhou 
27876cb89eSGnoCiYeH #[derive(Debug, Clone)]
28876cb89eSGnoCiYeH pub struct PipeFsPrivateData {
29876cb89eSGnoCiYeH     mode: FileMode,
30876cb89eSGnoCiYeH }
31876cb89eSGnoCiYeH 
32876cb89eSGnoCiYeH impl PipeFsPrivateData {
33876cb89eSGnoCiYeH     pub fn new(mode: FileMode) -> Self {
344ad52e57S裕依2439         return PipeFsPrivateData { mode };
35876cb89eSGnoCiYeH     }
365e948c56SGnoCiYeH 
375e948c56SGnoCiYeH     pub fn set_mode(&mut self, mode: FileMode) {
385e948c56SGnoCiYeH         self.mode = mode;
395e948c56SGnoCiYeH     }
40876cb89eSGnoCiYeH }
41876cb89eSGnoCiYeH 
42f678331aShanjiezhou /// @brief 管道文件i节点(锁)
43f678331aShanjiezhou #[derive(Debug)]
44dfe53cf0SGnoCiYeH pub struct LockedPipeInode {
45dfe53cf0SGnoCiYeH     inner: SpinLock<InnerPipeInode>,
46dfe53cf0SGnoCiYeH     read_wait_queue: WaitQueue,
47dfe53cf0SGnoCiYeH     write_wait_queue: WaitQueue,
48dfe53cf0SGnoCiYeH }
49f678331aShanjiezhou 
50f678331aShanjiezhou /// @brief 管道文件i节点(无锁)
51f678331aShanjiezhou #[derive(Debug)]
52f678331aShanjiezhou pub struct InnerPipeInode {
53f678331aShanjiezhou     self_ref: Weak<LockedPipeInode>,
545e948c56SGnoCiYeH     /// 管道内可读的数据数
55f678331aShanjiezhou     valid_cnt: i32,
56f678331aShanjiezhou     read_pos: i32,
57f678331aShanjiezhou     write_pos: i32,
58f678331aShanjiezhou     data: [u8; PIPE_BUFF_SIZE],
59f678331aShanjiezhou     /// INode 元数据
60f678331aShanjiezhou     metadata: Metadata,
61876cb89eSGnoCiYeH     reader: u32,
62876cb89eSGnoCiYeH     writer: u32,
635e948c56SGnoCiYeH     epitems: SpinLock<LinkedList<Arc<EPollItem>>>,
645e948c56SGnoCiYeH }
655e948c56SGnoCiYeH 
665e948c56SGnoCiYeH impl InnerPipeInode {
675e948c56SGnoCiYeH     pub fn poll(&self, private_data: &FilePrivateData) -> Result<usize, SystemError> {
685e948c56SGnoCiYeH         let mut events = EPollEventType::empty();
695e948c56SGnoCiYeH 
705e948c56SGnoCiYeH         let mode = if let FilePrivateData::Pipefs(PipeFsPrivateData { mode }) = private_data {
715e948c56SGnoCiYeH             mode
725e948c56SGnoCiYeH         } else {
735e948c56SGnoCiYeH             return Err(SystemError::EBADFD);
745e948c56SGnoCiYeH         };
755e948c56SGnoCiYeH 
765e948c56SGnoCiYeH         if mode.contains(FileMode::O_RDONLY) {
775e948c56SGnoCiYeH             if self.valid_cnt != 0 {
785e948c56SGnoCiYeH                 // 有数据可读
795e948c56SGnoCiYeH                 events.insert(EPollEventType::EPOLLIN & EPollEventType::EPOLLRDNORM);
805e948c56SGnoCiYeH             }
815e948c56SGnoCiYeH 
825e948c56SGnoCiYeH             // 没有写者
835e948c56SGnoCiYeH             if self.writer == 0 {
845e948c56SGnoCiYeH                 events.insert(EPollEventType::EPOLLHUP)
855e948c56SGnoCiYeH             }
865e948c56SGnoCiYeH         }
875e948c56SGnoCiYeH 
885e948c56SGnoCiYeH         if mode.contains(FileMode::O_WRONLY) {
895e948c56SGnoCiYeH             // 管道内数据未满
905e948c56SGnoCiYeH             if self.valid_cnt as usize != PIPE_BUFF_SIZE {
915e948c56SGnoCiYeH                 events.insert(EPollEventType::EPOLLIN & EPollEventType::EPOLLWRNORM);
925e948c56SGnoCiYeH             }
935e948c56SGnoCiYeH 
945e948c56SGnoCiYeH             // 没有读者
955e948c56SGnoCiYeH             if self.reader == 0 {
965e948c56SGnoCiYeH                 events.insert(EPollEventType::EPOLLERR);
975e948c56SGnoCiYeH             }
985e948c56SGnoCiYeH         }
995e948c56SGnoCiYeH 
1005e948c56SGnoCiYeH         Ok(events.bits() as usize)
1015e948c56SGnoCiYeH     }
1025e948c56SGnoCiYeH 
1035e948c56SGnoCiYeH     pub fn add_epoll(&mut self, epitem: Arc<EPollItem>) -> Result<(), SystemError> {
1045e948c56SGnoCiYeH         self.epitems.lock().push_back(epitem);
1055e948c56SGnoCiYeH         Ok(())
1065e948c56SGnoCiYeH     }
107f678331aShanjiezhou }
108f678331aShanjiezhou 
109f678331aShanjiezhou impl LockedPipeInode {
110876cb89eSGnoCiYeH     pub fn new() -> Arc<Self> {
111f678331aShanjiezhou         let inner = InnerPipeInode {
112f678331aShanjiezhou             self_ref: Weak::default(),
113f678331aShanjiezhou             valid_cnt: 0,
114f678331aShanjiezhou             read_pos: 0,
115f678331aShanjiezhou             write_pos: 0,
116f678331aShanjiezhou             data: [0; PIPE_BUFF_SIZE],
117f678331aShanjiezhou 
118f678331aShanjiezhou             metadata: Metadata {
119f678331aShanjiezhou                 dev_id: 0,
120f678331aShanjiezhou                 inode_id: generate_inode_id(),
12122c9db31Shanjiezhou                 size: PIPE_BUFF_SIZE as i64,
122f678331aShanjiezhou                 blk_size: 0,
123f678331aShanjiezhou                 blocks: 0,
124*6fc066acSJomo                 atime: PosixTimeSpec::default(),
125*6fc066acSJomo                 mtime: PosixTimeSpec::default(),
126*6fc066acSJomo                 ctime: PosixTimeSpec::default(),
127f678331aShanjiezhou                 file_type: FileType::Pipe,
1286b4e7a29SLoGin                 mode: ModeType::from_bits_truncate(0o666),
129f678331aShanjiezhou                 nlinks: 1,
130f678331aShanjiezhou                 uid: 0,
131f678331aShanjiezhou                 gid: 0,
13202343d0bSLoGin                 raw_dev: Default::default(),
133f678331aShanjiezhou             },
134876cb89eSGnoCiYeH             reader: 0,
135876cb89eSGnoCiYeH             writer: 0,
1365e948c56SGnoCiYeH             epitems: SpinLock::new(LinkedList::new()),
137f678331aShanjiezhou         };
138dfe53cf0SGnoCiYeH         let result = Arc::new(Self {
139dfe53cf0SGnoCiYeH             inner: SpinLock::new(inner),
140dfe53cf0SGnoCiYeH             read_wait_queue: WaitQueue::default(),
141dfe53cf0SGnoCiYeH             write_wait_queue: WaitQueue::default(),
142dfe53cf0SGnoCiYeH         });
143dfe53cf0SGnoCiYeH         let mut guard = result.inner.lock();
144f678331aShanjiezhou         guard.self_ref = Arc::downgrade(&result);
145f678331aShanjiezhou         // 释放锁
146f678331aShanjiezhou         drop(guard); //这一步其实不需要,只要离开作用域,guard生命周期结束,自会解锁
147f678331aShanjiezhou         return result;
148f678331aShanjiezhou     }
1495e948c56SGnoCiYeH 
1505e948c56SGnoCiYeH     pub fn inner(&self) -> &SpinLock<InnerPipeInode> {
151dfe53cf0SGnoCiYeH         &self.inner
1525e948c56SGnoCiYeH     }
153f678331aShanjiezhou }
154f678331aShanjiezhou 
155f678331aShanjiezhou impl IndexNode for LockedPipeInode {
156f678331aShanjiezhou     fn read_at(
157f678331aShanjiezhou         &self,
158f678331aShanjiezhou         _offset: usize,
159f678331aShanjiezhou         len: usize,
160f678331aShanjiezhou         buf: &mut [u8],
161dfe53cf0SGnoCiYeH         data: SpinLockGuard<FilePrivateData>,
16291e9d4abSLoGin     ) -> Result<usize, SystemError> {
163876cb89eSGnoCiYeH         // 获取mode
164876cb89eSGnoCiYeH         let mode: FileMode;
165dfe53cf0SGnoCiYeH         if let FilePrivateData::Pipefs(pdata) = &*data {
166876cb89eSGnoCiYeH             mode = pdata.mode;
167876cb89eSGnoCiYeH         } else {
168876cb89eSGnoCiYeH             return Err(SystemError::EBADF);
169876cb89eSGnoCiYeH         }
170876cb89eSGnoCiYeH 
171f678331aShanjiezhou         if buf.len() < len {
172f678331aShanjiezhou             return Err(SystemError::EINVAL);
173f678331aShanjiezhou         }
174f678331aShanjiezhou         // 加锁
175dfe53cf0SGnoCiYeH         let mut inode = self.inner.lock();
176f678331aShanjiezhou 
177f678331aShanjiezhou         // 如果管道里面没有数据,则唤醒写端,
178f678331aShanjiezhou         while inode.valid_cnt == 0 {
179876cb89eSGnoCiYeH             // 如果当前管道写者数为0,则返回EOF
180876cb89eSGnoCiYeH             if inode.writer == 0 {
181876cb89eSGnoCiYeH                 return Ok(0);
182876cb89eSGnoCiYeH             }
183876cb89eSGnoCiYeH 
184dfe53cf0SGnoCiYeH             self.write_wait_queue
1851496ba7bSLoGin                 .wakeup(Some(ProcessState::Blocked(true)));
1861496ba7bSLoGin 
18722c9db31Shanjiezhou             // 如果为非阻塞管道,直接返回错误
188876cb89eSGnoCiYeH             if mode.contains(FileMode::O_NONBLOCK) {
18922c9db31Shanjiezhou                 drop(inode);
19022c9db31Shanjiezhou                 return Err(SystemError::EAGAIN_OR_EWOULDBLOCK);
19122c9db31Shanjiezhou             }
1921496ba7bSLoGin 
19322c9db31Shanjiezhou             // 否则在读等待队列中睡眠,并释放锁
194f678331aShanjiezhou             unsafe {
195f678331aShanjiezhou                 let irq_guard = CurrentIrqArch::save_and_disable_irq();
196bf4a4899SLoGin 
197f678331aShanjiezhou                 drop(inode);
198dfe53cf0SGnoCiYeH                 self.read_wait_queue.sleep_without_schedule();
199f678331aShanjiezhou                 drop(irq_guard);
200f678331aShanjiezhou             }
201f0c87a89SGnoCiYeH             schedule(SchedMode::SM_NONE);
202dfe53cf0SGnoCiYeH             inode = self.inner.lock();
203f678331aShanjiezhou         }
204f678331aShanjiezhou 
205f678331aShanjiezhou         let mut num = inode.valid_cnt as usize;
206f678331aShanjiezhou         //决定要输出的字节
207f678331aShanjiezhou         let start = inode.read_pos as usize;
208f678331aShanjiezhou         //如果读端希望读取的字节数大于有效字节数,则输出有效字节
209f678331aShanjiezhou         let mut end = (inode.valid_cnt as usize + inode.read_pos as usize) % PIPE_BUFF_SIZE;
210f678331aShanjiezhou         //如果读端希望读取的字节数少于有效字节数,则输出希望读取的字节
211f678331aShanjiezhou         if len < inode.valid_cnt as usize {
212f678331aShanjiezhou             end = (len + inode.read_pos as usize) % PIPE_BUFF_SIZE;
213f678331aShanjiezhou             num = len;
214f678331aShanjiezhou         }
215f678331aShanjiezhou 
216f678331aShanjiezhou         // 从管道拷贝数据到用户的缓冲区
217f678331aShanjiezhou 
218f678331aShanjiezhou         if end < start {
219f678331aShanjiezhou             buf[0..(PIPE_BUFF_SIZE - start)].copy_from_slice(&inode.data[start..PIPE_BUFF_SIZE]);
220f678331aShanjiezhou             buf[(PIPE_BUFF_SIZE - start)..num].copy_from_slice(&inode.data[0..end]);
221f678331aShanjiezhou         } else {
222f678331aShanjiezhou             buf[0..num].copy_from_slice(&inode.data[start..end]);
223f678331aShanjiezhou         }
224f678331aShanjiezhou 
225f678331aShanjiezhou         //更新读位置以及valid_cnt
226f678331aShanjiezhou         inode.read_pos = (inode.read_pos + num as i32) % PIPE_BUFF_SIZE as i32;
227f678331aShanjiezhou         inode.valid_cnt -= num as i32;
228f678331aShanjiezhou 
2295e948c56SGnoCiYeH         // 读完以后如果未读完,则唤醒下一个读者
2305e948c56SGnoCiYeH         if inode.valid_cnt > 0 {
231dfe53cf0SGnoCiYeH             self.read_wait_queue
2325e948c56SGnoCiYeH                 .wakeup(Some(ProcessState::Blocked(true)));
2335e948c56SGnoCiYeH         }
2345e948c56SGnoCiYeH 
235f678331aShanjiezhou         //读完后解锁并唤醒等待在写等待队列中的进程
236dfe53cf0SGnoCiYeH         self.write_wait_queue
2371496ba7bSLoGin             .wakeup(Some(ProcessState::Blocked(true)));
2385e948c56SGnoCiYeH 
239dfe53cf0SGnoCiYeH         let pollflag = EPollEventType::from_bits_truncate(inode.poll(&data)? as u32);
2405e948c56SGnoCiYeH         // 唤醒epoll中等待的进程
241b5b571e0SLoGin         EventPoll::wakeup_epoll(&inode.epitems, pollflag)?;
2425e948c56SGnoCiYeH 
243f678331aShanjiezhou         //返回读取的字节数
244f678331aShanjiezhou         return Ok(num);
245f678331aShanjiezhou     }
246f678331aShanjiezhou 
247f678331aShanjiezhou     fn open(
248f678331aShanjiezhou         &self,
249dfe53cf0SGnoCiYeH         mut data: SpinLockGuard<FilePrivateData>,
250876cb89eSGnoCiYeH         mode: &crate::filesystem::vfs::file::FileMode,
251f678331aShanjiezhou     ) -> Result<(), SystemError> {
252dfe53cf0SGnoCiYeH         let mut guard = self.inner.lock();
253876cb89eSGnoCiYeH         // 不能以读写方式打开管道
254876cb89eSGnoCiYeH         if mode.contains(FileMode::O_RDWR) {
255876cb89eSGnoCiYeH             return Err(SystemError::EACCES);
256876cb89eSGnoCiYeH         }
257876cb89eSGnoCiYeH         if mode.contains(FileMode::O_RDONLY) {
258876cb89eSGnoCiYeH             guard.reader += 1;
259876cb89eSGnoCiYeH         }
260876cb89eSGnoCiYeH         if mode.contains(FileMode::O_WRONLY) {
261876cb89eSGnoCiYeH             guard.writer += 1;
262876cb89eSGnoCiYeH         }
263876cb89eSGnoCiYeH 
264876cb89eSGnoCiYeH         // 设置mode
265876cb89eSGnoCiYeH         *data = FilePrivateData::Pipefs(PipeFsPrivateData { mode: *mode });
266876cb89eSGnoCiYeH 
267f678331aShanjiezhou         return Ok(());
268f678331aShanjiezhou     }
269f678331aShanjiezhou 
270f678331aShanjiezhou     fn metadata(&self) -> Result<crate::filesystem::vfs::Metadata, SystemError> {
271dfe53cf0SGnoCiYeH         let inode = self.inner.lock();
272f678331aShanjiezhou         let mut metadata = inode.metadata.clone();
273f678331aShanjiezhou         metadata.size = inode.data.len() as i64;
274f678331aShanjiezhou 
275f678331aShanjiezhou         return Ok(metadata);
276f678331aShanjiezhou     }
277f678331aShanjiezhou 
278dfe53cf0SGnoCiYeH     fn close(&self, data: SpinLockGuard<FilePrivateData>) -> Result<(), SystemError> {
279876cb89eSGnoCiYeH         let mode: FileMode;
280dfe53cf0SGnoCiYeH         if let FilePrivateData::Pipefs(pipe_data) = &*data {
281876cb89eSGnoCiYeH             mode = pipe_data.mode;
282876cb89eSGnoCiYeH         } else {
283876cb89eSGnoCiYeH             return Err(SystemError::EBADF);
284876cb89eSGnoCiYeH         }
285dfe53cf0SGnoCiYeH         let mut guard = self.inner.lock();
286876cb89eSGnoCiYeH 
287876cb89eSGnoCiYeH         // 写端关闭
288876cb89eSGnoCiYeH         if mode.contains(FileMode::O_WRONLY) {
289876cb89eSGnoCiYeH             assert!(guard.writer > 0);
290876cb89eSGnoCiYeH             guard.writer -= 1;
291876cb89eSGnoCiYeH             // 如果已经没有写端了,则唤醒读端
292876cb89eSGnoCiYeH             if guard.writer == 0 {
293dfe53cf0SGnoCiYeH                 self.read_wait_queue
294876cb89eSGnoCiYeH                     .wakeup_all(Some(ProcessState::Blocked(true)));
295876cb89eSGnoCiYeH             }
296876cb89eSGnoCiYeH         }
297876cb89eSGnoCiYeH 
298876cb89eSGnoCiYeH         // 读端关闭
299876cb89eSGnoCiYeH         if mode.contains(FileMode::O_RDONLY) {
300876cb89eSGnoCiYeH             assert!(guard.reader > 0);
301876cb89eSGnoCiYeH             guard.reader -= 1;
302876cb89eSGnoCiYeH             // 如果已经没有写端了,则唤醒读端
303876cb89eSGnoCiYeH             if guard.reader == 0 {
304dfe53cf0SGnoCiYeH                 self.write_wait_queue
305876cb89eSGnoCiYeH                     .wakeup_all(Some(ProcessState::Blocked(true)));
306876cb89eSGnoCiYeH             }
307876cb89eSGnoCiYeH         }
308876cb89eSGnoCiYeH 
309f678331aShanjiezhou         return Ok(());
310f678331aShanjiezhou     }
311f678331aShanjiezhou 
312f678331aShanjiezhou     fn write_at(
313f678331aShanjiezhou         &self,
314f678331aShanjiezhou         _offset: usize,
315f678331aShanjiezhou         len: usize,
316f678331aShanjiezhou         buf: &[u8],
317dfe53cf0SGnoCiYeH         data: SpinLockGuard<FilePrivateData>,
31891e9d4abSLoGin     ) -> Result<usize, SystemError> {
319876cb89eSGnoCiYeH         // 获取mode
320876cb89eSGnoCiYeH         let mode: FileMode;
321dfe53cf0SGnoCiYeH         if let FilePrivateData::Pipefs(pdata) = &*data {
322876cb89eSGnoCiYeH             mode = pdata.mode;
323876cb89eSGnoCiYeH         } else {
324876cb89eSGnoCiYeH             return Err(SystemError::EBADF);
325876cb89eSGnoCiYeH         }
326876cb89eSGnoCiYeH 
327f678331aShanjiezhou         if buf.len() < len || len > PIPE_BUFF_SIZE {
328f678331aShanjiezhou             return Err(SystemError::EINVAL);
329f678331aShanjiezhou         }
330f678331aShanjiezhou         // 加锁
331f678331aShanjiezhou 
332dfe53cf0SGnoCiYeH         let mut inode = self.inner.lock();
333f678331aShanjiezhou 
334b5b571e0SLoGin         if inode.reader == 0 {
335876cb89eSGnoCiYeH             // TODO: 如果已经没有读端存在了,则向写端进程发送SIGPIPE信号
336b5b571e0SLoGin         }
337876cb89eSGnoCiYeH 
338f678331aShanjiezhou         // 如果管道空间不够
339f678331aShanjiezhou 
340f678331aShanjiezhou         while len + inode.valid_cnt as usize > PIPE_BUFF_SIZE {
341f678331aShanjiezhou             // 唤醒读端
342dfe53cf0SGnoCiYeH             self.read_wait_queue
3431496ba7bSLoGin                 .wakeup(Some(ProcessState::Blocked(true)));
3441496ba7bSLoGin 
34522c9db31Shanjiezhou             // 如果为非阻塞管道,直接返回错误
346876cb89eSGnoCiYeH             if mode.contains(FileMode::O_NONBLOCK) {
34722c9db31Shanjiezhou                 drop(inode);
34822c9db31Shanjiezhou                 return Err(SystemError::ENOMEM);
34922c9db31Shanjiezhou             }
3501496ba7bSLoGin 
351f678331aShanjiezhou             // 解锁并睡眠
352f678331aShanjiezhou             unsafe {
353f678331aShanjiezhou                 let irq_guard = CurrentIrqArch::save_and_disable_irq();
354f678331aShanjiezhou                 drop(inode);
355dfe53cf0SGnoCiYeH                 self.write_wait_queue.sleep_without_schedule();
356f678331aShanjiezhou                 drop(irq_guard);
357f678331aShanjiezhou             }
358f0c87a89SGnoCiYeH             schedule(SchedMode::SM_NONE);
359dfe53cf0SGnoCiYeH             inode = self.inner.lock();
360f678331aShanjiezhou         }
361f678331aShanjiezhou 
362f678331aShanjiezhou         // 决定要输入的字节
363f678331aShanjiezhou         let start = inode.write_pos as usize;
364f678331aShanjiezhou         let end = (inode.write_pos as usize + len) % PIPE_BUFF_SIZE;
365f678331aShanjiezhou         // 从用户的缓冲区拷贝数据到管道
366f678331aShanjiezhou 
367f678331aShanjiezhou         if end < start {
368f678331aShanjiezhou             inode.data[start..PIPE_BUFF_SIZE].copy_from_slice(&buf[0..(PIPE_BUFF_SIZE - start)]);
369f678331aShanjiezhou             inode.data[0..end].copy_from_slice(&buf[(PIPE_BUFF_SIZE - start)..len]);
370f678331aShanjiezhou         } else {
371f678331aShanjiezhou             inode.data[start..end].copy_from_slice(&buf[0..len]);
372f678331aShanjiezhou         }
373f678331aShanjiezhou         // 更新写位置以及valid_cnt
374f678331aShanjiezhou         inode.write_pos = (inode.write_pos + len as i32) % PIPE_BUFF_SIZE as i32;
375f678331aShanjiezhou         inode.valid_cnt += len as i32;
376f678331aShanjiezhou 
3775e948c56SGnoCiYeH         // 写完后还有位置,则唤醒下一个写者
3785e948c56SGnoCiYeH         if (inode.valid_cnt as usize) < PIPE_BUFF_SIZE {
379dfe53cf0SGnoCiYeH             self.write_wait_queue
3805e948c56SGnoCiYeH                 .wakeup(Some(ProcessState::Blocked(true)));
3815e948c56SGnoCiYeH         }
3825e948c56SGnoCiYeH 
383f678331aShanjiezhou         // 读完后解锁并唤醒等待在读等待队列中的进程
384dfe53cf0SGnoCiYeH         self.read_wait_queue
3851496ba7bSLoGin             .wakeup(Some(ProcessState::Blocked(true)));
3865e948c56SGnoCiYeH 
387dfe53cf0SGnoCiYeH         let pollflag = EPollEventType::from_bits_truncate(inode.poll(&data)? as u32);
3885e948c56SGnoCiYeH         // 唤醒epoll中等待的进程
389b5b571e0SLoGin         EventPoll::wakeup_epoll(&inode.epitems, pollflag)?;
3905e948c56SGnoCiYeH 
391f678331aShanjiezhou         // 返回写入的字节数
392f678331aShanjiezhou         return Ok(len);
393f678331aShanjiezhou     }
394f678331aShanjiezhou 
395f678331aShanjiezhou     fn as_any_ref(&self) -> &dyn core::any::Any {
396f678331aShanjiezhou         self
397f678331aShanjiezhou     }
398f678331aShanjiezhou 
399f678331aShanjiezhou     fn get_entry_name_and_metadata(
400f678331aShanjiezhou         &self,
401f678331aShanjiezhou         ino: crate::filesystem::vfs::InodeId,
402f678331aShanjiezhou     ) -> Result<(alloc::string::String, crate::filesystem::vfs::Metadata), SystemError> {
403f678331aShanjiezhou         // 如果有条件,请在文件系统中使用高效的方式实现本接口,而不是依赖这个低效率的默认实现。
404f678331aShanjiezhou         let name = self.get_entry_name(ino)?;
405f678331aShanjiezhou         let entry = self.find(&name)?;
406f678331aShanjiezhou         return Ok((name, entry.metadata()?));
407f678331aShanjiezhou     }
408f678331aShanjiezhou 
409f678331aShanjiezhou     fn fs(&self) -> Arc<(dyn FileSystem)> {
410f678331aShanjiezhou         todo!()
411f678331aShanjiezhou     }
412f678331aShanjiezhou 
413f678331aShanjiezhou     fn list(&self) -> Result<alloc::vec::Vec<alloc::string::String>, SystemError> {
414f678331aShanjiezhou         return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
415f678331aShanjiezhou     }
4165e948c56SGnoCiYeH 
4175e948c56SGnoCiYeH     fn poll(&self, private_data: &FilePrivateData) -> Result<usize, SystemError> {
418dfe53cf0SGnoCiYeH         return self.inner.lock().poll(private_data);
4195e948c56SGnoCiYeH     }
420f678331aShanjiezhou }
421