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