From owner-freebsd-hackers@FreeBSD.ORG Wed Jan 21 14:07:59 2009 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id BE3621065831 for ; Wed, 21 Jan 2009 14:07:59 +0000 (UTC) (envelope-from avg@icyb.net.ua) Received: from citadel.icyb.net.ua (citadel.icyb.net.ua [212.40.38.140]) by mx1.freebsd.org (Postfix) with ESMTP id E78968FC23 for ; Wed, 21 Jan 2009 14:07:58 +0000 (UTC) (envelope-from avg@icyb.net.ua) Received: from odyssey.starpoint.kiev.ua (alpha-e.starpoint.kiev.ua [212.40.38.101]) by citadel.icyb.net.ua (8.8.8p3/ICyb-2.3exp) with ESMTP id QAA07055; Wed, 21 Jan 2009 16:07:55 +0200 (EET) (envelope-from avg@icyb.net.ua) Message-ID: <49772C3A.10005@icyb.net.ua> Date: Wed, 21 Jan 2009 16:07:54 +0200 From: Andriy Gapon User-Agent: Thunderbird 2.0.0.18 (X11/20081124) MIME-Version: 1.0 To: Kostik Belousov References: <49770513.8090203@icyb.net.ua> <20090121133520.GK58517@deviant.kiev.zoral.com.ua> <497725C8.4010101@icyb.net.ua> <20090121135510.GL58517@deviant.kiev.zoral.com.ua> In-Reply-To: <20090121135510.GL58517@deviant.kiev.zoral.com.ua> Content-Type: text/plain; charset=KOI8-U Content-Transfer-Encoding: 7bit Cc: freebsd-hackers@freebsd.org Subject: Re: device driver: cdesw questions? X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 21 Jan 2009 14:08:01 -0000 on 21/01/2009 15:55 Kostik Belousov said the following: > On Wed, Jan 21, 2009 at 03:40:24PM +0200, Andriy Gapon wrote: >> on 21/01/2009 15:35 Kostik Belousov said the following: >>> On Wed, Jan 21, 2009 at 01:20:51PM +0200, Andriy Gapon wrote: >>>> Question 1: >>>> I am writing a driver that would use per-open private data (among other >>>> features). >>>> Do I have to use D_TRACKCLOSE flag in this case? >>> No, the dtr registered with devfs_set_cdevpriv(), is called exactly once >>> when the last close is performed, or the device is destroyed. >> Kostik, >> >> thanks a lot for the explanation! >> I am still a little bit confused about the term "last close" - what is >> it? I.e. I'd like to get an answer to the below question. >> >>>> In general I am a little bit confused about when d_close is invoked. >>>> Supposing D_TRACKCLOSE is not set and multiple programs concurrently >>>> open, use and close a device - when d_close is called - when one program >>>> closes its last descriptor tied to the device or when the system-wide >>>> last such descriptor is closed? > > The kernel data structures for the opened device are as follows: > > filedesc ---> struct file ---> vnode ---> cdev > [cdevpriv] \ / > --------->----- > > Each -> arrow represents a "many to one" relation. There may be zero > or one cdevpriv datum associated with struct file. > > cdev maintains the si_usecount, that is an accumulation of the vref > counters for all devfs vnodes that are attached to the cdev. > devfs_close() vop is called when the struct file is released. > When D_TRACKCLOSE is specified, d_close driver method will be > called unconditionally. > When D_TRACKCLOSE is not specified, d_close is called when si_usecount > is exactly 1, to become zero after the last close of the file that > opened a vnode referencing cdev. > Also, d_close() is called if the vnode is being reclaimed. Possible > causes are revoke(2) call or forced devfs mp unmount. This interferes > with close counting. > > cdevpriv dtr is called when either struct file is released, or > device is destroyed by the destroy_dev(). Kostik, is the following true: if D_TRACKCLOSE is specified then a number of d_close() calls and a number of cdevpriv dtr calls are equal (provide each file gets cdevpriv data) ? If not, what is the case where one is called but not the other? -- Andriy Gapon