xref: /netbsd-src/share/man/man9/vnsubr.9 (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1.\"     $NetBSD: vnsubr.9,v 1.45 2017/01/05 09:54:03 wiz Exp $
2.\"
3.\" Copyright (c) 2001, 2005, 2006 The NetBSD Foundation, Inc.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to The NetBSD Foundation
7.\" by Gregory McGarry.
8.\"
9.\" Redistribution and use in source and binary forms, with or without
10.\" modification, are permitted provided that the following conditions
11.\" are met:
12.\" 1. Redistributions of source code must retain the above copyright
13.\"    notice, this list of conditions and the following disclaimer.
14.\" 2. Redistributions in binary form must reproduce the above copyright
15.\"    notice, this list of conditions and the following disclaimer in the
16.\"    documentation and/or other materials provided with the distribution.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28.\" POSSIBILITY OF SUCH DAMAGE.
29.\"
30.Dd January 2, 2017
31.Dt VNSUBR 9
32.Os
33.Sh NAME
34.Nm vnsubr ,
35.Nm vn_bwrite ,
36.Nm vn_close ,
37.Nm vn_default_error ,
38.Nm vn_isunder ,
39.Nm vn_lock ,
40.Nm vn_markexec ,
41.Nm vn_marktext ,
42.Nm vn_rdwr ,
43.Nm vn_open ,
44.Nm vn_stat ,
45.Nm vn_writechk
46.Nd high-level convenience functions for vnode operations
47.Sh SYNOPSIS
48.In sys/param.h
49.In sys/lock.h
50.In sys/vnode.h
51.Ft int
52.Fn vn_bwrite "void *ap"
53.Ft int
54.Fn vn_close "struct vnode *vp" "int flags" "kauth_cred_t cred"
55.Ft int
56.Fn vn_default_error "void *v"
57.Ft int
58.Fn vn_isunder "struct vnode *dvp" "struct vnode *rvp" "struct lwp *l"
59.Ft int
60.Fn vn_lock "struct vnode *vp" "int flags"
61.Ft void
62.Fn vn_markexec "struct vnode *vp"
63.Ft void
64.Fn vn_marktext "struct vnode *vp"
65.Ft int
66.Fn vn_open "struct nameidata *ndp" "int fmode" "int cmode"
67.Ft int
68.Fo vn_rdwr
69.Fa "enum uio_rw rw" "struct vnode *vp" "void *base"
70.Fa "int len" "off_t offset" "enum uio_seg segflg" "int ioflg"
71.Fa "kauth_cred_t cred" "size_t *aresid" "struct lwp *l"
72.Fc
73.Ft int
74.Fn vn_readdir "file_t *fp" "char *buf" "int segflg" "u_int count" "int *done" "struct lwp *l" "off_t **cookies" "int *ncookies"
75.Ft int
76.Fn vn_stat "struct vnode *vp" "struct stat *sb"
77.Ft int
78.Fn vn_writechk "struct vnode *vp"
79.Sh DESCRIPTION
80The high-level functions described in this page are convenience
81functions for simplified access to the vnode operations described in
82.Xr vnodeops 9 .
83.Sh FUNCTIONS
84.Bl -tag -width compact
85.It Fn vn_bwrite "ap"
86Common code for block write operations.
87.It Fn vn_close "vp" "flags" "cred"
88Common code for a vnode close.
89The argument
90.Fa vp
91is the unlocked vnode of the vnode to close.
92.Fn vn_close
93simply locks the vnode, invokes the vnode operation
94.Xr VOP_CLOSE 9
95and calls
96.Fn vput
97to return the vnode to the freelist or holdlist.
98Note that
99.Fn vn_close
100expects an unlocked, referenced vnode and will dereference the vnode
101prior to returning.
102If the operation is successful zero is returned,
103otherwise an appropriate error is returned.
104.It Fn vn_default_error "v"
105A generic "default" routine that just returns error.
106It is used by a file system to specify unsupported operations in
107the vnode operations vector.
108.It Fn vn_isunder "dvp" "rvp" "l"
109Common code to check if one directory specified by the vnode
110.Fa rvp
111can be found inside the directory specified by the vnode
112.Fa dvp .
113The argument
114.Fa l
115is the calling process.
116.Fn vn_isunder
117is intended to be used in
118.Xr chroot 2 ,
119.Xr chdir 2 ,
120.Xr fchdir 2 ,
121etc., to ensure that
122.Xr chroot 2
123actually means something.
124If the operation is successful zero is returned, otherwise 1 is returned.
125.It Fn vn_lock "vp" "flags"
126Common code to acquire the lock for vnode
127.Fa vp .
128The argument
129.Fa flags
130specifies the flags used to lock the vnode.
131There are the following flags:
132.Pp
133.Bl -tag -offset indent -width LK_EXCLUSIVE -compact
134.It LK_SHARED
135shared lock
136.It LK_EXCLUSIVE
137exclusive lock
138.It LK_NOWAIT
139do not sleep to await lock
140.It LK_RETRY
141allow lock operation on dead vnode
142.El
143.Pp
144If the operation is successful zero is returned, otherwise an
145appropriate error code is returned.
146.Pp
147.Fn vn_lock
148must not be called when the vnode's reference count is zero.
149.It Fn vn_markexec "vp"
150Common code to mark the vnode
151.Fa vp
152as containing executable code of a running process.
153.It Fn vn_marktext "vp"
154Common code to mark the vnode
155.Fa vp
156as being the text of a running process.
157.It Fn vn_open "ndp" "fmode" "cmode"
158Common code for vnode open operations.
159The pathname is described in the nameidata pointer (see
160.Xr namei 9 ) .
161The arguments
162.Fa fmode
163and
164.Fa cmode
165specify the
166.Xr open 2
167file mode and the access permissions for creation.
168.Fn vn_open
169checks  permissions and invokes the
170.Xr VOP_OPEN 9
171or
172.Xr VOP_CREATE 9
173vnode operations.
174If the operation is successful zero is returned and the vnode is locked,
175otherwise an appropriate error code is returned.
176.It Fn vn_rdwr "rw" "vp" "base" "len" "offset" "segflg" "ioflg" "cred" "aresid" "l"
177Common code to package up an I/O request on a vnode into a uio and
178then perform the I/O.
179The argument
180.Fa rw
181specifies whether the I/O is a read
182.Dv ( UIO_READ )
183or write
184.Dv ( UIO_WRITE )
185operation.
186The vnode is specified by
187.Fa vp .
188The arguments
189.Fa l
190and
191.Fa cred
192are the calling lwp and its credentials.
193If
194.Fa ioflg
195contains
196.Dv IO_NODELOCKED ,
197it is expected that the vnode is locked.
198.Fa ioflg
199will be passed to
200.Fn VOP_READ Ns No / Ns Fn VOP_WRITE .
201The remaining arguments specify the uio parameters.
202For further information on these parameters see
203.Xr uiomove 9 .
204.It Fn vn_readdir "fp" "buf" "segflg" "count" "done" "l" "cookies" "ncookies"
205Common code for reading the contents of a directory.
206The argument
207.Fa fp
208is the file structure,
209.Fa buf
210is the buffer for placing the struct dirent structures.
211The arguments
212.Fa cookies
213and
214.Fa ncookies
215specify the addresses for the list and number of directory seek
216cookies generated for NFS.
217Both
218.Fa cookies
219and
220.Fa ncookies
221should be
222.Dv NULL
223if they aren't required to be returned by
224.Fn vn_readdir .
225If the operation is successful zero is returned, otherwise an
226appropriate error code is returned.
227.It Fn vn_stat "vp" "sb"
228Common code for a vnode stat operation.
229The vnode is specified by the argument
230.Fa vp ,
231and
232.Fa sb
233is the buffer to return the stat information.
234.Fn vn_stat
235basically calls the vnode operation
236.Xr VOP_GETATTR 9
237and transfers the contents of a vattr structure into a struct stat.
238If the operation is successful zero is returned, otherwise an
239appropriate error code is returned.
240.It Fn vn_writechk "vp"
241Common code to check for write permission on the vnode
242.Fa vp .
243A vnode is read-only if it is in use as a process's text image.
244If the vnode is read-only ETEXTBSY is returned, otherwise zero is
245returned to indicate that the vnode can be written to.
246.El
247.Sh ERRORS
248.Bl -tag -width Er
249.It Bq Er EBUSY
250The LK_NOWAIT flag was set and
251.Fn vn_lock
252would have slept.
253.It Bq Er ENOENT
254The vnode has been reclaimed and is dead.
255This error is only returned if the LK_RETRY flag is not passed to
256.Fn vn_lock .
257.It Bq Er ETXTBSY
258Cannot write to a vnode since is a process's text image.
259.El
260.Sh CODE REFERENCES
261The high-level convenience functions are implemented within the files
262.Pa sys/kern/vfs_vnops.c
263and
264.Pa sys/sys/vnode.h .
265.Sh SEE ALSO
266.Xr file 9 ,
267.Xr intro 9 ,
268.Xr lock 9 ,
269.Xr namei 9 ,
270.Xr vattr 9 ,
271.Xr vfs 9 ,
272.Xr vnode 9 ,
273.Xr vnodeops 9
274