Skip to contentSkip to conversation

pynfs: add tests for CB_NOTIFY_LOCK callbacks

PATCH New Jeff Layton 1 participant 3 messages Dec 3

Subsystems (2)
lore
Share & tools
lore

Jeff Layton

author maintainer
#0 lore raw

cover pynfs: add tests for CB_NOTIFY_LOCK callbacks

The first patch fixes a bug in the local pynfs server code. The second patch adds two new testcases for CB_NOTIFY_LOCK callbacks.

---
Jeff Layton (2):
nfs4.1: Fix byte-range lock conflicts and LOCKU in the server
server41tests: add standalone CB_NOTIFY_LOCK tests

 nfs4.1/nfs4state.py                 |  14 ++---
 nfs4.1/server41tests/st_callback.py | 110 ++++++++++++++++++++++++++++++++++++
 2 files changed, 115 insertions(+), 9 deletions(-)

---
base-commit: 111dff5b2250eebb24546f8a9aa3a6024563e1a7
change-id: 20261203-cb_notify_lock-b81cf3063bd1

Best regards,

Signature · 1 line
Jeff Layton <[email protected]>
author maintainer
#1 lore raw

PATCH 1/2 nfs4.1: Fix byte-range lock conflicts and LOCKU in the server

Byte-range locking between clients does not work in the server. find_conflicts() skips a lock as the requester's own when any field of its (client, open owner, lock owner) key matches the template, rather than every field. Two clients that use the same open-owner name never conflict.

ByteLock defines only __cmp__, which Python 3 ignores. As a result, list.remove() in remove_lock() never finds the lock, and every LOCKU fails with NFS4ERR_LOCK_RANGE.

Require every non-wildcard key field to match in find_conflicts(), and replace __cmp__ with an __eq__ that compares the lock range. COUR1 now passes. COUR2 depends on the first client's lease expiring, which the server does not implement. After this change, COUR2 fails with NFS4ERR_DENIED on the second client's LOCK rather than at LOCKU.

---
 nfs4.1/nfs4state.py | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)
nfs4.1/nfs4state.py +5 -9
index c34fb2931a55..4bd208007418 100644
@@ -106,12 +106,8 @@ class ByteLock(object):
         str = ("WRITE" if self.iswrite else "READ")
         return "%sLOCK: %i to %i" % (str, self.start, self.end)
 
-    def __cmp__(self, other):
-        out = cmp(self.start, other.start)
-        if out == 0:
-            return cmp(self.end, other.end)
-        else:
-            return out
+    def __eq__(self, other):
+        return (self.start, self.end) == (other.start, other.end)
 
     def overlaps(self, start, end, exact=False):
         """Returns True if given range overlaps that of lock.
@@ -417,9 +413,9 @@ class ByteState(FileStateTyped):
         """
         def match(template, key):
             for i,j in zip(template, key):
-                if i==j or i is None:
-                    return True
-            return False
+                if not (i==j or i is None):
+                    return False
+            return True
         for e in self._tree.itervalues():
             if match(key_template, e.key):
                 # Ignore locks in subtree indicated by key
Signature · 1 line
2.56.1
author maintainer
#2 lore raw

PATCH 2/2 server41tests: add standalone CB_NOTIFY_LOCK tests

In reply to Jeff Layton

CALLBACK1 is the only test that expects a CB_NOTIFY_LOCK, and it reaches that check only after idling for two lease periods and expiring a client through the serverhelper. Without a serverhelper, CALLBACK1 fails at the expiry check first, so an unattended run cannot catch a server that never sends the callback. NFSD in Linux v6.7 through v6.12 is such a server: on filesystems without their own ->lock, NFSD clears FL_SLEEP from every blocking LOCK.

Add CALLBACK2, which checks that a WRITEW_LT denied by another client's lock gets a CB_NOTIFY_LOCK after the holder's LOCKU, and CALLBACK3, which checks that a denied WRITE_LT does not. CALLBACK3 catches the NFSD bug fixed by kernel commit 246a90a5109b ("nfsd: fix FL_SLEEP being set unconditionally for all LOCK types"). Both tests report UNSUPPORTED when OPEN does not set OPEN4_RESULT_MAY_NOTIFY_LOCK.

---
 nfs4.1/server41tests/st_callback.py | 110 ++++++++++++++++++++++++++++++++++++
 1 file changed, 110 insertions(+)
nfs4.1/server41tests/st_callback.py +110 -0
index cdfc74a5775b..32d496a5efd3 100644
@@ -133,3 +133,113 @@ def testCbNotifyLockExpiredClient(t, env):
 
     res = close_file(sess2, fh2, stateid=stateid2)
     check(res)
