2024-01-03 03:22:36 +00:00
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
|
2025-01-24 10:06:53 +00:00
|
|
|
#![expect(unused_variables)]
|
2024-06-03 18:34:33 +00:00
|
|
|
|
2024-08-26 07:31:58 +00:00
|
|
|
//! Opened Inode-backed File Handle
|
2022-12-30 03:22:04 +00:00
|
|
|
|
|
|
|
|
mod dyn_cap;
|
|
|
|
|
mod static_cap;
|
|
|
|
|
|
2023-05-22 09:45:14 +00:00
|
|
|
use core::sync::atomic::{AtomicU32, Ordering};
|
2024-02-25 14:09:24 +00:00
|
|
|
|
|
|
|
|
use aster_rights::Rights;
|
2024-01-04 09:52:27 +00:00
|
|
|
use inherit_methods_macro::inherit_methods;
|
2023-05-22 09:45:14 +00:00
|
|
|
|
2024-02-25 14:09:24 +00:00
|
|
|
use crate::{
|
|
|
|
|
events::IoEvents,
|
|
|
|
|
fs::{
|
2025-08-13 16:11:06 +00:00
|
|
|
file_handle::{FileLike, Mappable},
|
2025-08-04 03:40:23 +00:00
|
|
|
path::Path,
|
2024-02-25 14:09:24 +00:00
|
|
|
utils::{
|
2025-07-30 09:00:08 +00:00
|
|
|
AccessMode, DirentVisitor, FallocMode, FileRange, FlockItem, FlockList, Inode,
|
2025-08-23 03:16:27 +00:00
|
|
|
InodeMode, InodeType, IoctlCmd, Metadata, RangeLockItem, RangeLockList, RangeLockType,
|
|
|
|
|
SeekFrom, StatusFlags, OFFSET_MAX,
|
2024-02-25 14:09:24 +00:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
prelude::*,
|
2024-09-02 06:31:20 +00:00
|
|
|
process::{
|
2024-10-24 09:45:47 +00:00
|
|
|
signal::{PollHandle, Pollable},
|
2024-09-02 06:31:20 +00:00
|
|
|
Gid, Uid,
|
|
|
|
|
},
|
2023-04-18 08:13:52 +00:00
|
|
|
};
|
2022-12-30 03:22:04 +00:00
|
|
|
|
2023-08-04 04:00:22 +00:00
|
|
|
#[derive(Debug)]
|
2022-12-30 03:22:04 +00:00
|
|
|
pub struct InodeHandle<R = Rights>(Arc<InodeHandle_>, R);
|
|
|
|
|
|
|
|
|
|
struct InodeHandle_ {
|
2025-08-04 03:40:23 +00:00
|
|
|
path: Path,
|
2023-11-16 09:26:42 +00:00
|
|
|
/// `file_io` is Similar to `file_private` field in `file` structure in linux. If
|
|
|
|
|
/// `file_io` is Some, typical file operations including `read`, `write`, `poll`,
|
2025-08-04 03:40:23 +00:00
|
|
|
/// `ioctl` will be provided by `file_io`, instead of `path`.
|
2023-11-16 09:26:42 +00:00
|
|
|
file_io: Option<Arc<dyn FileIo>>,
|
2022-12-30 03:22:04 +00:00
|
|
|
offset: Mutex<usize>,
|
|
|
|
|
access_mode: AccessMode,
|
2023-05-22 09:45:14 +00:00
|
|
|
status_flags: AtomicU32,
|
2022-12-30 03:22:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl InodeHandle_ {
|
2024-08-22 07:52:20 +00:00
|
|
|
pub fn read(&self, writer: &mut VmWriter) -> Result<usize> {
|
2023-11-16 09:26:42 +00:00
|
|
|
if let Some(ref file_io) = self.file_io {
|
2025-10-31 02:28:58 +00:00
|
|
|
return file_io.read(writer, self.status_flags());
|
2023-11-16 09:26:42 +00:00
|
|
|
}
|
|
|
|
|
|
2025-08-04 03:40:23 +00:00
|
|
|
if !self.path.inode().is_seekable() {
|
2024-09-02 09:48:15 +00:00
|
|
|
return self.read_at(0, writer);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-15 11:03:59 +00:00
|
|
|
let mut offset = self.offset.lock();
|
|
|
|
|
|
2024-08-22 07:52:20 +00:00
|
|
|
let len = self.read_at(*offset, writer)?;
|
2022-12-30 03:22:04 +00:00
|
|
|
|
|
|
|
|
*offset += len;
|
|
|
|
|
Ok(len)
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-22 07:52:20 +00:00
|
|
|
pub fn write(&self, reader: &mut VmReader) -> Result<usize> {
|
2023-11-16 09:26:42 +00:00
|
|
|
if let Some(ref file_io) = self.file_io {
|
2025-10-31 02:28:58 +00:00
|
|
|
return file_io.write(reader, self.status_flags());
|
2023-11-16 09:26:42 +00:00
|
|
|
}
|
|
|
|
|
|
2025-08-04 03:40:23 +00:00
|
|
|
if !self.path.inode().is_seekable() {
|
2024-09-02 09:48:15 +00:00
|
|
|
return self.write_at(0, reader);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-15 11:03:59 +00:00
|
|
|
let mut offset = self.offset.lock();
|
|
|
|
|
|
2023-05-22 09:45:14 +00:00
|
|
|
if self.status_flags().contains(StatusFlags::O_APPEND) {
|
2025-08-04 03:40:23 +00:00
|
|
|
*offset = self.path.size();
|
2022-12-30 03:22:04 +00:00
|
|
|
}
|
2024-05-15 11:03:59 +00:00
|
|
|
|
2024-08-22 07:52:20 +00:00
|
|
|
let len = self.write_at(*offset, reader)?;
|
2022-12-30 03:22:04 +00:00
|
|
|
|
|
|
|
|
*offset += len;
|
|
|
|
|
Ok(len)
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-22 07:52:20 +00:00
|
|
|
pub fn read_at(&self, offset: usize, writer: &mut VmWriter) -> Result<usize> {
|
2024-05-15 11:03:59 +00:00
|
|
|
if let Some(ref file_io) = self.file_io {
|
|
|
|
|
todo!("support read_at for FileIo");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if self.status_flags().contains(StatusFlags::O_DIRECT) {
|
2025-08-04 03:40:23 +00:00
|
|
|
self.path.inode().read_direct_at(offset, writer)
|
2024-05-15 11:03:59 +00:00
|
|
|
} else {
|
2025-08-04 03:40:23 +00:00
|
|
|
self.path.inode().read_at(offset, writer)
|
2024-05-15 11:03:59 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-22 07:52:20 +00:00
|
|
|
pub fn write_at(&self, mut offset: usize, reader: &mut VmReader) -> Result<usize> {
|
2024-05-15 11:03:59 +00:00
|
|
|
if let Some(ref file_io) = self.file_io {
|
|
|
|
|
todo!("support write_at for FileIo");
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-03 03:38:19 +00:00
|
|
|
let status_flags = self.status_flags();
|
|
|
|
|
if status_flags.contains(StatusFlags::O_APPEND) {
|
2024-05-15 11:03:59 +00:00
|
|
|
// If the file has the O_APPEND flag, the offset is ignored
|
2025-08-04 03:40:23 +00:00
|
|
|
offset = self.path.size();
|
2024-05-15 11:03:59 +00:00
|
|
|
}
|
|
|
|
|
|
2024-09-03 03:38:19 +00:00
|
|
|
if status_flags.contains(StatusFlags::O_DIRECT) {
|
2025-08-04 03:40:23 +00:00
|
|
|
self.path.inode().write_direct_at(offset, reader)
|
2024-05-15 11:03:59 +00:00
|
|
|
} else {
|
2025-08-04 03:40:23 +00:00
|
|
|
self.path.inode().write_at(offset, reader)
|
2024-05-15 11:03:59 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-30 03:22:04 +00:00
|
|
|
pub fn seek(&self, pos: SeekFrom) -> Result<usize> {
|
2025-11-06 08:30:03 +00:00
|
|
|
do_seek_util(self.path.inode().as_ref(), &self.offset, pos)
|
2022-12-30 03:22:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn offset(&self) -> usize {
|
|
|
|
|
let offset = self.offset.lock();
|
|
|
|
|
*offset
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-05 06:44:19 +00:00
|
|
|
pub fn resize(&self, new_size: usize) -> Result<()> {
|
2025-11-06 08:30:03 +00:00
|
|
|
do_resize_util(self.path.inode().as_ref(), self.status_flags(), new_size)
|
2023-03-03 02:47:14 +00:00
|
|
|
}
|
|
|
|
|
|
2022-12-30 03:22:04 +00:00
|
|
|
pub fn access_mode(&self) -> AccessMode {
|
|
|
|
|
self.access_mode
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn status_flags(&self) -> StatusFlags {
|
2023-05-22 09:45:14 +00:00
|
|
|
let bits = self.status_flags.load(Ordering::Relaxed);
|
|
|
|
|
StatusFlags::from_bits(bits).unwrap()
|
2022-12-30 03:22:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn set_status_flags(&self, new_status_flags: StatusFlags) {
|
2023-05-22 09:45:14 +00:00
|
|
|
self.status_flags
|
|
|
|
|
.store(new_status_flags.bits(), Ordering::Relaxed);
|
2022-12-30 03:22:04 +00:00
|
|
|
}
|
|
|
|
|
|
2023-03-08 07:08:31 +00:00
|
|
|
pub fn readdir(&self, visitor: &mut dyn DirentVisitor) -> Result<usize> {
|
2022-12-30 03:22:04 +00:00
|
|
|
let mut offset = self.offset.lock();
|
2025-08-04 03:40:23 +00:00
|
|
|
let read_cnt = self.path.inode().readdir_at(*offset, visitor)?;
|
2023-03-08 07:08:31 +00:00
|
|
|
*offset += read_cnt;
|
|
|
|
|
Ok(read_cnt)
|
2022-12-30 03:22:04 +00:00
|
|
|
}
|
2023-11-16 09:26:42 +00:00
|
|
|
|
2024-10-24 09:45:47 +00:00
|
|
|
fn poll(&self, mask: IoEvents, poller: Option<&mut PollHandle>) -> IoEvents {
|
2023-11-16 09:26:42 +00:00
|
|
|
if let Some(ref file_io) = self.file_io {
|
|
|
|
|
return file_io.poll(mask, poller);
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-04 03:40:23 +00:00
|
|
|
self.path.inode().poll(mask, poller)
|
2023-11-16 09:26:42 +00:00
|
|
|
}
|
|
|
|
|
|
2024-06-28 10:45:16 +00:00
|
|
|
fn fallocate(&self, mode: FallocMode, offset: usize, len: usize) -> Result<()> {
|
2025-11-06 08:30:03 +00:00
|
|
|
do_fallocate_util(
|
|
|
|
|
self.path.inode().as_ref(),
|
|
|
|
|
self.status_flags(),
|
|
|
|
|
mode,
|
|
|
|
|
offset,
|
|
|
|
|
len,
|
|
|
|
|
)
|
2024-06-28 10:45:16 +00:00
|
|
|
}
|
|
|
|
|
|
2023-11-16 09:26:42 +00:00
|
|
|
fn ioctl(&self, cmd: IoctlCmd, arg: usize) -> Result<i32> {
|
|
|
|
|
if let Some(ref file_io) = self.file_io {
|
|
|
|
|
return file_io.ioctl(cmd, arg);
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-04 03:40:23 +00:00
|
|
|
self.path.inode().ioctl(cmd, arg)
|
2023-11-16 09:26:42 +00:00
|
|
|
}
|
2024-08-12 03:51:32 +00:00
|
|
|
|
2025-08-13 16:11:06 +00:00
|
|
|
fn mappable(&self) -> Result<Mappable> {
|
|
|
|
|
let inode = self.path.inode();
|
|
|
|
|
if inode.page_cache().is_some() {
|
|
|
|
|
// If the inode has a page cache, it is a file-backed mapping and
|
|
|
|
|
// we directly return the corresponding inode.
|
|
|
|
|
Ok(Mappable::Inode(inode.clone()))
|
|
|
|
|
} else if let Some(ref file_io) = self.file_io {
|
|
|
|
|
// Otherwise, it is a special file (e.g. device file) and we should
|
|
|
|
|
// return the file-specific mappable object.
|
|
|
|
|
file_io.mappable()
|
|
|
|
|
} else {
|
|
|
|
|
return_errno_with_message!(Errno::EINVAL, "the file is not mappable");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-05 15:26:30 +00:00
|
|
|
fn test_range_lock(&self, mut lock: RangeLockItem) -> Result<RangeLockItem> {
|
|
|
|
|
let Some(extension) = self.path.inode().extension() else {
|
|
|
|
|
// Range locks are not supported. So nothing is locked.
|
|
|
|
|
lock.set_type(RangeLockType::Unlock);
|
|
|
|
|
return Ok(lock);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let Some(range_lock_list) = extension.get::<RangeLockList>() else {
|
|
|
|
|
// The lock list is not present. So nothing is locked.
|
|
|
|
|
lock.set_type(RangeLockType::Unlock);
|
|
|
|
|
return Ok(lock);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let req_lock = range_lock_list.test_lock(lock);
|
2024-08-12 03:51:32 +00:00
|
|
|
Ok(req_lock)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn set_range_lock(&self, lock: &RangeLockItem, is_nonblocking: bool) -> Result<()> {
|
|
|
|
|
if RangeLockType::Unlock == lock.type_() {
|
|
|
|
|
self.unlock_range_lock(lock);
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-05 15:26:30 +00:00
|
|
|
let Some(extension) = self.path.inode().extension() else {
|
|
|
|
|
// TODO: Figure out whether range locks are supported on all inodes.
|
|
|
|
|
warn!("the inode does not have support for range locks; this operation will fail");
|
|
|
|
|
return_errno_with_message!(Errno::ENOLCK, "range locks are not supported");
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let range_lock_list = match extension.get::<RangeLockList>() {
|
|
|
|
|
Some(list) => list,
|
|
|
|
|
None => extension.get_or_put_default::<RangeLockList>(),
|
|
|
|
|
};
|
|
|
|
|
range_lock_list.set_lock(lock, is_nonblocking)
|
2024-08-12 03:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn release_range_locks(&self) {
|
2025-08-04 03:40:23 +00:00
|
|
|
if self.path.inode().extension().is_none() {
|
2024-09-13 03:36:33 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-23 03:16:27 +00:00
|
|
|
let range_lock = RangeLockItem::new(
|
|
|
|
|
RangeLockType::Unlock,
|
|
|
|
|
FileRange::new(0, OFFSET_MAX).unwrap(),
|
|
|
|
|
);
|
2024-09-13 03:36:33 +00:00
|
|
|
self.unlock_range_lock(&range_lock);
|
2024-08-12 03:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn unlock_range_lock(&self, lock: &RangeLockItem) {
|
2025-11-05 15:26:30 +00:00
|
|
|
if let Some(extension) = self.path.inode().extension()
|
|
|
|
|
&& let Some(range_lock_list) = extension.get::<RangeLockList>()
|
|
|
|
|
{
|
|
|
|
|
range_lock_list.unlock(lock);
|
2024-08-12 03:51:32 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn set_flock(&self, lock: FlockItem, is_nonblocking: bool) -> Result<()> {
|
2025-11-05 15:26:30 +00:00
|
|
|
let Some(extension) = self.path.inode().extension() else {
|
|
|
|
|
// TODO: Figure out whether flocks are supported on all inodes.
|
|
|
|
|
warn!("the inode does not have support for flocks; this operation will fail");
|
|
|
|
|
return_errno_with_message!(Errno::ENOLCK, "flocks are not supported");
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let flock_list = match extension.get::<FlockList>() {
|
|
|
|
|
Some(list) => list,
|
|
|
|
|
None => extension.get_or_put_default::<FlockList>(),
|
|
|
|
|
};
|
|
|
|
|
flock_list.set_lock(lock, is_nonblocking)
|
2024-08-12 03:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn unlock_flock<R>(&self, req_owner: &InodeHandle<R>) {
|
2025-11-05 15:26:30 +00:00
|
|
|
if let Some(extension) = self.path.inode().extension()
|
|
|
|
|
&& let Some(flock_list) = extension.get::<FlockList>()
|
|
|
|
|
{
|
|
|
|
|
flock_list.unlock(req_owner);
|
2024-08-12 03:51:32 +00:00
|
|
|
}
|
|
|
|
|
}
|
2022-12-30 03:22:04 +00:00
|
|
|
}
|
|
|
|
|
|
2025-08-04 03:40:23 +00:00
|
|
|
#[inherit_methods(from = "self.path")]
|
2024-01-04 09:52:27 +00:00
|
|
|
impl InodeHandle_ {
|
|
|
|
|
pub fn size(&self) -> usize;
|
|
|
|
|
pub fn metadata(&self) -> Metadata;
|
|
|
|
|
pub fn mode(&self) -> Result<InodeMode>;
|
|
|
|
|
pub fn set_mode(&self, mode: InodeMode) -> Result<()>;
|
|
|
|
|
pub fn owner(&self) -> Result<Uid>;
|
|
|
|
|
pub fn set_owner(&self, uid: Uid) -> Result<()>;
|
|
|
|
|
pub fn group(&self) -> Result<Gid>;
|
|
|
|
|
pub fn set_group(&self, gid: Gid) -> Result<()>;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-04 04:00:22 +00:00
|
|
|
impl Debug for InodeHandle_ {
|
|
|
|
|
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
|
|
|
|
|
f.debug_struct("InodeHandle_")
|
2025-08-04 03:40:23 +00:00
|
|
|
.field("path", &self.path)
|
2023-08-04 04:00:22 +00:00
|
|
|
.field("offset", &self.offset())
|
|
|
|
|
.field("access_mode", &self.access_mode())
|
|
|
|
|
.field("status_flags", &self.status_flags())
|
2025-11-05 15:26:30 +00:00
|
|
|
.finish_non_exhaustive()
|
2023-08-04 04:00:22 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-30 03:22:04 +00:00
|
|
|
/// Methods for both dyn and static
|
|
|
|
|
impl<R> InodeHandle<R> {
|
2025-08-04 03:40:23 +00:00
|
|
|
pub fn path(&self) -> &Path {
|
|
|
|
|
&self.0.path
|
2023-01-06 08:06:33 +00:00
|
|
|
}
|
2024-08-12 03:51:32 +00:00
|
|
|
|
|
|
|
|
pub fn offset(&self) -> usize {
|
|
|
|
|
self.0.offset()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<R> Drop for InodeHandle<R> {
|
|
|
|
|
fn drop(&mut self) {
|
2025-11-05 15:26:30 +00:00
|
|
|
self.0.release_range_locks();
|
|
|
|
|
self.0.unlock_flock(self);
|
2024-08-12 03:51:32 +00:00
|
|
|
}
|
2022-12-30 03:22:04 +00:00
|
|
|
}
|
2023-11-16 09:26:42 +00:00
|
|
|
|
2025-10-31 02:28:58 +00:00
|
|
|
/// A trait for file-like objects that provide custom I/O operations.
|
|
|
|
|
///
|
|
|
|
|
/// This trait is typically implemented for special files like devices or
|
|
|
|
|
/// named pipes (FIFOs), which have behaviors different from regular on-disk files.
|
|
|
|
|
//
|
|
|
|
|
// TODO: The `status_flags` parameter in `read` and `write` may need to be stored directly
|
|
|
|
|
// in the `FileIo`. We need further refactoring to find an appropriate way to enable `FileIo`
|
|
|
|
|
// to utilize the information in the `InodeHandle_`.
|
2024-09-02 06:31:20 +00:00
|
|
|
pub trait FileIo: Pollable + Send + Sync + 'static {
|
2025-10-31 02:28:58 +00:00
|
|
|
/// Reads data from the file into the given `VmWriter`.
|
|
|
|
|
fn read(&self, writer: &mut VmWriter, status_flags: StatusFlags) -> Result<usize>;
|
2023-11-16 09:26:42 +00:00
|
|
|
|
2025-10-31 02:28:58 +00:00
|
|
|
/// Writes data from the given `VmReader` into the file.
|
|
|
|
|
fn write(&self, reader: &mut VmReader, status_flags: StatusFlags) -> Result<usize>;
|
2023-11-16 09:26:42 +00:00
|
|
|
|
2025-08-13 16:11:06 +00:00
|
|
|
/// See [`FileLike::mappable`].
|
|
|
|
|
fn mappable(&self) -> Result<Mappable> {
|
|
|
|
|
return_errno_with_message!(Errno::EINVAL, "the file is not mappable");
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-16 09:26:42 +00:00
|
|
|
fn ioctl(&self, cmd: IoctlCmd, arg: usize) -> Result<i32> {
|
|
|
|
|
return_errno_with_message!(Errno::EINVAL, "ioctl is not supported");
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-07-30 09:00:08 +00:00
|
|
|
|
2025-11-06 08:30:03 +00:00
|
|
|
pub(super) fn do_seek_util(
|
|
|
|
|
inode: &dyn Inode,
|
|
|
|
|
offset: &Mutex<usize>,
|
|
|
|
|
pos: SeekFrom,
|
|
|
|
|
) -> Result<usize> {
|
2025-07-30 09:00:08 +00:00
|
|
|
let mut offset = offset.lock();
|
|
|
|
|
let new_offset: isize = match pos {
|
|
|
|
|
SeekFrom::Start(off /* as usize */) => {
|
|
|
|
|
if off > isize::MAX as usize {
|
|
|
|
|
return_errno_with_message!(Errno::EINVAL, "file offset is too large");
|
|
|
|
|
}
|
|
|
|
|
off as isize
|
|
|
|
|
}
|
|
|
|
|
SeekFrom::End(off /* as isize */) => {
|
|
|
|
|
let file_size = inode.size() as isize;
|
|
|
|
|
assert!(file_size >= 0);
|
|
|
|
|
file_size
|
|
|
|
|
.checked_add(off)
|
|
|
|
|
.ok_or_else(|| Error::with_message(Errno::EOVERFLOW, "file offset overflow"))?
|
|
|
|
|
}
|
|
|
|
|
SeekFrom::Current(off /* as isize */) => (*offset as isize)
|
|
|
|
|
.checked_add(off)
|
|
|
|
|
.ok_or_else(|| Error::with_message(Errno::EOVERFLOW, "file offset overflow"))?,
|
|
|
|
|
};
|
|
|
|
|
if new_offset < 0 {
|
|
|
|
|
return_errno_with_message!(Errno::EINVAL, "file offset must not be negative");
|
|
|
|
|
}
|
|
|
|
|
// Invariant: 0 <= new_offset <= isize::MAX
|
|
|
|
|
let new_offset = new_offset as usize;
|
|
|
|
|
*offset = new_offset;
|
|
|
|
|
Ok(new_offset)
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-06 08:30:03 +00:00
|
|
|
pub(super) fn do_fallocate_util(
|
|
|
|
|
inode: &dyn Inode,
|
2025-07-30 09:00:08 +00:00
|
|
|
status_flags: StatusFlags,
|
|
|
|
|
mode: FallocMode,
|
|
|
|
|
offset: usize,
|
|
|
|
|
len: usize,
|
|
|
|
|
) -> Result<()> {
|
2025-10-31 02:32:41 +00:00
|
|
|
let inode_type = inode.type_();
|
|
|
|
|
// TODO: `fallocate` on pipe files also fails with `ESPIPE`.
|
|
|
|
|
if inode_type == InodeType::NamedPipe {
|
|
|
|
|
return_errno_with_message!(Errno::ESPIPE, "the inode is a FIFO file");
|
|
|
|
|
}
|
|
|
|
|
if !(inode_type == InodeType::File || inode_type == InodeType::Dir) {
|
|
|
|
|
return_errno_with_message!(
|
|
|
|
|
Errno::ENODEV,
|
|
|
|
|
"the inode is not a regular file or a directory"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-30 09:00:08 +00:00
|
|
|
if status_flags.contains(StatusFlags::O_APPEND)
|
|
|
|
|
&& (mode == FallocMode::PunchHoleKeepSize
|
|
|
|
|
|| mode == FallocMode::CollapseRange
|
|
|
|
|
|| mode == FallocMode::InsertRange)
|
|
|
|
|
{
|
|
|
|
|
return_errno_with_message!(
|
|
|
|
|
Errno::EPERM,
|
|
|
|
|
"the flags do not work on the append-only file"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
if status_flags.contains(StatusFlags::O_DIRECT) || status_flags.contains(StatusFlags::O_PATH) {
|
|
|
|
|
return_errno_with_message!(
|
|
|
|
|
Errno::EBADF,
|
|
|
|
|
"currently fallocate file with O_DIRECT or O_PATH is not supported"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inode.fallocate(mode, offset, len)
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-06 08:30:03 +00:00
|
|
|
pub(super) fn do_resize_util(
|
|
|
|
|
inode: &dyn Inode,
|
2025-07-30 09:00:08 +00:00
|
|
|
status_flags: StatusFlags,
|
|
|
|
|
new_size: usize,
|
|
|
|
|
) -> Result<()> {
|
|
|
|
|
if status_flags.contains(StatusFlags::O_APPEND) {
|
|
|
|
|
// FIXME: It's allowed to `ftruncate` an append-only file on Linux.
|
|
|
|
|
return_errno_with_message!(Errno::EPERM, "can not resize append-only file");
|
|
|
|
|
}
|
|
|
|
|
inode.resize(new_size)
|
|
|
|
|
}
|