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