fuse(4): add tests for POSIX file locking operations

PR:		234581
Sponsored by:	The FreeBSD Foundation
This commit is contained in:
Alan Somers 2019-03-13 22:16:00 +00:00
parent 1bb6c55076
commit ef61047a9b
6 changed files with 507 additions and 7 deletions

View File

@ -11,6 +11,7 @@ ATF_TESTS_CXX+= fsync
ATF_TESTS_CXX+= fsyncdir
ATF_TESTS_CXX+= getattr
ATF_TESTS_CXX+= link
ATF_TESTS_CXX+= locks
ATF_TESTS_CXX+= lookup
ATF_TESTS_CXX+= mkdir
ATF_TESTS_CXX+= mknod
@ -64,6 +65,11 @@ SRCS.link+= link.cc
SRCS.link+= mockfs.cc
SRCS.link+= utils.cc
SRCS.locks+= locks.cc
SRCS.locks+= getmntopts.c
SRCS.locks+= mockfs.cc
SRCS.locks+= utils.cc
SRCS.lookup+= getmntopts.c
SRCS.lookup+= lookup.cc
SRCS.lookup+= mockfs.cc

475
tests/sys/fs/fuse/locks.cc Normal file
View File

@ -0,0 +1,475 @@
/*-
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
*
* Copyright (c) 2019 The FreeBSD Foundation
*
* This software was developed by BFF Storage Systems, LLC under sponsorship
* from the FreeBSD Foundation.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
extern "C" {
#include <fcntl.h>
}
#include "mockfs.hh"
#include "utils.hh"
/* This flag value should probably be defined in fuse_kernel.h */
#define OFFSET_MAX 0x7fffffffffffffffLL
using namespace testing;
/* For testing filesystems without posix locking support */
class Fallback: public FuseTest {
public:
const static uint64_t FH = 0xdeadbeef1a7ebabe;
void expect_getattr(uint64_t ino)
{
/* Until the attr cache is working, we may send an additional GETATTR */
EXPECT_CALL(*m_mock, process(
ResultOf([=](auto in) {
return (in->header.opcode == FUSE_GETATTR &&
in->header.nodeid == ino);
}, Eq(true)),
_)
).WillRepeatedly(Invoke([=](auto in, auto out) {
out->header.unique = in->header.unique;
SET_OUT_HEADER_LEN(out, attr);
out->body.attr.attr.ino = ino; // Must match nodeid
out->body.attr.attr.mode = S_IFREG | 0644;
out->body.attr.attr_valid = UINT64_MAX;
}));
}
void expect_lookup(const char *relpath, uint64_t ino)
{
EXPECT_LOOKUP(1, relpath).WillRepeatedly(Invoke([=](auto in, auto out) {
out->header.unique = in->header.unique;
SET_OUT_HEADER_LEN(out, entry);
out->body.entry.attr.mode = S_IFREG | 0644;
out->body.entry.nodeid = ino;
out->body.entry.attr_valid = UINT64_MAX;
}));
}
void expect_open(uint64_t ino)
{
EXPECT_CALL(*m_mock, process(
ResultOf([=](auto in) {
return (in->header.opcode == FUSE_OPEN &&
in->header.nodeid == ino);
}, Eq(true)),
_)
).WillOnce(Invoke([=](auto in, auto out) {
out->header.unique = in->header.unique;
out->header.len = sizeof(out->header);
SET_OUT_HEADER_LEN(out, open);
out->body.open.fh = FH;
}));
}
};
/* For testing filesystems with posix locking support */
class Locks: public Fallback {
virtual void SetUp() {
m_init_flags = FUSE_POSIX_LOCKS;
Fallback::SetUp();
}
};
class GetlkFallback: public Fallback {};
class Getlk: public Locks {};
class SetlkFallback: public Fallback {};
class Setlk: public Locks {};
class SetlkwFallback: public Fallback {};
class Setlkw: public Locks {};
/*
* If the fuse filesystem does not support posix file locks, then the kernel
* should fall back to local locks.
*/
TEST_F(GetlkFallback, local)
{
const char FULLPATH[] = "mountpoint/some_file.txt";
const char RELPATH[] = "some_file.txt";
uint64_t ino = 42;
struct flock fl;
int fd;
expect_lookup(RELPATH, ino);
expect_open(ino);
expect_getattr(ino);
fd = open(FULLPATH, O_RDWR);
ASSERT_LE(0, fd) << strerror(errno);
fl.l_start = 10;
fl.l_len = 1000;
fl.l_pid = getpid();
fl.l_type = F_RDLCK;
fl.l_whence = SEEK_SET;
fl.l_sysid = 0;
ASSERT_NE(-1, fcntl(fd, F_GETLK, &fl)) << strerror(errno);
/* Deliberately leak fd. close(2) will be tested in release.cc */
}
/*
* If the filesystem has no locks that fit the description, the filesystem
* should return F_UNLCK
*/
/* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=234581 */
TEST_F(Getlk, DISABLED_no_locks)
{
const char FULLPATH[] = "mountpoint/some_file.txt";
const char RELPATH[] = "some_file.txt";
uint64_t ino = 42;
struct flock fl;
int fd;
pid_t pid = 1234;
expect_lookup(RELPATH, ino);
expect_open(ino);
expect_getattr(ino);
EXPECT_CALL(*m_mock, process(
ResultOf([=](auto in) {
return (in->header.opcode == FUSE_GETLK &&
in->header.nodeid == ino &&
in->body.getlk.fh == FH &&
in->body.getlk.owner == (uint32_t)pid &&
in->body.getlk.lk.start == 10 &&
in->body.getlk.lk.end == 1009 &&
in->body.getlk.lk.type == F_RDLCK &&
in->body.getlk.lk.pid == 10);
}, Eq(true)),
_)
).WillOnce(Invoke([=](auto in, auto out) {
out->header.unique = in->header.unique;
SET_OUT_HEADER_LEN(out, getlk);
out->body.getlk.lk = in->body.getlk.lk;
out->body.getlk.lk.type = F_UNLCK;
}));
fd = open(FULLPATH, O_RDWR);
ASSERT_LE(0, fd) << strerror(errno);
fl.l_start = 10;
fl.l_len = 1000;
fl.l_pid = pid;
fl.l_type = F_RDLCK;
fl.l_whence = SEEK_SET;
fl.l_sysid = 0;
ASSERT_NE(-1, fcntl(fd, F_GETLK, &fl)) << strerror(errno);
ASSERT_EQ(F_UNLCK, fl.l_type);
/* Deliberately leak fd. close(2) will be tested in release.cc */
}
/* A different pid does have a lock */
/* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=234581 */
TEST_F(Getlk, DISABLED_lock_exists)
{
const char FULLPATH[] = "mountpoint/some_file.txt";
const char RELPATH[] = "some_file.txt";
uint64_t ino = 42;
struct flock fl;
int fd;
pid_t pid = 1234;
pid_t pid2 = 1234;
expect_lookup(RELPATH, ino);
expect_open(ino);
expect_getattr(ino);
EXPECT_CALL(*m_mock, process(
ResultOf([=](auto in) {
return (in->header.opcode == FUSE_GETLK &&
in->header.nodeid == ino &&
in->body.getlk.fh == FH &&
in->body.getlk.owner == (uint32_t)pid &&
in->body.getlk.lk.start == 10 &&
in->body.getlk.lk.end == 1009 &&
in->body.getlk.lk.type == F_RDLCK &&
in->body.getlk.lk.pid == 10);
}, Eq(true)),
_)
).WillOnce(Invoke([=](auto in, auto out) {
out->header.unique = in->header.unique;
SET_OUT_HEADER_LEN(out, getlk);
out->body.getlk.lk.start = 100;
out->body.getlk.lk.end = 199;
out->body.getlk.lk.type = F_WRLCK;
out->body.getlk.lk.pid = (uint32_t)pid2;;
}));
fd = open(FULLPATH, O_RDWR);
ASSERT_LE(0, fd) << strerror(errno);
fl.l_start = 10;
fl.l_len = 1000;
fl.l_pid = pid;
fl.l_type = F_RDLCK;
fl.l_whence = SEEK_SET;
fl.l_sysid = 0;
ASSERT_NE(-1, fcntl(fd, F_GETLK, &fl)) << strerror(errno);
EXPECT_EQ(100, fl.l_start);
EXPECT_EQ(100, fl.l_len);
EXPECT_EQ(pid2, fl.l_pid);
EXPECT_EQ(F_WRLCK, fl.l_type);
EXPECT_EQ(SEEK_SET, fl.l_whence);
EXPECT_EQ(0, fl.l_sysid);
/* Deliberately leak fd. close(2) will be tested in release.cc */
}
/*
* If the fuse filesystem does not support posix file locks, then the kernel
* should fall back to local locks.
*/
TEST_F(SetlkFallback, local)
{
const char FULLPATH[] = "mountpoint/some_file.txt";
const char RELPATH[] = "some_file.txt";
uint64_t ino = 42;
struct flock fl;
int fd;
expect_lookup(RELPATH, ino);
expect_open(ino);
expect_getattr(ino);
fd = open(FULLPATH, O_RDWR);
ASSERT_LE(0, fd) << strerror(errno);
fl.l_start = 10;
fl.l_len = 1000;
fl.l_pid = getpid();
fl.l_type = F_RDLCK;
fl.l_whence = SEEK_SET;
fl.l_sysid = 0;
ASSERT_NE(-1, fcntl(fd, F_SETLK, &fl)) << strerror(errno);
/* Deliberately leak fd. close(2) will be tested in release.cc */
}
/* Set a new lock with FUSE_SETLK */
/* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=234581 */
TEST_F(Setlk, DISABLED_set)
{
const char FULLPATH[] = "mountpoint/some_file.txt";
const char RELPATH[] = "some_file.txt";
uint64_t ino = 42;
struct flock fl;
int fd;
pid_t pid = 1234;
expect_lookup(RELPATH, ino);
expect_open(ino);
expect_getattr(ino);
EXPECT_CALL(*m_mock, process(
ResultOf([=](auto in) {
return (in->header.opcode == FUSE_SETLK &&
in->header.nodeid == ino &&
in->body.getlk.fh == FH &&
in->body.getlk.owner == (uint32_t)pid &&
in->body.getlk.lk.start == 10 &&
in->body.getlk.lk.end == 1009 &&
in->body.getlk.lk.type == F_RDLCK &&
in->body.getlk.lk.pid == 10);
}, Eq(true)),
_)
).WillOnce(Invoke([=](auto in, auto out) {
out->header.unique = in->header.unique;
SET_OUT_HEADER_LEN(out, getlk);
out->body.getlk.lk = in->body.getlk.lk;
out->body.getlk.lk.type = F_UNLCK;
}));
fd = open(FULLPATH, O_RDWR);
ASSERT_LE(0, fd) << strerror(errno);
fl.l_start = 10;
fl.l_len = 1000;
fl.l_pid = pid;
fl.l_type = F_RDLCK;
fl.l_whence = SEEK_SET;
fl.l_sysid = 0;
ASSERT_NE(-1, fcntl(fd, F_SETLK, &fl)) << strerror(errno);
/* Deliberately leak fd. close(2) will be tested in release.cc */
}
/* l_len = 0 is a flag value that means to lock until EOF */
/* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=234581 */
TEST_F(Setlk, DISABLED_set_eof)
{
const char FULLPATH[] = "mountpoint/some_file.txt";
const char RELPATH[] = "some_file.txt";
uint64_t ino = 42;
struct flock fl;
int fd;
pid_t pid = 1234;
expect_lookup(RELPATH, ino);
expect_open(ino);
expect_getattr(ino);
EXPECT_CALL(*m_mock, process(
ResultOf([=](auto in) {
return (in->header.opcode == FUSE_SETLK &&
in->header.nodeid == ino &&
in->body.getlk.fh == FH &&
in->body.getlk.owner == (uint32_t)pid &&
in->body.getlk.lk.start == 10 &&
in->body.getlk.lk.end == OFFSET_MAX &&
in->body.getlk.lk.type == F_RDLCK &&
in->body.getlk.lk.pid == 10);
}, Eq(true)),
_)
).WillOnce(Invoke([=](auto in, auto out) {
out->header.unique = in->header.unique;
SET_OUT_HEADER_LEN(out, getlk);
out->body.getlk.lk = in->body.getlk.lk;
out->body.getlk.lk.type = F_UNLCK;
}));
fd = open(FULLPATH, O_RDWR);
ASSERT_LE(0, fd) << strerror(errno);
fl.l_start = 10;
fl.l_len = 0;
fl.l_pid = pid;
fl.l_type = F_RDLCK;
fl.l_whence = SEEK_SET;
fl.l_sysid = 0;
ASSERT_NE(-1, fcntl(fd, F_SETLK, &fl)) << strerror(errno);
/* Deliberately leak fd. close(2) will be tested in release.cc */
}
/* Fail to set a new lock with FUSE_SETLK due to a conflict */
/* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=234581 */
TEST_F(Setlk, DISABLED_eagain)
{
const char FULLPATH[] = "mountpoint/some_file.txt";
const char RELPATH[] = "some_file.txt";
uint64_t ino = 42;
struct flock fl;
int fd;
pid_t pid = 1234;
expect_lookup(RELPATH, ino);
expect_open(ino);
expect_getattr(ino);
EXPECT_CALL(*m_mock, process(
ResultOf([=](auto in) {
return (in->header.opcode == FUSE_SETLK &&
in->header.nodeid == ino &&
in->body.getlk.fh == FH &&
in->body.getlk.owner == (uint32_t)pid &&
in->body.getlk.lk.start == 10 &&
in->body.getlk.lk.end == 1009 &&
in->body.getlk.lk.type == F_RDLCK &&
in->body.getlk.lk.pid == 10);
}, Eq(true)),
_)
).WillOnce(Invoke(ReturnErrno(EAGAIN)));
fd = open(FULLPATH, O_RDWR);
ASSERT_LE(0, fd) << strerror(errno);
fl.l_start = 10;
fl.l_len = 1000;
fl.l_pid = pid;
fl.l_type = F_RDLCK;
fl.l_whence = SEEK_SET;
fl.l_sysid = 0;
ASSERT_EQ(-1, fcntl(fd, F_SETLK, &fl));
ASSERT_EQ(EAGAIN, errno);
/* Deliberately leak fd. close(2) will be tested in release.cc */
}
/*
* If the fuse filesystem does not support posix file locks, then the kernel
* should fall back to local locks.
*/
TEST_F(SetlkwFallback, local)
{
const char FULLPATH[] = "mountpoint/some_file.txt";
const char RELPATH[] = "some_file.txt";
uint64_t ino = 42;
struct flock fl;
int fd;
expect_lookup(RELPATH, ino);
expect_open(ino);
expect_getattr(ino);
fd = open(FULLPATH, O_RDWR);
ASSERT_LE(0, fd) << strerror(errno);
fl.l_start = 10;
fl.l_len = 1000;
fl.l_pid = getpid();
fl.l_type = F_RDLCK;
fl.l_whence = SEEK_SET;
fl.l_sysid = 0;
ASSERT_NE(-1, fcntl(fd, F_SETLKW, &fl)) << strerror(errno);
/* Deliberately leak fd. close(2) will be tested in release.cc */
}
/*
* Set a new lock with FUSE_SETLK. If the lock is not available, then the
* command should block. But to the kernel, that's the same as just being
* slow, so we don't need a separate test method
*/
/* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=234581 */
TEST_F(Setlkw, DISABLED_set)
{
const char FULLPATH[] = "mountpoint/some_file.txt";
const char RELPATH[] = "some_file.txt";
uint64_t ino = 42;
struct flock fl;
int fd;
pid_t pid = 1234;
expect_lookup(RELPATH, ino);
expect_open(ino);
expect_getattr(ino);
EXPECT_CALL(*m_mock, process(
ResultOf([=](auto in) {
return (in->header.opcode == FUSE_SETLK &&
in->header.nodeid == ino &&
in->body.getlk.fh == FH &&
in->body.getlk.owner == (uint32_t)pid &&
in->body.getlk.lk.start == 10 &&
in->body.getlk.lk.end == 1009 &&
in->body.getlk.lk.type == F_RDLCK &&
in->body.getlk.lk.pid == 10);
}, Eq(true)),
_)
).WillOnce(Invoke([=](auto in, auto out) {
out->header.unique = in->header.unique;
SET_OUT_HEADER_LEN(out, getlk);
out->body.getlk.lk = in->body.getlk.lk;
out->body.getlk.lk.type = F_UNLCK;
}));
fd = open(FULLPATH, O_RDWR);
ASSERT_LE(0, fd) << strerror(errno);
fl.l_start = 10;
fl.l_len = 1000;
fl.l_pid = pid;
fl.l_type = F_RDLCK;
fl.l_whence = SEEK_SET;
fl.l_sysid = 0;
ASSERT_NE(-1, fcntl(fd, F_SETLKW, &fl)) << strerror(errno);
/* Deliberately leak fd. close(2) will be tested in release.cc */
}

