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