From c35b87f3f8ccd90254a6f64f431dc2004eaf3d69 Mon Sep 17 00:00:00 2001 From: Allison Henderson Date: Wed, 11 Nov 2026 23:18:24 -0700 Subject: [PATCH 1/3] fuse: drop OFD locks at the last close instead of at RELEASE With a server that takes fcntl locks itself (FUSE_POSIX_LOCKS), an OFD lock outlives the last close() of its open file description. The server learns that the description is gone from RELEASE and drops the lock there, but RELEASE is queued as a background request by the final fput(), after close() has returned. Until the server has processed it, F_OFD_GETLK through another description still reports the lock and a non-blocking F_OFD_SETLK fails with EAGAIN. generic/478 fails on this in its CLONE_FILES cases ("get wrlck" where "lock could be placed" is expected) since redfsd handles fcntl locks (RED-46944). Before that it answered ENOSYS and the kernel kept the locks locally. In a loop of F_OFD_SETLK, close(), F_OFD_GETLK through a second description, the lock was still reported after 49 of 50 closes and stayed for 0.2 to 2.9 ms. The VFS does not help here: locks_remove_posix() returns early when the inode has no lock on its own list, and fuse does not record a lock the server holds. RELEASE cannot be made synchronous either, because the final fput() can run on a thread of the server (see fuse_file_release()). Do it from ->flush(), which close() calls in the closing task before that fput(). When ->flush() holds the only reference to the file, the descriptor it runs for was the last one: it has left the descriptor table already, and a mapping, an I/O in flight or a descriptor in transit would hold a reference of its own, so no new reference can appear. In that case send the unlock of the whole file for the OFD owner and wait for it, after the data has been flushed. It is sent only for a file through which an OFD lock was requested from the server. Closing one of several descriptors of a description (dup(), fork()) leaves the lock in place, as it must. A description that loses its last reference in another way (munmap() after close(), an aio completion) is unlocked by RELEASE, as before. flock() locks have the same window, because FUSE_RELEASE_FLOCK_UNLOCK travels in the same RELEASE. This change does not touch them. Tested on 7.0.0-42-generic against RED 2.5.2 (497d797afb + red#30365), with and without the writeback cache: the loop above reports the lock after 0 of 200 closes, and a lock survives the close of a dup() in 200 of 200 cases. generic/478 passes in 6 of 6 runs without the cache and in 3 of 3 with it; without the change it fails in 5 of 5 and in 2 of 2. Signed-off-by: Allison Henderson Co-Authored-By: Claude Fable 5.2 (cherry picked from commit 4bee006ce65e8388c2077771ef71229db24492c9) [ahenderson: there is one fuse_flush() here instead of fuse_wt_flush() and fuse_wb_flush(); its body becomes fuse_flush_data() and fuse_flush() calls the unlock after it; struct file_lock has fl_flags and fl_type here] --- fs/fuse/file.c | 67 +++++++++++++++++++++++++++++++++++++++++++++--- fs/fuse/fuse_i.h | 6 +++++ 2 files changed, 70 insertions(+), 3 deletions(-) diff --git a/fs/fuse/file.c b/fs/fuse/file.c index fb518003670b0b..417e5ead7756fa 100644 --- a/fs/fuse/file.c +++ b/fs/fuse/file.c @@ -612,7 +612,51 @@ static void fuse_sync_writes(struct inode *inode) fuse_release_nowrite(inode); } -static int fuse_flush(struct file *file, fl_owner_t id) +/* + * The server learns that an open file description is gone from RELEASE, and + * drops the description's OFD locks there. 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. + */ +static void fuse_ofd_unlock_on_close(struct file *file) +{ + 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); + + if (!READ_ONCE(ff->ofd_lock) || file_count(file) != 1 || + fuse_is_bad(inode)) + return; + + 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; + 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); +} + +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); @@ -665,6 +709,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_ofd_unlock_on_close(file); + return err; +} + int fuse_fsync_common(struct file *file, loff_t start, loff_t end, int datasync, int opcode) { @@ -3996,10 +4049,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_ofd_unlock_on_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; } diff --git a/fs/fuse/fuse_i.h b/fs/fuse/fuse_i.h index 7a846d92399089..2210db6fb45ac1 100644 --- a/fs/fuse/fuse_i.h +++ b/fs/fuse/fuse_i.h @@ -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 */ From dc4dce6267f5ab63167438e8952d8716e5f1dfbd Mon Sep 17 00:00:00 2001 From: Allison Henderson Date: Thu, 12 Nov 2026 10:01:09 -0700 Subject: [PATCH 2/3] fuse: refactor fuse_ofd_unlock_on_close() Hoist the unlock request out of fuse_ofd_unlock_on_close() into a common helper, fuse_unlock_file_owner(). The helper takes the lk_flags of the request, so the same logic can be reused for another kind of lock that is owned by the file. What stays in the caller is the decision when to unlock: first whether this is the last close, then what there is to unlock. Rename it fuse_unlock_on_last_close(), since it will not stay specific to OFD locks. This is a preparatory patch for closing the same window for flock() locks. No functional change. Signed-off-by: Allison Henderson Co-Authored-By: Claude Fable 5.2 (cherry picked from commit cd6cd0014a1a6fc2c647d1293a94c1dad0e2a93e) [ahenderson: one caller, fuse_flush(), instead of two] --- fs/fuse/file.c | 51 +++++++++++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 21 deletions(-) diff --git a/fs/fuse/file.c b/fs/fuse/file.c index 417e5ead7756fa..b52c9bce8b724d 100644 --- a/fs/fuse/file.c +++ b/fs/fuse/file.c @@ -612,6 +612,30 @@ static void fuse_sync_writes(struct inode *inode) fuse_release_nowrite(inode); } +/* 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. RELEASE is sent in the background @@ -630,30 +654,15 @@ static void fuse_sync_writes(struct inode *inode) * A description that loses its last reference some other way (munmap() after * close(), completion of an aio) is unlocked by RELEASE, as before. */ -static void fuse_ofd_unlock_on_close(struct file *file) +static void fuse_unlock_on_last_close(struct file *file) { - 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); - if (!READ_ONCE(ff->ofd_lock) || file_count(file) != 1 || - fuse_is_bad(inode)) + if (file_count(file) != 1 || fuse_is_bad(file_inode(file))) return; - 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; - 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); + if (READ_ONCE(ff->ofd_lock)) + fuse_unlock_file_owner(file, 0); } static int fuse_flush_data(struct file *file, fl_owner_t id) @@ -714,7 +723,7 @@ 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_ofd_unlock_on_close(file); + fuse_unlock_on_last_close(file); return err; } @@ -4052,7 +4061,7 @@ static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl) if (fc->no_lock) { err = posix_lock_file(file, fl, NULL); } else { - /* see fuse_ofd_unlock_on_close() */ + /* see fuse_unlock_on_last_close() */ if ((fl->fl_flags & FL_OFDLCK) && fl->fl_type != F_UNLCK) { struct fuse_file *ff = file->private_data; From 8f584bcb29030b06fac64cabc38c455915db5069 Mon Sep 17 00:00:00 2001 From: Allison Henderson Date: Thu, 12 Nov 2026 09:05:49 -0700 Subject: [PATCH 3/3] fuse: drop a flock() lock at the last close as well A flock() lock that the server holds (FUSE_FLOCK_LOCKS) has the window an OFD lock had: the server drops it when RELEASE arrives with FUSE_RELEASE_FLOCK_UNLOCK, and RELEASE is sent in the background after close() has returned. Until then flock(LOCK_EX | LOCK_NB) through another description fails with EWOULDBLOCK although the only holder has closed its file. In a loop of flock(LOCK_EX), close(), flock(LOCK_EX | LOCK_NB) through a second description, that happened after 187 of 200 closes and lasted up to 3.2 ms. Send that unlock from fuse_unlock_on_last_close() as well, which ->flush() calls and which acts when ->flush() holds the only reference to the file: fuse_unlock_file_owner() once more, with FUSE_LK_FLOCK, for a file that was locked with flock() through the server. RELEASE keeps its FUSE_RELEASE_FLOCK_UNLOCK flag for a description whose last reference goes away without a close(); after the unlock from ->flush() it finds nothing to drop. The cost is one more request at the last close of a file that was ever locked with flock(), also when the lock was dropped with LOCK_UN before. A file that never took a lock is closed with the same requests as before. Tested on 7.0.0-42-generic against RED 2.5.2 (497d797afb + red#30365), with and without the writeback cache: the loop above finds the lock after 0 of 200 closes; a flock() lock and an OFD lock survive the close of a dup() and the exit of a forked child in 200 of 200 cases each; and generic/131, 478 and 571 pass in 3 of 3 runs in each mode. Signed-off-by: Allison Henderson Co-Authored-By: Claude Fable 5.2 (cherry picked from commit 5fe0a9765c187d4349f288b949a00fc260343449) [ahenderson: applies unchanged] --- fs/fuse/file.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/fs/fuse/file.c b/fs/fuse/file.c index b52c9bce8b724d..7a5e0ce615610e 100644 --- a/fs/fuse/file.c +++ b/fs/fuse/file.c @@ -638,7 +638,8 @@ static void fuse_unlock_file_owner(struct file *file, u32 lk_flags) /* * The server learns that an open file description is gone from RELEASE, and - * drops the description's OFD locks there. RELEASE is sent in the background + * 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 @@ -652,7 +653,8 @@ static void fuse_unlock_file_owner(struct file *file, u32 lk_flags) * 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. + * 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) { @@ -663,6 +665,8 @@ static void fuse_unlock_on_last_close(struct file *file) 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)