294 lines
8.3 KiB
C
294 lines
8.3 KiB
C
/* $NetBSD: linux32_dirent.c,v 1.12 2010/09/11 20:53:04 chs Exp $ */
|
|
|
|
/*-
|
|
* Copyright (c) 2006 Emmanuel Dreyfus, all rights reserved.
|
|
*
|
|
* Redistribution and use in source and binary forms, with or without
|
|
* modification, are permitted provided that the following conditions
|
|
* are met:
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
* notice, this list of conditions and the following disclaimer.
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
* documentation and/or other materials provided with the distribution.
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
* must display the following acknowledgement:
|
|
* This product includes software developed by Emmanuel Dreyfus
|
|
* 4. The name of the author may not be used to endorse or promote
|
|
* products derived from this software without specific prior written
|
|
* permission.
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED BY THE THE AUTHOR AND CONTRIBUTORS ``AS IS''
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
|
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
*/
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__KERNEL_RCSID(0, "$NetBSD: linux32_dirent.c,v 1.12 2010/09/11 20:53:04 chs Exp $");
|
|
|
|
#include <sys/types.h>
|
|
#include <sys/param.h>
|
|
#include <sys/fstypes.h>
|
|
#include <sys/signal.h>
|
|
#include <sys/dirent.h>
|
|
#include <sys/kernel.h>
|
|
#include <sys/fcntl.h>
|
|
#include <sys/file.h>
|
|
#include <sys/filedesc.h>
|
|
#include <sys/malloc.h>
|
|
#include <sys/select.h>
|
|
#include <sys/proc.h>
|
|
#include <sys/ucred.h>
|
|
#include <sys/vnode.h>
|
|
#include <sys/swap.h>
|
|
|
|
#include <machine/types.h>
|
|
|
|
#include <sys/syscallargs.h>
|
|
|
|
#include <compat/netbsd32/netbsd32.h>
|
|
#include <compat/netbsd32/netbsd32_conv.h>
|
|
#include <compat/netbsd32/netbsd32_syscallargs.h>
|
|
|
|
#include <compat/linux/common/linux_types.h>
|
|
#include <compat/linux/common/linux_signal.h>
|
|
#include <compat/linux/common/linux_machdep.h>
|
|
#include <compat/linux/common/linux_misc.h>
|
|
#include <compat/linux/common/linux_oldolduname.h>
|
|
#include <compat/linux/common/linux_dirent.h>
|
|
#include <compat/linux/common/linux_ipc.h>
|
|
#include <compat/linux/common/linux_sem.h>
|
|
#include <compat/linux/linux_syscallargs.h>
|
|
|
|
#include <compat/linux32/common/linux32_types.h>
|
|
#include <compat/linux32/common/linux32_signal.h>
|
|
#include <compat/linux32/common/linux32_machdep.h>
|
|
#include <compat/linux32/common/linux32_sysctl.h>
|
|
#include <compat/linux32/common/linux32_socketcall.h>
|
|
#include <compat/linux32/linux32_syscallargs.h>
|
|
|
|
/*
|
|
* Linux 'readdir' call. This code is mostly taken from the
|
|
* SunOS getdents call (see compat/sunos/sunos_misc.c), though
|
|
* an attempt has been made to keep it a little cleaner (failing
|
|
* miserably, because of the cruft needed if count 1 is passed).
|
|
*
|
|
* The d_off field should contain the offset of the next valid entry,
|
|
* but in Linux it has the offset of the entry itself. We emulate
|
|
* that bug here.
|
|
*
|
|
* Read in BSD-style entries, convert them, and copy them out.
|
|
*
|
|
* Note that this doesn't handle union-mounted filesystems.
|
|
*/
|
|
|
|
int
|
|
linux32_sys_getdents(struct lwp *l, const struct linux32_sys_getdents_args *uap, register_t *retval)
|
|
{
|
|
/* {
|
|
syscallarg(int) fd;
|
|
syscallarg(linux32_direntp_t) dent;
|
|
syscallarg(unsigned int) count;
|
|
} */
|
|
struct dirent *bdp;
|
|
struct vnode *vp;
|
|
char *inp, *tbuf; /* BSD-format */
|
|
int len, reclen; /* BSD-format */
|
|
char *outp; /* Linux-format */
|
|
int resid, linux32_reclen = 0; /* Linux-format */
|
|
struct file *fp;
|
|
struct uio auio;
|
|
struct iovec aiov;
|
|
struct linux32_dirent idb;
|
|
off_t off; /* true file offset */
|
|
int buflen, error, eofflag, nbytes, oldcall;
|
|
struct vattr va;
|
|
off_t *cookiebuf = NULL, *cookie;
|
|
int ncookies;
|
|
|
|
/* fd_getvnode() will use the descriptor for us */
|
|
if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
|
|
return (error);
|
|
|
|
if ((fp->f_flag & FREAD) == 0) {
|
|
error = EBADF;
|
|
goto out1;
|
|
}
|
|
|
|
vp = (struct vnode *)fp->f_data;
|
|
if (vp->v_type != VDIR) {
|
|
error = ENOTDIR;
|
|
goto out1;
|
|
}
|
|
|
|
if ((error = VOP_GETATTR(vp, &va, l->l_cred)))
|
|
goto out1;
|
|
|
|
nbytes = SCARG(uap, count);
|
|
if (nbytes == 1) { /* emulating old, broken behaviour */
|
|
nbytes = sizeof (idb);
|
|
buflen = max(va.va_blocksize, nbytes);
|
|
oldcall = 1;
|
|
} else {
|
|
buflen = min(MAXBSIZE, nbytes);
|
|
if (buflen < va.va_blocksize)
|
|
buflen = va.va_blocksize;
|
|
oldcall = 0;
|
|
}
|
|
tbuf = malloc(buflen, M_TEMP, M_WAITOK);
|
|
|
|
vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
|
|
off = fp->f_offset;
|
|
again:
|
|
aiov.iov_base = tbuf;
|
|
aiov.iov_len = buflen;
|
|
auio.uio_iov = &aiov;
|
|
auio.uio_iovcnt = 1;
|
|
auio.uio_rw = UIO_READ;
|
|
auio.uio_resid = buflen;
|
|
auio.uio_offset = off;
|
|
UIO_SETUP_SYSSPACE(&auio);
|
|
/*
|
|
* First we read into the malloc'ed buffer, then
|
|
* we massage it into user space, one record at a time.
|
|
*/
|
|
error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
|
|
&ncookies);
|
|
if (error)
|
|
goto out;
|
|
|
|
inp = tbuf;
|
|
outp = (void *)SCARG_P32(uap, dent);
|
|
resid = nbytes;
|
|
if ((len = buflen - auio.uio_resid) == 0)
|
|
goto eof;
|
|
|
|
for (cookie = cookiebuf; len > 0; len -= reclen) {
|
|
bdp = (struct dirent *)inp;
|
|
reclen = bdp->d_reclen;
|
|
if (reclen & 3)
|
|
panic("linux32_readdir");
|
|
if (bdp->d_fileno == 0) {
|
|
inp += reclen; /* it is a hole; squish it out */
|
|
if (cookie)
|
|
off = *cookie++;
|
|
else
|
|
off += reclen;
|
|
continue;
|
|
}
|
|
linux32_reclen = LINUX_RECLEN(&idb, bdp->d_namlen);
|
|
if (reclen > len || resid < linux32_reclen) {
|
|
/* entry too big for buffer, so just stop */
|
|
outp++;
|
|
break;
|
|
}
|
|
/*
|
|
* Massage in place to make a Linux-shaped dirent (otherwise
|
|
* we have to worry about touching user memory outside of
|
|
* the copyout() call).
|
|
*/
|
|
idb.d_ino = bdp->d_fileno;
|
|
/*
|
|
* The old readdir() call misuses the offset and reclen fields.
|
|
*/
|
|
if (oldcall) {
|
|
idb.d_off = (linux32_off_t)linux32_reclen;
|
|
idb.d_reclen = (u_short)bdp->d_namlen;
|
|
} else {
|
|
if (sizeof (idb.d_off) <= 4 && (off >> 32) != 0) {
|
|
compat_offseterr(vp, "linux32_getdents");
|
|
error = EINVAL;
|
|
goto out;
|
|
}
|
|
idb.d_off = (linux32_off_t)off;
|
|
idb.d_reclen = (u_short)linux32_reclen;
|
|
}
|
|
strcpy(idb.d_name, bdp->d_name);
|
|
idb.d_name[strlen(idb.d_name) + 1] = bdp->d_type;
|
|
if ((error = copyout((void *)&idb, outp, linux32_reclen)))
|
|
goto out;
|
|
/* advance past this real entry */
|
|
inp += reclen;
|
|
if (cookie)
|
|
off = *cookie++; /* each entry points to itself */
|
|
else
|
|
off += reclen;
|
|
/* advance output past Linux-shaped entry */
|
|
outp += linux32_reclen;
|
|
resid -= linux32_reclen;
|
|
if (oldcall)
|
|
break;
|
|
}
|
|
|
|
/* if we squished out the whole block, try again */
|
|
if (outp == (void *)SCARG_P32(uap, dent)) {
|
|
if (cookiebuf)
|
|
free(cookiebuf, M_TEMP);
|
|
cookiebuf = NULL;
|
|
goto again;
|
|
}
|
|
fp->f_offset = off; /* update the vnode offset */
|
|
|
|
if (oldcall)
|
|
nbytes = resid + linux32_reclen;
|
|
|
|
eof:
|
|
*retval = nbytes - resid;
|
|
out:
|
|
VOP_UNLOCK(vp);
|
|
if (cookiebuf)
|
|
free(cookiebuf, M_TEMP);
|
|
free(tbuf, M_TEMP);
|
|
out1:
|
|
fd_putfile(SCARG(uap, fd));
|
|
return error;
|
|
}
|
|
|
|
int
|
|
linux32_sys_getdents64(struct lwp *l, const struct linux32_sys_getdents64_args *uap, register_t *retval)
|
|
{
|
|
/* {
|
|
syscallarg(int) fd;
|
|
syscallarg(linux32_dirent64p_t) dent;
|
|
syscallarg(unsigned int) count;
|
|
} */
|
|
struct linux_sys_getdents64_args ua;
|
|
|
|
NETBSD32TO64_UAP(fd);
|
|
NETBSD32TOP_UAP(dent, struct linux_dirent64);
|
|
NETBSD32TO64_UAP(count);
|
|
|
|
return linux_sys_getdents64(l, &ua, retval);
|
|
}
|
|
|
|
int
|
|
linux32_sys_readdir(struct lwp *l, const struct linux32_sys_readdir_args *uap, register_t *retval)
|
|
{
|
|
/* {
|
|
syscallarg(int) fd;
|
|
syscallarg(struct linux32_direntp_t) dent;
|
|
syscallarg(unsigned int) count;
|
|
} */
|
|
int error;
|
|
struct linux32_sys_getdents_args da;
|
|
|
|
SCARG(&da, fd) = SCARG(uap, fd);
|
|
SCARG(&da, dent) = SCARG(uap, dent);
|
|
SCARG(&da, count) = 1;
|
|
|
|
error = linux32_sys_getdents(l, &da, retval);
|
|
if (error == 0 && *retval > 1)
|
|
*retval = 1;
|
|
|
|
return error;
|
|
}
|