+
+# How long to wait for a CB_NOTIFY_LOCK after the conflicting lock
+# is released.
+NOTIFY_LOCK_WAIT = 10
+
+def _new_owner_lock_op(open_stateid, owner, locktype):
+    """LOCK of the whole file by a new lock owner"""
+    open_to_lock_owner = open_to_lock_owner4(0, open_stateid, 0,
+                                             lock_owner4(0, owner))
+    locker = locker4(open_owner=open_to_lock_owner, new_lock_owner=True)
+    return op.lock(locktype, False, 0, NFS4_UINT64_MAX, locker)
+
+def _notify_lock_conflict(t, env, locktype):
+    """Set up a lock conflict between two clients
+
+    The first client holds a WRITE_LT on the whole file. The second
+    client then requests a lock of type locktype on the whole file,
+    and gets NFS4ERR_DENIED. Any CB_NOTIFY_LOCK the second client
+    receives is recorded in the returned "notifies" list.
+    """
+    notifies = []
+    notified = threading.Event()
+    def pre_hook(arg, env):
+        notifies.append(arg)
+        notified.set()
+
+    sess1 = env.c1.new_client_session(b"%s_1" % env.testname(t))
+    res = create_file(sess1, env.testname(t))
+    check(res)
+    fh1 = res.resarray[-1].object
+    open_stateid1 = res.resarray[-2].stateid
+    res = sess1.compound([op.putfh(fh1),
+                          _new_owner_lock_op(open_stateid1, b"lock1",
+                                             WRITE_LT)])
+    check(res, msg="WRITE_LT by the first client")
+    lock_stateid1 = res.resarray[-1].lock_stateid
+
+    sess2 = env.c1.new_client_session(b"%s_2" % env.testname(t))
+    sess2.client.cb_pre_hook(OP_CB_NOTIFY_LOCK, pre_hook)
+    res = open_file(sess2, env.testname(t), access=OPEN4_SHARE_ACCESS_BOTH)
+    check(res)
+    fh2 = res.resarray[-1].object
+    open_stateid2 = res.resarray[-2].stateid
+    if not res.resarray[-2].rflags & OPEN4_RESULT_MAY_NOTIFY_LOCK:
+        t.fail_support("Server did not set OPEN4_RESULT_MAY_NOTIFY_LOCK")
+
+    lock_op2 = _new_owner_lock_op(open_stateid2, b"lock2", locktype)
+    res = sess2.compound([op.putfh(fh2), lock_op2])
+    check(res, NFS4ERR_DENIED,
+          "%s while another client holds a WRITE_LT" % nfs_lock_type4[locktype])
+
+    return {"sess1": sess1, "fh1": fh1, "open_stateid1": open_stateid1,
+            "lock_stateid1": lock_stateid1,
+            "sess2": sess2, "fh2": fh2, "open_stateid2": open_stateid2,
+            "lock_op2": lock_op2,
+            "notifies": notifies, "notified": notified}
+
+def _notify_lock_release(s):
+    """Release the first client's lock and close its file"""
+    res = s["sess1"].compound([op.putfh(s["fh1"]),
+                               op.locku(WRITE_LT, 0, s["lock_stateid1"],
+                                        0, NFS4_UINT64_MAX)])
+    check(res, msg="LOCKU by the first client")
+    res = close_file(s["sess1"], s["fh1"], stateid=s["open_stateid1"])
+    check(res)
+
+def testCbNotifyLockBlocking(t, env):
+    """A blocking LOCK denied by another client's lock gets a
+    CB_NOTIFY_LOCK when that lock is released
+
+    FLAGS: callback all
+    CODE: CALLBACK2
+    """
+    s = _notify_lock_conflict(t, env, WRITEW_LT)
+    _notify_lock_release(s)
+
+    s["notified"].wait(NOTIFY_LOCK_WAIT)
+    if not s["notifies"]:
+        fail("Did not receive CB_NOTIFY_LOCK within %d seconds of LOCKU"
+             % NOTIFY_LOCK_WAIT)
+    arg = s["notifies"][0]
+    if arg.cnla_fh != s["fh2"]:
+        fail("CB_NOTIFY_LOCK has the wrong filehandle")
+    if arg.cnla_lock_owner.owner != b"lock2":
+        fail("CB_NOTIFY_LOCK has lock owner %r, expected %r" %
+             (arg.cnla_lock_owner.owner, b"lock2"))
+
+    res = s["sess2"].compound([op.putfh(s["fh2"]), s["lock_op2"]])
+    check(res, msg="WRITEW_LT retried after CB_NOTIFY_LOCK")
+    res = close_file(s["sess2"], s["fh2"], stateid=s["open_stateid2"])
+    check(res)
+
+def testCbNotifyLockNonBlocking(t, env):
+    """A non-blocking LOCK denied by another client's lock does not get
+    a CB_NOTIFY_LOCK when that lock is released
+
+    FLAGS: callback all
+    CODE: CALLBACK3
+    """
+    s = _notify_lock_conflict(t, env, WRITE_LT)
+    _notify_lock_release(s)
+
+    s["notified"].wait(NOTIFY_LOCK_WAIT)
+    if s["notifies"]:
+        fail("Received CB_NOTIFY_LOCK for a non-blocking LOCK")
+
+    res = s["sess2"].compound([op.putfh(s["fh2"]), s["lock_op2"]])
+    check(res, msg="WRITE_LT retried after the conflicting lock was released")
+    res = close_file(s["sess2"], s["fh2"], stateid=s["open_stateid2"])
+    check(res)
Signature · 1 line
2.56.1