xref: /titanic_52/usr/src/uts/common/syscall/getdents.c (revision da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0)
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
5*da6c28aaSamw  * Common Development and Distribution License (the "License").
6*da6c28aaSamw  * 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  */
217c478bd9Sstevel@tonic-gate /*
22*da6c28aaSamw  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include <sys/param.h>
377c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h>
387c478bd9Sstevel@tonic-gate #include <sys/types.h>
397c478bd9Sstevel@tonic-gate #include <sys/inttypes.h>
407c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
417c478bd9Sstevel@tonic-gate #include <sys/cred.h>
427c478bd9Sstevel@tonic-gate #include <sys/dirent.h>
437c478bd9Sstevel@tonic-gate #include <sys/systm.h>
447c478bd9Sstevel@tonic-gate #include <sys/errno.h>
457c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
467c478bd9Sstevel@tonic-gate #include <sys/file.h>
477c478bd9Sstevel@tonic-gate #include <sys/mode.h>
487c478bd9Sstevel@tonic-gate #include <sys/uio.h>
497c478bd9Sstevel@tonic-gate #include <sys/filio.h>
507c478bd9Sstevel@tonic-gate #include <sys/debug.h>
517c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
527c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL) || defined(_ILP32)
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate /*
577c478bd9Sstevel@tonic-gate  * Get directory entries in a file system-independent format.
587c478bd9Sstevel@tonic-gate  *
597c478bd9Sstevel@tonic-gate  * The 32-bit version of this function now allocates a buffer to grab the
607c478bd9Sstevel@tonic-gate  * directory entries in dirent64 formats from VOP_READDIR routines.
617c478bd9Sstevel@tonic-gate  * The dirent64 structures are converted to dirent32 structures and
627c478bd9Sstevel@tonic-gate  * copied to the user space.
637c478bd9Sstevel@tonic-gate  *
647c478bd9Sstevel@tonic-gate  * Both 32-bit and 64-bit versions of libc use getdents64() and therefore
657c478bd9Sstevel@tonic-gate  * we don't expect any major performance impact due to the extra kmem_alloc's
667c478bd9Sstevel@tonic-gate  * and copying done in this routine.
677c478bd9Sstevel@tonic-gate  */
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate #define	MAXGETDENTS_SIZE	(64 * 1024)
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate /*
727c478bd9Sstevel@tonic-gate  * Native 32-bit system call for non-large-file applications.
737c478bd9Sstevel@tonic-gate  */
747c478bd9Sstevel@tonic-gate int
757c478bd9Sstevel@tonic-gate getdents32(int fd, void *buf, size_t count)
767c478bd9Sstevel@tonic-gate {
777c478bd9Sstevel@tonic-gate 	vnode_t *vp;
787c478bd9Sstevel@tonic-gate 	file_t *fp;
797c478bd9Sstevel@tonic-gate 	struct uio auio;
807c478bd9Sstevel@tonic-gate 	struct iovec aiov;
817c478bd9Sstevel@tonic-gate 	register int error;
827c478bd9Sstevel@tonic-gate 	int sink;
837c478bd9Sstevel@tonic-gate 	char *newbuf;
847c478bd9Sstevel@tonic-gate 	char *obuf;
857c478bd9Sstevel@tonic-gate 	int bufsize;
867c478bd9Sstevel@tonic-gate 	int osize, nsize;
877c478bd9Sstevel@tonic-gate 	struct dirent64 *dp;
887c478bd9Sstevel@tonic-gate 	struct dirent32 *op;
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	if (count < sizeof (struct dirent32))
917c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	if ((fp = getf(fd)) == NULL)
947c478bd9Sstevel@tonic-gate 		return (set_errno(EBADF));
957c478bd9Sstevel@tonic-gate 	vp = fp->f_vnode;
967c478bd9Sstevel@tonic-gate 	if (vp->v_type != VDIR) {
977c478bd9Sstevel@tonic-gate 		releasef(fd);
987c478bd9Sstevel@tonic-gate 		return (set_errno(ENOTDIR));
997c478bd9Sstevel@tonic-gate 	}
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);
121*da6c28aaSamw 	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 	}
2147c478bd9Sstevel@tonic-gate 	aiov.iov_base = buf;
2157c478bd9Sstevel@tonic-gate 	aiov.iov_len = count;
2167c478bd9Sstevel@tonic-gate 	auio.uio_iov = &aiov;
2177c478bd9Sstevel@tonic-gate 	auio.uio_iovcnt = 1;
2187c478bd9Sstevel@tonic-gate 	auio.uio_loffset = fp->f_offset;
2197c478bd9Sstevel@tonic-gate 	auio.uio_segflg = UIO_USERSPACE;
2207c478bd9Sstevel@tonic-gate 	auio.uio_resid = count;
2217c478bd9Sstevel@tonic-gate 	auio.uio_fmode = 0;
2227c478bd9Sstevel@tonic-gate 	auio.uio_extflg = UIO_COPY_CACHED;
2237c478bd9Sstevel@tonic-gate 	(void) VOP_RWLOCK(vp, V_WRITELOCK_FALSE, NULL);
224*da6c28aaSamw 	error = VOP_READDIR(vp, &auio, fp->f_cred, &sink, NULL, 0);
2257c478bd9Sstevel@tonic-gate 	VOP_RWUNLOCK(vp, V_WRITELOCK_FALSE, NULL);
2267c478bd9Sstevel@tonic-gate 	if (error) {
2277c478bd9Sstevel@tonic-gate 		releasef(fd);
2287c478bd9Sstevel@tonic-gate 		return (set_errno(error));
2297c478bd9Sstevel@tonic-gate 	}
2307c478bd9Sstevel@tonic-gate 	count = count - auio.uio_resid;
2317c478bd9Sstevel@tonic-gate 	fp->f_offset = auio.uio_loffset;
2327c478bd9Sstevel@tonic-gate 	releasef(fd);
2337c478bd9Sstevel@tonic-gate 	return (count);
2347c478bd9Sstevel@tonic-gate }
235