xref: /freebsd/share/man/man9/VOP_READDIR.9 (revision 6b3455a7665208c366849f0b2b3bc916fb97516e)
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 (NULL if not wanted)
54.It Fa ncookies
55number of directory cookies generated for NFS (NULL if not wanted)
56.It Fa cookies
57directory seek cookies generated for NFS (NULL if not wanted)
58.El
59The directory contents are read into
60.Vt struct dirent
61structures.
62If the on-disc data structures differ from this then they
63should be translated.
64.Sh LOCKS
65The directory should be locked on entry and will still be locked on exit.
66.Sh RETURN VALUES
67Zero is returned on success, otherwise an error code is returned.
68.Pp
69If this is called from the NFS server, the extra arguments
70.Fa eofflag ,
71.Fa ncookies
72and
73.Fa cookies
74are given.
75The value of
76.Fa *eofflag
77should be set to TRUE if the end of the directory is reached while
78reading.
79The directory seek cookies are returned to the NFS client and may be used
80later to restart a directory read part way through the directory.
81There should be one cookie returned per directory entry.
82The value of
83the cookie should be the offset within the directory where the on-disc
84version of the appropriate directory entry starts.
85Memory for the cookies should be allocated using:
86.Pp
87.Bd -literal
88	...;
89	*ncookies = number of entries read;
90	*cookies = (u_int*)#
91		malloc(*ncookies * sizeof(u_int), M_TEMP, M_WAITOK);
92.Ed
93.Sh PSEUDOCODE
94.Bd -literal
95int
96vop_readdir(struct vnode *vp, struct uio *uio, struct ucred *cred,
97	    int *eofflag, int *ncookies, u_int **cookies)
98{
99    off_t off;
100    int error = 0;
101
102    /*
103     * Remember the original offset to use later in generating cookies.
104     */
105    off = uio->uio_offset;
106
107    /*
108     * Read directory contents starting at uio->uio_offset into buffer
109     * pointed to by uio.
110     */
111    ...;
112
113    if (!error && ncookies != NULL) {
114	struct dirent *dpStart;
115	struct dirent *dpEnd;
116	struct dirent *dp;
117	int count;
118	u_int *cookiebuf;
119	u_int *cookiep;
120
121	if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
122	    panic("vop_readdir: unexpected uio from NFS server");
123
124	/*
125	 * Parse the stuff just read into the uio.
126	 */
127	dpStart = (struct dirent *)
128	    ((char *)uio->uio_iov->iov_base - (uio->uio_offset - off));
129	dpEnd = (struct dirent *) uio->uio_iov->iov_base;
130
131	/*
132	 * Count number of entries.
133	 */
134	for (dp = dpStart, count = 0;
135	     dp < dpEnd;
136	     dp = (struct dirent *)((caddr_t) dp + dp->d_reclen))
137	    count++;
138
139	cookiebuf = (u_int *) malloc(count * sizeof(u_int), M_TEMP, M_WAITOK);
140	for (dp = dpStart; cookiep = cookiebuf;
141	     dp < dpEnd;
142	     dp = (struct dirent *)((caddr_t) dp + dp->d_reclen)) {
143	    off += dp->d_reclen;
144	    *cookiep++ = (u_int) off;
145	}
146	*ncookies = count;
147	*cookies = cookiebuf;
148    }
149
150    if (eofflag && uio->uio_offset is past the end of the directory) {
151	*eofflag = TRUE;
152    }
153
154    return error;
155}
156.Ed
157.Sh ERRORS
158.Bl -tag -width Er
159.It Bq Er EINVAL
160An attempt was made to read from an illegal offset in the directory.
161.It Bq Er EIO
162A read error occurred while reading the directory.
163.El
164.Sh SEE ALSO
165.Xr vnode 9
166.Sh AUTHORS
167This man page was written by
168.An Doug Rabson .
169