17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
5da6c28aamw * Common Development and Distribution License (the "License").
6da6c28aamw * You may not use this file except in compliance with the License.
77c478bdstevel@tonic-gate *
87c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bdstevel@tonic-gate * See the License for the specific language governing permissions
117c478bdstevel@tonic-gate * and limitations under the License.
127c478bdstevel@tonic-gate *
137c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bdstevel@tonic-gate *
197c478bdstevel@tonic-gate * CDDL HEADER END
207c478bdstevel@tonic-gate */
21794f0adRoger A. Faulkner
227c478bdstevel@tonic-gate/*
23794f0adRoger A. Faulkner * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bdstevel@tonic-gate */
257c478bdstevel@tonic-gate
267c478bdstevel@tonic-gate/*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
277c478bdstevel@tonic-gate/*	  All Rights Reserved  	*/
287c478bdstevel@tonic-gate
297c478bdstevel@tonic-gate/*
307c478bdstevel@tonic-gate * Portions of this source code were derived from Berkeley 4.3 BSD
317c478bdstevel@tonic-gate * under license from the Regents of the University of California.
327c478bdstevel@tonic-gate */
337c478bdstevel@tonic-gate
347c478bdstevel@tonic-gate#include <sys/param.h>
357c478bdstevel@tonic-gate#include <sys/isa_defs.h>
367c478bdstevel@tonic-gate#include <sys/types.h>
377c478bdstevel@tonic-gate#include <sys/inttypes.h>
387c478bdstevel@tonic-gate#include <sys/sysmacros.h>
397c478bdstevel@tonic-gate#include <sys/cred.h>
407c478bdstevel@tonic-gate#include <sys/dirent.h>
417c478bdstevel@tonic-gate#include <sys/systm.h>
427c478bdstevel@tonic-gate#include <sys/errno.h>
437c478bdstevel@tonic-gate#include <sys/vnode.h>
447c478bdstevel@tonic-gate#include <sys/file.h>
457c478bdstevel@tonic-gate#include <sys/mode.h>
467c478bdstevel@tonic-gate#include <sys/uio.h>
477c478bdstevel@tonic-gate#include <sys/filio.h>
487c478bdstevel@tonic-gate#include <sys/debug.h>
497c478bdstevel@tonic-gate#include <sys/kmem.h>
507c478bdstevel@tonic-gate#include <sys/cmn_err.h>
517c478bdstevel@tonic-gate
527c478bdstevel@tonic-gate#if defined(_SYSCALL32_IMPL) || defined(_ILP32)
537c478bdstevel@tonic-gate
547c478bdstevel@tonic-gate/*
557c478bdstevel@tonic-gate * Get directory entries in a file system-independent format.
567c478bdstevel@tonic-gate *
577c478bdstevel@tonic-gate * The 32-bit version of this function now allocates a buffer to grab the
587c478bdstevel@tonic-gate * directory entries in dirent64 formats from VOP_READDIR routines.
597c478bdstevel@tonic-gate * The dirent64 structures are converted to dirent32 structures and
607c478bdstevel@tonic-gate * copied to the user space.
617c478bdstevel@tonic-gate *
627c478bdstevel@tonic-gate * Both 32-bit and 64-bit versions of libc use getdents64() and therefore
637c478bdstevel@tonic-gate * we don't expect any major performance impact due to the extra kmem_alloc's
647c478bdstevel@tonic-gate * and copying done in this routine.
657c478bdstevel@tonic-gate */
667c478bdstevel@tonic-gate
677c478bdstevel@tonic-gate/*
687c478bdstevel@tonic-gate * Native 32-bit system call for non-large-file applications.
697c478bdstevel@tonic-gate */
707c478bdstevel@tonic-gateint
717c478bdstevel@tonic-gategetdents32(int fd, void *buf, size_t count)
727c478bdstevel@tonic-gate{
737c478bdstevel@tonic-gate	vnode_t *vp;
747c478bdstevel@tonic-gate	file_t *fp;
757c478bdstevel@tonic-gate	struct uio auio;
767c478bdstevel@tonic-gate	struct iovec aiov;
777c478bdstevel@tonic-gate	register int error;
787c478bdstevel@tonic-gate	int sink;
797c478bdstevel@tonic-gate	char *newbuf;
807c478bdstevel@tonic-gate	char *obuf;
817c478bdstevel@tonic-gate	int bufsize;
827c478bdstevel@tonic-gate	int osize, nsize;
837c478bdstevel@tonic-gate	struct dirent64 *dp;
847c478bdstevel@tonic-gate	struct dirent32 *op;
857c478bdstevel@tonic-gate
867c478bdstevel@tonic-gate	if (count < sizeof (struct dirent32))
877c478bdstevel@tonic-gate		return (set_errno(EINVAL));
887c478bdstevel@tonic-gate
897c478bdstevel@tonic-gate	if ((fp = getf(fd)) == NULL)
907c478bdstevel@tonic-gate		return (set_errno(EBADF));
917c478bdstevel@tonic-gate	vp = fp->f_vnode;
927c478bdstevel@tonic-gate	if (vp->v_type != VDIR) {
937c478bdstevel@tonic-gate		releasef(fd);
947c478bdstevel@tonic-gate		return (set_errno(ENOTDIR));
957c478bdstevel@tonic-gate	}
96794f0adRoger A. Faulkner	if (!(fp->f_flag & FREAD)) {
97794f0adRoger A. Faulkner		releasef(fd);
98794f0adRoger A. Faulkner		return (set_errno(EBADF));
99794f0adRoger A. Faulkner	}
1007c478bdstevel@tonic-gate
1017c478bdstevel@tonic-gate	/*
1027c478bdstevel@tonic-gate	 * Don't let the user overcommit kernel resources.
1037c478bdstevel@tonic-gate	 */
1047c478bdstevel@tonic-gate	if (count > MAXGETDENTS_SIZE)
1057c478bdstevel@tonic-gate		count = MAXGETDENTS_SIZE;
1067c478bdstevel@tonic-gate
1077c478bdstevel@tonic-gate	bufsize = count;
1087c478bdstevel@tonic-gate	newbuf = kmem_alloc(bufsize, KM_SLEEP);
1097c478bdstevel@tonic-gate	obuf = kmem_alloc(bufsize, KM_SLEEP);
1107c478bdstevel@tonic-gate
1117c478bdstevel@tonic-gate	aiov.iov_base = newbuf;
1127c478bdstevel@tonic-gate	aiov.iov_len = count;
1137c478bdstevel@tonic-gate	auio.uio_iov = &aiov;
1147c478bdstevel@tonic-gate	auio.uio_iovcnt = 1;
1157c478bdstevel@tonic-gate	auio.uio_loffset = fp->f_offset;
1167c478bdstevel@tonic-gate	auio.uio_segflg = UIO_SYSSPACE;
1177c478bdstevel@tonic-gate	auio.uio_resid = count;
1187c478bdstevel@tonic-gate	auio.uio_fmode = 0;
1197c478bdstevel@tonic-gate	auio.uio_extflg = UIO_COPY_CACHED;
1207c478bdstevel@tonic-gate	(void) VOP_RWLOCK(vp, V_WRITELOCK_FALSE, NULL);
121da6c28aamw	error = VOP_READDIR(vp, &auio, fp->f_cred, &sink, NULL, 0);
1227c478bdstevel@tonic-gate	VOP_RWUNLOCK(vp, V_WRITELOCK_FALSE, NULL);
1237c478bdstevel@tonic-gate	if (error)
1247c478bdstevel@tonic-gate		goto out;
1257c478bdstevel@tonic-gate	count = count - auio.uio_resid;
1267c478bdstevel@tonic-gate	fp->f_offset = auio.uio_loffset;
1277c478bdstevel@tonic-gate
1287c478bdstevel@tonic-gate	dp = (struct dirent64 *)newbuf;
1297c478bdstevel@tonic-gate	op = (struct dirent32 *)obuf;
1307c478bdstevel@tonic-gate	osize = 0;
1317c478bdstevel@tonic-gate	nsize = 0;
1327c478bdstevel@tonic-gate
1337c478bdstevel@tonic-gate	while (nsize < count) {
1347c478bdstevel@tonic-gate		uint32_t reclen, namlen;
1357c478bdstevel@tonic-gate
1367c478bdstevel@tonic-gate		/*
1377c478bdstevel@tonic-gate		 * This check ensures that the 64 bit d_ino and d_off
1387c478bdstevel@tonic-gate		 * fields will fit into their 32 bit equivalents.
1397c478bdstevel@tonic-gate		 *
1407c478bdstevel@tonic-gate		 * Although d_off is a signed value, the check is done
1417c478bdstevel@tonic-gate		 * against the full 32 bits because certain file systems,
1427c478bdstevel@tonic-gate		 * NFS for one, allow directory cookies to use the full
1437c478bdstevel@tonic-gate		 * 32 bits.  We use uint64_t because there is no exact
1447c478bdstevel@tonic-gate		 * unsigned analog to the off64_t type of dp->d_off.
1457c478bdstevel@tonic-gate		 */
1467c478bdstevel@tonic-gate		if (dp->d_ino > (ino64_t)UINT32_MAX ||
1477c478bdstevel@tonic-gate		    dp->d_off > (uint64_t)UINT32_MAX) {
1487c478bdstevel@tonic-gate			error = EOVERFLOW;
1497c478bdstevel@tonic-gate			goto out;
1507c478bdstevel@tonic-gate		}
1517c478bdstevel@tonic-gate		op->d_ino = (ino32_t)dp->d_ino;
1527c478bdstevel@tonic-gate		op->d_off = (off32_t)dp->d_off;
1537c478bdstevel@tonic-gate		namlen = strlen(dp->d_name);
1547c478bdstevel@tonic-gate		reclen = DIRENT32_RECLEN(namlen);
1557c478bdstevel@tonic-gate		op->d_reclen = (uint16_t)reclen;
1567c478bdstevel@tonic-gate
1577c478bdstevel@tonic-gate		/* use strncpy(9f) to zero out uninitialized bytes */
1587c478bdstevel@tonic-gate
1597c478bdstevel@tonic-gate		(void) strncpy(op->d_name, dp->d_name,
1607c478bdstevel@tonic-gate		    DIRENT32_NAMELEN(reclen));
1617c478bdstevel@tonic-gate		nsize += (uint_t)dp->d_reclen;
1627c478bdstevel@tonic-gate		osize += (uint_t)op->d_reclen;
1637c478bdstevel@tonic-gate		dp = (struct dirent64 *)((char *)dp + (uint_t)dp->d_reclen);
1647c478bdstevel@tonic-gate		op = (struct dirent32 *)((char *)op + (uint_t)op->d_reclen);
1657c478bdstevel@tonic-gate	}
1667c478bdstevel@tonic-gate
1677c478bdstevel@tonic-gate	ASSERT(osize <= count);
1687c478bdstevel@tonic-gate	ASSERT((char *)op <= (char *)obuf + bufsize);
1697c478bdstevel@tonic-gate	ASSERT((char *)dp <= (char *)newbuf + bufsize);
1707c478bdstevel@tonic-gate
1717c478bdstevel@tonic-gate	if ((error = copyout(obuf, buf, osize)) < 0)
1727c478bdstevel@tonic-gate		error = EFAULT;
1737c478bdstevel@tonic-gateout:
1747c478bdstevel@tonic-gate	kmem_free(newbuf, bufsize);
1757c478bdstevel@tonic-gate	kmem_free(obuf, bufsize);
1767c478bdstevel@tonic-gate
1777c478bdstevel@tonic-gate	if (error) {
1787c478bdstevel@tonic-gate		releasef(fd);
1797c478bdstevel@tonic-gate		return (set_errno(error));
1807c478bdstevel@tonic-gate	}
1817c478bdstevel@tonic-gate
1827c478bdstevel@tonic-gate	releasef(fd);
1837c478bdstevel@tonic-gate	return (osize);
1847c478bdstevel@tonic-gate}
1857c478bdstevel@tonic-gate
1867c478bdstevel@tonic-gate#endif	/* _SYSCALL32 || _ILP32 */
1877c478bdstevel@tonic-gate
1887c478bdstevel@tonic-gateint
1897c478bdstevel@tonic-gategetdents64(int fd, void *buf, size_t count)
1907c478bdstevel@tonic-gate{
1917c478bdstevel@tonic-gate	vnode_t *vp;
1927c478bdstevel@tonic-gate	file_t *fp;
1937c478bdstevel@tonic-gate	struct uio auio;
1947c478bdstevel@tonic-gate	struct iovec aiov;
1957c478bdstevel@tonic-gate	register int error;
1967c478bdstevel@tonic-gate	int sink;
1977c478bdstevel@tonic-gate
1987c478bdstevel@tonic-gate	if (count < sizeof (struct dirent64))
1997c478bdstevel@tonic-gate		return (set_errno(EINVAL));
2007c478bdstevel@tonic-gate
2017c478bdstevel@tonic-gate	/*
2027c478bdstevel@tonic-gate	 * Don't let the user overcommit kernel resources.
2037c478bdstevel@tonic-gate	 */
2047c478bdstevel@tonic-gate	if (count > MAXGETDENTS_SIZE)
2057c478bdstevel@tonic-gate		count = MAXGETDENTS_SIZE;
2067c478bdstevel@tonic-gate
2077c478bdstevel@tonic-gate	if ((fp = getf(fd)) == NULL)
2087c478bdstevel@tonic-gate		return (set_errno(EBADF));
2097c478bdstevel@tonic-gate	vp = fp->f_vnode;
2107c478bdstevel@tonic-gate	if (vp->v_type != VDIR) {
2117c478bdstevel@tonic-gate		releasef(fd);
2127c478bdstevel@tonic-gate		return (set_errno(ENOTDIR));
2137c478bdstevel@tonic-gate	}
214794f0adRoger A. Faulkner	if (!(fp->f_flag & FREAD)) {
215794f0adRoger A. Faulkner		releasef(fd);
216794f0adRoger A. Faulkner		return (set_errno(EBADF));
217794f0adRoger A. Faulkner	}
2187c478bdstevel@tonic-gate	aiov.iov_base = buf;
2197c478bdstevel@tonic-gate	aiov.iov_len = count;
2207c478bdstevel@tonic-gate	auio.uio_iov = &aiov;
2217c478bdstevel@tonic-gate	auio.uio_iovcnt = 1;
2227c478bdstevel@tonic-gate	auio.uio_loffset = fp->f_offset;
2237c478bdstevel@tonic-gate	auio.uio_segflg = UIO_USERSPACE;
2247c478bdstevel@tonic-gate	auio.uio_resid = count;
2257c478bdstevel@tonic-gate	auio.uio_fmode = 0;
2267c478bdstevel@tonic-gate	auio.uio_extflg = UIO_COPY_CACHED;
2277c478bdstevel@tonic-gate	(void) VOP_RWLOCK(vp, V_WRITELOCK_FALSE, NULL);
228da6c28aamw	error = VOP_READDIR(vp, &auio, fp->f_cred, &sink, NULL, 0);
2297c478bdstevel@tonic-gate	VOP_RWUNLOCK(vp, V_WRITELOCK_FALSE, NULL);
2307c478bdstevel@tonic-gate	if (error) {
2317c478bdstevel@tonic-gate		releasef(fd);
2327c478bdstevel@tonic-gate		return (set_errno(error));
2337c478bdstevel@tonic-gate	}
2347c478bdstevel@tonic-gate	count = count - auio.uio_resid;
2357c478bdstevel@tonic-gate	fp->f_offset = auio.uio_loffset;
2367c478bdstevel@tonic-gate	releasef(fd);
2377c478bdstevel@tonic-gate	return (count);
2387c478bdstevel@tonic-gate}
239