FBSD: Check VOP_ISLOCKED for LK_EXCLUSIVE 04/14204/5
authorAndrew Deason <adeason@dson.org>
Sun, 10 May 2020 22:26:27 +0000 (17:26 -0500)
committerBenjamin Kaduk <kaduk@mit.edu>
Tue, 22 Feb 2022 01:44:15 +0000 (20:44 -0500)
commitfad319fea28ea72fa695edca4b56492c8d352055
tree663b6a7d4e63900c19731f94f930e95ffd515e83
parent2eed23e1591f9a988ff0e9b785abddb2e91f42ee
FBSD: Check VOP_ISLOCKED for LK_EXCLUSIVE

A couple of places in the code check if a vnode is locked by the
current thread by simply checking 'if (VOP_ISLOCKED(vp))'. But
VOP_ISLOCKED() doesn't just return a simple 1 or 0, it returns LK_*
constants (or 0). LK_EXCLUSIVE indicates that the calling thread holds
an exclusive lock on the vnode, but LK_SHARED means someone holds a
shared lock (possibly not the current thread), and LK_EXCLOTHER
indicates that a different thread holds an exclusive lock.

Because of this, it's possible for us to skip grabbing the vnode lock
for certain operations, if someone else holds the lock at the same
time. For example, this can happen when we call vinvalbuf() inside
afs_GetVCache(), and FreeBSD will warn us that we didn't lock the
vnode:

    #0 0xffffffff80bf6557 at kdb_backtrace+0x67
    #1 0xffffffff80c7a337 at assert_vop_locked+0x77
    #2 0xffffffff80c7a273 at vinvalbuf+0x23
    #3 0xffffffff8285aa2d at afs_GetVCache+0x25d
    #4 0xffffffff828d3325 at afs_root+0x145
    #5 0xffffffff80c70296 at lookup+0x8c6
    #6 0xffffffff80c6f599 at namei+0x4a9
    #7 0xffffffff80c872e4 at sys_lpathconf+0x54
    #8 0xffffffff81074581 at amd64_syscall+0x291
    #9 0xffffffff8104cde0 at fast_syscall_common+0x101
    vnode 0xfffff8012a62bc58: tag afs, type VDIR
        usecount 2, writecount 0, refcount 2 mountedhere 0
        flags (VV_ROOT|VI_ACTIVE)
        lock type afs: UNLOCKED
    #0 0xffffffff80b81fc2 at lockmgr_lock_fast_path+0x1e2
    #1 0xffffffff811fa9f6 at VOP_LOCK1_APV+0x96
    #2 0xffffffff80c8c705 at _vn_lock+0x65
    #3 0xffffffff80c7c066 at vget+0xa6
    #4 0xffffffff828d3437 at afs_root+0x257
    #5 0xffffffff80c70296 at lookup+0x8c6
    #6 0xffffffff80c6f599 at namei+0x4a9
    #7 0xffffffff80c872e4 at sys_lpathconf+0x54
    #8 0xffffffff81074581 at amd64_syscall+0x291
    #9 0xffffffff8104cde0 at fast_syscall_common+0x101
    vc 0xfffffe002be00000 vp 0xfffff8012a62bc58 tag afs, fid: 1.536870924.1.1, opens 0, writers 0

To fix this, change our "islocked"-style checks to check if
VOP_ISLOCKED() returns LK_EXCLUSIVE specifically.

Change-Id: If322f62dc443ee9acc2349a23c6c618afd3e29d4
Reviewed-on: https://gerrit.openafs.org/14204
Reviewed-by: Benjamin Kaduk <kaduk@mit.edu>
Tested-by: BuildBot <buildbot@rampaginggeek.com>
src/afs/afs_pioctl.c
src/afs/afs_vcache.c