Actions
Bug #13879
closedflock(3C) on NFS files should fail with EOPNOTSUPP
Start date:
Due date:
% Done:
100%
Estimated time:
Difficulty:
Medium
Tags:
Gerrit CR:
External Bug:
Description
Our flock(3C)
support does not presently support locking files on remote file systems like NFS (see F_REMOTELOCK
, and comments in fs_frlock()
). In the event that one tries to lock an NFS-backed file, flock()
presently fails with EINVAL
. This is probably not quite right; e.g., in the manual page:
EINVAL The operation argument does not contain one of LOCK_SH, LOCK_EX, or LOCK_UN; or the operation argument contains LOCK_UN and LOCK_NB; or the operation argument contains any bits other than those set by LOCK_SH, LOCK_EX, LOCK_NB, and LOCK_UN. ... EOPNOTSUPP The locking of files of the type indicated by the fildes argument is not supported.
The latter seems more appropriate here.
As an aside, a brief survey of the documentation of other platforms shows:
- FreeBSD and OpenBSD appear to use
EOPNOTSUPP
for this condition - Mac OS X appears to use
ENOTSUP
- Linux does not appear to document the possibility of a failure of this kind, but explicitly says
EINVAL
is for an invalidoperation
argument
It does not seem that flock()
has been standardised.
Updated by Luqman Aden 7 months ago
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/types.h>
int main() {
int fd;
int res;
struct flock lockd = {};
fd = open("/tmp/nfs-flock-test/test.lock", O_CREAT | O_WRONLY);
if (fd < 0) {
perror("Failed to open lock file");
exit(1);
}
res = flock(fd, LOCK_EX);
if (res != 0) {
perror("[flock] Failed to grab lock");
} else {
flock(fd, LOCK_UN);
}
lockd.l_type = F_WRLCK;
res = fcntl(fd, F_OFD_GETLK, &lockd);
if (res != 0) {
perror("[fcntl F_OFD_GETLK] Failed to grab lock");
} else {
lockd.l_type = F_UNLCK;
fcntl(fd, F_OFD_SETLK, &lockd);
}
return 0;
}
Using the above program where /tmp/nfs-flock-test
is an NFS mountpoint:
$ ./flock [flock] Failed to grab lock: Invalid argument [fcntl F_OFD_GETLK] Failed to grab lock: Invalid argument
After changes:
$ ./flock [flock] Failed to grab lock: Operation not supported on transport endpoint [fcntl F_OFD_GETLK] Failed to grab lock: Operation not supported on transport endpoint
Updated by Electric Monk 7 months ago
- Status changed from New to Closed
- % Done changed from 0 to 100
git commit b8af4a8966ef2150997d7664836f5c360b849005
commit b8af4a8966ef2150997d7664836f5c360b849005 Author: Luqman Aden <luqman@oxide.computer> Date: 2022-11-21T17:06:50.000Z 13879 flock(3C) on NFS files should fail with EOPNOTSUPP Reviewed by: Andy Stormont <andyjstormont@gmail.com> Approved by: Dan McDonald <danmcd@mnx.io>
Actions