Make linux32 getdents(2) do its own job instead of calling the

corresponding compat linux function, where struct dirent members types
differs.
This commit is contained in:
njoly 2008-09-04 17:45:00 +00:00
parent bdf7279542
commit 46da4ade28
3 changed files with 181 additions and 13 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: linux32_types.h,v 1.4 2007/12/25 18:33:35 perry Exp $ */
/* $NetBSD: linux32_types.h,v 1.5 2008/09/04 17:45:00 njoly Exp $ */
/*-
* Copyright (c) 2006 Emmanuel Dreyfus, all rights reserved.
@ -39,6 +39,7 @@ typedef int linux32_pid_t;
typedef int32_t linux32_clock_t;
typedef int32_t linux32_time_t;
typedef int32_t linux32_off_t;
typedef uint32_t linux32_ino_t;
#define LINUX32_STAT64_HAS_NSEC 1
struct linux32_stat64 {

View File

@ -1,4 +1,4 @@
/* $NetBSD: linux32_dirent.c,v 1.5 2007/12/26 13:50:48 njoly Exp $ */
/* $NetBSD: linux32_dirent.c,v 1.6 2008/09/04 17:45:00 njoly Exp $ */
/*-
* Copyright (c) 2006 Emmanuel Dreyfus, all rights reserved.
@ -33,7 +33,7 @@
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: linux32_dirent.c,v 1.5 2007/12/26 13:50:48 njoly Exp $");
__KERNEL_RCSID(0, "$NetBSD: linux32_dirent.c,v 1.6 2008/09/04 17:45:00 njoly Exp $");
#include <sys/types.h>
#include <sys/param.h>
@ -42,9 +42,13 @@ __KERNEL_RCSID(0, "$NetBSD: linux32_dirent.c,v 1.5 2007/12/26 13:50:48 njoly Exp
#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>
@ -60,6 +64,7 @@ __KERNEL_RCSID(0, "$NetBSD: linux32_dirent.c,v 1.5 2007/12/26 13:50:48 njoly Exp
#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/linux_syscallargs.h>
#include <compat/linux32/common/linux32_types.h>
@ -69,24 +74,178 @@ __KERNEL_RCSID(0, "$NetBSD: linux32_dirent.c,v 1.5 2007/12/26 13:50:48 njoly Exp
#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)
{
/* {
syscallcarg(int) fd;
syscallcarg(linux32_direntp_t) dent;
syscallcarg(unsigned int) count;
syscallarg(int) fd;
syscallarg(linux32_direntp_t) dent;
syscallarg(unsigned int) count;
} */
struct linux_sys_getdents_args ua;
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;
NETBSD32TO64_UAP(fd);
NETBSD32TOP_UAP(dent, struct linux_dirent);
NETBSD32TO64_UAP(count);
/* fd_getvnode() will use the descriptor for us */
if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
return (error);
return linux_sys_getdents(l, &ua, retval);
if ((fp->f_flag & FREAD) == 0) {
error = EBADF;
goto out1;
}
vp = (struct vnode *)fp->f_data;
if (vp->v_type != VDIR) {
error = EINVAL;
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);
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))
goto again;
fp->f_offset = off; /* update the vnode offset */
if (oldcall)
nbytes = resid + linux32_reclen;
eof:
*retval = nbytes - resid;
out:
VOP_UNLOCK(vp, 0);
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)
{

View File

@ -1,4 +1,4 @@
/* $NetBSD: linux32_types.h,v 1.7 2008/04/17 17:47:23 njoly Exp $ */
/* $NetBSD: linux32_types.h,v 1.8 2008/09/04 17:45:00 njoly Exp $ */
/*-
* Copyright (c) 2006 Emmanuel Dreyfus, all rights reserved.
@ -109,4 +109,12 @@ struct linux32_timespec {
netbsd32_long tv_nsec;
};
#define LINUX32_MAXNAMLEN 255
struct linux32_dirent {
linux32_ino_t d_ino;
linux32_off_t d_off;
u_short d_reclen;
char d_name[LINUX32_MAXNAMLEN + 1];
};
#endif /* !_LINUX32_TYPES_H */