Skip to content

Commit 7a630e5

Browse files
jiangliubergwolf
authored andcommitted
ptfs: make use of helpers from util
Make use of helpers from util. Signed-off-by: Jiang Liu <[email protected]>
1 parent 197a404 commit 7a630e5

File tree

3 files changed

+27
-20
lines changed

3 files changed

+27
-20
lines changed

src/passthrough/mod.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use self::inode_store::{InodeId, InodeStore};
3333
use self::mount_fd::MountFds;
3434
use self::statx::{statx, StatExt};
3535
use self::util::{
36-
ebadf, einval, enosys, is_dir, is_safe_inode, openat, reopen_fd_through_proc, stat_fd,
36+
ebadf, einval, enosys, eperm, is_dir, is_safe_inode, openat, reopen_fd_through_proc, stat_fd,
3737
UniqueInodeGenerator,
3838
};
3939
use crate::abi::fuse_abi as fuse;
@@ -581,16 +581,20 @@ impl<S: BitmapSlice + Send + Sync> PassthroughFs<S> {
581581
) -> io::Result<(File, Option<FileHandle>, StatExt)> {
582582
let path_file = self.open_file_restricted(dir, name, libc::O_PATH, 0)?;
583583
let st = statx(&path_file, None)?;
584-
let handle = FileHandle::from_fd(&path_file)?;
584+
let handle = if self.cfg.inode_file_handles {
585+
FileHandle::from_fd(&path_file)?
586+
} else {
587+
None
588+
};
585589

586-
return Ok((path_file, handle, st));
590+
Ok((path_file, handle, st))
587591
}
588592

589593
fn to_openable_handle(&self, fh: FileHandle) -> io::Result<Arc<OpenableFileHandle>> {
590594
fh.into_openable(&self.mount_fds, |fd, flags, _mode| {
591595
reopen_fd_through_proc(&fd, flags, &self.proc_self_fd)
592596
})
593-
.map(|v| Arc::new(v))
597+
.map(Arc::new)
594598
.map_err(|e| {
595599
if !e.silent() {
596600
error!("{}", e);
@@ -809,9 +813,7 @@ impl<S: BitmapSlice + Send + Sync> PassthroughFs<S> {
809813

810814
match opcode {
811815
// write should not exceed the file size.
812-
Opcode::Write if size + offset > file_size => {
813-
Err(io::Error::from_raw_os_error(libc::EPERM))
814-
}
816+
Opcode::Write if size + offset > file_size => Err(eperm()),
815817

816818
// fallocate operation should not allocate blocks exceed the file size.
817819
//
@@ -828,7 +830,7 @@ impl<S: BitmapSlice + Send + Sync> PassthroughFs<S> {
828830
|| (mode & libc::FALLOC_FL_COLLAPSE_RANGE != 0
829831
|| mode & libc::FALLOC_FL_INSERT_RANGE != 0) =>
830832
{
831-
Err(io::Error::from_raw_os_error(libc::EPERM))
833+
Err(eperm())
832834
}
833835

834836
// setattr operation should be handled in setattr handler, other operations won't

src/passthrough/sync_io.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,8 @@ impl<S: BitmapSlice + Send + Sync> FileSystem for PassthroughFs<S> {
339339
}
340340

341341
fn statfs(&self, _ctx: &Context, inode: Inode) -> io::Result<libc::statvfs64> {
342-
let data = self.inode_map.get(inode)?;
343342
let mut out = MaybeUninit::<libc::statvfs64>::zeroed();
343+
let data = self.inode_map.get(inode)?;
344344
let file = data.get_file()?;
345345

346346
// Safe because this will only modify `out` and we check the return value.
@@ -381,7 +381,7 @@ impl<S: BitmapSlice + Send + Sync> FileSystem for PassthroughFs<S> {
381381
) -> io::Result<(Option<Handle>, OpenOptions)> {
382382
if self.no_opendir.load(Ordering::Relaxed) {
383383
info!("fuse: opendir is not supported.");
384-
Err(io::Error::from_raw_os_error(libc::ENOSYS))
384+
Err(enosys())
385385
} else {
386386
self.do_open(inode, flags | (libc::O_DIRECTORY as u32), 0)
387387
}
@@ -507,7 +507,7 @@ impl<S: BitmapSlice + Send + Sync> FileSystem for PassthroughFs<S> {
507507
) -> io::Result<(Option<Handle>, OpenOptions)> {
508508
if self.no_open.load(Ordering::Relaxed) {
509509
info!("fuse: open is not supported.");
510-
Err(io::Error::from_raw_os_error(libc::ENOSYS))
510+
Err(enosys())
511511
} else {
512512
self.do_open(inode, flags, fuse_flags)
513513
}
@@ -524,7 +524,7 @@ impl<S: BitmapSlice + Send + Sync> FileSystem for PassthroughFs<S> {
524524
_lock_owner: Option<u64>,
525525
) -> io::Result<()> {
526526
if self.no_open.load(Ordering::Relaxed) {
527-
Err(io::Error::from_raw_os_error(libc::ENOSYS))
527+
Err(enosys())
528528
} else {
529529
self.do_release(inode, handle)
530530
}
@@ -764,13 +764,13 @@ impl<S: BitmapSlice + Send + Sync> FileSystem for PassthroughFs<S> {
764764
attr.st_uid
765765
} else {
766766
// Cannot use -1 here because these are unsigned values.
767-
::std::u32::MAX
767+
u32::MAX
768768
};
769769
let gid = if valid.contains(SetattrValid::GID) {
770770
attr.st_gid
771771
} else {
772772
// Cannot use -1 here because these are unsigned values.
773-
::std::u32::MAX
773+
u32::MAX
774774
};
775775

776776
// Safe because this is a constant value and a valid C string.
@@ -1020,7 +1020,7 @@ impl<S: BitmapSlice + Send + Sync> FileSystem for PassthroughFs<S> {
10201020
_lock_owner: u64,
10211021
) -> io::Result<()> {
10221022
if self.no_open.load(Ordering::Relaxed) {
1023-
return Err(io::Error::from_raw_os_error(libc::ENOSYS));
1023+
return Err(enosys());
10241024
}
10251025

10261026
let data = self.handle_map.get(handle, inode)?;
@@ -1128,7 +1128,7 @@ impl<S: BitmapSlice + Send + Sync> FileSystem for PassthroughFs<S> {
11281128
flags: u32,
11291129
) -> io::Result<()> {
11301130
if !self.cfg.xattr {
1131-
return Err(io::Error::from_raw_os_error(libc::ENOSYS));
1131+
return Err(enosys());
11321132
}
11331133

11341134
let data = self.inode_map.get(inode)?;
@@ -1163,7 +1163,7 @@ impl<S: BitmapSlice + Send + Sync> FileSystem for PassthroughFs<S> {
11631163
size: u32,
11641164
) -> io::Result<GetxattrReply> {
11651165
if !self.cfg.xattr {
1166-
return Err(io::Error::from_raw_os_error(libc::ENOSYS));
1166+
return Err(enosys());
11671167
}
11681168

11691169
let data = self.inode_map.get(inode)?;
@@ -1198,7 +1198,7 @@ impl<S: BitmapSlice + Send + Sync> FileSystem for PassthroughFs<S> {
11981198

11991199
fn listxattr(&self, _ctx: &Context, inode: Inode, size: u32) -> io::Result<ListxattrReply> {
12001200
if !self.cfg.xattr {
1201-
return Err(io::Error::from_raw_os_error(libc::ENOSYS));
1201+
return Err(enosys());
12021202
}
12031203

12041204
let data = self.inode_map.get(inode)?;
@@ -1232,7 +1232,7 @@ impl<S: BitmapSlice + Send + Sync> FileSystem for PassthroughFs<S> {
12321232

12331233
fn removexattr(&self, _ctx: &Context, inode: Inode, name: &CStr) -> io::Result<()> {
12341234
if !self.cfg.xattr {
1235-
return Err(io::Error::from_raw_os_error(libc::ENOSYS));
1235+
return Err(enosys());
12361236
}
12371237

12381238
let data = self.inode_map.get(inode)?;

src/passthrough/util.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Use of this source code is governed by a BSD-style license that can be
22
// found in the LICENSE-BSD-3-Clause file.
3+
// Copyright (C) 2023 Alibaba Cloud. All rights reserved.
34

45
use std::collections::{btree_map, BTreeMap};
56
use std::ffi::{CStr, CString};
@@ -160,7 +161,7 @@ pub fn openat(
160161
pub fn reopen_fd_through_proc(
161162
fd: &impl AsRawFd,
162163
flags: libc::c_int,
163-
proc_self_fd: &File,
164+
proc_self_fd: &impl AsRawFd,
164165
) -> io::Result<File> {
165166
let name = CString::new(format!("{}", fd.as_raw_fd()).as_str())?;
166167
// Clear the `O_NOFOLLOW` flag if it is set since we need to follow the `/proc/self/fd` symlink
@@ -220,6 +221,10 @@ pub fn enosys() -> io::Error {
220221
io::Error::from_raw_os_error(libc::ENOSYS)
221222
}
222223

224+
pub fn eperm() -> io::Error {
225+
io::Error::from_raw_os_error(libc::EPERM)
226+
}
227+
223228
#[cfg(test)]
224229
mod tests {
225230
use super::*;

0 commit comments

Comments
 (0)