Home | History | Annotate | Line # | Download | only in common
linux_file64.c revision 1.2.4.4
      1 /*	$NetBSD: linux_file64.c,v 1.2.4.4 2001/11/14 19:13:10 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/cdefs.h>
     44 __KERNEL_RCSID(0, "$NetBSD: linux_file64.c,v 1.2.4.4 2001/11/14 19:13:10 nathanw Exp $");
     45 
     46 #include <sys/param.h>
     47 #include <sys/systm.h>
     48 #include <sys/namei.h>
     49 #include <sys/proc.h>
     50 #include <sys/file.h>
     51 #include <sys/stat.h>
     52 #include <sys/filedesc.h>
     53 #include <sys/ioctl.h>
     54 #include <sys/kernel.h>
     55 #include <sys/mount.h>
     56 #include <sys/malloc.h>
     57 #include <sys/vnode.h>
     58 #include <sys/tty.h>
     59 #include <sys/conf.h>
     60 
     61 #include <sys/syscallargs.h>
     62 
     63 #include <compat/linux/common/linux_types.h>
     64 #include <compat/linux/common/linux_signal.h>
     65 #include <compat/linux/common/linux_fcntl.h>
     66 #include <compat/linux/common/linux_util.h>
     67 #include <compat/linux/common/linux_machdep.h>
     68 
     69 #include <compat/linux/linux_syscallargs.h>
     70 
     71 static void bsd_to_linux_stat __P((struct stat *, struct linux_stat64 *));
     72 static int linux_do_stat64 __P((struct lwp *, void *, register_t *, int));
     73 
     74 /*
     75  * Convert a NetBSD stat structure to a Linux stat structure.
     76  * Only the order of the fields and the padding in the structure
     77  * is different. linux_fakedev is a machine-dependent function
     78  * which optionally converts device driver major/minor numbers
     79  * (XXX horrible, but what can you do against code that compares
     80  * things against constant major device numbers? sigh)
     81  */
     82 static void
     83 bsd_to_linux_stat(bsp, lsp)
     84 	struct stat *bsp;
     85 	struct linux_stat64 *lsp;
     86 {
     87 	lsp->lst_dev     = bsp->st_dev;
     88 	lsp->lst_ino     = bsp->st_ino;
     89 	lsp->lst_mode    = (linux_mode_t)bsp->st_mode;
     90 	if (bsp->st_nlink >= (1 << 15))
     91 		lsp->lst_nlink = (1 << 15) - 1;
     92 	else
     93 		lsp->lst_nlink = (linux_nlink_t)bsp->st_nlink;
     94 	lsp->lst_uid     = bsp->st_uid;
     95 	lsp->lst_gid     = bsp->st_gid;
     96 	lsp->lst_rdev    = linux_fakedev(bsp->st_rdev);
     97 	lsp->lst_size    = bsp->st_size;
     98 	lsp->lst_blksize = bsp->st_blksize;
     99 	lsp->lst_blocks  = bsp->st_blocks;
    100 	lsp->lst_atime   = bsp->st_atime;
    101 	lsp->lst_mtime   = bsp->st_mtime;
    102 	lsp->lst_ctime   = bsp->st_ctime;
    103 }
    104 
    105 /*
    106  * The stat functions below are plain sailing. stat and lstat are handled
    107  * by one function to avoid code duplication.
    108  */
    109 int
    110 linux_sys_fstat64(l, v, retval)
    111 	struct lwp *l;
    112 	void *v;
    113 	register_t *retval;
    114 {
    115 	struct linux_sys_fstat64_args /* {
    116 		syscallarg(int) fd;
    117 		syscallarg(struct linux_stat64 *) sp;
    118 	} */ *uap = v;
    119 	struct proc *p = l->l_proc;
    120 	struct sys___fstat13_args fsa;
    121 	struct linux_stat64 tmplst;
    122 	struct stat *st,tmpst;
    123 	caddr_t sg;
    124 	int error;
    125 
    126 	sg = stackgap_init(p->p_emul);
    127 
    128 	st = stackgap_alloc(&sg, sizeof (struct stat));
    129 
    130 	SCARG(&fsa, fd) = SCARG(uap, fd);
    131 	SCARG(&fsa, sb) = st;
    132 
    133 	if ((error = sys___fstat13(l, &fsa, retval)))
    134 		return error;
    135 
    136 	if ((error = copyin(st, &tmpst, sizeof tmpst)))
    137 		return error;
    138 
    139 	bsd_to_linux_stat(&tmpst, &tmplst);
    140 
    141 	if ((error = copyout(&tmplst, SCARG(uap, sp), sizeof tmplst)))
    142 		return error;
    143 
    144 	return 0;
    145 }
    146 
    147 static int
    148 linux_do_stat64(l, v, retval, dolstat)
    149 	struct lwp *l;
    150 	void *v;
    151 	register_t *retval;
    152 	int dolstat;
    153 {
    154 	struct proc *p = l->l_proc;
    155 	struct sys___stat13_args sa;
    156 	struct linux_stat64 tmplst;
    157 	struct stat *st, tmpst;
    158 	caddr_t sg;
    159 	int error;
    160 	struct linux_sys_stat64_args *uap = v;
    161 
    162 	sg = stackgap_init(p->p_emul);
    163 	st = stackgap_alloc(&sg, sizeof (struct stat));
    164 	CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    165 
    166 	SCARG(&sa, ub) = st;
    167 	SCARG(&sa, path) = SCARG(uap, path);
    168 
    169 	if ((error = (dolstat ? sys___lstat13(l, &sa, retval) :
    170 				sys___stat13(l, &sa, retval))))
    171 		return error;
    172 
    173 	if ((error = copyin(st, &tmpst, sizeof tmpst)))
    174 		return error;
    175 
    176 	bsd_to_linux_stat(&tmpst, &tmplst);
    177 
    178 	if ((error = copyout(&tmplst, SCARG(uap, sp), sizeof tmplst)))
    179 		return error;
    180 
    181 	return 0;
    182 }
    183 
    184 int
    185 linux_sys_stat64(l, v, retval)
    186 	struct lwp *l;
    187 	void *v;
    188 	register_t *retval;
    189 {
    190 	struct linux_sys_stat64_args /* {
    191 		syscallarg(const char *) path;
    192 		syscallarg(struct linux_stat64 *) sp;
    193 	} */ *uap = v;
    194 
    195 	return linux_do_stat64(l, uap, retval, 0);
    196 }
    197 
    198 int
    199 linux_sys_lstat64(l, v, retval)
    200 	struct lwp *l;
    201 	void *v;
    202 	register_t *retval;
    203 {
    204 	struct linux_sys_lstat64_args /* {
    205 		syscallarg(const char *) path;
    206 		syscallarg(struct linux_stat64 *) sp;
    207 	} */ *uap = v;
    208 
    209 	return linux_do_stat64(l, uap, retval, 1);
    210 }
    211 
    212 int
    213 linux_sys_truncate64(l, v, retval)
    214 	struct lwp *l;
    215 	void *v;
    216 	register_t *retval;
    217 {
    218 	struct linux_sys_truncate64_args /* {
    219 		syscallarg(const char *) path;
    220 		syscallarg(off_t) length;
    221 	} */ *uap = v;
    222 	struct proc *p = l->l_proc;
    223 	caddr_t sg = stackgap_init(p->p_emul);
    224 
    225 	CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    226 
    227 	return sys_truncate(l, uap, retval);
    228 }
    229 
    230 #ifdef __mips__ /* i386 and powerpc could use it too */
    231 int
    232 linux_sys_fcntl64(p, v, retval)
    233 	struct proc *p;
    234 	void *v;
    235 	register_t *retval;
    236 {
    237 	struct linux_sys_fcntl64_args /* {
    238 		syscallarg(unsigned int) fd;
    239 		syscallarg(unsigned int) cmd;
    240 		syscallarg(unsigned long) arg;
    241 	} */ *uap = v;
    242 	unsigned int fd, cmd;
    243 	unsigned long arg;
    244 	int error;
    245 
    246 	fd = SCARG(uap, fd);
    247 	cmd = SCARG(uap, cmd);
    248 	arg = SCARG(uap, arg);
    249 
    250 	switch (cmd) {
    251 		/* XXX implement this later */
    252 		case LINUX_F_GETLK64:
    253 			error = 0;
    254 			break;
    255 		case LINUX_F_SETLK64:
    256 			error = 0;
    257 			break;
    258 		case LINUX_F_SETLKW64:
    259 			error = 0;
    260 			break;
    261 		default:
    262 			error = linux_sys_fcntl(p, v, retval);
    263 			break;
    264 	}
    265 
    266 	return error;
    267 }
    268 #endif /* __mips__ */
    269