fuse: fix flock

Commit a9ff4f87 "fuse: support BSD locking semantics" overlooked a
number of issues with supporing flock locks over existing POSIX
locking infrastructure:

  - it's not backward compatible, passing flock(2) calls to userspace
    unconditionally (if userspace sets FUSE_POSIX_LOCKS)

  - it doesn't cater for the fact that flock locks are automatically
    unlocked on file release

  - it doesn't take into account the fact that flock exclusive locks
    (write locks) don't need an fd opened for write.

The last one invalidates the original premise of the patch that flock
locks can be emulated with POSIX locks.

This patch fixes the first two issues.  The last one needs to be fixed
in userspace if the filesystem assumed that a write lock will happen
only on a file operned for write (as in the case of the current fuse
library).

Reported-by: Sebastian Pipping <webmaster@hartwork.org>
Signed-off-by: Miklos Szeredi <mszeredi@suse.cz>
This commit is contained in:
Miklos Szeredi 2011-08-08 16:08:08 +02:00
parent a2daff6803
commit 37fb3a30b4
4 changed files with 32 additions and 4 deletions

View File

@ -245,6 +245,12 @@ void fuse_release_common(struct file *file, int opcode)
req = ff->reserved_req; req = ff->reserved_req;
fuse_prepare_release(ff, file->f_flags, opcode); fuse_prepare_release(ff, file->f_flags, opcode);
if (ff->flock) {
struct fuse_release_in *inarg = &req->misc.release.in;
inarg->release_flags |= FUSE_RELEASE_FLOCK_UNLOCK;
inarg->lock_owner = fuse_lock_owner_id(ff->fc,
(fl_owner_t) file);
}
/* Hold vfsmount and dentry until release is finished */ /* Hold vfsmount and dentry until release is finished */
path_get(&file->f_path); path_get(&file->f_path);
req->misc.release.path = file->f_path; req->misc.release.path = file->f_path;
@ -1547,11 +1553,14 @@ static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
struct fuse_conn *fc = get_fuse_conn(inode); struct fuse_conn *fc = get_fuse_conn(inode);
int err; int err;
if (fc->no_lock) { if (fc->no_flock) {
err = flock_lock_file_wait(file, fl); err = flock_lock_file_wait(file, fl);
} else { } else {
struct fuse_file *ff = file->private_data;
/* emulate flock with POSIX locks */ /* emulate flock with POSIX locks */
fl->fl_owner = (fl_owner_t) file; fl->fl_owner = (fl_owner_t) file;
ff->flock = true;
err = fuse_setlk(file, fl, 1); err = fuse_setlk(file, fl, 1);
} }

View File

