164aa915fSConrad Meyer.\" Copyright (c) 2018 Conrad Meyer <cem@FreeBSD.org> 264aa915fSConrad Meyer.\" All rights reserved. 364aa915fSConrad Meyer.\" 464aa915fSConrad Meyer.\" Redistribution and use in source and binary forms, with or without 564aa915fSConrad Meyer.\" modification, are permitted provided that the following conditions 664aa915fSConrad Meyer.\" are met: 764aa915fSConrad Meyer.\" 1. Redistributions of source code must retain the above copyright 864aa915fSConrad Meyer.\" notice, this list of conditions and the following disclaimer. 964aa915fSConrad Meyer.\" 2. Redistributions in binary form must reproduce the above copyright 1064aa915fSConrad Meyer.\" notice, this list of conditions and the following disclaimer in the 1164aa915fSConrad Meyer.\" documentation and/or other materials provided with the distribution. 1264aa915fSConrad Meyer.\" 1364aa915fSConrad Meyer.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1464aa915fSConrad Meyer.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1564aa915fSConrad Meyer.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1664aa915fSConrad Meyer.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1764aa915fSConrad Meyer.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1864aa915fSConrad Meyer.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 1964aa915fSConrad Meyer.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2064aa915fSConrad Meyer.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2164aa915fSConrad Meyer.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2264aa915fSConrad Meyer.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2364aa915fSConrad Meyer.\" SUCH DAMAGE. 2464aa915fSConrad Meyer.\" 2564aa915fSConrad Meyer.Dd August 29, 2018 2664aa915fSConrad Meyer.Dt DEV_REFTHREAD 9 2764aa915fSConrad Meyer.Os 2864aa915fSConrad Meyer.Sh NAME 2964aa915fSConrad Meyer.Nm dev_refthread , 3064aa915fSConrad Meyer.Nm devvn_refthread , 3164aa915fSConrad Meyer.Nm dev_relthread 3264aa915fSConrad Meyer.Nd safely access device methods 3364aa915fSConrad Meyer.Sh SYNOPSIS 3464aa915fSConrad Meyer.In sys/param.h 3564aa915fSConrad Meyer.In sys/conf.h 3664aa915fSConrad Meyer.Ft "struct cdevsw *" 3764aa915fSConrad Meyer.Fn dev_refthread "struct cdev *dev" "int *ref" 3864aa915fSConrad Meyer.Ft "struct cdevsw *" 3964aa915fSConrad Meyer.Fn devvn_refthread "struct vnode *vp" "struct cdev **devp" "int *ref" 4064aa915fSConrad Meyer.Ft void 4164aa915fSConrad Meyer.Fn dev_relthread "struct cdev *dev" "int ref" 4264aa915fSConrad Meyer.Sh DESCRIPTION 4364aa915fSConrad MeyerThe 4464aa915fSConrad Meyer.Fn dev_refthread 4564aa915fSConrad Meyer(or 4664aa915fSConrad Meyer.Fn devvn_refthread ) 4764aa915fSConrad Meyerand 4864aa915fSConrad Meyer.Fn dev_relthread 4964aa915fSConrad Meyerroutines provide a safe way to access 50*1a720cbeSAlexander Ziaee.Xr devfs 4 5164aa915fSConrad Meyerdevices that may be concurrently destroyed by 5264aa915fSConrad Meyer.Fn destroy_dev 5364aa915fSConrad Meyer(e.g., removable media). 5464aa915fSConrad Meyer.Pp 5564aa915fSConrad MeyerIf successful, 5664aa915fSConrad Meyer.Fn dev_refthread 5764aa915fSConrad Meyerand 5864aa915fSConrad Meyer.Fn devvn_refthread 5964aa915fSConrad Meyeracquire a "thread reference" to the associated 6064aa915fSConrad Meyer.Vt "struct cdev" 6164aa915fSConrad Meyerand return a non-NULL pointer to the cdev's 6264aa915fSConrad Meyer.Vt "struct cdevsw" 6364aa915fSConrad Meyermethod table. 6464aa915fSConrad MeyerFor the duration of that reference, the cdev's associated private data and 6564aa915fSConrad Meyermethod table object are valid. 6664aa915fSConrad MeyerDestruction of the cdev sleeps until the thread reference is released. 6764aa915fSConrad Meyer.Pp 6864aa915fSConrad MeyerA reference cannot prevent media removal. 6964aa915fSConrad MeyerIt is an implementation detail of individual drivers how method calls from 7064aa915fSConrad Meyercallers with 7164aa915fSConrad Meyer.Fn dev_refthread 7264aa915fSConrad Meyerreferences are handled when the device is 7364aa915fSConrad Meyerpending destruction. 7464aa915fSConrad MeyerA common behavior for disk devices is to return the 7564aa915fSConrad Meyer.Er ENXIO 7664aa915fSConrad Meyerstatus, but that is not required by this KPI. 7764aa915fSConrad Meyer.Pp 7864aa915fSConrad MeyerThe 7964aa915fSConrad Meyer.Fn devvn_refthread 8064aa915fSConrad Meyervariant of 8164aa915fSConrad Meyer.Fn dev_refthread 8264aa915fSConrad Meyerextracts the 8364aa915fSConrad Meyer.Vt "struct cdev" 8464aa915fSConrad Meyerpointer out of the 8564aa915fSConrad Meyer.Dv VCHR 8664aa915fSConrad Meyer.Xr vnode 9 8764aa915fSConrad Meyerautomatically before performing the same actions as 8864aa915fSConrad Meyer.Fn dev_refthread . 8964aa915fSConrad MeyerAdditionally, a pointer to the 9064aa915fSConrad Meyer.Vt "struct cdev" 9164aa915fSConrad Meyeris returned to the caller via 9264aa915fSConrad Meyer.Fa "*devp" . 9364aa915fSConrad Meyer.Fn devvn_refthread 9464aa915fSConrad Meyercorrectly handles possible parallel reclamation of the vnode. 9564aa915fSConrad Meyer.Pp 9664aa915fSConrad Meyer.Fn dev_relthread 9764aa915fSConrad Meyeris used to release a reference to a 9864aa915fSConrad Meyer.Vt "struct cdev" . 9964aa915fSConrad Meyer.Fn dev_relthread 10064aa915fSConrad Meyer.Sy must 10164aa915fSConrad Meyeronly be invoked when the associated invocation of 10264aa915fSConrad Meyer.Fn dev_refthread 10364aa915fSConrad Meyeror 10464aa915fSConrad Meyer.Fn devvn_refthread 10564aa915fSConrad Meyerreturned a non-NULL 10664aa915fSConrad Meyer.Vt "struct cdevsw *" . 10764aa915fSConrad Meyer.Sh CONTEXT 10864aa915fSConrad Meyer.Vt struct cdev 10964aa915fSConrad Meyerobjects have two reference counts, 11064aa915fSConrad Meyer.Va si_refcount 11164aa915fSConrad Meyerand 11264aa915fSConrad Meyer.Va si_threadcount . 11364aa915fSConrad MeyerThe 11464aa915fSConrad Meyer.Fn dev_refthread , 11564aa915fSConrad Meyer.Fn devvn_refthread , 11664aa915fSConrad Meyerand 11764aa915fSConrad Meyer.Fn dev_relthread 11864aa915fSConrad Meyerfunctions manipulate the 11964aa915fSConrad Meyer.Va si_threadcount . 12064aa915fSConrad MeyerThe 12164aa915fSConrad Meyer.Va si_threadcount 12264aa915fSConrad Meyerreference guarantees the liveness of the 12364aa915fSConrad Meyer.Vt struct cdev 12464aa915fSConrad Meyerobject. 12564aa915fSConrad MeyerThe other 12664aa915fSConrad Meyer.Va si_refcount 12764aa915fSConrad Meyerreference provides only the weaker guarantee that the memory backing the 12864aa915fSConrad Meyer.Vt struct cdev 12964aa915fSConrad Meyerhas not been freed. 13064aa915fSConrad Meyer.Sh RETURN VALUES 13164aa915fSConrad MeyerIf 13264aa915fSConrad Meyer.Fn dev_refthread 13364aa915fSConrad Meyeror 13464aa915fSConrad Meyer.Fn devvn_refthread 13564aa915fSConrad Meyerare unsuccessful, they return 13664aa915fSConrad Meyer.Dv NULL . 13764aa915fSConrad Meyer.Bf Em 13864aa915fSConrad MeyerIf these routines are unsuccessful, they do not increment the 13964aa915fSConrad Meyer.Vt "struct cdev" 14064aa915fSConrad Meyer.Va si_threadcount 14164aa915fSConrad Meyerand do not initialize the value pointed to by the 14264aa915fSConrad Meyer.Fa "*ref" 14364aa915fSConrad Meyerparameter in any way. 14464aa915fSConrad Meyer.Ef 14564aa915fSConrad Meyer.Sh SEE ALSO 146*1a720cbeSAlexander Ziaee.Xr devfs 4 , 14732068667SChristian Brueffer.Xr destroy_dev 9 14864aa915fSConrad Meyer.Sh CAVEATS 14964aa915fSConrad MeyerDo not invoke 15064aa915fSConrad Meyer.Fn dev_relthread 15164aa915fSConrad Meyerunless the matching refthread routine succeeded! 152