pfiles(1), locked files and a pinch of salt
By PeteH on Jan 10, 2007
Following a few days working this out I thought I'd share my thoughts on why the locking information returned by pfiles(1) should be taken with a pinch of salt.
What pfiles(1) tells us about locked open files
On Solaris, if we run pfiles(1) on a process that has open files it checks each file to see if it has any locks. The way it does this is to use fcntl(2) and F_GETLK. This is more complex than it first appears as it runs the system call from the context of the process being inspected. Suffice to say, it produces output like this:
# pfiles 102226 102226: /home/foo/lock /home/foo/testfile Current rlimit: 256 file descriptors 0: S_IFCHR mode:0620 dev:301,0 ino:12582918 uid:54321 gid:7 rdev:24,1 O_RDWR|O_NOCTTY|O_LARGEFILE /devices/pseudo/pts@0:1 1: S_IFCHR mode:0620 dev:301,0 ino:12582918 uid:54321 gid:7 rdev:24,1 O_RDWR|O_NOCTTY|O_LARGEFILE /devices/pseudo/pts@0:1 2: S_IFCHR mode:0620 dev:301,0 ino:12582918 uid:54321 gid:7 rdev:24,1 O_RDWR|O_NOCTTY|O_LARGEFILE /devices/pseudo/pts@0:1 3: S_IFREG mode:0666 dev:314,5 ino:2557867 uid:54321 gid:10 size:343 O_RDWR advisory write lock set by system 0x1 process 102225 /home/foo/testfile #
All fine and dandy, but what is meant by "system" and is that process ID actually useful?
How the process and system IDs are derived
To understand this we need to look much closer at how these values are set and in particular whether we're using NFSv3 or NFSv4. I'm ignoring NFSv2 but that will behave similarly to NFSv3.
There are two sides to this, who sets the lock and who checks it with pfiles(1).
Setting the lock
For NFSv3 we use lockd/statd and the locking code passes the PID of the locking process to the server. There is no 'system id' passed to the server. The NFSv3 server passes the PID to underlying file system in the flock structure of the VOP_FRLOCK() call.
For NFSv4 it is considerably more complicated but essentially it passes what should be an opaque 'lock_owner' structure to the server. This can only by understood by the client that sent it. The NFSv4 server creates a unique identifier which it assigns to the PID member of the flock structure. As with NFSv3 this flock structure is passed to the underlying file system.
The pfiles(1) output
The pfiles(1) output is based on a F_GETLK call for the open file. This returns a structure with l_pid and l_sysid. The values of these is as follows:
For NFSv3 the locking code returns the PID passed to the underlying file system in the original VOP_FRLOCK() call. For locks set for NFSv3 this is the true PID of the locking process. The sysid may be set to zero in which case it is ignored by pfiles(1). If the lock was held by a process on this machine the sysid will be set to the system id of the NFS server plus 0x4000. For locks set by NFSv4 the PID is not the PID but the unique identifier used at lock time on the server.
For NFSv4, if we're the client that set the lock we can return sensible values for the PID and system ID - though the latter is \*always\* the client itself. If we're not the client we fabricate the PID and sysid as follows:
- sysid: we take the sysid of the server and add 1
- pid: we add up all the bytes in the clientid and owner15370 /\* 15371 \* Construct a new sysid which should be different from 15372 \* sysids of other systems. 15373 \*/ 15374 15375 flk->l_sysid++; 15376 flk->l_pid = lo_to_pid(&lockt_denied->owner);
Interpreting the results
So, with that in mind we have the following result:
NFSv3 set the lock, read by NFSv3
Process ID is correct, no system identification. eg:advisory write lock set by process 102250
Or perhaps:advisory write lock set by system 0x4001 process 102265
NFSv4 set the lock, read by NFSv3
Process ID is unique but meaningless to the client. No system identification. eg:advisory write lock set by process 776458
NFSv3 set the lock, read by NFSv4
The lock owner will not match on the client, result is fabricated. eg:advisory write lock set by system 0x2 process 512
NFSv4 set the lock, read by NFSv4
If the lock owner matches, we return the true PID of the process running on this system. The sysid is that of the server as understood by the client. eg:advisory write lock set by system 0x1 process 102313If the lock owner doesn't match the client the result is fabricated. eg:advisory write lock set by system 0x2 process 1580
While checking this out I found and logged two curious bugs in this area.
If pfiles(1) is run on an x64 box the 64-bit version is run. If the target process is 32-bit it mixes up the process and system id. Compare these two outputs:
# /usr/bin/amd64/pfiles 2353 | grep advisory advisory write lock set by system 0x943 # /usr/bin/i86/pfiles 2353 | grep advisory advisory write lock set by system 0x1 process 2371 #
Jan 11th - the following is not a bug
The fcntl(2) man page clearly states that the l_pid and l_sysid values are only used for F_GETLK calls:
The l_pid and l_sysid fields are used only with F_GETLK or
F_GETLK64 to return the process ID of the process holding a
blocking lock and to indicate which system is running that
One more complication - pfiles(1) doesn't show locks held by the process itself
The reason pfiles(1) fails to show locks held by a process is due to the way that pfiles works. It effectively hijacks the process being checked and makes the fcntl(2) call within that process context. Processes can re-lock files they already have open and locked as per the fcntl(2) man page:
There will be at most one type of lock set for each byte in the file. Before a successful return from an F_SETLK, F_SETLK64, F_SETLKW, or F_SETLKW64 request when the calling process has previously existing locks on bytes in the region specified by the request, the previous lock type for each byte in the specified region will be replaced by the new lock type.
The output of pfiles(1) is tremendously useful but the locking information is not necessarily accurate or useful.
If you're running pfiles(1) against an NFSv4 file then if the process ID matches a local process it's more than likely the process holding the lock. Otherwise, it's meaningless.
If you're running pfiles(1) against an NFSv3 file then it's harder. Again if the process ID makes sense on this client then it's probably correct. Otherwise you could check it against known NFS clients of the same file system ... but that's time consuming.
To properly identify lock owners some mdb(1) or dtrace(1M) is required - and this blog entry is already too long