1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
<table class="head">
<tr>
<td class="head-ltitle">DEV_REFTHREAD(9)</td>
<td class="head-vol">Kernel Developer's Manual</td>
<td class="head-rtitle">DEV_REFTHREAD(9)</td>
</tr>
</table>
<div class="manual-text">
<section class="Sh">
<h1 class="Sh" id="NAME"><a class="permalink" href="#NAME">NAME</a></h1>
<p class="Pp"><code class="Nm">dev_refthread</code>,
<code class="Nm">devvn_refthread</code>,
<code class="Nm">dev_relthread</code> — <span class="Nd">safely
access device methods</span></p>
</section>
<section class="Sh">
<h1 class="Sh" id="SYNOPSIS"><a class="permalink" href="#SYNOPSIS">SYNOPSIS</a></h1>
<p class="Pp"><code class="In">#include
<<a class="In">sys/param.h</a>></code>
<br/>
<code class="In">#include <<a class="In">sys/conf.h</a>></code></p>
<p class="Pp"><var class="Ft">struct cdevsw *</var>
<br/>
<code class="Fn">dev_refthread</code>(<var class="Fa" style="white-space: nowrap;">struct
cdev *dev</var>, <var class="Fa" style="white-space: nowrap;">int
*ref</var>);</p>
<p class="Pp"><var class="Ft">struct cdevsw *</var>
<br/>
<code class="Fn">devvn_refthread</code>(<var class="Fa" style="white-space: nowrap;">struct
vnode *vp</var>, <var class="Fa" style="white-space: nowrap;">struct cdev
**devp</var>, <var class="Fa" style="white-space: nowrap;">int
*ref</var>);</p>
<p class="Pp"><var class="Ft">void</var>
<br/>
<code class="Fn">dev_relthread</code>(<var class="Fa" style="white-space: nowrap;">struct
cdev *dev</var>, <var class="Fa" style="white-space: nowrap;">int
ref</var>);</p>
</section>
<section class="Sh">
<h1 class="Sh" id="DESCRIPTION"><a class="permalink" href="#DESCRIPTION">DESCRIPTION</a></h1>
<p class="Pp">The
<a class="permalink" href="#dev_refthread"><code class="Fn" id="dev_refthread">dev_refthread</code></a>()
(or <code class="Fn">devvn_refthread</code>()) and
<code class="Fn">dev_relthread</code>() routines provide a safe way to
access <a class="Xr">devfs(4)</a> devices that may be concurrently destroyed
by
<a class="permalink" href="#destroy_dev"><code class="Fn" id="destroy_dev">destroy_dev</code></a>()
(e.g., removable media).</p>
<p class="Pp" id="dev_refthread~2">If successful,
<a class="permalink" href="#dev_refthread~2"><code class="Fn">dev_refthread</code></a>()
and <code class="Fn">devvn_refthread</code>() acquire a "thread
reference" to the associated <var class="Vt">struct cdev</var> and
return a non-NULL pointer to the cdev's <var class="Vt">struct cdevsw</var>
method table. For the duration of that reference, the cdev's associated
private data and method table object are valid. Destruction of the cdev
sleeps until the thread reference is released.</p>
<p class="Pp" id="dev_refthread~3">A reference cannot prevent media removal. It
is an implementation detail of individual drivers how method calls from
callers with
<a class="permalink" href="#dev_refthread~3"><code class="Fn">dev_refthread</code></a>()
references are handled when the device is pending destruction. A common
behavior for disk devices is to return the <code class="Er">ENXIO</code>
status, but that is not required by this KPI.</p>
<p class="Pp" id="devvn_refthread">The
<a class="permalink" href="#devvn_refthread"><code class="Fn">devvn_refthread</code></a>()
variant of <code class="Fn">dev_refthread</code>() extracts the
<var class="Vt">struct cdev</var> pointer out of the
<code class="Dv">VCHR</code> <a class="Xr">vnode(9)</a> automatically before
performing the same actions as <code class="Fn">dev_refthread</code>().
Additionally, a pointer to the <var class="Vt">struct cdev</var> is returned
to the caller via <var class="Fa">*devp</var>.
<code class="Fn">devvn_refthread</code>() correctly handles possible
parallel reclamation of the vnode.</p>
<p class="Pp" id="dev_relthread"><a class="permalink" href="#dev_relthread"><code class="Fn">dev_relthread</code></a>()
is used to release a reference to a <var class="Vt">struct cdev</var>.
<code class="Fn">dev_relthread</code>()
<a class="permalink" href="#must"><b class="Sy" id="must">must</b></a> only
be invoked when the associated invocation of
<code class="Fn">dev_refthread</code>() or
<code class="Fn">devvn_refthread</code>() returned a non-NULL
<var class="Vt">struct cdevsw *</var>.</p>
</section>
<section class="Sh">
<h1 class="Sh" id="CONTEXT"><a class="permalink" href="#CONTEXT">CONTEXT</a></h1>
<p class="Pp"><var class="Vt">struct cdev</var> objects have two reference
counts, <var class="Va">si_refcount</var> and
<var class="Va">si_threadcount</var>. The
<code class="Fn">dev_refthread</code>(),
<code class="Fn">devvn_refthread</code>(), and
<code class="Fn">dev_relthread</code>() functions manipulate the
<var class="Va">si_threadcount</var>. The
<var class="Va">si_threadcount</var> reference guarantees the liveness of
the <var class="Vt">struct cdev</var> object. The other
<var class="Va">si_refcount</var> reference provides only the weaker
guarantee that the memory backing the <var class="Vt">struct cdev</var> has
not been freed.</p>
</section>
<section class="Sh">
<h1 class="Sh" id="RETURN_VALUES"><a class="permalink" href="#RETURN_VALUES">RETURN
VALUES</a></h1>
<p class="Pp">If <code class="Fn">dev_refthread</code>() or
<code class="Fn">devvn_refthread</code>() are unsuccessful, they return
<code class="Dv">NULL</code>.</p>
<div class="Bf Em">If these routines are unsuccessful, they do not increment the
<var class="Vt">struct cdev</var> <var class="Va">si_threadcount</var> and do
not initialize the value pointed to by the <var class="Fa">*ref</var>
parameter in any way.</div>
</section>
<section class="Sh">
<h1 class="Sh" id="SEE_ALSO"><a class="permalink" href="#SEE_ALSO">SEE
ALSO</a></h1>
<p class="Pp"><a class="Xr">devfs(4)</a>, <a class="Xr">destroy_dev(9)</a></p>
</section>
<section class="Sh">
<h1 class="Sh" id="CAVEATS"><a class="permalink" href="#CAVEATS">CAVEATS</a></h1>
<p class="Pp">Do not invoke <code class="Fn">dev_relthread</code>() unless the
matching refthread routine succeeded!</p>
</section>
</div>
<table class="foot">
<tr>
<td class="foot-date">August 29, 2018</td>
<td class="foot-os">FreeBSD 15.0</td>
</tr>
</table>
|