diff --git a/fs/fuse/file.c b/fs/fuse/file.c index 76b41626368c95..fa3c7641696509 100644 --- a/fs/fuse/file.c +++ b/fs/fuse/file.c @@ -658,7 +658,64 @@ static void fuse_splice_stage_done(struct inode *inode) smp_store_release(&fi->splice_stager, NULL); } -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); @@ -721,6 +778,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) { @@ -4994,10 +5060,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->c.flc_flags & FL_OFDLCK) && + fl->c.flc_type != F_UNLCK) { + struct fuse_file *ff = file->private_data; + + WRITE_ONCE(ff->ofd_lock, true); + } err = fuse_setlk(file, fl, 0); + } } return err; } diff --git a/fs/fuse/fuse_i.h b/fs/fuse/fuse_i.h index a516f0ab316ef0..83df0b38fb1af6 100644 --- a/fs/fuse/fuse_i.h +++ b/fs/fuse/fuse_i.h @@ -443,6 +443,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 */