Home | History | Annotate | Line # | Download | only in common
linux_file64.c revision 1.2.6.3
      1  1.2.6.3  jdolecek /*	$NetBSD: linux_file64.c,v 1.2.6.3 2002/06/23 17:44:22 jdolecek 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.1  jdolecek 
     43  1.2.6.1   thorpej #include <sys/cdefs.h>
     44  1.2.6.3  jdolecek __KERNEL_RCSID(0, "$NetBSD: linux_file64.c,v 1.2.6.3 2002/06/23 17:44:22 jdolecek Exp $");
     45  1.2.6.1   thorpej 
     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.2.6.3  jdolecek #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.2.6.3  jdolecek #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.2.6.3  jdolecek #ifndef alpha
     74  1.2.6.3  jdolecek 
     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.2.6.2  jdolecek 	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.2.6.2  jdolecek 	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.2.6.3  jdolecek #if LINUX_STAT64_HAS_BROKEN_ST_INO
    108  1.2.6.3  jdolecek 	lsp->__lst_ino   = (linux_ino_t) bsp->st_ino;
    109  1.2.6.3  jdolecek #endif
    110      1.1  jdolecek }
    111      1.1  jdolecek 
    112      1.1  jdolecek /*
    113      1.1  jdolecek  * The stat functions below are plain sailing. stat and lstat are handled
    114      1.1  jdolecek  * by one function to avoid code duplication.
    115      1.1  jdolecek  */
    116      1.1  jdolecek int
    117      1.1  jdolecek linux_sys_fstat64(p, v, retval)
    118      1.1  jdolecek 	struct proc *p;
    119      1.1  jdolecek 	void *v;
    120      1.1  jdolecek 	register_t *retval;
    121      1.1  jdolecek {
    122      1.1  jdolecek 	struct linux_sys_fstat64_args /* {
    123      1.1  jdolecek 		syscallarg(int) fd;
    124  1.2.6.1   thorpej 		syscallarg(struct linux_stat64 *) sp;
    125      1.1  jdolecek 	} */ *uap = v;
    126      1.1  jdolecek 	struct sys___fstat13_args fsa;
    127      1.1  jdolecek 	struct linux_stat64 tmplst;
    128      1.1  jdolecek 	struct stat *st,tmpst;
    129      1.1  jdolecek 	caddr_t sg;
    130      1.1  jdolecek 	int error;
    131      1.1  jdolecek 
    132  1.2.6.3  jdolecek 	sg = stackgap_init(p, 0);
    133      1.1  jdolecek 
    134  1.2.6.3  jdolecek 	st = stackgap_alloc(p, &sg, sizeof (struct stat));
    135      1.1  jdolecek 
    136      1.1  jdolecek 	SCARG(&fsa, fd) = SCARG(uap, fd);
    137      1.1  jdolecek 	SCARG(&fsa, sb) = st;
    138      1.1  jdolecek 
    139      1.1  jdolecek 	if ((error = sys___fstat13(p, &fsa, retval)))
    140      1.1  jdolecek 		return error;
    141      1.1  jdolecek 
    142      1.1  jdolecek 	if ((error = copyin(st, &tmpst, sizeof tmpst)))
    143      1.1  jdolecek 		return error;
    144      1.1  jdolecek 
    145      1.1  jdolecek 	bsd_to_linux_stat(&tmpst, &tmplst);
    146      1.1  jdolecek 
    147      1.1  jdolecek 	if ((error = copyout(&tmplst, SCARG(uap, sp), sizeof tmplst)))
    148      1.1  jdolecek 		return error;
    149      1.1  jdolecek 
    150      1.1  jdolecek 	return 0;
    151      1.1  jdolecek }
    152      1.1  jdolecek 
    153      1.1  jdolecek static int
    154      1.1  jdolecek linux_do_stat64(p, v, retval, dolstat)
    155      1.1  jdolecek 	struct proc *p;
    156      1.1  jdolecek 	void *v;
    157      1.1  jdolecek 	register_t *retval;
    158      1.1  jdolecek 	int dolstat;
    159      1.1  jdolecek {
    160      1.1  jdolecek 	struct sys___stat13_args sa;
    161      1.1  jdolecek 	struct linux_stat64 tmplst;
    162      1.1  jdolecek 	struct stat *st, tmpst;
    163      1.1  jdolecek 	caddr_t sg;
    164      1.1  jdolecek 	int error;
    165      1.1  jdolecek 	struct linux_sys_stat64_args *uap = v;
    166      1.1  jdolecek 
    167  1.2.6.3  jdolecek 	sg = stackgap_init(p, 0);
    168  1.2.6.3  jdolecek 	st = stackgap_alloc(p, &sg, sizeof (struct stat));
    169      1.1  jdolecek 	CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    170      1.1  jdolecek 
    171      1.1  jdolecek 	SCARG(&sa, ub) = st;
    172      1.1  jdolecek 	SCARG(&sa, path) = SCARG(uap, path);
    173      1.1  jdolecek 
    174      1.1  jdolecek 	if ((error = (dolstat ? sys___lstat13(p, &sa, retval) :
    175      1.1  jdolecek 				sys___stat13(p, &sa, retval))))
    176      1.1  jdolecek 		return error;
    177      1.1  jdolecek 
    178      1.1  jdolecek 	if ((error = copyin(st, &tmpst, sizeof tmpst)))
    179      1.1  jdolecek 		return error;
    180      1.1  jdolecek 
    181      1.1  jdolecek 	bsd_to_linux_stat(&tmpst, &tmplst);
    182      1.1  jdolecek 
    183      1.1  jdolecek 	if ((error = copyout(&tmplst, SCARG(uap, sp), sizeof tmplst)))
    184      1.1  jdolecek 		return error;
    185      1.1  jdolecek 
    186      1.1  jdolecek 	return 0;
    187      1.1  jdolecek }
    188      1.1  jdolecek 
    189      1.1  jdolecek int
    190      1.1  jdolecek linux_sys_stat64(p, v, retval)
    191      1.1  jdolecek 	struct proc *p;
    192      1.1  jdolecek 	void *v;
    193      1.1  jdolecek 	register_t *retval;
    194      1.1  jdolecek {
    195      1.1  jdolecek 	struct linux_sys_stat64_args /* {
    196      1.1  jdolecek 		syscallarg(const char *) path;
    197      1.1  jdolecek 		syscallarg(struct linux_stat64 *) sp;
    198      1.1  jdolecek 	} */ *uap = v;
    199      1.1  jdolecek 
    200      1.1  jdolecek 	return linux_do_stat64(p, uap, retval, 0);
    201      1.1  jdolecek }
    202      1.1  jdolecek 
    203      1.1  jdolecek int
    204      1.1  jdolecek linux_sys_lstat64(p, v, retval)
    205      1.1  jdolecek 	struct proc *p;
    206      1.1  jdolecek 	void *v;
    207      1.1  jdolecek 	register_t *retval;
    208      1.1  jdolecek {
    209      1.1  jdolecek 	struct linux_sys_lstat64_args /* {
    210      1.1  jdolecek 		syscallarg(const char *) path;
    211      1.1  jdolecek 		syscallarg(struct linux_stat64 *) sp;
    212      1.1  jdolecek 	} */ *uap = v;
    213      1.1  jdolecek 
    214      1.1  jdolecek 	return linux_do_stat64(p, uap, retval, 1);
    215      1.2  jdolecek }
    216      1.2  jdolecek 
    217      1.2  jdolecek int
    218      1.2  jdolecek linux_sys_truncate64(p, v, retval)
    219      1.2  jdolecek 	struct proc *p;
    220      1.2  jdolecek 	void *v;
    221      1.2  jdolecek 	register_t *retval;
    222      1.2  jdolecek {
    223      1.2  jdolecek 	struct linux_sys_truncate64_args /* {
    224      1.2  jdolecek 		syscallarg(const char *) path;
    225      1.2  jdolecek 		syscallarg(off_t) length;
    226      1.2  jdolecek 	} */ *uap = v;
    227  1.2.6.3  jdolecek 	caddr_t sg = stackgap_init(p, 0);
    228      1.2  jdolecek 
    229      1.2  jdolecek 	CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    230      1.2  jdolecek 
    231      1.2  jdolecek 	return sys_truncate(p, uap, retval);
    232      1.1  jdolecek }
    233  1.2.6.1   thorpej 
    234  1.2.6.3  jdolecek #if !defined(__m68k__)
    235  1.2.6.3  jdolecek static void bsd_to_linux_flock64 __P((struct linux_flock64 *,
    236  1.2.6.3  jdolecek     const struct flock *));
    237  1.2.6.3  jdolecek static void linux_to_bsd_flock64 __P((struct flock *,
    238  1.2.6.3  jdolecek     const struct linux_flock64 *));
    239  1.2.6.3  jdolecek 
    240  1.2.6.3  jdolecek static void
    241  1.2.6.3  jdolecek bsd_to_linux_flock64(lfp, bfp)
    242  1.2.6.3  jdolecek 	struct linux_flock64 *lfp;
    243  1.2.6.3  jdolecek 	const struct flock *bfp;
    244  1.2.6.3  jdolecek {
    245  1.2.6.3  jdolecek 
    246  1.2.6.3  jdolecek 	lfp->l_start = bfp->l_start;
    247  1.2.6.3  jdolecek 	lfp->l_len = bfp->l_len;
    248  1.2.6.3  jdolecek 	lfp->l_pid = bfp->l_pid;
    249  1.2.6.3  jdolecek 	lfp->l_whence = bfp->l_whence;
    250  1.2.6.3  jdolecek 	switch (bfp->l_type) {
    251  1.2.6.3  jdolecek 	case F_RDLCK:
    252  1.2.6.3  jdolecek 		lfp->l_type = LINUX_F_RDLCK;
    253  1.2.6.3  jdolecek 		break;
    254  1.2.6.3  jdolecek 	case F_UNLCK:
    255  1.2.6.3  jdolecek 		lfp->l_type = LINUX_F_UNLCK;
    256  1.2.6.3  jdolecek 		break;
    257  1.2.6.3  jdolecek 	case F_WRLCK:
    258  1.2.6.3  jdolecek 		lfp->l_type = LINUX_F_WRLCK;
    259  1.2.6.3  jdolecek 		break;
    260  1.2.6.3  jdolecek 	}
    261  1.2.6.3  jdolecek }
    262  1.2.6.3  jdolecek 
    263  1.2.6.3  jdolecek static void
    264  1.2.6.3  jdolecek linux_to_bsd_flock64(bfp, lfp)
    265  1.2.6.3  jdolecek 	struct flock *bfp;
    266  1.2.6.3  jdolecek 	const struct linux_flock64 *lfp;
    267  1.2.6.3  jdolecek {
    268  1.2.6.3  jdolecek 
    269  1.2.6.3  jdolecek 	bfp->l_start = lfp->l_start;
    270  1.2.6.3  jdolecek 	bfp->l_len = lfp->l_len;
    271  1.2.6.3  jdolecek 	bfp->l_pid = lfp->l_pid;
    272  1.2.6.3  jdolecek 	bfp->l_whence = lfp->l_whence;
    273  1.2.6.3  jdolecek 	switch (lfp->l_type) {
    274  1.2.6.3  jdolecek 	case LINUX_F_RDLCK:
    275  1.2.6.3  jdolecek 		bfp->l_type = F_RDLCK;
    276  1.2.6.3  jdolecek 		break;
    277  1.2.6.3  jdolecek 	case LINUX_F_UNLCK:
    278  1.2.6.3  jdolecek 		bfp->l_type = F_UNLCK;
    279  1.2.6.3  jdolecek 		break;
    280  1.2.6.3  jdolecek 	case LINUX_F_WRLCK:
    281  1.2.6.3  jdolecek 		bfp->l_type = F_WRLCK;
    282  1.2.6.3  jdolecek 		break;
    283  1.2.6.3  jdolecek 	}
    284  1.2.6.3  jdolecek }
    285  1.2.6.3  jdolecek 
    286  1.2.6.1   thorpej int
    287  1.2.6.1   thorpej linux_sys_fcntl64(p, v, retval)
    288  1.2.6.1   thorpej 	struct proc *p;
    289  1.2.6.1   thorpej 	void *v;
    290  1.2.6.1   thorpej 	register_t *retval;
    291  1.2.6.1   thorpej {
    292  1.2.6.1   thorpej 	struct linux_sys_fcntl64_args /* {
    293  1.2.6.3  jdolecek 		syscallarg(int) fd;
    294  1.2.6.3  jdolecek 		syscallarg(int) cmd;
    295  1.2.6.3  jdolecek 		syscallarg(void *) arg;
    296  1.2.6.1   thorpej 	} */ *uap = v;
    297  1.2.6.3  jdolecek 	struct sys_fcntl_args fca;
    298  1.2.6.3  jdolecek 	struct linux_flock64 lfl;
    299  1.2.6.3  jdolecek 	struct flock bfl, *bfp;
    300  1.2.6.1   thorpej 	int error;
    301  1.2.6.3  jdolecek 	caddr_t sg;
    302  1.2.6.3  jdolecek 	void *arg = SCARG(uap, arg);
    303  1.2.6.3  jdolecek 	int cmd = SCARG(uap, cmd);
    304  1.2.6.3  jdolecek 	int fd = SCARG(uap, fd);
    305  1.2.6.1   thorpej 
    306  1.2.6.1   thorpej 	switch (cmd) {
    307  1.2.6.3  jdolecek 	case LINUX_F_GETLK64:
    308  1.2.6.3  jdolecek 		sg = stackgap_init(p, 0);
    309  1.2.6.3  jdolecek 		bfp = (struct flock *) stackgap_alloc(p, &sg, sizeof *bfp);
    310  1.2.6.3  jdolecek 		if ((error = copyin(arg, &lfl, sizeof lfl)) != 0)
    311  1.2.6.3  jdolecek 			return error;
    312  1.2.6.3  jdolecek 		linux_to_bsd_flock64(&bfl, &lfl);
    313  1.2.6.3  jdolecek 		if ((error = copyout(&bfl, bfp, sizeof bfl)) != 0)
    314  1.2.6.3  jdolecek 			return error;
    315  1.2.6.3  jdolecek 		SCARG(&fca, fd) = fd;
    316  1.2.6.3  jdolecek 		SCARG(&fca, cmd) = F_GETLK;
    317  1.2.6.3  jdolecek 		SCARG(&fca, arg) = bfp;
    318  1.2.6.3  jdolecek 		if ((error = sys_fcntl(p, &fca, retval)) != 0)
    319  1.2.6.3  jdolecek 			return error;
    320  1.2.6.3  jdolecek 		if ((error = copyin(bfp, &bfl, sizeof bfl)) != 0)
    321  1.2.6.3  jdolecek 			return error;
    322  1.2.6.3  jdolecek 		bsd_to_linux_flock64(&lfl, &bfl);
    323  1.2.6.3  jdolecek 		return copyout(&lfl, arg, sizeof lfl);
    324  1.2.6.3  jdolecek 	case LINUX_F_SETLK64:
    325  1.2.6.3  jdolecek 	case LINUX_F_SETLKW64:
    326  1.2.6.3  jdolecek 		cmd = (cmd == LINUX_F_SETLK64 ? F_SETLK : F_SETLKW);
    327  1.2.6.3  jdolecek 		if ((error = copyin(arg, &lfl, sizeof lfl)) != 0)
    328  1.2.6.3  jdolecek 			return error;
    329  1.2.6.3  jdolecek 		linux_to_bsd_flock64(&bfl, &lfl);
    330  1.2.6.3  jdolecek 		sg = stackgap_init(p, 0);
    331  1.2.6.3  jdolecek 		bfp = (struct flock *) stackgap_alloc(p, &sg, sizeof *bfp);
    332  1.2.6.3  jdolecek 		if ((error = copyout(&bfl, bfp, sizeof bfl)) != 0)
    333  1.2.6.3  jdolecek 			return error;
    334  1.2.6.3  jdolecek 		SCARG(&fca, fd) = fd;
    335  1.2.6.3  jdolecek 		SCARG(&fca, cmd) = cmd;
    336  1.2.6.3  jdolecek 		SCARG(&fca, arg) = bfp;
    337  1.2.6.3  jdolecek 		return sys_fcntl(p, &fca, retval);
    338  1.2.6.3  jdolecek 	default:
    339  1.2.6.3  jdolecek 		return linux_sys_fcntl(p, v, retval);
    340  1.2.6.3  jdolecek 	}
    341  1.2.6.3  jdolecek }
    342  1.2.6.3  jdolecek #endif /* !m68k */
    343  1.2.6.3  jdolecek 
    344  1.2.6.3  jdolecek #endif /* !alpha */
    345  1.2.6.3  jdolecek 
    346  1.2.6.3  jdolecek /*
    347  1.2.6.3  jdolecek  * Linux 'readdir' call. This code is mostly taken from the
    348  1.2.6.3  jdolecek  * SunOS getdents call (see compat/sunos/sunos_misc.c), though
    349  1.2.6.3  jdolecek  * an attempt has been made to keep it a little cleaner.
    350  1.2.6.3  jdolecek  *
    351  1.2.6.3  jdolecek  * The d_off field contains the offset of the next valid entry,
    352  1.2.6.3  jdolecek  * unless the older Linux getdents(2), which used to have it set
    353  1.2.6.3  jdolecek  * to the offset of the entry itself. This function also doesn't
    354  1.2.6.3  jdolecek  * need to deal with the old count == 1 glibc problem.
    355  1.2.6.3  jdolecek  *
    356  1.2.6.3  jdolecek  * Read in BSD-style entries, convert them, and copy them out.
    357  1.2.6.3  jdolecek  *
    358  1.2.6.3  jdolecek  * Note that this doesn't handle union-mounted filesystems.
    359  1.2.6.3  jdolecek  */
    360  1.2.6.3  jdolecek int
    361  1.2.6.3  jdolecek linux_sys_getdents64(p, v, retval)
    362  1.2.6.3  jdolecek 	struct proc *p;
    363  1.2.6.3  jdolecek 	void *v;
    364  1.2.6.3  jdolecek 	register_t *retval;
    365  1.2.6.3  jdolecek {
    366  1.2.6.3  jdolecek 	struct linux_sys_getdents_args /* {
    367  1.2.6.3  jdolecek 		syscallarg(int) fd;
    368  1.2.6.3  jdolecek 		syscallarg(struct linux_dirent64 *) dent;
    369  1.2.6.3  jdolecek 		syscallarg(unsigned int) count;
    370  1.2.6.3  jdolecek 	} */ *uap = v;
    371  1.2.6.3  jdolecek 	struct dirent *bdp;
    372  1.2.6.3  jdolecek 	struct vnode *vp;
    373  1.2.6.3  jdolecek 	caddr_t	inp, buf;		/* BSD-format */
    374  1.2.6.3  jdolecek 	int len, reclen;		/* BSD-format */
    375  1.2.6.3  jdolecek 	caddr_t outp;			/* Linux-format */
    376  1.2.6.3  jdolecek 	int resid, linux_reclen = 0;	/* Linux-format */
    377  1.2.6.3  jdolecek 	struct file *fp;
    378  1.2.6.3  jdolecek 	struct uio auio;
    379  1.2.6.3  jdolecek 	struct iovec aiov;
    380  1.2.6.3  jdolecek 	struct linux_dirent64 idb;
    381  1.2.6.3  jdolecek 	off_t off;		/* true file offset */
    382  1.2.6.3  jdolecek 	int buflen, error, eofflag, nbytes;
    383  1.2.6.3  jdolecek 	struct vattr va;
    384  1.2.6.3  jdolecek 	off_t *cookiebuf = NULL, *cookie;
    385  1.2.6.3  jdolecek 	int ncookies;
    386  1.2.6.3  jdolecek 
    387  1.2.6.3  jdolecek 	/* getvnode() will use the descriptor for us */
    388  1.2.6.3  jdolecek 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
    389  1.2.6.3  jdolecek 		return (error);
    390  1.2.6.3  jdolecek 
    391  1.2.6.3  jdolecek 	if ((fp->f_flag & FREAD) == 0) {
    392  1.2.6.3  jdolecek 		error = EBADF;
    393  1.2.6.3  jdolecek 		goto out1;
    394  1.2.6.3  jdolecek 	}
    395  1.2.6.3  jdolecek 
    396  1.2.6.3  jdolecek 	vp = (struct vnode *)fp->f_data;
    397  1.2.6.3  jdolecek 	if (vp->v_type != VDIR) {
    398  1.2.6.3  jdolecek 		error = EINVAL;
    399  1.2.6.3  jdolecek 		goto out1;
    400  1.2.6.3  jdolecek 	}
    401  1.2.6.3  jdolecek 
    402  1.2.6.3  jdolecek 	if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)))
    403  1.2.6.3  jdolecek 		goto out1;
    404  1.2.6.3  jdolecek 
    405  1.2.6.3  jdolecek 	nbytes = SCARG(uap, count);
    406  1.2.6.3  jdolecek 	buflen = min(MAXBSIZE, nbytes);
    407  1.2.6.3  jdolecek 	if (buflen < va.va_blocksize)
    408  1.2.6.3  jdolecek 		buflen = va.va_blocksize;
    409  1.2.6.3  jdolecek 	buf = malloc(buflen, M_TEMP, M_WAITOK);
    410  1.2.6.3  jdolecek 
    411  1.2.6.3  jdolecek 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    412  1.2.6.3  jdolecek 	off = fp->f_offset;
    413  1.2.6.3  jdolecek again:
    414  1.2.6.3  jdolecek 	aiov.iov_base = buf;
    415  1.2.6.3  jdolecek 	aiov.iov_len = buflen;
    416  1.2.6.3  jdolecek 	auio.uio_iov = &aiov;
    417  1.2.6.3  jdolecek 	auio.uio_iovcnt = 1;
    418  1.2.6.3  jdolecek 	auio.uio_rw = UIO_READ;
    419  1.2.6.3  jdolecek 	auio.uio_segflg = UIO_SYSSPACE;
    420  1.2.6.3  jdolecek 	auio.uio_procp = p;
    421  1.2.6.3  jdolecek 	auio.uio_resid = buflen;
    422  1.2.6.3  jdolecek 	auio.uio_offset = off;
    423  1.2.6.3  jdolecek 	/*
    424  1.2.6.3  jdolecek          * First we read into the malloc'ed buffer, then
    425  1.2.6.3  jdolecek          * we massage it into user space, one record at a time.
    426  1.2.6.3  jdolecek          */
    427  1.2.6.3  jdolecek 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
    428  1.2.6.3  jdolecek 	    &ncookies);
    429  1.2.6.3  jdolecek 	if (error)
    430  1.2.6.3  jdolecek 		goto out;
    431  1.2.6.3  jdolecek 
    432  1.2.6.3  jdolecek 	inp = buf;
    433  1.2.6.3  jdolecek 	outp = (caddr_t)SCARG(uap, dent);
    434  1.2.6.3  jdolecek 	resid = nbytes;
    435  1.2.6.3  jdolecek 	if ((len = buflen - auio.uio_resid) == 0)
    436  1.2.6.3  jdolecek 		goto eof;
    437  1.2.6.3  jdolecek 
    438  1.2.6.3  jdolecek 	for (cookie = cookiebuf; len > 0; len -= reclen) {
    439  1.2.6.3  jdolecek 		bdp = (struct dirent *)inp;
    440  1.2.6.3  jdolecek 		reclen = bdp->d_reclen;
    441  1.2.6.3  jdolecek 		if (reclen & 3)
    442  1.2.6.3  jdolecek 			panic("linux_readdir");
    443  1.2.6.3  jdolecek 		if (bdp->d_fileno == 0) {
    444  1.2.6.3  jdolecek 			inp += reclen;	/* it is a hole; squish it out */
    445  1.2.6.3  jdolecek 			off = *cookie++;
    446  1.2.6.3  jdolecek 			continue;
    447  1.2.6.3  jdolecek 		}
    448  1.2.6.3  jdolecek 		linux_reclen = LINUX_RECLEN(&idb, bdp->d_namlen);
    449  1.2.6.3  jdolecek 		if (reclen > len || resid < linux_reclen) {
    450  1.2.6.3  jdolecek 			/* entry too big for buffer, so just stop */
    451  1.2.6.3  jdolecek 			outp++;
    452  1.2.6.1   thorpej 			break;
    453  1.2.6.3  jdolecek 		}
    454  1.2.6.3  jdolecek 		off = *cookie++;	/* each entry points to next */
    455  1.2.6.3  jdolecek 		/*
    456  1.2.6.3  jdolecek 		 * Massage in place to make a Linux-shaped dirent (otherwise
    457  1.2.6.3  jdolecek 		 * we have to worry about touching user memory outside of
    458  1.2.6.3  jdolecek 		 * the copyout() call).
    459  1.2.6.3  jdolecek 		 */
    460  1.2.6.3  jdolecek 		idb.d_ino = bdp->d_fileno;
    461  1.2.6.3  jdolecek 		idb.d_type = bdp->d_type;
    462  1.2.6.3  jdolecek 		idb.d_off = off;
    463  1.2.6.3  jdolecek 		idb.d_reclen = (u_short)linux_reclen;
    464  1.2.6.3  jdolecek 		strcpy(idb.d_name, bdp->d_name);
    465  1.2.6.3  jdolecek 		if ((error = copyout((caddr_t)&idb, outp, linux_reclen)))
    466  1.2.6.3  jdolecek 			goto out;
    467  1.2.6.3  jdolecek 		/* advance past this real entry */
    468  1.2.6.3  jdolecek 		inp += reclen;
    469  1.2.6.3  jdolecek 		/* advance output past Linux-shaped entry */
    470  1.2.6.3  jdolecek 		outp += linux_reclen;
    471  1.2.6.3  jdolecek 		resid -= linux_reclen;
    472  1.2.6.1   thorpej 	}
    473  1.2.6.1   thorpej 
    474  1.2.6.3  jdolecek 	/* if we squished out the whole block, try again */
    475  1.2.6.3  jdolecek 	if (outp == (caddr_t)SCARG(uap, dent))
    476  1.2.6.3  jdolecek 		goto again;
    477  1.2.6.3  jdolecek 	fp->f_offset = off;	/* update the vnode offset */
    478  1.2.6.3  jdolecek 
    479  1.2.6.3  jdolecek eof:
    480  1.2.6.3  jdolecek 	*retval = nbytes - resid;
    481  1.2.6.3  jdolecek out:
    482  1.2.6.3  jdolecek 	VOP_UNLOCK(vp, 0);
    483  1.2.6.3  jdolecek 	if (cookiebuf)
    484  1.2.6.3  jdolecek 		free(cookiebuf, M_TEMP);
    485  1.2.6.3  jdolecek 	free(buf, M_TEMP);
    486  1.2.6.3  jdolecek out1:
    487  1.2.6.3  jdolecek 	FILE_UNUSE(fp, p);
    488  1.2.6.1   thorpej 	return error;
    489  1.2.6.1   thorpej }
    490