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