#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: linux32_dirent.c,v 1.20 2019/08/23 07:53:36 maxv 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>
int
linux32_sys_getdents(struct lwp *l, const struct linux32_sys_getdents_args *uap, register_t *retval)
{
struct dirent *bdp;
struct vnode *vp;
char *inp, *tbuf;
int len, reclen;
char *outp;
int resid, linux32_reclen = 0;
struct file *fp;
struct uio auio;
struct iovec aiov;
struct linux32_dirent idb;
off_t off;
int buflen, error, eofflag, nbytes, oldcall;
struct vattr va;
off_t *cookiebuf = NULL, *cookie;
int ncookies;
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;
}
vn_lock(vp, LK_SHARED | LK_RETRY);
error = VOP_GETATTR(vp, &va, l->l_cred);
VOP_UNLOCK(vp);
if (error)
goto out1;
nbytes = SCARG(uap, count);
if (nbytes == 1) {
nbytes = sizeof (idb);
buflen = uimax(va.va_blocksize, nbytes);
oldcall = 1;
} else {
buflen = uimin(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);
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) {
error = EIO;
goto out;
}
if (bdp->d_fileno == 0) {
inp += reclen;
if (cookie)
off = *cookie++;
else
off += reclen;
continue;
}
linux32_reclen = LINUX_RECLEN(&idb, bdp->d_namlen);
if (reclen > len || resid < linux32_reclen) {
outp++;
break;
}
memset(&idb, 0, sizeof(idb));
idb.d_ino = bdp->d_fileno;
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;
}
size_t dirl = MIN(sizeof(idb.d_name) - 1, bdp->d_namlen + 1);
memcpy(idb.d_name, bdp->d_name, dirl);
idb.d_name[dirl + 1] = bdp->d_type;
if ((error = copyout((void *)&idb, outp, linux32_reclen)))
goto out;
inp += reclen;
if (cookie)
off = *cookie++;
else
off += reclen;
outp += linux32_reclen;
resid -= linux32_reclen;
if (oldcall)
break;
}
if (outp == (void *)SCARG_P32(uap, dent)) {
if (cookiebuf)
free(cookiebuf, M_TEMP);
cookiebuf = NULL;
goto again;
}
fp->f_offset = off;
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)
{
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)
{
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;
}