Home | History | Annotate | Line # | Download | only in common
linux_file64.c revision 1.2.2.2
      1  1.2.2.2  bouyer /*	$NetBSD: linux_file64.c,v 1.2.2.2 2000/12/13 15:49:49 bouyer Exp $	*/
      2  1.2.2.2  bouyer 
      3  1.2.2.2  bouyer /*-
      4  1.2.2.2  bouyer  * Copyright (c) 1995, 1998, 2000 The NetBSD Foundation, Inc.
      5  1.2.2.2  bouyer  * All rights reserved.
      6  1.2.2.2  bouyer  *
      7  1.2.2.2  bouyer  * This code is derived from software contributed to The NetBSD Foundation
      8  1.2.2.2  bouyer  * by Frank van der Linden and Eric Haszlakiewicz.
      9  1.2.2.2  bouyer  *
     10  1.2.2.2  bouyer  * Redistribution and use in source and binary forms, with or without
     11  1.2.2.2  bouyer  * modification, are permitted provided that the following conditions
     12  1.2.2.2  bouyer  * are met:
     13  1.2.2.2  bouyer  * 1. Redistributions of source code must retain the above copyright
     14  1.2.2.2  bouyer  *    notice, this list of conditions and the following disclaimer.
     15  1.2.2.2  bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.2.2.2  bouyer  *    notice, this list of conditions and the following disclaimer in the
     17  1.2.2.2  bouyer  *    documentation and/or other materials provided with the distribution.
     18  1.2.2.2  bouyer  * 3. All advertising materials mentioning features or use of this software
     19  1.2.2.2  bouyer  *    must display the following acknowledgement:
     20  1.2.2.2  bouyer  *	This product includes software developed by the NetBSD
     21  1.2.2.2  bouyer  *	Foundation, Inc. and its contributors.
     22  1.2.2.2  bouyer  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.2.2.2  bouyer  *    contributors may be used to endorse or promote products derived
     24  1.2.2.2  bouyer  *    from this software without specific prior written permission.
     25  1.2.2.2  bouyer  *
     26  1.2.2.2  bouyer  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.2.2.2  bouyer  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.2.2.2  bouyer  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.2.2.2  bouyer  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.2.2.2  bouyer  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.2.2.2  bouyer  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.2.2.2  bouyer  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.2.2.2  bouyer  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.2.2.2  bouyer  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.2.2.2  bouyer  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.2.2.2  bouyer  * POSSIBILITY OF SUCH DAMAGE.
     37  1.2.2.2  bouyer  */
     38  1.2.2.2  bouyer 
     39  1.2.2.2  bouyer /*
     40  1.2.2.2  bouyer  * Linux 64bit filesystem calls. Used on 32bit archs, not used on 64bit ones.
     41  1.2.2.2  bouyer  */
     42  1.2.2.2  bouyer 
     43  1.2.2.2  bouyer #include <sys/param.h>
     44  1.2.2.2  bouyer #include <sys/systm.h>
     45  1.2.2.2  bouyer #include <sys/namei.h>
     46  1.2.2.2  bouyer #include <sys/proc.h>
     47  1.2.2.2  bouyer #include <sys/file.h>
     48  1.2.2.2  bouyer #include <sys/stat.h>
     49  1.2.2.2  bouyer #include <sys/filedesc.h>
     50  1.2.2.2  bouyer #include <sys/ioctl.h>
     51  1.2.2.2  bouyer #include <sys/kernel.h>
     52  1.2.2.2  bouyer #include <sys/mount.h>
     53  1.2.2.2  bouyer #include <sys/malloc.h>
     54  1.2.2.2  bouyer #include <sys/vnode.h>
     55  1.2.2.2  bouyer #include <sys/tty.h>
     56  1.2.2.2  bouyer #include <sys/conf.h>
     57  1.2.2.2  bouyer 
     58  1.2.2.2  bouyer #include <sys/syscallargs.h>
     59  1.2.2.2  bouyer 
     60  1.2.2.2  bouyer #include <compat/linux/common/linux_types.h>
     61  1.2.2.2  bouyer #include <compat/linux/common/linux_signal.h>
     62  1.2.2.2  bouyer #include <compat/linux/common/linux_fcntl.h>
     63  1.2.2.2  bouyer #include <compat/linux/common/linux_util.h>
     64  1.2.2.2  bouyer #include <compat/linux/common/linux_machdep.h>
     65  1.2.2.2  bouyer 
     66  1.2.2.2  bouyer #include <compat/linux/linux_syscallargs.h>
     67  1.2.2.2  bouyer 
     68  1.2.2.2  bouyer static void bsd_to_linux_stat __P((struct stat *, struct linux_stat64 *));
     69  1.2.2.2  bouyer static int linux_do_stat64 __P((struct proc *, void *, register_t *, int));
     70  1.2.2.2  bouyer 
     71  1.2.2.2  bouyer /*
     72  1.2.2.2  bouyer  * Convert a NetBSD stat structure to a Linux stat structure.
     73  1.2.2.2  bouyer  * Only the order of the fields and the padding in the structure
     74  1.2.2.2  bouyer  * is different. linux_fakedev is a machine-dependent function
     75  1.2.2.2  bouyer  * which optionally converts device driver major/minor numbers
     76  1.2.2.2  bouyer  * (XXX horrible, but what can you do against code that compares
     77  1.2.2.2  bouyer  * things against constant major device numbers? sigh)
     78  1.2.2.2  bouyer  */
     79  1.2.2.2  bouyer static void
     80  1.2.2.2  bouyer bsd_to_linux_stat(bsp, lsp)
     81  1.2.2.2  bouyer 	struct stat *bsp;
     82  1.2.2.2  bouyer 	struct linux_stat64 *lsp;
     83  1.2.2.2  bouyer {
     84  1.2.2.2  bouyer 	lsp->lst_dev     = bsp->st_dev;
     85  1.2.2.2  bouyer 	lsp->lst_ino     = bsp->st_ino;
     86  1.2.2.2  bouyer 	lsp->lst_mode    = (linux_mode_t)bsp->st_mode;
     87  1.2.2.2  bouyer 	if (bsp->st_nlink >= (1 << 15))
     88  1.2.2.2  bouyer 		lsp->lst_nlink = (1 << 15) - 1;
     89  1.2.2.2  bouyer 	else
     90  1.2.2.2  bouyer 		lsp->lst_nlink = (linux_nlink_t)bsp->st_nlink;
     91  1.2.2.2  bouyer 	lsp->lst_uid     = bsp->st_uid;
     92  1.2.2.2  bouyer 	lsp->lst_gid     = bsp->st_gid;
     93  1.2.2.2  bouyer 	lsp->lst_rdev    = linux_fakedev(bsp->st_rdev);
     94  1.2.2.2  bouyer 	lsp->lst_size    = bsp->st_size;
     95  1.2.2.2  bouyer 	lsp->lst_blksize = bsp->st_blksize;
     96  1.2.2.2  bouyer 	lsp->lst_blocks  = bsp->st_blocks;
     97  1.2.2.2  bouyer 	lsp->lst_atime   = bsp->st_atime;
     98  1.2.2.2  bouyer 	lsp->lst_mtime   = bsp->st_mtime;
     99  1.2.2.2  bouyer 	lsp->lst_ctime   = bsp->st_ctime;
    100  1.2.2.2  bouyer }
    101  1.2.2.2  bouyer 
    102  1.2.2.2  bouyer /*
    103  1.2.2.2  bouyer  * The stat functions below are plain sailing. stat and lstat are handled
    104  1.2.2.2  bouyer  * by one function to avoid code duplication.
    105  1.2.2.2  bouyer  */
    106  1.2.2.2  bouyer int
    107  1.2.2.2  bouyer linux_sys_fstat64(p, v, retval)
    108  1.2.2.2  bouyer 	struct proc *p;
    109  1.2.2.2  bouyer 	void *v;
    110  1.2.2.2  bouyer 	register_t *retval;
    111  1.2.2.2  bouyer {
    112  1.2.2.2  bouyer 	struct linux_sys_fstat64_args /* {
    113  1.2.2.2  bouyer 		syscallarg(int) fd;
    114  1.2.2.2  bouyer 		syscallarg(linux_stat64 *) sp;
    115  1.2.2.2  bouyer 	} */ *uap = v;
    116  1.2.2.2  bouyer 	struct sys___fstat13_args fsa;
    117  1.2.2.2  bouyer 	struct linux_stat64 tmplst;
    118  1.2.2.2  bouyer 	struct stat *st,tmpst;
    119  1.2.2.2  bouyer 	caddr_t sg;
    120  1.2.2.2  bouyer 	int error;
    121  1.2.2.2  bouyer 
    122  1.2.2.2  bouyer 	sg = stackgap_init(p->p_emul);
    123  1.2.2.2  bouyer 
    124  1.2.2.2  bouyer 	st = stackgap_alloc(&sg, sizeof (struct stat));
    125  1.2.2.2  bouyer 
    126  1.2.2.2  bouyer 	SCARG(&fsa, fd) = SCARG(uap, fd);
    127  1.2.2.2  bouyer 	SCARG(&fsa, sb) = st;
    128  1.2.2.2  bouyer 
    129  1.2.2.2  bouyer 	if ((error = sys___fstat13(p, &fsa, retval)))
    130  1.2.2.2  bouyer 		return error;
    131  1.2.2.2  bouyer 
    132  1.2.2.2  bouyer 	if ((error = copyin(st, &tmpst, sizeof tmpst)))
    133  1.2.2.2  bouyer 		return error;
    134  1.2.2.2  bouyer 
    135  1.2.2.2  bouyer 	bsd_to_linux_stat(&tmpst, &tmplst);
    136  1.2.2.2  bouyer 
    137  1.2.2.2  bouyer 	if ((error = copyout(&tmplst, SCARG(uap, sp), sizeof tmplst)))
    138  1.2.2.2  bouyer 		return error;
    139  1.2.2.2  bouyer 
    140  1.2.2.2  bouyer 	return 0;
    141  1.2.2.2  bouyer }
    142  1.2.2.2  bouyer 
    143  1.2.2.2  bouyer static int
    144  1.2.2.2  bouyer linux_do_stat64(p, v, retval, dolstat)
    145  1.2.2.2  bouyer 	struct proc *p;
    146  1.2.2.2  bouyer 	void *v;
    147  1.2.2.2  bouyer 	register_t *retval;
    148  1.2.2.2  bouyer 	int dolstat;
    149  1.2.2.2  bouyer {
    150  1.2.2.2  bouyer 	struct sys___stat13_args sa;
    151  1.2.2.2  bouyer 	struct linux_stat64 tmplst;
    152  1.2.2.2  bouyer 	struct stat *st, tmpst;
    153  1.2.2.2  bouyer 	caddr_t sg;
    154  1.2.2.2  bouyer 	int error;
    155  1.2.2.2  bouyer 	struct linux_sys_stat64_args *uap = v;
    156  1.2.2.2  bouyer 
    157  1.2.2.2  bouyer 	sg = stackgap_init(p->p_emul);
    158  1.2.2.2  bouyer 	st = stackgap_alloc(&sg, sizeof (struct stat));
    159  1.2.2.2  bouyer 	CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    160  1.2.2.2  bouyer 
    161  1.2.2.2  bouyer 	SCARG(&sa, ub) = st;
    162  1.2.2.2  bouyer 	SCARG(&sa, path) = SCARG(uap, path);
    163  1.2.2.2  bouyer 
    164  1.2.2.2  bouyer 	if ((error = (dolstat ? sys___lstat13(p, &sa, retval) :
    165  1.2.2.2  bouyer 				sys___stat13(p, &sa, retval))))
    166  1.2.2.2  bouyer 		return error;
    167  1.2.2.2  bouyer 
    168  1.2.2.2  bouyer 	if ((error = copyin(st, &tmpst, sizeof tmpst)))
    169  1.2.2.2  bouyer 		return error;
    170  1.2.2.2  bouyer 
    171  1.2.2.2  bouyer 	bsd_to_linux_stat(&tmpst, &tmplst);
    172  1.2.2.2  bouyer 
    173  1.2.2.2  bouyer 	if ((error = copyout(&tmplst, SCARG(uap, sp), sizeof tmplst)))
    174  1.2.2.2  bouyer 		return error;
    175  1.2.2.2  bouyer 
    176  1.2.2.2  bouyer 	return 0;
    177  1.2.2.2  bouyer }
    178  1.2.2.2  bouyer 
    179  1.2.2.2  bouyer int
    180  1.2.2.2  bouyer linux_sys_stat64(p, v, retval)
    181  1.2.2.2  bouyer 	struct proc *p;
    182  1.2.2.2  bouyer 	void *v;
    183  1.2.2.2  bouyer 	register_t *retval;
    184  1.2.2.2  bouyer {
    185  1.2.2.2  bouyer 	struct linux_sys_stat64_args /* {
    186  1.2.2.2  bouyer 		syscallarg(const char *) path;
    187  1.2.2.2  bouyer 		syscallarg(struct linux_stat64 *) sp;
    188  1.2.2.2  bouyer 	} */ *uap = v;
    189  1.2.2.2  bouyer 
    190  1.2.2.2  bouyer 	return linux_do_stat64(p, uap, retval, 0);
    191  1.2.2.2  bouyer }
    192  1.2.2.2  bouyer 
    193  1.2.2.2  bouyer int
    194  1.2.2.2  bouyer linux_sys_lstat64(p, v, retval)
    195  1.2.2.2  bouyer 	struct proc *p;
    196  1.2.2.2  bouyer 	void *v;
    197  1.2.2.2  bouyer 	register_t *retval;
    198  1.2.2.2  bouyer {
    199  1.2.2.2  bouyer 	struct linux_sys_lstat64_args /* {
    200  1.2.2.2  bouyer 		syscallarg(const char *) path;
    201  1.2.2.2  bouyer 		syscallarg(struct linux_stat64 *) sp;
    202  1.2.2.2  bouyer 	} */ *uap = v;
    203  1.2.2.2  bouyer 
    204  1.2.2.2  bouyer 	return linux_do_stat64(p, uap, retval, 1);
    205  1.2.2.2  bouyer }
    206  1.2.2.2  bouyer 
    207  1.2.2.2  bouyer int
    208  1.2.2.2  bouyer linux_sys_truncate64(p, v, retval)
    209  1.2.2.2  bouyer 	struct proc *p;
    210  1.2.2.2  bouyer 	void *v;
    211  1.2.2.2  bouyer 	register_t *retval;
    212  1.2.2.2  bouyer {
    213  1.2.2.2  bouyer 	struct linux_sys_truncate64_args /* {
    214  1.2.2.2  bouyer 		syscallarg(const char *) path;
    215  1.2.2.2  bouyer 		syscallarg(off_t) length;
    216  1.2.2.2  bouyer 	} */ *uap = v;
    217  1.2.2.2  bouyer 	caddr_t sg = stackgap_init(p->p_emul);
    218  1.2.2.2  bouyer 
    219  1.2.2.2  bouyer 	CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    220  1.2.2.2  bouyer 
    221  1.2.2.2  bouyer 	return sys_truncate(p, uap, retval);
    222  1.2.2.2  bouyer }
    223