1
0
mirror of https://git.FreeBSD.org/src.git synced 2024-12-18 10:35:55 +00:00

fusefs: implement VOP_ADVLOCK

PR:		234581
Sponsored by:	The FreeBSD Foundation
This commit is contained in:
Alan Somers 2019-04-12 23:22:27 +00:00
parent 6af6fdcea7
commit f067b60946
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/projects/fuse2/; revision=346170
11 changed files with 179 additions and 55 deletions

View File

@ -217,6 +217,12 @@ fuse_filehandle_close(struct vnode *vp, struct fuse_filehandle *fufh,
fri = fdi.indata;
fri->fh = fufh->fh_id;
fri->flags = fufh_type_2_fflags(fufh->fufh_type);
/*
* If the file has a POSIX lock then we're supposed to set lock_owner.
* If not, then lock_owner is undefined. So we may as well always set
* it.
*/
fri->lock_owner = td->td_proc->p_pid;
err = fdisp_wait_answ(&fdi);
fdisp_destroy(&fdi);
@ -307,6 +313,37 @@ fuse_filehandle_get(struct vnode *vp, int fflag,
return 0;
}
/* Get a file handle with any kind of flags */
int
fuse_filehandle_get_anyflags(struct vnode *vp,
struct fuse_filehandle **fufhp, struct ucred *cred, pid_t pid)
{
struct fuse_vnode_data *fvdat = VTOFUD(vp);
struct fuse_filehandle *fufh;
if (cred == NULL)
goto fallback;
LIST_FOREACH(fufh, &fvdat->handles, next) {
if (fufh->uid == cred->cr_uid &&
fufh->gid == cred->cr_rgid &&
(pid == 0 || fufh->pid == pid))
goto found;
}
fallback:
/* Fallback: find any list entry */
fufh = LIST_FIRST(&fvdat->handles);
if (fufh == NULL)
return EBADF;
found:
if (fufhp != NULL)
*fufhp = fufh;
return 0;
}
int
fuse_filehandle_getrw(struct vnode *vp, int fflag,
struct fuse_filehandle **fufhp, struct ucred *cred, pid_t pid)

View File

@ -153,6 +153,9 @@ bool fuse_filehandle_validrw(struct vnode *vp, int mode,
int fuse_filehandle_get(struct vnode *vp, int fflag,
struct fuse_filehandle **fufhp, struct ucred *cred,
pid_t pid);
int fuse_filehandle_get_anyflags(struct vnode *vp,
struct fuse_filehandle **fufhp, struct ucred *cred,
pid_t pid);
int fuse_filehandle_getrw(struct vnode *vp, int fflag,
struct fuse_filehandle **fufhp, struct ucred *cred,
pid_t pid);

View File

@ -694,6 +694,8 @@ fuse_internal_init_callback(struct fuse_ticket *tick, struct uio *uio)
if (fuse_libabi_geq(data, 7, 5)) {
if (fticket_resp(tick)->len == sizeof(struct fuse_init_out)) {
data->max_write = fiio->max_write;
if (fiio->flags & FUSE_POSIX_LOCKS)
data->dataflags |= FSESS_POSIX_LOCKS;
} else {
err = EINVAL;
}
@ -731,7 +733,7 @@ fuse_internal_send_init(struct fuse_data *data, struct thread *td)
* the size of a buffer cache block.
*/
fiii->max_readahead = maxbcachebuf;
fiii->flags = 0;
fiii->flags = FUSE_POSIX_LOCKS;
fuse_insert_callback(fdi.tick, fuse_internal_init_callback);
fuse_insert_message(fdi.tick);

View File

@ -668,15 +668,15 @@ fuse_body_audit(struct fuse_ticket *ftick, size_t blen)
break;
case FUSE_GETLK:
panic("FUSE: no response body format check for FUSE_GETLK");
err = (blen == sizeof(struct fuse_lk_out)) ? 0 : EINVAL;
break;
case FUSE_SETLK:
panic("FUSE: no response body format check for FUSE_SETLK");
err = (blen == 0) ? 0 : EINVAL;
break;
case FUSE_SETLKW:
panic("FUSE: no response body format check for FUSE_SETLKW");
err = (blen == 0) ? 0 : EINVAL;
break;
case FUSE_ACCESS:

View File

@ -205,6 +205,7 @@ struct fuse_data {
#define FSESS_NO_NAMECACHE 0x0400 /* disable name cache */
#define FSESS_NO_MMAP 0x0800 /* disable mmap */
#define FSESS_BROKENIO 0x1000 /* fix broken io */
#define FSESS_POSIX_LOCKS 0x2000 /* daemon supports POSIX locks */
enum fuse_data_cache_mode {
FUSE_CACHE_UC,

View File

@ -119,6 +119,7 @@ SDT_PROBE_DEFINE2(fuse, , vnops, trace, "int", "char*");
/* vnode ops */
static vop_access_t fuse_vnop_access;
static vop_advlock_t fuse_vnop_advlock;
static vop_close_t fuse_vnop_close;
static vop_create_t fuse_vnop_create;
static vop_deleteextattr_t fuse_vnop_deleteextattr;
@ -153,6 +154,7 @@ static vop_print_t fuse_vnop_print;
struct vop_vector fuse_vnops = {
.vop_default = &default_vnodeops,
.vop_access = fuse_vnop_access,
.vop_advlock = fuse_vnop_advlock,
.vop_close = fuse_vnop_close,
.vop_create = fuse_vnop_create,
.vop_deleteextattr = fuse_vnop_deleteextattr,
@ -266,7 +268,7 @@ fuse_flush(struct vnode *vp, struct ucred *cred, pid_t pid, int fflag)
if (!fsess_isimpl(vnode_mount(vp), FUSE_FLUSH))
return 0;
err = fuse_filehandle_get(vp, fflag, &fufh, cred, pid);
err = fuse_filehandle_getrw(vp, fflag, &fufh, cred, pid);
if (err)
return err;
@ -274,6 +276,12 @@ fuse_flush(struct vnode *vp, struct ucred *cred, pid_t pid, int fflag)
fdisp_make_vp(&fdi, FUSE_FLUSH, vp, td, cred);
ffi = fdi.indata;
ffi->fh = fufh->fh_id;
/*
* If the file has a POSIX lock then we're supposed to set lock_owner.
* If not, then lock_owner is undefined. So we may as well always set
* it.
*/
ffi->lock_owner = td->td_proc->p_pid;
err = fdisp_wait_answ(&fdi);
if (err == ENOSYS) {
@ -330,6 +338,92 @@ fuse_vnop_access(struct vop_access_args *ap)
return err;
}
/*
* struct vop_advlock_args {
* struct vop_generic_args a_gen;
* struct vnode *a_vp;
* void *a_id;
* int a_op;
* struct flock *a_fl;
* int a_flags;
* }
*/
static int
fuse_vnop_advlock(struct vop_advlock_args *ap)
{
struct vnode *vp = ap->a_vp;
struct flock *fl = ap->a_fl;
struct thread *td = curthread;
struct ucred *cred = td->td_ucred;
pid_t pid = td->td_proc->p_pid;
struct fuse_filehandle *fufh;
struct fuse_dispatcher fdi;
struct fuse_lk_in *fli;
struct fuse_lk_out *flo;
enum fuse_opcode op;
int dataflags, err;
dataflags = fuse_get_mpdata(vnode_mount(vp))->dataflags;
if (fuse_isdeadfs(vp)) {
return ENXIO;
}
if (!(dataflags & FSESS_POSIX_LOCKS))
return vop_stdadvlock(ap);
err = fuse_filehandle_get_anyflags(vp, &fufh, cred, pid);
if (err)
return err;
fdisp_init(&fdi, sizeof(*fli));
switch(ap->a_op) {
case F_GETLK:
op = FUSE_GETLK;
break;
case F_SETLK:
op = FUSE_SETLK;
break;
case F_SETLKW:
op = FUSE_SETLKW;
break;
default:
return EINVAL;
}
fdisp_make_vp(&fdi, op, vp, td, cred);
fli = fdi.indata;
fli->fh = fufh->fh_id;
fli->owner = fl->l_pid;
fli->lk.start = fl->l_start;
if (fl->l_len != 0)
fli->lk.end = fl->l_start + fl->l_len - 1;
else
fli->lk.end = INT64_MAX;
fli->lk.type = fl->l_type;
fli->lk.pid = fl->l_pid;
err = fdisp_wait_answ(&fdi);
fdisp_destroy(&fdi);
if (err == 0 && op == FUSE_GETLK) {
flo = fdi.answ;
fl->l_type = flo->lk.type;
fl->l_pid = flo->lk.pid;
if (flo->lk.type != F_UNLCK) {
fl->l_start = flo->lk.start;
if (flo->lk.end == INT64_MAX)
fl->l_len = 0;
else
fl->l_len = flo->lk.end - flo->lk.start + 1;
fl->l_start = flo->lk.start;
}
}
return err;
}
/*
struct vop_close_args {
struct vnode *a_vp;

View File

@ -96,7 +96,7 @@ TEST_F(Flush, open_twice)
expect_lookup(RELPATH, ino, 2);
expect_open(ino, 0, 1);
expect_flush(ino, 2, 0, ReturnErrno(0));
expect_flush(ino, 2, getpid(), ReturnErrno(0));
expect_release();
fd = open(FULLPATH, O_WRONLY);
@ -126,7 +126,7 @@ TEST_F(Flush, eio)
expect_lookup(RELPATH, ino, 1);
expect_open(ino, 0, 1);
expect_flush(ino, 1, 0, ReturnErrno(EIO));
expect_flush(ino, 1, getpid(), ReturnErrno(EIO));
expect_release();
fd = open(FULLPATH, O_WRONLY);
@ -152,7 +152,7 @@ TEST_F(Flush, enosys)
expect_lookup(RELPATH0, ino0, 1);
expect_open(ino0, 0, 1);
/* On the 2nd close, FUSE_FLUSH won't be sent at all */
expect_flush(ino0, 1, 0, ReturnErrno(ENOSYS));
expect_flush(ino0, 1, getpid(), ReturnErrno(ENOSYS));
expect_release();
expect_lookup(RELPATH1, ino1, 1);
@ -180,7 +180,7 @@ TEST_F(Flush, flush)
expect_lookup(RELPATH, ino, 1);
expect_open(ino, 0, 1);
expect_flush(ino, 1, 0, ReturnErrno(0));
expect_flush(ino, 1, getpid(), ReturnErrno(0));
expect_release();
fd = open(FULLPATH, O_WRONLY);
@ -193,8 +193,7 @@ TEST_F(Flush, flush)
* When closing a file with a POSIX file lock, flush should release the lock,
* _even_if_ it's not the process's last file descriptor for this file.
*/
/* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=234581 */
TEST_F(FlushWithLocks, DISABLED_unlock_on_close)
TEST_F(FlushWithLocks, unlock_on_close)
{
const char FULLPATH[] = "mountpoint/some_file.txt";
const char RELPATH[] = "some_file.txt";
@ -203,7 +202,7 @@ TEST_F(FlushWithLocks, DISABLED_unlock_on_close)
struct flock fl;
pid_t pid = getpid();
expect_lookup(RELPATH, ino, 1);
expect_lookup(RELPATH, ino, 2);
expect_open(ino, 0, 1);
EXPECT_CALL(*m_mock, process(
ResultOf([=](auto in) {
@ -212,10 +211,7 @@ TEST_F(FlushWithLocks, DISABLED_unlock_on_close)
in->body.setlk.fh == FH);
}, Eq(true)),
_)
).WillOnce(Invoke(ReturnImmediate([=](auto in, auto out) {
SET_OUT_HEADER_LEN(out, setlk);
out->body.setlk.lk = in->body.setlk.lk;
})));
).WillOnce(Invoke(ReturnErrno(0)));
expect_flush(ino, 1, pid, ReturnErrno(0));
fd = open(FULLPATH, O_RDWR);
@ -228,7 +224,8 @@ TEST_F(FlushWithLocks, DISABLED_unlock_on_close)
fl.l_sysid = 0;
ASSERT_NE(-1, fcntl(fd, F_SETLKW, &fl)) << strerror(errno);
fd2 = dup(fd);
fd2 = open(FULLPATH, O_WRONLY);
ASSERT_LE(0, fd2) << strerror(errno);
ASSERT_EQ(0, close(fd2)) << strerror(errno);
/* Deliberately leak fd */
}

View File

@ -97,8 +97,7 @@ TEST_F(GetlkFallback, local)
* 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)
TEST_F(Getlk, no_locks)
{
const char FULLPATH[] = "mountpoint/some_file.txt";
const char RELPATH[] = "some_file.txt";
@ -141,8 +140,7 @@ TEST_F(Getlk, DISABLED_no_locks)
}
/* A different pid does have a lock */
/* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=234581 */
TEST_F(Getlk, DISABLED_lock_exists)
TEST_F(Getlk, lock_exists)
{
const char FULLPATH[] = "mountpoint/some_file.txt";
const char RELPATH[] = "some_file.txt";
@ -220,8 +218,7 @@ TEST_F(SetlkFallback, local)
}
/* Set a new lock with FUSE_SETLK */
/* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=234581 */
TEST_F(Setlk, DISABLED_set)
TEST_F(Setlk, set)
{
const char FULLPATH[] = "mountpoint/some_file.txt";
const char RELPATH[] = "some_file.txt";
@ -244,10 +241,7 @@ TEST_F(Setlk, DISABLED_set)
in->body.setlk.lk.pid == (uint64_t)pid);
}, Eq(true)),
_)
).WillOnce(Invoke(ReturnImmediate([=](auto in, auto out) {
SET_OUT_HEADER_LEN(out, setlk);
out->body.setlk.lk = in->body.setlk.lk;
})));
).WillOnce(Invoke(ReturnErrno(0)));
fd = open(FULLPATH, O_RDWR);
ASSERT_LE(0, fd) << strerror(errno);
@ -262,8 +256,7 @@ TEST_F(Setlk, DISABLED_set)
}
/* 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)
TEST_F(Setlk, set_eof)
{
const char FULLPATH[] = "mountpoint/some_file.txt";
const char RELPATH[] = "some_file.txt";
@ -286,10 +279,7 @@ TEST_F(Setlk, DISABLED_set_eof)
in->body.setlk.lk.pid == (uint64_t)pid);
}, Eq(true)),
_)
).WillOnce(Invoke(ReturnImmediate([=](auto in, auto out) {
SET_OUT_HEADER_LEN(out, setlk);
out->body.setlk.lk = in->body.setlk.lk;
})));
).WillOnce(Invoke(ReturnErrno(0)));
fd = open(FULLPATH, O_RDWR);
ASSERT_LE(0, fd) << strerror(errno);
@ -304,8 +294,7 @@ TEST_F(Setlk, DISABLED_set_eof)
}
/* 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)
TEST_F(Setlk, eagain)
{
const char FULLPATH[] = "mountpoint/some_file.txt";
const char RELPATH[] = "some_file.txt";
@ -375,8 +364,7 @@ TEST_F(SetlkwFallback, local)
* 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)
TEST_F(Setlkw, set)
{
const char FULLPATH[] = "mountpoint/some_file.txt";
const char RELPATH[] = "some_file.txt";
@ -399,10 +387,7 @@ TEST_F(Setlkw, DISABLED_set)
in->body.setlkw.lk.pid == (uint64_t)pid);
}, Eq(true)),
_)
).WillOnce(Invoke(ReturnImmediate([=](auto in, auto out) {
SET_OUT_HEADER_LEN(out, setlkw);
out->body.setlkw.lk = in->body.setlkw.lk;
})));
).WillOnce(Invoke(ReturnErrno(0)));
fd = open(FULLPATH, O_RDWR);
ASSERT_LE(0, fd) << strerror(errno);

View File

@ -238,6 +238,17 @@ void debug_fuseop(const mockfs_buf_in *in)
if (in->body.setattr.valid & FATTR_FH)
printf(" fh=%zu", in->body.setattr.fh);
break;
case FUSE_SETLK:
printf(" fh=%#lx owner=%lu type=%u pid=%u",
in->body.setlk.fh, in->body.setlk.owner,
in->body.setlk.lk.type,
in->body.setlk.lk.pid);
if (verbosity >= 2) {
printf(" range=[%lu-%lu]",
in->body.setlk.lk.start,
in->body.setlk.lk.end);
}
break;
case FUSE_SETXATTR:
/*
* In theory neither the xattr name and value need be

View File

@ -125,8 +125,6 @@ union fuse_payloads_out {
fuse_init_out init;
fuse_listxattr_out listxattr;
fuse_open_out open;
fuse_lk_out setlk;
fuse_lk_out setlkw;
fuse_statfs_out statfs;
/*
* The protocol places no limits on the length of the string. This is

View File

@ -82,7 +82,7 @@ TEST_F(Release, dup)
expect_lookup(RELPATH, ino, 1);
expect_open(ino, 0, 1);
expect_flush(ino, 1, ReturnErrno(0));
expect_release(ino, 0, O_RDONLY, 0);
expect_release(ino, getpid(), O_RDONLY, 0);
fd = open(FULLPATH, O_RDONLY);
EXPECT_LE(0, fd) << strerror(errno);
@ -111,7 +111,7 @@ TEST_F(Release, eio)
expect_lookup(RELPATH, ino, 1);
expect_open(ino, 0, 1);
expect_flush(ino, 1, ReturnErrno(0));
expect_release(ino, 0, O_WRONLY, EIO);
expect_release(ino, getpid(), O_WRONLY, EIO);
fd = open(FULLPATH, O_WRONLY);
EXPECT_LE(0, fd) << strerror(errno);
@ -133,7 +133,7 @@ TEST_F(Release, DISABLED_flags)
expect_lookup(RELPATH, ino, 1);
expect_open(ino, 0, 1);
expect_flush(ino, 1, ReturnErrno(0));
expect_release(ino, 0, O_RDWR | O_APPEND, 0);
expect_release(ino, getpid(), O_RDWR | O_APPEND, 0);
fd = open(FULLPATH, O_RDWR | O_APPEND);
EXPECT_LE(0, fd) << strerror(errno);
@ -156,12 +156,12 @@ TEST_F(Release, multiple_opens)
expect_lookup(RELPATH, ino, 2);
expect_open(ino, 0, 2);
expect_flush(ino, 2, ReturnErrno(0));
expect_release(ino, 0, O_RDONLY, 0);
expect_release(ino, getpid(), O_RDONLY, 0);
fd = open(FULLPATH, O_RDONLY);
EXPECT_LE(0, fd) << strerror(errno);
expect_release(ino, 0, O_WRONLY, 0);
expect_release(ino, getpid(), O_WRONLY, 0);
fd2 = open(FULLPATH, O_WRONLY);
EXPECT_LE(0, fd2) << strerror(errno);
@ -179,7 +179,7 @@ TEST_F(Release, ok)
expect_lookup(RELPATH, ino, 1);
expect_open(ino, 0, 1);
expect_flush(ino, 1, ReturnErrno(0));
expect_release(ino, 0, O_RDONLY, 0);
expect_release(ino, getpid(), O_RDONLY, 0);
fd = open(FULLPATH, O_RDONLY);
EXPECT_LE(0, fd) << strerror(errno);
@ -188,8 +188,7 @@ TEST_F(Release, ok)
}
/* When closing a file with a POSIX file lock, release should release the lock*/
/* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=234581 */
TEST_F(ReleaseWithLocks, DISABLED_unlock_on_close)
TEST_F(ReleaseWithLocks, unlock_on_close)
{
const char FULLPATH[] = "mountpoint/some_file.txt";
const char RELPATH[] = "some_file.txt";
@ -207,10 +206,7 @@ TEST_F(ReleaseWithLocks, DISABLED_unlock_on_close)
in->body.setlk.fh == FH);
}, Eq(true)),
_)
).WillOnce(Invoke(ReturnImmediate([=](auto in, auto out) {
SET_OUT_HEADER_LEN(out, setlk);
out->body.setlk.lk = in->body.setlk.lk;
})));
).WillOnce(Invoke(ReturnErrno(0)));
expect_flush(ino, 1, ReturnErrno(0));
expect_release(ino, (uint64_t)pid, O_RDWR, 0);