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