Skip to content

RED-54356: Linux 6.17: fuse: drop OFD and flock() locks at the last close instead of at RELEASE - #293

Merged
hbirth merged 3 commits into
DDNStorage:redfs-ubuntu-hwe-6.17.0-16.16-24.04.1from
achhenderson:fix-redfs-6.17-flock-unlock-on-close
Nov 18, 2026
Merged

hbirth merged 3 commits into
DDNStorage:redfs-ubuntu-hwe-6.17.0-16.16-24.04.1from
achhenderson:fix-redfs-6.17-flock-unlock-on-close

Conversation

@achhenderson

Copy link
Copy Markdown
Collaborator

Summary

6.17 port of #289 (4bee006ce65e, cd6cd0014a1a, 5fe0a9765c18 on the 7.0 branch).

With a server that takes fcntl and flock locks itself, an OFD lock or a flock() lock outlives the last close() of its open file description. The server drops the lock when it processes RELEASE, and RELEASE is queued in the background by the final fput(), after close() has returned. Until then F_OFD_GETLK through another description still reports the lock, a non-blocking F_OFD_SETLK fails with EAGAIN, and flock(LOCK_EX | LOCK_NB) fails with EWOULDBLOCK. generic/478 fails on this in its CLONE_FILES cases.

Three commits, as on 7.0:

  1. fuse: drop OFD locks at the last close instead of at RELEASE. When ->flush() holds the only reference to the file, it sends the whole-file unlock for the OFD owner and waits for it, after the data has been flushed.
  2. fuse: refactor fuse_ofd_unlock_on_close(). No functional change.
  3. fuse: drop a flock() lock at the last close as well. The same unlock with FUSE_LK_FLOCK.

Adaptation: this branch has one fuse_flush() where 7.0 has fuse_wt_flush() and fuse_wb_flush(). Its body becomes fuse_flush_data(), and fuse_flush() calls the unlock after it. The helper functions are identical to the 7.0 ones.

Cost, as on 7.0: one more request, a SETLK unlock, at the last close of a file that ever took such a lock through the server. A file that never took a lock is closed with the same requests as before.

Testing

Ubuntu 6.17.0-42-generic with redfs.ko built from this branch, RED release/2.5, with and without the writeback cache:

  • A loop of lock, close(), query through a second description. OFD: the lock is still reported after 0 of 200 closes in both modes; without the change after 194 of 200 without the writeback cache and 200 of 200 with it. flock(): still held after 0 of 200 closes; without the change after 187 of 200 and 159 of 200.
  • An OFD lock and a flock() lock each survive the close of a dup() and the exit of a forked child in 200 of 200 cases.
  • generic/478 fails without the change. With it, generic/131, 478 and 571 pass in 3 of 3 runs without the writeback cache and 2 of 2 with it. The body of generic/478, run 30 times in a row on one mount in each mode, matched the expected output every time; without the change it differed in 6 of 6 runs.
  • After commits 1 and 2 the OFD window is closed and the flock() window is unchanged, as intended.

JIRA Ticket

https://ime-ddn.atlassian.net/browse/RED-54356

Allison Henderson and others added 3 commits November 16, 2026 15:37
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 <allison.henderson@ddn.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
(cherry picked from commit 4bee006)
[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]
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 <allison.henderson@ddn.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
(cherry picked from commit cd6cd00)
[ahenderson: one caller, fuse_flush(), instead of two]
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 <allison.henderson@ddn.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
(cherry picked from commit 5fe0a97)
[ahenderson: applies unchanged]

@hbirth hbirth left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The backport of an approved change

@hbirth
hbirth merged commit 1ec58db into DDNStorage:redfs-ubuntu-hwe-6.17.0-16.16-24.04.1 Nov 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants