Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 9 Feb 2017 23:36:50 +0000 (UTC)
From:      Konstantin Belousov <kib@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r313496 - head/sys/kern
Message-ID:  <201702092336.v19NaoD2026955@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: kib
Date: Thu Feb  9 23:36:50 2017
New Revision: 313496
URL: https://svnweb.freebsd.org/changeset/base/313496

Log:
  Increase a chance of devfs_close() calling d_close cdevsw method.
  
  If a file opened over a vnode has an advisory lock set at close,
  vn_closefile() acquires additional vnode use reference to prevent
  freeing the vnode in vn_close().  Side effect is that for device
  vnodes, devfs_close() sees that vnode reference count is greater than
  one and refuses to call d_close().  Create internal version of
  vn_close() which can avoid dropping the vnode reference if needed, and
  use this to execute VOP_CLOSE() without acquiring a new reference.
  
  Note that any parallel reference to the vnode would still prevent
  d_close call, if the reference is not from an opened file, e.g. due to
  stat(2).
  
  Tested by:	pho
  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:35:57 2017	(r313495)
+++ head/sys/kern/vfs_vnops.c	Thu Feb  9 23:36:50 2017	(r313496)
@@ -430,12 +430,9 @@ vn_writechk(vp)
 /*
  * Vnode close call
  */
-int
-vn_close(vp, flags, file_cred, td)
-	register struct vnode *vp;
-	int flags;
-	struct ucred *file_cred;
-	struct thread *td;
+static int
+vn_close1(struct vnode *vp, int flags, struct ucred *file_cred,
+    struct thread *td, bool keep_ref)
 {
 	struct mount *mp;
 	int error, lock_flags;
@@ -457,11 +454,22 @@ vn_close(vp, flags, file_cred, td)
 		    __func__, vp, vp->v_writecount);
 	}
 	error = VOP_CLOSE(vp, flags, file_cred, td);
-	vput(vp);
+	if (keep_ref)
+		VOP_UNLOCK(vp, 0);
+	else
+		vput(vp);
 	vn_finished_write(mp);
 	return (error);
 }
 
+int
+vn_close(struct vnode *vp, int flags, struct ucred *file_cred,
+    struct thread *td)
+{
+
+	return (vn_close1(vp, flags, file_cred, td, false));
+}
+
 /*
  * Heuristic to detect sequential operation.
  */
@@ -1573,18 +1581,15 @@ vn_closefile(struct file *fp, struct thr
 	struct vnode *vp;
 	struct flock lf;
 	int error;
+	bool ref;
 
 	vp = fp->f_vnode;
 	fp->f_ops = &badfileops;
+	ref= (fp->f_flag & FHASLOCK) != 0 && fp->f_type == DTYPE_VNODE;
 
-	if (__predict_false((fp->f_flag & FHASLOCK) != 0) &&
-	    fp->f_type == DTYPE_VNODE)
-		vrefact(vp);
-
-	error = vn_close(vp, fp->f_flag, fp->f_cred, td);
+	error = vn_close1(vp, fp->f_flag, fp->f_cred, td, ref);
 
-	if (__predict_false((fp->f_flag & FHASLOCK) != 0) &&
-	    fp->f_type == DTYPE_VNODE) {
+	if (__predict_false(ref)) {
 		lf.l_whence = SEEK_SET;
 		lf.l_start = 0;
 		lf.l_len = 0;



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201702092336.v19NaoD2026955>