10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
55331Samw * Common Development and Distribution License (the "License").
65331Samw * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
21*12789SRoger.Faulkner@Oracle.COM
220Sstevel@tonic-gate /*
23*12789SRoger.Faulkner@Oracle.COM * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
270Sstevel@tonic-gate /* All Rights Reserved */
280Sstevel@tonic-gate
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate * Portions of this source code were derived from Berkeley 4.3 BSD
310Sstevel@tonic-gate * under license from the Regents of the University of California.
320Sstevel@tonic-gate */
330Sstevel@tonic-gate
340Sstevel@tonic-gate #include <sys/param.h>
350Sstevel@tonic-gate #include <sys/isa_defs.h>
360Sstevel@tonic-gate #include <sys/types.h>
370Sstevel@tonic-gate #include <sys/inttypes.h>
380Sstevel@tonic-gate #include <sys/sysmacros.h>
390Sstevel@tonic-gate #include <sys/cred.h>
400Sstevel@tonic-gate #include <sys/dirent.h>
410Sstevel@tonic-gate #include <sys/systm.h>
420Sstevel@tonic-gate #include <sys/errno.h>
430Sstevel@tonic-gate #include <sys/vnode.h>
440Sstevel@tonic-gate #include <sys/file.h>
450Sstevel@tonic-gate #include <sys/mode.h>
460Sstevel@tonic-gate #include <sys/uio.h>
470Sstevel@tonic-gate #include <sys/filio.h>
480Sstevel@tonic-gate #include <sys/debug.h>
490Sstevel@tonic-gate #include <sys/kmem.h>
500Sstevel@tonic-gate #include <sys/cmn_err.h>
510Sstevel@tonic-gate
520Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL) || defined(_ILP32)
530Sstevel@tonic-gate
540Sstevel@tonic-gate /*
550Sstevel@tonic-gate * Get directory entries in a file system-independent format.
560Sstevel@tonic-gate *
570Sstevel@tonic-gate * The 32-bit version of this function now allocates a buffer to grab the
580Sstevel@tonic-gate * directory entries in dirent64 formats from VOP_READDIR routines.
590Sstevel@tonic-gate * The dirent64 structures are converted to dirent32 structures and
600Sstevel@tonic-gate * copied to the user space.
610Sstevel@tonic-gate *
620Sstevel@tonic-gate * Both 32-bit and 64-bit versions of libc use getdents64() and therefore
630Sstevel@tonic-gate * we don't expect any major performance impact due to the extra kmem_alloc's
640Sstevel@tonic-gate * and copying done in this routine.
650Sstevel@tonic-gate */
660Sstevel@tonic-gate
670Sstevel@tonic-gate /*
680Sstevel@tonic-gate * Native 32-bit system call for non-large-file applications.
690Sstevel@tonic-gate */
700Sstevel@tonic-gate int
getdents32(int fd,void * buf,size_t count)710Sstevel@tonic-gate getdents32(int fd, void *buf, size_t count)
720Sstevel@tonic-gate {
730Sstevel@tonic-gate vnode_t *vp;
740Sstevel@tonic-gate file_t *fp;
750Sstevel@tonic-gate struct uio auio;
760Sstevel@tonic-gate struct iovec aiov;
770Sstevel@tonic-gate register int error;
780Sstevel@tonic-gate int sink;
790Sstevel@tonic-gate char *newbuf;
800Sstevel@tonic-gate char *obuf;
810Sstevel@tonic-gate int bufsize;
820Sstevel@tonic-gate int osize, nsize;
830Sstevel@tonic-gate struct dirent64 *dp;
840Sstevel@tonic-gate struct dirent32 *op;
850Sstevel@tonic-gate
860Sstevel@tonic-gate if (count < sizeof (struct dirent32))
870Sstevel@tonic-gate return (set_errno(EINVAL));
880Sstevel@tonic-gate
890Sstevel@tonic-gate if ((fp = getf(fd)) == NULL)
900Sstevel@tonic-gate return (set_errno(EBADF));
910Sstevel@tonic-gate vp = fp->f_vnode;
920Sstevel@tonic-gate if (vp->v_type != VDIR) {
930Sstevel@tonic-gate releasef(fd);
940Sstevel@tonic-gate return (set_errno(ENOTDIR));
950Sstevel@tonic-gate }
96*12789SRoger.Faulkner@Oracle.COM if (!(fp->f_flag & FREAD)) {
97*12789SRoger.Faulkner@Oracle.COM releasef(fd);
98*12789SRoger.Faulkner@Oracle.COM return (set_errno(EBADF));
99*12789SRoger.Faulkner@Oracle.COM }
1000Sstevel@tonic-gate
1010Sstevel@tonic-gate /*
1020Sstevel@tonic-gate * Don't let the user overcommit kernel resources.
1030Sstevel@tonic-gate */
1040Sstevel@tonic-gate if (count > MAXGETDENTS_SIZE)
1050Sstevel@tonic-gate count = MAXGETDENTS_SIZE;
1060Sstevel@tonic-gate
1070Sstevel@tonic-gate bufsize = count;
1080Sstevel@tonic-gate newbuf = kmem_alloc(bufsize, KM_SLEEP);
1090Sstevel@tonic-gate obuf = kmem_alloc(bufsize, KM_SLEEP);
1100Sstevel@tonic-gate
1110Sstevel@tonic-gate aiov.iov_base = newbuf;
1120Sstevel@tonic-gate aiov.iov_len = count;
1130Sstevel@tonic-gate auio.uio_iov = &aiov;
1140Sstevel@tonic-gate auio.uio_iovcnt = 1;
1150Sstevel@tonic-gate auio.uio_loffset = fp->f_offset;
1160Sstevel@tonic-gate auio.uio_segflg = UIO_SYSSPACE;
1170Sstevel@tonic-gate auio.uio_resid = count;
1180Sstevel@tonic-gate auio.uio_fmode = 0;
1190Sstevel@tonic-gate auio.uio_extflg = UIO_COPY_CACHED;
1200Sstevel@tonic-gate (void) VOP_RWLOCK(vp, V_WRITELOCK_FALSE, NULL);
1215331Samw error = VOP_READDIR(vp, &auio, fp->f_cred, &sink, NULL, 0);
1220Sstevel@tonic-gate VOP_RWUNLOCK(vp, V_WRITELOCK_FALSE, NULL);
1230Sstevel@tonic-gate if (error)
1240Sstevel@tonic-gate goto out;
1250Sstevel@tonic-gate count = count - auio.uio_resid;
1260Sstevel@tonic-gate fp->f_offset = auio.uio_loffset;
1270Sstevel@tonic-gate
1280Sstevel@tonic-gate dp = (struct dirent64 *)newbuf;
1290Sstevel@tonic-gate op = (struct dirent32 *)obuf;
1300Sstevel@tonic-gate osize = 0;
1310Sstevel@tonic-gate nsize = 0;
1320Sstevel@tonic-gate
1330Sstevel@tonic-gate while (nsize < count) {
1340Sstevel@tonic-gate uint32_t reclen, namlen;
1350Sstevel@tonic-gate
1360Sstevel@tonic-gate /*
1370Sstevel@tonic-gate * This check ensures that the 64 bit d_ino and d_off
1380Sstevel@tonic-gate * fields will fit into their 32 bit equivalents.
1390Sstevel@tonic-gate *
1400Sstevel@tonic-gate * Although d_off is a signed value, the check is done
1410Sstevel@tonic-gate * against the full 32 bits because certain file systems,
1420Sstevel@tonic-gate * NFS for one, allow directory cookies to use the full
1430Sstevel@tonic-gate * 32 bits. We use uint64_t because there is no exact
1440Sstevel@tonic-gate * unsigned analog to the off64_t type of dp->d_off.
1450Sstevel@tonic-gate */
1460Sstevel@tonic-gate if (dp->d_ino > (ino64_t)UINT32_MAX ||
1470Sstevel@tonic-gate dp->d_off > (uint64_t)UINT32_MAX) {
1480Sstevel@tonic-gate error = EOVERFLOW;
1490Sstevel@tonic-gate goto out;
1500Sstevel@tonic-gate }
1510Sstevel@tonic-gate op->d_ino = (ino32_t)dp->d_ino;
1520Sstevel@tonic-gate op->d_off = (off32_t)dp->d_off;
1530Sstevel@tonic-gate namlen = strlen(dp->d_name);
1540Sstevel@tonic-gate reclen = DIRENT32_RECLEN(namlen);
1550Sstevel@tonic-gate op->d_reclen = (uint16_t)reclen;
1560Sstevel@tonic-gate
1570Sstevel@tonic-gate /* use strncpy(9f) to zero out uninitialized bytes */
1580Sstevel@tonic-gate
1590Sstevel@tonic-gate (void) strncpy(op->d_name, dp->d_name,
1600Sstevel@tonic-gate DIRENT32_NAMELEN(reclen));
1610Sstevel@tonic-gate nsize += (uint_t)dp->d_reclen;
1620Sstevel@tonic-gate osize += (uint_t)op->d_reclen;
1630Sstevel@tonic-gate dp = (struct dirent64 *)((char *)dp + (uint_t)dp->d_reclen);
1640Sstevel@tonic-gate op = (struct dirent32 *)((char *)op + (uint_t)op->d_reclen);
1650Sstevel@tonic-gate }
1660Sstevel@tonic-gate
1670Sstevel@tonic-gate ASSERT(osize <= count);
1680Sstevel@tonic-gate ASSERT((char *)op <= (char *)obuf + bufsize);
1690Sstevel@tonic-gate ASSERT((char *)dp <= (char *)newbuf + bufsize);
1700Sstevel@tonic-gate
1710Sstevel@tonic-gate if ((error = copyout(obuf, buf, osize)) < 0)
1720Sstevel@tonic-gate error = EFAULT;
1730Sstevel@tonic-gate out:
1740Sstevel@tonic-gate kmem_free(newbuf, bufsize);
1750Sstevel@tonic-gate kmem_free(obuf, bufsize);
1760Sstevel@tonic-gate
1770Sstevel@tonic-gate if (error) {
1780Sstevel@tonic-gate releasef(fd);
1790Sstevel@tonic-gate return (set_errno(error));
1800Sstevel@tonic-gate }
1810Sstevel@tonic-gate
1820Sstevel@tonic-gate releasef(fd);
1830Sstevel@tonic-gate return (osize);
1840Sstevel@tonic-gate }
1850Sstevel@tonic-gate
1860Sstevel@tonic-gate #endif /* _SYSCALL32 || _ILP32 */
1870Sstevel@tonic-gate
1880Sstevel@tonic-gate int
getdents64(int fd,void * buf,size_t count)1890Sstevel@tonic-gate getdents64(int fd, void *buf, size_t count)
1900Sstevel@tonic-gate {
1910Sstevel@tonic-gate vnode_t *vp;
1920Sstevel@tonic-gate file_t *fp;
1930Sstevel@tonic-gate struct uio auio;
1940Sstevel@tonic-gate struct iovec aiov;
1950Sstevel@tonic-gate register int error;
1960Sstevel@tonic-gate int sink;
1970Sstevel@tonic-gate
1980Sstevel@tonic-gate if (count < sizeof (struct dirent64))
1990Sstevel@tonic-gate return (set_errno(EINVAL));
2000Sstevel@tonic-gate
2010Sstevel@tonic-gate /*
2020Sstevel@tonic-gate * Don't let the user overcommit kernel resources.
2030Sstevel@tonic-gate */
2040Sstevel@tonic-gate if (count > MAXGETDENTS_SIZE)
2050Sstevel@tonic-gate count = MAXGETDENTS_SIZE;
2060Sstevel@tonic-gate
2070Sstevel@tonic-gate if ((fp = getf(fd)) == NULL)
2080Sstevel@tonic-gate return (set_errno(EBADF));
2090Sstevel@tonic-gate vp = fp->f_vnode;
2100Sstevel@tonic-gate if (vp->v_type != VDIR) {
2110Sstevel@tonic-gate releasef(fd);
2120Sstevel@tonic-gate return (set_errno(ENOTDIR));
2130Sstevel@tonic-gate }
214*12789SRoger.Faulkner@Oracle.COM if (!(fp->f_flag & FREAD)) {
215*12789SRoger.Faulkner@Oracle.COM releasef(fd);
216*12789SRoger.Faulkner@Oracle.COM return (set_errno(EBADF));
217*12789SRoger.Faulkner@Oracle.COM }
2180Sstevel@tonic-gate aiov.iov_base = buf;
2190Sstevel@tonic-gate aiov.iov_len = count;
2200Sstevel@tonic-gate auio.uio_iov = &aiov;
2210Sstevel@tonic-gate auio.uio_iovcnt = 1;
2220Sstevel@tonic-gate auio.uio_loffset = fp->f_offset;
2230Sstevel@tonic-gate auio.uio_segflg = UIO_USERSPACE;
2240Sstevel@tonic-gate auio.uio_resid = count;
2250Sstevel@tonic-gate auio.uio_fmode = 0;
2260Sstevel@tonic-gate auio.uio_extflg = UIO_COPY_CACHED;
2270Sstevel@tonic-gate (void) VOP_RWLOCK(vp, V_WRITELOCK_FALSE, NULL);
2285331Samw error = VOP_READDIR(vp, &auio, fp->f_cred, &sink, NULL, 0);
2290Sstevel@tonic-gate VOP_RWUNLOCK(vp, V_WRITELOCK_FALSE, NULL);
2300Sstevel@tonic-gate if (error) {
2310Sstevel@tonic-gate releasef(fd);
2320Sstevel@tonic-gate return (set_errno(error));
2330Sstevel@tonic-gate }
2340Sstevel@tonic-gate count = count - auio.uio_resid;
2350Sstevel@tonic-gate fp->f_offset = auio.uio_loffset;
2360Sstevel@tonic-gate releasef(fd);
2370Sstevel@tonic-gate return (count);
2380Sstevel@tonic-gate }
239