Bug 16279 - SMB3 Directory Leases never invalidated by changes made outside Samba's own VFS write path
Summary: SMB3 Directory Leases never invalidated by changes made outside Samba's own V...
Status: NEW
Alias: None
Product: Samba 4.1 and newer
Classification: Unclassified
Component: File services (show other bugs)
Version: 4.24.7
Hardware: All FreeBSD
: P5 major (vote)
Target Milestone: ---
Assignee: Samba QA Contact
QA Contact: Samba QA Contact
URL: https://gitlab.com/samba-team/samba/-...
Keywords:
Depends on:
Blocks:
 
Reported: 2026-10-13 17:15 UTC by Michael Osipov
Modified: 2026-10-13 18:17 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Michael Osipov 2026-10-13 17:15:37 UTC
Preamble:
We run several servers which expose their local disks via NFS and have Samba 4.19.x running. Other servers access via NFS and modify data, Windows clients need to access those data. In other words, we run several Unix/Windows gateways. Long overdue to upgrade Samba, now tried 4.24.7 and it failed for automated processes and users which keep tracking directories for changes.

Note that Claude Code assisted me to narrow down the cause of this issue:

  SMB3 Directory Leases (smb3 directory leases, default "auto") are only
  invalidated via contend_dirleases() in source3/smbd/smb2_oplock.c, which
  is called from exactly one place: notify_fname() in source3/smbd/notify.c.
  notify_fname() is in turn only called from smbd's own VFS write-path
  operations performed on behalf of a live SMB2 client connection (create,
  rename, truncate, close-after-modify, remove, setinfo, hardlink).

  Any modification to a leased directory that does NOT go through smbd's
  own VFS write path is completely invisible to a client holding a
  directory lease on that path, forever, until the client's SMB session
  is fully torn down (a plain F5/refresh in Explorer does nothing - the
  client is honoring a lease it hasn't been told is broken). This
  includes:
    - a bare filesystem write performed directly on the server (e.g. by a
      local process, cron job, or any out-of-band tool)
    - a write to the same path via another protocol sharing the same
      filesystem (e.g. NFS from a different client)
    - presumably any other smbd-external modification path

  Separately, kernel-level change-notification (inotify on Linux, or the
  FreeBSD devel/libinotify-kqueue shim) is reported to notifyd
  (source3/smbd/notifyd/notifyd.c) via notifyd_sys_callback(), but notifyd
  only fans this out to clients holding an ACTIVE SMB2 CHANGE-NOTIFY
  REQUEST (the classic FindNotifyChange-style watch) - never to lease
  holders. notifyd has no code path that touches leases_db or
  contend_dirleases() at all (grep -n "lease" source3/smbd/notifyd/*.c
  returns zero matches). notifyd is a standalone daemon process with no
  struct connection_struct, and leases_db/share-mode-lock state (keyed by
  file_id, not by path) is designed to only be touched from inside a live
  smbd connection, so this isn't a small fix - notifyd would need a new
  path/file_id lookup and a way to send lease-break messages directly,
  replicating only the message-sending half of contend_dirleases().

  I believe this is a real gap in the feature's design/test coverage
  rather than an already-known limitation: every test case in
  source4/torture/smb2/lease.c that exercises directory leases
  (test_dirlease_oplocks, test_dirlease_leases, test_dirlease_setinfo,
  test_dirlease_seteof, etc.) triggers the lease break via a SECOND SMB2
  CONNECTION performing the modifying operation - none of them simulate a
  change that bypasses SMB entirely. I searched Bugzilla for existing
  reports (quicksearch on "directory lease"/"dirlease", reviewed bugs
  15865, 16064, 11655, 15917 in detail) and found nothing describing this
  specific gap.

  Environment where this was found: FreeBSD 14, net/samba424 port
  (Samba 4.24.7), Windows clients (Explorer and PowerShell
  Get-ChildItem, which share the Windows SMB2/3 client redirector's
  lease cache). Confirmed the same gap is present unchanged in Samba
  4.25.0 and the current master tip (checked 2026-10-13).

  Steps to Reproduce
  ==================
  No NFS or any special backend is required - this reproduces on a plain
  local-filesystem share.

  1. Create a share with default settings (smb3 directory leases left at
     "auto"), backed by any local directory, e.g.:
       [testshare]
         path = /path/to/testdir
         read only = no

  2. From a Windows client, open the share in File Explorer and leave the
     window open and idle on that folder (do not navigate away).

  3. On the Samba server, OUTSIDE of any SMB client connection - e.g.
     directly in a local shell - create a new file in the shared
     directory:
       touch /path/to/testdir/newfile

  4. Observe the open Explorer window: the new file does NOT appear.
     Press F5 / use the Refresh action in Explorer: the new file still
     does NOT appear. (PowerShell's Get-ChildItem against the same share
     from the same client session shows the same stale result, since it
     shares the Windows SMB2/3 redirector's lease cache with Explorer.)

  5. For contrast, repeat step 3 but create the file via SMB instead -
     e.g. from a second Windows client, or smbclient - rather than
     directly on the server filesystem. The first client's idle Explorer
     window DOES update immediately, with no F5 needed. This confirms
     directory leases work correctly for SMB-to-SMB changes; the gap is
     specifically scoped to changes that bypass smbd's own write path.

  6. Only disconnecting and reconnecting the first client's SMB session
     entirely (e.g. "net use \\server\share /delete" followed by a fresh
     connection, or closing all handles to force session teardown) clears
     the stale state, because a fresh session has no cached lease yet.

  7. Setting "smb3 directory leases = no" in smb.conf and restarting
     smbd avoids the issue entirely - with that option disabled, step 3's
     change is visible immediately via a plain refresh (no F5 even
     needed, since no caching happens without the lease).

  Actual results
  ==============
  A client holding a directory lease never learns about a directory
  modification unless that modification happened to go through smbd's
  own VFS write path. There is no mechanism connecting the kernel-level
  notify infrastructure (notifyd/inotify) - which DOES correctly detect
  such changes and DOES correctly serve plain SMB2 change-notify watch
  requests for them - to directory lease invalidation at all.

  Expected results
  =================
  A client holding a directory lease should see the lease broken (and
  be forced to re-fetch the directory listing) for ANY change to that
  directory's contents, regardless of whether the change originated
  from an SMB client or not - consistent with how plain SMB2
  change-notify (FindNotifyChange) already behaves for the same kind of
  external change via notifyd's kernel-event integration.

  Suggested fix direction
  ========================
  Wire notifyd's kernel-detected-change path into lease invalidation,
  e.g. by having notifyd (or a new intermediary) resolve the changed
  path to a file_id and send a direct lease-break message to any
  smbd process holding a directory lease on that file_id, mirroring
  the message-sending half of contend_dirleases() without requiring a
  live connection object inside notifyd itself. I have not attempted a
  patch - this looks like it needs design input from whoever owns the
  leases_db/notifyd separation of concerns, rather than a mechanical
  fix.
Comment 1 Michael Osipov 2026-10-13 17:16:20 UTC
Add Ralph since he is the author of the contend_dirleases/directory-leases commits.
Comment 2 Michael Osipov 2026-10-13 17:30:40 UTC
  I found a prior attempt at fixing exactly this gap on the samba-technical
  mailing list, which I missed in my initial Bugzilla search since it never
  got a bug number:

    https://marc.info/?l=samba-technical&m=178635059716784&w=2
    "[RFC PATCH samba] smbd: break directory leases before sending notify
    events" - ChenXiaoSong <chenxiaosong@kylinos.cn>, 2026-08-10

  The patch's reproducer and root cause description match this bug exactly:
  "A file change made on the server does not use the SMB request path."
  It adds a NOTIFY_ACTION_DIRLEASE_BREAK flag threaded through notifyd's
  trigger messages (source3/smbd/notify.c, notifyd/notifyd.c,
  notifyd/fcn_wait.c), plus a new contend_dirleases_by_fsp() in
  smb2_oplock.c that breaks leases via share_mode_forall_leases()/
  do_dirlease_break_to_none() when the flag is set.

  Ralph replied on-list:
    https://marc.info/?l=samba-technical&m=178695360287575&w=2

  pointing out a correctness problem: the patch would also incorrectly
  break a client's OWN directory lease when that same client is the one
  making the change via its own parent-dirlease key - the patch has no
  way to distinguish "this notify event originated from the lease holder
  itself" from "this notify event originated from somewhere else",
  because inotify events carry no information about which process/client
  triggered them.

  Ralph also mentioned he's planning a proper fix using fanotify instead
  of inotify, since fanotify events include the triggering process's PID,
  which would let notifyd correctly identify and skip self-triggered
  events. He described this as "on my roadmap for the next months" as of
  2026-08-17.

  Given fanotify is Linux-specific, I wanted to flag a portability
  question for this bug specifically (filed as Hardware: All FreeBSD,
  since that's where I found it): if the eventual fix is fanotify-based,
  would FreeBSD (and other non-Linux platforms using the generic inotify
  backend, e.g. notify_inotify.c via devel/libinotify-kqueue) still be
  left without a fix, or is a parallel/fallback mechanism planned for
  platforms where fanotify doesn't exist at all? I don't have visibility
  into whether this has already been considered as part of the fanotify
  redesign - just raising it so it isn't an afterthought if/when that
  work lands.
Comment 3 Ralph Böhme 2026-10-13 17:37:24 UTC
(In reply to Michael Osipov from comment #2)
Lacking fanotify, on FreeBSD you'll have to disable Directory Leases for your usecase.
Comment 4 Ralph Böhme 2026-10-13 17:40:00 UTC
For the records, I have a in-progress branch that I will be working on in the next months.
Comment 5 Michael Osipov 2026-10-13 17:40:37 UTC
(In reply to Ralph Böhme from comment #3)

This is what I did for now. Sadly, "smb3 directory leases = auto" is the defualt and people can fall in that trap, like me.
Comment 6 Michael Osipov 2026-10-13 17:41:06 UTC
(In reply to Ralph Böhme from comment #4)

With fanotify or something else? I'd be happy to test if it is portable.
Comment 7 Ralph Böhme 2026-10-13 17:48:40 UTC
(In reply to Michael Osipov from comment #5)
We should fix that I guess.
Comment 8 Ralph Böhme 2026-10-13 17:49:48 UTC
(In reply to Michael Osipov from comment #6)
fanotify. I'm not aware of any other notification systems that supports the required semantics (change message containing the pid of the trigger process).
Comment 9 Michael Osipov 2026-10-13 18:16:49 UTC
(In reply to Ralph Böhme from comment #8)

No objections, I agree.
Comment 10 Michael Osipov 2026-10-13 18:17:16 UTC
(In reply to Ralph Böhme from comment #7)

That would be great, even if it is just in the docs or manpage.