Implement FUSE_RELEASE/RELEASEDIR

Fixes #3314
This commit is contained in:
Boyuan He
2020-09-16 12:19:30 -07:00
committed by Andrei Vagin
parent 32044f94e9
commit 947088e10a
9 changed files with 154 additions and 6 deletions
+18
View File
@@ -395,3 +395,21 @@ type FUSEOpenOut struct {
_ uint32
}
// FUSEReleaseIn is the request sent by the kernel to the daemon
// when there is no more reference to a file.
//
// +marshal
type FUSEReleaseIn struct {
// Fh is the file handler for the file to be released.
Fh uint64
// Flags of the file.
Flags uint32
// ReleaseFlags of this release request.
ReleaseFlags uint32
// LockOwner is the id of the lock owner if there is one.
LockOwner uint64
}
+6
View File
@@ -168,6 +168,9 @@ func (fd *DeviceFD) readLocked(ctx context.Context, dst usermem.IOSequence, opts
// We're done with this request.
fd.queue.Remove(req)
if req.hdr.Opcode == linux.FUSE_RELEASE {
fd.numActiveRequests -= 1
}
// Restart the read as this request was invalid.
log.Warningf("fuse.DeviceFD.Read: request found was too large. Restarting read.")
@@ -184,6 +187,9 @@ func (fd *DeviceFD) readLocked(ctx context.Context, dst usermem.IOSequence, opts
if readCursor >= req.hdr.Len {
// Fully done with this req, remove it from the queue.
fd.queue.Remove(req)
if req.hdr.Opcode == linux.FUSE_RELEASE {
fd.numActiveRequests -= 1
}
break
}
}
+29 -2
View File
@@ -18,6 +18,8 @@ import (
"gvisor.dev/gvisor/pkg/abi/linux"
"gvisor.dev/gvisor/pkg/context"
"gvisor.dev/gvisor/pkg/sentry/fsimpl/kernfs"
"gvisor.dev/gvisor/pkg/sentry/kernel"
"gvisor.dev/gvisor/pkg/sentry/kernel/auth"
"gvisor.dev/gvisor/pkg/sentry/vfs"
"gvisor.dev/gvisor/pkg/usermem"
)
@@ -54,8 +56,34 @@ func (fd *fileDescription) filesystem() *vfs.Filesystem {
return fd.vfsfd.VirtualDentry().Mount().Filesystem()
}
func (fd *fileDescription) statusFlags() uint32 {
return fd.vfsfd.StatusFlags()
}
// Release implements vfs.FileDescriptionImpl.Release.
func (fd *fileDescription) Release(ctx context.Context) {}
func (fd *fileDescription) Release(ctx context.Context) {
// no need to release if FUSE server doesn't implement Open.
conn := fd.inode().fs.conn
if conn.noOpen {
return
}
in := linux.FUSEReleaseIn{
Fh: fd.Fh,
Flags: fd.statusFlags(),
}
// TODO(gvisor.dev/issue/3245): add logic when we support file lock owner.
var opcode linux.FUSEOpcode
if fd.inode().Mode().IsDir() {
opcode = linux.FUSE_RELEASEDIR
} else {
opcode = linux.FUSE_RELEASE
}
kernelTask := kernel.TaskFromContext(ctx)
// ignoring errors and FUSE server reply is analogous to Linux's behavior.
req, _ := conn.NewRequest(auth.CredentialsFromContext(ctx), uint32(kernelTask.ThreadID()), fd.inode().NodeID, opcode, &in)
conn.CallAsync(kernelTask, req)
}
// PRead implements vfs.FileDescriptionImpl.PRead.
func (fd *fileDescription) PRead(ctx context.Context, dst usermem.IOSequence, offset int64, opts vfs.ReadOptions) (int64, error) {
@@ -82,7 +110,6 @@ func (fd *fileDescription) Seek(ctx context.Context, offset int64, whence int32)
return 0, nil
}
// Stat implements FileDescriptionImpl.Stat.
// Stat implements vfs.FileDescriptionImpl.Stat.
func (fd *fileDescription) Stat(ctx context.Context, opts vfs.StatOptions) (linux.Statx, error) {
fs := fd.filesystem()
+5
View File
@@ -11,3 +11,8 @@ syscall_test(
fuse = "True",
test = "//test/fuse/linux:open_test",
)
syscall_test(
fuse = "True",
test = "//test/fuse/linux:release_test",
)
+13
View File
@@ -31,6 +31,19 @@ cc_binary(
],
)
cc_binary(
name = "release_test",
testonly = 1,
srcs = ["release_test.cc"],
deps = [
gtest,
":fuse_base",
"//test/util:fuse_util",
"//test/util:test_main",
"//test/util:test_util",
],
)
cc_library(
name = "fuse_base",
testonly = 1,
+2 -1
View File
@@ -387,7 +387,8 @@ void FuseTest::ServerProcessFuseRequest() {
requests_.AddMemBlock(in_header->opcode, buf.data(), len);
if (in_header->opcode == FUSE_RELEASE) return;
if (in_header->opcode == FUSE_RELEASE || in_header->opcode == FUSE_RELEASEDIR)
return;
// Check if there is a corresponding response.
if (responses_.End()) {
GTEST_NONFATAL_FAILURE_("No more FUSE response is expected");
+3 -3
View File
@@ -166,13 +166,13 @@ class FuseTest : public ::testing::Test {
protected:
TempPath mount_point_;
// Unmounts the mountpoint of the FUSE server.
void UnmountFuse();
private:
// Opens /dev/fuse and inherit the file descriptor for the FUSE server.
void MountFuse();
// Unmounts the mountpoint of the FUSE server.
void UnmountFuse();
// Creates a socketpair for communication and forks FUSE server.
void SetUpFuseServer();
+4
View File
@@ -33,6 +33,10 @@ namespace testing {
namespace {
class OpenTest : public FuseTest {
// OpenTest doesn't care the release request when close a fd,
// so doesn't check leftover requests when tearing down.
void TearDown() { UnmountFuse(); }
protected:
const std::string test_file_ = "test_file";
const mode_t regular_file_ = S_IFREG | S_IRWXU | S_IRWXG | S_IRWXO;
+74
View File
@@ -0,0 +1,74 @@
// Copyright 2020 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <errno.h>
#include <fcntl.h>
#include <linux/fuse.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/statfs.h>
#include <sys/types.h>
#include <unistd.h>
#include <string>
#include <vector>
#include "gtest/gtest.h"
#include "test/fuse/linux/fuse_base.h"
#include "test/util/fuse_util.h"
#include "test/util/test_util.h"
namespace gvisor {
namespace testing {
namespace {
class ReleaseTest : public FuseTest {
protected:
const std::string test_file_ = "test_file";
};
TEST_F(ReleaseTest, RegularFile) {
const std::string test_file_path =
JoinPath(mount_point_.path().c_str(), test_file_);
SetServerInodeLookup(test_file_, S_IFREG | S_IRWXU | S_IRWXG | S_IRWXO);
struct fuse_out_header out_header = {
.len = sizeof(struct fuse_out_header) + sizeof(struct fuse_open_out),
};
struct fuse_open_out out_payload = {
.fh = 1,
.open_flags = O_RDWR,
};
auto iov_out = FuseGenerateIovecs(out_header, out_payload);
SetServerResponse(FUSE_OPEN, iov_out);
FileDescriptor fd = ASSERT_NO_ERRNO_AND_VALUE(Open(test_file_path, O_RDWR));
SkipServerActualRequest();
ASSERT_THAT(close(fd.release()), SyscallSucceeds());
struct fuse_in_header in_header;
struct fuse_release_in in_payload;
auto iov_in = FuseGenerateIovecs(in_header, in_payload);
GetServerActualRequest(iov_in);
EXPECT_EQ(in_header.len, sizeof(in_header) + sizeof(in_payload));
EXPECT_EQ(in_header.opcode, FUSE_RELEASE);
EXPECT_EQ(in_payload.flags, O_RDWR);
EXPECT_EQ(in_payload.fh, 1);
}
} // namespace
} // namespace testing
} // namespace gvisor