From e8225e8e8716f653ac72e5c7cb8c467a12b1be9f Mon Sep 17 00:00:00 2001 From: Miklos Szeredi Date: Tue, 14 Aug 2018 21:37:02 +0200 Subject: [PATCH] passthrough_ll: add flock() Conditionally enable flock() locking on underlying filesystem, based on the flock/no_flock options. Default is "no_flock", meaning locking will be local to the fuse filesystem and won't be propagated to the filesystem passed through. Signed-off-by: Miklos Szeredi --- example/passthrough_ll.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/example/passthrough_ll.c b/example/passthrough_ll.c index ad1185b..ba0f4af 100644 --- a/example/passthrough_ll.c +++ b/example/passthrough_ll.c @@ -51,6 +51,7 @@ #include #include #include +#include /* We are re-using pointers to our `struct lo_inode` and `struct lo_dirp` elements as inodes. This means that we must be able to @@ -79,6 +80,7 @@ struct lo_data { pthread_mutex_t mutex; int debug; int writeback; + int flock; const char *source; struct lo_inode root; /* protected by lo->mutex */ }; @@ -90,6 +92,10 @@ static const struct fuse_opt lo_opts[] = { offsetof(struct lo_data, writeback), 0 }, { "source=%s", offsetof(struct lo_data, source), 0 }, + { "flock", + offsetof(struct lo_data, flock), 1 }, + { "no_flock", + offsetof(struct lo_data, flock), 0 }, FUSE_OPT_END }; @@ -131,6 +137,11 @@ static void lo_init(void *userdata, fprintf(stderr, "lo_init: activating writeback\n"); conn->want |= FUSE_CAP_WRITEBACK_CACHE; } + if (lo->flock && conn->capable & FUSE_CAP_FLOCK_LOCKS) { + if (lo->debug) + fprintf(stderr, "lo_init: activating flock locks\n"); + conn->want |= FUSE_CAP_FLOCK_LOCKS; + } } static void lo_getattr(fuse_req_t req, fuse_ino_t ino, @@ -888,6 +899,17 @@ static void lo_fallocate(fuse_req_t req, fuse_ino_t ino, int mode, fuse_reply_err(req, err); } +static void lo_flock(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi, + int op) +{ + int res; + (void) ino; + + res = flock(fi->fh, op); + + fuse_reply_err(req, res == -1 ? errno : 0); +} + static struct fuse_lowlevel_ops lo_oper = { .init = lo_init, .lookup = lo_lookup, @@ -917,6 +939,7 @@ static struct fuse_lowlevel_ops lo_oper = { .write_buf = lo_write_buf, .statfs = lo_statfs, .fallocate = lo_fallocate, + .flock = lo_flock, }; int main(int argc, char *argv[]) -- 2.30.2