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 }, 2067b48188Shoumkh time::TimeSpec, 21004e86ffSlogin }; 22004e86ffSlogin 23597ecc08STTaq use super::SuperBlock; 24004e86ffSlogin use super::{ 25ab5c8ca4Slogin core::{do_mkdir, do_remove_dir, do_unlink_at}, 26709498caSLoGin fcntl::{AtFlags, FcntlCommand, FD_CLOEXEC}, 27004e86ffSlogin file::{File, FileMode}, 280fb515b0SLoGin open::{do_faccessat, do_fchmodat, do_sys_open}, 299b0abe6dSLoGin utils::{rsplit_path, user_path_at}, 301d37ca6dSDonkey Kane Dirent, FileType, IndexNode, FSMAKER, MAX_PATHLEN, ROOT_INODE, 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 /// 最后访问时间 11367b48188Shoumkh atime: TimeSpec, 11467b48188Shoumkh /// 最后修改时间 11567b48188Shoumkh mtime: TimeSpec, 11667b48188Shoumkh /// 最后状态变化时间 11767b48188Shoumkh ctime: TimeSpec, 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, 13267b48188Shoumkh atime: TimeSpec { 13367b48188Shoumkh tv_sec: 0, 13467b48188Shoumkh tv_nsec: 0, 13567b48188Shoumkh }, 13667b48188Shoumkh mtime: TimeSpec { 13767b48188Shoumkh tv_sec: 0, 13867b48188Shoumkh tv_nsec: 0, 13967b48188Shoumkh }, 14067b48188Shoumkh ctime: TimeSpec { 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 /// 最后访问时间 183b4eb05a1STTaq stx_atime: TimeSpec, 184b4eb05a1STTaq /// 文件创建时间 185b4eb05a1STTaq stx_btime: TimeSpec, 186b4eb05a1STTaq /// 最后状态变化时间 187b4eb05a1STTaq stx_ctime: TimeSpec, 188b4eb05a1STTaq /// 最后修改时间 189b4eb05a1STTaq stx_mtime: TimeSpec, 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, 220b4eb05a1STTaq stx_atime: TimeSpec { 221b4eb05a1STTaq tv_sec: 0, 222b4eb05a1STTaq tv_nsec: 0, 223b4eb05a1STTaq }, 224b4eb05a1STTaq stx_btime: TimeSpec { 225b4eb05a1STTaq tv_sec: 0, 226b4eb05a1STTaq tv_nsec: 0, 227b4eb05a1STTaq }, 228b4eb05a1STTaq stx_ctime: TimeSpec { 229b4eb05a1STTaq tv_sec: 0, 230b4eb05a1STTaq tv_nsec: 0, 231b4eb05a1STTaq }, 232b4eb05a1STTaq stx_mtime: TimeSpec { 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 } 455ab5c8ca4Slogin impl Syscall { 456ab5c8ca4Slogin /// @brief 为当前进程打开一个文件 457ab5c8ca4Slogin /// 458ab5c8ca4Slogin /// @param path 文件路径 459ab5c8ca4Slogin /// @param o_flags 打开文件的标志位 460ab5c8ca4Slogin /// 461ab5c8ca4Slogin /// @return 文件描述符编号,或者是错误码 4620fb515b0SLoGin pub fn open( 46382df0a13Shmt path: *const u8, 46482df0a13Shmt o_flags: u32, 46582df0a13Shmt mode: u32, 4660fb515b0SLoGin follow_symlink: bool, 4670fb515b0SLoGin ) -> Result<usize, SystemError> { 46882df0a13Shmt let path = check_and_clone_cstr(path, Some(MAX_PATHLEN))?; 46982df0a13Shmt let open_flags: FileMode = FileMode::from_bits(o_flags).ok_or(SystemError::EINVAL)?; 470b5b571e0SLoGin let mode = ModeType::from_bits(mode).ok_or(SystemError::EINVAL)?; 47182df0a13Shmt return do_sys_open( 47282df0a13Shmt AtFlags::AT_FDCWD.bits(), 47382df0a13Shmt &path, 47482df0a13Shmt open_flags, 47582df0a13Shmt mode, 47682df0a13Shmt follow_symlink, 47782df0a13Shmt ); 478ab5c8ca4Slogin } 479ab5c8ca4Slogin 4800fb515b0SLoGin pub fn openat( 4810fb515b0SLoGin dirfd: i32, 48282df0a13Shmt path: *const u8, 48382df0a13Shmt o_flags: u32, 48482df0a13Shmt mode: u32, 4850fb515b0SLoGin follow_symlink: bool, 4860fb515b0SLoGin ) -> Result<usize, SystemError> { 48782df0a13Shmt let path = check_and_clone_cstr(path, Some(MAX_PATHLEN))?; 48882df0a13Shmt let open_flags: FileMode = FileMode::from_bits(o_flags).ok_or(SystemError::EINVAL)?; 489b5b571e0SLoGin let mode = ModeType::from_bits(mode).ok_or(SystemError::EINVAL)?; 49082df0a13Shmt return do_sys_open(dirfd, &path, open_flags, mode, follow_symlink); 491ab5c8ca4Slogin } 492ab5c8ca4Slogin 493ab5c8ca4Slogin /// @brief 关闭文件 494ab5c8ca4Slogin /// 495ab5c8ca4Slogin /// @param fd 文件描述符编号 496ab5c8ca4Slogin /// 497ab5c8ca4Slogin /// @return 成功返回0,失败返回错误码 498ab5c8ca4Slogin pub fn close(fd: usize) -> Result<usize, SystemError> { 4991496ba7bSLoGin let binding = ProcessManager::current_pcb().fd_table(); 5001496ba7bSLoGin let mut fd_table_guard = binding.write(); 5011496ba7bSLoGin 5026046f775S裕依 fd_table_guard.drop_fd(fd as i32).map(|_| 0) 503ab5c8ca4Slogin } 504ab5c8ca4Slogin 50540314b30SXiaoye Zheng /// @brief 发送命令到文件描述符对应的设备, 50640314b30SXiaoye Zheng /// 50740314b30SXiaoye Zheng /// @param fd 文件描述符编号 50840314b30SXiaoye Zheng /// @param cmd 设备相关的请求类型 50940314b30SXiaoye Zheng /// 51040314b30SXiaoye Zheng /// @return Ok(usize) 成功返回0 51140314b30SXiaoye Zheng /// @return Err(SystemError) 读取失败,返回posix错误码 51240314b30SXiaoye Zheng pub fn ioctl(fd: usize, cmd: u32, data: usize) -> Result<usize, SystemError> { 51340314b30SXiaoye Zheng let binding = ProcessManager::current_pcb().fd_table(); 51440314b30SXiaoye Zheng let fd_table_guard = binding.read(); 51540314b30SXiaoye Zheng 51640314b30SXiaoye Zheng let file = fd_table_guard 51740314b30SXiaoye Zheng .get_file_by_fd(fd as i32) 51840314b30SXiaoye Zheng .ok_or(SystemError::EBADF)?; 51940314b30SXiaoye Zheng 52040314b30SXiaoye Zheng // drop guard 以避免无法调度的问题 52140314b30SXiaoye Zheng drop(fd_table_guard); 522*dfe53cf0SGnoCiYeH let r = file.inode().ioctl(cmd, data, &file.private_data.lock()); 52340314b30SXiaoye Zheng return r; 52440314b30SXiaoye Zheng } 52540314b30SXiaoye Zheng 526ab5c8ca4Slogin /// @brief 根据文件描述符,读取文件数据。尝试读取的数据长度与buf的长度相同。 527ab5c8ca4Slogin /// 528ab5c8ca4Slogin /// @param fd 文件描述符编号 52927b967a3S裕依 /// @param buf 输出缓冲区 530ab5c8ca4Slogin /// 531ab5c8ca4Slogin /// @return Ok(usize) 成功读取的数据的字节数 532ab5c8ca4Slogin /// @return Err(SystemError) 读取失败,返回posix错误码 533ab5c8ca4Slogin pub fn read(fd: i32, buf: &mut [u8]) -> Result<usize, SystemError> { 5341496ba7bSLoGin let binding = ProcessManager::current_pcb().fd_table(); 5351496ba7bSLoGin let fd_table_guard = binding.read(); 5361496ba7bSLoGin 5371496ba7bSLoGin let file = fd_table_guard.get_file_by_fd(fd); 538ab5c8ca4Slogin if file.is_none() { 539ab5c8ca4Slogin return Err(SystemError::EBADF); 540ab5c8ca4Slogin } 5411496ba7bSLoGin // drop guard 以避免无法调度的问题 5421496ba7bSLoGin drop(fd_table_guard); 5431496ba7bSLoGin let file = file.unwrap(); 544ab5c8ca4Slogin 545*dfe53cf0SGnoCiYeH return file.read(buf.len(), buf); 546ab5c8ca4Slogin } 547ab5c8ca4Slogin 548ab5c8ca4Slogin /// @brief 根据文件描述符,向文件写入数据。尝试写入的数据长度与buf的长度相同。 549ab5c8ca4Slogin /// 550ab5c8ca4Slogin /// @param fd 文件描述符编号 55127b967a3S裕依 /// @param buf 输入缓冲区 552ab5c8ca4Slogin /// 553ab5c8ca4Slogin /// @return Ok(usize) 成功写入的数据的字节数 554ab5c8ca4Slogin /// @return Err(SystemError) 写入失败,返回posix错误码 555ab5c8ca4Slogin pub fn write(fd: i32, buf: &[u8]) -> Result<usize, SystemError> { 5561496ba7bSLoGin let binding = ProcessManager::current_pcb().fd_table(); 5571496ba7bSLoGin let fd_table_guard = binding.read(); 558ab5c8ca4Slogin 5591496ba7bSLoGin let file = fd_table_guard 5601496ba7bSLoGin .get_file_by_fd(fd) 5611496ba7bSLoGin .ok_or(SystemError::EBADF)?; 5621496ba7bSLoGin 5631496ba7bSLoGin // drop guard 以避免无法调度的问题 5641496ba7bSLoGin drop(fd_table_guard); 565*dfe53cf0SGnoCiYeH return file.write(buf.len(), buf); 566ab5c8ca4Slogin } 567ab5c8ca4Slogin 568ab5c8ca4Slogin /// @brief 调整文件操作指针的位置 569ab5c8ca4Slogin /// 570ab5c8ca4Slogin /// @param fd 文件描述符编号 571ab5c8ca4Slogin /// @param seek 调整的方式 572ab5c8ca4Slogin /// 573ab5c8ca4Slogin /// @return Ok(usize) 调整后,文件访问指针相对于文件头部的偏移量 574ab5c8ca4Slogin /// @return Err(SystemError) 调整失败,返回posix错误码 57582df0a13Shmt pub fn lseek(fd: i32, offset: i64, seek: u32) -> Result<usize, SystemError> { 57682df0a13Shmt let seek = match seek { 57782df0a13Shmt SEEK_SET => Ok(SeekFrom::SeekSet(offset)), 57882df0a13Shmt SEEK_CUR => Ok(SeekFrom::SeekCurrent(offset)), 57982df0a13Shmt SEEK_END => Ok(SeekFrom::SeekEnd(offset)), 58082df0a13Shmt SEEK_MAX => Ok(SeekFrom::SeekEnd(0)), 58182df0a13Shmt _ => Err(SystemError::EINVAL), 58282df0a13Shmt }?; 58382df0a13Shmt 5841496ba7bSLoGin let binding = ProcessManager::current_pcb().fd_table(); 5851496ba7bSLoGin let fd_table_guard = binding.read(); 5861496ba7bSLoGin let file = fd_table_guard 5871496ba7bSLoGin .get_file_by_fd(fd) 5881496ba7bSLoGin .ok_or(SystemError::EBADF)?; 5891496ba7bSLoGin 5901496ba7bSLoGin // drop guard 以避免无法调度的问题 5911496ba7bSLoGin drop(fd_table_guard); 592*dfe53cf0SGnoCiYeH return file.lseek(seek); 593004e86ffSlogin } 594004e86ffSlogin 59527b967a3S裕依 /// # sys_pread64 系统调用的实际执行函数 59627b967a3S裕依 /// 59727b967a3S裕依 /// ## 参数 59827b967a3S裕依 /// - `fd`: 文件描述符 59927b967a3S裕依 /// - `buf`: 读出缓冲区 60027b967a3S裕依 /// - `len`: 要读取的字节数 60127b967a3S裕依 /// - `offset`: 文件偏移量 60227b967a3S裕依 pub fn pread(fd: i32, buf: &mut [u8], len: usize, offset: usize) -> Result<usize, SystemError> { 60327b967a3S裕依 let binding = ProcessManager::current_pcb().fd_table(); 60427b967a3S裕依 let fd_table_guard = binding.read(); 60527b967a3S裕依 60627b967a3S裕依 let file = fd_table_guard.get_file_by_fd(fd); 60727b967a3S裕依 if file.is_none() { 60827b967a3S裕依 return Err(SystemError::EBADF); 60927b967a3S裕依 } 61027b967a3S裕依 // drop guard 以避免无法调度的问题 61127b967a3S裕依 drop(fd_table_guard); 61227b967a3S裕依 let file = file.unwrap(); 61327b967a3S裕依 614*dfe53cf0SGnoCiYeH return file.pread(offset, len, buf); 61527b967a3S裕依 } 61627b967a3S裕依 61727b967a3S裕依 /// # sys_pwrite64 系统调用的实际执行函数 61827b967a3S裕依 /// 61927b967a3S裕依 /// ## 参数 62027b967a3S裕依 /// - `fd`: 文件描述符 62127b967a3S裕依 /// - `buf`: 写入缓冲区 62227b967a3S裕依 /// - `len`: 要写入的字节数 62327b967a3S裕依 /// - `offset`: 文件偏移量 62427b967a3S裕依 pub fn pwrite(fd: i32, buf: &[u8], len: usize, offset: usize) -> Result<usize, SystemError> { 62527b967a3S裕依 let binding = ProcessManager::current_pcb().fd_table(); 62627b967a3S裕依 let fd_table_guard = binding.read(); 62727b967a3S裕依 62827b967a3S裕依 let file = fd_table_guard.get_file_by_fd(fd); 62927b967a3S裕依 if file.is_none() { 63027b967a3S裕依 return Err(SystemError::EBADF); 63127b967a3S裕依 } 63227b967a3S裕依 // drop guard 以避免无法调度的问题 63327b967a3S裕依 drop(fd_table_guard); 63427b967a3S裕依 let file = file.unwrap(); 63527b967a3S裕依 636*dfe53cf0SGnoCiYeH return file.pwrite(offset, len, buf); 63727b967a3S裕依 } 63827b967a3S裕依 639004e86ffSlogin /// @brief 切换工作目录 640004e86ffSlogin /// 641004e86ffSlogin /// @param dest_path 目标路径 642004e86ffSlogin /// 643004e86ffSlogin /// @return 返回码 描述 644004e86ffSlogin /// 0 | 成功 645004e86ffSlogin /// 646004e86ffSlogin /// EACCESS | 权限不足 647004e86ffSlogin /// 648004e86ffSlogin /// ELOOP | 解析path时遇到路径循环 649004e86ffSlogin /// 650004e86ffSlogin /// ENAMETOOLONG | 路径名过长 651004e86ffSlogin /// 652004e86ffSlogin /// ENOENT | 目标文件或目录不存在 653004e86ffSlogin /// 654004e86ffSlogin /// ENODIR | 检索期间发现非目录项 655004e86ffSlogin /// 656004e86ffSlogin /// ENOMEM | 系统内存不足 657004e86ffSlogin /// 658004e86ffSlogin /// EFAULT | 错误的地址 659004e86ffSlogin /// 660004e86ffSlogin /// ENAMETOOLONG | 路径过长 66182df0a13Shmt pub fn chdir(path: *const u8) -> Result<usize, SystemError> { 66282df0a13Shmt if path.is_null() { 66382df0a13Shmt return Err(SystemError::EFAULT); 66482df0a13Shmt } 66582df0a13Shmt 66682df0a13Shmt let path = check_and_clone_cstr(path, Some(MAX_PATHLEN))?; 6671496ba7bSLoGin let proc = ProcessManager::current_pcb(); 668ab5c8ca4Slogin // Copy path to kernel space to avoid some security issues 6691496ba7bSLoGin let mut new_path = String::from(""); 670b5b571e0SLoGin if !path.is_empty() { 6711496ba7bSLoGin let cwd = match path.as_bytes()[0] { 6721496ba7bSLoGin b'/' => String::from("/"), 6731496ba7bSLoGin _ => proc.basic().cwd(), 6741496ba7bSLoGin }; 675b5b571e0SLoGin let mut cwd_vec: Vec<_> = cwd.split('/').filter(|&x| !x.is_empty()).collect(); 676b5b571e0SLoGin let path_split = path.split('/').filter(|&x| !x.is_empty()); 6771496ba7bSLoGin for seg in path_split { 6781496ba7bSLoGin if seg == ".." { 6791496ba7bSLoGin cwd_vec.pop(); 6801496ba7bSLoGin } else if seg == "." { 6811496ba7bSLoGin // 当前目录 6821496ba7bSLoGin } else { 6831496ba7bSLoGin cwd_vec.push(seg); 6841496ba7bSLoGin } 6851496ba7bSLoGin } 6861496ba7bSLoGin //proc.basic().set_path(String::from("")); 6871496ba7bSLoGin for seg in cwd_vec { 688b5b571e0SLoGin new_path.push('/'); 6891496ba7bSLoGin new_path.push_str(seg); 6901496ba7bSLoGin } 691b5b571e0SLoGin if new_path.is_empty() { 6921496ba7bSLoGin new_path = String::from("/"); 6931496ba7bSLoGin } 6941496ba7bSLoGin } 695a03c4f9dSLoGin let inode = 696a03c4f9dSLoGin match ROOT_INODE().lookup_follow_symlink(&new_path, VFS_MAX_FOLLOW_SYMLINK_TIMES) { 69782df0a13Shmt Err(_) => { 698ab5c8ca4Slogin return Err(SystemError::ENOENT); 699004e86ffSlogin } 700004e86ffSlogin Ok(i) => i, 701004e86ffSlogin }; 7021496ba7bSLoGin let metadata = inode.metadata()?; 7031496ba7bSLoGin if metadata.file_type == FileType::Dir { 704b5b571e0SLoGin proc.basic_mut().set_cwd(new_path); 705ab5c8ca4Slogin return Ok(0); 706004e86ffSlogin } else { 707ab5c8ca4Slogin return Err(SystemError::ENOTDIR); 708004e86ffSlogin } 709004e86ffSlogin } 7101496ba7bSLoGin 7111496ba7bSLoGin /// @brief 获取当前进程的工作目录路径 7121496ba7bSLoGin /// 7131496ba7bSLoGin /// @param buf 指向缓冲区的指针 7141496ba7bSLoGin /// @param size 缓冲区的大小 7151496ba7bSLoGin /// 7161496ba7bSLoGin /// @return 成功,返回的指针指向包含工作目录路径的字符串 7171496ba7bSLoGin /// @return 错误,没有足够的空间 7181496ba7bSLoGin pub fn getcwd(buf: &mut [u8]) -> Result<VirtAddr, SystemError> { 7191496ba7bSLoGin let proc = ProcessManager::current_pcb(); 7201496ba7bSLoGin let cwd = proc.basic().cwd(); 7211496ba7bSLoGin 7221496ba7bSLoGin let cwd_bytes = cwd.as_bytes(); 7231496ba7bSLoGin let cwd_len = cwd_bytes.len(); 7241496ba7bSLoGin if cwd_len + 1 > buf.len() { 7251496ba7bSLoGin return Err(SystemError::ENOMEM); 726004e86ffSlogin } 7271496ba7bSLoGin buf[..cwd_len].copy_from_slice(cwd_bytes); 7281496ba7bSLoGin buf[cwd_len] = 0; 7291496ba7bSLoGin 7301496ba7bSLoGin return Ok(VirtAddr::new(buf.as_ptr() as usize)); 731004e86ffSlogin } 732004e86ffSlogin 733004e86ffSlogin /// @brief 获取目录中的数据 734004e86ffSlogin /// 735ab5c8ca4Slogin /// TODO: 这个函数的语义与Linux不一致,需要修改!!! 736ab5c8ca4Slogin /// 737004e86ffSlogin /// @param fd 文件描述符号 738ab5c8ca4Slogin /// @param buf 输出缓冲区 739ab5c8ca4Slogin /// 740ab5c8ca4Slogin /// @return 成功返回读取的字节数,失败返回错误码 741ab5c8ca4Slogin pub fn getdents(fd: i32, buf: &mut [u8]) -> Result<usize, SystemError> { 742ab5c8ca4Slogin let dirent = 743ab5c8ca4Slogin unsafe { (buf.as_mut_ptr() as *mut Dirent).as_mut() }.ok_or(SystemError::EFAULT)?; 744004e86ffSlogin 74546e234aeSLoGin if fd < 0 || fd as usize > FileDescriptorVec::PROCESS_MAX_FD { 746ab5c8ca4Slogin return Err(SystemError::EBADF); 747004e86ffSlogin } 748004e86ffSlogin 749004e86ffSlogin // 获取fd 7501496ba7bSLoGin let binding = ProcessManager::current_pcb().fd_table(); 7511496ba7bSLoGin let fd_table_guard = binding.read(); 7521496ba7bSLoGin let file = fd_table_guard 7531496ba7bSLoGin .get_file_by_fd(fd) 7541496ba7bSLoGin .ok_or(SystemError::EBADF)?; 755004e86ffSlogin 7561496ba7bSLoGin // drop guard 以避免无法调度的问题 7571496ba7bSLoGin drop(fd_table_guard); 75834e6d6c8Syuyi2439 759*dfe53cf0SGnoCiYeH let res = file.readdir(dirent).map(|x| x as usize); 76034e6d6c8Syuyi2439 76134e6d6c8Syuyi2439 return res; 762004e86ffSlogin } 763004e86ffSlogin 764004e86ffSlogin /// @brief 创建文件夹 765004e86ffSlogin /// 766004e86ffSlogin /// @param path(r8) 路径 / mode(r9) 模式 767004e86ffSlogin /// 768004e86ffSlogin /// @return uint64_t 负数错误码 / 0表示成功 76982df0a13Shmt pub fn mkdir(path: *const u8, mode: usize) -> Result<usize, SystemError> { 77082df0a13Shmt let path = check_and_clone_cstr(path, Some(MAX_PATHLEN))?; 77182df0a13Shmt return do_mkdir(&path, FileMode::from_bits_truncate(mode as u32)).map(|x| x as usize); 772004e86ffSlogin } 773004e86ffSlogin 7744695947eSChenzx /// **创建硬连接的系统调用** 7754695947eSChenzx /// 7764695947eSChenzx /// ## 参数 7774695947eSChenzx /// 7784695947eSChenzx /// - 'oldfd': 用于解析源文件路径的文件描述符 7794695947eSChenzx /// - 'old': 源文件路径 7804695947eSChenzx /// - 'newfd': 用于解析新文件路径的文件描述符 7814695947eSChenzx /// - 'new': 新文件将创建的路径 7824695947eSChenzx /// - 'flags': 标志位,仅以位或方式包含AT_EMPTY_PATH和AT_SYMLINK_FOLLOW 7834695947eSChenzx /// 7844695947eSChenzx /// 7854695947eSChenzx pub fn do_linkat( 7864695947eSChenzx oldfd: i32, 7874695947eSChenzx old: &str, 7884695947eSChenzx newfd: i32, 7894695947eSChenzx new: &str, 7904695947eSChenzx flags: AtFlags, 7914695947eSChenzx ) -> Result<usize, SystemError> { 7924695947eSChenzx // flag包含其他未规定值时返回EINVAL 7934695947eSChenzx if !(AtFlags::AT_EMPTY_PATH | AtFlags::AT_SYMLINK_FOLLOW).contains(flags) { 7944695947eSChenzx return Err(SystemError::EINVAL); 7954695947eSChenzx } 7964695947eSChenzx // TODO AT_EMPTY_PATH标志启用时,进行调用者CAP_DAC_READ_SEARCH或相似的检查 7974695947eSChenzx let symlink_times = if flags.contains(AtFlags::AT_SYMLINK_FOLLOW) { 7984e4c8c41SLoGin 0_usize 7994695947eSChenzx } else { 8004695947eSChenzx VFS_MAX_FOLLOW_SYMLINK_TIMES 8014695947eSChenzx }; 8024695947eSChenzx let pcb = ProcessManager::current_pcb(); 8034695947eSChenzx 8044695947eSChenzx // 得到源路径的inode 8054695947eSChenzx let old_inode: Arc<dyn IndexNode> = if old.is_empty() { 8064695947eSChenzx if flags.contains(AtFlags::AT_EMPTY_PATH) { 8074695947eSChenzx // 在AT_EMPTY_PATH启用时,old可以为空,old_inode实际为oldfd所指文件,但该文件不能为目录。 8084695947eSChenzx let binding = pcb.fd_table(); 8094695947eSChenzx let fd_table_guard = binding.read(); 8104695947eSChenzx let file = fd_table_guard 8114695947eSChenzx .get_file_by_fd(oldfd) 8124695947eSChenzx .ok_or(SystemError::EBADF)?; 813*dfe53cf0SGnoCiYeH let old_inode = file.inode(); 8144695947eSChenzx old_inode 8154695947eSChenzx } else { 8164695947eSChenzx return Err(SystemError::ENONET); 8174695947eSChenzx } 8184695947eSChenzx } else { 8194695947eSChenzx let (old_begin_inode, old_remain_path) = user_path_at(&pcb, oldfd, old)?; 8204695947eSChenzx old_begin_inode.lookup_follow_symlink(&old_remain_path, symlink_times)? 8214695947eSChenzx }; 8224695947eSChenzx 8234695947eSChenzx // old_inode为目录时返回EPERM 8244695947eSChenzx if old_inode.metadata().unwrap().file_type == FileType::Dir { 8254695947eSChenzx return Err(SystemError::EPERM); 8264695947eSChenzx } 8274695947eSChenzx 8284695947eSChenzx // 得到新创建节点的父节点 8294695947eSChenzx let (new_begin_inode, new_remain_path) = user_path_at(&pcb, newfd, new)?; 8304695947eSChenzx let (new_name, new_parent_path) = rsplit_path(&new_remain_path); 8314e4c8c41SLoGin let new_parent = 8324e4c8c41SLoGin new_begin_inode.lookup_follow_symlink(new_parent_path.unwrap_or("/"), symlink_times)?; 8334695947eSChenzx 8344695947eSChenzx // 被调用者利用downcast_ref判断两inode是否为同一文件系统 8354e4c8c41SLoGin return new_parent.link(new_name, &old_inode).map(|_| 0); 8364695947eSChenzx } 8374695947eSChenzx 8384695947eSChenzx pub fn link(old: *const u8, new: *const u8) -> Result<usize, SystemError> { 8394695947eSChenzx let get_path = |cstr: *const u8| -> Result<String, SystemError> { 8404695947eSChenzx let res = check_and_clone_cstr(cstr, Some(MAX_PATHLEN))?; 8414695947eSChenzx if res.len() >= MAX_PATHLEN { 8424695947eSChenzx return Err(SystemError::ENAMETOOLONG); 8434695947eSChenzx } 8444695947eSChenzx if res.is_empty() { 8454695947eSChenzx return Err(SystemError::ENOENT); 8464695947eSChenzx } 8474695947eSChenzx Ok(res) 8484695947eSChenzx }; 8494695947eSChenzx let old = get_path(old)?; 8504695947eSChenzx let new = get_path(new)?; 8514695947eSChenzx return Self::do_linkat( 8524e4c8c41SLoGin AtFlags::AT_FDCWD.bits(), 8534695947eSChenzx &old, 8544e4c8c41SLoGin AtFlags::AT_FDCWD.bits(), 8554695947eSChenzx &new, 8564695947eSChenzx AtFlags::empty(), 8574695947eSChenzx ); 8584695947eSChenzx } 8594695947eSChenzx 8604695947eSChenzx pub fn linkat( 8614695947eSChenzx oldfd: i32, 8624695947eSChenzx old: *const u8, 8634695947eSChenzx newfd: i32, 8644695947eSChenzx new: *const u8, 8654695947eSChenzx flags: i32, 8664695947eSChenzx ) -> Result<usize, SystemError> { 8674695947eSChenzx let old = check_and_clone_cstr(old, Some(MAX_PATHLEN))?; 8684695947eSChenzx let new = check_and_clone_cstr(new, Some(MAX_PATHLEN))?; 8694695947eSChenzx if old.len() >= MAX_PATHLEN || new.len() >= MAX_PATHLEN { 8704695947eSChenzx return Err(SystemError::ENAMETOOLONG); 8714695947eSChenzx } 8724695947eSChenzx // old 根据flags & AtFlags::AT_EMPTY_PATH判空 8734695947eSChenzx if new.is_empty() { 8744695947eSChenzx return Err(SystemError::ENOENT); 8754695947eSChenzx } 8764695947eSChenzx let flags = AtFlags::from_bits(flags).ok_or(SystemError::EINVAL)?; 8774695947eSChenzx Self::do_linkat(oldfd, &old, newfd, &new, flags) 8784695947eSChenzx } 8794695947eSChenzx 880ab5c8ca4Slogin /// **删除文件夹、取消文件的链接、删除文件的系统调用** 881004e86ffSlogin /// 882ab5c8ca4Slogin /// ## 参数 883004e86ffSlogin /// 884ab5c8ca4Slogin /// - `dirfd`:文件夹的文件描述符.目前暂未实现 885ab5c8ca4Slogin /// - `pathname`:文件夹的路径 886ab5c8ca4Slogin /// - `flags`:标志位 887004e86ffSlogin /// 888004e86ffSlogin /// 88982df0a13Shmt pub fn unlinkat(dirfd: i32, path: *const u8, flags: u32) -> Result<usize, SystemError> { 890bf4a4899SLoGin let flags = AtFlags::from_bits(flags as i32).ok_or(SystemError::EINVAL)?; 891004e86ffSlogin 89282df0a13Shmt let path = check_and_clone_cstr(path, Some(MAX_PATHLEN))?; 89382df0a13Shmt 894bf4a4899SLoGin if flags.contains(AtFlags::AT_REMOVEDIR) { 895004e86ffSlogin // kdebug!("rmdir"); 89682df0a13Shmt match do_remove_dir(dirfd, &path) { 897004e86ffSlogin Err(err) => { 898ab5c8ca4Slogin return Err(err); 899004e86ffSlogin } 900004e86ffSlogin Ok(_) => { 901ab5c8ca4Slogin return Ok(0); 902004e86ffSlogin } 903004e86ffSlogin } 904004e86ffSlogin } 905004e86ffSlogin 90682df0a13Shmt match do_unlink_at(dirfd, &path) { 907004e86ffSlogin Err(err) => { 908ab5c8ca4Slogin return Err(err); 909004e86ffSlogin } 910004e86ffSlogin Ok(_) => { 911ab5c8ca4Slogin return Ok(0); 912004e86ffSlogin } 913004e86ffSlogin } 914004e86ffSlogin } 9152b771e32SGou Ngai 91682df0a13Shmt pub fn rmdir(path: *const u8) -> Result<usize, SystemError> { 91782df0a13Shmt let path = check_and_clone_cstr(path, Some(MAX_PATHLEN))?; 91882df0a13Shmt return do_remove_dir(AtFlags::AT_FDCWD.bits(), &path).map(|v| v as usize); 9195eeefb8cSChenzx } 9205eeefb8cSChenzx 92182df0a13Shmt pub fn unlink(path: *const u8) -> Result<usize, SystemError> { 92282df0a13Shmt let path = check_and_clone_cstr(path, Some(MAX_PATHLEN))?; 92382df0a13Shmt return do_unlink_at(AtFlags::AT_FDCWD.bits(), &path).map(|v| v as usize); 924bf4a4899SLoGin } 925bf4a4899SLoGin 9269e481b3bSTTaq /// # 修改文件名 9279e481b3bSTTaq /// 9289e481b3bSTTaq /// 9299e481b3bSTTaq /// ## 参数 9309e481b3bSTTaq /// 9319e481b3bSTTaq /// - oldfd: 源文件描述符 9329e481b3bSTTaq /// - filename_from: 源文件路径 9339e481b3bSTTaq /// - newfd: 目标文件描述符 9349e481b3bSTTaq /// - filename_to: 目标文件路径 9359e481b3bSTTaq /// - flags: 标志位 9369e481b3bSTTaq /// 9379e481b3bSTTaq /// 9389e481b3bSTTaq /// ## 返回值 9399e481b3bSTTaq /// - Ok(返回值类型): 返回值的说明 9409e481b3bSTTaq /// - Err(错误值类型): 错误的说明 9419e481b3bSTTaq /// 9429e481b3bSTTaq pub fn do_renameat2( 9439e481b3bSTTaq oldfd: i32, 9449e481b3bSTTaq filename_from: *const u8, 9459e481b3bSTTaq newfd: i32, 9469e481b3bSTTaq filename_to: *const u8, 9479e481b3bSTTaq _flags: u32, 9489e481b3bSTTaq ) -> Result<usize, SystemError> { 9499e481b3bSTTaq let filename_from = check_and_clone_cstr(filename_from, Some(MAX_PATHLEN)).unwrap(); 9509e481b3bSTTaq let filename_to = check_and_clone_cstr(filename_to, Some(MAX_PATHLEN)).unwrap(); 9519e481b3bSTTaq // 文件名过长 952b5b571e0SLoGin if filename_from.len() > MAX_PATHLEN || filename_to.len() > MAX_PATHLEN { 9539e481b3bSTTaq return Err(SystemError::ENAMETOOLONG); 9549e481b3bSTTaq } 9559e481b3bSTTaq 9569e481b3bSTTaq //获取pcb,文件节点 9579e481b3bSTTaq let pcb = ProcessManager::current_pcb(); 9589e481b3bSTTaq let (_old_inode_begin, old_remain_path) = user_path_at(&pcb, oldfd, &filename_from)?; 9599e481b3bSTTaq let (_new_inode_begin, new_remain_path) = user_path_at(&pcb, newfd, &filename_to)?; 9609e481b3bSTTaq //获取父目录 9619e481b3bSTTaq let (old_filename, old_parent_path) = rsplit_path(&old_remain_path); 9629e481b3bSTTaq let old_parent_inode = ROOT_INODE() 9639e481b3bSTTaq .lookup_follow_symlink(old_parent_path.unwrap_or("/"), VFS_MAX_FOLLOW_SYMLINK_TIMES)?; 9649e481b3bSTTaq let (new_filename, new_parent_path) = rsplit_path(&new_remain_path); 9659e481b3bSTTaq let new_parent_inode = ROOT_INODE() 9669e481b3bSTTaq .lookup_follow_symlink(new_parent_path.unwrap_or("/"), VFS_MAX_FOLLOW_SYMLINK_TIMES)?; 9679e481b3bSTTaq old_parent_inode.move_to(old_filename, &new_parent_inode, new_filename)?; 9689e481b3bSTTaq return Ok(0); 9699e481b3bSTTaq } 9709e481b3bSTTaq 971ab5c8ca4Slogin /// @brief 根据提供的文件描述符的fd,复制对应的文件结构体,并返回新复制的文件结构体对应的fd 972ab5c8ca4Slogin pub fn dup(oldfd: i32) -> Result<usize, SystemError> { 9731496ba7bSLoGin let binding = ProcessManager::current_pcb().fd_table(); 9741496ba7bSLoGin let mut fd_table_guard = binding.write(); 975ab5c8ca4Slogin 9761496ba7bSLoGin let old_file = fd_table_guard 9771496ba7bSLoGin .get_file_by_fd(oldfd) 9781496ba7bSLoGin .ok_or(SystemError::EBADF)?; 9791496ba7bSLoGin 980*dfe53cf0SGnoCiYeH let new_file = old_file.try_clone().ok_or(SystemError::EBADF)?; 9812b771e32SGou Ngai // 申请文件描述符,并把文件对象存入其中 9821496ba7bSLoGin let res = fd_table_guard.alloc_fd(new_file, None).map(|x| x as usize); 9832b771e32SGou Ngai return res; 9842b771e32SGou Ngai } 9852b771e32SGou Ngai 986ab5c8ca4Slogin /// 根据提供的文件描述符的fd,和指定新fd,复制对应的文件结构体, 987ab5c8ca4Slogin /// 并返回新复制的文件结构体对应的fd. 988ab5c8ca4Slogin /// 如果新fd已经打开,则会先关闭新fd. 989ab5c8ca4Slogin /// 990ab5c8ca4Slogin /// ## 参数 991ab5c8ca4Slogin /// 992ab5c8ca4Slogin /// - `oldfd`:旧文件描述符 993ab5c8ca4Slogin /// - `newfd`:新文件描述符 994ab5c8ca4Slogin /// 995ab5c8ca4Slogin /// ## 返回值 996ab5c8ca4Slogin /// 997ab5c8ca4Slogin /// - 成功:新文件描述符 998ab5c8ca4Slogin /// - 失败:错误码 999ab5c8ca4Slogin pub fn dup2(oldfd: i32, newfd: i32) -> Result<usize, SystemError> { 10001496ba7bSLoGin let binding = ProcessManager::current_pcb().fd_table(); 10011496ba7bSLoGin let mut fd_table_guard = binding.write(); 10021496ba7bSLoGin return Self::do_dup2(oldfd, newfd, &mut fd_table_guard); 10031496ba7bSLoGin } 10041496ba7bSLoGin 10051496ba7bSLoGin fn do_dup2( 10061496ba7bSLoGin oldfd: i32, 10071496ba7bSLoGin newfd: i32, 10081496ba7bSLoGin fd_table_guard: &mut RwLockWriteGuard<'_, FileDescriptorVec>, 10091496ba7bSLoGin ) -> Result<usize, SystemError> { 10102b771e32SGou Ngai // 确认oldfd, newid是否有效 10111496ba7bSLoGin if !(FileDescriptorVec::validate_fd(oldfd) && FileDescriptorVec::validate_fd(newfd)) { 10121496ba7bSLoGin return Err(SystemError::EBADF); 10131496ba7bSLoGin } 10141496ba7bSLoGin 10152b771e32SGou Ngai if oldfd == newfd { 10162b771e32SGou Ngai // 若oldfd与newfd相等 1017ab5c8ca4Slogin return Ok(newfd as usize); 10182b771e32SGou Ngai } 10191496ba7bSLoGin let new_exists = fd_table_guard.get_file_by_fd(newfd).is_some(); 10201496ba7bSLoGin if new_exists { 10212b771e32SGou Ngai // close newfd 1022b5b571e0SLoGin if fd_table_guard.drop_fd(newfd).is_err() { 10232b771e32SGou Ngai // An I/O error occurred while attempting to close fildes2. 10242b771e32SGou Ngai return Err(SystemError::EIO); 10252b771e32SGou Ngai } 10262b771e32SGou Ngai } 10272b771e32SGou Ngai 10281496ba7bSLoGin let old_file = fd_table_guard 10291496ba7bSLoGin .get_file_by_fd(oldfd) 10301496ba7bSLoGin .ok_or(SystemError::EBADF)?; 1031*dfe53cf0SGnoCiYeH let new_file = old_file.try_clone().ok_or(SystemError::EBADF)?; 10322b771e32SGou Ngai // 申请文件描述符,并把文件对象存入其中 10331496ba7bSLoGin let res = fd_table_guard 10341496ba7bSLoGin .alloc_fd(new_file, Some(newfd)) 1035ab5c8ca4Slogin .map(|x| x as usize); 10362b771e32SGou Ngai return res; 10372b771e32SGou Ngai } 10386d81180bSLoGin 10396d81180bSLoGin /// # fcntl 10406d81180bSLoGin /// 10416d81180bSLoGin /// ## 参数 10426d81180bSLoGin /// 10436d81180bSLoGin /// - `fd`:文件描述符 10446d81180bSLoGin /// - `cmd`:命令 10456d81180bSLoGin /// - `arg`:参数 10466d81180bSLoGin pub fn fcntl(fd: i32, cmd: FcntlCommand, arg: i32) -> Result<usize, SystemError> { 10476d81180bSLoGin match cmd { 10486d81180bSLoGin FcntlCommand::DupFd => { 10496d81180bSLoGin if arg < 0 || arg as usize >= FileDescriptorVec::PROCESS_MAX_FD { 10506d81180bSLoGin return Err(SystemError::EBADF); 10516d81180bSLoGin } 10526d81180bSLoGin let arg = arg as usize; 10536d81180bSLoGin for i in arg..FileDescriptorVec::PROCESS_MAX_FD { 10541496ba7bSLoGin let binding = ProcessManager::current_pcb().fd_table(); 10551496ba7bSLoGin let mut fd_table_guard = binding.write(); 105652da9a59SGnoCiYeH if fd_table_guard.get_file_by_fd(i as i32).is_none() { 10571496ba7bSLoGin return Self::do_dup2(fd, i as i32, &mut fd_table_guard); 10586d81180bSLoGin } 10596d81180bSLoGin } 10606d81180bSLoGin return Err(SystemError::EMFILE); 10616d81180bSLoGin } 10626d81180bSLoGin FcntlCommand::GetFd => { 10636d81180bSLoGin // Get file descriptor flags. 10641496ba7bSLoGin let binding = ProcessManager::current_pcb().fd_table(); 10651496ba7bSLoGin let fd_table_guard = binding.read(); 10661496ba7bSLoGin if let Some(file) = fd_table_guard.get_file_by_fd(fd) { 10671496ba7bSLoGin // drop guard 以避免无法调度的问题 10681496ba7bSLoGin drop(fd_table_guard); 10696d81180bSLoGin 1070*dfe53cf0SGnoCiYeH if file.close_on_exec() { 10716d81180bSLoGin return Ok(FD_CLOEXEC as usize); 10726d81180bSLoGin } 10736d81180bSLoGin } 10746d81180bSLoGin return Err(SystemError::EBADF); 10756d81180bSLoGin } 10766d81180bSLoGin FcntlCommand::SetFd => { 10776d81180bSLoGin // Set file descriptor flags. 10781496ba7bSLoGin let binding = ProcessManager::current_pcb().fd_table(); 10791496ba7bSLoGin let fd_table_guard = binding.write(); 10801496ba7bSLoGin 10811496ba7bSLoGin if let Some(file) = fd_table_guard.get_file_by_fd(fd) { 10821496ba7bSLoGin // drop guard 以避免无法调度的问题 10831496ba7bSLoGin drop(fd_table_guard); 10846d81180bSLoGin let arg = arg as u32; 10856d81180bSLoGin if arg & FD_CLOEXEC != 0 { 1086*dfe53cf0SGnoCiYeH file.set_close_on_exec(true); 10876d81180bSLoGin } else { 1088*dfe53cf0SGnoCiYeH file.set_close_on_exec(false); 10896d81180bSLoGin } 10906d81180bSLoGin return Ok(0); 10916d81180bSLoGin } 10926d81180bSLoGin return Err(SystemError::EBADF); 10936d81180bSLoGin } 10946d81180bSLoGin 10956d81180bSLoGin FcntlCommand::GetFlags => { 10966d81180bSLoGin // Get file status flags. 10971496ba7bSLoGin let binding = ProcessManager::current_pcb().fd_table(); 10981496ba7bSLoGin let fd_table_guard = binding.read(); 10991496ba7bSLoGin 11001496ba7bSLoGin if let Some(file) = fd_table_guard.get_file_by_fd(fd) { 11011496ba7bSLoGin // drop guard 以避免无法调度的问题 11021496ba7bSLoGin drop(fd_table_guard); 1103*dfe53cf0SGnoCiYeH return Ok(file.mode().bits() as usize); 11046d81180bSLoGin } 11051496ba7bSLoGin 11066d81180bSLoGin return Err(SystemError::EBADF); 11076d81180bSLoGin } 11086d81180bSLoGin FcntlCommand::SetFlags => { 11096d81180bSLoGin // Set file status flags. 11101496ba7bSLoGin let binding = ProcessManager::current_pcb().fd_table(); 11111496ba7bSLoGin let fd_table_guard = binding.write(); 11121496ba7bSLoGin 11131496ba7bSLoGin if let Some(file) = fd_table_guard.get_file_by_fd(fd) { 11146d81180bSLoGin let arg = arg as u32; 11156d81180bSLoGin let mode = FileMode::from_bits(arg).ok_or(SystemError::EINVAL)?; 11161496ba7bSLoGin // drop guard 以避免无法调度的问题 11171496ba7bSLoGin drop(fd_table_guard); 1118*dfe53cf0SGnoCiYeH file.set_mode(mode)?; 11196d81180bSLoGin return Ok(0); 11206d81180bSLoGin } 11211496ba7bSLoGin 11226d81180bSLoGin return Err(SystemError::EBADF); 11236d81180bSLoGin } 11246d81180bSLoGin _ => { 11256d81180bSLoGin // TODO: unimplemented 11266d81180bSLoGin // 未实现的命令,返回0,不报错。 11276d81180bSLoGin 11286d81180bSLoGin // kwarn!("fcntl: unimplemented command: {:?}, defaults to 0.", cmd); 11296d81180bSLoGin return Ok(0); 11306d81180bSLoGin } 11316d81180bSLoGin } 11326d81180bSLoGin } 11336d81180bSLoGin 11346d81180bSLoGin /// # ftruncate 11356d81180bSLoGin /// 11366d81180bSLoGin /// ## 描述 11376d81180bSLoGin /// 11386d81180bSLoGin /// 改变文件大小. 11396d81180bSLoGin /// 如果文件大小大于原来的大小,那么文件的内容将会被扩展到指定的大小,新的空间将会用0填充. 11406d81180bSLoGin /// 如果文件大小小于原来的大小,那么文件的内容将会被截断到指定的大小. 11416d81180bSLoGin /// 11426d81180bSLoGin /// ## 参数 11436d81180bSLoGin /// 11446d81180bSLoGin /// - `fd`:文件描述符 11456d81180bSLoGin /// - `len`:文件大小 11466d81180bSLoGin /// 11476d81180bSLoGin /// ## 返回值 11486d81180bSLoGin /// 11496d81180bSLoGin /// 如果成功,返回0,否则返回错误码. 11506d81180bSLoGin pub fn ftruncate(fd: i32, len: usize) -> Result<usize, SystemError> { 11511496ba7bSLoGin let binding = ProcessManager::current_pcb().fd_table(); 11521496ba7bSLoGin let fd_table_guard = binding.read(); 11531496ba7bSLoGin 11541496ba7bSLoGin if let Some(file) = fd_table_guard.get_file_by_fd(fd) { 11551496ba7bSLoGin // drop guard 以避免无法调度的问题 11561496ba7bSLoGin drop(fd_table_guard); 1157*dfe53cf0SGnoCiYeH let r = file.ftruncate(len).map(|_| 0); 11586d81180bSLoGin return r; 11596d81180bSLoGin } 11601496ba7bSLoGin 11616d81180bSLoGin return Err(SystemError::EBADF); 11626d81180bSLoGin } 11631496ba7bSLoGin 116467b48188Shoumkh fn do_fstat(fd: i32) -> Result<PosixKstat, SystemError> { 11651496ba7bSLoGin let binding = ProcessManager::current_pcb().fd_table(); 11661496ba7bSLoGin let fd_table_guard = binding.read(); 11671496ba7bSLoGin let file = fd_table_guard 11681496ba7bSLoGin .get_file_by_fd(fd) 11691496ba7bSLoGin .ok_or(SystemError::EBADF)?; 11701496ba7bSLoGin // drop guard 以避免无法调度的问题 11711496ba7bSLoGin drop(fd_table_guard); 11721496ba7bSLoGin 117367b48188Shoumkh let mut kstat = PosixKstat::new(); 117467b48188Shoumkh // 获取文件信息 1175*dfe53cf0SGnoCiYeH let metadata = file.metadata()?; 1176b5b571e0SLoGin kstat.size = metadata.size; 117767b48188Shoumkh kstat.dev_id = metadata.dev_id as u64; 11786b4e7a29SLoGin kstat.inode = metadata.inode_id.into() as u64; 117967b48188Shoumkh kstat.blcok_size = metadata.blk_size as i64; 118067b48188Shoumkh kstat.blocks = metadata.blocks as u64; 118167b48188Shoumkh 118267b48188Shoumkh kstat.atime.tv_sec = metadata.atime.tv_sec; 118367b48188Shoumkh kstat.atime.tv_nsec = metadata.atime.tv_nsec; 118467b48188Shoumkh kstat.mtime.tv_sec = metadata.mtime.tv_sec; 118567b48188Shoumkh kstat.mtime.tv_nsec = metadata.mtime.tv_nsec; 118667b48188Shoumkh kstat.ctime.tv_sec = metadata.ctime.tv_sec; 118767b48188Shoumkh kstat.ctime.tv_nsec = metadata.ctime.tv_nsec; 118867b48188Shoumkh 118967b48188Shoumkh kstat.nlink = metadata.nlinks as u64; 119067b48188Shoumkh kstat.uid = metadata.uid as i32; 119167b48188Shoumkh kstat.gid = metadata.gid as i32; 119202343d0bSLoGin kstat.rdev = metadata.raw_dev.data() as i64; 11936b4e7a29SLoGin kstat.mode = metadata.mode; 1194*dfe53cf0SGnoCiYeH match file.file_type() { 11957eda31b2SLoGin FileType::File => kstat.mode.insert(ModeType::S_IFREG), 119667b48188Shoumkh FileType::Dir => kstat.mode.insert(ModeType::S_IFDIR), 119767b48188Shoumkh FileType::BlockDevice => kstat.mode.insert(ModeType::S_IFBLK), 119867b48188Shoumkh FileType::CharDevice => kstat.mode.insert(ModeType::S_IFCHR), 119967b48188Shoumkh FileType::SymLink => kstat.mode.insert(ModeType::S_IFLNK), 120067b48188Shoumkh FileType::Socket => kstat.mode.insert(ModeType::S_IFSOCK), 120167b48188Shoumkh FileType::Pipe => kstat.mode.insert(ModeType::S_IFIFO), 120240314b30SXiaoye Zheng FileType::KvmDevice => kstat.mode.insert(ModeType::S_IFCHR), 120302343d0bSLoGin FileType::FramebufferDevice => kstat.mode.insert(ModeType::S_IFCHR), 120467b48188Shoumkh } 120567b48188Shoumkh 120667b48188Shoumkh return Ok(kstat); 120767b48188Shoumkh } 12081496ba7bSLoGin 120967b48188Shoumkh pub fn fstat(fd: i32, usr_kstat: *mut PosixKstat) -> Result<usize, SystemError> { 121082df0a13Shmt let mut writer = UserBufferWriter::new(usr_kstat, size_of::<PosixKstat>(), true)?; 12111496ba7bSLoGin let kstat = Self::do_fstat(fd)?; 121282df0a13Shmt 121382df0a13Shmt writer.copy_one_to_user(&kstat, 0)?; 121467b48188Shoumkh return Ok(0); 121567b48188Shoumkh } 12162dbef785SGnoCiYeH 121782df0a13Shmt pub fn stat(path: *const u8, user_kstat: *mut PosixKstat) -> Result<usize, SystemError> { 121882df0a13Shmt let fd = Self::open( 121982df0a13Shmt path, 122082df0a13Shmt FileMode::O_RDONLY.bits(), 122182df0a13Shmt ModeType::empty().bits(), 122282df0a13Shmt true, 122382df0a13Shmt )?; 1224bf4a4899SLoGin let r = Self::fstat(fd as i32, user_kstat); 1225971462beSGnoCiYeH Self::close(fd).ok(); 1226bf4a4899SLoGin return r; 1227bf4a4899SLoGin } 1228bf4a4899SLoGin 122982df0a13Shmt pub fn lstat(path: *const u8, user_kstat: *mut PosixKstat) -> Result<usize, SystemError> { 123082df0a13Shmt let fd = Self::open( 123182df0a13Shmt path, 123282df0a13Shmt FileMode::O_RDONLY.bits(), 123382df0a13Shmt ModeType::empty().bits(), 123482df0a13Shmt false, 123582df0a13Shmt )?; 1236bf4a4899SLoGin let r = Self::fstat(fd as i32, user_kstat); 1237bf4a4899SLoGin Self::close(fd).ok(); 1238bf4a4899SLoGin return r; 1239971462beSGnoCiYeH } 1240971462beSGnoCiYeH 1241597ecc08STTaq pub fn statfs(path: *const u8, user_statfs: *mut PosixStatfs) -> Result<usize, SystemError> { 1242597ecc08STTaq let mut writer = UserBufferWriter::new(user_statfs, size_of::<PosixStatfs>(), true)?; 1243597ecc08STTaq let fd = Self::open( 1244597ecc08STTaq path, 1245597ecc08STTaq FileMode::O_RDONLY.bits(), 1246597ecc08STTaq ModeType::empty().bits(), 1247597ecc08STTaq true, 1248597ecc08STTaq )?; 1249597ecc08STTaq let path = check_and_clone_cstr(path, Some(MAX_PATHLEN)).unwrap(); 1250597ecc08STTaq let pcb = ProcessManager::current_pcb(); 1251597ecc08STTaq let (_inode_begin, remain_path) = user_path_at(&pcb, fd as i32, &path)?; 1252597ecc08STTaq let inode = ROOT_INODE().lookup_follow_symlink(&remain_path, MAX_PATHLEN)?; 1253597ecc08STTaq let statfs = PosixStatfs::from(inode.fs().super_block()); 1254597ecc08STTaq writer.copy_one_to_user(&statfs, 0)?; 1255597ecc08STTaq return Ok(0); 1256597ecc08STTaq } 1257597ecc08STTaq 1258597ecc08STTaq pub fn fstatfs(fd: i32, user_statfs: *mut PosixStatfs) -> Result<usize, SystemError> { 1259597ecc08STTaq let mut writer = UserBufferWriter::new(user_statfs, size_of::<PosixStatfs>(), true)?; 1260597ecc08STTaq let binding = ProcessManager::current_pcb().fd_table(); 1261597ecc08STTaq let fd_table_guard = binding.read(); 1262597ecc08STTaq let file = fd_table_guard 1263597ecc08STTaq .get_file_by_fd(fd) 1264597ecc08STTaq .ok_or(SystemError::EBADF)?; 1265597ecc08STTaq drop(fd_table_guard); 1266*dfe53cf0SGnoCiYeH let statfs = PosixStatfs::from(file.inode().fs().super_block()); 1267597ecc08STTaq writer.copy_one_to_user(&statfs, 0)?; 1268597ecc08STTaq return Ok(0); 1269597ecc08STTaq } 1270597ecc08STTaq 1271b4eb05a1STTaq pub fn do_statx( 1272b4eb05a1STTaq fd: i32, 1273b4eb05a1STTaq path: *const u8, 1274b4eb05a1STTaq flags: u32, 1275b4eb05a1STTaq mask: u32, 1276b4eb05a1STTaq usr_kstat: *mut PosixStatx, 1277b4eb05a1STTaq ) -> Result<usize, SystemError> { 1278b4eb05a1STTaq if usr_kstat.is_null() { 1279b4eb05a1STTaq return Err(SystemError::EFAULT); 1280b4eb05a1STTaq } 1281b4eb05a1STTaq 1282b4eb05a1STTaq let mask = PosixStatxMask::from_bits_truncate(mask); 1283b4eb05a1STTaq 1284b4eb05a1STTaq if mask.contains(PosixStatxMask::STATX_RESERVED) { 1285b4eb05a1STTaq return Err(SystemError::ENAVAIL); 1286b4eb05a1STTaq } 1287b4eb05a1STTaq 1288b4eb05a1STTaq let flags = FileMode::from_bits_truncate(flags); 1289b4eb05a1STTaq let ofd = Self::open(path, flags.bits(), ModeType::empty().bits, true)?; 1290b4eb05a1STTaq 1291b4eb05a1STTaq let binding = ProcessManager::current_pcb().fd_table(); 1292b4eb05a1STTaq let fd_table_guard = binding.read(); 1293b4eb05a1STTaq let file = fd_table_guard 1294b4eb05a1STTaq .get_file_by_fd(ofd as i32) 1295b4eb05a1STTaq .ok_or(SystemError::EBADF)?; 1296b4eb05a1STTaq // drop guard 以避免无法调度的问题 1297b4eb05a1STTaq drop(fd_table_guard); 1298b4eb05a1STTaq let mut writer = UserBufferWriter::new(usr_kstat, size_of::<PosixStatx>(), true)?; 1299b4eb05a1STTaq let mut tmp: PosixStatx = PosixStatx::new(); 1300b4eb05a1STTaq // 获取文件信息 1301*dfe53cf0SGnoCiYeH let metadata = file.metadata()?; 1302b4eb05a1STTaq 1303b4eb05a1STTaq tmp.stx_mask |= PosixStatxMask::STATX_BASIC_STATS; 1304b4eb05a1STTaq tmp.stx_blksize = metadata.blk_size as u32; 1305b4eb05a1STTaq if mask.contains(PosixStatxMask::STATX_MODE) || mask.contains(PosixStatxMask::STATX_TYPE) { 1306b4eb05a1STTaq tmp.stx_mode = metadata.mode; 1307b4eb05a1STTaq } 1308b4eb05a1STTaq if mask.contains(PosixStatxMask::STATX_NLINK) { 1309b4eb05a1STTaq tmp.stx_nlink = metadata.nlinks as u32; 1310b4eb05a1STTaq } 1311b4eb05a1STTaq if mask.contains(PosixStatxMask::STATX_UID) { 1312b4eb05a1STTaq tmp.stx_uid = metadata.uid as u32; 1313b4eb05a1STTaq } 1314b4eb05a1STTaq if mask.contains(PosixStatxMask::STATX_GID) { 1315b4eb05a1STTaq tmp.stx_gid = metadata.gid as u32; 1316b4eb05a1STTaq } 1317b4eb05a1STTaq if mask.contains(PosixStatxMask::STATX_ATIME) { 1318b4eb05a1STTaq tmp.stx_atime.tv_sec = metadata.atime.tv_sec; 1319b4eb05a1STTaq tmp.stx_atime.tv_nsec = metadata.atime.tv_nsec; 1320b4eb05a1STTaq } 1321b4eb05a1STTaq if mask.contains(PosixStatxMask::STATX_MTIME) { 1322b4eb05a1STTaq tmp.stx_mtime.tv_sec = metadata.ctime.tv_sec; 1323b4eb05a1STTaq tmp.stx_mtime.tv_nsec = metadata.ctime.tv_nsec; 1324b4eb05a1STTaq } 1325b4eb05a1STTaq if mask.contains(PosixStatxMask::STATX_CTIME) { 1326b4eb05a1STTaq // ctime是文件上次修改状态的时间 1327b4eb05a1STTaq tmp.stx_ctime.tv_sec = metadata.mtime.tv_sec; 1328b4eb05a1STTaq tmp.stx_ctime.tv_nsec = metadata.mtime.tv_nsec; 1329b4eb05a1STTaq } 1330b4eb05a1STTaq if mask.contains(PosixStatxMask::STATX_INO) { 1331b4eb05a1STTaq tmp.stx_inode = metadata.inode_id.into() as u64; 1332b4eb05a1STTaq } 1333b4eb05a1STTaq if mask.contains(PosixStatxMask::STATX_SIZE) { 1334b4eb05a1STTaq tmp.stx_size = metadata.size; 1335b4eb05a1STTaq } 1336b4eb05a1STTaq if mask.contains(PosixStatxMask::STATX_BLOCKS) { 1337b4eb05a1STTaq tmp.stx_blocks = metadata.blocks as u64; 1338b4eb05a1STTaq } 1339b4eb05a1STTaq 1340b4eb05a1STTaq if mask.contains(PosixStatxMask::STATX_BTIME) { 1341b4eb05a1STTaq // btime是文件创建时间 1342b4eb05a1STTaq tmp.stx_btime.tv_sec = metadata.ctime.tv_sec; 1343b4eb05a1STTaq tmp.stx_btime.tv_nsec = metadata.ctime.tv_nsec; 1344b4eb05a1STTaq } 1345b4eb05a1STTaq if mask.contains(PosixStatxMask::STATX_ALL) { 1346b4eb05a1STTaq tmp.stx_attributes = StxAttributes::STATX_ATTR_APPEND; 1347b4eb05a1STTaq tmp.stx_attributes_mask |= 1348b4eb05a1STTaq StxAttributes::STATX_ATTR_AUTOMOUNT | StxAttributes::STATX_ATTR_DAX; 1349b4eb05a1STTaq tmp.stx_dev_major = metadata.dev_id as u32; 1350b4eb05a1STTaq tmp.stx_dev_minor = metadata.dev_id as u32; // 1351b5b571e0SLoGin tmp.stx_rdev_major = metadata.raw_dev.data(); 1352b5b571e0SLoGin tmp.stx_rdev_minor = metadata.raw_dev.data(); 1353b4eb05a1STTaq } 1354b4eb05a1STTaq if mask.contains(PosixStatxMask::STATX_MNT_ID) { 1355b4eb05a1STTaq tmp.stx_mnt_id = 0; 1356b4eb05a1STTaq } 1357b4eb05a1STTaq if mask.contains(PosixStatxMask::STATX_DIOALIGN) { 1358b4eb05a1STTaq tmp.stx_dio_mem_align = 0; 1359b4eb05a1STTaq tmp.stx_dio_offset_align = 0; 1360b4eb05a1STTaq } 1361b4eb05a1STTaq 1362*dfe53cf0SGnoCiYeH match file.file_type() { 1363b4eb05a1STTaq FileType::File => tmp.stx_mode.insert(ModeType::S_IFREG), 1364b4eb05a1STTaq FileType::Dir => tmp.stx_mode.insert(ModeType::S_IFDIR), 1365b4eb05a1STTaq FileType::BlockDevice => tmp.stx_mode.insert(ModeType::S_IFBLK), 1366b4eb05a1STTaq FileType::CharDevice => tmp.stx_mode.insert(ModeType::S_IFCHR), 1367b4eb05a1STTaq FileType::SymLink => tmp.stx_mode.insert(ModeType::S_IFLNK), 1368b4eb05a1STTaq FileType::Socket => tmp.stx_mode.insert(ModeType::S_IFSOCK), 1369b4eb05a1STTaq FileType::Pipe => tmp.stx_mode.insert(ModeType::S_IFIFO), 1370b4eb05a1STTaq FileType::KvmDevice => tmp.stx_mode.insert(ModeType::S_IFCHR), 1371b4eb05a1STTaq FileType::FramebufferDevice => tmp.stx_mode.insert(ModeType::S_IFCHR), 1372b4eb05a1STTaq } 1373b4eb05a1STTaq 1374b4eb05a1STTaq writer.copy_one_to_user(&tmp, 0)?; 1375b4eb05a1STTaq Self::close(fd as usize).ok(); 1376b4eb05a1STTaq return Ok(0); 1377b4eb05a1STTaq } 1378b4eb05a1STTaq 13792dbef785SGnoCiYeH pub fn mknod( 138082df0a13Shmt path: *const u8, 13812dbef785SGnoCiYeH mode: ModeType, 13822dbef785SGnoCiYeH dev_t: DeviceNumber, 13832dbef785SGnoCiYeH ) -> Result<usize, SystemError> { 138482df0a13Shmt let path = check_and_clone_cstr(path, Some(MAX_PATHLEN))?; 138582df0a13Shmt let path = path.as_str().trim(); 13862dbef785SGnoCiYeH 1387a03c4f9dSLoGin let inode: Result<Arc<dyn IndexNode>, SystemError> = 1388a03c4f9dSLoGin ROOT_INODE().lookup_follow_symlink(path, VFS_MAX_FOLLOW_SYMLINK_TIMES); 13892dbef785SGnoCiYeH 13902dbef785SGnoCiYeH if inode.is_ok() { 13912dbef785SGnoCiYeH return Err(SystemError::EEXIST); 13922dbef785SGnoCiYeH } 13932dbef785SGnoCiYeH 13942dbef785SGnoCiYeH let (filename, parent_path) = rsplit_path(path); 13952dbef785SGnoCiYeH 13962dbef785SGnoCiYeH // 查找父目录 1397a03c4f9dSLoGin let parent_inode: Arc<dyn IndexNode> = ROOT_INODE() 1398a03c4f9dSLoGin .lookup_follow_symlink(parent_path.unwrap_or("/"), VFS_MAX_FOLLOW_SYMLINK_TIMES)?; 13992dbef785SGnoCiYeH // 创建nod 14002dbef785SGnoCiYeH parent_inode.mknod(filename, mode, dev_t)?; 14012dbef785SGnoCiYeH 14022dbef785SGnoCiYeH return Ok(0); 14032dbef785SGnoCiYeH } 1404971462beSGnoCiYeH 1405971462beSGnoCiYeH pub fn writev(fd: i32, iov: usize, count: usize) -> Result<usize, SystemError> { 1406971462beSGnoCiYeH // IoVecs会进行用户态检验 1407971462beSGnoCiYeH let iovecs = unsafe { IoVecs::from_user(iov as *const IoVec, count, false) }?; 1408971462beSGnoCiYeH 1409971462beSGnoCiYeH let data = iovecs.gather(); 1410971462beSGnoCiYeH 1411971462beSGnoCiYeH Self::write(fd, &data) 1412971462beSGnoCiYeH } 1413709498caSLoGin 1414bf4a4899SLoGin pub fn readv(fd: i32, iov: usize, count: usize) -> Result<usize, SystemError> { 1415bf4a4899SLoGin // IoVecs会进行用户态检验 1416bf4a4899SLoGin let mut iovecs = unsafe { IoVecs::from_user(iov as *const IoVec, count, true) }?; 1417bf4a4899SLoGin 1418b5b571e0SLoGin let mut data = vec![0; iovecs.0.iter().map(|x| x.len()).sum()]; 1419bf4a4899SLoGin 1420bf4a4899SLoGin let len = Self::read(fd, &mut data)?; 1421bf4a4899SLoGin 1422bf4a4899SLoGin iovecs.scatter(&data[..len]); 1423bf4a4899SLoGin 1424bf4a4899SLoGin return Ok(len); 1425bf4a4899SLoGin } 1426bf4a4899SLoGin 1427709498caSLoGin pub fn readlink_at( 1428709498caSLoGin dirfd: i32, 1429709498caSLoGin path: *const u8, 1430709498caSLoGin user_buf: *mut u8, 1431709498caSLoGin buf_size: usize, 1432709498caSLoGin ) -> Result<usize, SystemError> { 14339b0abe6dSLoGin let path = check_and_clone_cstr(path, Some(MAX_PATHLEN))?; 143482df0a13Shmt let path = path.as_str().trim(); 1435709498caSLoGin let mut user_buf = UserBufferWriter::new(user_buf, buf_size, true)?; 1436709498caSLoGin 1437b5b571e0SLoGin let (inode, path) = user_path_at(&ProcessManager::current_pcb(), dirfd, path)?; 1438709498caSLoGin 1439709498caSLoGin let inode = inode.lookup(path.as_str())?; 1440709498caSLoGin if inode.metadata()?.file_type != FileType::SymLink { 1441709498caSLoGin return Err(SystemError::EINVAL); 1442709498caSLoGin } 1443709498caSLoGin 1444709498caSLoGin let ubuf = user_buf.buffer::<u8>(0).unwrap(); 1445709498caSLoGin 1446*dfe53cf0SGnoCiYeH let file = File::new(inode, FileMode::O_RDONLY)?; 1447709498caSLoGin 1448709498caSLoGin let len = file.read(buf_size, ubuf)?; 1449709498caSLoGin 1450709498caSLoGin return Ok(len); 1451709498caSLoGin } 1452709498caSLoGin 1453709498caSLoGin pub fn readlink( 1454709498caSLoGin path: *const u8, 1455709498caSLoGin user_buf: *mut u8, 1456709498caSLoGin buf_size: usize, 1457709498caSLoGin ) -> Result<usize, SystemError> { 14589b0abe6dSLoGin return Self::readlink_at(AtFlags::AT_FDCWD.bits(), path, user_buf, buf_size); 14599b0abe6dSLoGin } 14609b0abe6dSLoGin 14619b0abe6dSLoGin pub fn access(pathname: *const u8, mode: u32) -> Result<usize, SystemError> { 14629b0abe6dSLoGin return do_faccessat( 14639b0abe6dSLoGin AtFlags::AT_FDCWD.bits(), 14649b0abe6dSLoGin pathname, 1465bf4a4899SLoGin ModeType::from_bits(mode).ok_or(SystemError::EINVAL)?, 14669b0abe6dSLoGin 0, 14679b0abe6dSLoGin ); 14689b0abe6dSLoGin } 14699b0abe6dSLoGin 14709b0abe6dSLoGin pub fn faccessat2( 14719b0abe6dSLoGin dirfd: i32, 14729b0abe6dSLoGin pathname: *const u8, 14739b0abe6dSLoGin mode: u32, 14749b0abe6dSLoGin flags: u32, 14759b0abe6dSLoGin ) -> Result<usize, SystemError> { 1476bf4a4899SLoGin return do_faccessat( 1477bf4a4899SLoGin dirfd, 1478bf4a4899SLoGin pathname, 1479bf4a4899SLoGin ModeType::from_bits(mode).ok_or(SystemError::EINVAL)?, 1480bf4a4899SLoGin flags, 1481bf4a4899SLoGin ); 1482bf4a4899SLoGin } 1483bf4a4899SLoGin 1484bf4a4899SLoGin pub fn chmod(pathname: *const u8, mode: u32) -> Result<usize, SystemError> { 1485bf4a4899SLoGin return do_fchmodat( 1486bf4a4899SLoGin AtFlags::AT_FDCWD.bits(), 1487bf4a4899SLoGin pathname, 1488bf4a4899SLoGin ModeType::from_bits(mode).ok_or(SystemError::EINVAL)?, 1489bf4a4899SLoGin ); 1490bf4a4899SLoGin } 1491bf4a4899SLoGin 1492bf4a4899SLoGin pub fn fchmodat(dirfd: i32, pathname: *const u8, mode: u32) -> Result<usize, SystemError> { 1493bf4a4899SLoGin return do_fchmodat( 1494bf4a4899SLoGin dirfd, 1495bf4a4899SLoGin pathname, 1496bf4a4899SLoGin ModeType::from_bits(mode).ok_or(SystemError::EINVAL)?, 1497bf4a4899SLoGin ); 1498bf4a4899SLoGin } 1499bf4a4899SLoGin 1500bf4a4899SLoGin pub fn fchmod(fd: i32, mode: u32) -> Result<usize, SystemError> { 1501bf4a4899SLoGin let _mode = ModeType::from_bits(mode).ok_or(SystemError::EINVAL)?; 1502bf4a4899SLoGin let binding = ProcessManager::current_pcb().fd_table(); 1503bf4a4899SLoGin let fd_table_guard = binding.read(); 1504bf4a4899SLoGin let _file = fd_table_guard 1505bf4a4899SLoGin .get_file_by_fd(fd) 1506bf4a4899SLoGin .ok_or(SystemError::EBADF)?; 1507bf4a4899SLoGin 1508bf4a4899SLoGin // fchmod没完全实现,因此不修改文件的权限 1509bf4a4899SLoGin // todo: 实现fchmod 1510bf4a4899SLoGin kwarn!("fchmod not fully implemented"); 1511bf4a4899SLoGin return Ok(0); 1512709498caSLoGin } 15131d37ca6dSDonkey Kane /// #挂载文件系统 15141d37ca6dSDonkey Kane /// 15151d37ca6dSDonkey Kane /// 用于挂载文件系统,目前仅支持ramfs挂载 15161d37ca6dSDonkey Kane /// 15171d37ca6dSDonkey Kane /// ## 参数: 15181d37ca6dSDonkey Kane /// 15191d37ca6dSDonkey Kane /// - source 挂载设备(暂时不支持) 15201d37ca6dSDonkey Kane /// - target 挂载目录 15211d37ca6dSDonkey Kane /// - filesystemtype 文件系统 15221d37ca6dSDonkey Kane /// - mountflags 挂载选项(暂未实现) 15231d37ca6dSDonkey Kane /// - data 带数据挂载 15241d37ca6dSDonkey Kane /// 15251d37ca6dSDonkey Kane /// ## 返回值 15261d37ca6dSDonkey Kane /// - Ok(0): 挂载成功 15271d37ca6dSDonkey Kane /// - Err(SystemError) :挂载过程中出错 15281d37ca6dSDonkey Kane pub fn mount( 15291d37ca6dSDonkey Kane _source: *const u8, 15301d37ca6dSDonkey Kane target: *const u8, 15311d37ca6dSDonkey Kane filesystemtype: *const u8, 15321d37ca6dSDonkey Kane _mountflags: usize, 15331d37ca6dSDonkey Kane _data: *const c_void, 15341d37ca6dSDonkey Kane ) -> Result<usize, SystemError> { 15351d37ca6dSDonkey Kane let target = user_access::check_and_clone_cstr(target, Some(MAX_PATHLEN))?; 15361d37ca6dSDonkey Kane 15371d37ca6dSDonkey Kane let filesystemtype = user_access::check_and_clone_cstr(filesystemtype, Some(MAX_PATHLEN))?; 15381d37ca6dSDonkey Kane 15391d37ca6dSDonkey Kane let filesystemtype = producefs!(FSMAKER, filesystemtype)?; 15401d37ca6dSDonkey Kane 1541b5b571e0SLoGin return Vcore::do_mount(filesystemtype, target.to_string().as_str()); 15421d37ca6dSDonkey Kane } 15431d37ca6dSDonkey Kane 15441d37ca6dSDonkey Kane // 想法:可以在VFS中实现一个文件系统分发器,流程如下: 15451d37ca6dSDonkey Kane // 1. 接受从上方传来的文件类型字符串 15461d37ca6dSDonkey Kane // 2. 将传入值与启动时准备好的字符串数组逐个比较(probe) 15471d37ca6dSDonkey Kane // 3. 直接在函数内调用构造方法并直接返回文件系统对象 1548709498caSLoGin } 1549709498caSLoGin 1550cde5492fSlogin #[repr(C)] 1551cde5492fSlogin #[derive(Debug, Clone, Copy)] 1552cde5492fSlogin pub struct IoVec { 1553cde5492fSlogin /// 缓冲区的起始地址 1554cde5492fSlogin pub iov_base: *mut u8, 1555cde5492fSlogin /// 缓冲区的长度 1556cde5492fSlogin pub iov_len: usize, 1557cde5492fSlogin } 1558cde5492fSlogin 1559cde5492fSlogin /// 用于存储多个来自用户空间的IoVec 1560cde5492fSlogin /// 1561cde5492fSlogin /// 由于目前内核中的文件系统还不支持分散读写,所以暂时只支持将用户空间的IoVec聚合成一个缓冲区,然后进行操作。 1562cde5492fSlogin /// TODO:支持分散读写 1563cde5492fSlogin #[derive(Debug)] 1564cde5492fSlogin pub struct IoVecs(Vec<&'static mut [u8]>); 1565cde5492fSlogin 1566cde5492fSlogin impl IoVecs { 1567cde5492fSlogin /// 从用户空间的IoVec中构造IoVecs 1568cde5492fSlogin /// 1569cde5492fSlogin /// @param iov 用户空间的IoVec 1570cde5492fSlogin /// @param iovcnt 用户空间的IoVec的数量 1571cde5492fSlogin /// @param readv 是否为readv系统调用 1572cde5492fSlogin /// 1573cde5492fSlogin /// @return 构造成功返回IoVecs,否则返回错误码 1574cde5492fSlogin pub unsafe fn from_user( 1575cde5492fSlogin iov: *const IoVec, 1576cde5492fSlogin iovcnt: usize, 1577cde5492fSlogin _readv: bool, 1578cde5492fSlogin ) -> Result<Self, SystemError> { 1579cde5492fSlogin // 检查iov指针所在空间是否合法 15804fda81ceSLoGin verify_area( 15814fda81ceSLoGin VirtAddr::new(iov as usize), 15824fda81ceSLoGin iovcnt * core::mem::size_of::<IoVec>(), 15834fda81ceSLoGin ) 15844fda81ceSLoGin .map_err(|_| SystemError::EFAULT)?; 1585cde5492fSlogin 1586cde5492fSlogin // 将用户空间的IoVec转换为引用(注意:这里的引用是静态的,因为用户空间的IoVec不会被释放) 1587cde5492fSlogin let iovs: &[IoVec] = core::slice::from_raw_parts(iov, iovcnt); 1588cde5492fSlogin 1589cde5492fSlogin let mut slices: Vec<&mut [u8]> = vec![]; 1590cde5492fSlogin slices.reserve(iovs.len()); 1591cde5492fSlogin 1592cde5492fSlogin for iov in iovs.iter() { 1593cde5492fSlogin if iov.iov_len == 0 { 1594cde5492fSlogin continue; 1595cde5492fSlogin } 1596cde5492fSlogin 15974fda81ceSLoGin verify_area( 15984fda81ceSLoGin VirtAddr::new(iov.iov_base as usize), 15994fda81ceSLoGin iovcnt * core::mem::size_of::<IoVec>(), 16004fda81ceSLoGin ) 16014fda81ceSLoGin .map_err(|_| SystemError::EFAULT)?; 1602cde5492fSlogin 1603cde5492fSlogin slices.push(core::slice::from_raw_parts_mut(iov.iov_base, iov.iov_len)); 1604cde5492fSlogin } 1605cde5492fSlogin 1606cde5492fSlogin return Ok(Self(slices)); 1607cde5492fSlogin } 1608cde5492fSlogin 1609cde5492fSlogin /// @brief 将IoVecs中的数据聚合到一个缓冲区中 1610cde5492fSlogin /// 1611cde5492fSlogin /// @return 返回聚合后的缓冲区 1612cde5492fSlogin pub fn gather(&self) -> Vec<u8> { 1613cde5492fSlogin let mut buf = Vec::new(); 1614cde5492fSlogin for slice in self.0.iter() { 1615cde5492fSlogin buf.extend_from_slice(slice); 1616cde5492fSlogin } 1617cde5492fSlogin return buf; 1618cde5492fSlogin } 1619cde5492fSlogin 1620cde5492fSlogin /// @brief 将给定的数据分散写入到IoVecs中 1621cde5492fSlogin pub fn scatter(&mut self, data: &[u8]) { 1622cde5492fSlogin let mut data: &[u8] = data; 1623cde5492fSlogin for slice in self.0.iter_mut() { 1624cde5492fSlogin let len = core::cmp::min(slice.len(), data.len()); 1625cde5492fSlogin if len == 0 { 1626cde5492fSlogin continue; 1627cde5492fSlogin } 1628cde5492fSlogin 1629cde5492fSlogin slice[..len].copy_from_slice(&data[..len]); 1630cde5492fSlogin data = &data[len..]; 1631cde5492fSlogin } 1632cde5492fSlogin } 1633cde5492fSlogin 1634cde5492fSlogin /// @brief 创建与IoVecs等长的缓冲区 1635cde5492fSlogin /// 1636cde5492fSlogin /// @param set_len 是否设置返回的Vec的len。 1637cde5492fSlogin /// 如果为true,则返回的Vec的len为所有IoVec的长度之和; 1638cde5492fSlogin /// 否则返回的Vec的len为0,capacity为所有IoVec的长度之和. 1639cde5492fSlogin /// 1640cde5492fSlogin /// @return 返回创建的缓冲区 1641cde5492fSlogin pub fn new_buf(&self, set_len: bool) -> Vec<u8> { 1642cde5492fSlogin let total_len: usize = self.0.iter().map(|slice| slice.len()).sum(); 1643cde5492fSlogin let mut buf: Vec<u8> = Vec::with_capacity(total_len); 1644cde5492fSlogin 1645cde5492fSlogin if set_len { 16467ae679ddSLoGin buf.resize(total_len, 0); 1647cde5492fSlogin } 1648cde5492fSlogin return buf; 1649cde5492fSlogin } 1650cde5492fSlogin } 1651