Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 77 additions & 3 deletions fs/fuse/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,64 @@ static void fuse_sync_writes(struct inode *inode)
fuse_release_nowrite(inode);
}

static int fuse_flush(struct file *file, fl_owner_t id)
/* Drop what the server holds for the owner that is @file itself, and wait. */
static void fuse_unlock_file_owner(struct file *file, u32 lk_flags)
{
struct inode *inode = file_inode(file);
struct fuse_mount *fm = get_fuse_mount(inode);
struct fuse_file *ff = file->private_data;
struct fuse_lk_in inarg;
FUSE_ARGS(args);

memset(&inarg, 0, sizeof(inarg));
inarg.fh = ff->fh;
inarg.owner = fuse_lock_owner_id(fm->fc, file);
inarg.lk.end = OFFSET_MAX;
inarg.lk.type = F_UNLCK;
inarg.lk_flags = lk_flags;
args.opcode = FUSE_SETLK;
args.nodeid = get_node_id(inode);
args.in_numargs = 1;
args.in_args[0].size = sizeof(inarg);
args.in_args[0].value = &inarg;
args.force = true;
fuse_simple_request(fm, &args);
}

/*
* The server learns that an open file description is gone from RELEASE, and
* drops the description's OFD locks there, and its flock() lock when RELEASE
* carries FUSE_RELEASE_FLOCK_UNLOCK. RELEASE is sent in the background
* after close() has returned, so for a moment another process still finds a
* lock that is gone as far as the closing process can tell (generic/478).
* RELEASE cannot be made synchronous: the final fput() can run on a thread of
* the server.
*
* ->flush() runs in the closing task, before that fput(). If it holds the
* only reference to the file, the descriptor it was called for was the last
* one: the descriptor is out of the table already, and a mapping, an I/O in
* flight or a descriptor in transit would each hold a reference of its own.
* Nothing can take a new reference then, so the locks of the description are
* dropped here, with a request the caller waits for.
*
* A description that loses its last reference some other way (munmap() after
* close(), completion of an aio) is unlocked by RELEASE, as before. RELEASE
* still asks for the flock() unlock; after this one it finds nothing to drop.
*/
static void fuse_unlock_on_last_close(struct file *file)
{
struct fuse_file *ff = file->private_data;

if (file_count(file) != 1 || fuse_is_bad(file_inode(file)))
return;

if (READ_ONCE(ff->ofd_lock))
fuse_unlock_file_owner(file, 0);
if (ff->flock)
fuse_unlock_file_owner(file, FUSE_LK_FLOCK);
}

static int fuse_flush_data(struct file *file, fl_owner_t id)
{
struct inode *inode = file_inode(file);
struct fuse_mount *fm = get_fuse_mount(inode);
Expand Down Expand Up @@ -665,6 +722,15 @@ static int fuse_flush(struct file *file, fl_owner_t id)
return err;
}

static int fuse_flush(struct file *file, fl_owner_t id)
{
int err = fuse_flush_data(file, id);

/* after the data: the lock may be what orders it against a reader */
fuse_unlock_on_last_close(file);
return err;
}

int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
int datasync, int opcode)
{
Expand Down Expand Up @@ -3996,10 +4062,18 @@ static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
} else
err = fuse_getlk(file, fl);
} else {
if (fc->no_lock)
if (fc->no_lock) {
err = posix_lock_file(file, fl, NULL);
else
} else {
/* see fuse_unlock_on_last_close() */
if ((fl->fl_flags & FL_OFDLCK) &&
fl->fl_type != F_UNLCK) {
struct fuse_file *ff = file->private_data;

WRITE_ONCE(ff->ofd_lock, true);
}
err = fuse_setlk(file, fl, 0);
}
}
return err;
}
Expand Down
6 changes: 6 additions & 0 deletions fs/fuse/fuse_i.h
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,12 @@ struct fuse_file {

/** Has flock been performed on this file? */
bool flock:1;

/**
* Has an OFD lock been requested from the server through this file?
* Not a bit next to flock: the two are set without a common lock.
*/
bool ofd_lock;
};

/** One input argument of a request */
Expand Down