xref: /titanic_50/usr/src/uts/common/syscall/getdents.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate /*
31*7c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
32*7c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of California.
33*7c478bd9Sstevel@tonic-gate  */
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/inttypes.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/cred.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/dirent.h>
44*7c478bd9Sstevel@tonic-gate #include <sys/systm.h>
45*7c478bd9Sstevel@tonic-gate #include <sys/errno.h>
46*7c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
47*7c478bd9Sstevel@tonic-gate #include <sys/file.h>
48*7c478bd9Sstevel@tonic-gate #include <sys/mode.h>
49*7c478bd9Sstevel@tonic-gate #include <sys/uio.h>
50*7c478bd9Sstevel@tonic-gate #include <sys/ioreq.h>
51*7c478bd9Sstevel@tonic-gate #include <sys/filio.h>
52*7c478bd9Sstevel@tonic-gate #include <sys/debug.h>
53*7c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
54*7c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL) || defined(_ILP32)
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate /*
59*7c478bd9Sstevel@tonic-gate  * Get directory entries in a file system-independent format.
60*7c478bd9Sstevel@tonic-gate  *
61*7c478bd9Sstevel@tonic-gate  * The 32-bit version of this function now allocates a buffer to grab the
62*7c478bd9Sstevel@tonic-gate  * directory entries in dirent64 formats from VOP_READDIR routines.
63*7c478bd9Sstevel@tonic-gate  * The dirent64 structures are converted to dirent32 structures and
64*7c478bd9Sstevel@tonic-gate  * copied to the user space.
65*7c478bd9Sstevel@tonic-gate  *
66*7c478bd9Sstevel@tonic-gate  * Both 32-bit and 64-bit versions of libc use getdents64() and therefore
67*7c478bd9Sstevel@tonic-gate  * we don't expect any major performance impact due to the extra kmem_alloc's
68*7c478bd9Sstevel@tonic-gate  * and copying done in this routine.
69*7c478bd9Sstevel@tonic-gate  */
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate #define	MAXGETDENTS_SIZE	(64 * 1024)
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate /*
74*7c478bd9Sstevel@tonic-gate  * Native 32-bit system call for non-large-file applications.
75*7c478bd9Sstevel@tonic-gate  */
76*7c478bd9Sstevel@tonic-gate int
77*7c478bd9Sstevel@tonic-gate getdents32(int fd, void *buf, size_t count)
78*7c478bd9Sstevel@tonic-gate {
79*7c478bd9Sstevel@tonic-gate 	vnode_t *vp;
80*7c478bd9Sstevel@tonic-gate 	file_t *fp;
81*7c478bd9Sstevel@tonic-gate 	struct uio auio;
82*7c478bd9Sstevel@tonic-gate 	struct iovec aiov;
83*7c478bd9Sstevel@tonic-gate 	register int error;
84*7c478bd9Sstevel@tonic-gate 	int sink;
85*7c478bd9Sstevel@tonic-gate 	char *newbuf;
86*7c478bd9Sstevel@tonic-gate 	char *obuf;
87*7c478bd9Sstevel@tonic-gate 	int bufsize;
88*7c478bd9Sstevel@tonic-gate 	int osize, nsize;
89*7c478bd9Sstevel@tonic-gate 	struct dirent64 *dp;
90*7c478bd9Sstevel@tonic-gate 	struct dirent32 *op;
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate 	if (count < sizeof (struct dirent32))
93*7c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate 	if ((fp = getf(fd)) == NULL)
96*7c478bd9Sstevel@tonic-gate 		return (set_errno(EBADF));
97*7c478bd9Sstevel@tonic-gate 	vp = fp->f_vnode;
98*7c478bd9Sstevel@tonic-gate 	if (vp->v_type != VDIR) {
99*7c478bd9Sstevel@tonic-gate 		releasef(fd);
100*7c478bd9Sstevel@tonic-gate 		return (set_errno(ENOTDIR));
101*7c478bd9Sstevel@tonic-gate 	}
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate 	/*
104*7c478bd9Sstevel@tonic-gate 	 * Don't let the user overcommit kernel resources.
105*7c478bd9Sstevel@tonic-gate 	 */
106*7c478bd9Sstevel@tonic-gate 	if (count > MAXGETDENTS_SIZE)
107*7c478bd9Sstevel@tonic-gate 		count = MAXGETDENTS_SIZE;
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate 	bufsize = count;
110*7c478bd9Sstevel@tonic-gate 	newbuf = kmem_alloc(bufsize, KM_SLEEP);
111*7c478bd9Sstevel@tonic-gate 	obuf = kmem_alloc(bufsize, KM_SLEEP);
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate 	aiov.iov_base = newbuf;
114*7c478bd9Sstevel@tonic-gate 	aiov.iov_len = count;
115*7c478bd9Sstevel@tonic-gate 	auio.uio_iov = &aiov;
116*7c478bd9Sstevel@tonic-gate 	auio.uio_iovcnt = 1;
117*7c478bd9Sstevel@tonic-gate 	auio.uio_loffset = fp->f_offset;
118*7c478bd9Sstevel@tonic-gate 	auio.uio_segflg = UIO_SYSSPACE;
119*7c478bd9Sstevel@tonic-gate 	auio.uio_resid = count;
120*7c478bd9Sstevel@tonic-gate 	auio.uio_fmode = 0;
121*7c478bd9Sstevel@tonic-gate 	auio.uio_extflg = UIO_COPY_CACHED;
122*7c478bd9Sstevel@tonic-gate 	(void) VOP_RWLOCK(vp, V_WRITELOCK_FALSE, NULL);
123*7c478bd9Sstevel@tonic-gate 	error = VOP_READDIR(vp, &auio, fp->f_cred, &sink);
124*7c478bd9Sstevel@tonic-gate 	VOP_RWUNLOCK(vp, V_WRITELOCK_FALSE, NULL);
125*7c478bd9Sstevel@tonic-gate 	if (error)
126*7c478bd9Sstevel@tonic-gate 		goto out;
127*7c478bd9Sstevel@tonic-gate 	count = count - auio.uio_resid;
128*7c478bd9Sstevel@tonic-gate 	fp->f_offset = auio.uio_loffset;
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate 	dp = (struct dirent64 *)newbuf;
131*7c478bd9Sstevel@tonic-gate 	op = (struct dirent32 *)obuf;
132*7c478bd9Sstevel@tonic-gate 	osize = 0;
133*7c478bd9Sstevel@tonic-gate 	nsize = 0;
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate 	while (nsize < count) {
136*7c478bd9Sstevel@tonic-gate 		uint32_t reclen, namlen;
137*7c478bd9Sstevel@tonic-gate 
138*7c478bd9Sstevel@tonic-gate 		/*
139*7c478bd9Sstevel@tonic-gate 		 * This check ensures that the 64 bit d_ino and d_off
140*7c478bd9Sstevel@tonic-gate 		 * fields will fit into their 32 bit equivalents.
141*7c478bd9Sstevel@tonic-gate 		 *
142*7c478bd9Sstevel@tonic-gate 		 * Although d_off is a signed value, the check is done
143*7c478bd9Sstevel@tonic-gate 		 * against the full 32 bits because certain file systems,
144*7c478bd9Sstevel@tonic-gate 		 * NFS for one, allow directory cookies to use the full
145*7c478bd9Sstevel@tonic-gate 		 * 32 bits.  We use uint64_t because there is no exact
146*7c478bd9Sstevel@tonic-gate 		 * unsigned analog to the off64_t type of dp->d_off.
147*7c478bd9Sstevel@tonic-gate 		 */
148*7c478bd9Sstevel@tonic-gate 		if (dp->d_ino > (ino64_t)UINT32_MAX ||
149*7c478bd9Sstevel@tonic-gate 		    dp->d_off > (uint64_t)UINT32_MAX) {
150*7c478bd9Sstevel@tonic-gate 			error = EOVERFLOW;
151*7c478bd9Sstevel@tonic-gate 			goto out;
152*7c478bd9Sstevel@tonic-gate 		}
153*7c478bd9Sstevel@tonic-gate 		op->d_ino = (ino32_t)dp->d_ino;
154*7c478bd9Sstevel@tonic-gate 		op->d_off = (off32_t)dp->d_off;
155*7c478bd9Sstevel@tonic-gate 		namlen = strlen(dp->d_name);
156*7c478bd9Sstevel@tonic-gate 		reclen = DIRENT32_RECLEN(namlen);
157*7c478bd9Sstevel@tonic-gate 		op->d_reclen = (uint16_t)reclen;
158*7c478bd9Sstevel@tonic-gate 
159*7c478bd9Sstevel@tonic-gate 		/* use strncpy(9f) to zero out uninitialized bytes */
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate 		(void) strncpy(op->d_name, dp->d_name,
162*7c478bd9Sstevel@tonic-gate 		    DIRENT32_NAMELEN(reclen));
163*7c478bd9Sstevel@tonic-gate 		nsize += (uint_t)dp->d_reclen;
164*7c478bd9Sstevel@tonic-gate 		osize += (uint_t)op->d_reclen;
165*7c478bd9Sstevel@tonic-gate 		dp = (struct dirent64 *)((char *)dp + (uint_t)dp->d_reclen);
166*7c478bd9Sstevel@tonic-gate 		op = (struct dirent32 *)((char *)op + (uint_t)op->d_reclen);
167*7c478bd9Sstevel@tonic-gate 	}
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate 	ASSERT(osize <= count);
170*7c478bd9Sstevel@tonic-gate 	ASSERT((char *)op <= (char *)obuf + bufsize);
171*7c478bd9Sstevel@tonic-gate 	ASSERT((char *)dp <= (char *)newbuf + bufsize);
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate 	if ((error = copyout(obuf, buf, osize)) < 0)
174*7c478bd9Sstevel@tonic-gate 		error = EFAULT;
175*7c478bd9Sstevel@tonic-gate out:
176*7c478bd9Sstevel@tonic-gate 	kmem_free(newbuf, bufsize);
177*7c478bd9Sstevel@tonic-gate 	kmem_free(obuf, bufsize);
178*7c478bd9Sstevel@tonic-gate 
179*7c478bd9Sstevel@tonic-gate 	if (error) {
180*7c478bd9Sstevel@tonic-gate 		releasef(fd);
181*7c478bd9Sstevel@tonic-gate 		return (set_errno(error));
182*7c478bd9Sstevel@tonic-gate 	}
183*7c478bd9Sstevel@tonic-gate 
184*7c478bd9Sstevel@tonic-gate 	releasef(fd);
185*7c478bd9Sstevel@tonic-gate 	return (osize);
186*7c478bd9Sstevel@tonic-gate }
187*7c478bd9Sstevel@tonic-gate 
188*7c478bd9Sstevel@tonic-gate #endif	/* _SYSCALL32 || _ILP32 */
189*7c478bd9Sstevel@tonic-gate 
190*7c478bd9Sstevel@tonic-gate int
191*7c478bd9Sstevel@tonic-gate getdents64(int fd, void *buf, size_t count)
192*7c478bd9Sstevel@tonic-gate {
193*7c478bd9Sstevel@tonic-gate 	vnode_t *vp;
194*7c478bd9Sstevel@tonic-gate 	file_t *fp;
195*7c478bd9Sstevel@tonic-gate 	struct uio auio;
196*7c478bd9Sstevel@tonic-gate 	struct iovec aiov;
197*7c478bd9Sstevel@tonic-gate 	register int error;
198*7c478bd9Sstevel@tonic-gate 	int sink;
199*7c478bd9Sstevel@tonic-gate 
200*7c478bd9Sstevel@tonic-gate 	if (count < sizeof (struct dirent64))
201*7c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
202*7c478bd9Sstevel@tonic-gate 
203*7c478bd9Sstevel@tonic-gate 	/*
204*7c478bd9Sstevel@tonic-gate 	 * Don't let the user overcommit kernel resources.
205*7c478bd9Sstevel@tonic-gate 	 */
206*7c478bd9Sstevel@tonic-gate 	if (count > MAXGETDENTS_SIZE)
207*7c478bd9Sstevel@tonic-gate 		count = MAXGETDENTS_SIZE;
208*7c478bd9Sstevel@tonic-gate 
209*7c478bd9Sstevel@tonic-gate 	if ((fp = getf(fd)) == NULL)
210*7c478bd9Sstevel@tonic-gate 		return (set_errno(EBADF));
211*7c478bd9Sstevel@tonic-gate 	vp = fp->f_vnode;
212*7c478bd9Sstevel@tonic-gate 	if (vp->v_type != VDIR) {
213*7c478bd9Sstevel@tonic-gate 		releasef(fd);
214*7c478bd9Sstevel@tonic-gate 		return (set_errno(ENOTDIR));
215*7c478bd9Sstevel@tonic-gate 	}
216*7c478bd9Sstevel@tonic-gate 	aiov.iov_base = buf;
217*7c478bd9Sstevel@tonic-gate 	aiov.iov_len = count;
218*7c478bd9Sstevel@tonic-gate 	auio.uio_iov = &aiov;
219*7c478bd9Sstevel@tonic-gate 	auio.uio_iovcnt = 1;
220*7c478bd9Sstevel@tonic-gate 	auio.uio_loffset = fp->f_offset;
221*7c478bd9Sstevel@tonic-gate 	auio.uio_segflg = UIO_USERSPACE;
222*7c478bd9Sstevel@tonic-gate 	auio.uio_resid = count;
223*7c478bd9Sstevel@tonic-gate 	auio.uio_fmode = 0;
224*7c478bd9Sstevel@tonic-gate 	auio.uio_extflg = UIO_COPY_CACHED;
225*7c478bd9Sstevel@tonic-gate 	(void) VOP_RWLOCK(vp, V_WRITELOCK_FALSE, NULL);
226*7c478bd9Sstevel@tonic-gate 	error = VOP_READDIR(vp, &auio, fp->f_cred, &sink);
227*7c478bd9Sstevel@tonic-gate 	VOP_RWUNLOCK(vp, V_WRITELOCK_FALSE, NULL);
228*7c478bd9Sstevel@tonic-gate 	if (error) {
229*7c478bd9Sstevel@tonic-gate 		releasef(fd);
230*7c478bd9Sstevel@tonic-gate 		return (set_errno(error));
231*7c478bd9Sstevel@tonic-gate 	}
232*7c478bd9Sstevel@tonic-gate 	count = count - auio.uio_resid;
233*7c478bd9Sstevel@tonic-gate 	fp->f_offset = auio.uio_loffset;
234*7c478bd9Sstevel@tonic-gate 	releasef(fd);
235*7c478bd9Sstevel@tonic-gate 	return (count);
236*7c478bd9Sstevel@tonic-gate }
237