xref: /dflybsd-src/share/man/man9/VOP_READDIR.9 (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
1*86d7f5d3SJohn Marino.\" Copyright (c) 1996 Doug Rabson
2*86d7f5d3SJohn Marino.\"
3*86d7f5d3SJohn Marino.\" All rights reserved.
4*86d7f5d3SJohn Marino.\"
5*86d7f5d3SJohn Marino.\" This program is free software.
6*86d7f5d3SJohn Marino.\"
7*86d7f5d3SJohn Marino.\" Redistribution and use in source and binary forms, with or without
8*86d7f5d3SJohn Marino.\" modification, are permitted provided that the following conditions
9*86d7f5d3SJohn Marino.\" are met:
10*86d7f5d3SJohn Marino.\" 1. Redistributions of source code must retain the above copyright
11*86d7f5d3SJohn Marino.\"    notice, this list of conditions and the following disclaimer.
12*86d7f5d3SJohn Marino.\" 2. Redistributions in binary form must reproduce the above copyright
13*86d7f5d3SJohn Marino.\"    notice, this list of conditions and the following disclaimer in the
14*86d7f5d3SJohn Marino.\"    documentation and/or other materials provided with the distribution.
15*86d7f5d3SJohn Marino.\"
16*86d7f5d3SJohn Marino.\" THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR
17*86d7f5d3SJohn Marino.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18*86d7f5d3SJohn Marino.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19*86d7f5d3SJohn Marino.\" IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT,
20*86d7f5d3SJohn Marino.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21*86d7f5d3SJohn Marino.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22*86d7f5d3SJohn Marino.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23*86d7f5d3SJohn Marino.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24*86d7f5d3SJohn Marino.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25*86d7f5d3SJohn Marino.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*86d7f5d3SJohn Marino.\"
27*86d7f5d3SJohn Marino.\" $FreeBSD: src/share/man/man9/VOP_READDIR.9,v 1.6.2.1 2001/12/17 11:30:18 ru Exp $
28*86d7f5d3SJohn Marino.\"
29*86d7f5d3SJohn Marino.Dd July 24, 1996
30*86d7f5d3SJohn Marino.Dt VOP_READDIR 9
31*86d7f5d3SJohn Marino.Os
32*86d7f5d3SJohn Marino.Sh NAME
33*86d7f5d3SJohn Marino.Nm VOP_READDIR
34*86d7f5d3SJohn Marino.Nd read contents of a directory
35*86d7f5d3SJohn Marino.Sh SYNOPSIS
36*86d7f5d3SJohn Marino.In sys/param.h
37*86d7f5d3SJohn Marino.In sys/dirent.h
38*86d7f5d3SJohn Marino.In sys/vnode.h
39*86d7f5d3SJohn Marino.Ft int
40*86d7f5d3SJohn Marino.Fn VOP_READDIR "struct vnode *vp" "struct uio *uio" "struct ucred *cred" "int *eofflag" "int *ncookies" "u_long **cookies"
41*86d7f5d3SJohn Marino.Sh DESCRIPTION
42*86d7f5d3SJohn MarinoRead directory entries.
43*86d7f5d3SJohn Marino.Bl -tag -width ncookies
44*86d7f5d3SJohn Marino.It Fa vp
45*86d7f5d3SJohn Marinothe vnode of the directory
46*86d7f5d3SJohn Marino.It Fa uio
47*86d7f5d3SJohn Marinowhere to read the directory contents
48*86d7f5d3SJohn Marino.It Fa cred
49*86d7f5d3SJohn Marinothe caller's credentials
50*86d7f5d3SJohn Marino.It Fa eofflag
51*86d7f5d3SJohn Marinoreturn end of file status (NULL if not wanted)
52*86d7f5d3SJohn Marino.It Fa ncookies
53*86d7f5d3SJohn Marinonumber of directory cookies generated for NFS (NULL if not wanted)
54*86d7f5d3SJohn Marino.It Fa cookies
55*86d7f5d3SJohn Marinodirectory seek cookies generated for NFS (NULL if not wanted)
56*86d7f5d3SJohn Marino.El
57*86d7f5d3SJohn MarinoThe directory contents are read into
58*86d7f5d3SJohn Marino.Vt struct dirent
59*86d7f5d3SJohn Marinostructures.  If the on-disc data structures differ from this then they
60*86d7f5d3SJohn Marinoshould be translated.
61*86d7f5d3SJohn Marino.Sh LOCKS
62*86d7f5d3SJohn MarinoThe directory should be locked on entry and will still be locked on exit.
63*86d7f5d3SJohn Marino.Sh RETURN VALUES
64*86d7f5d3SJohn MarinoZero is returned on success, otherwise an error code is returned.
65*86d7f5d3SJohn Marino.Pp
66*86d7f5d3SJohn MarinoIf this is called from the NFS server, the extra arguments
67*86d7f5d3SJohn Marino.Fa eofflag ,
68*86d7f5d3SJohn Marino.Fa ncookies
69*86d7f5d3SJohn Marinoand
70*86d7f5d3SJohn Marino.Fa cookies
71*86d7f5d3SJohn Marinoare given.
72*86d7f5d3SJohn MarinoThe value of
73*86d7f5d3SJohn Marino.Fa *eofflag
74*86d7f5d3SJohn Marinoshould be set to TRUE if the end of the directory is reached while
75*86d7f5d3SJohn Marinoreading.
76*86d7f5d3SJohn MarinoThe directory seek cookies are returned to the NFS client and may be used
77*86d7f5d3SJohn Marinolater to restart a directory read part way through the directory.
78*86d7f5d3SJohn MarinoThere should be one cookie returned per directory entry.  The value of
79*86d7f5d3SJohn Marinothe cookie should be the offset within the directory where the on-disc
80*86d7f5d3SJohn Marinoversion of the appropriate directory entry starts.
81*86d7f5d3SJohn MarinoMemory for the cookies should be allocated using:
82*86d7f5d3SJohn Marino.Bd -literal
83*86d7f5d3SJohn Marino	...;
84*86d7f5d3SJohn Marino	*ncookies = number of entries read;
85*86d7f5d3SJohn Marino	*cookies = (u_int*)#
86*86d7f5d3SJohn Marino		kmalloc(*ncookies * sizeof(u_int), M_TEMP, M_WAITOK);
87*86d7f5d3SJohn Marino.Ed
88*86d7f5d3SJohn Marino.Sh PSEUDOCODE
89*86d7f5d3SJohn Marino.Bd -literal
90*86d7f5d3SJohn Marinoint
91*86d7f5d3SJohn Marinovop_readdir(struct vnode *vp, struct uio *uio, struct ucred *cred,
92*86d7f5d3SJohn Marino	    int *eofflag, int *ncookies, u_int **cookies)
93*86d7f5d3SJohn Marino{
94*86d7f5d3SJohn Marino    off_t off;
95*86d7f5d3SJohn Marino    int error = 0;
96*86d7f5d3SJohn Marino
97*86d7f5d3SJohn Marino    /*
98*86d7f5d3SJohn Marino     * Remember the original offset to use later in generating cookies.
99*86d7f5d3SJohn Marino     */
100*86d7f5d3SJohn Marino    off = uio->uio_offset;
101*86d7f5d3SJohn Marino
102*86d7f5d3SJohn Marino    /*
103*86d7f5d3SJohn Marino     * Read directory contents starting at uio->uio_offset into buffer
104*86d7f5d3SJohn Marino     * pointed to by uio.
105*86d7f5d3SJohn Marino     */
106*86d7f5d3SJohn Marino    ...;
107*86d7f5d3SJohn Marino
108*86d7f5d3SJohn Marino    if (!error && ncookies != NULL) {
109*86d7f5d3SJohn Marino	struct dirent *dpStart;
110*86d7f5d3SJohn Marino	struct dirent *dpEnd;
111*86d7f5d3SJohn Marino	struct dirent *dp;
112*86d7f5d3SJohn Marino	int count;
113*86d7f5d3SJohn Marino	u_int *cookiebuf;
114*86d7f5d3SJohn Marino	u_int *cookiep;
115*86d7f5d3SJohn Marino
116*86d7f5d3SJohn Marino	if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
117*86d7f5d3SJohn Marino	    panic("vop_readdir: unexpected uio from NFS server");
118*86d7f5d3SJohn Marino
119*86d7f5d3SJohn Marino	/*
120*86d7f5d3SJohn Marino	 * Parse the stuff just read into the uio.
121*86d7f5d3SJohn Marino	 */
122*86d7f5d3SJohn Marino	dpStart = (struct dirent *)
123*86d7f5d3SJohn Marino	    (uio->uio_iov->iov_base - (uio->uio_offset - off));
124*86d7f5d3SJohn Marino	dpEnd = (struct dirent *) uio->uio_iov->iov_base;
125*86d7f5d3SJohn Marino
126*86d7f5d3SJohn Marino	/*
127*86d7f5d3SJohn Marino	 * Count number of entries.
128*86d7f5d3SJohn Marino	 */
129*86d7f5d3SJohn Marino	for (dp = dpStart, count = 0;
130*86d7f5d3SJohn Marino	     dp < dpEnd;
131*86d7f5d3SJohn Marino	     dp = (struct dirent *)((caddr_t) dp + dp->d_reclen))
132*86d7f5d3SJohn Marino	    count++;
133*86d7f5d3SJohn Marino
134*86d7f5d3SJohn Marino	cookiebuf = (u_int *) kmalloc(count * sizeof(u_int), M_TEMP, M_WAITOK);
135*86d7f5d3SJohn Marino	for (dp = dpStart; cookiep = cookiebuf;
136*86d7f5d3SJohn Marino	     dp < dpEnd;
137*86d7f5d3SJohn Marino	     dp = (struct dirent *)((caddr_t) dp + dp->d_reclen)) {
138*86d7f5d3SJohn Marino	    off += dp->d_reclen;
139*86d7f5d3SJohn Marino	    *cookiep++ = (u_int) off;
140*86d7f5d3SJohn Marino	}
141*86d7f5d3SJohn Marino	*ncookies = count;
142*86d7f5d3SJohn Marino	*cookies = cookiebuf;
143*86d7f5d3SJohn Marino    }
144*86d7f5d3SJohn Marino
145*86d7f5d3SJohn Marino    if (eofflag && uio->uio_offset is past the end of the directory) {
146*86d7f5d3SJohn Marino	*eofflag = TRUE;
147*86d7f5d3SJohn Marino    }
148*86d7f5d3SJohn Marino
149*86d7f5d3SJohn Marino    return error;
150*86d7f5d3SJohn Marino}
151*86d7f5d3SJohn Marino.Ed
152*86d7f5d3SJohn Marino.Sh ERRORS
153*86d7f5d3SJohn Marino.Bl -tag -width Er
154*86d7f5d3SJohn Marino.It Bq Er EINVAL
155*86d7f5d3SJohn Marinoattempt to read from an illegal offset in the directory
156*86d7f5d3SJohn Marino.It Bq Er EIO
157*86d7f5d3SJohn Marinoa read error occurred while reading the directory
158*86d7f5d3SJohn Marino.El
159*86d7f5d3SJohn Marino.Sh SEE ALSO
160*86d7f5d3SJohn Marino.Xr vnode 9
161*86d7f5d3SJohn Marino.Sh AUTHORS
162*86d7f5d3SJohn MarinoThis man page was written by
163*86d7f5d3SJohn Marino.An Doug Rabson .
164