1
0
mirror of https://github.com/rcore-os/rCore.git synced 2024-11-23 08:26:17 +04:00

impl more file system *at syscalls

This commit is contained in:
WangRunji 2019-04-14 15:47:42 +08:00
parent fe0045c0c9
commit 3556c758db
3 changed files with 135 additions and 92 deletions

View File

@ -123,24 +123,13 @@ impl rcore_thread::Context for Thread {
impl Thread { impl Thread {
/// Make a struct for the init thread /// Make a struct for the init thread
/// TODO: remove this, we only need `Context::null()`
pub unsafe fn new_init() -> Box<Thread> { pub unsafe fn new_init() -> Box<Thread> {
Box::new(Thread { Box::new(Thread {
context: Context::null(), context: Context::null(),
kstack: KernelStack::new(), kstack: KernelStack::new(),
clear_child_tid: 0, clear_child_tid: 0,
proc: Arc::new(Mutex::new(Process { // safety: this field will never be used
vm: MemorySet::new(), proc: core::mem::uninitialized(),
files: BTreeMap::default(),
cwd: String::from("/"),
futexes: BTreeMap::default(),
pid: Pid::uninitialized(),
parent: None,
children: Vec::new(),
threads: Vec::new(),
child_exit: Arc::new(Condvar::new()),
child_exit_code: BTreeMap::new(),
})),
}) })
} }

View File

@ -237,49 +237,24 @@ pub fn sys_openat(dir_fd: usize, path: *const u8, flags: usize, mode: usize) ->
dir_fd as isize, path, flags, mode dir_fd as isize, path, flags, mode
); );
let inode = if dir_fd == AT_FDCWD { let inode = if flags.contains(OpenFlags::CREATE) {
// from process cwd let (dir_path, file_name) = split_path(&path);
if flags.contains(OpenFlags::CREATE) { // relative to cwd
let (dir_path, file_name) = split_path(&path); let dir_inode = proc.lookup_inode_at(dir_fd, dir_path)?;
// relative to cwd match dir_inode.find(file_name) {
let dir_inode = proc.lookup_inode(dir_path)?; Ok(file_inode) => {
match dir_inode.find(file_name) { if flags.contains(OpenFlags::EXCLUSIVE) {
Ok(file_inode) => { return Err(SysError::EEXIST);
if flags.contains(OpenFlags::EXCLUSIVE) {
return Err(SysError::EEXIST);
}
file_inode
} }
Err(FsError::EntryNotFound) => { file_inode
dir_inode.create(file_name, FileType::File, mode as u32)?
}
Err(e) => return Err(SysError::from(e)),
} }
} else { Err(FsError::EntryNotFound) => {
proc.lookup_inode(&path)? dir_inode.create(file_name, FileType::File, mode as u32)?
}
Err(e) => return Err(SysError::from(e)),
} }
} else { } else {
// relative to dir_fd proc.lookup_inode_at(dir_fd, &path)?
let dir_file = proc.get_file(dir_fd)?;
if flags.contains(OpenFlags::CREATE) {
let (dir_path, file_name) = split_path(&path);
// relative to cwd
let dir_inode = dir_file.lookup_follow(dir_path, FOLLOW_MAX_DEPTH)?;
match dir_inode.find(file_name) {
Ok(file_inode) => {
if flags.contains(OpenFlags::EXCLUSIVE) {
return Err(SysError::EEXIST);
}
file_inode
}
Err(FsError::EntryNotFound) => {
dir_inode.create(file_name, FileType::File, mode as u32)?
}
Err(e) => return Err(SysError::from(e)),
}
} else {
dir_file.lookup_follow(&path, FOLLOW_MAX_DEPTH)?
}
}; };
let fd = proc.get_free_fd(); let fd = proc.get_free_fd();
@ -297,14 +272,22 @@ pub fn sys_close(fd: usize) -> SysResult {
} }
pub fn sys_access(path: *const u8, mode: usize) -> SysResult { pub fn sys_access(path: *const u8, mode: usize) -> SysResult {
sys_faccessat(AT_FDCWD, path, mode, 0)
}
pub fn sys_faccessat(dirfd: usize, path: *const u8, mode: usize, flags: usize) -> SysResult {
// TODO: check permissions based on uid/gid // TODO: check permissions based on uid/gid
let proc = process(); let proc = process();
let path = unsafe { proc.vm.check_and_clone_cstr(path)? }; let path = unsafe { proc.vm.check_and_clone_cstr(path)? };
let flags = AtFlags::from_bits_truncate(flags);
if !proc.pid.is_init() { if !proc.pid.is_init() {
// we trust pid 0 process // we trust pid 0 process
info!("access: path: {:?}, mode: {:#o}", path, mode); info!(
"faccessat: dirfd: {}, path: {:?}, mode: {:#o}, flags: {:?}",
dirfd, path, mode, flags
);
} }
let inode = proc.lookup_inode(&path)?; let inode = proc.lookup_inode_at(dirfd, &path)?;
Ok(0) Ok(0)
} }
@ -322,9 +305,9 @@ pub fn sys_getcwd(buf: *mut u8, len: usize) -> SysResult {
Ok(buf as usize) Ok(buf as usize)
} }
pub fn sys_stat(path: *const u8, stat_ptr: *mut Stat) -> SysResult { pub fn sys_lstat(path: *const u8, stat_ptr: *mut Stat) -> SysResult {
warn!("stat is partial implemented as lstat"); warn!("lstat is partial implemented as stat");
sys_lstat(path, stat_ptr) sys_stat(path, stat_ptr)
} }
pub fn sys_fstat(fd: usize, stat_ptr: *mut Stat) -> SysResult { pub fn sys_fstat(fd: usize, stat_ptr: *mut Stat) -> SysResult {
@ -333,20 +316,23 @@ pub fn sys_fstat(fd: usize, stat_ptr: *mut Stat) -> SysResult {
proc.vm.check_write_ptr(stat_ptr)?; proc.vm.check_write_ptr(stat_ptr)?;
let file = proc.get_file(fd)?; let file = proc.get_file(fd)?;
let stat = Stat::from(file.metadata()?); let stat = Stat::from(file.metadata()?);
// TODO: handle symlink
unsafe { unsafe {
stat_ptr.write(stat); stat_ptr.write(stat);
} }
Ok(0) Ok(0)
} }
pub fn sys_lstat(path: *const u8, stat_ptr: *mut Stat) -> SysResult { pub fn sys_fstatat(dirfd: usize, path: *const u8, stat_ptr: *mut Stat, flags: usize) -> SysResult {
let proc = process(); let proc = process();
let path = unsafe { proc.vm.check_and_clone_cstr(path)? }; let path = unsafe { proc.vm.check_and_clone_cstr(path)? };
proc.vm.check_write_ptr(stat_ptr)?; proc.vm.check_write_ptr(stat_ptr)?;
info!("lstat: path: {:?}, stat_ptr: {:?}", path, stat_ptr); let flags = AtFlags::from_bits_truncate(flags);
info!(
"fstatat: dirfd: {}, path: {:?}, stat_ptr: {:?}, flags: {:?}",
dirfd, path, stat_ptr, flags
);
let inode = proc.lookup_inode(&path)?; let inode = proc.lookup_inode_at(dirfd, &path)?;
let stat = Stat::from(inode.metadata()?); let stat = Stat::from(inode.metadata()?);
unsafe { unsafe {
stat_ptr.write(stat); stat_ptr.write(stat);
@ -354,13 +340,21 @@ pub fn sys_lstat(path: *const u8, stat_ptr: *mut Stat) -> SysResult {
Ok(0) Ok(0)
} }
pub fn sys_stat(path: *const u8, stat_ptr: *mut Stat) -> SysResult {
sys_fstatat(AT_FDCWD, path, stat_ptr, 0)
}
pub fn sys_readlink(path: *const u8, base: *mut u8, len: usize) -> SysResult { pub fn sys_readlink(path: *const u8, base: *mut u8, len: usize) -> SysResult {
sys_readlinkat(AT_FDCWD, path, base, len)
}
pub fn sys_readlinkat(dirfd: usize, path: *const u8, base: *mut u8, len: usize) -> SysResult {
let proc = process(); let proc = process();
let path = unsafe { proc.vm.check_and_clone_cstr(path)? }; let path = unsafe { proc.vm.check_and_clone_cstr(path)? };
proc.vm.check_write_array(base, len)?; proc.vm.check_write_array(base, len)?;
info!("readlink: path: {:?}, base: {:?}, len: {}", path, base, len); info!("readlink: path: {:?}, base: {:?}, len: {}", path, base, len);
let inode = proc.lookup_inode(&path)?; let inode = proc.lookup_inode_at(dirfd, &path)?;
if inode.metadata()?.type_ == FileType::SymLink { if inode.metadata()?.type_ == FileType::SymLink {
// TODO: recursive link resolution and loop detection // TODO: recursive link resolution and loop detection
let mut slice = unsafe { slice::from_raw_parts_mut(base, len) }; let mut slice = unsafe { slice::from_raw_parts_mut(base, len) };
@ -504,30 +498,27 @@ pub fn sys_renameat(
let (old_dir_path, old_file_name) = split_path(&oldpath); let (old_dir_path, old_file_name) = split_path(&oldpath);
let (new_dir_path, new_file_name) = split_path(&newpath); let (new_dir_path, new_file_name) = split_path(&newpath);
let old_dir_inode = if olddirfd == AT_FDCWD { let old_dir_inode = proc.lookup_inode_at(olddirfd, old_dir_path)?;
proc.lookup_inode(old_dir_path)? let new_dir_inode = proc.lookup_inode_at(newdirfd, new_dir_path)?;
} else {
proc.get_file(olddirfd)?
.lookup_follow(old_dir_path, FOLLOW_MAX_DEPTH)?
};
let new_dir_inode = if newdirfd == AT_FDCWD {
proc.lookup_inode(new_dir_path)?
} else {
proc.get_file(newdirfd)?
.lookup_follow(new_dir_path, FOLLOW_MAX_DEPTH)?
};
old_dir_inode.move_(old_file_name, &new_dir_inode, new_file_name)?; old_dir_inode.move_(old_file_name, &new_dir_inode, new_file_name)?;
Ok(0) Ok(0)
} }
pub fn sys_mkdir(path: *const u8, mode: usize) -> SysResult { pub fn sys_mkdir(path: *const u8, mode: usize) -> SysResult {
sys_mkdirat(AT_FDCWD, path, mode)
}
pub fn sys_mkdirat(dirfd: usize, path: *const u8, mode: usize) -> SysResult {
let proc = process(); let proc = process();
let path = unsafe { proc.vm.check_and_clone_cstr(path)? }; let path = unsafe { proc.vm.check_and_clone_cstr(path)? };
// TODO: check pathname // TODO: check pathname
info!("mkdir: path: {:?}, mode: {:#o}", path, mode); info!(
"mkdirat: dirfd: {}, path: {:?}, mode: {:#o}",
dirfd, path, mode
);
let (dir_path, file_name) = split_path(&path); let (dir_path, file_name) = split_path(&path);
let inode = proc.lookup_inode(dir_path)?; let inode = proc.lookup_inode_at(dirfd, dir_path)?;
if inode.find(file_name).is_ok() { if inode.find(file_name).is_ok() {
return Err(SysError::EEXIST); return Err(SysError::EEXIST);
} }
@ -551,25 +542,47 @@ pub fn sys_rmdir(path: *const u8) -> SysResult {
} }
pub fn sys_link(oldpath: *const u8, newpath: *const u8) -> SysResult { pub fn sys_link(oldpath: *const u8, newpath: *const u8) -> SysResult {
sys_linkat(AT_FDCWD, oldpath, AT_FDCWD, newpath, 0)
}
pub fn sys_linkat(
olddirfd: usize,
oldpath: *const u8,
newdirfd: usize,
newpath: *const u8,
flags: usize,
) -> SysResult {
let proc = process(); let proc = process();
let oldpath = unsafe { proc.vm.check_and_clone_cstr(oldpath)? }; let oldpath = unsafe { proc.vm.check_and_clone_cstr(oldpath)? };
let newpath = unsafe { proc.vm.check_and_clone_cstr(newpath)? }; let newpath = unsafe { proc.vm.check_and_clone_cstr(newpath)? };
info!("link: oldpath: {:?}, newpath: {:?}", oldpath, newpath); let flags = AtFlags::from_bits_truncate(flags);
info!(
"linkat: olddirfd: {}, oldpath: {:?}, newdirfd: {}, newpath: {:?}, flags: {:?}",
olddirfd, oldpath, newdirfd, newpath, flags
);
let (new_dir_path, new_file_name) = split_path(&newpath); let (new_dir_path, new_file_name) = split_path(&newpath);
let inode = proc.lookup_inode(&oldpath)?; let inode = proc.lookup_inode_at(olddirfd, &oldpath)?;
let new_dir_inode = proc.lookup_inode(new_dir_path)?; let new_dir_inode = proc.lookup_inode_at(newdirfd, new_dir_path)?;
new_dir_inode.link(new_file_name, &inode)?; new_dir_inode.link(new_file_name, &inode)?;
Ok(0) Ok(0)
} }
pub fn sys_unlink(path: *const u8) -> SysResult { pub fn sys_unlink(path: *const u8) -> SysResult {
sys_unlinkat(AT_FDCWD, path, 0)
}
pub fn sys_unlinkat(dirfd: usize, path: *const u8, flags: usize) -> SysResult {
let proc = process(); let proc = process();
let path = unsafe { proc.vm.check_and_clone_cstr(path)? }; let path = unsafe { proc.vm.check_and_clone_cstr(path)? };
info!("unlink: path: {:?}", path); let flags = AtFlags::from_bits_truncate(flags);
info!(
"unlinkat: dirfd: {}, path: {:?}, flags: {:?}",
dirfd, path, flags
);
let (dir_path, file_name) = split_path(&path); let (dir_path, file_name) = split_path(&path);
let dir_inode = proc.lookup_inode(dir_path)?; let dir_inode = proc.lookup_inode_at(dirfd, dir_path)?;
let file_inode = dir_inode.find(file_name)?; let file_inode = dir_inode.find(file_name)?;
if file_inode.metadata()?.type_ == FileType::Dir { if file_inode.metadata()?.type_ == FileType::Dir {
return Err(SysError::EISDIR); return Err(SysError::EISDIR);
@ -701,11 +714,44 @@ impl Process {
_ => Err(SysError::EBADF), _ => Err(SysError::EBADF),
} }
} }
/// Lookup INode from the process.
///
/// - If `path` is relative, then it is interpreted relative to the directory
/// referred to by the file descriptor `dirfd`.
///
/// - If the `dirfd` is the special value `AT_FDCWD`, then the directory is
/// current working directory of the process.
///
/// - If `path` is absolute, then `dirfd` is ignored.
///
/// - If `follow` is true, then dereference `path` if it is a symbolic link.
pub fn lookup_inode_at(
&self,
dirfd: usize,
path: &str,
// follow: bool,
) -> Result<Arc<INode>, SysError> {
let follow = true;
debug!(
"lookup_inode_at: fd: {:?}, cwd: {:?}, path: {:?}, follow: {:?}",
dirfd, self.cwd, path, follow
);
let follow_max_depth = if follow { FOLLOW_MAX_DEPTH } else { 0 };
if dirfd == AT_FDCWD {
Ok(ROOT_INODE
.lookup(&self.cwd)?
.lookup_follow(path, follow_max_depth)?)
} else {
let file = match self.files.get(&dirfd).ok_or(SysError::EBADF)? {
FileLike::File(file) => file,
_ => return Err(SysError::EBADF),
};
Ok(file.lookup_follow(path, follow_max_depth)?)
}
}
pub fn lookup_inode(&self, path: &str) -> Result<Arc<INode>, SysError> { pub fn lookup_inode(&self, path: &str) -> Result<Arc<INode>, SysError> {
debug!("lookup_inode: cwd {} path {}", self.cwd, path); self.lookup_inode_at(AT_FDCWD, path)
Ok(ROOT_INODE
.lookup(&self.cwd)?
.lookup_follow(path, FOLLOW_MAX_DEPTH)?)
} }
} }
@ -740,6 +786,13 @@ impl From<FsError> for SysError {
} }
} }
bitflags! {
struct AtFlags: usize {
const EMPTY_PATH = 0x1000;
const SYMLINK_NOFOLLOW = 0x100;
}
}
bitflags! { bitflags! {
struct OpenFlags: usize { struct OpenFlags: usize {
/// read only /// read only

View File

@ -244,20 +244,21 @@ pub fn syscall(id: usize, args: [usize; 6], tf: &mut TrapFrame) -> isize {
} }
SYS_CLOCK_GETTIME => sys_clock_gettime(args[0], args[1] as *mut TimeSpec), SYS_CLOCK_GETTIME => sys_clock_gettime(args[0], args[1] as *mut TimeSpec),
SYS_EXIT_GROUP => sys_exit_group(args[0]), SYS_EXIT_GROUP => sys_exit_group(args[0]),
SYS_OPENAT => sys_openat(args[0], args[1] as *const u8, args[2], args[3]), // TODO: handle `dfd` SYS_OPENAT => sys_openat(args[0], args[1] as *const u8, args[2], args[3]),
SYS_MKDIRAT => sys_mkdir(args[1] as *const u8, args[2]), // TODO: handle `dfd` SYS_MKDIRAT => sys_mkdirat(args[0], args[1] as *const u8, args[2]),
// SYS_MKNODAT => sys_mknod(), // SYS_MKNODAT => sys_mknod(),
// 260 // 260
SYS_FCHOWNAT => { SYS_FCHOWNAT => {
warn!("sys_fchownat is unimplemented"); warn!("sys_fchownat is unimplemented");
Ok(0) Ok(0)
} }
SYS_NEWFSTATAT => sys_stat(args[1] as *const u8, args[2] as *mut Stat), // TODO: handle `dfd`, `flag` SYS_NEWFSTATAT => sys_fstatat(args[0], args[1] as *const u8, args[2] as *mut Stat, args[3]),
SYS_UNLINKAT => sys_unlink(args[1] as *const u8), // TODO: handle `dfd`, `flag` SYS_UNLINKAT => sys_unlinkat(args[0], args[1] as *const u8, args[2]),
SYS_RENAMEAT => sys_renameat(args[0], args[1] as *const u8, args[2], args[3] as *const u8), // TODO: handle `olddfd`, `newdfd` SYS_READLINKAT => sys_readlinkat(args[0], args[1] as *const u8, args[2] as *mut u8, args[3]),
SYS_LINKAT => sys_link(args[1] as *const u8, args[3] as *const u8), // TODO: handle `olddfd`, `newdfd`, `flags` SYS_RENAMEAT => sys_renameat(args[0], args[1] as *const u8, args[2], args[3] as *const u8),
SYS_LINKAT => sys_linkat(args[0], args[1] as *const u8, args[2], args[3] as *const u8, args[4]),
SYS_SYMLINKAT => Err(SysError::EACCES), SYS_SYMLINKAT => Err(SysError::EACCES),
SYS_FACCESSAT => sys_access(args[1] as *const u8, args[2]), // TODO: handle `dfd` SYS_FACCESSAT => sys_faccessat(args[0], args[1] as *const u8, args[2], args[3]),
// 280 // 280
SYS_UTIMENSAT => { SYS_UTIMENSAT => {
warn!("sys_utimensat is unimplemented"); warn!("sys_utimensat is unimplemented");