From owner-freebsd-hackers@FreeBSD.ORG Wed Jan 21 14:14:04 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 E402110657C3 for ; Wed, 21 Jan 2009 14:14:04 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from mail.terabit.net.ua (mail.terabit.net.ua [195.137.202.147]) by mx1.freebsd.org (Postfix) with ESMTP id 82C9D8FC26 for ; Wed, 21 Jan 2009 14:14:04 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from skuns.zoral.com.ua ([91.193.166.194] helo=mail.zoral.com.ua) by mail.terabit.net.ua with esmtps (TLSv1:AES256-SHA:256) (Exim 4.63 (FreeBSD)) (envelope-from ) id 1LPdqF-000BSZ-1S; Wed, 21 Jan 2009 16:14:03 +0200 Received: from deviant.kiev.zoral.com.ua (root@deviant.kiev.zoral.com.ua [10.1.1.148]) by mail.zoral.com.ua (8.14.2/8.14.2) with ESMTP id n0LEDuKb011883 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Wed, 21 Jan 2009 16:13:56 +0200 (EET) (envelope-from kostikbel@gmail.com) Received: from deviant.kiev.zoral.com.ua (kostik@localhost [127.0.0.1]) by deviant.kiev.zoral.com.ua (8.14.3/8.14.3) with ESMTP id n0LEDuMA023952; Wed, 21 Jan 2009 16:13:56 +0200 (EET) (envelope-from kostikbel@gmail.com) Received: (from kostik@localhost) by deviant.kiev.zoral.com.ua (8.14.3/8.14.3/Submit) id n0LEDuXI023950; Wed, 21 Jan 2009 16:13:56 +0200 (EET) (envelope-from kostikbel@gmail.com) X-Authentication-Warning: deviant.kiev.zoral.com.ua: kostik set sender to kostikbel@gmail.com using -f Date: Wed, 21 Jan 2009 16:13:55 +0200 From: Kostik Belousov To: Andriy Gapon Message-ID: <20090121141355.GM58517@deviant.kiev.zoral.com.ua> 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> <49772C3A.10005@icyb.net.ua> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="m+utxuhC6KVTvgNz" Content-Disposition: inline In-Reply-To: <49772C3A.10005@icyb.net.ua> User-Agent: Mutt/1.4.2.3i X-Virus-Scanned: ClamAV version 0.94.2, clamav-milter version 0.94.2 on skuns.kiev.zoral.com.ua X-Virus-Status: Clean X-Spam-Status: No, score=-4.4 required=5.0 tests=ALL_TRUSTED,AWL,BAYES_00 autolearn=ham version=3.2.5 X-Spam-Checker-Version: SpamAssassin 3.2.5 (2008-06-10) on skuns.kiev.zoral.com.ua X-Virus-Scanned: mail.terabit.net.ua 1LPdqF-000BSZ-1S f555fa7860a3dfe1788a8fb7382ee76b X-Terabit: YES 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:14:08 -0000 --m+utxuhC6KVTvgNz Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Wed, Jan 21, 2009 at 04:07:54PM +0200, Andriy Gapon wrote: > 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 ot= her > >>>> features). > >>>> Do I have to use D_TRACKCLOSE flag in this case? > >>> No, the dtr registered with devfs_set_cdevpriv(), is called exactly o= nce > >>> 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 pro= gram > >>>> closes its last descriptor tied to the device or when the system-wide > >>>> last such descriptor is closed? > >=20 > > The kernel data structures for the opened device are as follows: > >=20 > > filedesc ---> struct file ---> vnode ---> cdev > > [cdevpriv] \ / =20 > > --------->----- > >=20 > > Each -> arrow represents a "many to one" relation. There may be zero > > or one cdevpriv datum associated with struct file. > >=20 > > 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. > >=20 > > cdevpriv dtr is called when either struct file is released, or > > device is destroyed by the destroy_dev(). >=20 > Kostik, >=20 > 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? No, I already described this. See the note about vnode reclamation. Also, d_close counter would have an interacation with destroy_dev(). To give you short summary, for D_TRACKCLOSE, d_close() may be called less times then dtr. --m+utxuhC6KVTvgNz Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (FreeBSD) iEYEARECAAYFAkl3LaMACgkQC3+MBN1Mb4gFGgCgoQSl8YQZJMQww5RIiam7k5aV MY8AniVnLokljoW1W0ksV8wxqC/n6iYT =T06g -----END PGP SIGNATURE----- --m+utxuhC6KVTvgNz--