View File

@ -183,7 +183,7 @@ void debug_fuseop(const mockfs_buf_in *in)
printf("\n");
}
MockFS::MockFS(int max_readahead) {
MockFS::MockFS(int max_readahead, uint32_t flags) {
struct iovec *iov = NULL;
int iovlen = 0;
char fdstr[15];
@ -225,7 +225,7 @@ MockFS::MockFS(int max_readahead) {
ON_CALL(*this, process(_, _))
.WillByDefault(Invoke(this, &MockFS::process_default));
init();
init(flags);
signal(SIGUSR1, sigint_handler);
if (pthread_create(&m_daemon_id, NULL, service, (void*)this))
throw(std::system_error(errno, std::system_category(),
@ -238,7 +238,7 @@ MockFS::~MockFS() {
rmdir("mountpoint");
}
void MockFS::init() {
void MockFS::init(uint32_t flags) {
mockfs_buf_in *in;
mockfs_buf_out *out;
@ -255,6 +255,7 @@ void MockFS::init() {
out->header.error = 0;
out->body.init.major = FUSE_KERNEL_VERSION;
out->body.init.minor = FUSE_KERNEL_MINOR_VERSION;
out->body.init.flags = in->body.init.flags & flags;
/*
* The default max_write is set to this formula in libfuse, though
@ -313,6 +314,10 @@ void MockFS::loop() {
/*Alone among the opcodes, FORGET expects no response*/
continue;
}
if (out.header.error == FUSE_NORESPONSE) {
/* Used by tests of slow opcodes. No response ATM */
continue;
}
ASSERT_TRUE(write(m_fuse_fd, &out, out.header.len) > 0 ||
errno == EAGAIN)
<< strerror(errno);

View File

@ -40,6 +40,12 @@ extern "C" {
#define TIME_T_MAX (std::numeric_limits<time_t>::max())
/*
* A pseudo-fuse errno used indicate that a fuse operation should have no
* response, at least not immediately
*/
#define FUSE_NORESPONSE 9999
#define SET_OUT_HEADER_LEN(out, variant) { \
(out)->header.len = (sizeof((out)->header) + \
sizeof((out)->body.variant)); \
@ -78,6 +84,8 @@ union fuse_payloads_in {
fuse_fsync_in fsync;
fuse_fsync_in fsyncdir;
fuse_forget_in forget;
fuse_interrupt_in interrupt;
fuse_lk_in getlk;
fuse_init_in init;
fuse_link_in link;
char lookup[0];
@ -92,6 +100,7 @@ union fuse_payloads_in {
fuse_rename_in rename;
char rmdir[0];
fuse_setattr_in setattr;
fuse_lk_in setlk;
char unlink[0];
fuse_write_in write;
};
@ -107,8 +116,10 @@ union fuse_payloads_out {
/* The protocol places no limits on the size of bytes */
uint8_t bytes[0x20000];
fuse_entry_out entry;
fuse_lk_out getlk;
fuse_init_out init;
fuse_open_out open;
fuse_lk_out setlk;
fuse_statfs_out statfs;
/*
* The protocol places no limits on the length of the string. This is
@ -165,7 +176,7 @@ class MockFS {
pid_t m_pid;
/* Initialize a session after mounting */
void init();
void init(uint32_t flags);
/* Is pid from a process that might be involved in the test? */
bool pid_ok(pid_t pid);
@ -184,7 +195,7 @@ class MockFS {
uint32_t m_max_write;
/* Create a new mockfs and mount it to a tempdir */
MockFS(int max_readahead);
MockFS(int max_readahead, uint32_t flags);
virtual ~MockFS();
/* Kill the filesystem daemon without unmounting the filesystem */

View File

@ -76,7 +76,7 @@ void FuseTest::SetUp() {
m_maxbcachebuf = val;
try {
m_mock = new MockFS(m_maxreadahead);
m_mock = new MockFS(m_maxreadahead, m_init_flags);
} catch (std::system_error err) {
FAIL() << err.what();
}

View File

@ -31,6 +31,7 @@
class FuseTest : public ::testing::Test {
protected:
uint32_t m_maxreadahead;
uint32_t m_init_flags;
MockFS *m_mock = NULL;
public:
@ -40,8 +41,10 @@ class FuseTest : public ::testing::Test {
* libfuse's default max_readahead is UINT_MAX, though it can be
* lowered
*/
FuseTest(): FuseTest(UINT_MAX) {}
FuseTest(): FuseTest(UINT_MAX, 0) {}
FuseTest(uint32_t maxreadahead): m_maxreadahead(maxreadahead) {}
FuseTest(uint32_t maxreadahead, uint32_t init_flags):
m_maxreadahead(maxreadahead), m_init_flags(init_flags) {}
virtual void SetUp();