Author: kib
Date: Thu Feb  9 23:35:57 2017
New Revision: 313495
URL: https://svnweb.freebsd.org/changeset/base/313495

Log:
  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

Modified:
  head/sys/kern/vfs_vnops.c

Modified: head/sys/kern/vfs_vnops.c
==============================================================================
--- head/sys/kern/vfs_vnops.c   Thu Feb  9 23:33:06 2017        (r313494)
+++ head/sys/kern/vfs_vnops.c   Thu Feb  9 23:35:57 2017        (r313495)
@@ -349,8 +349,12 @@ vn_open_vnode(struct vnode *vp, int fmod
        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 fmod
                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 fmod
                 * 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);
+               break;
+       }
 
-               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);
+       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;
                }
-       }
-       if (error == 0 && fmode & FWRITE) {
+               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);
_______________________________________________
svn-src-all@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscr...@freebsd.org"

Reply via email to