Skip to content

Commit 37fb3a3

Browse files
author
Miklos Szeredi
committed
fuse: fix flock
Commit a9ff4f8 "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>
1 parent a2daff6 commit 37fb3a3

4 files changed

Lines changed: 32 additions & 4 deletions

File tree

‎fs/fuse/file.c‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,12 @@ void fuse_release_common(struct file *file, int opcode)
245245
req = ff->reserved_req;
246246
fuse_prepare_release(ff, file->f_flags, opcode);
247247

248+
if (ff->flock) {
249+
struct fuse_release_in *inarg = &req->misc.release.in;
250+
inarg->release_flags |= FUSE_RELEASE_FLOCK_UNLOCK;
251+
inarg->lock_owner = fuse_lock_owner_id(ff->fc,
252+
(fl_owner_t) file);
253+
}
248254
/* Hold vfsmount and dentry until release is finished */
249255
path_get(&file->f_path);
250256
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)
15471553
struct fuse_conn *fc = get_fuse_conn(inode);
15481554
int err;
15491555

1550-
if (fc->no_lock) {
1556+
if (fc->no_flock) {
15511557
err = flock_lock_file_wait(file, fl);
15521558
} else {
1559+
struct fuse_file *ff = file->private_data;
1560+
15531561
/* emulate flock with POSIX locks */
15541562
fl->fl_owner = (fl_owner_t) file;
1563+
ff->flock = true;
15551564
err = fuse_setlk(file, fl, 1);
15561565
}
15571566

‎fs/fuse/fuse_i.h‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ struct fuse_file {
135135

136136
/** Wait queue head for poll */
137137
wait_queue_head_t poll_wait;
138+
139+
/** Has flock been performed on this file? */
140+
bool flock:1;
138141
};
139142

140143
/** One input argument of a request */
@@ -448,7 +451,7 @@ struct fuse_conn {
448451
/** Is removexattr not implemented by fs? */
449452
unsigned no_removexattr:1;
450453

451-
/** Are file locking primitives not implemented by fs? */
454+
/** Are posix file locking primitives not implemented by fs? */
452455
unsigned no_lock:1;
453456

454457
/** Is access not implemented by fs? */
@@ -472,6 +475,9 @@ struct fuse_conn {
472475
/** Don't apply umask to creation modes */
473476
unsigned dont_mask:1;
474477

478+
/** Are BSD file locking primitives not implemented by fs? */
479+
unsigned no_flock:1;
480+
475481
/** The number of requests waiting for completion */
476482
atomic_t num_waiting;
477483

‎fs/fuse/inode.c‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,10 @@ static void process_init_reply(struct fuse_conn *fc, struct fuse_req *req)
809809
fc->async_read = 1;
810810
if (!(arg->flags & FUSE_POSIX_LOCKS))
811811
fc->no_lock = 1;
812+
if (arg->minor >= 17) {
813+
if (!(arg->flags & FUSE_FLOCK_LOCKS))
814+
fc->no_flock = 1;
815+
}
812816
if (arg->flags & FUSE_ATOMIC_O_TRUNC)
813817
fc->atomic_o_trunc = 1;
814818
if (arg->minor >= 9) {
@@ -823,6 +827,7 @@ static void process_init_reply(struct fuse_conn *fc, struct fuse_req *req)
823827
} else {
824828
ra_pages = fc->max_read / PAGE_CACHE_SIZE;
825829
fc->no_lock = 1;
830+
fc->no_flock = 1;
826831
}
827832

828833
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)
843848
arg->minor = FUSE_KERNEL_MINOR_VERSION;
844849
arg->max_readahead = fc->bdi.ra_pages * PAGE_CACHE_SIZE;
845850
arg->flags |= FUSE_ASYNC_READ | FUSE_POSIX_LOCKS | FUSE_ATOMIC_O_TRUNC |
846-
FUSE_EXPORT_SUPPORT | FUSE_BIG_WRITES | FUSE_DONT_MASK;
851+
FUSE_EXPORT_SUPPORT | FUSE_BIG_WRITES | FUSE_DONT_MASK |
852+
FUSE_FLOCK_LOCKS;
847853
req->in.h.opcode = FUSE_INIT;
848854
req->in.numargs = 1;
849855
req->in.args[0].size = sizeof(*arg);

‎include/linux/fuse.h‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@
4747
* - FUSE_IOCTL_UNRESTRICTED shall now return with array of 'struct
4848
* fuse_ioctl_iovec' instead of ambiguous 'struct iovec'
4949
* - add FUSE_IOCTL_32BIT flag
50+
*
51+
* 7.17
52+
* - add FUSE_FLOCK_LOCKS and FUSE_RELEASE_FLOCK_UNLOCK
5053
*/
5154

5255
#ifndef _LINUX_FUSE_H
@@ -78,7 +81,7 @@
7881
#define FUSE_KERNEL_VERSION 7
7982

8083
/** Minor version number of this interface */
81-
#define FUSE_KERNEL_MINOR_VERSION 16
84+
#define FUSE_KERNEL_MINOR_VERSION 17
8285

8386
/** The node ID of the root inode */
8487
#define FUSE_ROOT_ID 1
@@ -153,8 +156,10 @@ struct fuse_file_lock {
153156
/**
154157
* INIT request/reply flags
155158
*
159+
* FUSE_POSIX_LOCKS: remote locking for POSIX file locks
156160
* FUSE_EXPORT_SUPPORT: filesystem handles lookups of "." and ".."
157161
* FUSE_DONT_MASK: don't apply umask to file mode on create operations
162+
* FUSE_FLOCK_LOCKS: remote locking for BSD style file locks
158163
*/
159164
#define FUSE_ASYNC_READ (1 << 0)
160165
#define FUSE_POSIX_LOCKS (1 << 1)
@@ -163,6 +168,7 @@ struct fuse_file_lock {
163168
#define FUSE_EXPORT_SUPPORT (1 << 4)
164169
#define FUSE_BIG_WRITES (1 << 5)
165170
#define FUSE_DONT_MASK (1 << 6)
171+
#define FUSE_FLOCK_LOCKS (1 << 10)
166172

167173
/**
168174
* CUSE INIT request/reply flags
@@ -175,6 +181,7 @@ struct fuse_file_lock {
175181
* Release flags
176182
*/
177183
#define FUSE_RELEASE_FLUSH (1 << 0)
184+
#define FUSE_RELEASE_FLOCK_UNLOCK (1 << 1)
178185

179186
/**
180187
* Getattr flags

0 commit comments

Comments
 (0)