Do not establish advisory locks when doing open(O_EXLOCK) or open(O_SHLOCK)

for files which do not have DTYPE_VNODE type.

Both flock(2) and fcntl(2) syscalls refuse to acquire advisory lock on
a file which type is not DTYPE_VNODE.  Do the same when lock is
requested from open(2).

Restructure the block in vn_open_vnode() which handles O_EXLOCK and
O_SHLOCK open flags to make it easier to quit its execution earlier
with an error.

Tested by:	pho (previous version)
Sponsored by:	The FreeBSD Foundation
MFC after:	2 weeks
This commit is contained in:
Konstantin Belousov 2017-02-09 23:35:57 +00:00
parent 6d20836aa7
commit 7903b00087
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=313495

View File

@ -349,8 +349,12 @@ vn_open_vnode(struct vnode *vp, int fmode, struct ucred *cred,
if ((error = VOP_OPEN(vp, fmode, cred, td, fp)) != 0)
return (error);
if (fmode & (O_EXLOCK | O_SHLOCK)) {
while ((fmode & (O_EXLOCK | O_SHLOCK)) != 0) {
KASSERT(fp != NULL, ("open with flock requires fp"));
if (fp->f_type != DTYPE_VNODE) {
error = EBADF;
break;
}
lock_flags = VOP_ISLOCKED(vp);
VOP_UNLOCK(vp, 0);
lf.l_whence = SEEK_SET;
@ -367,8 +371,12 @@ vn_open_vnode(struct vnode *vp, int fmode, struct ucred *cred,
if (error == 0)
fp->f_flag |= FHASLOCK;
vn_lock(vp, lock_flags | LK_RETRY);
if (error == 0 && vp->v_iflag & VI_DOOMED)
if (error != 0)
break;
if ((vp->v_iflag & VI_DOOMED) != 0) {
error = ENOENT;
break;
}
/*
* Another thread might have used this vnode as an
@ -376,20 +384,20 @@ vn_open_vnode(struct vnode *vp, int fmode, struct ucred *cred,
* Ensure the vnode is still able to be opened for
* writing after the lock has been obtained.
*/
if (error == 0 && accmode & VWRITE)
if ((accmode & VWRITE) != 0)
error = vn_writechk(vp);
if (error != 0) {
fp->f_flag |= FOPENFAILED;
fp->f_vnode = vp;
if (fp->f_ops == &badfileops) {
fp->f_type = DTYPE_VNODE;
fp->f_ops = &vnops;
}
vref(vp);
}
break;
}
if (error == 0 && fmode & FWRITE) {
if (error != 0) {
fp->f_flag |= FOPENFAILED;
fp->f_vnode = vp;
if (fp->f_ops == &badfileops) {
fp->f_type = DTYPE_VNODE;
fp->f_ops = &vnops;
}
vref(vp);
} else if ((fmode & FWRITE) != 0) {
VOP_ADD_WRITECOUNT(vp, 1);
CTR3(KTR_VFS, "%s: vp %p v_writecount increased to %d",
__func__, vp, vp->v_writecount);