@ -135,6 +135,9 @@ struct fuse_file {
/** Wait queue head for poll */ /** Wait queue head for poll */
wait_queue_head_t poll_wait; wait_queue_head_t poll_wait;
/** Has flock been performed on this file? */
bool flock:1;
}; };
/** One input argument of a request */ /** One input argument of a request */
@ -448,7 +451,7 @@ struct fuse_conn {
/** Is removexattr not implemented by fs? */ /** Is removexattr not implemented by fs? */
unsigned no_removexattr:1; unsigned no_removexattr:1;
/** Are file locking primitives not implemented by fs? */ /** Are posix file locking primitives not implemented by fs? */
unsigned no_lock:1; unsigned no_lock:1;
/** Is access not implemented by fs? */ /** Is access not implemented by fs? */
@ -472,6 +475,9 @@ struct fuse_conn {
/** Don't apply umask to creation modes */ /** Don't apply umask to creation modes */
unsigned dont_mask:1; unsigned dont_mask:1;
/** Are BSD file locking primitives not implemented by fs? */
unsigned no_flock:1;
/** The number of requests waiting for completion */ /** The number of requests waiting for completion */
atomic_t num_waiting; atomic_t num_waiting;

View File

@ -809,6 +809,10 @@ static void process_init_reply(struct fuse_conn *fc, struct fuse_req *req)
fc->async_read = 1; fc->async_read = 1;
if (!(arg->flags & FUSE_POSIX_LOCKS)) if (!(arg->flags & FUSE_POSIX_LOCKS))
fc->no_lock = 1; fc->no_lock = 1;
if (arg->minor >= 17) {
if (!(arg->flags & FUSE_FLOCK_LOCKS))
fc->no_flock = 1;
}
if (arg->flags & FUSE_ATOMIC_O_TRUNC) if (arg->flags & FUSE_ATOMIC_O_TRUNC)
fc->atomic_o_trunc = 1; fc->atomic_o_trunc = 1;
if (arg->minor >= 9) { if (arg->minor >= 9) {
@ -823,6 +827,7 @@ static void process_init_reply(struct fuse_conn *fc, struct fuse_req *req)
} else { } else {
ra_pages = fc->max_read / PAGE_CACHE_SIZE; ra_pages = fc->max_read / PAGE_CACHE_SIZE;
fc->no_lock = 1; fc->no_lock = 1;
fc->no_flock = 1;
} }
fc->bdi.ra_pages = min(fc->bdi.ra_pages, ra_pages); fc->bdi.ra_pages = min(fc->bdi.ra_pages, ra_pages);
@ -843,7 +848,8 @@ static void fuse_send_init(struct fuse_conn *fc, struct fuse_req *req)
arg->minor = FUSE_KERNEL_MINOR_VERSION; arg->minor = FUSE_KERNEL_MINOR_VERSION;
arg->max_readahead = fc->bdi.ra_pages * PAGE_CACHE_SIZE; arg->max_readahead = fc->bdi.ra_pages * PAGE_CACHE_SIZE;
arg->flags |= FUSE_ASYNC_READ | FUSE_POSIX_LOCKS | FUSE_ATOMIC_O_TRUNC | arg->flags |= FUSE_ASYNC_READ | FUSE_POSIX_LOCKS | FUSE_ATOMIC_O_TRUNC |
FUSE_EXPORT_SUPPORT | FUSE_BIG_WRITES | FUSE_DONT_MASK; FUSE_EXPORT_SUPPORT | FUSE_BIG_WRITES | FUSE_DONT_MASK |
FUSE_FLOCK_LOCKS;
req->in.h.opcode = FUSE_INIT; req->in.h.opcode = FUSE_INIT;
req->in.numargs = 1; req->in.numargs = 1;
req->in.args[0].size = sizeof(*arg); req->in.args[0].size = sizeof(*arg);

View File

@ -47,6 +47,9 @@
* - FUSE_IOCTL_UNRESTRICTED shall now return with array of 'struct * - FUSE_IOCTL_UNRESTRICTED shall now return with array of 'struct
* fuse_ioctl_iovec' instead of ambiguous 'struct iovec' * fuse_ioctl_iovec' instead of ambiguous 'struct iovec'
* - add FUSE_IOCTL_32BIT flag * - add FUSE_IOCTL_32BIT flag
*
* 7.17
* - add FUSE_FLOCK_LOCKS and FUSE_RELEASE_FLOCK_UNLOCK
*/ */
#ifndef _LINUX_FUSE_H #ifndef _LINUX_FUSE_H
@ -78,7 +81,7 @@
#define FUSE_KERNEL_VERSION 7 #define FUSE_KERNEL_VERSION 7
/** Minor version number of this interface */ /** Minor version number of this interface */
#define FUSE_KERNEL_MINOR_VERSION 16 #define FUSE_KERNEL_MINOR_VERSION 17
/** The node ID of the root inode */ /** The node ID of the root inode */
#define FUSE_ROOT_ID 1 #define FUSE_ROOT_ID 1
@ -153,8 +156,10 @@ struct fuse_file_lock {
/** /**
* INIT request/reply flags * INIT request/reply flags
* *
* FUSE_POSIX_LOCKS: remote locking for POSIX file locks
* FUSE_EXPORT_SUPPORT: filesystem handles lookups of "." and ".." * FUSE_EXPORT_SUPPORT: filesystem handles lookups of "." and ".."
* FUSE_DONT_MASK: don't apply umask to file mode on create operations * FUSE_DONT_MASK: don't apply umask to file mode on create operations
* FUSE_FLOCK_LOCKS: remote locking for BSD style file locks
*/ */
#define FUSE_ASYNC_READ (1 << 0) #define FUSE_ASYNC_READ (1 << 0)
#define FUSE_POSIX_LOCKS (1 << 1) #define FUSE_POSIX_LOCKS (1 << 1)
@ -163,6 +168,7 @@ struct fuse_file_lock {
#define FUSE_EXPORT_SUPPORT (1 << 4) #define FUSE_EXPORT_SUPPORT (1 << 4)
#define FUSE_BIG_WRITES (1 << 5) #define FUSE_BIG_WRITES (1 << 5)
#define FUSE_DONT_MASK (1 << 6) #define FUSE_DONT_MASK (1 << 6)
#define FUSE_FLOCK_LOCKS (1 << 10)
/** /**
* CUSE INIT request/reply flags * CUSE INIT request/reply flags
@ -175,6 +181,7 @@ struct fuse_file_lock {
* Release flags * Release flags
*/ */
#define FUSE_RELEASE_FLUSH (1 << 0) #define FUSE_RELEASE_FLUSH (1 << 0)
#define FUSE_RELEASE_FLOCK_UNLOCK (1 << 1)
/** /**
* Getattr flags * Getattr flags