sys_sync.rs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. use alloc::string::ToString;
  2. use alloc::vec::Vec;
  3. use crate::{
  4. arch::{
  5. interrupt::TrapFrame,
  6. syscall::nr::{SYS_SYNC, SYS_SYNCFS},
  7. },
  8. mm::page::page_reclaimer_lock_irqsave,
  9. syscall::table::{FormattedSyscallParam, Syscall},
  10. };
  11. /// sync() causes all pending modifications to filesystem metadata and
  12. /// cached file data to be written to the underlying filesystems.
  13. ///
  14. /// See https://man7.org/linux/man-pages/man2/sync.2.html
  15. pub struct SysSyncHandle;
  16. impl Syscall for SysSyncHandle {
  17. fn num_args(&self) -> usize {
  18. 0
  19. }
  20. fn handle(
  21. &self,
  22. _args: &[usize],
  23. _frame: &mut TrapFrame,
  24. ) -> Result<usize, system_error::SystemError> {
  25. page_reclaimer_lock_irqsave().flush_dirty_pages();
  26. Ok(0)
  27. }
  28. fn entry_format(&self, _args: &[usize]) -> Vec<FormattedSyscallParam> {
  29. vec![FormattedSyscallParam::new(
  30. "No arguments",
  31. "sync()".to_string(),
  32. )]
  33. }
  34. }
  35. syscall_table_macros::declare_syscall!(SYS_SYNC, SysSyncHandle);
  36. /// syncfs() is like sync(), but synchronizes just the filesystem
  37. /// containing file referred to by the open file descriptor fd.
  38. pub struct SysSyncFsHandle;
  39. impl Syscall for SysSyncFsHandle {
  40. fn num_args(&self) -> usize {
  41. 1
  42. }
  43. fn handle(
  44. &self,
  45. _args: &[usize],
  46. _frame: &mut TrapFrame,
  47. ) -> Result<usize, system_error::SystemError> {
  48. // TODO: now, we ignore the fd and sync all filesystems.
  49. // In the future, we should sync only the filesystem of the given fd.
  50. page_reclaimer_lock_irqsave().flush_dirty_pages();
  51. Ok(0)
  52. }
  53. fn entry_format(&self, args: &[usize]) -> Vec<FormattedSyscallParam> {
  54. vec![FormattedSyscallParam::new("fd", format!("{}", args[0]))]
  55. }
  56. }
  57. syscall_table_macros::declare_syscall!(SYS_SYNCFS, SysSyncHandle);