Home | History | Annotate | Line # | Download | only in common
linux_file64.c revision 1.2.4.9
      1  1.2.4.9   nathanw /*	$NetBSD: linux_file64.c,v 1.2.4.9 2002/05/29 21:32:38 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.9   nathanw __KERNEL_RCSID(0, "$NetBSD: linux_file64.c,v 1.2.4.9 2002/05/29 21:32:38 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.1  jdolecek #include <sys/file.h>
     51      1.1  jdolecek #include <sys/stat.h>
     52      1.1  jdolecek #include <sys/filedesc.h>
     53      1.1  jdolecek #include <sys/ioctl.h>
     54      1.1  jdolecek #include <sys/kernel.h>
     55      1.1  jdolecek #include <sys/mount.h>
     56      1.1  jdolecek #include <sys/malloc.h>
     57      1.1  jdolecek #include <sys/vnode.h>
     58      1.1  jdolecek #include <sys/tty.h>
     59      1.1  jdolecek #include <sys/conf.h>
     60      1.1  jdolecek 
     61  1.2.4.9   nathanw #include <sys/sa.h>
     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.1  jdolecek 
     70      1.1  jdolecek #include <compat/linux/linux_syscallargs.h>
     71      1.1  jdolecek 
     72      1.1  jdolecek static void bsd_to_linux_stat __P((struct stat *, struct linux_stat64 *));
     73  1.2.4.1   nathanw static int linux_do_stat64 __P((struct lwp *, void *, register_t *, int));
     74      1.1  jdolecek 
     75      1.1  jdolecek /*
     76      1.1  jdolecek  * Convert a NetBSD stat structure to a Linux stat structure.
     77      1.1  jdolecek  * Only the order of the fields and the padding in the structure
     78      1.1  jdolecek  * is different. linux_fakedev is a machine-dependent function
     79      1.1  jdolecek  * which optionally converts device driver major/minor numbers
     80      1.1  jdolecek  * (XXX horrible, but what can you do against code that compares
     81      1.1  jdolecek  * things against constant major device numbers? sigh)
     82      1.1  jdolecek  */
     83      1.1  jdolecek static void
     84      1.1  jdolecek bsd_to_linux_stat(bsp, lsp)
     85      1.1  jdolecek 	struct stat *bsp;
     86      1.1  jdolecek 	struct linux_stat64 *lsp;
     87      1.1  jdolecek {
     88  1.2.4.6   nathanw 	lsp->lst_dev     = linux_fakedev(bsp->st_dev, 0);
     89      1.1  jdolecek 	lsp->lst_ino     = bsp->st_ino;
     90      1.1  jdolecek 	lsp->lst_mode    = (linux_mode_t)bsp->st_mode;
     91      1.1  jdolecek 	if (bsp->st_nlink >= (1 << 15))
     92      1.1  jdolecek 		lsp->lst_nlink = (1 << 15) - 1;
     93      1.1  jdolecek 	else
     94      1.1  jdolecek 		lsp->lst_nlink = (linux_nlink_t)bsp->st_nlink;
     95      1.1  jdolecek 	lsp->lst_uid     = bsp->st_uid;
     96      1.1  jdolecek 	lsp->lst_gid     = bsp->st_gid;
     97  1.2.4.6   nathanw 	lsp->lst_rdev    = linux_fakedev(bsp->st_rdev, 1);
     98      1.1  jdolecek 	lsp->lst_size    = bsp->st_size;
     99      1.1  jdolecek 	lsp->lst_blksize = bsp->st_blksize;
    100      1.1  jdolecek 	lsp->lst_blocks  = bsp->st_blocks;
    101      1.1  jdolecek 	lsp->lst_atime   = bsp->st_atime;
    102      1.1  jdolecek 	lsp->lst_mtime   = bsp->st_mtime;
    103      1.1  jdolecek 	lsp->lst_ctime   = bsp->st_ctime;
    104  1.2.4.6   nathanw 	lsp->lst_ino64   = bsp->st_ino;
    105      1.1  jdolecek }
    106      1.1  jdolecek 
    107      1.1  jdolecek /*
    108      1.1  jdolecek  * The stat functions below are plain sailing. stat and lstat are handled
    109      1.1  jdolecek  * by one function to avoid code duplication.
    110      1.1  jdolecek  */
    111      1.1  jdolecek int
    112  1.2.4.1   nathanw linux_sys_fstat64(l, v, retval)
    113  1.2.4.1   nathanw 	struct lwp *l;
    114      1.1  jdolecek 	void *v;
    115      1.1  jdolecek 	register_t *retval;
    116      1.1  jdolecek {
    117      1.1  jdolecek 	struct linux_sys_fstat64_args /* {
    118      1.1  jdolecek 		syscallarg(int) fd;
    119  1.2.4.3   nathanw 		syscallarg(struct linux_stat64 *) sp;
    120      1.1  jdolecek 	} */ *uap = v;
    121  1.2.4.1   nathanw 	struct proc *p = l->l_proc;
    122      1.1  jdolecek 	struct sys___fstat13_args fsa;
    123      1.1  jdolecek 	struct linux_stat64 tmplst;
    124      1.1  jdolecek 	struct stat *st,tmpst;
    125      1.1  jdolecek 	caddr_t sg;
    126      1.1  jdolecek 	int error;
    127      1.1  jdolecek 
    128  1.2.4.7   nathanw 	sg = stackgap_init(p, 0);
    129      1.1  jdolecek 
    130  1.2.4.7   nathanw 	st = stackgap_alloc(p, &sg, sizeof (struct stat));
    131      1.1  jdolecek 
    132      1.1  jdolecek 	SCARG(&fsa, fd) = SCARG(uap, fd);
    133      1.1  jdolecek 	SCARG(&fsa, sb) = st;
    134      1.1  jdolecek 
    135  1.2.4.1   nathanw 	if ((error = sys___fstat13(l, &fsa, retval)))
    136      1.1  jdolecek 		return error;
    137      1.1  jdolecek 
    138      1.1  jdolecek 	if ((error = copyin(st, &tmpst, sizeof tmpst)))
    139      1.1  jdolecek 		return error;
    140      1.1  jdolecek 
    141      1.1  jdolecek 	bsd_to_linux_stat(&tmpst, &tmplst);
    142      1.1  jdolecek 
    143      1.1  jdolecek 	if ((error = copyout(&tmplst, SCARG(uap, sp), sizeof tmplst)))
    144      1.1  jdolecek 		return error;
    145      1.1  jdolecek 
    146      1.1  jdolecek 	return 0;
    147      1.1  jdolecek }
    148      1.1  jdolecek 
    149      1.1  jdolecek static int
    150  1.2.4.1   nathanw linux_do_stat64(l, v, retval, dolstat)
    151  1.2.4.1   nathanw 	struct lwp *l;
    152      1.1  jdolecek 	void *v;
    153      1.1  jdolecek 	register_t *retval;
    154      1.1  jdolecek 	int dolstat;
    155      1.1  jdolecek {
    156  1.2.4.1   nathanw 	struct proc *p = l->l_proc;
    157      1.1  jdolecek 	struct sys___stat13_args sa;
    158      1.1  jdolecek 	struct linux_stat64 tmplst;
    159      1.1  jdolecek 	struct stat *st, tmpst;
    160      1.1  jdolecek 	caddr_t sg;
    161      1.1  jdolecek 	int error;
    162      1.1  jdolecek 	struct linux_sys_stat64_args *uap = v;
    163      1.1  jdolecek 
    164  1.2.4.7   nathanw 	sg = stackgap_init(p, 0);
    165  1.2.4.7   nathanw 	st = stackgap_alloc(p, &sg, sizeof (struct stat));
    166      1.1  jdolecek 	CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    167      1.1  jdolecek 
    168      1.1  jdolecek 	SCARG(&sa, ub) = st;
    169      1.1  jdolecek 	SCARG(&sa, path) = SCARG(uap, path);
    170      1.1  jdolecek 
    171  1.2.4.1   nathanw 	if ((error = (dolstat ? sys___lstat13(l, &sa, retval) :
    172  1.2.4.1   nathanw 				sys___stat13(l, &sa, retval))))
    173      1.1  jdolecek 		return error;
    174      1.1  jdolecek 
    175      1.1  jdolecek 	if ((error = copyin(st, &tmpst, sizeof tmpst)))
    176      1.1  jdolecek 		return error;
    177      1.1  jdolecek 
    178      1.1  jdolecek 	bsd_to_linux_stat(&tmpst, &tmplst);
    179      1.1  jdolecek 
    180      1.1  jdolecek 	if ((error = copyout(&tmplst, SCARG(uap, sp), sizeof tmplst)))
    181      1.1  jdolecek 		return error;
    182      1.1  jdolecek 
    183      1.1  jdolecek 	return 0;
    184      1.1  jdolecek }
    185      1.1  jdolecek 
    186      1.1  jdolecek int
    187  1.2.4.1   nathanw linux_sys_stat64(l, v, retval)
    188  1.2.4.1   nathanw 	struct lwp *l;
    189      1.1  jdolecek 	void *v;
    190      1.1  jdolecek 	register_t *retval;
    191      1.1  jdolecek {
    192      1.1  jdolecek 	struct linux_sys_stat64_args /* {
    193      1.1  jdolecek 		syscallarg(const char *) path;
    194      1.1  jdolecek 		syscallarg(struct linux_stat64 *) sp;
    195      1.1  jdolecek 	} */ *uap = v;
    196      1.1  jdolecek 
    197  1.2.4.1   nathanw 	return linux_do_stat64(l, uap, retval, 0);
    198      1.1  jdolecek }
    199      1.1  jdolecek 
    200      1.1  jdolecek int
    201  1.2.4.1   nathanw linux_sys_lstat64(l, v, retval)
    202  1.2.4.1   nathanw 	struct lwp *l;
    203      1.1  jdolecek 	void *v;
    204      1.1  jdolecek 	register_t *retval;
    205      1.1  jdolecek {
    206      1.1  jdolecek 	struct linux_sys_lstat64_args /* {
    207      1.1  jdolecek 		syscallarg(const char *) path;
    208      1.1  jdolecek 		syscallarg(struct linux_stat64 *) sp;
    209      1.1  jdolecek 	} */ *uap = v;
    210      1.1  jdolecek 
    211  1.2.4.1   nathanw 	return linux_do_stat64(l, uap, retval, 1);
    212      1.2  jdolecek }
    213      1.2  jdolecek 
    214      1.2  jdolecek int
    215  1.2.4.1   nathanw linux_sys_truncate64(l, v, retval)
    216  1.2.4.1   nathanw 	struct lwp *l;
    217      1.2  jdolecek 	void *v;
    218      1.2  jdolecek 	register_t *retval;
    219      1.2  jdolecek {
    220      1.2  jdolecek 	struct linux_sys_truncate64_args /* {
    221      1.2  jdolecek 		syscallarg(const char *) path;
    222      1.2  jdolecek 		syscallarg(off_t) length;
    223      1.2  jdolecek 	} */ *uap = v;
    224  1.2.4.1   nathanw 	struct proc *p = l->l_proc;
    225  1.2.4.7   nathanw 	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.4.1   nathanw 	return sys_truncate(l, uap, retval);
    230      1.1  jdolecek }
    231  1.2.4.2   nathanw 
    232  1.2.4.7   nathanw #if defined(__mips__) || defined(__i386__) /* powerpc could use it too */
    233  1.2.4.7   nathanw static void bsd_to_linux_flock64 __P((struct linux_flock64 *,
    234  1.2.4.7   nathanw     const struct flock *));
    235  1.2.4.7   nathanw static void linux_to_bsd_flock64 __P((struct flock *,
    236  1.2.4.7   nathanw     const struct linux_flock64 *));
    237  1.2.4.7   nathanw 
    238  1.2.4.7   nathanw static void
    239  1.2.4.7   nathanw bsd_to_linux_flock64(lfp, bfp)
    240  1.2.4.7   nathanw 	struct linux_flock64 *lfp;
    241  1.2.4.7   nathanw 	const struct flock *bfp;
    242  1.2.4.7   nathanw {
    243  1.2.4.7   nathanw 
    244  1.2.4.7   nathanw 	lfp->l_start = bfp->l_start;
    245  1.2.4.7   nathanw 	lfp->l_len = bfp->l_len;
    246  1.2.4.7   nathanw 	lfp->l_pid = bfp->l_pid;
    247  1.2.4.7   nathanw 	lfp->l_whence = bfp->l_whence;
    248  1.2.4.7   nathanw 	switch (bfp->l_type) {
    249  1.2.4.7   nathanw 	case F_RDLCK:
    250  1.2.4.7   nathanw 		lfp->l_type = LINUX_F_RDLCK;
    251  1.2.4.7   nathanw 		break;
    252  1.2.4.7   nathanw 	case F_UNLCK:
    253  1.2.4.7   nathanw 		lfp->l_type = LINUX_F_UNLCK;
    254  1.2.4.7   nathanw 		break;
    255  1.2.4.7   nathanw 	case F_WRLCK:
    256  1.2.4.7   nathanw 		lfp->l_type = LINUX_F_WRLCK;
    257  1.2.4.7   nathanw 		break;
    258  1.2.4.7   nathanw 	}
    259  1.2.4.7   nathanw }
    260  1.2.4.7   nathanw 
    261  1.2.4.7   nathanw static void
    262  1.2.4.7   nathanw linux_to_bsd_flock64(bfp, lfp)
    263  1.2.4.7   nathanw 	struct flock *bfp;
    264  1.2.4.7   nathanw 	const struct linux_flock64 *lfp;
    265  1.2.4.7   nathanw {
    266  1.2.4.7   nathanw 
    267  1.2.4.7   nathanw 	bfp->l_start = lfp->l_start;
    268  1.2.4.7   nathanw 	bfp->l_len = lfp->l_len;
    269  1.2.4.7   nathanw 	bfp->l_pid = lfp->l_pid;
    270  1.2.4.7   nathanw 	bfp->l_whence = lfp->l_whence;
    271  1.2.4.7   nathanw 	switch (lfp->l_type) {
    272  1.2.4.7   nathanw 	case LINUX_F_RDLCK:
    273  1.2.4.7   nathanw 		bfp->l_type = F_RDLCK;
    274  1.2.4.7   nathanw 		break;
    275  1.2.4.7   nathanw 	case LINUX_F_UNLCK:
    276  1.2.4.7   nathanw 		bfp->l_type = F_UNLCK;
    277  1.2.4.7   nathanw 		break;
    278  1.2.4.7   nathanw 	case LINUX_F_WRLCK:
    279  1.2.4.7   nathanw 		bfp->l_type = F_WRLCK;
    280  1.2.4.7   nathanw 		break;
    281  1.2.4.7   nathanw 	}
    282  1.2.4.7   nathanw }
    283  1.2.4.2   nathanw int
    284  1.2.4.5       wdk linux_sys_fcntl64(l, v, retval)
    285  1.2.4.5       wdk 	struct lwp *l;
    286  1.2.4.2   nathanw 	void *v;
    287  1.2.4.2   nathanw 	register_t *retval;
    288  1.2.4.2   nathanw {
    289  1.2.4.2   nathanw 	struct linux_sys_fcntl64_args /* {
    290  1.2.4.7   nathanw 		syscallarg(int) fd;
    291  1.2.4.7   nathanw 		syscallarg(int) cmd;
    292  1.2.4.7   nathanw 		syscallarg(void *) arg;
    293  1.2.4.2   nathanw 	} */ *uap = v;
    294  1.2.4.7   nathanw 	struct proc *p = l->l_proc;
    295  1.2.4.7   nathanw 	struct sys_fcntl_args fca;
    296  1.2.4.7   nathanw 	struct linux_flock64 lfl;
    297  1.2.4.7   nathanw 	struct flock bfl, *bfp;
    298  1.2.4.2   nathanw 	int error;
    299  1.2.4.7   nathanw 	caddr_t sg;
    300  1.2.4.7   nathanw 	void *arg = SCARG(uap, arg);
    301  1.2.4.7   nathanw 	int cmd = SCARG(uap, cmd);
    302  1.2.4.7   nathanw 	int fd = SCARG(uap, fd);
    303  1.2.4.2   nathanw 
    304  1.2.4.2   nathanw 	switch (cmd) {
    305  1.2.4.7   nathanw 	case LINUX_F_GETLK64:
    306  1.2.4.7   nathanw 		sg = stackgap_init(p, 0);
    307  1.2.4.7   nathanw 		bfp = (struct flock *) stackgap_alloc(p, &sg, sizeof *bfp);
    308  1.2.4.7   nathanw 		if ((error = copyin(arg, &lfl, sizeof lfl)) != 0)
    309  1.2.4.7   nathanw 			return error;
    310  1.2.4.7   nathanw 		linux_to_bsd_flock64(&bfl, &lfl);
    311  1.2.4.7   nathanw 		if ((error = copyout(&bfl, bfp, sizeof bfl)) != 0)
    312  1.2.4.7   nathanw 			return error;
    313  1.2.4.7   nathanw 		SCARG(&fca, fd) = fd;
    314  1.2.4.7   nathanw 		SCARG(&fca, cmd) = F_GETLK;
    315  1.2.4.7   nathanw 		SCARG(&fca, arg) = bfp;
    316  1.2.4.8   nathanw 		if ((error = sys_fcntl(l, &fca, retval)) != 0)
    317  1.2.4.7   nathanw 			return error;
    318  1.2.4.7   nathanw 		if ((error = copyin(bfp, &bfl, sizeof bfl)) != 0)
    319  1.2.4.7   nathanw 			return error;
    320  1.2.4.7   nathanw 		bsd_to_linux_flock64(&lfl, &bfl);
    321  1.2.4.7   nathanw 		return copyout(&lfl, arg, sizeof lfl);
    322  1.2.4.7   nathanw 	case LINUX_F_SETLK64:
    323  1.2.4.7   nathanw 	case LINUX_F_SETLKW64:
    324  1.2.4.7   nathanw 		cmd = (cmd == LINUX_F_SETLK64 ? F_SETLK : F_SETLKW);
    325  1.2.4.7   nathanw 		if ((error = copyin(arg, &lfl, sizeof lfl)) != 0)
    326  1.2.4.7   nathanw 			return error;
    327  1.2.4.7   nathanw 		linux_to_bsd_flock64(&bfl, &lfl);
    328  1.2.4.7   nathanw 		sg = stackgap_init(p, 0);
    329  1.2.4.7   nathanw 		bfp = (struct flock *) stackgap_alloc(p, &sg, sizeof *bfp);
    330  1.2.4.7   nathanw 		if ((error = copyout(&bfl, bfp, sizeof bfl)) != 0)
    331  1.2.4.7   nathanw 			return error;
    332  1.2.4.7   nathanw 		SCARG(&fca, fd) = fd;
    333  1.2.4.7   nathanw 		SCARG(&fca, cmd) = cmd;
    334  1.2.4.7   nathanw 		SCARG(&fca, arg) = bfp;
    335  1.2.4.8   nathanw 		return sys_fcntl(l, &fca, retval);
    336  1.2.4.7   nathanw 	default:
    337  1.2.4.7   nathanw 		return linux_sys_fcntl(l, v, retval);
    338  1.2.4.2   nathanw 	}
    339  1.2.4.2   nathanw 
    340  1.2.4.2   nathanw 	return error;
    341  1.2.4.2   nathanw }
    342  1.2.4.7   nathanw #endif /* __mips__ || __i386__ */
    343