Home | History | Annotate | Line # | Download | only in common
linux32_dirent.c revision 1.6
      1  1.6  njoly /*	$NetBSD: linux32_dirent.c,v 1.6 2008/09/04 17:45:00 njoly Exp $ */
      2  1.1   manu 
      3  1.1   manu /*-
      4  1.1   manu  * Copyright (c) 2006 Emmanuel Dreyfus, all rights reserved.
      5  1.1   manu  *
      6  1.1   manu  * Redistribution and use in source and binary forms, with or without
      7  1.1   manu  * modification, are permitted provided that the following conditions
      8  1.1   manu  * are met:
      9  1.1   manu  * 1. Redistributions of source code must retain the above copyright
     10  1.1   manu  *    notice, this list of conditions and the following disclaimer.
     11  1.1   manu  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1   manu  *    notice, this list of conditions and the following disclaimer in the
     13  1.1   manu  *    documentation and/or other materials provided with the distribution.
     14  1.1   manu  * 3. All advertising materials mentioning features or use of this software
     15  1.1   manu  *    must display the following acknowledgement:
     16  1.1   manu  *	This product includes software developed by Emmanuel Dreyfus
     17  1.1   manu  * 4. The name of the author may not be used to endorse or promote
     18  1.1   manu  *    products derived from this software without specific prior written
     19  1.1   manu  *    permission.
     20  1.1   manu  *
     21  1.1   manu  * THIS SOFTWARE IS PROVIDED BY THE THE AUTHOR AND CONTRIBUTORS ``AS IS''
     22  1.1   manu  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     23  1.1   manu  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24  1.1   manu  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
     25  1.1   manu  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26  1.1   manu  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27  1.1   manu  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  1.1   manu  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29  1.1   manu  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30  1.1   manu  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31  1.1   manu  * POSSIBILITY OF SUCH DAMAGE.
     32  1.1   manu  */
     33  1.1   manu 
     34  1.1   manu #include <sys/cdefs.h>
     35  1.1   manu 
     36  1.6  njoly __KERNEL_RCSID(0, "$NetBSD: linux32_dirent.c,v 1.6 2008/09/04 17:45:00 njoly Exp $");
     37  1.1   manu 
     38  1.1   manu #include <sys/types.h>
     39  1.1   manu #include <sys/param.h>
     40  1.1   manu #include <sys/fstypes.h>
     41  1.1   manu #include <sys/signal.h>
     42  1.1   manu #include <sys/dirent.h>
     43  1.1   manu #include <sys/kernel.h>
     44  1.1   manu #include <sys/fcntl.h>
     45  1.6  njoly #include <sys/file.h>
     46  1.6  njoly #include <sys/filedesc.h>
     47  1.6  njoly #include <sys/malloc.h>
     48  1.1   manu #include <sys/select.h>
     49  1.1   manu #include <sys/proc.h>
     50  1.1   manu #include <sys/ucred.h>
     51  1.6  njoly #include <sys/vnode.h>
     52  1.1   manu #include <sys/swap.h>
     53  1.1   manu 
     54  1.1   manu #include <machine/types.h>
     55  1.1   manu 
     56  1.1   manu #include <sys/syscallargs.h>
     57  1.1   manu 
     58  1.1   manu #include <compat/netbsd32/netbsd32.h>
     59  1.1   manu #include <compat/netbsd32/netbsd32_conv.h>
     60  1.1   manu #include <compat/netbsd32/netbsd32_syscallargs.h>
     61  1.1   manu 
     62  1.1   manu #include <compat/linux/common/linux_types.h>
     63  1.1   manu #include <compat/linux/common/linux_signal.h>
     64  1.1   manu #include <compat/linux/common/linux_machdep.h>
     65  1.1   manu #include <compat/linux/common/linux_misc.h>
     66  1.1   manu #include <compat/linux/common/linux_oldolduname.h>
     67  1.6  njoly #include <compat/linux/common/linux_dirent.h>
     68  1.1   manu #include <compat/linux/linux_syscallargs.h>
     69  1.1   manu 
     70  1.1   manu #include <compat/linux32/common/linux32_types.h>
     71  1.1   manu #include <compat/linux32/common/linux32_signal.h>
     72  1.1   manu #include <compat/linux32/common/linux32_machdep.h>
     73  1.1   manu #include <compat/linux32/common/linux32_sysctl.h>
     74  1.1   manu #include <compat/linux32/common/linux32_socketcall.h>
     75  1.1   manu #include <compat/linux32/linux32_syscallargs.h>
     76  1.1   manu 
     77  1.6  njoly /*
     78  1.6  njoly  * Linux 'readdir' call. This code is mostly taken from the
     79  1.6  njoly  * SunOS getdents call (see compat/sunos/sunos_misc.c), though
     80  1.6  njoly  * an attempt has been made to keep it a little cleaner (failing
     81  1.6  njoly  * miserably, because of the cruft needed if count 1 is passed).
     82  1.6  njoly  *
     83  1.6  njoly  * The d_off field should contain the offset of the next valid entry,
     84  1.6  njoly  * but in Linux it has the offset of the entry itself. We emulate
     85  1.6  njoly  * that bug here.
     86  1.6  njoly  *
     87  1.6  njoly  * Read in BSD-style entries, convert them, and copy them out.
     88  1.6  njoly  *
     89  1.6  njoly  * Note that this doesn't handle union-mounted filesystems.
     90  1.6  njoly  */
     91  1.6  njoly 
     92  1.1   manu int
     93  1.4    dsl linux32_sys_getdents(struct lwp *l, const struct linux32_sys_getdents_args *uap, register_t *retval)
     94  1.1   manu {
     95  1.4    dsl 	/* {
     96  1.6  njoly 		syscallarg(int) fd;
     97  1.6  njoly 		syscallarg(linux32_direntp_t) dent;
     98  1.6  njoly 		syscallarg(unsigned int) count;
     99  1.4    dsl 	} */
    100  1.6  njoly 	struct dirent *bdp;
    101  1.6  njoly 	struct vnode *vp;
    102  1.6  njoly 	char *inp, *tbuf;		/* BSD-format */
    103  1.6  njoly 	int len, reclen;		/* BSD-format */
    104  1.6  njoly 	char *outp;			/* Linux-format */
    105  1.6  njoly 	int resid, linux32_reclen = 0;	/* Linux-format */
    106  1.6  njoly 	struct file *fp;
    107  1.6  njoly 	struct uio auio;
    108  1.6  njoly 	struct iovec aiov;
    109  1.6  njoly 	struct linux32_dirent idb;
    110  1.6  njoly 	off_t off;		/* true file offset */
    111  1.6  njoly 	int buflen, error, eofflag, nbytes, oldcall;
    112  1.6  njoly 	struct vattr va;
    113  1.6  njoly 	off_t *cookiebuf = NULL, *cookie;
    114  1.6  njoly 	int ncookies;
    115  1.6  njoly 
    116  1.6  njoly 	/* fd_getvnode() will use the descriptor for us */
    117  1.6  njoly 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
    118  1.6  njoly 		return (error);
    119  1.6  njoly 
    120  1.6  njoly 	if ((fp->f_flag & FREAD) == 0) {
    121  1.6  njoly 		error = EBADF;
    122  1.6  njoly 		goto out1;
    123  1.6  njoly 	}
    124  1.6  njoly 
    125  1.6  njoly 	vp = (struct vnode *)fp->f_data;
    126  1.6  njoly 	if (vp->v_type != VDIR) {
    127  1.6  njoly 		error = EINVAL;
    128  1.6  njoly 		goto out1;
    129  1.6  njoly 	}
    130  1.6  njoly 
    131  1.6  njoly 	if ((error = VOP_GETATTR(vp, &va, l->l_cred)))
    132  1.6  njoly 		goto out1;
    133  1.6  njoly 
    134  1.6  njoly 	nbytes = SCARG(uap, count);
    135  1.6  njoly 	if (nbytes == 1) {	/* emulating old, broken behaviour */
    136  1.6  njoly 		nbytes = sizeof (idb);
    137  1.6  njoly 		buflen = max(va.va_blocksize, nbytes);
    138  1.6  njoly 		oldcall = 1;
    139  1.6  njoly 	} else {
    140  1.6  njoly 		buflen = min(MAXBSIZE, nbytes);
    141  1.6  njoly 		if (buflen < va.va_blocksize)
    142  1.6  njoly 			buflen = va.va_blocksize;
    143  1.6  njoly 		oldcall = 0;
    144  1.6  njoly 	}
    145  1.6  njoly 	tbuf = malloc(buflen, M_TEMP, M_WAITOK);
    146  1.6  njoly 
    147  1.6  njoly 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    148  1.6  njoly 	off = fp->f_offset;
    149  1.6  njoly again:
    150  1.6  njoly 	aiov.iov_base = tbuf;
    151  1.6  njoly 	aiov.iov_len = buflen;
    152  1.6  njoly 	auio.uio_iov = &aiov;
    153  1.6  njoly 	auio.uio_iovcnt = 1;
    154  1.6  njoly 	auio.uio_rw = UIO_READ;
    155  1.6  njoly 	auio.uio_resid = buflen;
    156  1.6  njoly 	auio.uio_offset = off;
    157  1.6  njoly 	UIO_SETUP_SYSSPACE(&auio);
    158  1.6  njoly 	/*
    159  1.6  njoly          * First we read into the malloc'ed buffer, then
    160  1.6  njoly          * we massage it into user space, one record at a time.
    161  1.6  njoly          */
    162  1.6  njoly 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
    163  1.6  njoly 	    &ncookies);
    164  1.6  njoly 	if (error)
    165  1.6  njoly 		goto out;
    166  1.6  njoly 
    167  1.6  njoly 	inp = tbuf;
    168  1.6  njoly 	outp = (void *)SCARG_P32(uap, dent);
    169  1.6  njoly 	resid = nbytes;
    170  1.6  njoly 	if ((len = buflen - auio.uio_resid) == 0)
    171  1.6  njoly 		goto eof;
    172  1.6  njoly 
    173  1.6  njoly 	for (cookie = cookiebuf; len > 0; len -= reclen) {
    174  1.6  njoly 		bdp = (struct dirent *)inp;
    175  1.6  njoly 		reclen = bdp->d_reclen;
    176  1.6  njoly 		if (reclen & 3)
    177  1.6  njoly 			panic("linux32_readdir");
    178  1.6  njoly 		if (bdp->d_fileno == 0) {
    179  1.6  njoly 			inp += reclen;	/* it is a hole; squish it out */
    180  1.6  njoly 			if (cookie)
    181  1.6  njoly 				off = *cookie++;
    182  1.6  njoly 			else
    183  1.6  njoly 				off += reclen;
    184  1.6  njoly 			continue;
    185  1.6  njoly 		}
    186  1.6  njoly 		linux32_reclen = LINUX_RECLEN(&idb, bdp->d_namlen);
    187  1.6  njoly 		if (reclen > len || resid < linux32_reclen) {
    188  1.6  njoly 			/* entry too big for buffer, so just stop */
    189  1.6  njoly 			outp++;
    190  1.6  njoly 			break;
    191  1.6  njoly 		}
    192  1.6  njoly 		/*
    193  1.6  njoly 		 * Massage in place to make a Linux-shaped dirent (otherwise
    194  1.6  njoly 		 * we have to worry about touching user memory outside of
    195  1.6  njoly 		 * the copyout() call).
    196  1.6  njoly 		 */
    197  1.6  njoly 		idb.d_ino = bdp->d_fileno;
    198  1.6  njoly 		/*
    199  1.6  njoly 		 * The old readdir() call misuses the offset and reclen fields.
    200  1.6  njoly 		 */
    201  1.6  njoly 		if (oldcall) {
    202  1.6  njoly 			idb.d_off = (linux32_off_t)linux32_reclen;
    203  1.6  njoly 			idb.d_reclen = (u_short)bdp->d_namlen;
    204  1.6  njoly 		} else {
    205  1.6  njoly 			if (sizeof (idb.d_off) <= 4 && (off >> 32) != 0) {
    206  1.6  njoly 				compat_offseterr(vp, "linux32_getdents");
    207  1.6  njoly 				error = EINVAL;
    208  1.6  njoly 				goto out;
    209  1.6  njoly 			}
    210  1.6  njoly 			idb.d_off = (linux32_off_t)off;
    211  1.6  njoly 			idb.d_reclen = (u_short)linux32_reclen;
    212  1.6  njoly 		}
    213  1.6  njoly 		strcpy(idb.d_name, bdp->d_name);
    214  1.6  njoly 		if ((error = copyout((void *)&idb, outp, linux32_reclen)))
    215  1.6  njoly 			goto out;
    216  1.6  njoly 		/* advance past this real entry */
    217  1.6  njoly 		inp += reclen;
    218  1.6  njoly 		if (cookie)
    219  1.6  njoly 			off = *cookie++; /* each entry points to itself */
    220  1.6  njoly 		else
    221  1.6  njoly 			off += reclen;
    222  1.6  njoly 		/* advance output past Linux-shaped entry */
    223  1.6  njoly 		outp += linux32_reclen;
    224  1.6  njoly 		resid -= linux32_reclen;
    225  1.6  njoly 		if (oldcall)
    226  1.6  njoly 			break;
    227  1.6  njoly 	}
    228  1.6  njoly 
    229  1.6  njoly 	/* if we squished out the whole block, try again */
    230  1.6  njoly 	if (outp == (void *)SCARG_P32(uap, dent))
    231  1.6  njoly 		goto again;
    232  1.6  njoly 	fp->f_offset = off;	/* update the vnode offset */
    233  1.6  njoly 
    234  1.6  njoly 	if (oldcall)
    235  1.6  njoly 		nbytes = resid + linux32_reclen;
    236  1.6  njoly 
    237  1.6  njoly eof:
    238  1.6  njoly 	*retval = nbytes - resid;
    239  1.6  njoly out:
    240  1.6  njoly 	VOP_UNLOCK(vp, 0);
    241  1.6  njoly 	if (cookiebuf)
    242  1.6  njoly 		free(cookiebuf, M_TEMP);
    243  1.6  njoly 	free(tbuf, M_TEMP);
    244  1.6  njoly out1:
    245  1.6  njoly 	fd_putfile(SCARG(uap, fd));
    246  1.6  njoly 	return error;
    247  1.1   manu }
    248  1.1   manu 
    249  1.1   manu int
    250  1.4    dsl linux32_sys_getdents64(struct lwp *l, const struct linux32_sys_getdents64_args *uap, register_t *retval)
    251  1.1   manu {
    252  1.4    dsl 	/* {
    253  1.1   manu 		syscallcarg(int) fd;
    254  1.1   manu 		syscallcarg(linux32_dirent64p_t) dent;
    255  1.1   manu 		syscallcarg(unsigned int) count;
    256  1.4    dsl 	} */
    257  1.1   manu 	struct linux_sys_getdents64_args ua;
    258  1.1   manu 
    259  1.1   manu 	NETBSD32TO64_UAP(fd);
    260  1.1   manu 	NETBSD32TOP_UAP(dent, struct linux_dirent64);
    261  1.1   manu 	NETBSD32TO64_UAP(count);
    262  1.1   manu 
    263  1.1   manu 	return linux_sys_getdents64(l, &ua, retval);
    264  1.1   manu }
    265  1.1   manu 
    266  1.5  njoly int
    267  1.5  njoly linux32_sys_readdir(struct lwp *l, const struct linux32_sys_readdir_args *uap, register_t *retval)
    268  1.5  njoly {
    269  1.5  njoly 	/* {
    270  1.5  njoly 		syscallarg(int) fd;
    271  1.5  njoly 		syscallarg(struct linux32_direntp_t) dent;
    272  1.5  njoly 		syscallarg(unsigned int) count;
    273  1.5  njoly 	} */
    274  1.5  njoly 	int error;
    275  1.5  njoly 	struct linux32_sys_getdents_args da;
    276  1.5  njoly 
    277  1.5  njoly 	SCARG(&da, fd) = SCARG(uap, fd);
    278  1.5  njoly 	SCARG(&da, dent) = SCARG(uap, dent);
    279  1.5  njoly 	SCARG(&da, count) = 1;
    280  1.5  njoly 
    281  1.5  njoly 	error = linux32_sys_getdents(l, &da, retval);
    282  1.5  njoly 	if (error == 0 && *retval > 1)
    283  1.5  njoly 		*retval = 1;
    284  1.5  njoly 
    285  1.5  njoly 	return error;
    286  1.5  njoly }
    287