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