Home | History | Annotate | Line # | Download | only in common
linux_file64.c revision 1.15
      1  1.15      matt /*	$NetBSD: linux_file64.c,v 1.15 2002/05/13 05:41:27 matt Exp $	*/
      2   1.1  jdolecek 
      3   1.1  jdolecek /*-
      4   1.1  jdolecek  * Copyright (c) 1995, 1998, 2000 The NetBSD Foundation, Inc.
      5   1.1  jdolecek  * All rights reserved.
      6   1.1  jdolecek  *
      7   1.1  jdolecek  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1  jdolecek  * by Frank van der Linden and Eric Haszlakiewicz.
      9   1.1  jdolecek  *
     10   1.1  jdolecek  * Redistribution and use in source and binary forms, with or without
     11   1.1  jdolecek  * modification, are permitted provided that the following conditions
     12   1.1  jdolecek  * are met:
     13   1.1  jdolecek  * 1. Redistributions of source code must retain the above copyright
     14   1.1  jdolecek  *    notice, this list of conditions and the following disclaimer.
     15   1.1  jdolecek  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1  jdolecek  *    notice, this list of conditions and the following disclaimer in the
     17   1.1  jdolecek  *    documentation and/or other materials provided with the distribution.
     18   1.1  jdolecek  * 3. All advertising materials mentioning features or use of this software
     19   1.1  jdolecek  *    must display the following acknowledgement:
     20   1.1  jdolecek  *	This product includes software developed by the NetBSD
     21   1.1  jdolecek  *	Foundation, Inc. and its contributors.
     22   1.1  jdolecek  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23   1.1  jdolecek  *    contributors may be used to endorse or promote products derived
     24   1.1  jdolecek  *    from this software without specific prior written permission.
     25   1.1  jdolecek  *
     26   1.1  jdolecek  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27   1.1  jdolecek  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28   1.1  jdolecek  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29   1.1  jdolecek  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30   1.1  jdolecek  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31   1.1  jdolecek  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32   1.1  jdolecek  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33   1.1  jdolecek  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34   1.1  jdolecek  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35   1.1  jdolecek  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36   1.1  jdolecek  * POSSIBILITY OF SUCH DAMAGE.
     37   1.1  jdolecek  */
     38   1.1  jdolecek 
     39   1.1  jdolecek /*
     40   1.1  jdolecek  * Linux 64bit filesystem calls. Used on 32bit archs, not used on 64bit ones.
     41   1.1  jdolecek  */
     42   1.6     lukem 
     43   1.6     lukem #include <sys/cdefs.h>
     44  1.15      matt __KERNEL_RCSID(0, "$NetBSD: linux_file64.c,v 1.15 2002/05/13 05:41:27 matt Exp $");
     45   1.1  jdolecek 
     46   1.1  jdolecek #include <sys/param.h>
     47   1.1  jdolecek #include <sys/systm.h>
     48   1.1  jdolecek #include <sys/namei.h>
     49   1.1  jdolecek #include <sys/proc.h>
     50  1.11      tron #include <sys/dirent.h>
     51   1.1  jdolecek #include <sys/file.h>
     52   1.1  jdolecek #include <sys/stat.h>
     53   1.1  jdolecek #include <sys/filedesc.h>
     54   1.1  jdolecek #include <sys/ioctl.h>
     55   1.1  jdolecek #include <sys/kernel.h>
     56   1.1  jdolecek #include <sys/mount.h>
     57   1.1  jdolecek #include <sys/malloc.h>
     58   1.1  jdolecek #include <sys/vnode.h>
     59   1.1  jdolecek #include <sys/tty.h>
     60   1.1  jdolecek #include <sys/conf.h>
     61   1.1  jdolecek 
     62   1.1  jdolecek #include <sys/syscallargs.h>
     63   1.1  jdolecek 
     64   1.1  jdolecek #include <compat/linux/common/linux_types.h>
     65   1.1  jdolecek #include <compat/linux/common/linux_signal.h>
     66   1.1  jdolecek #include <compat/linux/common/linux_fcntl.h>
     67   1.1  jdolecek #include <compat/linux/common/linux_util.h>
     68   1.1  jdolecek #include <compat/linux/common/linux_machdep.h>
     69  1.11      tron #include <compat/linux/common/linux_dirent.h>
     70   1.1  jdolecek 
     71   1.1  jdolecek #include <compat/linux/linux_syscallargs.h>
     72   1.1  jdolecek 
     73  1.15      matt #ifndef alpha
     74  1.15      matt 
     75   1.1  jdolecek static void bsd_to_linux_stat __P((struct stat *, struct linux_stat64 *));
     76   1.1  jdolecek static int linux_do_stat64 __P((struct proc *, void *, register_t *, int));
     77   1.1  jdolecek 
     78   1.1  jdolecek /*
     79   1.1  jdolecek  * Convert a NetBSD stat structure to a Linux stat structure.
     80   1.1  jdolecek  * Only the order of the fields and the padding in the structure
     81   1.1  jdolecek  * is different. linux_fakedev is a machine-dependent function
     82   1.1  jdolecek  * which optionally converts device driver major/minor numbers
     83   1.1  jdolecek  * (XXX horrible, but what can you do against code that compares
     84   1.1  jdolecek  * things against constant major device numbers? sigh)
     85   1.1  jdolecek  */
     86   1.1  jdolecek static void
     87   1.1  jdolecek bsd_to_linux_stat(bsp, lsp)
     88   1.1  jdolecek 	struct stat *bsp;
     89   1.1  jdolecek 	struct linux_stat64 *lsp;
     90   1.1  jdolecek {
     91   1.7  christos 	lsp->lst_dev     = linux_fakedev(bsp->st_dev, 0);
     92   1.1  jdolecek 	lsp->lst_ino     = bsp->st_ino;
     93   1.1  jdolecek 	lsp->lst_mode    = (linux_mode_t)bsp->st_mode;
     94   1.1  jdolecek 	if (bsp->st_nlink >= (1 << 15))
     95   1.1  jdolecek 		lsp->lst_nlink = (1 << 15) - 1;
     96   1.1  jdolecek 	else
     97   1.1  jdolecek 		lsp->lst_nlink = (linux_nlink_t)bsp->st_nlink;
     98   1.1  jdolecek 	lsp->lst_uid     = bsp->st_uid;
     99   1.1  jdolecek 	lsp->lst_gid     = bsp->st_gid;
    100   1.7  christos 	lsp->lst_rdev    = linux_fakedev(bsp->st_rdev, 1);
    101   1.1  jdolecek 	lsp->lst_size    = bsp->st_size;
    102   1.1  jdolecek 	lsp->lst_blksize = bsp->st_blksize;
    103   1.1  jdolecek 	lsp->lst_blocks  = bsp->st_blocks;
    104   1.1  jdolecek 	lsp->lst_atime   = bsp->st_atime;
    105   1.1  jdolecek 	lsp->lst_mtime   = bsp->st_mtime;
    106   1.1  jdolecek 	lsp->lst_ctime   = bsp->st_ctime;
    107   1.7  christos 	lsp->lst_ino64   = bsp->st_ino;
    108   1.1  jdolecek }
    109   1.1  jdolecek 
    110   1.1  jdolecek /*
    111   1.1  jdolecek  * The stat functions below are plain sailing. stat and lstat are handled
    112   1.1  jdolecek  * by one function to avoid code duplication.
    113   1.1  jdolecek  */
    114   1.1  jdolecek int
    115   1.1  jdolecek linux_sys_fstat64(p, v, retval)
    116   1.1  jdolecek 	struct proc *p;
    117   1.1  jdolecek 	void *v;
    118   1.1  jdolecek 	register_t *retval;
    119   1.1  jdolecek {
    120   1.1  jdolecek 	struct linux_sys_fstat64_args /* {
    121   1.1  jdolecek 		syscallarg(int) fd;
    122   1.5      manu 		syscallarg(struct linux_stat64 *) sp;
    123   1.1  jdolecek 	} */ *uap = v;
    124   1.1  jdolecek 	struct sys___fstat13_args fsa;
    125   1.1  jdolecek 	struct linux_stat64 tmplst;
    126   1.1  jdolecek 	struct stat *st,tmpst;
    127   1.1  jdolecek 	caddr_t sg;
    128   1.1  jdolecek 	int error;
    129   1.1  jdolecek 
    130   1.8  christos 	sg = stackgap_init(p, 0);
    131   1.1  jdolecek 
    132   1.8  christos 	st = stackgap_alloc(p, &sg, sizeof (struct stat));
    133   1.1  jdolecek 
    134   1.1  jdolecek 	SCARG(&fsa, fd) = SCARG(uap, fd);
    135   1.1  jdolecek 	SCARG(&fsa, sb) = st;
    136   1.1  jdolecek 
    137   1.1  jdolecek 	if ((error = sys___fstat13(p, &fsa, retval)))
    138   1.1  jdolecek 		return error;
    139   1.1  jdolecek 
    140   1.1  jdolecek 	if ((error = copyin(st, &tmpst, sizeof tmpst)))
    141   1.1  jdolecek 		return error;
    142   1.1  jdolecek 
    143   1.1  jdolecek 	bsd_to_linux_stat(&tmpst, &tmplst);
    144   1.1  jdolecek 
    145   1.1  jdolecek 	if ((error = copyout(&tmplst, SCARG(uap, sp), sizeof tmplst)))
    146   1.1  jdolecek 		return error;
    147   1.1  jdolecek 
    148   1.1  jdolecek 	return 0;
    149   1.1  jdolecek }
    150   1.1  jdolecek 
    151   1.1  jdolecek static int
    152   1.1  jdolecek linux_do_stat64(p, v, retval, dolstat)
    153   1.1  jdolecek 	struct proc *p;
    154   1.1  jdolecek 	void *v;
    155   1.1  jdolecek 	register_t *retval;
    156   1.1  jdolecek 	int dolstat;
    157   1.1  jdolecek {
    158   1.1  jdolecek 	struct sys___stat13_args sa;
    159   1.1  jdolecek 	struct linux_stat64 tmplst;
    160   1.1  jdolecek 	struct stat *st, tmpst;
    161   1.1  jdolecek 	caddr_t sg;
    162   1.1  jdolecek 	int error;
    163   1.1  jdolecek 	struct linux_sys_stat64_args *uap = v;
    164   1.1  jdolecek 
    165   1.8  christos 	sg = stackgap_init(p, 0);
    166   1.8  christos 	st = stackgap_alloc(p, &sg, sizeof (struct stat));
    167   1.1  jdolecek 	CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    168   1.1  jdolecek 
    169   1.1  jdolecek 	SCARG(&sa, ub) = st;
    170   1.1  jdolecek 	SCARG(&sa, path) = SCARG(uap, path);
    171   1.1  jdolecek 
    172   1.1  jdolecek 	if ((error = (dolstat ? sys___lstat13(p, &sa, retval) :
    173   1.1  jdolecek 				sys___stat13(p, &sa, retval))))
    174   1.1  jdolecek 		return error;
    175   1.1  jdolecek 
    176   1.1  jdolecek 	if ((error = copyin(st, &tmpst, sizeof tmpst)))
    177   1.1  jdolecek 		return error;
    178   1.1  jdolecek 
    179   1.1  jdolecek 	bsd_to_linux_stat(&tmpst, &tmplst);
    180   1.1  jdolecek 
    181   1.1  jdolecek 	if ((error = copyout(&tmplst, SCARG(uap, sp), sizeof tmplst)))
    182   1.1  jdolecek 		return error;
    183   1.1  jdolecek 
    184   1.1  jdolecek 	return 0;
    185   1.1  jdolecek }
    186   1.1  jdolecek 
    187   1.1  jdolecek int
    188   1.1  jdolecek linux_sys_stat64(p, v, retval)
    189   1.1  jdolecek 	struct proc *p;
    190   1.1  jdolecek 	void *v;
    191   1.1  jdolecek 	register_t *retval;
    192   1.1  jdolecek {
    193   1.1  jdolecek 	struct linux_sys_stat64_args /* {
    194   1.1  jdolecek 		syscallarg(const char *) path;
    195   1.1  jdolecek 		syscallarg(struct linux_stat64 *) sp;
    196   1.1  jdolecek 	} */ *uap = v;
    197   1.1  jdolecek 
    198   1.1  jdolecek 	return linux_do_stat64(p, uap, retval, 0);
    199   1.1  jdolecek }
    200   1.1  jdolecek 
    201   1.1  jdolecek int
    202   1.1  jdolecek linux_sys_lstat64(p, v, retval)
    203   1.1  jdolecek 	struct proc *p;
    204   1.1  jdolecek 	void *v;
    205   1.1  jdolecek 	register_t *retval;
    206   1.1  jdolecek {
    207   1.1  jdolecek 	struct linux_sys_lstat64_args /* {
    208   1.1  jdolecek 		syscallarg(const char *) path;
    209   1.1  jdolecek 		syscallarg(struct linux_stat64 *) sp;
    210   1.1  jdolecek 	} */ *uap = v;
    211   1.1  jdolecek 
    212   1.1  jdolecek 	return linux_do_stat64(p, uap, retval, 1);
    213   1.2  jdolecek }
    214   1.2  jdolecek 
    215   1.2  jdolecek int
    216   1.2  jdolecek linux_sys_truncate64(p, v, retval)
    217   1.2  jdolecek 	struct proc *p;
    218   1.2  jdolecek 	void *v;
    219   1.2  jdolecek 	register_t *retval;
    220   1.2  jdolecek {
    221   1.2  jdolecek 	struct linux_sys_truncate64_args /* {
    222   1.2  jdolecek 		syscallarg(const char *) path;
    223   1.2  jdolecek 		syscallarg(off_t) length;
    224   1.2  jdolecek 	} */ *uap = v;
    225   1.8  christos 	caddr_t sg = stackgap_init(p, 0);
    226   1.2  jdolecek 
    227   1.2  jdolecek 	CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    228   1.2  jdolecek 
    229   1.2  jdolecek 	return sys_truncate(p, uap, retval);
    230   1.1  jdolecek }
    231   1.3      manu 
    232  1.13  jdolecek #if !defined(__m68k__)
    233   1.9  christos static void bsd_to_linux_flock64 __P((struct linux_flock64 *,
    234   1.9  christos     const struct flock *));
    235   1.9  christos static void linux_to_bsd_flock64 __P((struct flock *,
    236   1.9  christos     const struct linux_flock64 *));
    237   1.9  christos 
    238   1.9  christos static void
    239   1.9  christos bsd_to_linux_flock64(lfp, bfp)
    240   1.9  christos 	struct linux_flock64 *lfp;
    241   1.9  christos 	const struct flock *bfp;
    242   1.9  christos {
    243   1.9  christos 
    244   1.9  christos 	lfp->l_start = bfp->l_start;
    245   1.9  christos 	lfp->l_len = bfp->l_len;
    246   1.9  christos 	lfp->l_pid = bfp->l_pid;
    247   1.9  christos 	lfp->l_whence = bfp->l_whence;
    248   1.9  christos 	switch (bfp->l_type) {
    249   1.9  christos 	case F_RDLCK:
    250   1.9  christos 		lfp->l_type = LINUX_F_RDLCK;
    251   1.9  christos 		break;
    252   1.9  christos 	case F_UNLCK:
    253   1.9  christos 		lfp->l_type = LINUX_F_UNLCK;
    254   1.9  christos 		break;
    255   1.9  christos 	case F_WRLCK:
    256   1.9  christos 		lfp->l_type = LINUX_F_WRLCK;
    257   1.9  christos 		break;
    258   1.9  christos 	}
    259   1.9  christos }
    260   1.9  christos 
    261   1.9  christos static void
    262   1.9  christos linux_to_bsd_flock64(bfp, lfp)
    263   1.9  christos 	struct flock *bfp;
    264   1.9  christos 	const struct linux_flock64 *lfp;
    265   1.9  christos {
    266   1.9  christos 
    267   1.9  christos 	bfp->l_start = lfp->l_start;
    268   1.9  christos 	bfp->l_len = lfp->l_len;
    269   1.9  christos 	bfp->l_pid = lfp->l_pid;
    270   1.9  christos 	bfp->l_whence = lfp->l_whence;
    271   1.9  christos 	switch (lfp->l_type) {
    272   1.9  christos 	case LINUX_F_RDLCK:
    273   1.9  christos 		bfp->l_type = F_RDLCK;
    274   1.9  christos 		break;
    275   1.9  christos 	case LINUX_F_UNLCK:
    276   1.9  christos 		bfp->l_type = F_UNLCK;
    277   1.9  christos 		break;
    278   1.9  christos 	case LINUX_F_WRLCK:
    279   1.9  christos 		bfp->l_type = F_WRLCK;
    280   1.9  christos 		break;
    281   1.9  christos 	}
    282   1.9  christos }
    283  1.13  jdolecek 
    284   1.3      manu int
    285   1.3      manu linux_sys_fcntl64(p, v, retval)
    286   1.3      manu 	struct proc *p;
    287   1.3      manu 	void *v;
    288   1.3      manu 	register_t *retval;
    289   1.3      manu {
    290   1.3      manu 	struct linux_sys_fcntl64_args /* {
    291   1.9  christos 		syscallarg(int) fd;
    292   1.9  christos 		syscallarg(int) cmd;
    293   1.9  christos 		syscallarg(void *) arg;
    294   1.3      manu 	} */ *uap = v;
    295   1.9  christos 	struct sys_fcntl_args fca;
    296   1.9  christos 	struct linux_flock64 lfl;
    297   1.9  christos 	struct flock bfl, *bfp;
    298   1.3      manu 	int error;
    299   1.9  christos 	caddr_t sg;
    300   1.9  christos 	void *arg = SCARG(uap, arg);
    301   1.9  christos 	int cmd = SCARG(uap, cmd);
    302   1.9  christos 	int fd = SCARG(uap, fd);
    303   1.3      manu 
    304   1.3      manu 	switch (cmd) {
    305   1.9  christos 	case LINUX_F_GETLK64:
    306   1.9  christos 		sg = stackgap_init(p, 0);
    307   1.9  christos 		bfp = (struct flock *) stackgap_alloc(p, &sg, sizeof *bfp);
    308   1.9  christos 		if ((error = copyin(arg, &lfl, sizeof lfl)) != 0)
    309   1.9  christos 			return error;
    310   1.9  christos 		linux_to_bsd_flock64(&bfl, &lfl);
    311   1.9  christos 		if ((error = copyout(&bfl, bfp, sizeof bfl)) != 0)
    312   1.9  christos 			return error;
    313   1.9  christos 		SCARG(&fca, fd) = fd;
    314   1.9  christos 		SCARG(&fca, cmd) = F_GETLK;
    315   1.9  christos 		SCARG(&fca, arg) = bfp;
    316   1.9  christos 		if ((error = sys_fcntl(p, &fca, retval)) != 0)
    317   1.9  christos 			return error;
    318   1.9  christos 		if ((error = copyin(bfp, &bfl, sizeof bfl)) != 0)
    319   1.9  christos 			return error;
    320   1.9  christos 		bsd_to_linux_flock64(&lfl, &bfl);
    321   1.9  christos 		return copyout(&lfl, arg, sizeof lfl);
    322   1.9  christos 	case LINUX_F_SETLK64:
    323   1.9  christos 	case LINUX_F_SETLKW64:
    324   1.9  christos 		cmd = (cmd == LINUX_F_SETLK64 ? F_SETLK : F_SETLKW);
    325   1.9  christos 		if ((error = copyin(arg, &lfl, sizeof lfl)) != 0)
    326   1.9  christos 			return error;
    327   1.9  christos 		linux_to_bsd_flock64(&bfl, &lfl);
    328   1.9  christos 		sg = stackgap_init(p, 0);
    329   1.9  christos 		bfp = (struct flock *) stackgap_alloc(p, &sg, sizeof *bfp);
    330   1.9  christos 		if ((error = copyout(&bfl, bfp, sizeof bfl)) != 0)
    331   1.9  christos 			return error;
    332   1.9  christos 		SCARG(&fca, fd) = fd;
    333   1.9  christos 		SCARG(&fca, cmd) = cmd;
    334   1.9  christos 		SCARG(&fca, arg) = bfp;
    335   1.9  christos 		return sys_fcntl(p, &fca, retval);
    336   1.9  christos 	default:
    337   1.9  christos 		return linux_sys_fcntl(p, v, retval);
    338   1.3      manu 	}
    339  1.11      tron }
    340  1.13  jdolecek #endif /* !m68k */
    341  1.15      matt 
    342  1.15      matt #endif /* !alpha */
    343  1.11      tron 
    344  1.11      tron /*
    345  1.11      tron  * Linux 'readdir' call. This code is mostly taken from the
    346  1.11      tron  * SunOS getdents call (see compat/sunos/sunos_misc.c), though
    347  1.11      tron  * an attempt has been made to keep it a little cleaner (failing
    348  1.11      tron  * miserably, because of the cruft needed if count 1 is passed).
    349  1.11      tron  *
    350  1.11      tron  * The d_off field should contain the offset of the next valid entry,
    351  1.11      tron  * but in Linux it has the offset of the entry itself. We emulate
    352  1.11      tron  * that bug here.
    353  1.11      tron  *
    354  1.11      tron  * Read in BSD-style entries, convert them, and copy them out.
    355  1.11      tron  *
    356  1.11      tron  * Note that this doesn't handle union-mounted filesystems.
    357  1.11      tron  */
    358  1.11      tron int
    359  1.11      tron linux_sys_getdents64(p, v, retval)
    360  1.11      tron 	struct proc *p;
    361  1.11      tron 	void *v;
    362  1.11      tron 	register_t *retval;
    363  1.11      tron {
    364  1.11      tron 	struct linux_sys_getdents_args /* {
    365  1.11      tron 		syscallarg(int) fd;
    366  1.11      tron 		syscallarg(struct linux_dirent64 *) dent;
    367  1.11      tron 		syscallarg(unsigned int) count;
    368  1.11      tron 	} */ *uap = v;
    369  1.11      tron 	struct dirent *bdp;
    370  1.11      tron 	struct vnode *vp;
    371  1.11      tron 	caddr_t	inp, buf;		/* BSD-format */
    372  1.11      tron 	int len, reclen;		/* BSD-format */
    373  1.11      tron 	caddr_t outp;			/* Linux-format */
    374  1.11      tron 	int resid, linux_reclen = 0;	/* Linux-format */
    375  1.11      tron 	struct file *fp;
    376  1.11      tron 	struct uio auio;
    377  1.11      tron 	struct iovec aiov;
    378  1.11      tron 	struct linux_dirent64 idb;
    379  1.11      tron 	off_t off;		/* true file offset */
    380  1.11      tron 	int buflen, error, eofflag, nbytes, oldcall;
    381  1.11      tron 	struct vattr va;
    382  1.11      tron 	off_t *cookiebuf = NULL, *cookie;
    383  1.11      tron 	int ncookies;
    384  1.11      tron 
    385  1.11      tron 	/* getvnode() will use the descriptor for us */
    386  1.11      tron 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
    387  1.11      tron 		return (error);
    388  1.11      tron 
    389  1.11      tron 	if ((fp->f_flag & FREAD) == 0) {
    390  1.11      tron 		error = EBADF;
    391  1.11      tron 		goto out1;
    392  1.11      tron 	}
    393  1.11      tron 
    394  1.11      tron 	vp = (struct vnode *)fp->f_data;
    395  1.11      tron 	if (vp->v_type != VDIR) {
    396  1.11      tron 		error = EINVAL;
    397  1.11      tron 		goto out1;
    398  1.11      tron 	}
    399  1.11      tron 
    400  1.11      tron 	if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)))
    401  1.11      tron 		goto out1;
    402  1.11      tron 
    403  1.11      tron 	nbytes = SCARG(uap, count);
    404  1.11      tron 	if (nbytes == 1) {	/* emulating old, broken behaviour */
    405  1.11      tron 		nbytes = sizeof (idb);
    406  1.11      tron 		buflen = max(va.va_blocksize, nbytes);
    407  1.11      tron 		oldcall = 1;
    408  1.11      tron 	} else {
    409  1.11      tron 		buflen = min(MAXBSIZE, nbytes);
    410  1.11      tron 		if (buflen < va.va_blocksize)
    411  1.11      tron 			buflen = va.va_blocksize;
    412  1.11      tron 		oldcall = 0;
    413  1.11      tron 	}
    414  1.11      tron 	buf = malloc(buflen, M_TEMP, M_WAITOK);
    415  1.11      tron 
    416  1.11      tron 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    417  1.11      tron 	off = fp->f_offset;
    418  1.11      tron again:
    419  1.11      tron 	aiov.iov_base = buf;
    420  1.11      tron 	aiov.iov_len = buflen;
    421  1.11      tron 	auio.uio_iov = &aiov;
    422  1.11      tron 	auio.uio_iovcnt = 1;
    423  1.11      tron 	auio.uio_rw = UIO_READ;
    424  1.11      tron 	auio.uio_segflg = UIO_SYSSPACE;
    425  1.11      tron 	auio.uio_procp = p;
    426  1.11      tron 	auio.uio_resid = buflen;
    427  1.11      tron 	auio.uio_offset = off;
    428  1.11      tron 	/*
    429  1.11      tron          * First we read into the malloc'ed buffer, then
    430  1.11      tron          * we massage it into user space, one record at a time.
    431  1.11      tron          */
    432  1.11      tron 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
    433  1.11      tron 	    &ncookies);
    434  1.11      tron 	if (error)
    435  1.11      tron 		goto out;
    436  1.11      tron 
    437  1.11      tron 	inp = buf;
    438  1.11      tron 	outp = (caddr_t)SCARG(uap, dent);
    439  1.11      tron 	resid = nbytes;
    440  1.11      tron 	if ((len = buflen - auio.uio_resid) == 0)
    441  1.11      tron 		goto eof;
    442  1.11      tron 
    443  1.11      tron 	for (cookie = cookiebuf; len > 0; len -= reclen) {
    444  1.11      tron 		bdp = (struct dirent *)inp;
    445  1.11      tron 		reclen = bdp->d_reclen;
    446  1.11      tron 		if (reclen & 3)
    447  1.11      tron 			panic("linux_readdir");
    448  1.11      tron 		if (bdp->d_fileno == 0) {
    449  1.11      tron 			inp += reclen;	/* it is a hole; squish it out */
    450  1.11      tron 			off = *cookie++;
    451  1.11      tron 			continue;
    452  1.11      tron 		}
    453  1.11      tron 		linux_reclen = LINUX_RECLEN(&idb, bdp->d_namlen);
    454  1.11      tron 		if (reclen > len || resid < linux_reclen) {
    455  1.11      tron 			/* entry too big for buffer, so just stop */
    456  1.11      tron 			outp++;
    457  1.11      tron 			break;
    458  1.11      tron 		}
    459  1.11      tron 		/*
    460  1.11      tron 		 * Massage in place to make a Linux-shaped dirent (otherwise
    461  1.11      tron 		 * we have to worry about touching user memory outside of
    462  1.11      tron 		 * the copyout() call).
    463  1.11      tron 		 */
    464  1.11      tron 		idb.d_ino = bdp->d_fileno;
    465  1.11      tron 		idb.d_type = bdp->d_type;
    466  1.11      tron 		/*
    467  1.11      tron 		 * The old readdir() call misuses the offset and reclen fields.
    468  1.11      tron 		 */
    469  1.11      tron 		if (oldcall) {
    470  1.11      tron 			idb.d_off = linux_reclen;
    471  1.11      tron 			idb.d_reclen = (u_short)bdp->d_namlen;
    472  1.11      tron 		} else {
    473  1.11      tron 			idb.d_off = off;
    474  1.11      tron 			idb.d_reclen = (u_short)linux_reclen;
    475  1.11      tron 		}
    476  1.11      tron 		strcpy(idb.d_name, bdp->d_name);
    477  1.11      tron 		if ((error = copyout((caddr_t)&idb, outp, linux_reclen)))
    478  1.11      tron 			goto out;
    479  1.11      tron 		/* advance past this real entry */
    480  1.11      tron 		inp += reclen;
    481  1.11      tron 		off = *cookie++;	/* each entry points to itself */
    482  1.11      tron 		/* advance output past Linux-shaped entry */
    483  1.11      tron 		outp += linux_reclen;
    484  1.11      tron 		resid -= linux_reclen;
    485  1.11      tron 		if (oldcall)
    486  1.11      tron 			break;
    487  1.11      tron 	}
    488  1.11      tron 
    489  1.11      tron 	/* if we squished out the whole block, try again */
    490  1.11      tron 	if (outp == (caddr_t)SCARG(uap, dent))
    491  1.11      tron 		goto again;
    492  1.11      tron 	fp->f_offset = off;	/* update the vnode offset */
    493  1.11      tron 
    494  1.11      tron 	if (oldcall)
    495  1.11      tron 		nbytes = resid + linux_reclen;
    496  1.11      tron 
    497  1.11      tron eof:
    498  1.11      tron 	*retval = nbytes - resid;
    499  1.11      tron out:
    500  1.11      tron 	VOP_UNLOCK(vp, 0);
    501  1.11      tron 	if (cookiebuf)
    502  1.11      tron 		free(cookiebuf, M_TEMP);
    503  1.11      tron 	free(buf, M_TEMP);
    504  1.11      tron out1:
    505  1.11      tron 	FILE_UNUSE(fp, p);
    506   1.3      manu 	return error;
    507   1.3      manu }
    508