Home | History | Annotate | Line # | Download | only in common
linux32_dirent.c revision 1.10
      1  1.10     he /*	$NetBSD: linux32_dirent.c,v 1.10 2010/03/03 08:20:38 he 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.10     he __KERNEL_RCSID(0, "$NetBSD: linux32_dirent.c,v 1.10 2010/03/03 08:20:38 he 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.7     ad #include <compat/linux/common/linux_ipc.h>
     69   1.7     ad #include <compat/linux/common/linux_sem.h>
     70   1.1   manu #include <compat/linux/linux_syscallargs.h>
     71   1.1   manu 
     72   1.1   manu #include <compat/linux32/common/linux32_types.h>
     73   1.1   manu #include <compat/linux32/common/linux32_signal.h>
     74   1.1   manu #include <compat/linux32/common/linux32_machdep.h>
     75   1.1   manu #include <compat/linux32/common/linux32_sysctl.h>
     76   1.1   manu #include <compat/linux32/common/linux32_socketcall.h>
     77   1.1   manu #include <compat/linux32/linux32_syscallargs.h>
     78   1.1   manu 
     79   1.6  njoly /*
     80   1.6  njoly  * Linux 'readdir' call. This code is mostly taken from the
     81   1.6  njoly  * SunOS getdents call (see compat/sunos/sunos_misc.c), though
     82   1.6  njoly  * an attempt has been made to keep it a little cleaner (failing
     83   1.6  njoly  * miserably, because of the cruft needed if count 1 is passed).
     84   1.6  njoly  *
     85   1.6  njoly  * The d_off field should contain the offset of the next valid entry,
     86   1.6  njoly  * but in Linux it has the offset of the entry itself. We emulate
     87   1.6  njoly  * that bug here.
     88   1.6  njoly  *
     89   1.6  njoly  * Read in BSD-style entries, convert them, and copy them out.
     90   1.6  njoly  *
     91   1.6  njoly  * Note that this doesn't handle union-mounted filesystems.
     92   1.6  njoly  */
     93   1.6  njoly 
     94   1.1   manu int
     95   1.4    dsl linux32_sys_getdents(struct lwp *l, const struct linux32_sys_getdents_args *uap, register_t *retval)
     96   1.1   manu {
     97   1.4    dsl 	/* {
     98   1.6  njoly 		syscallarg(int) fd;
     99   1.6  njoly 		syscallarg(linux32_direntp_t) dent;
    100   1.6  njoly 		syscallarg(unsigned int) count;
    101   1.4    dsl 	} */
    102   1.6  njoly 	struct dirent *bdp;
    103   1.6  njoly 	struct vnode *vp;
    104   1.6  njoly 	char *inp, *tbuf;		/* BSD-format */
    105   1.6  njoly 	int len, reclen;		/* BSD-format */
    106   1.6  njoly 	char *outp;			/* Linux-format */
    107   1.6  njoly 	int resid, linux32_reclen = 0;	/* Linux-format */
    108   1.6  njoly 	struct file *fp;
    109   1.6  njoly 	struct uio auio;
    110   1.6  njoly 	struct iovec aiov;
    111   1.6  njoly 	struct linux32_dirent idb;
    112   1.6  njoly 	off_t off;		/* true file offset */
    113   1.6  njoly 	int buflen, error, eofflag, nbytes, oldcall;
    114   1.6  njoly 	struct vattr va;
    115   1.6  njoly 	off_t *cookiebuf = NULL, *cookie;
    116   1.6  njoly 	int ncookies;
    117   1.6  njoly 
    118   1.6  njoly 	/* fd_getvnode() will use the descriptor for us */
    119   1.6  njoly 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
    120   1.6  njoly 		return (error);
    121   1.6  njoly 
    122   1.6  njoly 	if ((fp->f_flag & FREAD) == 0) {
    123   1.6  njoly 		error = EBADF;
    124   1.6  njoly 		goto out1;
    125   1.6  njoly 	}
    126   1.6  njoly 
    127   1.6  njoly 	vp = (struct vnode *)fp->f_data;
    128   1.6  njoly 	if (vp->v_type != VDIR) {
    129   1.9  njoly 		error = ENOTDIR;
    130   1.6  njoly 		goto out1;
    131   1.6  njoly 	}
    132   1.6  njoly 
    133   1.6  njoly 	if ((error = VOP_GETATTR(vp, &va, l->l_cred)))
    134   1.6  njoly 		goto out1;
    135   1.6  njoly 
    136   1.6  njoly 	nbytes = SCARG(uap, count);
    137   1.6  njoly 	if (nbytes == 1) {	/* emulating old, broken behaviour */
    138   1.6  njoly 		nbytes = sizeof (idb);
    139   1.6  njoly 		buflen = max(va.va_blocksize, nbytes);
    140   1.6  njoly 		oldcall = 1;
    141   1.6  njoly 	} else {
    142   1.6  njoly 		buflen = min(MAXBSIZE, nbytes);
    143   1.6  njoly 		if (buflen < va.va_blocksize)
    144   1.6  njoly 			buflen = va.va_blocksize;
    145   1.6  njoly 		oldcall = 0;
    146   1.6  njoly 	}
    147   1.6  njoly 	tbuf = malloc(buflen, M_TEMP, M_WAITOK);
    148   1.6  njoly 
    149   1.6  njoly 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    150   1.6  njoly 	off = fp->f_offset;
    151   1.6  njoly again:
    152   1.6  njoly 	aiov.iov_base = tbuf;
    153   1.6  njoly 	aiov.iov_len = buflen;
    154   1.6  njoly 	auio.uio_iov = &aiov;
    155   1.6  njoly 	auio.uio_iovcnt = 1;
    156   1.6  njoly 	auio.uio_rw = UIO_READ;
    157   1.6  njoly 	auio.uio_resid = buflen;
    158   1.6  njoly 	auio.uio_offset = off;
    159   1.6  njoly 	UIO_SETUP_SYSSPACE(&auio);
    160   1.6  njoly 	/*
    161   1.6  njoly          * First we read into the malloc'ed buffer, then
    162   1.6  njoly          * we massage it into user space, one record at a time.
    163   1.6  njoly          */
    164   1.6  njoly 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
    165   1.6  njoly 	    &ncookies);
    166   1.6  njoly 	if (error)
    167   1.6  njoly 		goto out;
    168   1.6  njoly 
    169   1.6  njoly 	inp = tbuf;
    170   1.6  njoly 	outp = (void *)SCARG_P32(uap, dent);
    171   1.6  njoly 	resid = nbytes;
    172   1.6  njoly 	if ((len = buflen - auio.uio_resid) == 0)
    173   1.6  njoly 		goto eof;
    174   1.6  njoly 
    175   1.6  njoly 	for (cookie = cookiebuf; len > 0; len -= reclen) {
    176   1.6  njoly 		bdp = (struct dirent *)inp;
    177   1.6  njoly 		reclen = bdp->d_reclen;
    178   1.6  njoly 		if (reclen & 3)
    179   1.6  njoly 			panic("linux32_readdir");
    180   1.6  njoly 		if (bdp->d_fileno == 0) {
    181   1.6  njoly 			inp += reclen;	/* it is a hole; squish it out */
    182   1.6  njoly 			if (cookie)
    183   1.6  njoly 				off = *cookie++;
    184   1.6  njoly 			else
    185   1.6  njoly 				off += reclen;
    186   1.6  njoly 			continue;
    187   1.6  njoly 		}
    188   1.6  njoly 		linux32_reclen = LINUX_RECLEN(&idb, bdp->d_namlen);
    189   1.6  njoly 		if (reclen > len || resid < linux32_reclen) {
    190   1.6  njoly 			/* entry too big for buffer, so just stop */
    191   1.6  njoly 			outp++;
    192   1.6  njoly 			break;
    193   1.6  njoly 		}
    194   1.6  njoly 		/*
    195   1.6  njoly 		 * Massage in place to make a Linux-shaped dirent (otherwise
    196   1.6  njoly 		 * we have to worry about touching user memory outside of
    197   1.6  njoly 		 * the copyout() call).
    198   1.6  njoly 		 */
    199   1.6  njoly 		idb.d_ino = bdp->d_fileno;
    200   1.6  njoly 		/*
    201   1.6  njoly 		 * The old readdir() call misuses the offset and reclen fields.
    202   1.6  njoly 		 */
    203   1.6  njoly 		if (oldcall) {
    204   1.6  njoly 			idb.d_off = (linux32_off_t)linux32_reclen;
    205   1.6  njoly 			idb.d_reclen = (u_short)bdp->d_namlen;
    206   1.6  njoly 		} else {
    207   1.6  njoly 			if (sizeof (idb.d_off) <= 4 && (off >> 32) != 0) {
    208   1.6  njoly 				compat_offseterr(vp, "linux32_getdents");
    209   1.6  njoly 				error = EINVAL;
    210   1.6  njoly 				goto out;
    211   1.6  njoly 			}
    212   1.6  njoly 			idb.d_off = (linux32_off_t)off;
    213   1.6  njoly 			idb.d_reclen = (u_short)linux32_reclen;
    214   1.6  njoly 		}
    215   1.6  njoly 		strcpy(idb.d_name, bdp->d_name);
    216   1.6  njoly 		if ((error = copyout((void *)&idb, outp, linux32_reclen)))
    217   1.6  njoly 			goto out;
    218   1.6  njoly 		/* advance past this real entry */
    219   1.6  njoly 		inp += reclen;
    220   1.6  njoly 		if (cookie)
    221   1.6  njoly 			off = *cookie++; /* each entry points to itself */
    222   1.6  njoly 		else
    223   1.6  njoly 			off += reclen;
    224   1.6  njoly 		/* advance output past Linux-shaped entry */
    225   1.6  njoly 		outp += linux32_reclen;
    226   1.6  njoly 		resid -= linux32_reclen;
    227   1.6  njoly 		if (oldcall)
    228   1.6  njoly 			break;
    229   1.6  njoly 	}
    230   1.6  njoly 
    231   1.6  njoly 	/* if we squished out the whole block, try again */
    232  1.10     he 	if (outp == (void *)SCARG_P32(uap, dent)) {
    233  1.10     he 		if (cookiebuf)
    234  1.10     he 			free(cookiebuf, M_TEMP);
    235  1.10     he 		cookiebuf = NULL;
    236   1.6  njoly 		goto again;
    237  1.10     he 	}
    238   1.6  njoly 	fp->f_offset = off;	/* update the vnode offset */
    239   1.6  njoly 
    240   1.6  njoly 	if (oldcall)
    241   1.6  njoly 		nbytes = resid + linux32_reclen;
    242   1.6  njoly 
    243   1.6  njoly eof:
    244   1.6  njoly 	*retval = nbytes - resid;
    245   1.6  njoly out:
    246   1.6  njoly 	VOP_UNLOCK(vp, 0);
    247   1.6  njoly 	if (cookiebuf)
    248   1.6  njoly 		free(cookiebuf, M_TEMP);
    249   1.6  njoly 	free(tbuf, M_TEMP);
    250   1.6  njoly out1:
    251   1.6  njoly 	fd_putfile(SCARG(uap, fd));
    252   1.6  njoly 	return error;
    253   1.1   manu }
    254   1.1   manu 
    255   1.1   manu int
    256   1.4    dsl linux32_sys_getdents64(struct lwp *l, const struct linux32_sys_getdents64_args *uap, register_t *retval)
    257   1.1   manu {
    258   1.4    dsl 	/* {
    259   1.8  njoly 		syscallarg(int) fd;
    260   1.8  njoly 		syscallarg(linux32_dirent64p_t) dent;
    261   1.8  njoly 		syscallarg(unsigned int) count;
    262   1.4    dsl 	} */
    263   1.1   manu 	struct linux_sys_getdents64_args ua;
    264   1.1   manu 
    265   1.1   manu 	NETBSD32TO64_UAP(fd);
    266   1.1   manu 	NETBSD32TOP_UAP(dent, struct linux_dirent64);
    267   1.1   manu 	NETBSD32TO64_UAP(count);
    268   1.1   manu 
    269   1.1   manu 	return linux_sys_getdents64(l, &ua, retval);
    270   1.1   manu }
    271   1.1   manu 
    272   1.5  njoly int
    273   1.5  njoly linux32_sys_readdir(struct lwp *l, const struct linux32_sys_readdir_args *uap, register_t *retval)
    274   1.5  njoly {
    275   1.5  njoly 	/* {
    276   1.5  njoly 		syscallarg(int) fd;
    277   1.5  njoly 		syscallarg(struct linux32_direntp_t) dent;
    278   1.5  njoly 		syscallarg(unsigned int) count;
    279   1.5  njoly 	} */
    280   1.5  njoly 	int error;
    281   1.5  njoly 	struct linux32_sys_getdents_args da;
    282   1.5  njoly 
    283   1.5  njoly 	SCARG(&da, fd) = SCARG(uap, fd);
    284   1.5  njoly 	SCARG(&da, dent) = SCARG(uap, dent);
    285   1.5  njoly 	SCARG(&da, count) = 1;
    286   1.5  njoly 
    287   1.5  njoly 	error = linux32_sys_getdents(l, &da, retval);
    288   1.5  njoly 	if (error == 0 && *retval > 1)
    289   1.5  njoly 		*retval = 1;
    290   1.5  njoly 
    291   1.5  njoly 	return error;
    292   1.5  njoly }
    293