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