11d37ca6dSDonkey Kane use core::ffi::c_void; 282df0a13Shmt use core::mem::size_of; 32dbef785SGnoCiYeH 4b5b571e0SLoGin use alloc::string::ToString; 582df0a13Shmt use alloc::{string::String, sync::Arc, vec::Vec}; 691e9d4abSLoGin use system_error::SystemError; 7004e86ffSlogin 81d37ca6dSDonkey Kane use crate::producefs; 9004e86ffSlogin use crate::{ 10c566df45SLoGin driver::base::{block::SeekFrom, device::device_number::DeviceNumber}, 111d37ca6dSDonkey Kane filesystem::vfs::{core as Vcore, file::FileDescriptorVec}, 121d37ca6dSDonkey Kane kerror, 131496ba7bSLoGin libs::rwlock::RwLockWriteGuard, 144fda81ceSLoGin mm::{verify_area, VirtAddr}, 151496ba7bSLoGin process::ProcessManager, 16709498caSLoGin syscall::{ 171d37ca6dSDonkey Kane user_access::{self, check_and_clone_cstr, UserBufferWriter}, 1891e9d4abSLoGin Syscall, 19709498caSLoGin }, 206fc066acSJomo time::PosixTimeSpec, 21004e86ffSlogin }; 22004e86ffSlogin 23004e86ffSlogin use super::{ 241074eb34SSamuel Dai core::{do_mkdir_at, do_remove_dir, do_unlink_at}, 25709498caSLoGin fcntl::{AtFlags, FcntlCommand, FD_CLOEXEC}, 26004e86ffSlogin file::{File, FileMode}, 270fb515b0SLoGin open::{do_faccessat, do_fchmodat, do_sys_open}, 289b0abe6dSLoGin utils::{rsplit_path, user_path_at}, 291074eb34SSamuel Dai Dirent, FileType, IndexNode, SuperBlock, FSMAKER, MAX_PATHLEN, ROOT_INODE, 301074eb34SSamuel Dai VFS_MAX_FOLLOW_SYMLINK_TIMES, 31004e86ffSlogin }; 3240314b30SXiaoye Zheng // use crate::kdebug; 33004e86ffSlogin 34ab5c8ca4Slogin pub const SEEK_SET: u32 = 0; 35ab5c8ca4Slogin pub const SEEK_CUR: u32 = 1; 36ab5c8ca4Slogin pub const SEEK_END: u32 = 2; 37ab5c8ca4Slogin pub const SEEK_MAX: u32 = 3; 38004e86ffSlogin 3967b48188Shoumkh bitflags! { 4067b48188Shoumkh /// 文件类型和权限 416b4e7a29SLoGin #[repr(C)] 4267b48188Shoumkh pub struct ModeType: u32 { 4367b48188Shoumkh /// 掩码 4467b48188Shoumkh const S_IFMT = 0o0_170_000; 4567b48188Shoumkh /// 文件类型 4667b48188Shoumkh const S_IFSOCK = 0o140000; 4767b48188Shoumkh const S_IFLNK = 0o120000; 4867b48188Shoumkh const S_IFREG = 0o100000; 4967b48188Shoumkh const S_IFBLK = 0o060000; 5067b48188Shoumkh const S_IFDIR = 0o040000; 5167b48188Shoumkh const S_IFCHR = 0o020000; 5267b48188Shoumkh const S_IFIFO = 0o010000; 5367b48188Shoumkh 5467b48188Shoumkh const S_ISUID = 0o004000; 5567b48188Shoumkh const S_ISGID = 0o002000; 5667b48188Shoumkh const S_ISVTX = 0o001000; 5767b48188Shoumkh /// 文件用户权限 5867b48188Shoumkh const S_IRWXU = 0o0700; 5967b48188Shoumkh const S_IRUSR = 0o0400; 6067b48188Shoumkh const S_IWUSR = 0o0200; 6167b48188Shoumkh const S_IXUSR = 0o0100; 6267b48188Shoumkh /// 文件组权限 6367b48188Shoumkh const S_IRWXG = 0o0070; 6467b48188Shoumkh const S_IRGRP = 0o0040; 6567b48188Shoumkh const S_IWGRP = 0o0020; 6667b48188Shoumkh const S_IXGRP = 0o0010; 6767b48188Shoumkh /// 文件其他用户权限 6867b48188Shoumkh const S_IRWXO = 0o0007; 6967b48188Shoumkh const S_IROTH = 0o0004; 7067b48188Shoumkh const S_IWOTH = 0o0002; 7167b48188Shoumkh const S_IXOTH = 0o0001; 7206d5e247SLoGin 7306d5e247SLoGin /// 0o777 7406d5e247SLoGin const S_IRWXUGO = Self::S_IRWXU.bits | Self::S_IRWXG.bits | Self::S_IRWXO.bits; 7506d5e247SLoGin /// 0o7777 7606d5e247SLoGin const S_IALLUGO = Self::S_ISUID.bits | Self::S_ISGID.bits | Self::S_ISVTX.bits| Self::S_IRWXUGO.bits; 7706d5e247SLoGin /// 0o444 7806d5e247SLoGin const S_IRUGO = Self::S_IRUSR.bits | Self::S_IRGRP.bits | Self::S_IROTH.bits; 7906d5e247SLoGin /// 0o222 8006d5e247SLoGin const S_IWUGO = Self::S_IWUSR.bits | Self::S_IWGRP.bits | Self::S_IWOTH.bits; 8106d5e247SLoGin /// 0o111 8206d5e247SLoGin const S_IXUGO = Self::S_IXUSR.bits | Self::S_IXGRP.bits | Self::S_IXOTH.bits; 8306d5e247SLoGin 8406d5e247SLoGin 8567b48188Shoumkh } 8667b48188Shoumkh } 8767b48188Shoumkh 8867b48188Shoumkh #[repr(C)] 8982df0a13Shmt #[derive(Clone, Copy)] 9067b48188Shoumkh /// # 文件信息结构体 9167b48188Shoumkh pub struct PosixKstat { 9267b48188Shoumkh /// 硬件设备ID 9367b48188Shoumkh dev_id: u64, 9467b48188Shoumkh /// inode号 9567b48188Shoumkh inode: u64, 9667b48188Shoumkh /// 硬链接数 9767b48188Shoumkh nlink: u64, 9867b48188Shoumkh /// 文件权限 9967b48188Shoumkh mode: ModeType, 10067b48188Shoumkh /// 所有者用户ID 10167b48188Shoumkh uid: i32, 10267b48188Shoumkh /// 所有者组ID 10367b48188Shoumkh gid: i32, 10467b48188Shoumkh /// 设备ID 10567b48188Shoumkh rdev: i64, 10667b48188Shoumkh /// 文件大小 10767b48188Shoumkh size: i64, 10867b48188Shoumkh /// 文件系统块大小 10967b48188Shoumkh blcok_size: i64, 11067b48188Shoumkh /// 分配的512B块数 11167b48188Shoumkh blocks: u64, 11267b48188Shoumkh /// 最后访问时间 1136fc066acSJomo atime: PosixTimeSpec, 11467b48188Shoumkh /// 最后修改时间 1156fc066acSJomo mtime: PosixTimeSpec, 11667b48188Shoumkh /// 最后状态变化时间 1176fc066acSJomo ctime: PosixTimeSpec, 11867b48188Shoumkh /// 用于填充结构体大小的空白数据 11967b48188Shoumkh pub _pad: [i8; 24], 12067b48188Shoumkh } 12167b48188Shoumkh impl PosixKstat { 12267b48188Shoumkh fn new() -> Self { 12367b48188Shoumkh Self { 12467b48188Shoumkh inode: 0, 12567b48188Shoumkh dev_id: 0, 12667b48188Shoumkh mode: ModeType { bits: 0 }, 12767b48188Shoumkh nlink: 0, 12867b48188Shoumkh uid: 0, 12967b48188Shoumkh gid: 0, 13067b48188Shoumkh rdev: 0, 13167b48188Shoumkh size: 0, 1326fc066acSJomo atime: PosixTimeSpec { 13367b48188Shoumkh tv_sec: 0, 13467b48188Shoumkh tv_nsec: 0, 13567b48188Shoumkh }, 1366fc066acSJomo mtime: PosixTimeSpec { 13767b48188Shoumkh tv_sec: 0, 13867b48188Shoumkh tv_nsec: 0, 13967b48188Shoumkh }, 1406fc066acSJomo ctime: PosixTimeSpec { 14167b48188Shoumkh tv_sec: 0, 14267b48188Shoumkh tv_nsec: 0, 14367b48188Shoumkh }, 14467b48188Shoumkh blcok_size: 0, 14567b48188Shoumkh blocks: 0, 14667b48188Shoumkh _pad: Default::default(), 14767b48188Shoumkh } 14867b48188Shoumkh } 14967b48188Shoumkh } 1500fb515b0SLoGin 151b4eb05a1STTaq #[repr(C)] 152b4eb05a1STTaq #[derive(Clone, Copy)] 153b4eb05a1STTaq /// # 文件信息结构体X 154b4eb05a1STTaq pub struct PosixStatx { 155b4eb05a1STTaq /* 0x00 */ 156b4eb05a1STTaq stx_mask: PosixStatxMask, 157b4eb05a1STTaq /// 文件系统块大小 158b4eb05a1STTaq stx_blksize: u32, 159b4eb05a1STTaq /// Flags conveying information about the file [uncond] 160b4eb05a1STTaq stx_attributes: StxAttributes, 161b4eb05a1STTaq /* 0x10 */ 162b4eb05a1STTaq /// 硬链接数 163b4eb05a1STTaq stx_nlink: u32, 164b4eb05a1STTaq /// 所有者用户ID 165b4eb05a1STTaq stx_uid: u32, 166b4eb05a1STTaq /// 所有者组ID 167b4eb05a1STTaq stx_gid: u32, 168b4eb05a1STTaq /// 文件权限 169b4eb05a1STTaq stx_mode: ModeType, 170b4eb05a1STTaq 171b4eb05a1STTaq /* 0x20 */ 172b4eb05a1STTaq /// inode号 173b4eb05a1STTaq stx_inode: u64, 174b4eb05a1STTaq /// 文件大小 175b4eb05a1STTaq stx_size: i64, 176b4eb05a1STTaq /// 分配的512B块数 177b4eb05a1STTaq stx_blocks: u64, 178b4eb05a1STTaq /// Mask to show what's supported in stx_attributes 179b4eb05a1STTaq stx_attributes_mask: StxAttributes, 180b4eb05a1STTaq 181b4eb05a1STTaq /* 0x40 */ 182b4eb05a1STTaq /// 最后访问时间 1836fc066acSJomo stx_atime: PosixTimeSpec, 184b4eb05a1STTaq /// 文件创建时间 1856fc066acSJomo stx_btime: PosixTimeSpec, 186b4eb05a1STTaq /// 最后状态变化时间 1876fc066acSJomo stx_ctime: PosixTimeSpec, 188b4eb05a1STTaq /// 最后修改时间 1896fc066acSJomo stx_mtime: PosixTimeSpec, 190b4eb05a1STTaq 191b4eb05a1STTaq /* 0x80 */ 192b4eb05a1STTaq /// 主设备ID 193b4eb05a1STTaq stx_rdev_major: u32, 194b4eb05a1STTaq /// 次设备ID 195b4eb05a1STTaq stx_rdev_minor: u32, 196b4eb05a1STTaq /// 主硬件设备ID 197b4eb05a1STTaq stx_dev_major: u32, 198b4eb05a1STTaq /// 次硬件设备ID 199b4eb05a1STTaq stx_dev_minor: u32, 200b4eb05a1STTaq 201b4eb05a1STTaq /* 0x90 */ 202b4eb05a1STTaq stx_mnt_id: u64, 203b4eb05a1STTaq stx_dio_mem_align: u32, 204b4eb05a1STTaq stx_dio_offset_align: u32, 205b4eb05a1STTaq } 206b4eb05a1STTaq impl PosixStatx { 207b4eb05a1STTaq fn new() -> Self { 208b4eb05a1STTaq Self { 209b4eb05a1STTaq stx_mask: PosixStatxMask::STATX_BASIC_STATS, 210b4eb05a1STTaq stx_blksize: 0, 211b4eb05a1STTaq stx_attributes: StxAttributes::STATX_ATTR_APPEND, 212b4eb05a1STTaq stx_nlink: 0, 213b4eb05a1STTaq stx_uid: 0, 214b4eb05a1STTaq stx_gid: 0, 215b4eb05a1STTaq stx_mode: ModeType { bits: 0 }, 216b4eb05a1STTaq stx_inode: 0, 217b4eb05a1STTaq stx_size: 0, 218b4eb05a1STTaq stx_blocks: 0, 219b4eb05a1STTaq stx_attributes_mask: StxAttributes::STATX_ATTR_APPEND, 2206fc066acSJomo stx_atime: PosixTimeSpec { 221b4eb05a1STTaq tv_sec: 0, 222b4eb05a1STTaq tv_nsec: 0, 223b4eb05a1STTaq }, 2246fc066acSJomo stx_btime: PosixTimeSpec { 225b4eb05a1STTaq tv_sec: 0, 226b4eb05a1STTaq tv_nsec: 0, 227b4eb05a1STTaq }, 2286fc066acSJomo stx_ctime: PosixTimeSpec { 229b4eb05a1STTaq tv_sec: 0, 230b4eb05a1STTaq tv_nsec: 0, 231b4eb05a1STTaq }, 2326fc066acSJomo stx_mtime: PosixTimeSpec { 233b4eb05a1STTaq tv_sec: 0, 234b4eb05a1STTaq tv_nsec: 0, 235b4eb05a1STTaq }, 236b4eb05a1STTaq stx_rdev_major: 0, 237b4eb05a1STTaq stx_rdev_minor: 0, 238b4eb05a1STTaq stx_dev_major: 0, 239b4eb05a1STTaq stx_dev_minor: 0, 240b4eb05a1STTaq stx_mnt_id: 0, 241b4eb05a1STTaq stx_dio_mem_align: 0, 242b4eb05a1STTaq stx_dio_offset_align: 0, 243b4eb05a1STTaq } 244b4eb05a1STTaq } 245b4eb05a1STTaq } 246b4eb05a1STTaq 247b4eb05a1STTaq bitflags! { 248b4eb05a1STTaq pub struct PosixStatxMask: u32{ 249b4eb05a1STTaq /// Want stx_mode & S_IFMT 250b4eb05a1STTaq const STATX_TYPE = 0x00000001; 251b4eb05a1STTaq 252b4eb05a1STTaq /// Want stx_mode & ~S_IFMT 253b4eb05a1STTaq const STATX_MODE = 0x00000002; 254b4eb05a1STTaq 255b4eb05a1STTaq /// Want stx_nlink 256b4eb05a1STTaq const STATX_NLINK = 0x00000004; 257b4eb05a1STTaq 258b4eb05a1STTaq /// Want stx_uid 259b4eb05a1STTaq const STATX_UID = 0x00000008; 260b4eb05a1STTaq 261b4eb05a1STTaq /// Want stx_gid 262b4eb05a1STTaq const STATX_GID = 0x00000010; 263b4eb05a1STTaq 264b4eb05a1STTaq /// Want stx_atime 265b4eb05a1STTaq const STATX_ATIME = 0x00000020; 266b4eb05a1STTaq 267b4eb05a1STTaq /// Want stx_mtime 268b4eb05a1STTaq const STATX_MTIME = 0x00000040; 269b4eb05a1STTaq 270b4eb05a1STTaq /// Want stx_ctime 271b4eb05a1STTaq const STATX_CTIME = 0x00000080; 272b4eb05a1STTaq 273b4eb05a1STTaq /// Want stx_ino 274b4eb05a1STTaq const STATX_INO = 0x00000100; 275b4eb05a1STTaq 276b4eb05a1STTaq /// Want stx_size 277b4eb05a1STTaq const STATX_SIZE = 0x00000200; 278b4eb05a1STTaq 279b4eb05a1STTaq /// Want stx_blocks 280b4eb05a1STTaq const STATX_BLOCKS = 0x00000400; 281b4eb05a1STTaq 282b4eb05a1STTaq /// [All of the above] 283b4eb05a1STTaq const STATX_BASIC_STATS = 0x000007ff; 284b4eb05a1STTaq 285b4eb05a1STTaq /// Want stx_btime 286b4eb05a1STTaq const STATX_BTIME = 0x00000800; 287b4eb05a1STTaq 288b4eb05a1STTaq /// The same as STATX_BASIC_STATS | STATX_BTIME. 289b4eb05a1STTaq /// It is deprecated and should not be used. 290b4eb05a1STTaq const STATX_ALL = 0x00000fff; 291b4eb05a1STTaq 292b4eb05a1STTaq /// Want stx_mnt_id (since Linux 5.8) 293b4eb05a1STTaq const STATX_MNT_ID = 0x00001000; 294b4eb05a1STTaq 295b4eb05a1STTaq /// Want stx_dio_mem_align and stx_dio_offset_align 296b4eb05a1STTaq /// (since Linux 6.1; support varies by filesystem) 297b4eb05a1STTaq const STATX_DIOALIGN = 0x00002000; 298b4eb05a1STTaq 299b4eb05a1STTaq /// Reserved for future struct statx expansion 300b4eb05a1STTaq const STATX_RESERVED = 0x80000000; 301b4eb05a1STTaq } 302b4eb05a1STTaq } 303b4eb05a1STTaq 304b4eb05a1STTaq bitflags! { 305b4eb05a1STTaq pub struct StxAttributes: u64 { 306b4eb05a1STTaq /// 文件被文件系统压缩 307b4eb05a1STTaq const STATX_ATTR_COMPRESSED = 0x00000004; 308b4eb05a1STTaq /// 文件被标记为不可修改 309b4eb05a1STTaq const STATX_ATTR_IMMUTABLE = 0x00000010; 310b4eb05a1STTaq /// 文件是只追加写入的 311b4eb05a1STTaq const STATX_ATTR_APPEND = 0x00000020; 312b4eb05a1STTaq /// 文件不会被备份 313b4eb05a1STTaq const STATX_ATTR_NODUMP = 0x00000040; 314b4eb05a1STTaq /// 文件需要密钥才能在文件系统中解密 315b4eb05a1STTaq const STATX_ATTR_ENCRYPTED = 0x00000800; 316b4eb05a1STTaq /// 目录是自动挂载触发器 317b4eb05a1STTaq const STATX_ATTR_AUTOMOUNT = 0x00001000; 318b4eb05a1STTaq /// 目录是挂载点的根目录 319b4eb05a1STTaq const STATX_ATTR_MOUNT_ROOT = 0x00002000; 320b4eb05a1STTaq /// 文件受到 Verity 保护 321b4eb05a1STTaq const STATX_ATTR_VERITY = 0x00100000; 322b4eb05a1STTaq /// 文件当前处于 DAX 状态 CPU直接访问 323b4eb05a1STTaq const STATX_ATTR_DAX = 0x00200000; 324b4eb05a1STTaq } 325b4eb05a1STTaq } 326b4eb05a1STTaq 327597ecc08STTaq #[repr(C)] 328597ecc08STTaq #[derive(Debug, Clone, Copy)] 329597ecc08STTaq pub struct PosixStatfs { 330597ecc08STTaq f_type: u64, 331597ecc08STTaq f_bsize: u64, 332597ecc08STTaq f_blocks: u64, 333597ecc08STTaq f_bfree: u64, 334597ecc08STTaq f_bavail: u64, 335597ecc08STTaq f_files: u64, 336597ecc08STTaq f_ffree: u64, 337597ecc08STTaq f_fsid: u64, 338597ecc08STTaq f_namelen: u64, 339597ecc08STTaq f_frsize: u64, 340597ecc08STTaq f_flags: u64, 341597ecc08STTaq f_spare: [u64; 4], 342597ecc08STTaq } 343597ecc08STTaq 344597ecc08STTaq impl From<SuperBlock> for PosixStatfs { 345597ecc08STTaq fn from(super_block: SuperBlock) -> Self { 346597ecc08STTaq Self { 347597ecc08STTaq f_type: super_block.magic.bits, 348597ecc08STTaq f_bsize: super_block.bsize, 349597ecc08STTaq f_blocks: super_block.blocks, 350597ecc08STTaq f_bfree: super_block.bfree, 351597ecc08STTaq f_bavail: super_block.bavail, 352597ecc08STTaq f_files: super_block.files, 353597ecc08STTaq f_ffree: super_block.ffree, 354597ecc08STTaq f_fsid: super_block.fsid, 355597ecc08STTaq f_namelen: super_block.namelen, 356597ecc08STTaq f_frsize: super_block.frsize, 357597ecc08STTaq f_flags: super_block.flags, 358597ecc08STTaq f_spare: [0u64; 4], 359597ecc08STTaq } 360597ecc08STTaq } 361597ecc08STTaq } 3620fb515b0SLoGin /// 3630fb515b0SLoGin /// Arguments for how openat2(2) should open the target path. If only @flags and 3640fb515b0SLoGin /// @mode are non-zero, then openat2(2) operates very similarly to openat(2). 3650fb515b0SLoGin /// 3660fb515b0SLoGin /// However, unlike openat(2), unknown or invalid bits in @flags result in 3670fb515b0SLoGin /// -EINVAL rather than being silently ignored. @mode must be zero unless one of 3680fb515b0SLoGin /// {O_CREAT, O_TMPFILE} are set. 3690fb515b0SLoGin /// 3700fb515b0SLoGin /// ## 成员变量 3710fb515b0SLoGin /// 3720fb515b0SLoGin /// - flags: O_* flags. 3730fb515b0SLoGin /// - mode: O_CREAT/O_TMPFILE file mode. 3740fb515b0SLoGin /// - resolve: RESOLVE_* flags. 3750fb515b0SLoGin #[derive(Debug, Clone, Copy)] 3760fb515b0SLoGin #[repr(C)] 3770fb515b0SLoGin pub struct PosixOpenHow { 3780fb515b0SLoGin pub flags: u64, 3790fb515b0SLoGin pub mode: u64, 3800fb515b0SLoGin pub resolve: u64, 3810fb515b0SLoGin } 3820fb515b0SLoGin 3830fb515b0SLoGin impl PosixOpenHow { 3840fb515b0SLoGin #[allow(dead_code)] 3850fb515b0SLoGin pub fn new(flags: u64, mode: u64, resolve: u64) -> Self { 3860fb515b0SLoGin Self { 3870fb515b0SLoGin flags, 3880fb515b0SLoGin mode, 3890fb515b0SLoGin resolve, 3900fb515b0SLoGin } 3910fb515b0SLoGin } 3920fb515b0SLoGin } 3930fb515b0SLoGin 3940fb515b0SLoGin #[derive(Debug, Clone, Copy)] 3950fb515b0SLoGin pub struct OpenHow { 3960fb515b0SLoGin pub o_flags: FileMode, 3970fb515b0SLoGin pub mode: ModeType, 3980fb515b0SLoGin pub resolve: OpenHowResolve, 3990fb515b0SLoGin } 4000fb515b0SLoGin 4010fb515b0SLoGin impl OpenHow { 4020fb515b0SLoGin pub fn new(mut o_flags: FileMode, mut mode: ModeType, resolve: OpenHowResolve) -> Self { 4030fb515b0SLoGin if !o_flags.contains(FileMode::O_CREAT) { 4040fb515b0SLoGin mode = ModeType::empty(); 4050fb515b0SLoGin } 4060fb515b0SLoGin 4070fb515b0SLoGin if o_flags.contains(FileMode::O_PATH) { 4080fb515b0SLoGin o_flags = o_flags.intersection(FileMode::O_PATH_FLAGS); 4090fb515b0SLoGin } 4100fb515b0SLoGin 4110fb515b0SLoGin Self { 4120fb515b0SLoGin o_flags, 4130fb515b0SLoGin mode, 4140fb515b0SLoGin resolve, 4150fb515b0SLoGin } 4160fb515b0SLoGin } 4170fb515b0SLoGin } 4180fb515b0SLoGin 4190fb515b0SLoGin impl From<PosixOpenHow> for OpenHow { 4200fb515b0SLoGin fn from(posix_open_how: PosixOpenHow) -> Self { 4210fb515b0SLoGin let o_flags = FileMode::from_bits_truncate(posix_open_how.flags as u32); 4220fb515b0SLoGin let mode = ModeType::from_bits_truncate(posix_open_how.mode as u32); 423b5b571e0SLoGin let resolve = OpenHowResolve::from_bits_truncate(posix_open_how.resolve); 4240fb515b0SLoGin return Self::new(o_flags, mode, resolve); 4250fb515b0SLoGin } 4260fb515b0SLoGin } 4270fb515b0SLoGin 4280fb515b0SLoGin bitflags! { 4290fb515b0SLoGin pub struct OpenHowResolve: u64{ 4300fb515b0SLoGin /// Block mount-point crossings 4310fb515b0SLoGin /// (including bind-mounts). 4320fb515b0SLoGin const RESOLVE_NO_XDEV = 0x01; 4330fb515b0SLoGin 4340fb515b0SLoGin /// Block traversal through procfs-style 4350fb515b0SLoGin /// "magic-links" 4360fb515b0SLoGin const RESOLVE_NO_MAGICLINKS = 0x02; 4370fb515b0SLoGin 4380fb515b0SLoGin /// Block traversal through all symlinks 4390fb515b0SLoGin /// (implies OEXT_NO_MAGICLINKS) 4400fb515b0SLoGin const RESOLVE_NO_SYMLINKS = 0x04; 4410fb515b0SLoGin /// Block "lexical" trickery like 4420fb515b0SLoGin /// "..", symlinks, and absolute 4430fb515b0SLoGin const RESOLVE_BENEATH = 0x08; 4440fb515b0SLoGin /// Make all jumps to "/" and ".." 4450fb515b0SLoGin /// be scoped inside the dirfd 4460fb515b0SLoGin /// (similar to chroot(2)). 4470fb515b0SLoGin const RESOLVE_IN_ROOT = 0x10; 4480fb515b0SLoGin // Only complete if resolution can be 4490fb515b0SLoGin // completed through cached lookup. May 4500fb515b0SLoGin // return -EAGAIN if that's not 4510fb515b0SLoGin // possible. 4520fb515b0SLoGin const RESOLVE_CACHED = 0x20; 4530fb515b0SLoGin } 4540fb515b0SLoGin } 4551074eb34SSamuel Dai 4561074eb34SSamuel Dai bitflags! { 4571074eb34SSamuel Dai pub struct UmountFlag: i32 { 4581074eb34SSamuel Dai const DEFAULT = 0; /* Default call to umount. */ 4591074eb34SSamuel Dai const MNT_FORCE = 1; /* Force unmounting. */ 4601074eb34SSamuel Dai const MNT_DETACH = 2; /* Just detach from the tree. */ 4611074eb34SSamuel Dai const MNT_EXPIRE = 4; /* Mark for expiry. */ 4621074eb34SSamuel Dai const UMOUNT_NOFOLLOW = 8; /* Don't follow symlink on umount. */ 4631074eb34SSamuel Dai } 4641074eb34SSamuel Dai } 4651074eb34SSamuel Dai 466ab5c8ca4Slogin impl Syscall { 467ab5c8ca4Slogin /// @brief 为当前进程打开一个文件 468ab5c8ca4Slogin /// 469ab5c8ca4Slogin /// @param path 文件路径 470ab5c8ca4Slogin /// @param o_flags 打开文件的标志位 471ab5c8ca4Slogin /// 472ab5c8ca4Slogin /// @return 文件描述符编号,或者是错误码 4730fb515b0SLoGin pub fn open( 47482df0a13Shmt path: *const u8, 47582df0a13Shmt o_flags: u32, 47682df0a13Shmt mode: u32, 4770fb515b0SLoGin follow_symlink: bool, 4780fb515b0SLoGin ) -> Result<usize, SystemError> { 47982df0a13Shmt let path = check_and_clone_cstr(path, Some(MAX_PATHLEN))?; 48082df0a13Shmt let open_flags: FileMode = FileMode::from_bits(o_flags).ok_or(SystemError::EINVAL)?; 481b5b571e0SLoGin let mode = ModeType::from_bits(mode).ok_or(SystemError::EINVAL)?; 48282df0a13Shmt return do_sys_open( 48382df0a13Shmt AtFlags::AT_FDCWD.bits(), 48482df0a13Shmt &path, 48582df0a13Shmt open_flags, 48682df0a13Shmt mode, 48782df0a13Shmt follow_symlink, 48882df0a13Shmt ); 489ab5c8ca4Slogin } 490ab5c8ca4Slogin 4910fb515b0SLoGin pub fn openat( 4920fb515b0SLoGin dirfd: i32, 49382df0a13Shmt path: *const u8, 49482df0a13Shmt o_flags: u32, 49582df0a13Shmt mode: u32, 4960fb515b0SLoGin follow_symlink: bool, 4970fb515b0SLoGin ) -> Result<usize, SystemError> { 49882df0a13Shmt let path = check_and_clone_cstr(path, Some(MAX_PATHLEN))?; 49982df0a13Shmt let open_flags: FileMode = FileMode::from_bits(o_flags).ok_or(SystemError::EINVAL)?; 500b5b571e0SLoGin let mode = ModeType::from_bits(mode).ok_or(SystemError::EINVAL)?; 50182df0a13Shmt return do_sys_open(dirfd, &path, open_flags, mode, follow_symlink); 502ab5c8ca4Slogin } 503ab5c8ca4Slogin 504ab5c8ca4Slogin /// @brief 关闭文件 505ab5c8ca4Slogin /// 506ab5c8ca4Slogin /// @param fd 文件描述符编号 507ab5c8ca4Slogin /// 508ab5c8ca4Slogin /// @return 成功返回0,失败返回错误码 509ab5c8ca4Slogin pub fn close(fd: usize) -> Result<usize, SystemError> { 5101496ba7bSLoGin let binding = ProcessManager::current_pcb().fd_table(); 5111496ba7bSLoGin let mut fd_table_guard = binding.write(); 5121496ba7bSLoGin 5136046f775S裕依 fd_table_guard.drop_fd(fd as i32).map(|_| 0) 514ab5c8ca4Slogin } 515ab5c8ca4Slogin 51640314b30SXiaoye Zheng /// @brief 发送命令到文件描述符对应的设备, 51740314b30SXiaoye Zheng /// 51840314b30SXiaoye Zheng /// @param fd 文件描述符编号 51940314b30SXiaoye Zheng /// @param cmd 设备相关的请求类型 52040314b30SXiaoye Zheng /// 52140314b30SXiaoye Zheng /// @return Ok(usize) 成功返回0 52240314b30SXiaoye Zheng /// @return Err(SystemError) 读取失败,返回posix错误码 52340314b30SXiaoye Zheng pub fn ioctl(fd: usize, cmd: u32, data: usize) -> Result<usize, SystemError> { 52440314b30SXiaoye Zheng let binding = ProcessManager::current_pcb().fd_table(); 52540314b30SXiaoye Zheng let fd_table_guard = binding.read(); 52640314b30SXiaoye Zheng 52740314b30SXiaoye Zheng let file = fd_table_guard 52840314b30SXiaoye Zheng .get_file_by_fd(fd as i32) 52940314b30SXiaoye Zheng .ok_or(SystemError::EBADF)?; 53040314b30SXiaoye Zheng 53140314b30SXiaoye Zheng // drop guard 以避免无法调度的问题 53240314b30SXiaoye Zheng drop(fd_table_guard); 533dfe53cf0SGnoCiYeH let r = file.inode().ioctl(cmd, data, &file.private_data.lock()); 53440314b30SXiaoye Zheng return r; 53540314b30SXiaoye Zheng } 53640314b30SXiaoye Zheng 537ab5c8ca4Slogin /// @brief 根据文件描述符,读取文件数据。尝试读取的数据长度与buf的长度相同。 538ab5c8ca4Slogin /// 539ab5c8ca4Slogin /// @param fd 文件描述符编号 54027b967a3S裕依 /// @param buf 输出缓冲区 541ab5c8ca4Slogin /// 542ab5c8ca4Slogin /// @return Ok(usize) 成功读取的数据的字节数 543ab5c8ca4Slogin /// @return Err(SystemError) 读取失败,返回posix错误码 544ab5c8ca4Slogin pub fn read(fd: i32, buf: &mut [u8]) -> Result<usize, SystemError> { 5451496ba7bSLoGin let binding = ProcessManager::current_pcb().fd_table(); 5461496ba7bSLoGin let fd_table_guard = binding.read(); 5471496ba7bSLoGin 5481496ba7bSLoGin let file = fd_table_guard.get_file_by_fd(fd); 549ab5c8ca4Slogin if file.is_none() { 550ab5c8ca4Slogin return Err(SystemError::EBADF); 551ab5c8ca4Slogin } 5521496ba7bSLoGin // drop guard 以避免无法调度的问题 5531496ba7bSLoGin drop(fd_table_guard); 5541496ba7bSLoGin let file = file.unwrap(); 555ab5c8ca4Slogin 556dfe53cf0SGnoCiYeH return file.read(buf.len(), buf); 557ab5c8ca4Slogin } 558ab5c8ca4Slogin 559ab5c8ca4Slogin /// @brief 根据文件描述符,向文件写入数据。尝试写入的数据长度与buf的长度相同。 560ab5c8ca4Slogin /// 561ab5c8ca4Slogin /// @param fd 文件描述符编号 56227b967a3S裕依 /// @param buf 输入缓冲区 563ab5c8ca4Slogin /// 564ab5c8ca4Slogin /// @return Ok(usize) 成功写入的数据的字节数 565ab5c8ca4Slogin /// @return Err(SystemError) 写入失败,返回posix错误码 566ab5c8ca4Slogin pub fn write(fd: i32, buf: &[u8]) -> Result<usize, SystemError> { 5671496ba7bSLoGin let binding = ProcessManager::current_pcb().fd_table(); 5681496ba7bSLoGin let fd_table_guard = binding.read(); 569ab5c8ca4Slogin 5701496ba7bSLoGin let file = fd_table_guard 5711496ba7bSLoGin .get_file_by_fd(fd) 5721496ba7bSLoGin .ok_or(SystemError::EBADF)?; 5731496ba7bSLoGin 5741496ba7bSLoGin // drop guard 以避免无法调度的问题 5751496ba7bSLoGin drop(fd_table_guard); 576dfe53cf0SGnoCiYeH return file.write(buf.len(), buf); 577ab5c8ca4Slogin } 578ab5c8ca4Slogin 579ab5c8ca4Slogin /// @brief 调整文件操作指针的位置 580ab5c8ca4Slogin /// 581ab5c8ca4Slogin /// @param fd 文件描述符编号 582ab5c8ca4Slogin /// @param seek 调整的方式 583ab5c8ca4Slogin /// 584ab5c8ca4Slogin /// @return Ok(usize) 调整后,文件访问指针相对于文件头部的偏移量 585ab5c8ca4Slogin /// @return Err(SystemError) 调整失败,返回posix错误码 58682df0a13Shmt pub fn lseek(fd: i32, offset: i64, seek: u32) -> Result<usize, SystemError> { 58782df0a13Shmt let seek = match seek { 58882df0a13Shmt SEEK_SET => Ok(SeekFrom::SeekSet(offset)), 58982df0a13Shmt SEEK_CUR => Ok(SeekFrom::SeekCurrent(offset)), 59082df0a13Shmt SEEK_END => Ok(SeekFrom::SeekEnd(offset)), 59182df0a13Shmt SEEK_MAX => Ok(SeekFrom::SeekEnd(0)), 59282df0a13Shmt _ => Err(SystemError::EINVAL), 59382df0a13Shmt }?; 59482df0a13Shmt 5951496ba7bSLoGin let binding = ProcessManager::current_pcb().fd_table(); 5961496ba7bSLoGin let fd_table_guard = binding.read(); 5971496ba7bSLoGin let file = fd_table_guard 5981496ba7bSLoGin .get_file_by_fd(fd) 5991496ba7bSLoGin .ok_or(SystemError::EBADF)?; 6001496ba7bSLoGin 6011496ba7bSLoGin // drop guard 以避免无法调度的问题 6021496ba7bSLoGin drop(fd_table_guard); 603dfe53cf0SGnoCiYeH return file.lseek(seek); 604004e86ffSlogin } 605004e86ffSlogin 60627b967a3S裕依 /// # sys_pread64 系统调用的实际执行函数 60727b967a3S裕依 /// 60827b967a3S裕依 /// ## 参数 60927b967a3S裕依 /// - `fd`: 文件描述符 61027b967a3S裕依 /// - `buf`: 读出缓冲区 61127b967a3S裕依 /// - `len`: 要读取的字节数 61227b967a3S裕依 /// - `offset`: 文件偏移量 61327b967a3S裕依 pub fn pread(fd: i32, buf: &mut [u8], len: usize, offset: usize) -> Result<usize, SystemError> { 61427b967a3S裕依 let binding = ProcessManager::current_pcb().fd_table(); 61527b967a3S裕依 let fd_table_guard = binding.read(); 61627b967a3S裕依 61727b967a3S裕依 let file = fd_table_guard.get_file_by_fd(fd); 61827b967a3S裕依 if file.is_none() { 61927b967a3S裕依 return Err(SystemError::EBADF); 62027b967a3S裕依 } 62127b967a3S裕依 // drop guard 以避免无法调度的问题 62227b967a3S裕依 drop(fd_table_guard); 62327b967a3S裕依 let file = file.unwrap(); 62427b967a3S裕依 625dfe53cf0SGnoCiYeH return file.pread(offset, len, buf); 62627b967a3S裕依 } 62727b967a3S裕依 62827b967a3S裕依 /// # sys_pwrite64 系统调用的实际执行函数 62927b967a3S裕依 /// 63027b967a3S裕依 /// ## 参数 63127b967a3S裕依 /// - `fd`: 文件描述符 63227b967a3S裕依 /// - `buf`: 写入缓冲区 63327b967a3S裕依 /// - `len`: 要写入的字节数 63427b967a3S裕依 /// - `offset`: 文件偏移量 63527b967a3S裕依 pub fn pwrite(fd: i32, buf: &[u8], len: usize, offset: usize) -> Result<usize, SystemError> { 63627b967a3S裕依 let binding = ProcessManager::current_pcb().fd_table(); 63727b967a3S裕依 let fd_table_guard = binding.read(); 63827b967a3S裕依 63927b967a3S裕依 let file = fd_table_guard.get_file_by_fd(fd); 64027b967a3S裕依 if file.is_none() { 64127b967a3S裕依 return Err(SystemError::EBADF); 64227b967a3S裕依 } 64327b967a3S裕依 // drop guard 以避免无法调度的问题 64427b967a3S裕依 drop(fd_table_guard); 64527b967a3S裕依 let file = file.unwrap(); 64627b967a3S裕依 647dfe53cf0SGnoCiYeH return file.pwrite(offset, len, buf); 64827b967a3S裕依 } 64927b967a3S裕依 650004e86ffSlogin /// @brief 切换工作目录 651004e86ffSlogin /// 652004e86ffSlogin /// @param dest_path 目标路径 653004e86ffSlogin /// 654004e86ffSlogin /// @return 返回码 描述 655004e86ffSlogin /// 0 | 成功 656004e86ffSlogin /// 657004e86ffSlogin /// EACCESS | 权限不足 658004e86ffSlogin /// 659004e86ffSlogin /// ELOOP | 解析path时遇到路径循环 660004e86ffSlogin /// 661004e86ffSlogin /// ENAMETOOLONG | 路径名过长 662004e86ffSlogin /// 663004e86ffSlogin /// ENOENT | 目标文件或目录不存在 664004e86ffSlogin /// 665004e86ffSlogin /// ENODIR | 检索期间发现非目录项 666004e86ffSlogin /// 667004e86ffSlogin /// ENOMEM | 系统内存不足 668004e86ffSlogin /// 669004e86ffSlogin /// EFAULT | 错误的地址 670004e86ffSlogin /// 671004e86ffSlogin /// ENAMETOOLONG | 路径过长 67282df0a13Shmt pub fn chdir(path: *const u8) -> Result<usize, SystemError> { 67382df0a13Shmt if path.is_null() { 67482df0a13Shmt return Err(SystemError::EFAULT); 67582df0a13Shmt } 67682df0a13Shmt 67782df0a13Shmt let path = check_and_clone_cstr(path, Some(MAX_PATHLEN))?; 6781496ba7bSLoGin let proc = ProcessManager::current_pcb(); 679ab5c8ca4Slogin // Copy path to kernel space to avoid some security issues 6801496ba7bSLoGin let mut new_path = String::from(""); 681b5b571e0SLoGin if !path.is_empty() { 6821496ba7bSLoGin let cwd = match path.as_bytes()[0] { 6831496ba7bSLoGin b'/' => String::from("/"), 6841496ba7bSLoGin _ => proc.basic().cwd(), 6851496ba7bSLoGin }; 686b5b571e0SLoGin let mut cwd_vec: Vec<_> = cwd.split('/').filter(|&x| !x.is_empty()).collect(); 687b5b571e0SLoGin let path_split = path.split('/').filter(|&x| !x.is_empty()); 6881496ba7bSLoGin for seg in path_split { 6891496ba7bSLoGin if seg == ".." { 6901496ba7bSLoGin cwd_vec.pop(); 6911496ba7bSLoGin } else if seg == "." { 6921496ba7bSLoGin // 当前目录 6931496ba7bSLoGin } else { 6941496ba7bSLoGin cwd_vec.push(seg); 6951496ba7bSLoGin } 6961496ba7bSLoGin } 6971496ba7bSLoGin //proc.basic().set_path(String::from("")); 6981496ba7bSLoGin for seg in cwd_vec { 699b5b571e0SLoGin new_path.push('/'); 7001496ba7bSLoGin new_path.push_str(seg); 7011496ba7bSLoGin } 702b5b571e0SLoGin if new_path.is_empty() { 7031496ba7bSLoGin new_path = String::from("/"); 7041496ba7bSLoGin } 7051496ba7bSLoGin } 706a03c4f9dSLoGin let inode = 707a03c4f9dSLoGin match ROOT_INODE().lookup_follow_symlink(&new_path, VFS_MAX_FOLLOW_SYMLINK_TIMES) { 70882df0a13Shmt Err(_) => { 709ab5c8ca4Slogin return Err(SystemError::ENOENT); 710004e86ffSlogin } 711004e86ffSlogin Ok(i) => i, 712004e86ffSlogin }; 7131496ba7bSLoGin let metadata = inode.metadata()?; 7141496ba7bSLoGin if metadata.file_type == FileType::Dir { 715b5b571e0SLoGin proc.basic_mut().set_cwd(new_path); 716ab5c8ca4Slogin return Ok(0); 717004e86ffSlogin } else { 718ab5c8ca4Slogin return Err(SystemError::ENOTDIR); 719004e86ffSlogin } 720004e86ffSlogin } 7211496ba7bSLoGin 7221496ba7bSLoGin /// @brief 获取当前进程的工作目录路径 7231496ba7bSLoGin /// 7241496ba7bSLoGin /// @param buf 指向缓冲区的指针 7251496ba7bSLoGin /// @param size 缓冲区的大小 7261496ba7bSLoGin /// 7271496ba7bSLoGin /// @return 成功,返回的指针指向包含工作目录路径的字符串 7281496ba7bSLoGin /// @return 错误,没有足够的空间 7291496ba7bSLoGin pub fn getcwd(buf: &mut [u8]) -> Result<VirtAddr, SystemError> { 7301496ba7bSLoGin let proc = ProcessManager::current_pcb(); 7311496ba7bSLoGin let cwd = proc.basic().cwd(); 7321496ba7bSLoGin 7331496ba7bSLoGin let cwd_bytes = cwd.as_bytes(); 7341496ba7bSLoGin let cwd_len = cwd_bytes.len(); 7351496ba7bSLoGin if cwd_len + 1 > buf.len() { 7361496ba7bSLoGin return Err(SystemError::ENOMEM); 737004e86ffSlogin } 7381496ba7bSLoGin buf[..cwd_len].copy_from_slice(cwd_bytes); 7391496ba7bSLoGin buf[cwd_len] = 0; 7401496ba7bSLoGin 7411496ba7bSLoGin return Ok(VirtAddr::new(buf.as_ptr() as usize)); 742004e86ffSlogin } 743004e86ffSlogin 744004e86ffSlogin /// @brief 获取目录中的数据 745004e86ffSlogin /// 746ab5c8ca4Slogin /// TODO: 这个函数的语义与Linux不一致,需要修改!!! 747ab5c8ca4Slogin /// 748004e86ffSlogin /// @param fd 文件描述符号 749ab5c8ca4Slogin /// @param buf 输出缓冲区 750ab5c8ca4Slogin /// 751ab5c8ca4Slogin /// @return 成功返回读取的字节数,失败返回错误码 752ab5c8ca4Slogin pub fn getdents(fd: i32, buf: &mut [u8]) -> Result<usize, SystemError> { 753ab5c8ca4Slogin let dirent = 754ab5c8ca4Slogin unsafe { (buf.as_mut_ptr() as *mut Dirent).as_mut() }.ok_or(SystemError::EFAULT)?; 755004e86ffSlogin 75646e234aeSLoGin if fd < 0 || fd as usize > FileDescriptorVec::PROCESS_MAX_FD { 757ab5c8ca4Slogin return Err(SystemError::EBADF); 758004e86ffSlogin } 759004e86ffSlogin 760004e86ffSlogin // 获取fd 7611496ba7bSLoGin let binding = ProcessManager::current_pcb().fd_table(); 7621496ba7bSLoGin let fd_table_guard = binding.read(); 7631496ba7bSLoGin let file = fd_table_guard 7641496ba7bSLoGin .get_file_by_fd(fd) 7651496ba7bSLoGin .ok_or(SystemError::EBADF)?; 766004e86ffSlogin 7671496ba7bSLoGin // drop guard 以避免无法调度的问题 7681496ba7bSLoGin drop(fd_table_guard); 76934e6d6c8Syuyi2439 770dfe53cf0SGnoCiYeH let res = file.readdir(dirent).map(|x| x as usize); 77134e6d6c8Syuyi2439 77234e6d6c8Syuyi2439 return res; 773004e86ffSlogin } 774004e86ffSlogin 775004e86ffSlogin /// @brief 创建文件夹 776004e86ffSlogin /// 777004e86ffSlogin /// @param path(r8) 路径 / mode(r9) 模式 778004e86ffSlogin /// 779004e86ffSlogin /// @return uint64_t 负数错误码 / 0表示成功 78082df0a13Shmt pub fn mkdir(path: *const u8, mode: usize) -> Result<usize, SystemError> { 78182df0a13Shmt let path = check_and_clone_cstr(path, Some(MAX_PATHLEN))?; 7821074eb34SSamuel Dai do_mkdir_at( 7831074eb34SSamuel Dai AtFlags::AT_FDCWD.bits(), 7841074eb34SSamuel Dai &path, 7851074eb34SSamuel Dai FileMode::from_bits_truncate(mode as u32), 7861074eb34SSamuel Dai )?; 7871074eb34SSamuel Dai return Ok(0); 788004e86ffSlogin } 789004e86ffSlogin 7904695947eSChenzx /// **创建硬连接的系统调用** 7914695947eSChenzx /// 7924695947eSChenzx /// ## 参数 7934695947eSChenzx /// 7944695947eSChenzx /// - 'oldfd': 用于解析源文件路径的文件描述符 7954695947eSChenzx /// - 'old': 源文件路径 7964695947eSChenzx /// - 'newfd': 用于解析新文件路径的文件描述符 7974695947eSChenzx /// - 'new': 新文件将创建的路径 7984695947eSChenzx /// - 'flags': 标志位,仅以位或方式包含AT_EMPTY_PATH和AT_SYMLINK_FOLLOW 7994695947eSChenzx /// 8004695947eSChenzx /// 8014695947eSChenzx pub fn do_linkat( 8024695947eSChenzx oldfd: i32, 8034695947eSChenzx old: &str, 8044695947eSChenzx newfd: i32, 8054695947eSChenzx new: &str, 8064695947eSChenzx flags: AtFlags, 8074695947eSChenzx ) -> Result<usize, SystemError> { 8084695947eSChenzx // flag包含其他未规定值时返回EINVAL 8094695947eSChenzx if !(AtFlags::AT_EMPTY_PATH | AtFlags::AT_SYMLINK_FOLLOW).contains(flags) { 8104695947eSChenzx return Err(SystemError::EINVAL); 8114695947eSChenzx } 8124695947eSChenzx // TODO AT_EMPTY_PATH标志启用时,进行调用者CAP_DAC_READ_SEARCH或相似的检查 8134695947eSChenzx let symlink_times = if flags.contains(AtFlags::AT_SYMLINK_FOLLOW) { 8144e4c8c41SLoGin 0_usize 8154695947eSChenzx } else { 8164695947eSChenzx VFS_MAX_FOLLOW_SYMLINK_TIMES 8174695947eSChenzx }; 8184695947eSChenzx let pcb = ProcessManager::current_pcb(); 8194695947eSChenzx 8204695947eSChenzx // 得到源路径的inode 8214695947eSChenzx let old_inode: Arc<dyn IndexNode> = if old.is_empty() { 8224695947eSChenzx if flags.contains(AtFlags::AT_EMPTY_PATH) { 8234695947eSChenzx // 在AT_EMPTY_PATH启用时,old可以为空,old_inode实际为oldfd所指文件,但该文件不能为目录。 8244695947eSChenzx let binding = pcb.fd_table(); 8254695947eSChenzx let fd_table_guard = binding.read(); 8264695947eSChenzx let file = fd_table_guard 8274695947eSChenzx .get_file_by_fd(oldfd) 8284695947eSChenzx .ok_or(SystemError::EBADF)?; 829dfe53cf0SGnoCiYeH let old_inode = file.inode(); 8304695947eSChenzx old_inode 8314695947eSChenzx } else { 8324695947eSChenzx return Err(SystemError::ENONET); 8334695947eSChenzx } 8344695947eSChenzx } else { 8354695947eSChenzx let (old_begin_inode, old_remain_path) = user_path_at(&pcb, oldfd, old)?; 8364695947eSChenzx old_begin_inode.lookup_follow_symlink(&old_remain_path, symlink_times)? 8374695947eSChenzx }; 8384695947eSChenzx 8394695947eSChenzx // old_inode为目录时返回EPERM 8404695947eSChenzx if old_inode.metadata().unwrap().file_type == FileType::Dir { 8414695947eSChenzx return Err(SystemError::EPERM); 8424695947eSChenzx } 8434695947eSChenzx 8444695947eSChenzx // 得到新创建节点的父节点 8454695947eSChenzx let (new_begin_inode, new_remain_path) = user_path_at(&pcb, newfd, new)?; 8464695947eSChenzx let (new_name, new_parent_path) = rsplit_path(&new_remain_path); 8474e4c8c41SLoGin let new_parent = 8484e4c8c41SLoGin new_begin_inode.lookup_follow_symlink(new_parent_path.unwrap_or("/"), symlink_times)?; 8494695947eSChenzx 8504695947eSChenzx // 被调用者利用downcast_ref判断两inode是否为同一文件系统 8514e4c8c41SLoGin return new_parent.link(new_name, &old_inode).map(|_| 0); 8524695947eSChenzx } 8534695947eSChenzx 8544695947eSChenzx pub fn link(old: *const u8, new: *const u8) -> Result<usize, SystemError> { 8554695947eSChenzx let get_path = |cstr: *const u8| -> Result<String, SystemError> { 8564695947eSChenzx let res = check_and_clone_cstr(cstr, Some(MAX_PATHLEN))?; 8574695947eSChenzx if res.len() >= MAX_PATHLEN { 8584695947eSChenzx return Err(SystemError::ENAMETOOLONG); 8594695947eSChenzx } 8604695947eSChenzx if res.is_empty() { 8614695947eSChenzx return Err(SystemError::ENOENT); 8624695947eSChenzx } 8634695947eSChenzx Ok(res) 8644695947eSChenzx }; 8654695947eSChenzx let old = get_path(old)?; 8664695947eSChenzx let new = get_path(new)?; 8674695947eSChenzx return Self::do_linkat( 8684e4c8c41SLoGin AtFlags::AT_FDCWD.bits(), 8694695947eSChenzx &old, 8704e4c8c41SLoGin AtFlags::AT_FDCWD.bits(), 8714695947eSChenzx &new, 8724695947eSChenzx AtFlags::empty(), 8734695947eSChenzx ); 8744695947eSChenzx } 8754695947eSChenzx 8764695947eSChenzx pub fn linkat( 8774695947eSChenzx oldfd: i32, 8784695947eSChenzx old: *const u8, 8794695947eSChenzx newfd: i32, 8804695947eSChenzx new: *const u8, 8814695947eSChenzx flags: i32, 8824695947eSChenzx ) -> Result<usize, SystemError> { 8834695947eSChenzx let old = check_and_clone_cstr(old, Some(MAX_PATHLEN))?; 8844695947eSChenzx let new = check_and_clone_cstr(new, Some(MAX_PATHLEN))?; 8854695947eSChenzx if old.len() >= MAX_PATHLEN || new.len() >= MAX_PATHLEN { 8864695947eSChenzx return Err(SystemError::ENAMETOOLONG); 8874695947eSChenzx } 8884695947eSChenzx // old 根据flags & AtFlags::AT_EMPTY_PATH判空 8894695947eSChenzx if new.is_empty() { 8904695947eSChenzx return Err(SystemError::ENOENT); 8914695947eSChenzx } 8924695947eSChenzx let flags = AtFlags::from_bits(flags).ok_or(SystemError::EINVAL)?; 8934695947eSChenzx Self::do_linkat(oldfd, &old, newfd, &new, flags) 8944695947eSChenzx } 8954695947eSChenzx 896ab5c8ca4Slogin /// **删除文件夹、取消文件的链接、删除文件的系统调用** 897004e86ffSlogin /// 898ab5c8ca4Slogin /// ## 参数 899004e86ffSlogin /// 900ab5c8ca4Slogin /// - `dirfd`:文件夹的文件描述符.目前暂未实现 901ab5c8ca4Slogin /// - `pathname`:文件夹的路径 902ab5c8ca4Slogin /// - `flags`:标志位 903004e86ffSlogin /// 904004e86ffSlogin /// 90582df0a13Shmt pub fn unlinkat(dirfd: i32, path: *const u8, flags: u32) -> Result<usize, SystemError> { 906bf4a4899SLoGin let flags = AtFlags::from_bits(flags as i32).ok_or(SystemError::EINVAL)?; 907004e86ffSlogin 90882df0a13Shmt let path = check_and_clone_cstr(path, Some(MAX_PATHLEN))?; 90982df0a13Shmt 910bf4a4899SLoGin if flags.contains(AtFlags::AT_REMOVEDIR) { 911004e86ffSlogin // kdebug!("rmdir"); 91282df0a13Shmt match do_remove_dir(dirfd, &path) { 913004e86ffSlogin Err(err) => { 914ab5c8ca4Slogin return Err(err); 915004e86ffSlogin } 916004e86ffSlogin Ok(_) => { 917ab5c8ca4Slogin return Ok(0); 918004e86ffSlogin } 919004e86ffSlogin } 920004e86ffSlogin } 921004e86ffSlogin 92282df0a13Shmt match do_unlink_at(dirfd, &path) { 923004e86ffSlogin Err(err) => { 924ab5c8ca4Slogin return Err(err); 925004e86ffSlogin } 926004e86ffSlogin Ok(_) => { 927ab5c8ca4Slogin return Ok(0); 928004e86ffSlogin } 929004e86ffSlogin } 930004e86ffSlogin } 9312b771e32SGou Ngai 93282df0a13Shmt pub fn rmdir(path: *const u8) -> Result<usize, SystemError> { 93382df0a13Shmt let path = check_and_clone_cstr(path, Some(MAX_PATHLEN))?; 93482df0a13Shmt return do_remove_dir(AtFlags::AT_FDCWD.bits(), &path).map(|v| v as usize); 9355eeefb8cSChenzx } 9365eeefb8cSChenzx 93782df0a13Shmt pub fn unlink(path: *const u8) -> Result<usize, SystemError> { 93882df0a13Shmt let path = check_and_clone_cstr(path, Some(MAX_PATHLEN))?; 93982df0a13Shmt return do_unlink_at(AtFlags::AT_FDCWD.bits(), &path).map(|v| v as usize); 940bf4a4899SLoGin } 941bf4a4899SLoGin 9429e481b3bSTTaq /// # 修改文件名 9439e481b3bSTTaq /// 9449e481b3bSTTaq /// 9459e481b3bSTTaq /// ## 参数 9469e481b3bSTTaq /// 9471074eb34SSamuel Dai /// - oldfd: 源文件夹文件描述符 9489e481b3bSTTaq /// - filename_from: 源文件路径 9491074eb34SSamuel Dai /// - newfd: 目标文件夹文件描述符 9509e481b3bSTTaq /// - filename_to: 目标文件路径 9519e481b3bSTTaq /// - flags: 标志位 9529e481b3bSTTaq /// 9539e481b3bSTTaq /// 9549e481b3bSTTaq /// ## 返回值 9559e481b3bSTTaq /// - Ok(返回值类型): 返回值的说明 9569e481b3bSTTaq /// - Err(错误值类型): 错误的说明 9579e481b3bSTTaq /// 9589e481b3bSTTaq pub fn do_renameat2( 9599e481b3bSTTaq oldfd: i32, 9609e481b3bSTTaq filename_from: *const u8, 9619e481b3bSTTaq newfd: i32, 9629e481b3bSTTaq filename_to: *const u8, 9639e481b3bSTTaq _flags: u32, 9649e481b3bSTTaq ) -> Result<usize, SystemError> { 9659e481b3bSTTaq let filename_from = check_and_clone_cstr(filename_from, Some(MAX_PATHLEN)).unwrap(); 9669e481b3bSTTaq let filename_to = check_and_clone_cstr(filename_to, Some(MAX_PATHLEN)).unwrap(); 9679e481b3bSTTaq // 文件名过长 968b5b571e0SLoGin if filename_from.len() > MAX_PATHLEN || filename_to.len() > MAX_PATHLEN { 9699e481b3bSTTaq return Err(SystemError::ENAMETOOLONG); 9709e481b3bSTTaq } 9719e481b3bSTTaq 9729e481b3bSTTaq //获取pcb,文件节点 9739e481b3bSTTaq let pcb = ProcessManager::current_pcb(); 9749e481b3bSTTaq let (_old_inode_begin, old_remain_path) = user_path_at(&pcb, oldfd, &filename_from)?; 9759e481b3bSTTaq let (_new_inode_begin, new_remain_path) = user_path_at(&pcb, newfd, &filename_to)?; 9769e481b3bSTTaq //获取父目录 9779e481b3bSTTaq let (old_filename, old_parent_path) = rsplit_path(&old_remain_path); 9789e481b3bSTTaq let old_parent_inode = ROOT_INODE() 9799e481b3bSTTaq .lookup_follow_symlink(old_parent_path.unwrap_or("/"), VFS_MAX_FOLLOW_SYMLINK_TIMES)?; 9809e481b3bSTTaq let (new_filename, new_parent_path) = rsplit_path(&new_remain_path); 9819e481b3bSTTaq let new_parent_inode = ROOT_INODE() 9829e481b3bSTTaq .lookup_follow_symlink(new_parent_path.unwrap_or("/"), VFS_MAX_FOLLOW_SYMLINK_TIMES)?; 9839e481b3bSTTaq old_parent_inode.move_to(old_filename, &new_parent_inode, new_filename)?; 9849e481b3bSTTaq return Ok(0); 9859e481b3bSTTaq } 9869e481b3bSTTaq 987ab5c8ca4Slogin /// @brief 根据提供的文件描述符的fd,复制对应的文件结构体,并返回新复制的文件结构体对应的fd 988ab5c8ca4Slogin pub fn dup(oldfd: i32) -> Result<usize, SystemError> { 9891496ba7bSLoGin let binding = ProcessManager::current_pcb().fd_table(); 9901496ba7bSLoGin let mut fd_table_guard = binding.write(); 991ab5c8ca4Slogin 9921496ba7bSLoGin let old_file = fd_table_guard 9931496ba7bSLoGin .get_file_by_fd(oldfd) 9941496ba7bSLoGin .ok_or(SystemError::EBADF)?; 9951496ba7bSLoGin 996dfe53cf0SGnoCiYeH let new_file = old_file.try_clone().ok_or(SystemError::EBADF)?; 9979365e801SGnoCiYeH // dup默认非cloexec 9989365e801SGnoCiYeH new_file.set_close_on_exec(false); 9992b771e32SGou Ngai // 申请文件描述符,并把文件对象存入其中 10001496ba7bSLoGin let res = fd_table_guard.alloc_fd(new_file, None).map(|x| x as usize); 10012b771e32SGou Ngai return res; 10022b771e32SGou Ngai } 10032b771e32SGou Ngai 1004ab5c8ca4Slogin /// 根据提供的文件描述符的fd,和指定新fd,复制对应的文件结构体, 1005ab5c8ca4Slogin /// 并返回新复制的文件结构体对应的fd. 1006ab5c8ca4Slogin /// 如果新fd已经打开,则会先关闭新fd. 1007ab5c8ca4Slogin /// 1008ab5c8ca4Slogin /// ## 参数 1009ab5c8ca4Slogin /// 1010ab5c8ca4Slogin /// - `oldfd`:旧文件描述符 1011ab5c8ca4Slogin /// - `newfd`:新文件描述符 1012ab5c8ca4Slogin /// 1013ab5c8ca4Slogin /// ## 返回值 1014ab5c8ca4Slogin /// 1015ab5c8ca4Slogin /// - 成功:新文件描述符 1016ab5c8ca4Slogin /// - 失败:错误码 1017ab5c8ca4Slogin pub fn dup2(oldfd: i32, newfd: i32) -> Result<usize, SystemError> { 10181496ba7bSLoGin let binding = ProcessManager::current_pcb().fd_table(); 10191496ba7bSLoGin let mut fd_table_guard = binding.write(); 10201496ba7bSLoGin return Self::do_dup2(oldfd, newfd, &mut fd_table_guard); 10211496ba7bSLoGin } 10221496ba7bSLoGin 1023*40348dd8Szwb0x00 pub fn dup3(oldfd: i32, newfd: i32, flags: u32) -> Result<usize, SystemError> { 1024*40348dd8Szwb0x00 let flags = FileMode::from_bits_truncate(flags); 1025*40348dd8Szwb0x00 if (flags.bits() & !FileMode::O_CLOEXEC.bits()) != 0 { 1026*40348dd8Szwb0x00 return Err(SystemError::EINVAL); 1027*40348dd8Szwb0x00 } 1028*40348dd8Szwb0x00 1029*40348dd8Szwb0x00 if oldfd == newfd { 1030*40348dd8Szwb0x00 return Err(SystemError::EINVAL); 1031*40348dd8Szwb0x00 } 1032*40348dd8Szwb0x00 1033*40348dd8Szwb0x00 let binding = ProcessManager::current_pcb().fd_table(); 1034*40348dd8Szwb0x00 let mut fd_table_guard = binding.write(); 1035*40348dd8Szwb0x00 return Self::do_dup3(oldfd, newfd, flags, &mut fd_table_guard); 1036*40348dd8Szwb0x00 } 1037*40348dd8Szwb0x00 10381496ba7bSLoGin fn do_dup2( 10391496ba7bSLoGin oldfd: i32, 10401496ba7bSLoGin newfd: i32, 10411496ba7bSLoGin fd_table_guard: &mut RwLockWriteGuard<'_, FileDescriptorVec>, 10421496ba7bSLoGin ) -> Result<usize, SystemError> { 1043d623e902SGnoCiYeH Self::do_dup3(oldfd, newfd, FileMode::empty(), fd_table_guard) 1044d623e902SGnoCiYeH } 1045d623e902SGnoCiYeH 1046d623e902SGnoCiYeH fn do_dup3( 1047d623e902SGnoCiYeH oldfd: i32, 1048d623e902SGnoCiYeH newfd: i32, 1049d623e902SGnoCiYeH flags: FileMode, 1050d623e902SGnoCiYeH fd_table_guard: &mut RwLockWriteGuard<'_, FileDescriptorVec>, 1051d623e902SGnoCiYeH ) -> Result<usize, SystemError> { 10522b771e32SGou Ngai // 确认oldfd, newid是否有效 10531496ba7bSLoGin if !(FileDescriptorVec::validate_fd(oldfd) && FileDescriptorVec::validate_fd(newfd)) { 10541496ba7bSLoGin return Err(SystemError::EBADF); 10551496ba7bSLoGin } 10561496ba7bSLoGin 10572b771e32SGou Ngai if oldfd == newfd { 10582b771e32SGou Ngai // 若oldfd与newfd相等 1059ab5c8ca4Slogin return Ok(newfd as usize); 10602b771e32SGou Ngai } 10611496ba7bSLoGin let new_exists = fd_table_guard.get_file_by_fd(newfd).is_some(); 10621496ba7bSLoGin if new_exists { 10632b771e32SGou Ngai // close newfd 1064b5b571e0SLoGin if fd_table_guard.drop_fd(newfd).is_err() { 10652b771e32SGou Ngai // An I/O error occurred while attempting to close fildes2. 10662b771e32SGou Ngai return Err(SystemError::EIO); 10672b771e32SGou Ngai } 10682b771e32SGou Ngai } 10692b771e32SGou Ngai 10701496ba7bSLoGin let old_file = fd_table_guard 10711496ba7bSLoGin .get_file_by_fd(oldfd) 10721496ba7bSLoGin .ok_or(SystemError::EBADF)?; 1073dfe53cf0SGnoCiYeH let new_file = old_file.try_clone().ok_or(SystemError::EBADF)?; 1074d623e902SGnoCiYeH 1075d623e902SGnoCiYeH if flags.contains(FileMode::O_CLOEXEC) { 1076d623e902SGnoCiYeH new_file.set_close_on_exec(true); 1077d623e902SGnoCiYeH } else { 10789365e801SGnoCiYeH new_file.set_close_on_exec(false); 1079d623e902SGnoCiYeH } 10802b771e32SGou Ngai // 申请文件描述符,并把文件对象存入其中 10811496ba7bSLoGin let res = fd_table_guard 10821496ba7bSLoGin .alloc_fd(new_file, Some(newfd)) 1083ab5c8ca4Slogin .map(|x| x as usize); 10842b771e32SGou Ngai return res; 10852b771e32SGou Ngai } 10866d81180bSLoGin 10876d81180bSLoGin /// # fcntl 10886d81180bSLoGin /// 10896d81180bSLoGin /// ## 参数 10906d81180bSLoGin /// 10916d81180bSLoGin /// - `fd`:文件描述符 10926d81180bSLoGin /// - `cmd`:命令 10936d81180bSLoGin /// - `arg`:参数 10946d81180bSLoGin pub fn fcntl(fd: i32, cmd: FcntlCommand, arg: i32) -> Result<usize, SystemError> { 1095d623e902SGnoCiYeH // kdebug!("fcntl ({cmd:?}) fd: {fd}, arg={arg}"); 10966d81180bSLoGin match cmd { 1097d623e902SGnoCiYeH FcntlCommand::DupFd | FcntlCommand::DupFdCloexec => { 10986d81180bSLoGin if arg < 0 || arg as usize >= FileDescriptorVec::PROCESS_MAX_FD { 10996d81180bSLoGin return Err(SystemError::EBADF); 11006d81180bSLoGin } 11016d81180bSLoGin let arg = arg as usize; 11026d81180bSLoGin for i in arg..FileDescriptorVec::PROCESS_MAX_FD { 11031496ba7bSLoGin let binding = ProcessManager::current_pcb().fd_table(); 11041496ba7bSLoGin let mut fd_table_guard = binding.write(); 110552da9a59SGnoCiYeH if fd_table_guard.get_file_by_fd(i as i32).is_none() { 1106d623e902SGnoCiYeH if cmd == FcntlCommand::DupFd { 11071496ba7bSLoGin return Self::do_dup2(fd, i as i32, &mut fd_table_guard); 1108d623e902SGnoCiYeH } else { 1109d623e902SGnoCiYeH return Self::do_dup3( 1110d623e902SGnoCiYeH fd, 1111d623e902SGnoCiYeH i as i32, 1112d623e902SGnoCiYeH FileMode::O_CLOEXEC, 1113d623e902SGnoCiYeH &mut fd_table_guard, 1114d623e902SGnoCiYeH ); 1115d623e902SGnoCiYeH } 11166d81180bSLoGin } 11176d81180bSLoGin } 11186d81180bSLoGin return Err(SystemError::EMFILE); 11196d81180bSLoGin } 11206d81180bSLoGin FcntlCommand::GetFd => { 11216d81180bSLoGin // Get file descriptor flags. 11221496ba7bSLoGin let binding = ProcessManager::current_pcb().fd_table(); 11231496ba7bSLoGin let fd_table_guard = binding.read(); 1124d623e902SGnoCiYeH 11251496ba7bSLoGin if let Some(file) = fd_table_guard.get_file_by_fd(fd) { 11261496ba7bSLoGin // drop guard 以避免无法调度的问题 11271496ba7bSLoGin drop(fd_table_guard); 11286d81180bSLoGin 1129dfe53cf0SGnoCiYeH if file.close_on_exec() { 11306d81180bSLoGin return Ok(FD_CLOEXEC as usize); 1131d623e902SGnoCiYeH } else { 1132d623e902SGnoCiYeH return Ok(0); 11336d81180bSLoGin } 11346d81180bSLoGin } 11356d81180bSLoGin return Err(SystemError::EBADF); 11366d81180bSLoGin } 11376d81180bSLoGin FcntlCommand::SetFd => { 11386d81180bSLoGin // Set file descriptor flags. 11391496ba7bSLoGin let binding = ProcessManager::current_pcb().fd_table(); 11401496ba7bSLoGin let fd_table_guard = binding.write(); 11411496ba7bSLoGin 11421496ba7bSLoGin if let Some(file) = fd_table_guard.get_file_by_fd(fd) { 11431496ba7bSLoGin // drop guard 以避免无法调度的问题 11441496ba7bSLoGin drop(fd_table_guard); 11456d81180bSLoGin let arg = arg as u32; 11466d81180bSLoGin if arg & FD_CLOEXEC != 0 { 1147dfe53cf0SGnoCiYeH file.set_close_on_exec(true); 11486d81180bSLoGin } else { 1149dfe53cf0SGnoCiYeH file.set_close_on_exec(false); 11506d81180bSLoGin } 11516d81180bSLoGin return Ok(0); 11526d81180bSLoGin } 11536d81180bSLoGin return Err(SystemError::EBADF); 11546d81180bSLoGin } 11556d81180bSLoGin 11566d81180bSLoGin FcntlCommand::GetFlags => { 11576d81180bSLoGin // Get file status flags. 11581496ba7bSLoGin let binding = ProcessManager::current_pcb().fd_table(); 11591496ba7bSLoGin let fd_table_guard = binding.read(); 11601496ba7bSLoGin 11611496ba7bSLoGin if let Some(file) = fd_table_guard.get_file_by_fd(fd) { 11621496ba7bSLoGin // drop guard 以避免无法调度的问题 11631496ba7bSLoGin drop(fd_table_guard); 1164dfe53cf0SGnoCiYeH return Ok(file.mode().bits() as usize); 11656d81180bSLoGin } 11661496ba7bSLoGin 11676d81180bSLoGin return Err(SystemError::EBADF); 11686d81180bSLoGin } 11696d81180bSLoGin FcntlCommand::SetFlags => { 11706d81180bSLoGin // Set file status flags. 11711496ba7bSLoGin let binding = ProcessManager::current_pcb().fd_table(); 11721496ba7bSLoGin let fd_table_guard = binding.write(); 11731496ba7bSLoGin 11741496ba7bSLoGin if let Some(file) = fd_table_guard.get_file_by_fd(fd) { 11756d81180bSLoGin let arg = arg as u32; 11766d81180bSLoGin let mode = FileMode::from_bits(arg).ok_or(SystemError::EINVAL)?; 11771496ba7bSLoGin // drop guard 以避免无法调度的问题 11781496ba7bSLoGin drop(fd_table_guard); 1179dfe53cf0SGnoCiYeH file.set_mode(mode)?; 11806d81180bSLoGin return Ok(0); 11816d81180bSLoGin } 11821496ba7bSLoGin 11836d81180bSLoGin return Err(SystemError::EBADF); 11846d81180bSLoGin } 11856d81180bSLoGin _ => { 11866d81180bSLoGin // TODO: unimplemented 11876d81180bSLoGin // 未实现的命令,返回0,不报错。 11886d81180bSLoGin 1189d623e902SGnoCiYeH kwarn!("fcntl: unimplemented command: {:?}, defaults to 0.", cmd); 1190d623e902SGnoCiYeH return Err(SystemError::ENOSYS); 11916d81180bSLoGin } 11926d81180bSLoGin } 11936d81180bSLoGin } 11946d81180bSLoGin 11956d81180bSLoGin /// # ftruncate 11966d81180bSLoGin /// 11976d81180bSLoGin /// ## 描述 11986d81180bSLoGin /// 11996d81180bSLoGin /// 改变文件大小. 12006d81180bSLoGin /// 如果文件大小大于原来的大小,那么文件的内容将会被扩展到指定的大小,新的空间将会用0填充. 12016d81180bSLoGin /// 如果文件大小小于原来的大小,那么文件的内容将会被截断到指定的大小. 12026d81180bSLoGin /// 12036d81180bSLoGin /// ## 参数 12046d81180bSLoGin /// 12056d81180bSLoGin /// - `fd`:文件描述符 12066d81180bSLoGin /// - `len`:文件大小 12076d81180bSLoGin /// 12086d81180bSLoGin /// ## 返回值 12096d81180bSLoGin /// 12106d81180bSLoGin /// 如果成功,返回0,否则返回错误码. 12116d81180bSLoGin pub fn ftruncate(fd: i32, len: usize) -> Result<usize, SystemError> { 12121496ba7bSLoGin let binding = ProcessManager::current_pcb().fd_table(); 12131496ba7bSLoGin let fd_table_guard = binding.read(); 12141496ba7bSLoGin 12151496ba7bSLoGin if let Some(file) = fd_table_guard.get_file_by_fd(fd) { 12161496ba7bSLoGin // drop guard 以避免无法调度的问题 12171496ba7bSLoGin drop(fd_table_guard); 1218dfe53cf0SGnoCiYeH let r = file.ftruncate(len).map(|_| 0); 12196d81180bSLoGin return r; 12206d81180bSLoGin } 12211496ba7bSLoGin 12226d81180bSLoGin return Err(SystemError::EBADF); 12236d81180bSLoGin } 12241496ba7bSLoGin 122567b48188Shoumkh fn do_fstat(fd: i32) -> Result<PosixKstat, SystemError> { 12261496ba7bSLoGin let binding = ProcessManager::current_pcb().fd_table(); 12271496ba7bSLoGin let fd_table_guard = binding.read(); 12281496ba7bSLoGin let file = fd_table_guard 12291496ba7bSLoGin .get_file_by_fd(fd) 12301496ba7bSLoGin .ok_or(SystemError::EBADF)?; 12311496ba7bSLoGin // drop guard 以避免无法调度的问题 12321496ba7bSLoGin drop(fd_table_guard); 12331496ba7bSLoGin 123467b48188Shoumkh let mut kstat = PosixKstat::new(); 123567b48188Shoumkh // 获取文件信息 1236dfe53cf0SGnoCiYeH let metadata = file.metadata()?; 1237b5b571e0SLoGin kstat.size = metadata.size; 123867b48188Shoumkh kstat.dev_id = metadata.dev_id as u64; 12396b4e7a29SLoGin kstat.inode = metadata.inode_id.into() as u64; 124067b48188Shoumkh kstat.blcok_size = metadata.blk_size as i64; 124167b48188Shoumkh kstat.blocks = metadata.blocks as u64; 124267b48188Shoumkh 124367b48188Shoumkh kstat.atime.tv_sec = metadata.atime.tv_sec; 124467b48188Shoumkh kstat.atime.tv_nsec = metadata.atime.tv_nsec; 124567b48188Shoumkh kstat.mtime.tv_sec = metadata.mtime.tv_sec; 124667b48188Shoumkh kstat.mtime.tv_nsec = metadata.mtime.tv_nsec; 124767b48188Shoumkh kstat.ctime.tv_sec = metadata.ctime.tv_sec; 124867b48188Shoumkh kstat.ctime.tv_nsec = metadata.ctime.tv_nsec; 124967b48188Shoumkh 125067b48188Shoumkh kstat.nlink = metadata.nlinks as u64; 125167b48188Shoumkh kstat.uid = metadata.uid as i32; 125267b48188Shoumkh kstat.gid = metadata.gid as i32; 125302343d0bSLoGin kstat.rdev = metadata.raw_dev.data() as i64; 12546b4e7a29SLoGin kstat.mode = metadata.mode; 1255dfe53cf0SGnoCiYeH match file.file_type() { 12567eda31b2SLoGin FileType::File => kstat.mode.insert(ModeType::S_IFREG), 125767b48188Shoumkh FileType::Dir => kstat.mode.insert(ModeType::S_IFDIR), 125867b48188Shoumkh FileType::BlockDevice => kstat.mode.insert(ModeType::S_IFBLK), 125967b48188Shoumkh FileType::CharDevice => kstat.mode.insert(ModeType::S_IFCHR), 126067b48188Shoumkh FileType::SymLink => kstat.mode.insert(ModeType::S_IFLNK), 126167b48188Shoumkh FileType::Socket => kstat.mode.insert(ModeType::S_IFSOCK), 126267b48188Shoumkh FileType::Pipe => kstat.mode.insert(ModeType::S_IFIFO), 126340314b30SXiaoye Zheng FileType::KvmDevice => kstat.mode.insert(ModeType::S_IFCHR), 126402343d0bSLoGin FileType::FramebufferDevice => kstat.mode.insert(ModeType::S_IFCHR), 126567b48188Shoumkh } 126667b48188Shoumkh 126767b48188Shoumkh return Ok(kstat); 126867b48188Shoumkh } 12691496ba7bSLoGin 127067b48188Shoumkh pub fn fstat(fd: i32, usr_kstat: *mut PosixKstat) -> Result<usize, SystemError> { 127182df0a13Shmt let mut writer = UserBufferWriter::new(usr_kstat, size_of::<PosixKstat>(), true)?; 12721496ba7bSLoGin let kstat = Self::do_fstat(fd)?; 127382df0a13Shmt 127482df0a13Shmt writer.copy_one_to_user(&kstat, 0)?; 127567b48188Shoumkh return Ok(0); 127667b48188Shoumkh } 12772dbef785SGnoCiYeH 127882df0a13Shmt pub fn stat(path: *const u8, user_kstat: *mut PosixKstat) -> Result<usize, SystemError> { 127982df0a13Shmt let fd = Self::open( 128082df0a13Shmt path, 128182df0a13Shmt FileMode::O_RDONLY.bits(), 128282df0a13Shmt ModeType::empty().bits(), 128382df0a13Shmt true, 128482df0a13Shmt )?; 1285bf4a4899SLoGin let r = Self::fstat(fd as i32, user_kstat); 1286971462beSGnoCiYeH Self::close(fd).ok(); 1287bf4a4899SLoGin return r; 1288bf4a4899SLoGin } 1289bf4a4899SLoGin 129082df0a13Shmt pub fn lstat(path: *const u8, user_kstat: *mut PosixKstat) -> Result<usize, SystemError> { 129182df0a13Shmt let fd = Self::open( 129282df0a13Shmt path, 129382df0a13Shmt FileMode::O_RDONLY.bits(), 129482df0a13Shmt ModeType::empty().bits(), 129582df0a13Shmt false, 129682df0a13Shmt )?; 1297bf4a4899SLoGin let r = Self::fstat(fd as i32, user_kstat); 1298bf4a4899SLoGin Self::close(fd).ok(); 1299bf4a4899SLoGin return r; 1300971462beSGnoCiYeH } 1301971462beSGnoCiYeH 1302597ecc08STTaq pub fn statfs(path: *const u8, user_statfs: *mut PosixStatfs) -> Result<usize, SystemError> { 1303597ecc08STTaq let mut writer = UserBufferWriter::new(user_statfs, size_of::<PosixStatfs>(), true)?; 1304597ecc08STTaq let fd = Self::open( 1305597ecc08STTaq path, 1306597ecc08STTaq FileMode::O_RDONLY.bits(), 1307597ecc08STTaq ModeType::empty().bits(), 1308597ecc08STTaq true, 1309597ecc08STTaq )?; 1310597ecc08STTaq let path = check_and_clone_cstr(path, Some(MAX_PATHLEN)).unwrap(); 1311597ecc08STTaq let pcb = ProcessManager::current_pcb(); 1312597ecc08STTaq let (_inode_begin, remain_path) = user_path_at(&pcb, fd as i32, &path)?; 1313597ecc08STTaq let inode = ROOT_INODE().lookup_follow_symlink(&remain_path, MAX_PATHLEN)?; 1314597ecc08STTaq let statfs = PosixStatfs::from(inode.fs().super_block()); 1315597ecc08STTaq writer.copy_one_to_user(&statfs, 0)?; 1316597ecc08STTaq return Ok(0); 1317597ecc08STTaq } 1318597ecc08STTaq 1319597ecc08STTaq pub fn fstatfs(fd: i32, user_statfs: *mut PosixStatfs) -> Result<usize, SystemError> { 1320597ecc08STTaq let mut writer = UserBufferWriter::new(user_statfs, size_of::<PosixStatfs>(), true)?; 1321597ecc08STTaq let binding = ProcessManager::current_pcb().fd_table(); 1322597ecc08STTaq let fd_table_guard = binding.read(); 1323597ecc08STTaq let file = fd_table_guard 1324597ecc08STTaq .get_file_by_fd(fd) 1325597ecc08STTaq .ok_or(SystemError::EBADF)?; 1326597ecc08STTaq drop(fd_table_guard); 1327dfe53cf0SGnoCiYeH let statfs = PosixStatfs::from(file.inode().fs().super_block()); 1328597ecc08STTaq writer.copy_one_to_user(&statfs, 0)?; 1329597ecc08STTaq return Ok(0); 1330597ecc08STTaq } 1331597ecc08STTaq 1332b4eb05a1STTaq pub fn do_statx( 1333b4eb05a1STTaq fd: i32, 1334b4eb05a1STTaq path: *const u8, 1335b4eb05a1STTaq flags: u32, 1336b4eb05a1STTaq mask: u32, 1337b4eb05a1STTaq usr_kstat: *mut PosixStatx, 1338b4eb05a1STTaq ) -> Result<usize, SystemError> { 1339b4eb05a1STTaq if usr_kstat.is_null() { 1340b4eb05a1STTaq return Err(SystemError::EFAULT); 1341b4eb05a1STTaq } 1342b4eb05a1STTaq 1343b4eb05a1STTaq let mask = PosixStatxMask::from_bits_truncate(mask); 1344b4eb05a1STTaq 1345b4eb05a1STTaq if mask.contains(PosixStatxMask::STATX_RESERVED) { 1346b4eb05a1STTaq return Err(SystemError::ENAVAIL); 1347b4eb05a1STTaq } 1348b4eb05a1STTaq 1349b4eb05a1STTaq let flags = FileMode::from_bits_truncate(flags); 1350b4eb05a1STTaq let ofd = Self::open(path, flags.bits(), ModeType::empty().bits, true)?; 1351b4eb05a1STTaq 1352b4eb05a1STTaq let binding = ProcessManager::current_pcb().fd_table(); 1353b4eb05a1STTaq let fd_table_guard = binding.read(); 1354b4eb05a1STTaq let file = fd_table_guard 1355b4eb05a1STTaq .get_file_by_fd(ofd as i32) 1356b4eb05a1STTaq .ok_or(SystemError::EBADF)?; 1357b4eb05a1STTaq // drop guard 以避免无法调度的问题 1358b4eb05a1STTaq drop(fd_table_guard); 1359b4eb05a1STTaq let mut writer = UserBufferWriter::new(usr_kstat, size_of::<PosixStatx>(), true)?; 1360b4eb05a1STTaq let mut tmp: PosixStatx = PosixStatx::new(); 1361b4eb05a1STTaq // 获取文件信息 1362dfe53cf0SGnoCiYeH let metadata = file.metadata()?; 1363b4eb05a1STTaq 1364b4eb05a1STTaq tmp.stx_mask |= PosixStatxMask::STATX_BASIC_STATS; 1365b4eb05a1STTaq tmp.stx_blksize = metadata.blk_size as u32; 1366b4eb05a1STTaq if mask.contains(PosixStatxMask::STATX_MODE) || mask.contains(PosixStatxMask::STATX_TYPE) { 1367b4eb05a1STTaq tmp.stx_mode = metadata.mode; 1368b4eb05a1STTaq } 1369b4eb05a1STTaq if mask.contains(PosixStatxMask::STATX_NLINK) { 1370b4eb05a1STTaq tmp.stx_nlink = metadata.nlinks as u32; 1371b4eb05a1STTaq } 1372b4eb05a1STTaq if mask.contains(PosixStatxMask::STATX_UID) { 1373b4eb05a1STTaq tmp.stx_uid = metadata.uid as u32; 1374b4eb05a1STTaq } 1375b4eb05a1STTaq if mask.contains(PosixStatxMask::STATX_GID) { 1376b4eb05a1STTaq tmp.stx_gid = metadata.gid as u32; 1377b4eb05a1STTaq } 1378b4eb05a1STTaq if mask.contains(PosixStatxMask::STATX_ATIME) { 1379b4eb05a1STTaq tmp.stx_atime.tv_sec = metadata.atime.tv_sec; 1380b4eb05a1STTaq tmp.stx_atime.tv_nsec = metadata.atime.tv_nsec; 1381b4eb05a1STTaq } 1382b4eb05a1STTaq if mask.contains(PosixStatxMask::STATX_MTIME) { 1383b4eb05a1STTaq tmp.stx_mtime.tv_sec = metadata.ctime.tv_sec; 1384b4eb05a1STTaq tmp.stx_mtime.tv_nsec = metadata.ctime.tv_nsec; 1385b4eb05a1STTaq } 1386b4eb05a1STTaq if mask.contains(PosixStatxMask::STATX_CTIME) { 1387b4eb05a1STTaq // ctime是文件上次修改状态的时间 1388b4eb05a1STTaq tmp.stx_ctime.tv_sec = metadata.mtime.tv_sec; 1389b4eb05a1STTaq tmp.stx_ctime.tv_nsec = metadata.mtime.tv_nsec; 1390b4eb05a1STTaq } 1391b4eb05a1STTaq if mask.contains(PosixStatxMask::STATX_INO) { 1392b4eb05a1STTaq tmp.stx_inode = metadata.inode_id.into() as u64; 1393b4eb05a1STTaq } 1394b4eb05a1STTaq if mask.contains(PosixStatxMask::STATX_SIZE) { 1395b4eb05a1STTaq tmp.stx_size = metadata.size; 1396b4eb05a1STTaq } 1397b4eb05a1STTaq if mask.contains(PosixStatxMask::STATX_BLOCKS) { 1398b4eb05a1STTaq tmp.stx_blocks = metadata.blocks as u64; 1399b4eb05a1STTaq } 1400b4eb05a1STTaq 1401b4eb05a1STTaq if mask.contains(PosixStatxMask::STATX_BTIME) { 1402b4eb05a1STTaq // btime是文件创建时间 1403b4eb05a1STTaq tmp.stx_btime.tv_sec = metadata.ctime.tv_sec; 1404b4eb05a1STTaq tmp.stx_btime.tv_nsec = metadata.ctime.tv_nsec; 1405b4eb05a1STTaq } 1406b4eb05a1STTaq if mask.contains(PosixStatxMask::STATX_ALL) { 1407b4eb05a1STTaq tmp.stx_attributes = StxAttributes::STATX_ATTR_APPEND; 1408b4eb05a1STTaq tmp.stx_attributes_mask |= 1409b4eb05a1STTaq StxAttributes::STATX_ATTR_AUTOMOUNT | StxAttributes::STATX_ATTR_DAX; 1410b4eb05a1STTaq tmp.stx_dev_major = metadata.dev_id as u32; 1411b4eb05a1STTaq tmp.stx_dev_minor = metadata.dev_id as u32; // 1412b5b571e0SLoGin tmp.stx_rdev_major = metadata.raw_dev.data(); 1413b5b571e0SLoGin tmp.stx_rdev_minor = metadata.raw_dev.data(); 1414b4eb05a1STTaq } 1415b4eb05a1STTaq if mask.contains(PosixStatxMask::STATX_MNT_ID) { 1416b4eb05a1STTaq tmp.stx_mnt_id = 0; 1417b4eb05a1STTaq } 1418b4eb05a1STTaq if mask.contains(PosixStatxMask::STATX_DIOALIGN) { 1419b4eb05a1STTaq tmp.stx_dio_mem_align = 0; 1420b4eb05a1STTaq tmp.stx_dio_offset_align = 0; 1421b4eb05a1STTaq } 1422b4eb05a1STTaq 1423dfe53cf0SGnoCiYeH match file.file_type() { 1424b4eb05a1STTaq FileType::File => tmp.stx_mode.insert(ModeType::S_IFREG), 1425b4eb05a1STTaq FileType::Dir => tmp.stx_mode.insert(ModeType::S_IFDIR), 1426b4eb05a1STTaq FileType::BlockDevice => tmp.stx_mode.insert(ModeType::S_IFBLK), 1427b4eb05a1STTaq FileType::CharDevice => tmp.stx_mode.insert(ModeType::S_IFCHR), 1428b4eb05a1STTaq FileType::SymLink => tmp.stx_mode.insert(ModeType::S_IFLNK), 1429b4eb05a1STTaq FileType::Socket => tmp.stx_mode.insert(ModeType::S_IFSOCK), 1430b4eb05a1STTaq FileType::Pipe => tmp.stx_mode.insert(ModeType::S_IFIFO), 1431b4eb05a1STTaq FileType::KvmDevice => tmp.stx_mode.insert(ModeType::S_IFCHR), 1432b4eb05a1STTaq FileType::FramebufferDevice => tmp.stx_mode.insert(ModeType::S_IFCHR), 1433b4eb05a1STTaq } 1434b4eb05a1STTaq 1435b4eb05a1STTaq writer.copy_one_to_user(&tmp, 0)?; 1436b4eb05a1STTaq Self::close(fd as usize).ok(); 1437b4eb05a1STTaq return Ok(0); 1438b4eb05a1STTaq } 1439b4eb05a1STTaq 14402dbef785SGnoCiYeH pub fn mknod( 144182df0a13Shmt path: *const u8, 14422dbef785SGnoCiYeH mode: ModeType, 14432dbef785SGnoCiYeH dev_t: DeviceNumber, 14442dbef785SGnoCiYeH ) -> Result<usize, SystemError> { 144582df0a13Shmt let path = check_and_clone_cstr(path, Some(MAX_PATHLEN))?; 144682df0a13Shmt let path = path.as_str().trim(); 14472dbef785SGnoCiYeH 1448a03c4f9dSLoGin let inode: Result<Arc<dyn IndexNode>, SystemError> = 1449a03c4f9dSLoGin ROOT_INODE().lookup_follow_symlink(path, VFS_MAX_FOLLOW_SYMLINK_TIMES); 14502dbef785SGnoCiYeH 14512dbef785SGnoCiYeH if inode.is_ok() { 14522dbef785SGnoCiYeH return Err(SystemError::EEXIST); 14532dbef785SGnoCiYeH } 14542dbef785SGnoCiYeH 14552dbef785SGnoCiYeH let (filename, parent_path) = rsplit_path(path); 14562dbef785SGnoCiYeH 14572dbef785SGnoCiYeH // 查找父目录 1458a03c4f9dSLoGin let parent_inode: Arc<dyn IndexNode> = ROOT_INODE() 1459a03c4f9dSLoGin .lookup_follow_symlink(parent_path.unwrap_or("/"), VFS_MAX_FOLLOW_SYMLINK_TIMES)?; 14602dbef785SGnoCiYeH // 创建nod 14612dbef785SGnoCiYeH parent_inode.mknod(filename, mode, dev_t)?; 14622dbef785SGnoCiYeH 14632dbef785SGnoCiYeH return Ok(0); 14642dbef785SGnoCiYeH } 1465971462beSGnoCiYeH 1466971462beSGnoCiYeH pub fn writev(fd: i32, iov: usize, count: usize) -> Result<usize, SystemError> { 1467971462beSGnoCiYeH // IoVecs会进行用户态检验 1468971462beSGnoCiYeH let iovecs = unsafe { IoVecs::from_user(iov as *const IoVec, count, false) }?; 1469971462beSGnoCiYeH 1470971462beSGnoCiYeH let data = iovecs.gather(); 1471971462beSGnoCiYeH 1472971462beSGnoCiYeH Self::write(fd, &data) 1473971462beSGnoCiYeH } 1474709498caSLoGin 1475bf4a4899SLoGin pub fn readv(fd: i32, iov: usize, count: usize) -> Result<usize, SystemError> { 1476bf4a4899SLoGin // IoVecs会进行用户态检验 1477bf4a4899SLoGin let mut iovecs = unsafe { IoVecs::from_user(iov as *const IoVec, count, true) }?; 1478bf4a4899SLoGin 1479b5b571e0SLoGin let mut data = vec![0; iovecs.0.iter().map(|x| x.len()).sum()]; 1480bf4a4899SLoGin 1481bf4a4899SLoGin let len = Self::read(fd, &mut data)?; 1482bf4a4899SLoGin 1483bf4a4899SLoGin iovecs.scatter(&data[..len]); 1484bf4a4899SLoGin 1485bf4a4899SLoGin return Ok(len); 1486bf4a4899SLoGin } 1487bf4a4899SLoGin 1488709498caSLoGin pub fn readlink_at( 1489709498caSLoGin dirfd: i32, 1490709498caSLoGin path: *const u8, 1491709498caSLoGin user_buf: *mut u8, 1492709498caSLoGin buf_size: usize, 1493709498caSLoGin ) -> Result<usize, SystemError> { 14949b0abe6dSLoGin let path = check_and_clone_cstr(path, Some(MAX_PATHLEN))?; 149582df0a13Shmt let path = path.as_str().trim(); 1496709498caSLoGin let mut user_buf = UserBufferWriter::new(user_buf, buf_size, true)?; 1497709498caSLoGin 1498b5b571e0SLoGin let (inode, path) = user_path_at(&ProcessManager::current_pcb(), dirfd, path)?; 1499709498caSLoGin 1500709498caSLoGin let inode = inode.lookup(path.as_str())?; 1501709498caSLoGin if inode.metadata()?.file_type != FileType::SymLink { 1502709498caSLoGin return Err(SystemError::EINVAL); 1503709498caSLoGin } 1504709498caSLoGin 1505709498caSLoGin let ubuf = user_buf.buffer::<u8>(0).unwrap(); 1506709498caSLoGin 1507dfe53cf0SGnoCiYeH let file = File::new(inode, FileMode::O_RDONLY)?; 1508709498caSLoGin 1509709498caSLoGin let len = file.read(buf_size, ubuf)?; 1510709498caSLoGin 1511709498caSLoGin return Ok(len); 1512709498caSLoGin } 1513709498caSLoGin 1514709498caSLoGin pub fn readlink( 1515709498caSLoGin path: *const u8, 1516709498caSLoGin user_buf: *mut u8, 1517709498caSLoGin buf_size: usize, 1518709498caSLoGin ) -> Result<usize, SystemError> { 15199b0abe6dSLoGin return Self::readlink_at(AtFlags::AT_FDCWD.bits(), path, user_buf, buf_size); 15209b0abe6dSLoGin } 15219b0abe6dSLoGin 15229b0abe6dSLoGin pub fn access(pathname: *const u8, mode: u32) -> Result<usize, SystemError> { 15239b0abe6dSLoGin return do_faccessat( 15249b0abe6dSLoGin AtFlags::AT_FDCWD.bits(), 15259b0abe6dSLoGin pathname, 1526bf4a4899SLoGin ModeType::from_bits(mode).ok_or(SystemError::EINVAL)?, 15279b0abe6dSLoGin 0, 15289b0abe6dSLoGin ); 15299b0abe6dSLoGin } 15309b0abe6dSLoGin 15319b0abe6dSLoGin pub fn faccessat2( 15329b0abe6dSLoGin dirfd: i32, 15339b0abe6dSLoGin pathname: *const u8, 15349b0abe6dSLoGin mode: u32, 15359b0abe6dSLoGin flags: u32, 15369b0abe6dSLoGin ) -> Result<usize, SystemError> { 1537bf4a4899SLoGin return do_faccessat( 1538bf4a4899SLoGin dirfd, 1539bf4a4899SLoGin pathname, 1540bf4a4899SLoGin ModeType::from_bits(mode).ok_or(SystemError::EINVAL)?, 1541bf4a4899SLoGin flags, 1542bf4a4899SLoGin ); 1543bf4a4899SLoGin } 1544bf4a4899SLoGin 1545bf4a4899SLoGin pub fn chmod(pathname: *const u8, mode: u32) -> Result<usize, SystemError> { 1546bf4a4899SLoGin return do_fchmodat( 1547bf4a4899SLoGin AtFlags::AT_FDCWD.bits(), 1548bf4a4899SLoGin pathname, 1549bf4a4899SLoGin ModeType::from_bits(mode).ok_or(SystemError::EINVAL)?, 1550bf4a4899SLoGin ); 1551bf4a4899SLoGin } 1552bf4a4899SLoGin 1553bf4a4899SLoGin pub fn fchmodat(dirfd: i32, pathname: *const u8, mode: u32) -> Result<usize, SystemError> { 1554bf4a4899SLoGin return do_fchmodat( 1555bf4a4899SLoGin dirfd, 1556bf4a4899SLoGin pathname, 1557bf4a4899SLoGin ModeType::from_bits(mode).ok_or(SystemError::EINVAL)?, 1558bf4a4899SLoGin ); 1559bf4a4899SLoGin } 1560bf4a4899SLoGin 1561bf4a4899SLoGin pub fn fchmod(fd: i32, mode: u32) -> Result<usize, SystemError> { 1562bf4a4899SLoGin let _mode = ModeType::from_bits(mode).ok_or(SystemError::EINVAL)?; 1563bf4a4899SLoGin let binding = ProcessManager::current_pcb().fd_table(); 1564bf4a4899SLoGin let fd_table_guard = binding.read(); 1565bf4a4899SLoGin let _file = fd_table_guard 1566bf4a4899SLoGin .get_file_by_fd(fd) 1567bf4a4899SLoGin .ok_or(SystemError::EBADF)?; 1568bf4a4899SLoGin 1569bf4a4899SLoGin // fchmod没完全实现,因此不修改文件的权限 1570bf4a4899SLoGin // todo: 实现fchmod 1571bf4a4899SLoGin kwarn!("fchmod not fully implemented"); 1572bf4a4899SLoGin return Ok(0); 1573709498caSLoGin } 15741d37ca6dSDonkey Kane /// #挂载文件系统 15751d37ca6dSDonkey Kane /// 15761d37ca6dSDonkey Kane /// 用于挂载文件系统,目前仅支持ramfs挂载 15771d37ca6dSDonkey Kane /// 15781d37ca6dSDonkey Kane /// ## 参数: 15791d37ca6dSDonkey Kane /// 15801d37ca6dSDonkey Kane /// - source 挂载设备(暂时不支持) 15811d37ca6dSDonkey Kane /// - target 挂载目录 15821d37ca6dSDonkey Kane /// - filesystemtype 文件系统 15831d37ca6dSDonkey Kane /// - mountflags 挂载选项(暂未实现) 15841d37ca6dSDonkey Kane /// - data 带数据挂载 15851d37ca6dSDonkey Kane /// 15861d37ca6dSDonkey Kane /// ## 返回值 15871d37ca6dSDonkey Kane /// - Ok(0): 挂载成功 15881d37ca6dSDonkey Kane /// - Err(SystemError) :挂载过程中出错 15891d37ca6dSDonkey Kane pub fn mount( 15901d37ca6dSDonkey Kane _source: *const u8, 15911d37ca6dSDonkey Kane target: *const u8, 15921d37ca6dSDonkey Kane filesystemtype: *const u8, 15931d37ca6dSDonkey Kane _mountflags: usize, 15941d37ca6dSDonkey Kane _data: *const c_void, 15951d37ca6dSDonkey Kane ) -> Result<usize, SystemError> { 15961d37ca6dSDonkey Kane let target = user_access::check_and_clone_cstr(target, Some(MAX_PATHLEN))?; 15971d37ca6dSDonkey Kane 15981d37ca6dSDonkey Kane let filesystemtype = user_access::check_and_clone_cstr(filesystemtype, Some(MAX_PATHLEN))?; 15991d37ca6dSDonkey Kane 16001d37ca6dSDonkey Kane let filesystemtype = producefs!(FSMAKER, filesystemtype)?; 16011d37ca6dSDonkey Kane 16021074eb34SSamuel Dai Vcore::do_mount(filesystemtype, target.to_string().as_str())?; 16031074eb34SSamuel Dai 16041074eb34SSamuel Dai return Ok(0); 16051d37ca6dSDonkey Kane } 16061d37ca6dSDonkey Kane 16071d37ca6dSDonkey Kane // 想法:可以在VFS中实现一个文件系统分发器,流程如下: 16081d37ca6dSDonkey Kane // 1. 接受从上方传来的文件类型字符串 16091d37ca6dSDonkey Kane // 2. 将传入值与启动时准备好的字符串数组逐个比较(probe) 16101d37ca6dSDonkey Kane // 3. 直接在函数内调用构造方法并直接返回文件系统对象 16111074eb34SSamuel Dai 16121074eb34SSamuel Dai /// src/linux/mount.c `umount` & `umount2` 16131074eb34SSamuel Dai /// 16141074eb34SSamuel Dai /// [umount(2) — Linux manual page](https://www.man7.org/linux/man-pages/man2/umount.2.html) 16151074eb34SSamuel Dai pub fn umount2(target: *const u8, flags: i32) -> Result<(), SystemError> { 16161074eb34SSamuel Dai let target = user_access::check_and_clone_cstr(target, Some(MAX_PATHLEN))?; 16171074eb34SSamuel Dai Vcore::do_umount2( 16181074eb34SSamuel Dai AtFlags::AT_FDCWD.bits(), 16191074eb34SSamuel Dai &target, 16201074eb34SSamuel Dai UmountFlag::from_bits(flags).ok_or(SystemError::EINVAL)?, 16211074eb34SSamuel Dai )?; 16221074eb34SSamuel Dai return Ok(()); 16231074eb34SSamuel Dai } 1624709498caSLoGin } 1625709498caSLoGin 1626cde5492fSlogin #[repr(C)] 1627cde5492fSlogin #[derive(Debug, Clone, Copy)] 1628cde5492fSlogin pub struct IoVec { 1629cde5492fSlogin /// 缓冲区的起始地址 1630cde5492fSlogin pub iov_base: *mut u8, 1631cde5492fSlogin /// 缓冲区的长度 1632cde5492fSlogin pub iov_len: usize, 1633cde5492fSlogin } 1634cde5492fSlogin 1635cde5492fSlogin /// 用于存储多个来自用户空间的IoVec 1636cde5492fSlogin /// 1637cde5492fSlogin /// 由于目前内核中的文件系统还不支持分散读写,所以暂时只支持将用户空间的IoVec聚合成一个缓冲区,然后进行操作。 1638cde5492fSlogin /// TODO:支持分散读写 1639cde5492fSlogin #[derive(Debug)] 1640cde5492fSlogin pub struct IoVecs(Vec<&'static mut [u8]>); 1641cde5492fSlogin 1642cde5492fSlogin impl IoVecs { 1643cde5492fSlogin /// 从用户空间的IoVec中构造IoVecs 1644cde5492fSlogin /// 1645cde5492fSlogin /// @param iov 用户空间的IoVec 1646cde5492fSlogin /// @param iovcnt 用户空间的IoVec的数量 1647cde5492fSlogin /// @param readv 是否为readv系统调用 1648cde5492fSlogin /// 1649cde5492fSlogin /// @return 构造成功返回IoVecs,否则返回错误码 1650cde5492fSlogin pub unsafe fn from_user( 1651cde5492fSlogin iov: *const IoVec, 1652cde5492fSlogin iovcnt: usize, 1653cde5492fSlogin _readv: bool, 1654cde5492fSlogin ) -> Result<Self, SystemError> { 1655cde5492fSlogin // 检查iov指针所在空间是否合法 16564fda81ceSLoGin verify_area( 16574fda81ceSLoGin VirtAddr::new(iov as usize), 16584fda81ceSLoGin iovcnt * core::mem::size_of::<IoVec>(), 16594fda81ceSLoGin ) 16604fda81ceSLoGin .map_err(|_| SystemError::EFAULT)?; 1661cde5492fSlogin 1662cde5492fSlogin // 将用户空间的IoVec转换为引用(注意:这里的引用是静态的,因为用户空间的IoVec不会被释放) 1663cde5492fSlogin let iovs: &[IoVec] = core::slice::from_raw_parts(iov, iovcnt); 1664cde5492fSlogin 1665cde5492fSlogin let mut slices: Vec<&mut [u8]> = vec![]; 1666cde5492fSlogin slices.reserve(iovs.len()); 1667cde5492fSlogin 1668cde5492fSlogin for iov in iovs.iter() { 1669cde5492fSlogin if iov.iov_len == 0 { 1670cde5492fSlogin continue; 1671cde5492fSlogin } 1672cde5492fSlogin 16734fda81ceSLoGin verify_area( 16744fda81ceSLoGin VirtAddr::new(iov.iov_base as usize), 16754fda81ceSLoGin iovcnt * core::mem::size_of::<IoVec>(), 16764fda81ceSLoGin ) 16774fda81ceSLoGin .map_err(|_| SystemError::EFAULT)?; 1678cde5492fSlogin 1679cde5492fSlogin slices.push(core::slice::from_raw_parts_mut(iov.iov_base, iov.iov_len)); 1680cde5492fSlogin } 1681cde5492fSlogin 1682cde5492fSlogin return Ok(Self(slices)); 1683cde5492fSlogin } 1684cde5492fSlogin 1685cde5492fSlogin /// @brief 将IoVecs中的数据聚合到一个缓冲区中 1686cde5492fSlogin /// 1687cde5492fSlogin /// @return 返回聚合后的缓冲区 1688cde5492fSlogin pub fn gather(&self) -> Vec<u8> { 1689cde5492fSlogin let mut buf = Vec::new(); 1690cde5492fSlogin for slice in self.0.iter() { 1691cde5492fSlogin buf.extend_from_slice(slice); 1692cde5492fSlogin } 1693cde5492fSlogin return buf; 1694cde5492fSlogin } 1695cde5492fSlogin 1696cde5492fSlogin /// @brief 将给定的数据分散写入到IoVecs中 1697cde5492fSlogin pub fn scatter(&mut self, data: &[u8]) { 1698cde5492fSlogin let mut data: &[u8] = data; 1699cde5492fSlogin for slice in self.0.iter_mut() { 1700cde5492fSlogin let len = core::cmp::min(slice.len(), data.len()); 1701cde5492fSlogin if len == 0 { 1702cde5492fSlogin continue; 1703cde5492fSlogin } 1704cde5492fSlogin 1705cde5492fSlogin slice[..len].copy_from_slice(&data[..len]); 1706cde5492fSlogin data = &data[len..]; 1707cde5492fSlogin } 1708cde5492fSlogin } 1709cde5492fSlogin 1710cde5492fSlogin /// @brief 创建与IoVecs等长的缓冲区 1711cde5492fSlogin /// 1712cde5492fSlogin /// @param set_len 是否设置返回的Vec的len。 1713cde5492fSlogin /// 如果为true,则返回的Vec的len为所有IoVec的长度之和; 1714cde5492fSlogin /// 否则返回的Vec的len为0,capacity为所有IoVec的长度之和. 1715cde5492fSlogin /// 1716cde5492fSlogin /// @return 返回创建的缓冲区 1717cde5492fSlogin pub fn new_buf(&self, set_len: bool) -> Vec<u8> { 1718cde5492fSlogin let total_len: usize = self.0.iter().map(|slice| slice.len()).sum(); 1719cde5492fSlogin let mut buf: Vec<u8> = Vec::with_capacity(total_len); 1720cde5492fSlogin 1721cde5492fSlogin if set_len { 17227ae679ddSLoGin buf.resize(total_len, 0); 1723cde5492fSlogin } 1724cde5492fSlogin return buf; 1725cde5492fSlogin } 1726cde5492fSlogin } 1727